| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BytesConsumerTestUtil_h |
| 6 #define BytesConsumerTestUtil_h |
| 7 |
| 8 #include "modules/fetch/BytesConsumer.h" |
| 9 #include "platform/heap/Handle.h" |
| 10 #include "wtf/Deque.h" |
| 11 #include "wtf/Vector.h" |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 class ExecutionContext; |
| 16 |
| 17 class BytesConsumerTestUtil { |
| 18 STATIC_ONLY(BytesConsumerTestUtil); |
| 19 public: |
| 20 class Command final { |
| 21 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
| 22 public: |
| 23 enum Name { |
| 24 Data, |
| 25 Done, |
| 26 Error, |
| 27 Wait, |
| 28 }; |
| 29 |
| 30 explicit Command(Name name) : m_name(name) { } |
| 31 Command(Name name, const Vector<char>& body) : m_name(name), m_body(body
) { } |
| 32 Command(Name name, const char* body, size_t size) : m_name(name) |
| 33 { |
| 34 m_body.append(body, size); |
| 35 } |
| 36 Command(Name name, const char* body) : Command(name, body, strlen(body))
{ } |
| 37 Name getName() const { return m_name; } |
| 38 const Vector<char>& body() const { return m_body; } |
| 39 |
| 40 private: |
| 41 const Name m_name; |
| 42 Vector<char> m_body; |
| 43 }; |
| 44 |
| 45 // ReplayingBytesConsumer stores commands via |add| and replays the stored |
| 46 // commends when read. |
| 47 class ReplayingBytesConsumer final : public BytesConsumer { |
| 48 public: |
| 49 // The ExecutionContext is needed to get a WebTaskRunner. |
| 50 explicit ReplayingBytesConsumer(ExecutionContext*); |
| 51 ~ReplayingBytesConsumer(); |
| 52 |
| 53 // Add a command to this handle. This function must be called BEFORE |
| 54 // any BytesConsumer methods are called. |
| 55 void add(const Command& command) { m_commands.append(command); } |
| 56 |
| 57 Result beginRead(const char** buffer, size_t* available) override; |
| 58 Result endRead(size_t readSize) override; |
| 59 |
| 60 void setClient(Client*) override; |
| 61 void clearClient() override; |
| 62 void cancel() override; |
| 63 PublicState getPublicState() const override; |
| 64 Error getError() const override; |
| 65 String debugName() const override { return "ReplayingBytesConsumer"; } |
| 66 |
| 67 bool isCancelled() const { return m_isCancelled; } |
| 68 |
| 69 DECLARE_TRACE(); |
| 70 |
| 71 private: |
| 72 void notifyAsReadable(int notificationToken); |
| 73 void close(); |
| 74 void error(const Error&); |
| 75 |
| 76 Member<ExecutionContext> m_executionContext; |
| 77 Member<BytesConsumer::Client> m_client; |
| 78 InternalState m_state = InternalState::Waiting; |
| 79 Deque<Command> m_commands; |
| 80 size_t m_offset = 0; |
| 81 BytesConsumer::Error m_error; |
| 82 int m_notificationToken = 0; |
| 83 bool m_isCancelled = false; |
| 84 }; |
| 85 |
| 86 class Reader final : public GarbageCollectedFinalized<Reader>, public BytesC
onsumer::Client { |
| 87 USING_GARBAGE_COLLECTED_MIXIN(Reader); |
| 88 public: |
| 89 // |consumer| must not have a client when called. |
| 90 explicit Reader(BytesConsumer* /* consumer */); |
| 91 |
| 92 void onStateChange() override; |
| 93 std::pair<BytesConsumer::Result, Vector<char>> run(); |
| 94 |
| 95 DEFINE_INLINE_TRACE() |
| 96 { |
| 97 visitor->trace(m_consumer); |
| 98 BytesConsumer::Client::trace(visitor); |
| 99 } |
| 100 |
| 101 private: |
| 102 Member<BytesConsumer> m_consumer; |
| 103 BytesConsumer::Result m_result = BytesConsumer::Result::ShouldWait; |
| 104 Vector<char> m_data; |
| 105 }; |
| 106 |
| 107 class TwoPhaseReader final : public GarbageCollectedFinalized<Reader>, publi
c BytesConsumer::Client { |
| 108 USING_GARBAGE_COLLECTED_MIXIN(TwoPhaseReader); |
| 109 public: |
| 110 // |consumer| must not have a client when called. |
| 111 explicit TwoPhaseReader(BytesConsumer* /* consumer */); |
| 112 |
| 113 void onStateChange() override; |
| 114 std::pair<BytesConsumer::Result, Vector<char>> run(); |
| 115 |
| 116 DEFINE_INLINE_TRACE() |
| 117 { |
| 118 visitor->trace(m_consumer); |
| 119 BytesConsumer::Client::trace(visitor); |
| 120 } |
| 121 |
| 122 private: |
| 123 Member<BytesConsumer> m_consumer; |
| 124 BytesConsumer::Result m_result = BytesConsumer::Result::ShouldWait; |
| 125 Vector<char> m_data; |
| 126 }; |
| 127 }; |
| 128 |
| 129 } // namespace blink |
| 130 |
| 131 #endif // BytesConsumerTestUtil_h |
| OLD | NEW |