| 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 BytesConsumer_h |
| 6 #define BytesConsumer_h |
| 7 |
| 8 #include "modules/ModulesExport.h" |
| 9 #include "platform/blob/BlobData.h" |
| 10 #include "platform/heap/Handle.h" |
| 11 #include "platform/network/EncodedFormData.h" |
| 12 #include "wtf/PassRefPtr.h" |
| 13 #include "wtf/text/WTFString.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 // BytesConsumer represents the "consumer" side of a data pipe. A user |
| 18 // can read data from it. |
| 19 // |
| 20 // A BytesConsumer is bound to the thread on which it is created. |
| 21 // BytesConsumer has four states: waiting, readable, closed and errored. Once |
| 22 // the state becomes closed or errored, it will never change. |readable| means |
| 23 // that the BytesConsumer is ready to read non-empty bytes synchronously. |
| 24 class MODULES_EXPORT BytesConsumer : public GarbageCollectedFinalized<BytesConsu
mer> { |
| 25 public: |
| 26 enum class Result { |
| 27 Ok, |
| 28 ShouldWait, |
| 29 Done, |
| 30 Error, |
| 31 }; |
| 32 // Readable and Waiting are indistinguishable from BytesConsumer users. |
| 33 enum class PublicState { |
| 34 ReadableOrWaiting, |
| 35 Closed, |
| 36 Errored, |
| 37 }; |
| 38 enum class BlobSizePolicy { |
| 39 // The returned blob must have a valid size (i.e. != kuint64max). |
| 40 DisallowBlobWithInvalidSize, |
| 41 // The returned blob can have an invalid size. |
| 42 AllowBlobWithInvalidSize |
| 43 }; |
| 44 class MODULES_EXPORT Error { |
| 45 public: |
| 46 Error() {} |
| 47 explicit Error(const String& message) : m_message(message) {} |
| 48 const String& message() const { return m_message; } |
| 49 bool operator ==(const Error& e) const { return e.m_message == m_message
; } |
| 50 |
| 51 private: |
| 52 String m_message; |
| 53 }; |
| 54 // Client gets notification from the associated ByteConsumer. |
| 55 class MODULES_EXPORT Client : public GarbageCollectedMixin { |
| 56 public: |
| 57 virtual ~Client() {} |
| 58 |
| 59 // This function is called when the state changes. This function can |
| 60 // be called more than needed, i.e., it can be called even when the |
| 61 // state is not actually changed. |
| 62 // |
| 63 // This function is not called when the state change is trigerred by |
| 64 // public methods called by a user. For example, when a user reads |
| 65 // data by |read| and the state changes from waiting to readable, this |
| 66 // function will not be called. |
| 67 virtual void onStateChange() = 0; |
| 68 }; |
| 69 |
| 70 virtual ~BytesConsumer() {} |
| 71 |
| 72 // Reads data into |buffer| up to |size| bytes. The actual read size will |
| 73 // be stored in |*readSize|. This function cannot be called when a two-phase |
| 74 // read is in progress. |
| 75 // Returns Ok when readable. |
| 76 // Returns ShouldWait when it's waiting. |
| 77 // Returns Done when closed. |
| 78 // Returns Error when errored. |
| 79 // |buffer| can be null if |size| is 0. |
| 80 // |*readSize| will be set to 0 if not readable. |
| 81 virtual Result read(char* buffer, size_t /* size */, size_t* readSize); |
| 82 |
| 83 // Begins a two-phase read. On success, the function stores a buffer |
| 84 // that contains the read data of length |*available| into |*buffer|. |
| 85 // Returns Ok when readable. |
| 86 // Returns ShouldWait when it's waiting. |
| 87 // Returns Done when it's closed. |
| 88 // Returns Error when errored. |
| 89 // When not readable, the caller don't have to (and must not) call |
| 90 // endRead, because the read session implicitly ends in that case. |
| 91 // |
| 92 // |*buffer| will be set to null and |*available| will be set to 0 if not |
| 93 // readable. |
| 94 virtual Result beginRead(const char** buffer, size_t* available) = 0; |
| 95 |
| 96 // Ends a two-phase read. |
| 97 virtual Result endRead(size_t readSize) = 0; |
| 98 |
| 99 // Drains the data as a BlobDataHandle. |
| 100 // When this function returns a non-null value, the returned blob handle |
| 101 // contains bytes that would be read through read, beginRead and |
| 102 // endRead functions without calling this function. In such a case, this |
| 103 // object becomes closed. |
| 104 // When this function returns null value, this function does nothing. |
| 105 // When |policy| is DisallowBlobWithInvalidSize, this function doesn't |
| 106 // return a non-null blob handle with unspecified size. |
| 107 // The type of the returned blob handle may not be meaningful. |
| 108 virtual PassRefPtr<BlobDataHandle> drainAsBlobDataHandle(BlobSizePolicy = Bl
obSizePolicy::DisallowBlobWithInvalidSize) { return nullptr; } |
| 109 |
| 110 // Drains the data as an EncodedFormData. |
| 111 // When this function returns a non-null value, the returned form data |
| 112 // contains bytes that would be read through read, beginRead and |
| 113 // endRead functions without calling this function. In such a case, this |
| 114 // object becomes closed. |
| 115 // When this function returns null value, this function does nothing. |
| 116 // This function returns a non-null form data when the handle is made |
| 117 // from an EncodedFormData-convertible value. |
| 118 virtual PassRefPtr<EncodedFormData> drainAsFormData() { return nullptr; } |
| 119 |
| 120 // Sets a client. |
| 121 virtual void setClient(Client*) = 0; |
| 122 // Clears the set client. This can be called only when a client is set. |
| 123 virtual void clearClient() = 0; |
| 124 |
| 125 // Cancels this ByteConsumer. This function does nothing when |this| is |
| 126 // already closed or errored. Otherwise, this object becomes closed. |
| 127 virtual void cancel() = 0; |
| 128 |
| 129 // Returns the current state. |
| 130 virtual PublicState getPublicState() const = 0; |
| 131 |
| 132 // Returns the associated error of this object. This function can be called |
| 133 // only when errored. |
| 134 virtual Error getError() const = 0; |
| 135 |
| 136 // Each implementation should return a string that represents the |
| 137 // implementation for debug purpose. |
| 138 virtual String debugName() const = 0; |
| 139 |
| 140 DEFINE_INLINE_VIRTUAL_TRACE() {} |
| 141 |
| 142 protected: |
| 143 // This InternalState directly corresponds to the states in the class |
| 144 // comments. This enum is defined here for subclasses. |
| 145 enum class InternalState { |
| 146 Readable, |
| 147 Waiting, |
| 148 Closed, |
| 149 Errored, |
| 150 }; |
| 151 |
| 152 static PublicState getPublicStateFromInternalState(InternalState state) |
| 153 { |
| 154 switch (state) { |
| 155 case InternalState::Readable: |
| 156 case InternalState::Waiting: |
| 157 return PublicState::ReadableOrWaiting; |
| 158 case InternalState::Closed: |
| 159 return PublicState::Closed; |
| 160 case InternalState::Errored: |
| 161 return PublicState::Errored; |
| 162 } |
| 163 NOTREACHED(); |
| 164 return PublicState::ReadableOrWaiting; |
| 165 } |
| 166 }; |
| 167 |
| 168 } // namespace blink |
| 169 |
| 170 #endif // BytesConsumer_h |
| OLD | NEW |