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" |
19 #include "wtf/PassRefPtr.h" | 22 #include "wtf/PassRefPtr.h" |
20 #include "wtf/RefPtr.h" | 23 #include "wtf/RefPtr.h" |
tyoshino (SeeGerritForStatus)
2016/04/04 09:21:30
OwnPtr.h
WebDataConsumerHandle.h
yhirano
2016/04/04 09:28:20
Done.
| |
21 | 24 |
22 namespace blink { | 25 namespace blink { |
23 | 26 |
24 namespace { | 27 namespace { |
25 | 28 |
26 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, pub lic FetchDataLoader::Client { | 29 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, pub lic FetchDataLoader::Client { |
27 WTF_MAKE_NONCOPYABLE(BodyConsumerBase); | 30 WTF_MAKE_NONCOPYABLE(BodyConsumerBase); |
28 USING_GARBAGE_COLLECTED_MIXIN(BodyConsumerBase); | 31 USING_GARBAGE_COLLECTED_MIXIN(BodyConsumerBase); |
29 public: | 32 public: |
30 explicit BodyConsumerBase(ScriptPromiseResolver* resolver) : m_resolver(reso lver) {} | 33 explicit BodyConsumerBase(ScriptPromiseResolver* resolver) : m_resolver(reso lver) {} |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 v8::Local<v8::String> inputString = v8String(isolate, string); | 95 v8::Local<v8::String> inputString = v8String(isolate, string); |
93 v8::TryCatch trycatch(isolate); | 96 v8::TryCatch trycatch(isolate); |
94 v8::Local<v8::Value> parsed; | 97 v8::Local<v8::Value> parsed; |
95 if (v8Call(v8::JSON::Parse(isolate, inputString), parsed, trycatch)) | 98 if (v8Call(v8::JSON::Parse(isolate, inputString), parsed, trycatch)) |
96 resolver()->resolve(parsed); | 99 resolver()->resolve(parsed); |
97 else | 100 else |
98 resolver()->reject(trycatch.Exception()); | 101 resolver()->reject(trycatch.Exception()); |
99 } | 102 } |
100 }; | 103 }; |
101 | 104 |
105 class UnderlyingSourceFromDataConsumerHandle final : public UnderlyingSourceBase , public WebDataConsumerHandle::Client { | |
106 EAGERLY_FINALIZE(); | |
107 DECLARE_EAGER_FINALIZATION_OPERATOR_NEW(); | |
tyoshino (SeeGerritForStatus)
2016/04/04 09:21:30
Could you please point me to some doc by which I c
yhirano
2016/04/04 09:28:20
I don't know much about it either. I got an assert
| |
108 public: | |
109 UnderlyingSourceFromDataConsumerHandle(ScriptState* scriptState, PassOwnPtr< WebDataConsumerHandle> handle) | |
110 : UnderlyingSourceBase(scriptState) | |
111 , m_scriptState(scriptState) | |
112 , m_reader(handle->obtainReader(this)) | |
113 { | |
114 } | |
115 | |
116 ScriptPromise pull(ScriptState* scriptState) override | |
117 { | |
118 didGetReadable(); | |
119 return ScriptPromise::castUndefined(scriptState); | |
120 } | |
121 | |
122 ScriptPromise cancel(ScriptState* scriptState, ScriptValue reason) override | |
123 { | |
124 m_reader = nullptr; | |
125 return ScriptPromise::castUndefined(scriptState); | |
126 } | |
127 | |
128 void didGetReadable() override | |
129 { | |
130 while (controller()->desiredSize() > 0) { | |
131 const auto kNone = WebDataConsumerHandle::FlagNone; | |
tyoshino (SeeGerritForStatus)
2016/04/04 09:21:30
kNone is used just once. how about just putting th
yhirano
2016/04/04 09:28:20
Done.
| |
132 size_t available; | |
133 const void* buffer = nullptr; | |
134 WebDataConsumerHandle::Result result = m_reader->beginRead(&buffer, kNone, &available); | |
135 if (result == WebDataConsumerHandle::Ok) { | |
136 RefPtr<ArrayBuffer> arrayBuffer = ArrayBuffer::create(available, 1); | |
137 memcpy(arrayBuffer->data(), buffer, available); | |
138 m_reader->endRead(available); | |
139 controller()->enqueue(DOMUint8Array::create(arrayBuffer.release( ), 0, available)); | |
140 } else if (result == WebDataConsumerHandle::ShouldWait) { | |
141 break; | |
142 } else if (result == WebDataConsumerHandle::Done) { | |
143 m_reader = nullptr; | |
144 controller()->close(); | |
145 break; | |
146 } else { | |
147 m_reader = nullptr; | |
148 controller()->error(V8ThrowException::createTypeError(m_scriptSt ate->isolate(), "Network error")); | |
149 break; | |
150 } | |
151 } | |
152 } | |
153 | |
154 private: | |
155 RefPtr<ScriptState> m_scriptState; | |
156 OwnPtr<WebDataConsumerHandle::Reader> m_reader; | |
157 }; | |
158 | |
102 } // namespace | 159 } // namespace |
103 | 160 |
104 ScriptPromise Body::arrayBuffer(ScriptState* scriptState) | 161 ScriptPromise Body::arrayBuffer(ScriptState* scriptState) |
105 { | 162 { |
106 ScriptPromise promise = rejectInvalidConsumption(scriptState); | 163 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
107 if (!promise.isEmpty()) | 164 if (!promise.isEmpty()) |
108 return promise; | 165 return promise; |
109 | 166 |
110 // When the main thread sends a V8::TerminateExecution() signal to a worker | 167 // 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 | 168 // 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 { | 250 { |
194 return bodyBuffer() ? bodyBuffer()->stream() : nullptr; | 251 return bodyBuffer() ? bodyBuffer()->stream() : nullptr; |
195 } | 252 } |
196 | 253 |
197 ReadableByteStream* Body::bodyWithUseCounter() | 254 ReadableByteStream* Body::bodyWithUseCounter() |
198 { | 255 { |
199 UseCounter::count(getExecutionContext(), UseCounter::FetchBodyStream); | 256 UseCounter::count(getExecutionContext(), UseCounter::FetchBodyStream); |
200 return body(); | 257 return body(); |
201 } | 258 } |
202 | 259 |
260 ScriptValue Body::v8ExtraStreamBody(ScriptState* scriptState) | |
261 { | |
262 if (bodyUsed() || isBodyLocked() || !bodyBuffer()) | |
263 return ScriptValue(scriptState, v8::Undefined(scriptState->isolate())); | |
264 OwnPtr<FetchDataConsumerHandle> handle = bodyBuffer()->releaseHandle(scriptS tate->getExecutionContext()); | |
265 return ReadableStreamOperations::createReadableStream(scriptState, | |
266 new UnderlyingSourceFromDataConsumerHandle(scriptState, handle.release() ), | |
267 ScriptValue(scriptState, v8::Undefined(scriptState->isolate()))); | |
268 } | |
269 | |
203 bool Body::bodyUsed() | 270 bool Body::bodyUsed() |
204 { | 271 { |
205 return body() && body()->isDisturbed(); | 272 return body() && body()->isDisturbed(); |
206 } | 273 } |
207 | 274 |
208 bool Body::isBodyLocked() | 275 bool Body::isBodyLocked() |
209 { | 276 { |
210 return body() && body()->isLocked(); | 277 return body() && body()->isLocked(); |
211 } | 278 } |
212 | 279 |
(...skipping 17 matching lines...) Expand all Loading... | |
230 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) | 297 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) |
231 { | 298 { |
232 if (m_opaque) | 299 if (m_opaque) |
233 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "The body is opaque.")); | 300 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "The body is opaque.")); |
234 if (isBodyLocked() || bodyUsed()) | 301 if (isBodyLocked() || bodyUsed()) |
235 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); | 302 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); |
236 return ScriptPromise(); | 303 return ScriptPromise(); |
237 } | 304 } |
238 | 305 |
239 } // namespace blink | 306 } // namespace blink |
OLD | NEW |