| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/FetchFormDataConsumerHandle.h" | 5 #include "modules/fetch/FetchFormDataConsumerHandle.h" |
| 6 | 6 |
| 7 #include "modules/fetch/DataConsumerHandleUtil.h" | 7 #include "modules/fetch/DataConsumerHandleUtil.h" |
| 8 #include "modules/fetch/FetchBlobDataConsumerHandle.h" | 8 #include "modules/fetch/FetchBlobDataConsumerHandle.h" |
| 9 #include "public/platform/WebTaskRunner.h" |
| 9 #include "wtf/PtrUtil.h" | 10 #include "wtf/PtrUtil.h" |
| 10 #include "wtf/ThreadingPrimitives.h" | 11 #include "wtf/ThreadingPrimitives.h" |
| 11 #include "wtf/Vector.h" | 12 #include "wtf/Vector.h" |
| 12 #include "wtf/text/TextCodec.h" | 13 #include "wtf/text/TextCodec.h" |
| 13 #include "wtf/text/TextEncoding.h" | 14 #include "wtf/text/TextEncoding.h" |
| 14 #include "wtf/text/WTFString.h" | 15 #include "wtf/text/WTFString.h" |
| 15 #include <memory> | 16 #include <memory> |
| 16 #include <utility> | 17 #include <utility> |
| 17 | 18 |
| 18 namespace blink { | 19 namespace blink { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 29 } | 30 } |
| 30 return true; | 31 return true; |
| 31 } | 32 } |
| 32 | 33 |
| 33 } // namespace | 34 } // namespace |
| 34 | 35 |
| 35 class FetchFormDataConsumerHandle::Context : public ThreadSafeRefCounted<Context
> { | 36 class FetchFormDataConsumerHandle::Context : public ThreadSafeRefCounted<Context
> { |
| 36 WTF_MAKE_NONCOPYABLE(Context); | 37 WTF_MAKE_NONCOPYABLE(Context); |
| 37 public: | 38 public: |
| 38 virtual ~Context() {} | 39 virtual ~Context() {} |
| 39 virtual std::unique_ptr<FetchDataConsumerHandle::Reader> obtainReader(Client
*) = 0; | 40 virtual std::unique_ptr<FetchDataConsumerHandle::Reader> obtainReader(Client
*, std::unique_ptr<WebTaskRunner>) = 0; |
| 40 | 41 |
| 41 protected: | 42 protected: |
| 42 explicit Context() {} | 43 explicit Context() {} |
| 43 }; | 44 }; |
| 44 | 45 |
| 45 class FetchFormDataConsumerHandle::SimpleContext final : public Context { | 46 class FetchFormDataConsumerHandle::SimpleContext final : public Context { |
| 46 class ReaderImpl; | 47 class ReaderImpl; |
| 47 public: | 48 public: |
| 48 static PassRefPtr<SimpleContext> create(const String& body) { return adoptRe
f(new SimpleContext(body)); } | 49 static PassRefPtr<SimpleContext> create(const String& body) { return adoptRe
f(new SimpleContext(body)); } |
| 49 static PassRefPtr<SimpleContext> create(const void* data, size_t size) { ret
urn adoptRef(new SimpleContext(data, size)); } | 50 static PassRefPtr<SimpleContext> create(const void* data, size_t size) { ret
urn adoptRef(new SimpleContext(data, size)); } |
| 50 static PassRefPtr<SimpleContext> create(PassRefPtr<EncodedFormData> body) {
return adoptRef(new SimpleContext(body)); } | 51 static PassRefPtr<SimpleContext> create(PassRefPtr<EncodedFormData> body) {
return adoptRef(new SimpleContext(body)); } |
| 51 | 52 |
| 52 std::unique_ptr<Reader> obtainReader(Client* client) override | 53 std::unique_ptr<Reader> obtainReader(Client* client, std::unique_ptr<WebTask
Runner> readerTaskRunner) override |
| 53 { | 54 { |
| 54 // For memory barrier. | 55 // For memory barrier. |
| 55 Mutex m; | 56 Mutex m; |
| 56 MutexLocker locker(m); | 57 MutexLocker locker(m); |
| 57 return ReaderImpl::create(this, client); | 58 return ReaderImpl::create(this, client, std::move(readerTaskRunner)); |
| 58 } | 59 } |
| 59 | 60 |
| 60 PassRefPtr<BlobDataHandle> drainAsBlobDataHandle() | 61 PassRefPtr<BlobDataHandle> drainAsBlobDataHandle() |
| 61 { | 62 { |
| 62 if (!m_formData) | 63 if (!m_formData) |
| 63 return nullptr; | 64 return nullptr; |
| 64 flatten(); | 65 flatten(); |
| 65 std::unique_ptr<BlobData> blobData = BlobData::create(); | 66 std::unique_ptr<BlobData> blobData = BlobData::create(); |
| 66 blobData->appendBytes(m_flattenFormData.data(), m_flattenFormData.size()
); | 67 blobData->appendBytes(m_flattenFormData.data(), m_flattenFormData.size()
); |
| 67 m_flattenFormData.clear(); | 68 m_flattenFormData.clear(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 RELEASE_ASSERT(read <= m_flattenFormData.size() - m_flattenFormDataOffse
t); | 116 RELEASE_ASSERT(read <= m_flattenFormData.size() - m_flattenFormDataOffse
t); |
| 116 m_flattenFormDataOffset += read; | 117 m_flattenFormDataOffset += read; |
| 117 | 118 |
| 118 return WebDataConsumerHandle::Ok; | 119 return WebDataConsumerHandle::Ok; |
| 119 } | 120 } |
| 120 | 121 |
| 121 private: | 122 private: |
| 122 class ReaderImpl final : public FetchDataConsumerHandle::Reader { | 123 class ReaderImpl final : public FetchDataConsumerHandle::Reader { |
| 123 WTF_MAKE_NONCOPYABLE(ReaderImpl); | 124 WTF_MAKE_NONCOPYABLE(ReaderImpl); |
| 124 public: | 125 public: |
| 125 static std::unique_ptr<ReaderImpl> create(PassRefPtr<SimpleContext> cont
ext, Client* client) { return wrapUnique(new ReaderImpl(context, client)); } | 126 static std::unique_ptr<ReaderImpl> create(PassRefPtr<SimpleContext> cont
ext, Client* client, std::unique_ptr<WebTaskRunner> readerTaskRunner) |
| 127 { |
| 128 return wrapUnique(new ReaderImpl(context, client, std::move(readerTa
skRunner))); |
| 129 } |
| 126 Result read(void* data, size_t size, Flags flags, size_t* readSize) over
ride | 130 Result read(void* data, size_t size, Flags flags, size_t* readSize) over
ride |
| 127 { | 131 { |
| 128 return m_context->read(data, size, flags, readSize); | 132 return m_context->read(data, size, flags, readSize); |
| 129 } | 133 } |
| 130 Result beginRead(const void** buffer, Flags flags, size_t* available) ov
erride | 134 Result beginRead(const void** buffer, Flags flags, size_t* available) ov
erride |
| 131 { | 135 { |
| 132 return m_context->beginRead(buffer, flags, available); | 136 return m_context->beginRead(buffer, flags, available); |
| 133 } | 137 } |
| 134 Result endRead(size_t read) override | 138 Result endRead(size_t read) override |
| 135 { | 139 { |
| 136 return m_context->endRead(read); | 140 return m_context->endRead(read); |
| 137 } | 141 } |
| 138 PassRefPtr<BlobDataHandle> drainAsBlobDataHandle(BlobSizePolicy) overrid
e | 142 PassRefPtr<BlobDataHandle> drainAsBlobDataHandle(BlobSizePolicy) overrid
e |
| 139 { | 143 { |
| 140 // A "simple" FormData always has a finite known size. | 144 // A "simple" FormData always has a finite known size. |
| 141 return m_context->drainAsBlobDataHandle(); | 145 return m_context->drainAsBlobDataHandle(); |
| 142 } | 146 } |
| 143 PassRefPtr<EncodedFormData> drainAsFormData() override | 147 PassRefPtr<EncodedFormData> drainAsFormData() override |
| 144 { | 148 { |
| 145 return m_context->drainFormData(); | 149 return m_context->drainFormData(); |
| 146 } | 150 } |
| 147 | 151 |
| 148 private: | 152 private: |
| 149 ReaderImpl(PassRefPtr<SimpleContext> context, Client* client) : m_contex
t(context), m_notifier(client) {} | 153 ReaderImpl(PassRefPtr<SimpleContext> context, Client* client, std::uniqu
e_ptr<WebTaskRunner> readerTaskRunner) |
| 154 : m_context(context) |
| 155 , m_notifier(client, readerTaskRunner.get()) {} |
| 150 | 156 |
| 151 RefPtr<SimpleContext> m_context; | 157 RefPtr<SimpleContext> m_context; |
| 152 NotifyOnReaderCreationHelper m_notifier; | 158 NotifyOnReaderCreationHelper m_notifier; |
| 153 }; | 159 }; |
| 154 | 160 |
| 155 explicit SimpleContext(const String& body) | 161 explicit SimpleContext(const String& body) |
| 156 : m_formData(EncodedFormData::create(UTF8Encoding().encode(body, WTF::En
titiesForUnencodables))) | 162 : m_formData(EncodedFormData::create(UTF8Encoding().encode(body, WTF::En
titiesForUnencodables))) |
| 157 , m_flattenFormDataOffset(0) {} | 163 , m_flattenFormDataOffset(0) {} |
| 158 SimpleContext(const void* data, size_t size) : m_formData(EncodedFormData::c
reate(data, size)) , m_flattenFormDataOffset(0) {} | 164 SimpleContext(const void* data, size_t size) : m_formData(EncodedFormData::c
reate(data, size)) , m_flattenFormDataOffset(0) {} |
| 159 explicit SimpleContext(PassRefPtr<EncodedFormData> body) : m_formData(body->
deepCopy()) , m_flattenFormDataOffset(0) {} | 165 explicit SimpleContext(PassRefPtr<EncodedFormData> body) : m_formData(body->
deepCopy()) , m_flattenFormDataOffset(0) {} |
| (...skipping 18 matching lines...) Expand all Loading... |
| 178 class FetchFormDataConsumerHandle::ComplexContext final : public Context { | 184 class FetchFormDataConsumerHandle::ComplexContext final : public Context { |
| 179 class ReaderImpl; | 185 class ReaderImpl; |
| 180 public: | 186 public: |
| 181 static PassRefPtr<ComplexContext> create(ExecutionContext* executionContext, | 187 static PassRefPtr<ComplexContext> create(ExecutionContext* executionContext, |
| 182 PassRefPtr<EncodedFormData> formData, | 188 PassRefPtr<EncodedFormData> formData, |
| 183 FetchBlobDataConsumerHandle::LoaderFactory* factory) | 189 FetchBlobDataConsumerHandle::LoaderFactory* factory) |
| 184 { | 190 { |
| 185 return adoptRef(new ComplexContext(executionContext, formData, factory))
; | 191 return adoptRef(new ComplexContext(executionContext, formData, factory))
; |
| 186 } | 192 } |
| 187 | 193 |
| 188 std::unique_ptr<FetchFormDataConsumerHandle::Reader> obtainReader(Client* cl
ient) override | 194 std::unique_ptr<FetchFormDataConsumerHandle::Reader> obtainReader(Client* cl
ient, std::unique_ptr<WebTaskRunner> readerTaskRunner) override |
| 189 { | 195 { |
| 190 // For memory barrier. | 196 // For memory barrier. |
| 191 Mutex m; | 197 Mutex m; |
| 192 MutexLocker locker(m); | 198 MutexLocker locker(m); |
| 193 return ReaderImpl::create(this, client); | 199 return ReaderImpl::create(this, client, std::move(readerTaskRunner)); |
| 194 } | 200 } |
| 195 | 201 |
| 196 private: | 202 private: |
| 197 class ReaderImpl final : public FetchDataConsumerHandle::Reader { | 203 class ReaderImpl final : public FetchDataConsumerHandle::Reader { |
| 198 WTF_MAKE_NONCOPYABLE(ReaderImpl); | 204 WTF_MAKE_NONCOPYABLE(ReaderImpl); |
| 199 public: | 205 public: |
| 200 static std::unique_ptr<ReaderImpl> create(PassRefPtr<ComplexContext> con
text, Client* client) { return wrapUnique(new ReaderImpl(context, client)); } | 206 static std::unique_ptr<ReaderImpl> create(PassRefPtr<ComplexContext> con
text, Client* client, std::unique_ptr<WebTaskRunner> readerTaskRunner) |
| 207 { |
| 208 return wrapUnique(new ReaderImpl(context, client, std::move(readerTa
skRunner))); |
| 209 } |
| 201 Result read(void* data, size_t size, Flags flags, size_t* readSize) over
ride | 210 Result read(void* data, size_t size, Flags flags, size_t* readSize) over
ride |
| 202 { | 211 { |
| 203 Result r = m_reader->read(data, size, flags, readSize); | 212 Result r = m_reader->read(data, size, flags, readSize); |
| 204 if (!(size == 0 && (r == Ok || r == ShouldWait))) { | 213 if (!(size == 0 && (r == Ok || r == ShouldWait))) { |
| 205 m_context->drainFormData(); | 214 m_context->drainFormData(); |
| 206 } | 215 } |
| 207 return r; | 216 return r; |
| 208 } | 217 } |
| 209 Result beginRead(const void** buffer, Flags flags, size_t* available) ov
erride | 218 Result beginRead(const void** buffer, Flags flags, size_t* available) ov
erride |
| 210 { | 219 { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 230 // Drain blob from the underlying handle to mark data as read. | 239 // Drain blob from the underlying handle to mark data as read. |
| 231 RefPtr<BlobDataHandle> handle = m_reader->drainAsBlobDataHandle(
AllowBlobWithInvalidSize); | 240 RefPtr<BlobDataHandle> handle = m_reader->drainAsBlobDataHandle(
AllowBlobWithInvalidSize); |
| 232 // Here we assume we can always get the valid handle. That is | 241 // Here we assume we can always get the valid handle. That is |
| 233 // in fact not specified at FetchDataConsumerHandle level, but | 242 // in fact not specified at FetchDataConsumerHandle level, but |
| 234 // |m_context->m_handle| is a FetchBlobDataConsumerHandle. | 243 // |m_context->m_handle| is a FetchBlobDataConsumerHandle. |
| 235 ASSERT(handle); | 244 ASSERT(handle); |
| 236 } | 245 } |
| 237 return formData.release(); | 246 return formData.release(); |
| 238 } | 247 } |
| 239 private: | 248 private: |
| 240 ReaderImpl(PassRefPtr<ComplexContext> context, Client* client) : m_conte
xt(context), m_reader(m_context->m_handle->obtainFetchDataReader(client)) {} | 249 ReaderImpl(PassRefPtr<ComplexContext> context, Client* client, std::uniq
ue_ptr<WebTaskRunner> readerTaskRunner) |
| 250 : m_context(context) |
| 251 , m_reader(m_context->m_handle->obtainFetchDataReader(client, std::m
ove(readerTaskRunner))) {} |
| 241 | 252 |
| 242 RefPtr<ComplexContext> m_context; | 253 RefPtr<ComplexContext> m_context; |
| 243 std::unique_ptr<FetchDataConsumerHandle::Reader> m_reader; | 254 std::unique_ptr<FetchDataConsumerHandle::Reader> m_reader; |
| 244 }; | 255 }; |
| 245 | 256 |
| 246 ComplexContext(ExecutionContext* executionContext, PassRefPtr<EncodedFormDat
a> body, FetchBlobDataConsumerHandle::LoaderFactory* factory) | 257 ComplexContext(ExecutionContext* executionContext, PassRefPtr<EncodedFormDat
a> body, FetchBlobDataConsumerHandle::LoaderFactory* factory) |
| 247 { | 258 { |
| 248 std::unique_ptr<BlobData> blobData = BlobData::create(); | 259 std::unique_ptr<BlobData> blobData = BlobData::create(); |
| 249 for (const auto& element : body->elements()) { | 260 for (const auto& element : body->elements()) { |
| 250 switch (element.m_type) { | 261 switch (element.m_type) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 FetchBlobDataConsumerHandle::LoaderFactory* loaderFactory) | 334 FetchBlobDataConsumerHandle::LoaderFactory* loaderFactory) |
| 324 { | 335 { |
| 325 if (isSimple(body.get())) { | 336 if (isSimple(body.get())) { |
| 326 m_context = SimpleContext::create(body); | 337 m_context = SimpleContext::create(body); |
| 327 } else { | 338 } else { |
| 328 m_context = ComplexContext::create(executionContext, body, loaderFactory
); | 339 m_context = ComplexContext::create(executionContext, body, loaderFactory
); |
| 329 } | 340 } |
| 330 } | 341 } |
| 331 FetchFormDataConsumerHandle::~FetchFormDataConsumerHandle() {} | 342 FetchFormDataConsumerHandle::~FetchFormDataConsumerHandle() {} |
| 332 | 343 |
| 333 std::unique_ptr<FetchDataConsumerHandle::Reader> FetchFormDataConsumerHandle::ob
tainFetchDataReader(Client* client) | 344 std::unique_ptr<FetchDataConsumerHandle::Reader> FetchFormDataConsumerHandle::ob
tainFetchDataReader(Client* client, std::unique_ptr<WebTaskRunner> readerTaskRun
ner) |
| 334 { | 345 { |
| 335 return m_context->obtainReader(client); | 346 return m_context->obtainReader(client, std::move(readerTaskRunner)); |
| 336 } | 347 } |
| 337 | 348 |
| 338 } // namespace blink | 349 } // namespace blink |
| OLD | NEW |