OLD | NEW |
---|---|
1 | |
yhirano
2017/04/05 03:48:03
-
Mircea Trofin
2017/04/05 05:12:21
Acknowledged.
| |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 4 // found in the LICENSE file. |
4 | 5 |
5 #include "modules/fetch/Response.h" | 6 #include "modules/fetch/Response.h" |
6 | 7 |
7 #include <memory> | 8 #include <memory> |
8 #include "bindings/core/v8/Dictionary.h" | 9 #include "bindings/core/v8/Dictionary.h" |
9 #include "bindings/core/v8/ExceptionState.h" | 10 #include "bindings/core/v8/ExceptionState.h" |
10 #include "bindings/core/v8/ScriptState.h" | 11 #include "bindings/core/v8/ScriptState.h" |
11 #include "bindings/core/v8/V8ArrayBuffer.h" | 12 #include "bindings/core/v8/V8ArrayBuffer.h" |
12 #include "bindings/core/v8/V8ArrayBufferView.h" | 13 #include "bindings/core/v8/V8ArrayBufferView.h" |
13 #include "bindings/core/v8/V8Binding.h" | 14 #include "bindings/core/v8/V8Binding.h" |
14 #include "bindings/core/v8/V8Blob.h" | 15 #include "bindings/core/v8/V8Blob.h" |
15 #include "bindings/core/v8/V8FormData.h" | 16 #include "bindings/core/v8/V8FormData.h" |
16 #include "bindings/core/v8/V8HiddenValue.h" | 17 #include "bindings/core/v8/V8HiddenValue.h" |
17 #include "bindings/core/v8/V8URLSearchParams.h" | 18 #include "bindings/core/v8/V8URLSearchParams.h" |
18 #include "bindings/modules/v8/ByteStringSequenceSequenceOrDictionaryOrHeaders.h" | 19 #include "bindings/modules/v8/ByteStringSequenceSequenceOrDictionaryOrHeaders.h" |
20 #include "bindings/modules/v8/V8Response.h" | |
19 #include "core/dom/DOMArrayBuffer.h" | 21 #include "core/dom/DOMArrayBuffer.h" |
20 #include "core/dom/DOMArrayBufferView.h" | 22 #include "core/dom/DOMArrayBufferView.h" |
21 #include "core/dom/URLSearchParams.h" | 23 #include "core/dom/URLSearchParams.h" |
22 #include "core/fileapi/Blob.h" | 24 #include "core/fileapi/Blob.h" |
23 #include "core/frame/UseCounter.h" | 25 #include "core/frame/UseCounter.h" |
24 #include "core/html/FormData.h" | 26 #include "core/html/FormData.h" |
25 #include "core/streams/ReadableStreamOperations.h" | 27 #include "core/streams/ReadableStreamOperations.h" |
26 #include "modules/fetch/BlobBytesConsumer.h" | 28 #include "modules/fetch/BlobBytesConsumer.h" |
27 #include "modules/fetch/BodyStreamBuffer.h" | 29 #include "modules/fetch/BodyStreamBuffer.h" |
28 #include "modules/fetch/FormDataBytesConsumer.h" | 30 #include "modules/fetch/FormDataBytesConsumer.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 for (unsigned i = 0; i < statusText.length(); ++i) { | 115 for (unsigned i = 0; i < statusText.length(); ++i) { |
114 UChar c = statusText[i]; | 116 UChar c = statusText[i]; |
115 if (!(c == 0x09 // HTAB | 117 if (!(c == 0x09 // HTAB |
116 || (0x20 <= c && c <= 0x7E) // SP / VCHAR | 118 || (0x20 <= c && c <= 0x7E) // SP / VCHAR |
117 || (0x80 <= c && c <= 0xFF))) // obs-text | 119 || (0x80 <= c && c <= 0xFF))) // obs-text |
118 return false; | 120 return false; |
119 } | 121 } |
120 return true; | 122 return true; |
121 } | 123 } |
122 | 124 |
125 // TODO(mtrofin): WasmDataLoaderClient is necessary so we may provide an | |
126 // argument to BodyStreamBuffer::startLoading, however, it fulfills | |
127 // a very small role. Consider refactoring to avoid it. | |
128 class WasmDataLoaderClient final | |
129 : public GarbageCollectedFinalized<WasmDataLoaderClient>, | |
130 public FetchDataLoader::Client { | |
131 WTF_MAKE_NONCOPYABLE(WasmDataLoaderClient); | |
132 USING_GARBAGE_COLLECTED_MIXIN(WasmDataLoaderClient); | |
133 | |
134 public: | |
135 explicit WasmDataLoaderClient(ScriptPromiseResolver* resolver) | |
136 : m_resolver(resolver) {} | |
137 | |
138 void didFetchDataLoadedStream() override {} | |
139 void didFetchDataLoadFailed() override { | |
140 ScriptState::Scope scope(m_resolver->getScriptState()); | |
141 m_resolver->reject(V8ThrowException::createTypeError( | |
142 m_resolver->getScriptState()->isolate(), "Failed to fetch")); | |
143 } | |
144 DEFINE_INLINE_TRACE() { | |
145 visitor->trace(m_resolver); | |
146 FetchDataLoader::Client::trace(visitor); | |
147 } | |
148 | |
149 private: | |
150 Member<ScriptPromiseResolver> m_resolver; | |
151 }; | |
152 | |
153 // This callback may be entered as a promise is resolved, or directly | |
154 // from the overload callback. | |
155 // See https://crbug.com/708238 for tracking avoiding the hand-generated code. | |
156 void compileFromResponseCallback( | |
157 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
158 ExceptionState exceptionState(args.GetIsolate(), | |
159 ExceptionState::ExecutionContext, "WebAssembly", | |
160 "compile"); | |
161 ExceptionToRejectPromiseScope rejectPromiseScope(args, exceptionState); | |
162 | |
163 ScriptState* scriptState = ScriptState::forReceiverObject(args); | |
164 if (!scriptState->getExecutionContext()) { | |
165 args.GetReturnValue().Set(ScriptPromise().v8Value()); | |
166 return; | |
167 } | |
168 | |
169 if (args.Length() < 1 || !args[0]->IsObject() || | |
170 !V8Response::hasInstance(args[0], args.GetIsolate())) { | |
171 args.GetReturnValue().Set( | |
172 ScriptPromise::reject( | |
173 scriptState, V8ThrowException::createTypeError( | |
174 scriptState->isolate(), | |
175 "Promise argument must produce a Response object")) | |
176 .v8Value()); | |
177 return; | |
178 } | |
179 | |
180 Response* response = V8Response::toImpl(v8::Local<v8::Object>::Cast(args[0])); | |
181 ScriptPromise promise; | |
182 if (response->isBodyLocked() || response->bodyUsed()) { | |
183 promise = ScriptPromise::reject( | |
184 scriptState, | |
185 V8ThrowException::createTypeError( | |
186 scriptState->isolate(), | |
187 "Cannot compile WebAssembly.Module from an already read Response")); | |
188 } else { | |
189 ScriptPromiseResolver* resolver = | |
190 ScriptPromiseResolver::create(scriptState); | |
191 if (response->bodyBuffer()) { | |
192 promise = resolver->promise(); | |
193 response->bodyBuffer()->startLoading( | |
194 FetchDataLoader::createLoaderAsWasmModule(args.GetIsolate(), resolver, | |
195 scriptState), | |
196 new WasmDataLoaderClient(resolver)); | |
197 } else { | |
198 promise = ScriptPromise::reject( | |
199 scriptState, V8ThrowException::createTypeError( | |
200 scriptState->isolate(), | |
201 "Promise argument must produce a Response object")); | |
202 } | |
203 } | |
204 args.GetReturnValue().Set(promise.v8Value()); | |
205 } | |
206 | |
207 // See https://crbug.com/708238 for tracking avoiding the hand-generated code. | |
208 bool wasmCompileOverload(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
209 if (args.Length() < 1 || !args[0]->IsObject()) | |
210 return false; | |
211 | |
212 if (args[0]->IsPromise()) { | |
213 ScriptState* scriptState = ScriptState::forReceiverObject(args); | |
214 ScriptPromise sp = ScriptPromise(scriptState, args[0]); | |
215 ScriptPromise thenCompile = sp.then( | |
216 v8::Function::New(args.GetIsolate(), compileFromResponseCallback)); | |
217 args.GetReturnValue().Set(thenCompile.v8Value()); | |
218 return true; | |
219 } | |
220 if (V8Response::hasInstance(args[0], args.GetIsolate())) { | |
221 compileFromResponseCallback(args); | |
yhirano
2017/04/05 03:48:03
I think this should work as
wasmCompileOverLoad
Mircea Trofin
2017/04/05 05:12:22
So I understand - what would be the motivation - e
yhirano
2017/04/05 12:02:50
The current implementation calls compileFromRespon
Mircea Trofin
2017/04/05 17:17:49
Thanks for the clarification and the pointer. I ma
yhirano
2017/04/07 02:39:36
Ah, I see, thank you. Returning false means redire
yhirano
2017/04/07 02:52:29
Sorry, I was wrong. Please replace the last respon
Mircea Trofin
2017/04/07 06:11:16
Ah! I think I see what you mean. The example at th
| |
222 return true; | |
223 } | |
224 return false; | |
225 } | |
226 | |
123 } // namespace | 227 } // namespace |
124 | 228 |
229 v8::Local<v8::FunctionTemplate> Response::installerOverride( | |
230 v8::Isolate* isolate, | |
231 const DOMWrapperWorld& world) { | |
232 isolate->SetWasmCompileCallback(wasmCompileOverload); | |
233 return Response::s_wrapperTypeInfo.domTemplateFunction(isolate, world); | |
234 } | |
235 | |
236 const WrapperTypeInfo Response::s_wrapperTypeInfoOverride = { | |
237 gin::kEmbedderBlink, | |
238 Response::installerOverride, | |
239 V8Response::trace, | |
240 V8Response::traceWrappers, | |
241 nullptr, | |
242 "Response", | |
243 0, | |
244 WrapperTypeInfo::WrapperTypeObjectPrototype, | |
245 WrapperTypeInfo::ObjectClassId, | |
246 WrapperTypeInfo::InheritFromActiveScriptWrappable, | |
247 WrapperTypeInfo::Dependent}; | |
248 | |
125 Response* Response::create(ScriptState* scriptState, | 249 Response* Response::create(ScriptState* scriptState, |
126 ExceptionState& exceptionState) { | 250 ExceptionState& exceptionState) { |
127 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState); | 251 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState); |
128 } | 252 } |
129 | 253 |
130 Response* Response::create(ScriptState* scriptState, | 254 Response* Response::create(ScriptState* scriptState, |
131 ScriptValue bodyValue, | 255 ScriptValue bodyValue, |
132 const ResponseInit& init, | 256 const ResponseInit& init, |
133 ExceptionState& exceptionState) { | 257 ExceptionState& exceptionState) { |
134 v8::Local<v8::Value> body = bodyValue.v8Value(); | 258 v8::Local<v8::Value> body = bodyValue.v8Value(); |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
460 V8HiddenValue::internalBodyBuffer(scriptState->isolate()), bodyBuffer); | 584 V8HiddenValue::internalBodyBuffer(scriptState->isolate()), bodyBuffer); |
461 } | 585 } |
462 | 586 |
463 DEFINE_TRACE(Response) { | 587 DEFINE_TRACE(Response) { |
464 Body::trace(visitor); | 588 Body::trace(visitor); |
465 visitor->trace(m_response); | 589 visitor->trace(m_response); |
466 visitor->trace(m_headers); | 590 visitor->trace(m_headers); |
467 } | 591 } |
468 | 592 |
469 } // namespace blink | 593 } // namespace blink |
OLD | NEW |