summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryhirano <yhirano@chromium.org>2016-09-21 05:44:24 (GMT)
committerCommit bot <commit-bot@chromium.org>2016-09-21 05:47:46 (GMT)
commitdc89b6cd125269af5033d6e6a158c12542c0785d (patch)
treeb866738ef78bf503d2a6d27597071748b89416fc
parent67a801a052b6bec0bdf62145d190f9bd2e6e9a36 (diff)
downloadchromium-dc89b6cd125269af5033d6e6a158c12542c0785d.tar.gz
chromium-dc89b6cd125269af5033d6e6a158c12542c0785d.tar.xz
Remove BytesConsumer::read
BytesConsumer::read is rarely used, and it can be implemented on top of two-phase read functions. Removing it allows endRead to return Result::Done, which simplifies BytesConsumer implementations. This CL also fixes a bug in BytesConsumerTestUtil. BUG=610195 Review-Url: https://codereview.chromium.org/2356693002 Cr-Commit-Position: refs/heads/master@{#419983}
-rw-r--r--third_party/WebKit/Source/modules/fetch/BytesConsumer.cpp16
-rw-r--r--third_party/WebKit/Source/modules/fetch/BytesConsumer.h15
-rw-r--r--third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp28
-rw-r--r--third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.h1
-rw-r--r--third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandleTest.cpp60
-rw-r--r--third_party/WebKit/Source/modules/fetch/BytesConsumerTest.cpp37
-rw-r--r--third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.cpp43
-rw-r--r--third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.h21
8 files changed, 6 insertions, 215 deletions
diff --git a/third_party/WebKit/Source/modules/fetch/BytesConsumer.cpp b/third_party/WebKit/Source/modules/fetch/BytesConsumer.cpp
index 5c4eedf..f1584d0 100644
--- a/third_party/WebKit/Source/modules/fetch/BytesConsumer.cpp
+++ b/third_party/WebKit/Source/modules/fetch/BytesConsumer.cpp
@@ -313,22 +313,6 @@ private:
} // namespace
-BytesConsumer::Result BytesConsumer::read(char* buffer, size_t size, size_t* readSize)
-{
- *readSize = 0;
- const char* src = nullptr;
- size_t available;
- Result r = beginRead(&src, &available);
- if (r != Result::Ok)
- return r;
- *readSize = std::min(available, size);
- memcpy(buffer, src, *readSize);
- auto result = endRead(*readSize);
- if (result != BytesConsumer::Result::Ok)
- *readSize = 0;
- return result;
-}
-
void BytesConsumer::tee(ExecutionContext* executionContext, BytesConsumer* src, BytesConsumer** dest1, BytesConsumer** dest2)
{
RefPtr<BlobDataHandle> blobDataHandle = src->drainAsBlobDataHandle(BlobSizePolicy::AllowBlobWithInvalidSize);
diff --git a/third_party/WebKit/Source/modules/fetch/BytesConsumer.h b/third_party/WebKit/Source/modules/fetch/BytesConsumer.h
index 7388ba5..6f73c0b 100644
--- a/third_party/WebKit/Source/modules/fetch/BytesConsumer.h
+++ b/third_party/WebKit/Source/modules/fetch/BytesConsumer.h
@@ -74,17 +74,6 @@ public:
virtual ~BytesConsumer() {}
- // Reads data into |buffer| up to |size| bytes. The actual read size will
- // be stored in |*readSize|. This function cannot be called when a two-phase
- // read is in progress.
- // Returns Ok when readable.
- // Returns ShouldWait when it's waiting.
- // Returns Done when closed.
- // Returns Error when errored.
- // |buffer| can be null if |size| is 0.
- // |*readSize| will be set to 0 if not readable.
- virtual Result read(char* buffer, size_t /* size */, size_t* readSize) WARN_UNUSED_RESULT;
-
// Begins a two-phase read. On success, the function stores a buffer
// that contains the read data of length |*available| into |*buffer|.
// Returns Ok when readable.
@@ -106,7 +95,7 @@ public:
// Drains the data as a BlobDataHandle.
// When this function returns a non-null value, the returned blob handle
- // contains bytes that would be read through read, beginRead and
+ // contains bytes that would be read through beginRead and
// endRead functions without calling this function. In such a case, this
// object becomes closed.
// When this function returns null value, this function does nothing.
@@ -117,7 +106,7 @@ public:
// Drains the data as an EncodedFormData.
// When this function returns a non-null value, the returned form data
- // contains bytes that would be read through read, beginRead and
+ // contains bytes that would be read through beginRead and
// endRead functions without calling this function. In such a case, this
// object becomes closed.
// When this function returns null value, this function does nothing.
diff --git a/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp b/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp
index e4a61ab..1e4706b 100644
--- a/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp
+++ b/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp
@@ -25,34 +25,6 @@ BytesConsumerForDataConsumerHandle::~BytesConsumerForDataConsumerHandle()
{
}
-BytesConsumer::Result BytesConsumerForDataConsumerHandle::read(char* buffer, size_t size, size_t* readSize)
-{
- DCHECK(!m_isInTwoPhaseRead);
- *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();
- return Result::Done;
- case WebDataConsumerHandle::Busy:
- case WebDataConsumerHandle::ResourceExhausted:
- case WebDataConsumerHandle::UnexpectedError:
- error();
- return Result::Error;
- }
- NOTREACHED();
- return Result::Error;
-}
-
BytesConsumer::Result BytesConsumerForDataConsumerHandle::beginRead(const char** buffer, size_t* available)
{
DCHECK(!m_isInTwoPhaseRead);
diff --git a/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.h b/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.h
index cee1a54..8f11070 100644
--- a/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.h
+++ b/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.h
@@ -26,7 +26,6 @@ public:
BytesConsumerForDataConsumerHandle(ExecutionContext*, std::unique_ptr<FetchDataConsumerHandle>);
~BytesConsumerForDataConsumerHandle() override;
- Result read(char* buffer, size_t /* size */, size_t* readSize) override;
Result beginRead(const char** buffer, size_t* available) override;
PassRefPtr<BlobDataHandle> drainAsBlobDataHandle(BlobSizePolicy) override;
PassRefPtr<EncodedFormData> drainAsFormData() override;
diff --git a/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandleTest.cpp b/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandleTest.cpp
index 0565f67..dce1602 100644
--- a/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandleTest.cpp
+++ b/third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandleTest.cpp
@@ -53,7 +53,6 @@ class MockDataConsumerHandle final : public FetchDataConsumerHandle {
public:
class MockReaderProxy : public GarbageCollectedFinalized<MockReaderProxy> {
public:
- MOCK_METHOD4(read, WebDataConsumerHandle::Result(void*, size_t, WebDataConsumerHandle::Flags, size_t*));
MOCK_METHOD3(beginRead, WebDataConsumerHandle::Result(const void**, WebDataConsumerHandle::Flags, size_t*));
MOCK_METHOD1(endRead, WebDataConsumerHandle::Result(size_t));
MOCK_METHOD1(drainAsBlobDataHandle, PassRefPtr<BlobDataHandle>(FetchDataConsumerHandle::Reader::BlobSizePolicy));
@@ -70,10 +69,6 @@ private:
class Reader final : public FetchDataConsumerHandle::Reader {
public:
explicit Reader(MockReaderProxy* proxy) : m_proxy(proxy) {}
- Result read(void* buffer, size_t size, WebDataConsumerHandle::Flags flags, size_t* read) override
- {
- return m_proxy->read(buffer, size, flags, read);
- }
Result beginRead(const void** buffer, Flags flags, size_t* available) override
{
return m_proxy->beginRead(buffer, flags, available);
@@ -193,61 +188,6 @@ TEST_F(BytesConsumerForDataConsumerHandleTest, ClearClient)
checkpoint.Call(2);
}
-TEST_F(BytesConsumerForDataConsumerHandleTest, ReadWhenReadable)
-{
- std::unique_ptr<ReplayingHandle> handle = ReplayingHandle::create();
- handle->add(Command(Command::Data, "hello"));
- Persistent<BytesConsumer> consumer = new BytesConsumerForDataConsumerHandle(document(), createFetchDataConsumerHandleFromWebHandle(std::move(handle)));
- consumer->setClient(MockClient::create());
-
- char buffer[16];
- size_t read;
- Result r = consumer->read(buffer, sizeof(buffer), &read);
- ASSERT_EQ(Result::Ok, r);
- EXPECT_EQ("hello", String(buffer, read));
-}
-
-TEST_F(BytesConsumerForDataConsumerHandleTest, ReadWhenWaiting)
-{
- std::unique_ptr<ReplayingHandle> handle = ReplayingHandle::create();
- Persistent<BytesConsumer> consumer = new BytesConsumerForDataConsumerHandle(document(), createFetchDataConsumerHandleFromWebHandle(std::move(handle)));
- consumer->setClient(MockClient::create());
-
- char buffer[16];
- size_t read = 42;
- Result r = consumer->read(buffer, sizeof(buffer), &read);
- ASSERT_EQ(Result::ShouldWait, r);
- EXPECT_EQ(0u, read);
-}
-
-TEST_F(BytesConsumerForDataConsumerHandleTest, ReadWhenClosed)
-{
- std::unique_ptr<ReplayingHandle> handle = ReplayingHandle::create();
- handle->add(Command(Command::Done));
- Persistent<BytesConsumer> consumer = new BytesConsumerForDataConsumerHandle(document(), createFetchDataConsumerHandleFromWebHandle(std::move(handle)));
- consumer->setClient(MockClient::create());
-
- char buffer[16];
- size_t read = 42;
- Result r = consumer->read(buffer, sizeof(buffer), &read);
- ASSERT_EQ(Result::Done, r);
- EXPECT_EQ(0u, read);
-}
-
-TEST_F(BytesConsumerForDataConsumerHandleTest, ReadWhenErrored)
-{
- std::unique_ptr<ReplayingHandle> handle = ReplayingHandle::create();
- handle->add(Command(Command::Error));
- Persistent<BytesConsumer> consumer = new BytesConsumerForDataConsumerHandle(document(), createFetchDataConsumerHandleFromWebHandle(std::move(handle)));
- consumer->setClient(MockClient::create());
-
- char buffer[16];
- size_t read;
- Result r = consumer->read(buffer, sizeof(buffer), &read);
- ASSERT_EQ(Result::Error, r);
- EXPECT_EQ(BytesConsumer::Error("error"), consumer->getError());
-}
-
TEST_F(BytesConsumerForDataConsumerHandleTest, TwoPhaseReadWhenReadable)
{
std::unique_ptr<ReplayingHandle> handle = ReplayingHandle::create();
diff --git a/third_party/WebKit/Source/modules/fetch/BytesConsumerTest.cpp b/third_party/WebKit/Source/modules/fetch/BytesConsumerTest.cpp
index a4ace68..a9bebf5 100644
--- a/third_party/WebKit/Source/modules/fetch/BytesConsumerTest.cpp
+++ b/third_party/WebKit/Source/modules/fetch/BytesConsumerTest.cpp
@@ -101,8 +101,8 @@ TEST_F(BytesConsumerTeeTest, CreateDone)
BytesConsumer* dest2 = nullptr;
BytesConsumer::tee(document(), src, &dest1, &dest2);
- auto result1 = (new BytesConsumerTestUtil::Reader(dest1))->run();
- auto result2 = (new BytesConsumerTestUtil::Reader(dest2))->run();
+ auto result1 = (new BytesConsumerTestUtil::TwoPhaseReader(dest1))->run();
+ auto result2 = (new BytesConsumerTestUtil::TwoPhaseReader(dest2))->run();
EXPECT_EQ(Result::Done, result1.first);
EXPECT_TRUE(result1.second.isEmpty());
@@ -120,37 +120,6 @@ TEST_F(BytesConsumerTeeTest, CreateDone)
EXPECT_FALSE(src->isCancelled());
}
-TEST_F(BytesConsumerTeeTest, Read)
-{
- ReplayingBytesConsumer* src = new ReplayingBytesConsumer(document());
-
- src->add(Command(Command::Wait));
- src->add(Command(Command::Data, "hello, "));
- src->add(Command(Command::Wait));
- src->add(Command(Command::Data, "world"));
- src->add(Command(Command::Wait));
- src->add(Command(Command::Wait));
- src->add(Command(Command::Done));
-
- BytesConsumer* dest1 = nullptr;
- BytesConsumer* dest2 = nullptr;
- BytesConsumer::tee(document(), src, &dest1, &dest2);
-
- EXPECT_EQ(BytesConsumer::PublicState::ReadableOrWaiting, dest1->getPublicState());
- EXPECT_EQ(BytesConsumer::PublicState::ReadableOrWaiting, dest2->getPublicState());
-
- auto result1 = (new BytesConsumerTestUtil::Reader(dest1))->run();
- auto result2 = (new BytesConsumerTestUtil::Reader(dest2))->run();
-
- EXPECT_EQ(Result::Done, result1.first);
- EXPECT_EQ("hello, world", toString(result1.second));
- EXPECT_EQ(BytesConsumer::PublicState::Closed, dest1->getPublicState());
- EXPECT_EQ(Result::Done, result2.first);
- EXPECT_EQ("hello, world", toString(result2.second));
- EXPECT_EQ(BytesConsumer::PublicState::Closed, dest2->getPublicState());
- EXPECT_FALSE(src->isCancelled());
-}
-
TEST_F(BytesConsumerTeeTest, TwoPhaseRead)
{
ReplayingBytesConsumer* src = new ReplayingBytesConsumer(document());
@@ -355,7 +324,7 @@ TEST_F(BytesConsumerTeeTest, ConsumerCanBeErroredInTwoPhaseRead)
EXPECT_EQ(BytesConsumer::PublicState::ReadableOrWaiting, dest1->getPublicState());
int numOnStateChangeCalled = client->numOnStateChangeCalled();
- EXPECT_EQ(Result::Error, (new BytesConsumerTestUtil::Reader(dest2))->run().first);
+ EXPECT_EQ(Result::Error, (new BytesConsumerTestUtil::TwoPhaseReader(dest2))->run().first);
EXPECT_EQ(BytesConsumer::PublicState::Errored, dest1->getPublicState());
EXPECT_EQ(numOnStateChangeCalled + 1, client->numOnStateChangeCalled());
EXPECT_EQ('a', buffer[0]);
diff --git a/third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.cpp b/third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.cpp
index 27267d9..22a6f1e 100644
--- a/third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.cpp
+++ b/third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.cpp
@@ -149,47 +149,6 @@ DEFINE_TRACE(BytesConsumerTestUtil::ReplayingBytesConsumer)
BytesConsumer::trace(visitor);
}
-BytesConsumerTestUtil::Reader::Reader(BytesConsumer* consumer)
- : m_consumer(consumer)
-{
- m_consumer->setClient(this);
-}
-
-void BytesConsumerTestUtil::Reader::onStateChange()
-{
- while (true) {
- // We choose 3 here because of the following reasons.
- // - We want to split a string with multiple chunks, so we need to
- // choose a small number.
- // - An odd number is preferable to check an out-of-range error.
- // - With 1, every chunk consists of one byte it's too simple.
- char buffer[3];
- size_t read = 0;
- switch (m_consumer->read(buffer, sizeof(buffer), &read)) {
- case BytesConsumer::Result::Ok:
- m_data.append(buffer, read);
- break;
- case BytesConsumer::Result::ShouldWait:
- return;
- case BytesConsumer::Result::Done:
- m_result = BytesConsumer::Result::Done;
- return;
- case BytesConsumer::Result::Error:
- m_result = BytesConsumer::Result::Error;
- return;
- }
- }
-}
-
-std::pair<BytesConsumer::Result, Vector<char>> BytesConsumerTestUtil::Reader::run()
-{
- onStateChange();
- while (m_result != BytesConsumer::Result::Done && m_result != BytesConsumer::Result::Error)
- testing::runPendingTasks();
- testing::runPendingTasks();
- return std::make_pair(m_result, std::move(m_data));
-}
-
BytesConsumerTestUtil::TwoPhaseReader::TwoPhaseReader(BytesConsumer* consumer)
: m_consumer(consumer)
{
@@ -207,7 +166,7 @@ void BytesConsumerTestUtil::TwoPhaseReader::onStateChange()
// We don't use |available| as-is to test cases where endRead
// is called with a number smaller than |available|. We choose 3
// because of the same reasons as Reader::onStateChange.
- size_t read = std::max(static_cast<size_t>(3), available);
+ size_t read = std::min(static_cast<size_t>(3), available);
m_data.append(buffer, read);
if (m_consumer->endRead(read) != BytesConsumer::Result::Ok) {
m_result = BytesConsumer::Result::Error;
diff --git a/third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.h b/third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.h
index e3efa6d..ec6fef4 100644
--- a/third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.h
+++ b/third_party/WebKit/Source/modules/fetch/BytesConsumerTestUtil.h
@@ -83,27 +83,6 @@ public:
bool m_isCancelled = false;
};
- class Reader final : public GarbageCollectedFinalized<Reader>, public BytesConsumer::Client {
- USING_GARBAGE_COLLECTED_MIXIN(Reader);
- public:
- // |consumer| must not have a client when called.
- explicit Reader(BytesConsumer* /* consumer */);
-
- void onStateChange() override;
- std::pair<BytesConsumer::Result, Vector<char>> run();
-
- DEFINE_INLINE_TRACE()
- {
- visitor->trace(m_consumer);
- BytesConsumer::Client::trace(visitor);
- }
-
- private:
- Member<BytesConsumer> m_consumer;
- BytesConsumer::Result m_result = BytesConsumer::Result::ShouldWait;
- Vector<char> m_data;
- };
-
class TwoPhaseReader final : public GarbageCollectedFinalized<TwoPhaseReader>, public BytesConsumer::Client {
USING_GARBAGE_COLLECTED_MIXIN(TwoPhaseReader);
public: