Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | |
| 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 void compileFromResponseCallback( | |
|
haraken
2017/04/04 06:05:31
I'd prefer moving these wasm-specific methods to S
Mircea Trofin
2017/04/04 06:48:07
Won't layering cause an issue? These implementatio
| |
| 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 bool wasmCompileOverload(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 207 if (args.Length() < 1 || !args[0]->IsObject()) | |
| 208 return false; | |
| 209 | |
| 210 if (args[0]->IsPromise()) { | |
| 211 ScriptState* scriptState = ScriptState::forReceiverObject(args); | |
| 212 ScriptPromise sp = ScriptPromise(scriptState, args[0]); | |
| 213 ScriptPromise thenCompile = sp.then( | |
| 214 v8::Function::New(args.GetIsolate(), compileFromResponseCallback)); | |
| 215 args.GetReturnValue().Set(thenCompile.v8Value()); | |
| 216 return true; | |
| 217 } | |
| 218 if (V8Response::hasInstance(args[0], args.GetIsolate())) { | |
| 219 compileFromResponseCallback(args); | |
| 220 return true; | |
| 221 } | |
| 222 return false; | |
| 223 } | |
| 224 | |
| 123 } // namespace | 225 } // namespace |
| 124 | 226 |
| 227 v8::Local<v8::FunctionTemplate> Response::installerOverride( | |
| 228 v8::Isolate* isolate, | |
| 229 const DOMWrapperWorld& world) { | |
| 230 isolate->SetWasmCompileCallback(wasmCompileOverload); | |
|
haraken
2017/04/04 06:05:31
Just to confirm: This statement is the only reason
Mircea Trofin
2017/04/04 06:48:07
Yes. It'll be followed by a similar statement for
haraken
2017/04/04 07:05:15
Yeah, I was confused about this CL because it's in
| |
| 231 return Response::s_wrapperTypeInfo.domTemplateFunction(isolate, world); | |
| 232 } | |
| 233 | |
| 234 const WrapperTypeInfo Response::s_wrapperTypeInfoOverride = { | |
| 235 gin::kEmbedderBlink, | |
| 236 Response::installerOverride, | |
| 237 V8Response::trace, | |
| 238 V8Response::traceWrappers, | |
| 239 nullptr, | |
| 240 "Response", | |
| 241 0, | |
| 242 WrapperTypeInfo::WrapperTypeObjectPrototype, | |
| 243 WrapperTypeInfo::ObjectClassId, | |
| 244 WrapperTypeInfo::InheritFromActiveScriptWrappable, | |
| 245 WrapperTypeInfo::Dependent}; | |
| 246 | |
| 125 Response* Response::create(ScriptState* scriptState, | 247 Response* Response::create(ScriptState* scriptState, |
| 126 ExceptionState& exceptionState) { | 248 ExceptionState& exceptionState) { |
| 127 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState); | 249 return create(scriptState, nullptr, String(), ResponseInit(), exceptionState); |
| 128 } | 250 } |
| 129 | 251 |
| 130 Response* Response::create(ScriptState* scriptState, | 252 Response* Response::create(ScriptState* scriptState, |
| 131 ScriptValue bodyValue, | 253 ScriptValue bodyValue, |
| 132 const ResponseInit& init, | 254 const ResponseInit& init, |
| 133 ExceptionState& exceptionState) { | 255 ExceptionState& exceptionState) { |
| 134 v8::Local<v8::Value> body = bodyValue.v8Value(); | 256 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); | 582 V8HiddenValue::internalBodyBuffer(scriptState->isolate()), bodyBuffer); |
| 461 } | 583 } |
| 462 | 584 |
| 463 DEFINE_TRACE(Response) { | 585 DEFINE_TRACE(Response) { |
| 464 Body::trace(visitor); | 586 Body::trace(visitor); |
| 465 visitor->trace(m_response); | 587 visitor->trace(m_response); |
| 466 visitor->trace(m_headers); | 588 visitor->trace(m_headers); |
| 467 } | 589 } |
| 468 | 590 |
| 469 } // namespace blink | 591 } // namespace blink |
| OLD | NEW |