Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: third_party/WebKit/Source/modules/fetch/Response.cpp

Issue 2780693003: [wasm] response-based compile APIs (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/fetch/Response.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/Response.h" 5 #include "modules/fetch/Response.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include "bindings/core/v8/Dictionary.h" 8 #include "bindings/core/v8/Dictionary.h"
9 #include "bindings/core/v8/ExceptionState.h" 9 #include "bindings/core/v8/ExceptionState.h"
10 #include "bindings/core/v8/ScriptState.h" 10 #include "bindings/core/v8/ScriptState.h"
11 #include "bindings/core/v8/V8ArrayBuffer.h" 11 #include "bindings/core/v8/V8ArrayBuffer.h"
12 #include "bindings/core/v8/V8ArrayBufferView.h" 12 #include "bindings/core/v8/V8ArrayBufferView.h"
13 #include "bindings/core/v8/V8Binding.h" 13 #include "bindings/core/v8/V8Binding.h"
14 #include "bindings/core/v8/V8Blob.h" 14 #include "bindings/core/v8/V8Blob.h"
15 #include "bindings/core/v8/V8FormData.h" 15 #include "bindings/core/v8/V8FormData.h"
16 #include "bindings/core/v8/V8HiddenValue.h" 16 #include "bindings/core/v8/V8HiddenValue.h"
17 #include "bindings/core/v8/V8URLSearchParams.h" 17 #include "bindings/core/v8/V8URLSearchParams.h"
18 #include "bindings/modules/v8/ByteStringSequenceSequenceOrDictionaryOrHeaders.h" 18 #include "bindings/modules/v8/ByteStringSequenceSequenceOrDictionaryOrHeaders.h"
19 #include "bindings/modules/v8/V8Response.h"
19 #include "core/dom/DOMArrayBuffer.h" 20 #include "core/dom/DOMArrayBuffer.h"
20 #include "core/dom/DOMArrayBufferView.h" 21 #include "core/dom/DOMArrayBufferView.h"
21 #include "core/dom/URLSearchParams.h" 22 #include "core/dom/URLSearchParams.h"
22 #include "core/fileapi/Blob.h" 23 #include "core/fileapi/Blob.h"
23 #include "core/frame/UseCounter.h" 24 #include "core/frame/UseCounter.h"
24 #include "core/html/FormData.h" 25 #include "core/html/FormData.h"
25 #include "core/streams/ReadableStreamOperations.h" 26 #include "core/streams/ReadableStreamOperations.h"
26 #include "modules/fetch/BlobBytesConsumer.h" 27 #include "modules/fetch/BlobBytesConsumer.h"
27 #include "modules/fetch/BodyStreamBuffer.h" 28 #include "modules/fetch/BodyStreamBuffer.h"
28 #include "modules/fetch/FormDataBytesConsumer.h" 29 #include "modules/fetch/FormDataBytesConsumer.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 for (unsigned i = 0; i < statusText.length(); ++i) { 114 for (unsigned i = 0; i < statusText.length(); ++i) {
114 UChar c = statusText[i]; 115 UChar c = statusText[i];
115 if (!(c == 0x09 // HTAB 116 if (!(c == 0x09 // HTAB
116 || (0x20 <= c && c <= 0x7E) // SP / VCHAR 117 || (0x20 <= c && c <= 0x7E) // SP / VCHAR
117 || (0x80 <= c && c <= 0xFF))) // obs-text 118 || (0x80 <= c && c <= 0xFF))) // obs-text
118 return false; 119 return false;
119 } 120 }
120 return true; 121 return true;
121 } 122 }
122 123
124 class FetchDataLoaderAsWasm final : public FetchDataLoader,
125 public BytesConsumer::Client {
126 USING_GARBAGE_COLLECTED_MIXIN(FetchDataLoaderAsWasm);
127
128 public:
129 explicit FetchDataLoaderAsWasm(v8::Isolate* isolate,
haraken 2017/04/04 02:39:38 Drop explicit.
Mircea Trofin 2017/04/04 04:08:12 Done.
130 ScriptPromiseResolver* resolver,
131 ScriptState* scriptState)
132 : m_resolver(resolver), m_builder(isolate), m_scriptState(scriptState) {}
133
134 void start(BytesConsumer* consumer,
135 FetchDataLoader::Client* client) override {
136 DCHECK(!m_consumer);
137 DCHECK(!m_client);
138 m_client = client;
139 m_consumer = consumer;
140 m_consumer->setClient(this);
141 onStateChange();
142 }
143
144 void onStateChange() override {
145 while (true) {
146 const char* buffer;
147 size_t available;
148 BytesConsumer::Result result = m_consumer->beginRead(&buffer, &available);
haraken 2017/04/04 02:39:38 Who allocates the buffer memory and who deallocate
Mircea Trofin 2017/04/04 04:08:12 the consumer allocates, and deallocates at endRead
149
150 switch (result) {
151 case BytesConsumer::Result::ShouldWait:
152 return;
153 case BytesConsumer::Result::Ok: {
154 SendBytes(buffer, available);
155 result = m_consumer->endRead(available);
156 break;
157 }
158 case BytesConsumer::Result::Done: {
159 if (available > 0)
160 SendBytes(buffer, available);
161 v8::Isolate* isolate = m_resolver->getScriptState()->isolate();
162 ScriptState::Scope scope(m_scriptState.get());
163 v8::TryCatch trycatch(isolate);
164 v8::MaybeLocal<v8::WasmCompiledModule> maybeModule =
165 m_builder.Finish();
166 v8::Local<v8::WasmCompiledModule> module;
167 if (maybeModule.ToLocal(&module)) {
haraken 2017/04/04 02:39:38 Nit: Use m_builder.ToLocal(&module). We want to av
Mircea Trofin 2017/04/04 04:08:12 Done. I'm curious though, what is the motivation
168 DCHECK(!trycatch.HasCaught());
169 ScriptValue sv(m_scriptState.get(), module);
haraken 2017/04/04 02:39:38 sv => scriptValue Blink prefers a fully qualified
Mircea Trofin 2017/04/04 04:08:12 Done.
170 m_resolver->resolve(sv);
171 } else {
172 DCHECK(trycatch.HasCaught());
173 m_resolver->reject(trycatch.Exception());
174 trycatch.Reset();
haraken 2017/04/04 02:39:38 Why do you need to Reset it?
Mircea Trofin 2017/04/04 04:08:12 We don't want the exception to survive past the ca
175 }
176 m_client->didFetchDataLoadedStream();
177 return;
178 }
179 case BytesConsumer::Result::Error: {
180 // TODO(mtrofin): do we need an abort on the wasm side?
181 // m_outStream->abort();
182 m_client->didFetchDataLoadFailed();
183 return;
184 }
185 }
186 }
187 }
188
189 void cancel() override { m_consumer->cancel(); }
190
191 DEFINE_INLINE_TRACE() {
192 visitor->trace(m_consumer);
193 visitor->trace(m_resolver);
194 visitor->trace(m_client);
195 FetchDataLoader::trace(visitor);
196 BytesConsumer::Client::trace(visitor);
197 }
198
199 private:
200 void SendBytes(const char* bytes, size_t size) {
haraken 2017/04/04 02:39:38 sendBytes
201 char* bufferCopy = new char[size];
haraken 2017/04/04 02:39:38 Use fastMalloc(). Who deletes the bufferCopy?
Mircea Trofin 2017/04/04 04:08:12 Changed to using std::unique_ptr from the get-go,
202 memcpy(bufferCopy, bytes, size);
203 // TODO(mtrofin): we want to extend OnBytesReceived to test
204 // for decoding errors or for compilation errors that
205 // happened meanwhile.
206 m_builder.OnBytesReceived(std::unique_ptr<const uint8_t[]>(
207 reinterpret_cast<const uint8_t*>(bufferCopy)),
208 size);
209 }
210
211 Member<BytesConsumer> m_consumer;
212 Member<ScriptPromiseResolver> m_resolver;
213 Member<FetchDataLoader::Client> m_client;
214 v8::WasmModuleObjectBuilder m_builder;
215 const RefPtr<ScriptState> m_scriptState;
216 };
217
218 // TODO(mtrofin): WasmConsumer is necessary so we may provide an
219 // argument to BodyStreamBuffer::startLoading, however, it fulfills
220 // a very small role. Consider refactoring to avoid it.
221 class WasmConsumer final : public GarbageCollectedFinalized<WasmConsumer>,
haraken 2017/04/04 02:39:38 "Finalized" wouldn't be needed.
Mircea Trofin 2017/04/04 04:08:12 You mean just "GarbageCollected"? Removing the "Fi
222 public FetchDataLoader::Client {
223 WTF_MAKE_NONCOPYABLE(WasmConsumer);
224 USING_GARBAGE_COLLECTED_MIXIN(WasmConsumer);
225
226 public:
227 explicit WasmConsumer(ScriptPromiseResolver* resolver)
228 : m_resolver(resolver) {}
229
230 void didFetchDataLoadedStream() override {}
231 void didFetchDataLoadFailed() override {
232 ScriptState::Scope scope(m_resolver->getScriptState());
233 m_resolver->reject(V8ThrowException::createTypeError(
234 m_resolver->getScriptState()->isolate(), "Failed to fetch"));
235 }
236 DEFINE_INLINE_TRACE() {
237 visitor->trace(m_resolver);
238 FetchDataLoader::Client::trace(visitor);
239 }
240
241 private:
242 Member<ScriptPromiseResolver> m_resolver;
243 };
244
245 // This callback may be entered as a promise is resolved, or directly
246 // from the overload callback.
247 void CompileFromResponseCallback(
haraken 2017/04/04 02:39:38 I'm not really happy about the hand-written callba
Mircea Trofin 2017/04/04 04:08:12 Added you to a thread. The very succinct explana
248 const v8::FunctionCallbackInfo<v8::Value>& args) {
249 ExceptionState exceptionState(args.GetIsolate(),
250 ExceptionState::ExecutionContext, "WebAssembly",
251 "compile");
252 ExceptionToRejectPromiseScope rejectPromiseScope(args, exceptionState);
253
254 ScriptState* scriptState = ScriptState::forReceiverObject(args);
255 if (!scriptState->getExecutionContext()) {
256 args.GetReturnValue().Set(ScriptPromise().v8Value());
257 return;
258 }
259
260 if (args.Length() < 1 || !args[0]->IsObject() ||
261 !V8Response::hasInstance(args[0], args.GetIsolate())) {
262 args.GetReturnValue().Set(
263 ScriptPromise::reject(
264 scriptState, V8ThrowException::createTypeError(
265 scriptState->isolate(),
266 "Promise argument must produce a Response object"))
267 .v8Value());
268 return;
269 }
270
271 Response* response = V8Response::toImpl(v8::Local<v8::Object>::Cast(args[0]));
272 ScriptPromise promise;
273 if (response->isBodyLocked() || response->bodyUsed()) {
274 promise = ScriptPromise::reject(
275 scriptState,
276 V8ThrowException::createTypeError(
277 scriptState->isolate(),
278 "Cannot compile WebAssembly.Module from an already read Response"));
279 } else {
280 ScriptPromiseResolver* resolver =
281 ScriptPromiseResolver::create(scriptState);
282 if (response->bodyBuffer()) {
283 promise = resolver->promise();
284 response->bodyBuffer()->startLoading(
285 new FetchDataLoaderAsWasm(args.GetIsolate(), resolver, scriptState),
286 new WasmConsumer(resolver));
287 } else {
288 promise = ScriptPromise::reject(
289 scriptState, V8ThrowException::createTypeError(
290 scriptState->isolate(),
291 "Promise argument must produce a Response object"));
292 }
293 }
294 args.GetReturnValue().Set(promise.v8Value());
295 }
296
297 bool WasmCompileOverload(const v8::FunctionCallbackInfo<v8::Value>& args) {
298 if (args.Length() < 1 || !args[0]->IsObject())
299 return false;
300
301 if (args[0]->IsPromise()) {
302 ScriptState* scriptState = ScriptState::forReceiverObject(args);
303 ScriptPromise sp = ScriptPromise(scriptState, args[0]);
304 ScriptPromise thenCompile = sp.then(
305 v8::Function::New(args.GetIsolate(), CompileFromResponseCallback));
306 args.GetReturnValue().Set(thenCompile.v8Value());
307 return true;
308 }
309 if (V8Response::hasInstance(args[0], args.GetIsolate())) {
310 CompileFromResponseCallback(args);
311 return true;
312 }
313 return false;
314 }
315
123 } // namespace 316 } // namespace
124 317
318 v8::Local<v8::FunctionTemplate> Response::installerOverride(
319 v8::Isolate* isolate,
320 const DOMWrapperWorld& world) {
321 isolate->SetWasmCompileCallback(WasmCompileOverload);
322 return Response::s_wrapperTypeInfo.domTemplateFunction(isolate, world);
323 }
324
325 const WrapperTypeInfo Response::s_wrapperTypeInfoOverride = {
326 gin::kEmbedderBlink,
327 Response::installerOverride,
328 V8Response::trace,
329 V8Response::traceWrappers,
330 nullptr,
331 "Response",
332 0,
333 WrapperTypeInfo::WrapperTypeObjectPrototype,
334 WrapperTypeInfo::ObjectClassId,
335 WrapperTypeInfo::InheritFromActiveScriptWrappable,
336 WrapperTypeInfo::Dependent};
337
125 Response* Response::create(ScriptState* scriptState, 338 Response* Response::create(ScriptState* scriptState,
126 ExceptionState& exceptionState) { 339 ExceptionState& exceptionState) {
127 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState); 340 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState);
128 } 341 }
129 342
130 Response* Response::create(ScriptState* scriptState, 343 Response* Response::create(ScriptState* scriptState,
131 ScriptValue bodyValue, 344 ScriptValue bodyValue,
132 const ResponseInit& init, 345 const ResponseInit& init,
133 ExceptionState& exceptionState) { 346 ExceptionState& exceptionState) {
134 v8::Local<v8::Value> body = bodyValue.v8Value(); 347 v8::Local<v8::Value> body = bodyValue.v8Value();
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 V8HiddenValue::internalBodyBuffer(scriptState->isolate()), bodyBuffer); 673 V8HiddenValue::internalBodyBuffer(scriptState->isolate()), bodyBuffer);
461 } 674 }
462 675
463 DEFINE_TRACE(Response) { 676 DEFINE_TRACE(Response) {
464 Body::trace(visitor); 677 Body::trace(visitor);
465 visitor->trace(m_response); 678 visitor->trace(m_response);
466 visitor->trace(m_headers); 679 visitor->trace(m_headers);
467 } 680 }
468 681
469 } // namespace blink 682 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/fetch/Response.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698