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

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

Issue 2780693003: [wasm] response-based compile APIs (Closed)
Patch Set: . 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
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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
123 } // namespace 124 } // namespace
124 125
126 class FetchDataLoaderAsWasm final : public FetchDataLoader,
Mircea Trofin 2017/03/28 02:05:57 Probably belongs to its own file, or in Body.cpp.
127 public BytesConsumer::Client {
128 USING_GARBAGE_COLLECTED_MIXIN(FetchDataLoaderAsWasm);
129
130 public:
131 explicit FetchDataLoaderAsWasm(v8::Isolate* isolate,
132 ScriptPromiseResolver* resolver,
133 ScriptState* scriptState)
134 : m_resolver(resolver), m_builder(isolate), m_scriptState(scriptState) {}
135
136 void start(BytesConsumer* consumer,
137 FetchDataLoader::Client* client) override {
138 DCHECK(!m_consumer);
139 DCHECK(!m_client);
140 m_consumer = consumer;
141 m_consumer->setClient(this);
142 m_client = client;
143 onStateChange();
144 }
145
146 void onStateChange() override {
147 while (true) {
148 const char* buffer;
149 size_t available;
150 auto result = m_consumer->beginRead(&buffer, &available);
151 if (result == BytesConsumer::Result::ShouldWait) {
152 return;
153 }
154 if (result == BytesConsumer::Result::Ok) {
155 char* bufferCopy = new char[available];
156 memcpy(bufferCopy, buffer, available);
157 m_builder.OnBytesReceived(
158 std::unique_ptr<const uint8_t[]>(
159 reinterpret_cast<const uint8_t*>(bufferCopy)),
160 available);
161 result = m_consumer->endRead(available);
162 }
163 switch (result) {
164 case BytesConsumer::Result::Ok:
165 break;
166 case BytesConsumer::Result::ShouldWait:
167 NOTREACHED();
168 return;
169 case BytesConsumer::Result::Done: {
170 ScriptState::Scope scope(m_scriptState.get());
171 v8::MaybeLocal<v8::WasmCompiledModule> module = m_builder.Finish();
172 ScriptValue sv(m_scriptState.get(), module.ToLocalChecked());
173 m_resolver->resolve(sv);
174 m_client->didFetchDataLoadedStream();
175 return;
176 }
177 case BytesConsumer::Result::Error:
178 // TODO(mtrofin): do we need an abort?
179 // m_outStream->abort();
180 m_client->didFetchDataLoadFailed();
181 return;
182 }
183 }
184 }
185
186 void cancel() override { m_consumer->cancel(); }
187
188 DEFINE_INLINE_TRACE() {
189 visitor->trace(m_consumer);
190 visitor->trace(m_resolver);
191 visitor->trace(m_client);
192 FetchDataLoader::trace(visitor);
193 BytesConsumer::Client::trace(visitor);
194 }
195
196 Member<BytesConsumer> m_consumer;
197 Member<ScriptPromiseResolver> m_resolver;
198 v8::WasmModuleObjectBuilder m_builder;
199 Member<FetchDataLoader::Client> m_client;
200 const RefPtr<ScriptState> m_scriptState;
201 };
202
203 class WasmConsumer final : public GarbageCollectedFinalized<WasmConsumer>,
Mircea Trofin 2017/03/28 02:05:57 Not 100% convinced this brings much value here. Ad
204 public FetchDataLoader::Client {
205 WTF_MAKE_NONCOPYABLE(WasmConsumer);
206 USING_GARBAGE_COLLECTED_MIXIN(WasmConsumer);
207
208 public:
209 explicit WasmConsumer(ScriptPromiseResolver* resolver)
210 : m_resolver(resolver) {}
211 ScriptPromiseResolver* resolver() { return m_resolver; }
212 void didFetchDataLoadFailed() override {
213 ScriptState::Scope scope(resolver()->getScriptState());
214 m_resolver->reject(V8ThrowException::createTypeError(
215 resolver()->getScriptState()->isolate(), "Failed to fetch"));
216 }
217
218 void didFetchDataLoadedStream() {}
219
220 DEFINE_INLINE_TRACE() {
221 visitor->trace(m_resolver);
222 FetchDataLoader::Client::trace(visitor);
223 }
224
225 private:
226 Member<ScriptPromiseResolver> m_resolver;
227 };
228
229 void CompileCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
230 ExceptionState exceptionState(args.GetIsolate(),
231 ExceptionState::ExecutionContext, "WebAssembly",
232 "compile");
233 ExceptionToRejectPromiseScope rejectPromiseScope(args, exceptionState);
234 // TODO(mtrofin): what if args[0] is again a promise? We can probably
235 // factor this better.
236
237 ScriptState* scriptState = ScriptState::forReceiverObject(args);
238 if (args.Length() < 1 || !args[0]->IsObject() ||
239 !V8Response::hasInstance(args[0], args.GetIsolate())) {
240 args.GetReturnValue().Set(
241 ScriptPromise::reject(
242 scriptState, V8ThrowException::createTypeError(
243 scriptState->isolate(),
244 "Promise argument must produce a Response object"))
245 .v8Value());
246 return;
247 }
248
249 Response* response = V8Response::toImpl(v8::Local<v8::Object>::Cast(args[0]));
250 ScriptPromise promise = response->rejectInvalidConsumption(scriptState);
251 if (!promise.isEmpty()) {
252 args.GetReturnValue().Set(promise.v8Value());
253 return;
254 }
255
256 if (!scriptState->getExecutionContext()) {
257 args.GetReturnValue().Set(ScriptPromise().v8Value());
258 return;
259 }
260 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
261 promise = resolver->promise();
262 if (response->bodyBuffer()) {
263 response->bodyBuffer()->startLoading(
264 new FetchDataLoaderAsWasm(args.GetIsolate(), resolver, scriptState),
265 new WasmConsumer(resolver));
266 } else {
267 resolver->reject();
268 }
269 args.GetReturnValue().Set(promise.v8Value());
270 }
271
272 bool WasmResponseOverloads::CompileOverload(
273 const v8::FunctionCallbackInfo<v8::Value>& args) {
274 if (args.Length() < 1 || !args[0]->IsObject())
275 return false;
276 if (args[0]->IsPromise()) {
277 ScriptState* scriptState = ScriptState::forReceiverObject(args);
278 ScriptPromise sp = ScriptPromise(scriptState, args[0]);
279 ScriptPromise thenCompile =
280 sp.then(v8::Function::New(args.GetIsolate(), CompileCallback));
281 args.GetReturnValue().Set(thenCompile.v8Value());
282 return true;
283 }
284 if (!V8Response::hasInstance(args[0], args.GetIsolate()))
285 return false;
286
287 CompileCallback(args);
288 return true;
289 }
290
125 Response* Response::create(ScriptState* scriptState, 291 Response* Response::create(ScriptState* scriptState,
126 ExceptionState& exceptionState) { 292 ExceptionState& exceptionState) {
127 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState); 293 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState);
128 } 294 }
129 295
130 Response* Response::create(ScriptState* scriptState, 296 Response* Response::create(ScriptState* scriptState,
131 ScriptValue bodyValue, 297 ScriptValue bodyValue,
132 const ResponseInit& init, 298 const ResponseInit& init,
133 ExceptionState& exceptionState) { 299 ExceptionState& exceptionState) {
134 v8::Local<v8::Value> body = bodyValue.v8Value(); 300 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); 626 V8HiddenValue::internalBodyBuffer(scriptState->isolate()), bodyBuffer);
461 } 627 }
462 628
463 DEFINE_TRACE(Response) { 629 DEFINE_TRACE(Response) {
464 Body::trace(visitor); 630 Body::trace(visitor);
465 visitor->trace(m_response); 631 visitor->trace(m_response);
466 visitor->trace(m_headers); 632 visitor->trace(m_headers);
467 } 633 }
468 634
469 } // namespace blink 635 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698