/src/mozilla-central/dom/fetch/FetchStreamReader.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "FetchStreamReader.h" |
8 | | #include "InternalResponse.h" |
9 | | #include "mozilla/dom/PromiseBinding.h" |
10 | | #include "mozilla/SystemGroup.h" |
11 | | #include "mozilla/TaskCategory.h" |
12 | | #include "nsContentUtils.h" |
13 | | #include "nsIScriptError.h" |
14 | | #include "nsPIDOMWindow.h" |
15 | | |
16 | | namespace mozilla { |
17 | | namespace dom { |
18 | | |
19 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(FetchStreamReader) |
20 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(FetchStreamReader) |
21 | | |
22 | | NS_IMPL_CYCLE_COLLECTION_CLASS(FetchStreamReader) |
23 | | |
24 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(FetchStreamReader) |
25 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal) |
26 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
27 | | |
28 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(FetchStreamReader) |
29 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal) |
30 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
31 | | |
32 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(FetchStreamReader) |
33 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mReader) |
34 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_END |
35 | | |
36 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FetchStreamReader) |
37 | 0 | NS_INTERFACE_MAP_ENTRY(nsIOutputStreamCallback) |
38 | 0 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIOutputStreamCallback) |
39 | 0 | NS_INTERFACE_MAP_END |
40 | | |
41 | | /* static */ nsresult |
42 | | FetchStreamReader::Create(JSContext* aCx, nsIGlobalObject* aGlobal, |
43 | | FetchStreamReader** aStreamReader, |
44 | | nsIInputStream** aInputStream) |
45 | 0 | { |
46 | 0 | MOZ_ASSERT(aCx); |
47 | 0 | MOZ_ASSERT(aGlobal); |
48 | 0 | MOZ_ASSERT(aStreamReader); |
49 | 0 | MOZ_ASSERT(aInputStream); |
50 | 0 |
|
51 | 0 | RefPtr<FetchStreamReader> streamReader = new FetchStreamReader(aGlobal); |
52 | 0 |
|
53 | 0 | nsCOMPtr<nsIAsyncInputStream> pipeIn; |
54 | 0 |
|
55 | 0 | nsresult rv = NS_NewPipe2(getter_AddRefs(pipeIn), |
56 | 0 | getter_AddRefs(streamReader->mPipeOut), |
57 | 0 | true, true, 0, 0); |
58 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
59 | 0 | return rv; |
60 | 0 | } |
61 | 0 | |
62 | 0 | if (!NS_IsMainThread()) { |
63 | 0 | WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx); |
64 | 0 | MOZ_ASSERT(workerPrivate); |
65 | 0 |
|
66 | 0 | RefPtr<WeakWorkerRef> workerRef = |
67 | 0 | WeakWorkerRef::Create(workerPrivate, [streamReader]() { |
68 | 0 | // The WorkerPrivate does have a context available, and we could pass |
69 | 0 | // it here to trigger cancellation of the reader, but the author of |
70 | 0 | // this comment chickened out. |
71 | 0 | streamReader->CloseAndRelease(nullptr, NS_ERROR_DOM_INVALID_STATE_ERR); |
72 | 0 | }); |
73 | 0 |
|
74 | 0 | if (NS_WARN_IF(!workerRef)) { |
75 | 0 | streamReader->mPipeOut->CloseWithStatus(NS_ERROR_DOM_INVALID_STATE_ERR); |
76 | 0 | return NS_ERROR_DOM_INVALID_STATE_ERR; |
77 | 0 | } |
78 | 0 | |
79 | 0 | // These 2 objects create a ref-cycle here that is broken when the stream is |
80 | 0 | // closed or the worker shutsdown. |
81 | 0 | streamReader->mWorkerRef = workerRef.forget(); |
82 | 0 | } |
83 | 0 |
|
84 | 0 | pipeIn.forget(aInputStream); |
85 | 0 | streamReader.forget(aStreamReader); |
86 | 0 | return NS_OK; |
87 | 0 | } |
88 | | |
89 | | FetchStreamReader::FetchStreamReader(nsIGlobalObject* aGlobal) |
90 | | : mGlobal(aGlobal) |
91 | | , mOwningEventTarget(mGlobal->EventTargetFor(TaskCategory::Other)) |
92 | | , mBufferRemaining(0) |
93 | | , mBufferOffset(0) |
94 | | , mStreamClosed(false) |
95 | 0 | { |
96 | 0 | MOZ_ASSERT(aGlobal); |
97 | 0 | } |
98 | | |
99 | | FetchStreamReader::~FetchStreamReader() |
100 | 0 | { |
101 | 0 | CloseAndRelease(nullptr, NS_BASE_STREAM_CLOSED); |
102 | 0 | } |
103 | | |
104 | | // If a context is provided, an attempt will be made to cancel the reader. The |
105 | | // only situation where we don't expect to have a context is when closure is |
106 | | // being triggered from the destructor or the WorkerRef is notifying. If |
107 | | // we're at the destructor, it's far too late to cancel anything. And if the |
108 | | // WorkerRef is being notified, the global is going away, so there's also |
109 | | // no need to do further JS work. |
110 | | void |
111 | | FetchStreamReader::CloseAndRelease(JSContext* aCx, nsresult aStatus) |
112 | 0 | { |
113 | 0 | NS_ASSERT_OWNINGTHREAD(FetchStreamReader); |
114 | 0 |
|
115 | 0 | if (mStreamClosed) { |
116 | 0 | // Already closed. |
117 | 0 | return; |
118 | 0 | } |
119 | 0 | |
120 | 0 | RefPtr<FetchStreamReader> kungFuDeathGrip = this; |
121 | 0 |
|
122 | 0 | if (aCx) { |
123 | 0 | MOZ_ASSERT(mReader); |
124 | 0 |
|
125 | 0 | RefPtr<DOMException> error = DOMException::Create(aStatus); |
126 | 0 |
|
127 | 0 | JS::Rooted<JS::Value> errorValue(aCx); |
128 | 0 | if (ToJSValue(aCx, error, &errorValue)) { |
129 | 0 | JS::Rooted<JSObject*> reader(aCx, mReader); |
130 | 0 | // It's currently safe to cancel an already closed reader because, per the |
131 | 0 | // comments in ReadableStream::cancel() conveying the spec, step 2 of |
132 | 0 | // 3.4.3 that specified ReadableStreamCancel is: If stream.[[state]] is |
133 | 0 | // "closed", return a new promise resolved with undefined. |
134 | 0 | JS::ReadableStreamReaderCancel(aCx, reader, errorValue); |
135 | 0 | } |
136 | 0 | } |
137 | 0 |
|
138 | 0 | mStreamClosed = true; |
139 | 0 |
|
140 | 0 | mGlobal = nullptr; |
141 | 0 |
|
142 | 0 | mPipeOut->CloseWithStatus(aStatus); |
143 | 0 | mPipeOut = nullptr; |
144 | 0 |
|
145 | 0 | mWorkerRef = nullptr; |
146 | 0 |
|
147 | 0 | mReader = nullptr; |
148 | 0 | mBuffer = nullptr; |
149 | 0 | } |
150 | | |
151 | | void |
152 | | FetchStreamReader::StartConsuming(JSContext* aCx, |
153 | | JS::HandleObject aStream, |
154 | | JS::MutableHandle<JSObject*> aReader, |
155 | | ErrorResult& aRv) |
156 | 0 | { |
157 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mReader); |
158 | 0 | MOZ_DIAGNOSTIC_ASSERT(aStream); |
159 | 0 |
|
160 | 0 | JS::Rooted<JSObject*> reader(aCx, |
161 | 0 | JS::ReadableStreamGetReader(aCx, aStream, |
162 | 0 | JS::ReadableStreamReaderMode::Default)); |
163 | 0 | if (!reader) { |
164 | 0 | aRv.StealExceptionFromJSContext(aCx); |
165 | 0 | CloseAndRelease(aCx, NS_ERROR_DOM_INVALID_STATE_ERR); |
166 | 0 | return; |
167 | 0 | } |
168 | 0 | |
169 | 0 | mReader = reader; |
170 | 0 | aReader.set(reader); |
171 | 0 |
|
172 | 0 | aRv = mPipeOut->AsyncWait(this, 0, 0, mOwningEventTarget); |
173 | 0 | if (NS_WARN_IF(aRv.Failed())) { |
174 | 0 | return; |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | | // nsIOutputStreamCallback interface |
179 | | |
180 | | NS_IMETHODIMP |
181 | | FetchStreamReader::OnOutputStreamReady(nsIAsyncOutputStream* aStream) |
182 | 0 | { |
183 | 0 | NS_ASSERT_OWNINGTHREAD(FetchStreamReader); |
184 | 0 | MOZ_ASSERT(aStream == mPipeOut); |
185 | 0 | MOZ_ASSERT(mReader); |
186 | 0 |
|
187 | 0 | if (mStreamClosed) { |
188 | 0 | return NS_OK; |
189 | 0 | } |
190 | 0 | |
191 | 0 | if (mBuffer) { |
192 | 0 | return WriteBuffer(); |
193 | 0 | } |
194 | 0 | |
195 | 0 | // TODO: We need to verify this is the correct global per the spec. |
196 | 0 | // See bug 1385890. |
197 | 0 | AutoEntryScript aes(mGlobal, "ReadableStreamReader.read", !mWorkerRef); |
198 | 0 |
|
199 | 0 | JS::Rooted<JSObject*> reader(aes.cx(), mReader); |
200 | 0 | JS::Rooted<JSObject*> promise(aes.cx(), |
201 | 0 | JS::ReadableStreamDefaultReaderRead(aes.cx(), |
202 | 0 | reader)); |
203 | 0 | if (NS_WARN_IF(!promise)) { |
204 | 0 | // Let's close the stream. |
205 | 0 | CloseAndRelease(aes.cx(), NS_ERROR_DOM_INVALID_STATE_ERR); |
206 | 0 | return NS_ERROR_FAILURE; |
207 | 0 | } |
208 | 0 | |
209 | 0 | RefPtr<Promise> domPromise = Promise::CreateFromExisting(mGlobal, promise); |
210 | 0 | if (NS_WARN_IF(!domPromise)) { |
211 | 0 | // Let's close the stream. |
212 | 0 | CloseAndRelease(aes.cx(), NS_ERROR_DOM_INVALID_STATE_ERR); |
213 | 0 | return NS_ERROR_FAILURE; |
214 | 0 | } |
215 | 0 | |
216 | 0 | // Let's wait. |
217 | 0 | domPromise->AppendNativeHandler(this); |
218 | 0 | return NS_OK; |
219 | 0 | } |
220 | | |
221 | | void |
222 | | FetchStreamReader::ResolvedCallback(JSContext* aCx, |
223 | | JS::Handle<JS::Value> aValue) |
224 | 0 | { |
225 | 0 | if (mStreamClosed) { |
226 | 0 | return; |
227 | 0 | } |
228 | 0 | |
229 | 0 | // This promise should be resolved with { done: boolean, value: something }, |
230 | 0 | // "value" is interesting only if done is false. |
231 | 0 | |
232 | 0 | // We don't want to play with JS api, let's WebIDL bindings doing it for us. |
233 | 0 | // FetchReadableStreamReadDataDone is a dictionary with just a boolean, if the |
234 | 0 | // parsing succeeded, we can proceed with the parsing of the "value", which it |
235 | 0 | // must be a Uint8Array. |
236 | 0 | FetchReadableStreamReadDataDone valueDone; |
237 | 0 | if (!valueDone.Init(aCx, aValue)) { |
238 | 0 | JS_ClearPendingException(aCx); |
239 | 0 | CloseAndRelease(aCx, NS_ERROR_DOM_INVALID_STATE_ERR); |
240 | 0 | return; |
241 | 0 | } |
242 | 0 | |
243 | 0 | if (valueDone.mDone) { |
244 | 0 | // Stream is completed. |
245 | 0 | CloseAndRelease(aCx, NS_BASE_STREAM_CLOSED); |
246 | 0 | return; |
247 | 0 | } |
248 | 0 | |
249 | 0 | UniquePtr<FetchReadableStreamReadDataArray> value( |
250 | 0 | new FetchReadableStreamReadDataArray); |
251 | 0 | if (!value->Init(aCx, aValue) || !value->mValue.WasPassed()) { |
252 | 0 | JS_ClearPendingException(aCx); |
253 | 0 | CloseAndRelease(aCx, NS_ERROR_DOM_INVALID_STATE_ERR); |
254 | 0 | return; |
255 | 0 | } |
256 | 0 | |
257 | 0 | Uint8Array& array = value->mValue.Value(); |
258 | 0 | array.ComputeLengthAndData(); |
259 | 0 | uint32_t len = array.Length(); |
260 | 0 |
|
261 | 0 | if (len == 0) { |
262 | 0 | // If there is nothing to read, let's do another reading. |
263 | 0 | OnOutputStreamReady(mPipeOut); |
264 | 0 | return; |
265 | 0 | } |
266 | 0 | |
267 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mBuffer); |
268 | 0 | mBuffer = std::move(value); |
269 | 0 |
|
270 | 0 | mBufferOffset = 0; |
271 | 0 | mBufferRemaining = len; |
272 | 0 |
|
273 | 0 | nsresult rv = WriteBuffer(); |
274 | 0 | if (NS_FAILED(rv)) { |
275 | 0 | // DOMException only understands errors from domerr.msg, so we normalize to |
276 | 0 | // identifying an abort if the write fails. |
277 | 0 | CloseAndRelease(aCx, NS_ERROR_DOM_ABORT_ERR); |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | | nsresult |
282 | | FetchStreamReader::WriteBuffer() |
283 | 0 | { |
284 | 0 | MOZ_ASSERT(mBuffer); |
285 | 0 | MOZ_ASSERT(mBuffer->mValue.WasPassed()); |
286 | 0 |
|
287 | 0 | Uint8Array& array = mBuffer->mValue.Value(); |
288 | 0 | char* data = reinterpret_cast<char*>(array.Data()); |
289 | 0 |
|
290 | 0 | while (1) { |
291 | 0 | uint32_t written = 0; |
292 | 0 | nsresult rv = |
293 | 0 | mPipeOut->Write(data + mBufferOffset, mBufferRemaining, &written); |
294 | 0 |
|
295 | 0 | if (rv == NS_BASE_STREAM_WOULD_BLOCK) { |
296 | 0 | break; |
297 | 0 | } |
298 | 0 | |
299 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
300 | 0 | return rv; |
301 | 0 | } |
302 | 0 | |
303 | 0 | MOZ_ASSERT(written <= mBufferRemaining); |
304 | 0 | mBufferRemaining -= written; |
305 | 0 | mBufferOffset += written; |
306 | 0 |
|
307 | 0 | if (mBufferRemaining == 0) { |
308 | 0 | mBuffer = nullptr; |
309 | 0 | break; |
310 | 0 | } |
311 | 0 | } |
312 | 0 |
|
313 | 0 | nsresult rv = mPipeOut->AsyncWait(this, 0, 0, mOwningEventTarget); |
314 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
315 | 0 | return rv; |
316 | 0 | } |
317 | 0 | |
318 | 0 | return NS_OK; |
319 | 0 | } |
320 | | |
321 | | void |
322 | | FetchStreamReader::RejectedCallback(JSContext* aCx, |
323 | | JS::Handle<JS::Value> aValue) |
324 | 0 | { |
325 | 0 | ReportErrorToConsole(aCx, aValue); |
326 | 0 | CloseAndRelease(aCx, NS_ERROR_FAILURE); |
327 | 0 | } |
328 | | |
329 | | void |
330 | | FetchStreamReader::ReportErrorToConsole(JSContext* aCx, |
331 | | JS::Handle<JS::Value> aValue) |
332 | 0 | { |
333 | 0 | nsCString sourceSpec; |
334 | 0 | uint32_t line = 0; |
335 | 0 | uint32_t column = 0; |
336 | 0 | nsString valueString; |
337 | 0 |
|
338 | 0 | nsContentUtils::ExtractErrorValues(aCx, aValue, sourceSpec, &line, |
339 | 0 | &column, valueString); |
340 | 0 |
|
341 | 0 | nsTArray<nsString> params; |
342 | 0 | params.AppendElement(valueString); |
343 | 0 |
|
344 | 0 | RefPtr<ConsoleReportCollector> reporter = new ConsoleReportCollector(); |
345 | 0 | reporter->AddConsoleReport(nsIScriptError::errorFlag, |
346 | 0 | NS_LITERAL_CSTRING("ReadableStreamReader.read"), |
347 | 0 | nsContentUtils::eDOM_PROPERTIES, |
348 | 0 | sourceSpec, line, column, |
349 | 0 | NS_LITERAL_CSTRING("ReadableStreamReadingFailed"), |
350 | 0 | params); |
351 | 0 |
|
352 | 0 | uint64_t innerWindowId = 0; |
353 | 0 |
|
354 | 0 | if (NS_IsMainThread()) { |
355 | 0 | nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mGlobal); |
356 | 0 | if (window) { |
357 | 0 | innerWindowId = window->WindowID(); |
358 | 0 | } |
359 | 0 | reporter->FlushReportsToConsole(innerWindowId); |
360 | 0 | return; |
361 | 0 | } |
362 | 0 |
|
363 | 0 | WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx); |
364 | 0 | if (workerPrivate) { |
365 | 0 | innerWindowId = workerPrivate->WindowID(); |
366 | 0 | } |
367 | 0 |
|
368 | 0 | RefPtr<Runnable> r = NS_NewRunnableFunction( |
369 | 0 | "FetchStreamReader::ReportErrorToConsole", |
370 | 0 | [reporter, innerWindowId] () { |
371 | 0 | reporter->FlushReportsToConsole(innerWindowId); |
372 | 0 | }); |
373 | 0 |
|
374 | 0 | workerPrivate->DispatchToMainThread(r.forget()); |
375 | 0 | } |
376 | | |
377 | | } // dom namespace |
378 | | } // mozilla namespace |