Chromium Code Reviews| Index: third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp |
| diff --git a/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp b/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2cfd72425baa3a6ff4e043847572939b8a0d1b1e |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp |
| @@ -0,0 +1,175 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "modules/fetch/BytesConsumerForDataConsumerHandle.h" |
| + |
| +#include <algorithm> |
| +#include <string.h> |
| + |
| +namespace blink { |
| + |
| +BytesConsumerForDataConsumerHandle::BytesConsumerForDataConsumerHandle(std::unique_ptr<FetchDataConsumerHandle> handle) |
| + : m_reader(handle->obtainReader(this)) |
| +{ |
| +} |
| + |
| +BytesConsumerForDataConsumerHandle::~BytesConsumerForDataConsumerHandle() {} |
| + |
| +BytesConsumer::Result BytesConsumerForDataConsumerHandle::read(char* buffer, size_t size, size_t* readSize) |
| +{ |
| + *readSize = 0; |
| + if (m_state == InternalState::Closed) |
| + return Result::Done; |
| + if (m_state == InternalState::Errored) |
| + return Result::Error; |
| + |
| + WebDataConsumerHandle::Result r = m_reader->read(buffer, size, WebDataConsumerHandle::FlagNone, readSize); |
| + switch (r) { |
| + case WebDataConsumerHandle::Ok: |
| + return Result::Ok; |
| + case WebDataConsumerHandle::ShouldWait: |
| + return Result::ShouldWait; |
| + case WebDataConsumerHandle::Done: |
| + close(); |
|
hiroshige
2016/07/26 06:48:12
Calling close() here can cause onStateChange() cal
yhirano
2016/07/27 13:19:52
Done.
|
| + return Result::Done; |
| + default: |
| + error(); |
|
hiroshige
2016/07/26 06:48:12
ditto.
yhirano
2016/07/27 13:19:52
Done.
|
| + return Result::Error; |
| + } |
| +} |
| + |
| +BytesConsumer::Result BytesConsumerForDataConsumerHandle::beginRead(const char** buffer, size_t* available) |
| +{ |
| + *buffer = nullptr; |
| + *available = 0; |
| + if (m_state == InternalState::Closed) |
| + return Result::Done; |
| + if (m_state == InternalState::Errored) |
| + return Result::Error; |
| + |
| + WebDataConsumerHandle::Result r = m_reader->beginRead(reinterpret_cast<const void**>(buffer), WebDataConsumerHandle::FlagNone, available); |
| + switch (r) { |
| + case WebDataConsumerHandle::Ok: |
| + return Result::Ok; |
| + case WebDataConsumerHandle::ShouldWait: |
| + return Result::ShouldWait; |
| + case WebDataConsumerHandle::Done: |
| + close(); |
|
hiroshige
2016/07/26 06:48:11
ditto.
yhirano
2016/07/27 13:19:52
Done.
|
| + return Result::Done; |
| + default: |
| + error(); |
|
hiroshige
2016/07/26 06:48:12
ditto.
yhirano
2016/07/27 13:19:52
Done.
|
| + return Result::Error; |
| + } |
| +} |
| + |
| +BytesConsumer::Result BytesConsumerForDataConsumerHandle::endRead(size_t read) |
| +{ |
| + DCHECK(m_state == InternalState::Readable || m_state == InternalState::Waiting); |
| + WebDataConsumerHandle::Result r = m_reader->endRead(read); |
| + if (r == WebDataConsumerHandle::Ok) |
| + return Result::Ok; |
| + error(); |
|
hiroshige
2016/07/26 06:48:12
ditto.
yhirano
2016/07/27 13:19:52
Done.
|
| + return Result::Error; |
| +} |
| + |
| +PassRefPtr<BlobDataHandle> BytesConsumerForDataConsumerHandle::drainAsBlobDataHandle(BlobSizePolicy policy) |
| +{ |
| + if (!m_reader) |
| + return nullptr; |
| + if (policy == BlobSizePolicy::DisallowBlobWithInvalidSize) |
| + return m_reader->drainAsBlobDataHandle(FetchDataConsumerHandle::Reader::DisallowBlobWithInvalidSize); |
|
hiroshige
2016/07/26 06:48:12
Shouldn't we call close() if the returned value is
yhirano
2016/07/27 13:19:52
Done.
|
| + DCHECK_EQ(BlobSizePolicy::AllowBlobWithInvalidSize, policy); |
| + return m_reader->drainAsBlobDataHandle(FetchDataConsumerHandle::Reader::AllowBlobWithInvalidSize); |
|
hiroshige
2016/07/26 06:48:11
ditto.
yhirano
2016/07/27 13:19:52
Done.
|
| +} |
| + |
| +PassRefPtr<EncodedFormData> BytesConsumerForDataConsumerHandle::drainAsFormData() |
| +{ |
| + if (!m_reader) |
| + return nullptr; |
| + return m_reader->drainAsFormData(); |
|
hiroshige
2016/07/26 06:48:12
ditto.
yhirano
2016/07/27 13:19:52
Done.
|
| +} |
| + |
| +void BytesConsumerForDataConsumerHandle::setClient(BytesConsumer::Client* client) |
| +{ |
| + DCHECK(!m_client); |
| + DCHECK(client); |
| + m_client = client; |
| +} |
| + |
| +void BytesConsumerForDataConsumerHandle::clearClient() |
| +{ |
| + DCHECK(m_client); |
| + m_client = nullptr; |
| +} |
| + |
| +void BytesConsumerForDataConsumerHandle::cancel() |
| +{ |
| + if (m_state == InternalState::Readable || m_state == InternalState::Waiting) { |
| + // We don't want the client to be notified in this case. |
| + BytesConsumer::Client* client = m_client; |
| + m_client = nullptr; |
| + close(); |
| + m_client = client; |
| + } |
| +} |
| + |
| +BytesConsumer::PublicState BytesConsumerForDataConsumerHandle::getPublicState() const |
| +{ |
| + return getPublicStateFromInternalState(m_state); |
| +} |
| + |
| +void BytesConsumerForDataConsumerHandle::didGetReadable() |
| +{ |
| + DCHECK(m_state == InternalState::Readable || m_state == InternalState::Waiting); |
| + // Perform zero-length read to call check handle's status. |
| + size_t readSize; |
| + WebDataConsumerHandle::Result result = m_reader->read(nullptr, 0, WebDataConsumerHandle::FlagNone, &readSize); |
| + switch (result) { |
| + case WebDataConsumerHandle::Ok: |
| + case WebDataConsumerHandle::ShouldWait: |
| + if (m_client) |
| + m_client->onStateChange(); |
| + return; |
| + case WebDataConsumerHandle::Done: |
| + close(); |
| + return; |
| + case WebDataConsumerHandle::Busy: |
| + case WebDataConsumerHandle::ResourceExhausted: |
| + case WebDataConsumerHandle::UnexpectedError: |
|
hiroshige
2016/07/26 06:48:12
nit: Can we make these lines "default:", as done i
yhirano
2016/07/27 13:19:52
My understanding is "default" is not preferred bec
hiroshige
2016/07/28 06:40:12
Agree. Then can we turn |default| in BytesConsumer
yhirano
2016/07/28 07:32:31
Thanks, done.
|
| + error(); |
| + return; |
| + } |
| + return; |
| +} |
| + |
| +DEFINE_TRACE(BytesConsumerForDataConsumerHandle) |
| +{ |
| + visitor->trace(m_client); |
| + BytesConsumer::trace(visitor); |
| +} |
| + |
| +void BytesConsumerForDataConsumerHandle::close() |
| +{ |
| + if (m_state == InternalState::Closed) |
| + return; |
| + DCHECK(m_state == InternalState::Readable || m_state == InternalState::Waiting); |
| + m_state = InternalState::Closed; |
| + m_reader = nullptr; |
| + if (m_client) |
| + m_client->onStateChange(); |
| +} |
| + |
| +void BytesConsumerForDataConsumerHandle::error() |
| +{ |
| + if (m_state == InternalState::Errored) |
| + return; |
| + DCHECK(m_state == InternalState::Readable || m_state == InternalState::Waiting); |
| + m_state = InternalState::Errored; |
| + m_reader = nullptr; |
| + m_error = Error("error"); |
| + if (m_client) |
| + m_client->onStateChange(); |
| +} |
| + |
| +} // namespace blink |