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