Chromium Code Reviews| 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/Body.h" | 5 #include "modules/fetch/Body.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "bindings/core/v8/V8ArrayBuffer.h" | 10 #include "bindings/core/v8/V8ArrayBuffer.h" |
| 11 #include "bindings/core/v8/V8ThrowException.h" | 11 #include "bindings/core/v8/V8ThrowException.h" |
| 12 #include "core/dom/DOMArrayBuffer.h" | 12 #include "core/dom/DOMArrayBuffer.h" |
| 13 #include "core/dom/DOMTypedArray.h" | 13 #include "core/dom/DOMTypedArray.h" |
| 14 #include "core/dom/ExecutionContext.h" | 14 #include "core/dom/ExecutionContext.h" |
| 15 #include "core/dom/URLSearchParams.h" | |
| 15 #include "core/fileapi/Blob.h" | 16 #include "core/fileapi/Blob.h" |
| 17 #include "core/html/FormData.h" | |
| 16 #include "modules/fetch/BodyStreamBuffer.h" | 18 #include "modules/fetch/BodyStreamBuffer.h" |
| 17 #include "modules/fetch/FetchDataLoader.h" | 19 #include "modules/fetch/FetchDataLoader.h" |
| 20 #include "platform/network/ParsedContentType.h" | |
| 18 #include "platform/wtf/PassRefPtr.h" | 21 #include "platform/wtf/PassRefPtr.h" |
| 19 #include "platform/wtf/RefPtr.h" | 22 #include "platform/wtf/RefPtr.h" |
| 20 #include "public/platform/WebDataConsumerHandle.h" | 23 #include "public/platform/WebDataConsumerHandle.h" |
| 21 | 24 |
| 22 namespace blink { | 25 namespace blink { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, | 29 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, |
| 27 public FetchDataLoader::Client { | 30 public FetchDataLoader::Client { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 | 68 |
| 66 public: | 69 public: |
| 67 explicit BodyArrayBufferConsumer(ScriptPromiseResolver* resolver) | 70 explicit BodyArrayBufferConsumer(ScriptPromiseResolver* resolver) |
| 68 : BodyConsumerBase(resolver) {} | 71 : BodyConsumerBase(resolver) {} |
| 69 | 72 |
| 70 void DidFetchDataLoadedArrayBuffer(DOMArrayBuffer* array_buffer) override { | 73 void DidFetchDataLoadedArrayBuffer(DOMArrayBuffer* array_buffer) override { |
| 71 Resolver()->Resolve(array_buffer); | 74 Resolver()->Resolve(array_buffer); |
| 72 } | 75 } |
| 73 }; | 76 }; |
| 74 | 77 |
| 78 class BodyFormDataConsumer final : public BodyConsumerBase { | |
| 79 WTF_MAKE_NONCOPYABLE(BodyFormDataConsumer); | |
| 80 | |
| 81 public: | |
| 82 explicit BodyFormDataConsumer(ScriptPromiseResolver* resolver) | |
| 83 : BodyConsumerBase(resolver) {} | |
| 84 | |
| 85 void DidFetchDataLoadedFormData(FormData* formData) override { | |
| 86 Resolver()->Resolve(formData); | |
| 87 } | |
| 88 | |
| 89 void DidFetchDataLoadedString(const String& string) override { | |
| 90 FormData* formData = FormData::Create(); | |
| 91 for (const auto& pair : URLSearchParams::Create(string)->Params()) | |
| 92 formData->append(pair.first, pair.second); | |
| 93 DidFetchDataLoadedFormData(formData); | |
| 94 } | |
| 95 }; | |
| 96 | |
| 75 class BodyTextConsumer final : public BodyConsumerBase { | 97 class BodyTextConsumer final : public BodyConsumerBase { |
| 76 WTF_MAKE_NONCOPYABLE(BodyTextConsumer); | 98 WTF_MAKE_NONCOPYABLE(BodyTextConsumer); |
| 77 | 99 |
| 78 public: | 100 public: |
| 79 explicit BodyTextConsumer(ScriptPromiseResolver* resolver) | 101 explicit BodyTextConsumer(ScriptPromiseResolver* resolver) |
| 80 : BodyConsumerBase(resolver) {} | 102 : BodyConsumerBase(resolver) {} |
| 81 | 103 |
| 82 void DidFetchDataLoadedString(const String& string) override { | 104 void DidFetchDataLoadedString(const String& string) override { |
| 83 Resolver()->Resolve(string); | 105 Resolver()->Resolve(string); |
| 84 } | 106 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 new BodyBlobConsumer(resolver)); | 173 new BodyBlobConsumer(resolver)); |
| 152 } else { | 174 } else { |
| 153 std::unique_ptr<BlobData> blob_data = BlobData::Create(); | 175 std::unique_ptr<BlobData> blob_data = BlobData::Create(); |
| 154 blob_data->SetContentType(MimeType()); | 176 blob_data->SetContentType(MimeType()); |
| 155 resolver->Resolve( | 177 resolver->Resolve( |
| 156 Blob::Create(BlobDataHandle::Create(std::move(blob_data), 0))); | 178 Blob::Create(BlobDataHandle::Create(std::move(blob_data), 0))); |
| 157 } | 179 } |
| 158 return promise; | 180 return promise; |
| 159 } | 181 } |
| 160 | 182 |
| 183 ScriptPromise Body::formData(ScriptState* script_state) { | |
| 184 ScriptPromise promise = RejectInvalidConsumption(script_state); | |
| 185 if (!promise.IsEmpty()) | |
| 186 return promise; | |
| 187 | |
| 188 // See above comment. | |
| 189 if (!ExecutionContext::From(script_state)) | |
| 190 return ScriptPromise(); | |
| 191 | |
| 192 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | |
| 193 const ParsedContentType parsedTypeWithParameters(ContentType()); | |
| 194 const String parsedType = parsedTypeWithParameters.MimeType().LowerASCII(); | |
| 195 promise = resolver->Promise(); | |
| 196 if (parsedType == "multipart/form-data") { | |
| 197 const String boundary = | |
| 198 parsedTypeWithParameters.ParameterValueForName("boundary"); | |
| 199 if (BodyBuffer() && !boundary.IsEmpty()) { | |
| 200 BodyBuffer()->StartLoading( | |
| 201 FetchDataLoader::CreateLoaderAsFormData(boundary), | |
| 202 new BodyFormDataConsumer(resolver)); | |
| 203 return promise; | |
| 204 } | |
| 205 } else if (parsedType == "application/x-www-form-urlencoded") { | |
| 206 if (BodyBuffer()) { | |
| 207 BodyBuffer()->StartLoading(FetchDataLoader::CreateLoaderAsString(), | |
| 208 new BodyFormDataConsumer(resolver)); | |
| 209 } else { | |
| 210 resolver->Resolve(FormData::Create()); | |
| 211 } | |
| 212 return promise; | |
| 213 } else { | |
| 214 if (BodyBuffer()) | |
| 215 BodyBuffer()->CloseAndLockAndDisturb(); | |
| 216 } | |
| 217 | |
| 218 resolver->Reject(V8ThrowException::CreateTypeError(script_state->GetIsolate(), | |
|
horo
2017/05/11 04:44:16
Please update external/wpt/fetch/api/response/resp
e_hakkinen
2017/05/11 11:01:51
Done.
horo
2017/05/12 08:43:02
What I wanted to say was that these tests are inco
e_hakkinen
2017/05/12 14:56:12
Done. Upstream review with one LGTM is at https://
horo
2017/05/16 00:58:10
Thank you!
| |
| 219 "Invalid MIME type")); | |
| 220 return promise; | |
| 221 } | |
| 222 | |
| 161 ScriptPromise Body::json(ScriptState* script_state) { | 223 ScriptPromise Body::json(ScriptState* script_state) { |
| 162 ScriptPromise promise = RejectInvalidConsumption(script_state); | 224 ScriptPromise promise = RejectInvalidConsumption(script_state); |
| 163 if (!promise.IsEmpty()) | 225 if (!promise.IsEmpty()) |
| 164 return promise; | 226 return promise; |
| 165 | 227 |
| 166 // See above comment. | 228 // See above comment. |
| 167 if (!ExecutionContext::From(script_state)) | 229 if (!ExecutionContext::From(script_state)) |
| 168 return ScriptPromise(); | 230 return ScriptPromise(); |
| 169 | 231 |
| 170 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 232 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 | 289 |
| 228 ScriptPromise Body::RejectInvalidConsumption(ScriptState* script_state) { | 290 ScriptPromise Body::RejectInvalidConsumption(ScriptState* script_state) { |
| 229 if (IsBodyLocked() || bodyUsed()) | 291 if (IsBodyLocked() || bodyUsed()) |
| 230 return ScriptPromise::Reject( | 292 return ScriptPromise::Reject( |
| 231 script_state, V8ThrowException::CreateTypeError( | 293 script_state, V8ThrowException::CreateTypeError( |
| 232 script_state->GetIsolate(), "Already read")); | 294 script_state->GetIsolate(), "Already read")); |
| 233 return ScriptPromise(); | 295 return ScriptPromise(); |
| 234 } | 296 } |
| 235 | 297 |
| 236 } // namespace blink | 298 } // namespace blink |
| OLD | NEW |