1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "WebVTTListener.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/HTMLTrackElement.h"
#include "mozilla/dom/TextTrackCue.h"
#include "mozilla/dom/TextTrackRegion.h"
#include "mozilla/dom/VTTRegionBinding.h"
#include "nsComponentManagerUtils.h"
#include "nsIAsyncVerifyRedirectCallback.h"
#include "nsIInputStream.h"
extern mozilla::LazyLogModule gTextTrackLog;
#define LOG(msg, ...) \
MOZ_LOG(gTextTrackLog, LogLevel::Debug, \
("WebVTTListener=%p, " msg, this, ##__VA_ARGS__))
#define LOG_WIHTOUT_ADDRESS(msg, ...) \
MOZ_LOG(gTextTrackLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
namespace mozilla::dom {
NS_IMPL_CYCLE_COLLECTION(WebVTTListener, mElement, mParserWrapper)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebVTTListener)
NS_INTERFACE_MAP_ENTRY(nsIWebVTTListener)
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
NS_INTERFACE_MAP_ENTRY(nsIChannelEventSink)
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebVTTListener)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(WebVTTListener)
NS_IMPL_CYCLE_COLLECTING_RELEASE(WebVTTListener)
WebVTTListener::WebVTTListener(HTMLTrackElement* aElement)
: mElement(aElement), mParserWrapperError(NS_OK) {
MOZ_ASSERT(mElement, "Must pass an element to the callback");
LOG("Created listener for track element %p", aElement);
MOZ_DIAGNOSTIC_ASSERT(
CycleCollectedJSContext::Get() &&
!CycleCollectedJSContext::Get()->IsInStableOrMetaStableState());
mParserWrapper = do_CreateInstance(NS_WEBVTTPARSERWRAPPER_CONTRACTID,
&mParserWrapperError);
if (NS_SUCCEEDED(mParserWrapperError)) {
nsPIDOMWindowInner* window = mElement->OwnerDoc()->GetInnerWindow();
mParserWrapperError = mParserWrapper->LoadParser(window);
}
if (NS_SUCCEEDED(mParserWrapperError)) {
mParserWrapperError = mParserWrapper->Watch(this);
}
}
WebVTTListener::~WebVTTListener() { LOG("destroyed."); }
NS_IMETHODIMP
WebVTTListener::GetInterface(const nsIID& aIID, void** aResult) {
return QueryInterface(aIID, aResult);
}
nsresult WebVTTListener::LoadResource() {
if (IsCanceled()) {
return NS_OK;
}
// Exit if we failed to create the WebVTTParserWrapper (vtt.jsm)
NS_ENSURE_SUCCESS(mParserWrapperError, mParserWrapperError);
mElement->SetReadyState(TextTrackReadyState::Loading);
return NS_OK;
}
NS_IMETHODIMP
WebVTTListener::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
nsIChannel* aNewChannel, uint32_t aFlags,
nsIAsyncVerifyRedirectCallback* cb) {
if (IsCanceled()) {
return NS_OK;
}
if (mElement) {
mElement->OnChannelRedirect(aOldChannel, aNewChannel, aFlags);
}
cb->OnRedirectVerifyCallback(NS_OK);
return NS_OK;
}
NS_IMETHODIMP
WebVTTListener::OnStartRequest(nsIRequest* aRequest) {
if (IsCanceled()) {
return NS_OK;
}
LOG("OnStartRequest");
mElement->DispatchTestEvent(u"mozStartedLoadingTextTrack"_ns);
return NS_OK;
}
NS_IMETHODIMP
WebVTTListener::OnStopRequest(nsIRequest* aRequest, nsresult aStatus) {
if (IsCanceled()) {
return NS_OK;
}
LOG("OnStopRequest");
if (NS_FAILED(aStatus)) {
LOG("Got error status");
mElement->SetReadyState(TextTrackReadyState::FailedToLoad);
}
// Attempt to parse any final data the parser might still have.
mParserWrapper->Flush();
if (mElement->ReadyState() != TextTrackReadyState::FailedToLoad) {
mElement->SetReadyState(TextTrackReadyState::Loaded);
}
mElement->CancelChannelAndListener();
return aStatus;
}
nsresult WebVTTListener::ParseChunk(nsIInputStream* aInStream, void* aClosure,
const char* aFromSegment,
uint32_t aToOffset, uint32_t aCount,
uint32_t* aWriteCount) {
nsCString buffer(aFromSegment, aCount);
WebVTTListener* listener = static_cast<WebVTTListener*>(aClosure);
MOZ_ASSERT(!listener->IsCanceled());
if (NS_FAILED(listener->mParserWrapper->Parse(buffer))) {
LOG_WIHTOUT_ADDRESS(
"WebVTTListener=%p, Unable to parse chunk of WEBVTT text. Aborting.",
listener);
*aWriteCount = 0;
return NS_ERROR_FAILURE;
}
*aWriteCount = aCount;
return NS_OK;
}
NS_IMETHODIMP
WebVTTListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aStream,
uint64_t aOffset, uint32_t aCount) {
if (IsCanceled()) {
return NS_OK;
}
LOG("OnDataAvailable");
uint32_t count = aCount;
while (count > 0) {
uint32_t read;
nsresult rv = aStream->ReadSegments(ParseChunk, this, count, &read);
NS_ENSURE_SUCCESS(rv, rv);
if (!read) {
return NS_ERROR_FAILURE;
}
count -= read;
}
return NS_OK;
}
NS_IMETHODIMP
WebVTTListener::OnCue(JS::Handle<JS::Value> aCue, JSContext* aCx) {
MOZ_ASSERT(!IsCanceled());
if (!aCue.isObject()) {
return NS_ERROR_FAILURE;
}
JS::Rooted<JSObject*> obj(aCx, &aCue.toObject());
TextTrackCue* cue = nullptr;
nsresult rv = UNWRAP_OBJECT(VTTCue, &obj, cue);
NS_ENSURE_SUCCESS(rv, rv);
cue->SetTrackElement(mElement);
mElement->mTrack->AddCue(*cue);
return NS_OK;
}
NS_IMETHODIMP
WebVTTListener::OnRegion(JS::Handle<JS::Value> aRegion, JSContext* aCx) {
MOZ_ASSERT(!IsCanceled());
// Nothing for this callback to do.
return NS_OK;
}
NS_IMETHODIMP
WebVTTListener::OnParsingError(int32_t errorCode, JSContext* cx) {
MOZ_ASSERT(!IsCanceled());
// We only care about files that have a bad WebVTT file signature right now
// as that means the file failed to load.
if (errorCode == ErrorCodes::BadSignature) {
LOG("parsing error");
mElement->SetReadyState(TextTrackReadyState::FailedToLoad);
}
return NS_OK;
}
bool WebVTTListener::IsCanceled() const { return mCancel; }
void WebVTTListener::Cancel() {
MOZ_ASSERT(!IsCanceled(), "Do not cancel canceled listener again!");
LOG("Cancel listen to channel's response.");
mCancel = true;
mParserWrapper->Cancel();
mParserWrapper = nullptr;
mElement = nullptr;
}
} // namespace mozilla::dom
|