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/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 Loading... | |
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 // TODO(mtrofin): WasmDataLoaderClient is necessary so we may provide an | |
125 // argument to BodyStreamBuffer::startLoading, however, it fulfills | |
126 // a very small role. Consider refactoring to avoid it. | |
127 class WasmDataLoaderClient final | |
128 : public GarbageCollectedFinalized<WasmDataLoaderClient>, | |
129 public FetchDataLoader::Client { | |
130 WTF_MAKE_NONCOPYABLE(WasmDataLoaderClient); | |
131 USING_GARBAGE_COLLECTED_MIXIN(WasmDataLoaderClient); | |
132 | |
133 public: | |
134 explicit WasmDataLoaderClient(ScriptPromiseResolver* resolver) | |
135 : m_resolver(resolver) {} | |
136 | |
137 void didFetchDataLoadedWasmModule() override {} | |
138 void didFetchDataLoadFailed() override { | |
yhirano
2017/04/05 12:02:50
NOTREACHED();
Mircea Trofin
2017/04/05 17:17:50
Uh, yes. Done.
| |
139 ScriptState::Scope scope(m_resolver->getScriptState()); | |
140 m_resolver->reject(V8ThrowException::createTypeError( | |
141 m_resolver->getScriptState()->isolate(), "Failed to fetch")); | |
142 } | |
143 DEFINE_INLINE_TRACE() { | |
144 visitor->trace(m_resolver); | |
145 FetchDataLoader::Client::trace(visitor); | |
146 } | |
147 | |
148 private: | |
149 Member<ScriptPromiseResolver> m_resolver; | |
150 }; | |
151 | |
152 // This callback may be entered as a promise is resolved, or directly | |
153 // from the overload callback. | |
154 // See https://crbug.com/708238 for tracking avoiding the hand-generated code. | |
155 void compileFromResponseCallback( | |
156 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
157 ExceptionState exceptionState(args.GetIsolate(), | |
158 ExceptionState::ExecutionContext, "WebAssembly", | |
159 "compile"); | |
160 ExceptionToRejectPromiseScope rejectPromiseScope(args, exceptionState); | |
161 | |
162 ScriptState* scriptState = ScriptState::forReceiverObject(args); | |
163 if (!scriptState->getExecutionContext()) { | |
164 args.GetReturnValue().Set(ScriptPromise().v8Value()); | |
165 return; | |
166 } | |
167 | |
168 if (args.Length() < 1 || !args[0]->IsObject() || | |
169 !V8Response::hasInstance(args[0], args.GetIsolate())) { | |
170 args.GetReturnValue().Set( | |
171 ScriptPromise::reject( | |
172 scriptState, V8ThrowException::createTypeError( | |
173 scriptState->isolate(), | |
174 "Promise argument must produce a Response object")) | |
175 .v8Value()); | |
176 return; | |
177 } | |
178 | |
179 Response* response = V8Response::toImpl(v8::Local<v8::Object>::Cast(args[0])); | |
180 ScriptPromise promise; | |
181 if (response->isBodyLocked() || response->bodyUsed()) { | |
182 promise = ScriptPromise::reject( | |
183 scriptState, | |
184 V8ThrowException::createTypeError( | |
185 scriptState->isolate(), | |
186 "Cannot compile WebAssembly.Module from an already read Response")); | |
187 } else { | |
188 ScriptPromiseResolver* resolver = | |
189 ScriptPromiseResolver::create(scriptState); | |
190 if (response->bodyBuffer()) { | |
191 promise = resolver->promise(); | |
192 response->bodyBuffer()->startLoading( | |
193 FetchDataLoader::createLoaderAsWasmModule(args.GetIsolate(), resolver, | |
194 scriptState), | |
195 new WasmDataLoaderClient(resolver)); | |
196 } else { | |
197 promise = ScriptPromise::reject( | |
198 scriptState, V8ThrowException::createTypeError( | |
199 scriptState->isolate(), | |
200 "Promise argument must produce a Response object")); | |
201 } | |
202 } | |
203 args.GetReturnValue().Set(promise.v8Value()); | |
204 } | |
205 | |
206 // See https://crbug.com/708238 for tracking avoiding the hand-generated code. | |
207 bool wasmCompileOverload(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
208 if (args.Length() < 1 || !args[0]->IsObject()) | |
209 return false; | |
210 | |
211 if (args[0]->IsPromise()) { | |
212 ScriptState* scriptState = ScriptState::forReceiverObject(args); | |
213 ScriptPromise sp = ScriptPromise(scriptState, args[0]); | |
214 ScriptPromise thenCompile = sp.then( | |
215 v8::Function::New(args.GetIsolate(), compileFromResponseCallback)); | |
216 args.GetReturnValue().Set(thenCompile.v8Value()); | |
217 return true; | |
218 } | |
219 if (V8Response::hasInstance(args[0], args.GetIsolate())) { | |
220 compileFromResponseCallback(args); | |
221 return true; | |
222 } | |
223 return false; | |
224 } | |
225 | |
123 } // namespace | 226 } // namespace |
124 | 227 |
228 v8::Local<v8::FunctionTemplate> Response::installerOverride( | |
229 v8::Isolate* isolate, | |
230 const DOMWrapperWorld& world) { | |
231 isolate->SetWasmCompileCallback(wasmCompileOverload); | |
232 return Response::s_wrapperTypeInfo.domTemplateFunction(isolate, world); | |
233 } | |
234 | |
235 const WrapperTypeInfo Response::s_wrapperTypeInfoOverride = { | |
236 gin::kEmbedderBlink, | |
237 Response::installerOverride, | |
238 V8Response::trace, | |
239 V8Response::traceWrappers, | |
240 nullptr, | |
241 "Response", | |
242 0, | |
243 WrapperTypeInfo::WrapperTypeObjectPrototype, | |
244 WrapperTypeInfo::ObjectClassId, | |
245 WrapperTypeInfo::InheritFromActiveScriptWrappable, | |
246 WrapperTypeInfo::Dependent}; | |
247 | |
125 Response* Response::create(ScriptState* scriptState, | 248 Response* Response::create(ScriptState* scriptState, |
126 ExceptionState& exceptionState) { | 249 ExceptionState& exceptionState) { |
127 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState); | 250 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState); |
128 } | 251 } |
129 | 252 |
130 Response* Response::create(ScriptState* scriptState, | 253 Response* Response::create(ScriptState* scriptState, |
131 ScriptValue bodyValue, | 254 ScriptValue bodyValue, |
132 const ResponseInit& init, | 255 const ResponseInit& init, |
133 ExceptionState& exceptionState) { | 256 ExceptionState& exceptionState) { |
134 v8::Local<v8::Value> body = bodyValue.v8Value(); | 257 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); | 583 V8HiddenValue::internalBodyBuffer(scriptState->isolate()), bodyBuffer); |
461 } | 584 } |
462 | 585 |
463 DEFINE_TRACE(Response) { | 586 DEFINE_TRACE(Response) { |
464 Body::trace(visitor); | 587 Body::trace(visitor); |
465 visitor->trace(m_response); | 588 visitor->trace(m_response); |
466 visitor->trace(m_headers); | 589 visitor->trace(m_headers); |
467 } | 590 } |
468 | 591 |
469 } // namespace blink | 592 } // namespace blink |
OLD | NEW |