| 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 "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "bindings/core/v8/V8ArrayBuffer.h" | 9 #include "bindings/core/v8/V8ArrayBuffer.h" |
| 10 #include "bindings/core/v8/V8ThrowException.h" | 10 #include "bindings/core/v8/V8ThrowException.h" |
| 11 #include "core/dom/DOMArrayBuffer.h" | 11 #include "core/dom/DOMArrayBuffer.h" |
| 12 #include "core/dom/DOMTypedArray.h" | 12 #include "core/dom/DOMTypedArray.h" |
| 13 #include "core/dom/URLSearchParams.h" |
| 13 #include "core/fileapi/Blob.h" | 14 #include "core/fileapi/Blob.h" |
| 14 #include "core/frame/UseCounter.h" | 15 #include "core/frame/UseCounter.h" |
| 16 #include "core/html/FormData.h" |
| 15 #include "modules/fetch/BodyStreamBuffer.h" | 17 #include "modules/fetch/BodyStreamBuffer.h" |
| 16 #include "modules/fetch/FetchDataLoader.h" | 18 #include "modules/fetch/FetchDataLoader.h" |
| 19 #include "platform/network/ParsedContentType.h" |
| 17 #include "public/platform/WebDataConsumerHandle.h" | 20 #include "public/platform/WebDataConsumerHandle.h" |
| 18 #include "wtf/PassRefPtr.h" | 21 #include "wtf/PassRefPtr.h" |
| 19 #include "wtf/RefPtr.h" | 22 #include "wtf/RefPtr.h" |
| 20 #include <memory> | 23 #include <memory> |
| 21 | 24 |
| 22 namespace blink { | 25 namespace blink { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, pub
lic FetchDataLoader::Client { | 29 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, pub
lic FetchDataLoader::Client { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 WTF_MAKE_NONCOPYABLE(BodyArrayBufferConsumer); | 63 WTF_MAKE_NONCOPYABLE(BodyArrayBufferConsumer); |
| 61 public: | 64 public: |
| 62 explicit BodyArrayBufferConsumer(ScriptPromiseResolver* resolver) : BodyCons
umerBase(resolver) {} | 65 explicit BodyArrayBufferConsumer(ScriptPromiseResolver* resolver) : BodyCons
umerBase(resolver) {} |
| 63 | 66 |
| 64 void didFetchDataLoadedArrayBuffer(DOMArrayBuffer* arrayBuffer) override | 67 void didFetchDataLoadedArrayBuffer(DOMArrayBuffer* arrayBuffer) override |
| 65 { | 68 { |
| 66 resolver()->resolve(arrayBuffer); | 69 resolver()->resolve(arrayBuffer); |
| 67 } | 70 } |
| 68 }; | 71 }; |
| 69 | 72 |
| 73 class BodyFormDataConsumer final : public BodyConsumerBase { |
| 74 WTF_MAKE_NONCOPYABLE(BodyFormDataConsumer); |
| 75 public: |
| 76 explicit BodyFormDataConsumer(ScriptPromiseResolver* resolver) : BodyConsume
rBase(resolver) {} |
| 77 |
| 78 void didFetchDataLoadedFormData(FormData* formData) override |
| 79 { |
| 80 resolver()->resolve(formData); |
| 81 } |
| 82 |
| 83 void didFetchDataLoadedString(const String& string) override |
| 84 { |
| 85 FormData* formData = FormData::create(); |
| 86 for (const auto& pair : URLSearchParams::create(string)->params()) |
| 87 formData->append(pair.first, pair.second); |
| 88 didFetchDataLoadedFormData(formData); |
| 89 } |
| 90 }; |
| 91 |
| 70 class BodyTextConsumer final : public BodyConsumerBase { | 92 class BodyTextConsumer final : public BodyConsumerBase { |
| 71 WTF_MAKE_NONCOPYABLE(BodyTextConsumer); | 93 WTF_MAKE_NONCOPYABLE(BodyTextConsumer); |
| 72 public: | 94 public: |
| 73 explicit BodyTextConsumer(ScriptPromiseResolver* resolver) : BodyConsumerBas
e(resolver) {} | 95 explicit BodyTextConsumer(ScriptPromiseResolver* resolver) : BodyConsumerBas
e(resolver) {} |
| 74 | 96 |
| 75 void didFetchDataLoadedString(const String& string) override | 97 void didFetchDataLoadedString(const String& string) override |
| 76 { | 98 { |
| 77 resolver()->resolve(string); | 99 resolver()->resolve(string); |
| 78 } | 100 } |
| 79 }; | 101 }; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsBlobHandle(mim
eType()), new BodyBlobConsumer(resolver)); | 164 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsBlobHandle(mim
eType()), new BodyBlobConsumer(resolver)); |
| 143 } else { | 165 } else { |
| 144 std::unique_ptr<BlobData> blobData = BlobData::create(); | 166 std::unique_ptr<BlobData> blobData = BlobData::create(); |
| 145 blobData->setContentType(mimeType()); | 167 blobData->setContentType(mimeType()); |
| 146 resolver->resolve(Blob::create(BlobDataHandle::create(std::move(blobData
), 0))); | 168 resolver->resolve(Blob::create(BlobDataHandle::create(std::move(blobData
), 0))); |
| 147 } | 169 } |
| 148 return promise; | 170 return promise; |
| 149 | 171 |
| 150 } | 172 } |
| 151 | 173 |
| 174 ScriptPromise Body::formData(ScriptState* scriptState) |
| 175 { |
| 176 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
| 177 if (!promise.isEmpty()) |
| 178 return promise; |
| 179 |
| 180 // See above comment. |
| 181 if (!scriptState->getExecutionContext()) |
| 182 return ScriptPromise(); |
| 183 |
| 184 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 185 ParsedContentType parsedTypeWithParameters(contentType()); |
| 186 String parsedType = parsedTypeWithParameters.mimeType().lower(); |
| 187 promise = resolver->promise(); |
| 188 if (parsedType == "multipart/form-data") { |
| 189 String boundary = parsedTypeWithParameters.parameterValueForName("bounda
ry"); |
| 190 if (bodyBuffer() && !boundary.isEmpty()) { |
| 191 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsFormData(b
oundary), new BodyFormDataConsumer(resolver)); |
| 192 return promise; |
| 193 } |
| 194 } else if (parsedType == "application/x-www-form-urlencoded") { |
| 195 if (bodyBuffer()) { |
| 196 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsString(),
new BodyFormDataConsumer(resolver)); |
| 197 } else { |
| 198 resolver->resolve(FormData::create()); |
| 199 } |
| 200 return promise; |
| 201 } |
| 202 |
| 203 resolver->reject(V8ThrowException::createTypeError(scriptState->isolate(), "
Invalid MIME type")); |
| 204 return promise; |
| 205 } |
| 206 |
| 152 ScriptPromise Body::json(ScriptState* scriptState) | 207 ScriptPromise Body::json(ScriptState* scriptState) |
| 153 { | 208 { |
| 154 ScriptPromise promise = rejectInvalidConsumption(scriptState); | 209 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
| 155 if (!promise.isEmpty()) | 210 if (!promise.isEmpty()) |
| 156 return promise; | 211 return promise; |
| 157 | 212 |
| 158 // See above comment. | 213 // See above comment. |
| 159 if (!scriptState->getExecutionContext()) | 214 if (!scriptState->getExecutionContext()) |
| 160 return ScriptPromise(); | 215 return ScriptPromise(); |
| 161 | 216 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 Body::Body(ExecutionContext* context) : ActiveScriptWrappable(this) , ContextLif
ecycleObserver(context) {} | 276 Body::Body(ExecutionContext* context) : ActiveScriptWrappable(this) , ContextLif
ecycleObserver(context) {} |
| 222 | 277 |
| 223 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) | 278 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) |
| 224 { | 279 { |
| 225 if (isBodyLocked() || bodyUsed()) | 280 if (isBodyLocked() || bodyUsed()) |
| 226 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 281 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); |
| 227 return ScriptPromise(); | 282 return ScriptPromise(); |
| 228 } | 283 } |
| 229 | 284 |
| 230 } // namespace blink | 285 } // namespace blink |
| OLD | NEW |