| 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/RequestInit.h" | 5 #include "modules/fetch/RequestInit.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/Dictionary.h" | 7 #include "bindings/core/v8/Dictionary.h" |
| 8 #include "bindings/core/v8/ScriptState.h" |
| 8 #include "bindings/core/v8/V8ArrayBuffer.h" | 9 #include "bindings/core/v8/V8ArrayBuffer.h" |
| 9 #include "bindings/core/v8/V8ArrayBufferView.h" | 10 #include "bindings/core/v8/V8ArrayBufferView.h" |
| 10 #include "bindings/core/v8/V8Binding.h" | 11 #include "bindings/core/v8/V8Binding.h" |
| 11 #include "bindings/core/v8/V8Blob.h" | 12 #include "bindings/core/v8/V8Blob.h" |
| 12 #include "bindings/core/v8/V8FormData.h" | 13 #include "bindings/core/v8/V8FormData.h" |
| 13 #include "bindings/core/v8/V8URLSearchParams.h" | 14 #include "bindings/core/v8/V8URLSearchParams.h" |
| 14 #include "bindings/modules/v8/V8PasswordCredential.h" | 15 #include "bindings/modules/v8/V8PasswordCredential.h" |
| 15 #include "core/dom/URLSearchParams.h" | 16 #include "core/dom/URLSearchParams.h" |
| 16 #include "core/fileapi/Blob.h" | 17 #include "core/fileapi/Blob.h" |
| 17 #include "core/html/FormData.h" | 18 #include "core/html/FormData.h" |
| 19 #include "core/streams/ReadableStreamOperations.h" |
| 18 #include "modules/fetch/BlobBytesConsumer.h" | 20 #include "modules/fetch/BlobBytesConsumer.h" |
| 21 #include "modules/fetch/BodyStreamBuffer.h" |
| 19 #include "modules/fetch/FormDataBytesConsumer.h" | 22 #include "modules/fetch/FormDataBytesConsumer.h" |
| 20 #include "modules/fetch/Headers.h" | 23 #include "modules/fetch/Headers.h" |
| 21 #include "platform/RuntimeEnabledFeatures.h" | 24 #include "platform/RuntimeEnabledFeatures.h" |
| 22 #include "platform/blob/BlobData.h" | 25 #include "platform/blob/BlobData.h" |
| 23 #include "platform/network/EncodedFormData.h" | 26 #include "platform/network/EncodedFormData.h" |
| 24 #include "platform/weborigin/ReferrerPolicy.h" | 27 #include "platform/weborigin/ReferrerPolicy.h" |
| 25 | 28 |
| 26 namespace blink { | 29 namespace blink { |
| 27 | 30 |
| 28 RequestInit::RequestInit(ExecutionContext* context, | 31 RequestInit::RequestInit(ScriptState* scriptState, |
| 29 const Dictionary& options, | 32 const Dictionary& options, |
| 30 ExceptionState& exceptionState) | 33 ExceptionState& exceptionState) |
| 31 : areAnyMembersSet(false) { | 34 : areAnyMembersSet(false) { |
| 32 areAnyMembersSet |= DictionaryHelper::get(options, "method", method); | 35 areAnyMembersSet |= DictionaryHelper::get(options, "method", method); |
| 33 areAnyMembersSet |= DictionaryHelper::get(options, "headers", headers); | 36 areAnyMembersSet |= DictionaryHelper::get(options, "headers", headers); |
| 34 if (!headers) { | 37 if (!headers) { |
| 35 Vector<Vector<String>> headersVector; | 38 Vector<Vector<String>> headersVector; |
| 36 if (DictionaryHelper::get(options, "headers", headersVector, | 39 if (DictionaryHelper::get(options, "headers", headersVector, |
| 37 exceptionState)) { | 40 exceptionState)) { |
| 38 headers = Headers::create(headersVector, exceptionState); | 41 headers = Headers::create(headersVector, exceptionState); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 RuntimeEnabledFeatures::reducedReferrerGranularityEnabled()) { | 94 RuntimeEnabledFeatures::reducedReferrerGranularityEnabled()) { |
| 92 referrer.referrerPolicy = | 95 referrer.referrerPolicy = |
| 93 ReferrerPolicyNoReferrerWhenDowngradeOriginWhenCrossOrigin; | 96 ReferrerPolicyNoReferrerWhenDowngradeOriginWhenCrossOrigin; |
| 94 } else { | 97 } else { |
| 95 exceptionState.throwTypeError("Invalid referrer policy"); | 98 exceptionState.throwTypeError("Invalid referrer policy"); |
| 96 return; | 99 return; |
| 97 } | 100 } |
| 98 } | 101 } |
| 99 } | 102 } |
| 100 | 103 |
| 101 v8::Isolate* isolate = toIsolate(context); | 104 v8::Isolate* isolate = scriptState->isolate(); |
| 102 if (isCredentialSet) { | 105 if (isCredentialSet) { |
| 103 if (V8PasswordCredential::hasInstance(v8Credential, isolate)) { | 106 if (V8PasswordCredential::hasInstance(v8Credential, isolate)) { |
| 104 // TODO(mkwst): According to the spec, we'd serialize this once we touch | 107 // TODO(mkwst): According to the spec, we'd serialize this once we touch |
| 105 // the network. We're serializing it here, ahead of time, because lifetime | 108 // the network. We're serializing it here, ahead of time, because lifetime |
| 106 // issues around ResourceRequest make it pretty difficult to pass a | 109 // issues around ResourceRequest make it pretty difficult to pass a |
| 107 // PasswordCredential around at the platform level, and the hop between | 110 // PasswordCredential around at the platform level, and the hop between |
| 108 // the browser and renderer processes to deal with service workers is | 111 // the browser and renderer processes to deal with service workers is |
| 109 // equally painful. There should be no developer-visible difference in | 112 // equally painful. There should be no developer-visible difference in |
| 110 // behavior with this option, except that the `Content-Type` header will | 113 // behavior with this option, except that the `Content-Type` header will |
| 111 // be set early. That seems reasonable. | 114 // be set early. That seems reasonable. |
| 112 PasswordCredential* credential = | 115 PasswordCredential* credential = |
| 113 V8PasswordCredential::toImpl(v8Credential.As<v8::Object>()); | 116 V8PasswordCredential::toImpl(v8Credential.As<v8::Object>()); |
| 114 attachedCredential = credential->encodeFormData(contentType); | 117 attachedCredential = credential->encodeFormData(contentType); |
| 115 credentials = "password"; | 118 credentials = "password"; |
| 116 } else if (v8Credential->IsString()) { | 119 } else if (v8Credential->IsString()) { |
| 117 credentials = toUSVString(isolate, v8Credential, exceptionState); | 120 credentials = toUSVString(isolate, v8Credential, exceptionState); |
| 118 } | 121 } |
| 119 } | 122 } |
| 120 | 123 |
| 121 if (attachedCredential.get() || !isBodySet || v8Body->IsUndefined() || | 124 if (attachedCredential.get() || !isBodySet || v8Body->IsUndefined() || |
| 122 v8Body->IsNull()) | 125 v8Body->IsNull()) |
| 123 return; | 126 return; |
| 124 | 127 |
| 125 if (v8Body->IsArrayBuffer()) { | 128 if (v8Body->IsArrayBuffer()) { |
| 126 body = new FormDataBytesConsumer( | 129 body = new BodyStreamBuffer( |
| 127 V8ArrayBuffer::toImpl(v8Body.As<v8::Object>())); | 130 scriptState, new FormDataBytesConsumer( |
| 131 V8ArrayBuffer::toImpl(v8Body.As<v8::Object>()))); |
| 128 } else if (v8Body->IsArrayBufferView()) { | 132 } else if (v8Body->IsArrayBufferView()) { |
| 129 body = new FormDataBytesConsumer( | 133 body = new BodyStreamBuffer( |
| 130 V8ArrayBufferView::toImpl(v8Body.As<v8::Object>())); | 134 scriptState, new FormDataBytesConsumer( |
| 135 V8ArrayBufferView::toImpl(v8Body.As<v8::Object>()))); |
| 131 } else if (V8Blob::hasInstance(v8Body, isolate)) { | 136 } else if (V8Blob::hasInstance(v8Body, isolate)) { |
| 132 RefPtr<BlobDataHandle> blobDataHandle = | 137 RefPtr<BlobDataHandle> blobDataHandle = |
| 133 V8Blob::toImpl(v8Body.As<v8::Object>())->blobDataHandle(); | 138 V8Blob::toImpl(v8Body.As<v8::Object>())->blobDataHandle(); |
| 134 contentType = blobDataHandle->type(); | 139 contentType = blobDataHandle->type(); |
| 135 body = new BlobBytesConsumer(context, blobDataHandle.release()); | 140 body = new BodyStreamBuffer( |
| 141 scriptState, new BlobBytesConsumer(scriptState->getExecutionContext(), |
| 142 blobDataHandle.release())); |
| 136 } else if (V8FormData::hasInstance(v8Body, isolate)) { | 143 } else if (V8FormData::hasInstance(v8Body, isolate)) { |
| 137 RefPtr<EncodedFormData> formData = | 144 RefPtr<EncodedFormData> formData = |
| 138 V8FormData::toImpl(v8Body.As<v8::Object>())->encodeMultiPartFormData(); | 145 V8FormData::toImpl(v8Body.As<v8::Object>())->encodeMultiPartFormData(); |
| 139 // Here we handle formData->boundary() as a C-style string. See | 146 // Here we handle formData->boundary() as a C-style string. See |
| 140 // FormDataEncoder::generateUniqueBoundaryString. | 147 // FormDataEncoder::generateUniqueBoundaryString. |
| 141 contentType = AtomicString("multipart/form-data; boundary=") + | 148 contentType = AtomicString("multipart/form-data; boundary=") + |
| 142 formData->boundary().data(); | 149 formData->boundary().data(); |
| 143 body = new FormDataBytesConsumer(context, formData.release()); | 150 body = new BodyStreamBuffer( |
| 151 scriptState, |
| 152 new FormDataBytesConsumer(scriptState->getExecutionContext(), |
| 153 formData.release())); |
| 144 } else if (V8URLSearchParams::hasInstance(v8Body, isolate)) { | 154 } else if (V8URLSearchParams::hasInstance(v8Body, isolate)) { |
| 145 RefPtr<EncodedFormData> formData = | 155 RefPtr<EncodedFormData> formData = |
| 146 V8URLSearchParams::toImpl(v8Body.As<v8::Object>())->toEncodedFormData(); | 156 V8URLSearchParams::toImpl(v8Body.As<v8::Object>())->toEncodedFormData(); |
| 147 contentType = | 157 contentType = |
| 148 AtomicString("application/x-www-form-urlencoded;charset=UTF-8"); | 158 AtomicString("application/x-www-form-urlencoded;charset=UTF-8"); |
| 149 body = new FormDataBytesConsumer(context, formData.release()); | 159 body = new BodyStreamBuffer( |
| 160 scriptState, |
| 161 new FormDataBytesConsumer(scriptState->getExecutionContext(), |
| 162 formData.release())); |
| 150 } else if (v8Body->IsString()) { | 163 } else if (v8Body->IsString()) { |
| 151 contentType = "text/plain;charset=UTF-8"; | 164 contentType = "text/plain;charset=UTF-8"; |
| 152 body = | 165 body = new BodyStreamBuffer( |
| 153 new FormDataBytesConsumer(toUSVString(isolate, v8Body, exceptionState)); | 166 scriptState, new FormDataBytesConsumer( |
| 167 toUSVString(isolate, v8Body, exceptionState))); |
| 168 } else { |
| 169 ScriptValue value(scriptState, v8Body); |
| 170 if (ReadableStreamOperations::isReadableStream(scriptState, value)) |
| 171 body = new BodyStreamBuffer(scriptState, value); |
| 154 } | 172 } |
| 155 } | 173 } |
| 156 | 174 |
| 157 } // namespace blink | 175 } // namespace blink |
| OLD | NEW |