OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/fetch/Body.h" | 5 #include "modules/fetch/Body.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
10 #include "bindings/core/v8/V8ArrayBuffer.h" | 10 #include "bindings/core/v8/V8ArrayBuffer.h" |
11 #include "bindings/core/v8/V8ThrowException.h" | 11 #include "bindings/core/v8/V8ThrowException.h" |
12 #include "core/dom/DOMArrayBuffer.h" | 12 #include "core/dom/DOMArrayBuffer.h" |
13 #include "core/dom/DOMTypedArray.h" | 13 #include "core/dom/DOMTypedArray.h" |
14 #include "core/dom/ExceptionCode.h" | 14 #include "core/dom/ExceptionCode.h" |
15 #include "core/fileapi/Blob.h" | 15 #include "core/fileapi/Blob.h" |
16 #include "core/frame/UseCounter.h" | 16 #include "core/frame/UseCounter.h" |
| 17 #include "core/streams/ReadableStreamController.h" |
| 18 #include "core/streams/ReadableStreamOperations.h" |
| 19 #include "core/streams/UnderlyingSourceBase.h" |
17 #include "modules/fetch/BodyStreamBuffer.h" | 20 #include "modules/fetch/BodyStreamBuffer.h" |
18 #include "modules/fetch/FetchDataLoader.h" | 21 #include "modules/fetch/FetchDataLoader.h" |
| 22 #include "public/platform/WebDataConsumerHandle.h" |
| 23 #include "wtf/OwnPtr.h" |
19 #include "wtf/PassRefPtr.h" | 24 #include "wtf/PassRefPtr.h" |
20 #include "wtf/RefPtr.h" | 25 #include "wtf/RefPtr.h" |
21 | 26 |
22 namespace blink { | 27 namespace blink { |
23 | 28 |
24 namespace { | 29 namespace { |
25 | 30 |
26 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, pub
lic FetchDataLoader::Client { | 31 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, pub
lic FetchDataLoader::Client { |
27 WTF_MAKE_NONCOPYABLE(BodyConsumerBase); | 32 WTF_MAKE_NONCOPYABLE(BodyConsumerBase); |
28 USING_GARBAGE_COLLECTED_MIXIN(BodyConsumerBase); | 33 USING_GARBAGE_COLLECTED_MIXIN(BodyConsumerBase); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 v8::Local<v8::String> inputString = v8String(isolate, string); | 97 v8::Local<v8::String> inputString = v8String(isolate, string); |
93 v8::TryCatch trycatch(isolate); | 98 v8::TryCatch trycatch(isolate); |
94 v8::Local<v8::Value> parsed; | 99 v8::Local<v8::Value> parsed; |
95 if (v8Call(v8::JSON::Parse(isolate, inputString), parsed, trycatch)) | 100 if (v8Call(v8::JSON::Parse(isolate, inputString), parsed, trycatch)) |
96 resolver()->resolve(parsed); | 101 resolver()->resolve(parsed); |
97 else | 102 else |
98 resolver()->reject(trycatch.Exception()); | 103 resolver()->reject(trycatch.Exception()); |
99 } | 104 } |
100 }; | 105 }; |
101 | 106 |
| 107 class UnderlyingSourceFromDataConsumerHandle final : public UnderlyingSourceBase
, public WebDataConsumerHandle::Client { |
| 108 EAGERLY_FINALIZE(); |
| 109 DECLARE_EAGER_FINALIZATION_OPERATOR_NEW(); |
| 110 public: |
| 111 UnderlyingSourceFromDataConsumerHandle(ScriptState* scriptState, PassOwnPtr<
WebDataConsumerHandle> handle) |
| 112 : UnderlyingSourceBase(scriptState) |
| 113 , m_scriptState(scriptState) |
| 114 , m_reader(handle->obtainReader(this)) |
| 115 { |
| 116 } |
| 117 |
| 118 ScriptPromise pull(ScriptState* scriptState) override |
| 119 { |
| 120 didGetReadable(); |
| 121 return ScriptPromise::castUndefined(scriptState); |
| 122 } |
| 123 |
| 124 ScriptPromise cancel(ScriptState* scriptState, ScriptValue reason) override |
| 125 { |
| 126 m_reader = nullptr; |
| 127 return ScriptPromise::castUndefined(scriptState); |
| 128 } |
| 129 |
| 130 void didGetReadable() override |
| 131 { |
| 132 while (controller()->desiredSize() > 0) { |
| 133 size_t available = 0; |
| 134 const void* buffer = nullptr; |
| 135 WebDataConsumerHandle::Result result = m_reader->beginRead(&buffer,
WebDataConsumerHandle::FlagNone, &available); |
| 136 if (result == WebDataConsumerHandle::Ok) { |
| 137 RefPtr<ArrayBuffer> arrayBuffer = ArrayBuffer::create(available,
1); |
| 138 memcpy(arrayBuffer->data(), buffer, available); |
| 139 m_reader->endRead(available); |
| 140 controller()->enqueue(DOMUint8Array::create(arrayBuffer.release(
), 0, available)); |
| 141 } else if (result == WebDataConsumerHandle::ShouldWait) { |
| 142 break; |
| 143 } else if (result == WebDataConsumerHandle::Done) { |
| 144 m_reader = nullptr; |
| 145 controller()->close(); |
| 146 break; |
| 147 } else { |
| 148 m_reader = nullptr; |
| 149 controller()->error(V8ThrowException::createTypeError(m_scriptSt
ate->isolate(), "Network error")); |
| 150 break; |
| 151 } |
| 152 } |
| 153 } |
| 154 |
| 155 private: |
| 156 RefPtr<ScriptState> m_scriptState; |
| 157 OwnPtr<WebDataConsumerHandle::Reader> m_reader; |
| 158 }; |
| 159 |
102 } // namespace | 160 } // namespace |
103 | 161 |
104 ScriptPromise Body::arrayBuffer(ScriptState* scriptState) | 162 ScriptPromise Body::arrayBuffer(ScriptState* scriptState) |
105 { | 163 { |
106 ScriptPromise promise = rejectInvalidConsumption(scriptState); | 164 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
107 if (!promise.isEmpty()) | 165 if (!promise.isEmpty()) |
108 return promise; | 166 return promise; |
109 | 167 |
110 // When the main thread sends a V8::TerminateExecution() signal to a worker | 168 // When the main thread sends a V8::TerminateExecution() signal to a worker |
111 // thread, any V8 API on the worker thread starts returning an empty | 169 // thread, any V8 API on the worker thread starts returning an empty |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 { | 251 { |
194 return bodyBuffer() ? bodyBuffer()->stream() : nullptr; | 252 return bodyBuffer() ? bodyBuffer()->stream() : nullptr; |
195 } | 253 } |
196 | 254 |
197 ReadableByteStream* Body::bodyWithUseCounter() | 255 ReadableByteStream* Body::bodyWithUseCounter() |
198 { | 256 { |
199 UseCounter::count(getExecutionContext(), UseCounter::FetchBodyStream); | 257 UseCounter::count(getExecutionContext(), UseCounter::FetchBodyStream); |
200 return body(); | 258 return body(); |
201 } | 259 } |
202 | 260 |
| 261 ScriptValue Body::v8ExtraStreamBody(ScriptState* scriptState) |
| 262 { |
| 263 if (bodyUsed() || isBodyLocked() || !bodyBuffer()) |
| 264 return ScriptValue(scriptState, v8::Undefined(scriptState->isolate())); |
| 265 OwnPtr<FetchDataConsumerHandle> handle = bodyBuffer()->releaseHandle(scriptS
tate->getExecutionContext()); |
| 266 return ReadableStreamOperations::createReadableStream(scriptState, |
| 267 new UnderlyingSourceFromDataConsumerHandle(scriptState, handle.release()
), |
| 268 ScriptValue(scriptState, v8::Undefined(scriptState->isolate()))); |
| 269 } |
| 270 |
203 bool Body::bodyUsed() | 271 bool Body::bodyUsed() |
204 { | 272 { |
205 return body() && body()->isDisturbed(); | 273 return body() && body()->isDisturbed(); |
206 } | 274 } |
207 | 275 |
208 bool Body::isBodyLocked() | 276 bool Body::isBodyLocked() |
209 { | 277 { |
210 return body() && body()->isLocked(); | 278 return body() && body()->isLocked(); |
211 } | 279 } |
212 | 280 |
(...skipping 17 matching lines...) Expand all Loading... |
230 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) | 298 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) |
231 { | 299 { |
232 if (m_opaque) | 300 if (m_opaque) |
233 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "The body is opaque.")); | 301 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "The body is opaque.")); |
234 if (isBodyLocked() || bodyUsed()) | 302 if (isBodyLocked() || bodyUsed()) |
235 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 303 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); |
236 return ScriptPromise(); | 304 return ScriptPromise(); |
237 } | 305 } |
238 | 306 |
239 } // namespace blink | 307 } // namespace blink |
OLD | NEW |