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 "config.h" | 5 #include "config.h" |
6 #include "modules/fetch/Response.h" | 6 #include "modules/fetch/Response.h" |
7 | 7 |
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/ReadableStreamOperations.h" | 10 #include "bindings/core/v8/ReadableStreamOperations.h" |
11 #include "bindings/core/v8/ScriptState.h" | 11 #include "bindings/core/v8/ScriptState.h" |
12 #include "bindings/core/v8/V8ArrayBuffer.h" | 12 #include "bindings/core/v8/V8ArrayBuffer.h" |
13 #include "bindings/core/v8/V8ArrayBufferView.h" | 13 #include "bindings/core/v8/V8ArrayBufferView.h" |
14 #include "bindings/core/v8/V8Binding.h" | 14 #include "bindings/core/v8/V8Binding.h" |
15 #include "bindings/core/v8/V8Blob.h" | 15 #include "bindings/core/v8/V8Blob.h" |
16 #include "bindings/core/v8/V8FormData.h" | 16 #include "bindings/core/v8/V8FormData.h" |
17 #include "bindings/core/v8/V8HiddenValue.h" | |
17 #include "core/dom/DOMArrayBuffer.h" | 18 #include "core/dom/DOMArrayBuffer.h" |
18 #include "core/dom/DOMArrayBufferView.h" | 19 #include "core/dom/DOMArrayBufferView.h" |
19 #include "core/fileapi/Blob.h" | 20 #include "core/fileapi/Blob.h" |
20 #include "core/html/FormData.h" | 21 #include "core/html/FormData.h" |
21 #include "modules/fetch/BodyStreamBuffer.h" | 22 #include "modules/fetch/BodyStreamBuffer.h" |
23 #include "modules/fetch/DataConsumerHandleUtil.h" | |
22 #include "modules/fetch/FetchBlobDataConsumerHandle.h" | 24 #include "modules/fetch/FetchBlobDataConsumerHandle.h" |
23 #include "modules/fetch/FetchFormDataConsumerHandle.h" | 25 #include "modules/fetch/FetchFormDataConsumerHandle.h" |
24 #include "modules/fetch/ReadableStreamDataConsumerHandle.h" | 26 #include "modules/fetch/ReadableStreamDataConsumerHandle.h" |
25 #include "modules/fetch/ResponseInit.h" | 27 #include "modules/fetch/ResponseInit.h" |
26 #include "platform/RuntimeEnabledFeatures.h" | 28 #include "platform/RuntimeEnabledFeatures.h" |
27 #include "platform/network/EncodedFormData.h" | 29 #include "platform/network/EncodedFormData.h" |
28 #include "platform/network/HTTPHeaderMap.h" | 30 #include "platform/network/HTTPHeaderMap.h" |
29 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" | 31 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" |
30 #include "wtf/RefPtr.h" | 32 #include "wtf/RefPtr.h" |
31 | 33 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 } | 107 } |
106 | 108 |
107 Response* Response::create(ScriptState* scriptState, ExceptionState& exceptionSt ate) | 109 Response* Response::create(ScriptState* scriptState, ExceptionState& exceptionSt ate) |
108 { | 110 { |
109 return create(scriptState->executionContext(), nullptr, String(), ResponseIn it(), exceptionState); | 111 return create(scriptState->executionContext(), nullptr, String(), ResponseIn it(), exceptionState); |
110 } | 112 } |
111 | 113 |
112 Response* Response::create(ScriptState* scriptState, ScriptValue bodyValue, cons t Dictionary& init, ExceptionState& exceptionState) | 114 Response* Response::create(ScriptState* scriptState, ScriptValue bodyValue, cons t Dictionary& init, ExceptionState& exceptionState) |
113 { | 115 { |
114 v8::Local<v8::Value> body = bodyValue.v8Value(); | 116 v8::Local<v8::Value> body = bodyValue.v8Value(); |
117 ScriptValue reader; | |
115 v8::Isolate* isolate = scriptState->isolate(); | 118 v8::Isolate* isolate = scriptState->isolate(); |
116 ExecutionContext* executionContext = scriptState->executionContext(); | 119 ExecutionContext* executionContext = scriptState->executionContext(); |
117 | 120 |
118 OwnPtr<FetchDataConsumerHandle> bodyHandle; | 121 OwnPtr<FetchDataConsumerHandle> bodyHandle; |
119 String contentType; | 122 String contentType; |
120 if (V8Blob::hasInstance(body, isolate)) { | 123 if (V8Blob::hasInstance(body, isolate)) { |
121 Blob* blob = V8Blob::toImpl(body.As<v8::Object>()); | 124 Blob* blob = V8Blob::toImpl(body.As<v8::Object>()); |
122 bodyHandle = FetchBlobDataConsumerHandle::create(executionContext, blob- >blobDataHandle()); | 125 bodyHandle = FetchBlobDataConsumerHandle::create(executionContext, blob- >blobDataHandle()); |
123 contentType = blob->type(); | 126 contentType = blob->type(); |
124 } else if (V8ArrayBuffer::hasInstance(body, isolate)) { | 127 } else if (V8ArrayBuffer::hasInstance(body, isolate)) { |
125 bodyHandle = FetchFormDataConsumerHandle::create(V8ArrayBuffer::toImpl(b ody.As<v8::Object>())); | 128 bodyHandle = FetchFormDataConsumerHandle::create(V8ArrayBuffer::toImpl(b ody.As<v8::Object>())); |
126 } else if (V8ArrayBufferView::hasInstance(body, isolate)) { | 129 } else if (V8ArrayBufferView::hasInstance(body, isolate)) { |
127 bodyHandle = FetchFormDataConsumerHandle::create(V8ArrayBufferView::toIm pl(body.As<v8::Object>())); | 130 bodyHandle = FetchFormDataConsumerHandle::create(V8ArrayBufferView::toIm pl(body.As<v8::Object>())); |
128 } else if (V8FormData::hasInstance(body, isolate)) { | 131 } else if (V8FormData::hasInstance(body, isolate)) { |
129 RefPtr<EncodedFormData> formData = V8FormData::toImpl(body.As<v8::Object >())->encodeMultiPartFormData(); | 132 RefPtr<EncodedFormData> formData = V8FormData::toImpl(body.As<v8::Object >())->encodeMultiPartFormData(); |
130 // Here we handle formData->boundary() as a C-style string. See | 133 // Here we handle formData->boundary() as a C-style string. See |
131 // FormDataEncoder::generateUniqueBoundaryString. | 134 // FormDataEncoder::generateUniqueBoundaryString. |
132 contentType = AtomicString("multipart/form-data; boundary=", AtomicStrin g::ConstructFromLiteral) + formData->boundary().data(); | 135 contentType = AtomicString("multipart/form-data; boundary=", AtomicStrin g::ConstructFromLiteral) + formData->boundary().data(); |
133 bodyHandle = FetchFormDataConsumerHandle::create(executionContext, formD ata.release()); | 136 bodyHandle = FetchFormDataConsumerHandle::create(executionContext, formD ata.release()); |
134 } else if (RuntimeEnabledFeatures::responseConstructedWithReadableStreamEnab led() && ReadableStreamOperations::isReadableStream(scriptState, body)) { | 137 } else if (RuntimeEnabledFeatures::responseConstructedWithReadableStreamEnab led() && ReadableStreamOperations::isReadableStream(scriptState, body)) { |
135 bodyHandle = ReadableStreamDataConsumerHandle::create(scriptState, body) ; | 138 reader = ReadableStreamOperations::getReader(scriptState, body, exceptio nState); |
139 if (exceptionState.hadException()) { | |
140 reader = ScriptValue(); | |
141 bodyHandle = createFetchDataConsumerHandleFromWebHandle(createUnexpe ctedErrorDataConsumerHandle()); | |
142 exceptionState.clearException(); | |
143 } else { | |
144 bodyHandle = ReadableStreamDataConsumerHandle::create(scriptState, r eader.v8Value().As<v8::Object>()); | |
bashi
2016/01/06 05:39:17
just to clarify: reader.v8Value() is always an Obj
yhirano
2016/01/26 05:09:32
Deleted, but yes, ReadableStreamOperations::isRead
| |
145 } | |
136 } else { | 146 } else { |
137 String string = toUSVString(isolate, body, exceptionState); | 147 String string = toUSVString(isolate, body, exceptionState); |
138 if (exceptionState.hadException()) | 148 if (exceptionState.hadException()) |
139 return nullptr; | 149 return nullptr; |
140 bodyHandle = FetchFormDataConsumerHandle::create(string); | 150 bodyHandle = FetchFormDataConsumerHandle::create(string); |
141 contentType = "text/plain;charset=UTF-8"; | 151 contentType = "text/plain;charset=UTF-8"; |
142 } | 152 } |
143 // TODO(yhirano): Add the URLSearchParams case. | 153 // TODO(yhirano): Add the URLSearchParams case. |
144 return create(executionContext, bodyHandle.release(), contentType, ResponseI nit(init, exceptionState), exceptionState); | 154 Response* response = create(executionContext, bodyHandle.release(), contentT ype, ResponseInit(init, exceptionState), exceptionState); |
155 if (!reader.isEmpty()) { | |
156 // Add a hidden reference so that the weak persistent in the | |
157 // ReadableStreamDataConsumerHandle will be valid as long as the | |
158 // the Response is valid. | |
haraken
2016/01/06 05:49:06
the the
yhirano
2016/01/26 05:09:32
Done.
| |
159 v8::Local<v8::Value> wrapper = toV8(response, scriptState->context()->Gl obal(), scriptState->isolate()); | |
160 ASSERT(!wrapper.IsEmpty()); | |
haraken
2016/01/06 05:49:06
toV8 can return an empty handle.
yhirano
2016/01/26 05:09:32
Done.
| |
161 ASSERT(wrapper->IsObject()); | |
162 V8HiddenValue::setHiddenValue(scriptState, wrapper.As<v8::Object>(), V8H iddenValue::readableStreamReaderInResponse(scriptState->isolate()), reader.v8Val ue()); | |
163 } | |
164 return response; | |
145 } | 165 } |
146 | 166 |
147 Response* Response::create(ExecutionContext* context, PassOwnPtr<FetchDataConsum erHandle> bodyHandle, const String& contentType, const ResponseInit& init, Excep tionState& exceptionState) | 167 Response* Response::create(ExecutionContext* context, PassOwnPtr<FetchDataConsum erHandle> bodyHandle, const String& contentType, const ResponseInit& init, Excep tionState& exceptionState) |
148 { | 168 { |
149 unsigned short status = init.status; | 169 unsigned short status = init.status; |
150 | 170 |
151 // "1. If |init|'s status member is not in the range 200 to 599, inclusive, throw a | 171 // "1. If |init|'s status member is not in the range 200 to 599, inclusive, throw a |
152 // RangeError." | 172 // RangeError." |
153 if (status < 200 || 599 < status) { | 173 if (status < 200 || 599 < status) { |
154 exceptionState.throwRangeError(ExceptionMessages::indexOutsideRange<unsi gned>("status", status, 200, ExceptionMessages::InclusiveBound, 599, ExceptionMe ssages::InclusiveBound)); | 174 exceptionState.throwRangeError(ExceptionMessages::indexOutsideRange<unsi gned>("status", status, 200, ExceptionMessages::InclusiveBound, 599, ExceptionMe ssages::InclusiveBound)); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 } | 414 } |
395 | 415 |
396 DEFINE_TRACE(Response) | 416 DEFINE_TRACE(Response) |
397 { | 417 { |
398 Body::trace(visitor); | 418 Body::trace(visitor); |
399 visitor->trace(m_response); | 419 visitor->trace(m_response); |
400 visitor->trace(m_headers); | 420 visitor->trace(m_headers); |
401 } | 421 } |
402 | 422 |
403 } // namespace blink | 423 } // namespace blink |
OLD | NEW |