Chromium Code Reviews| 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. The | |
| 22 // initial state is |waiting|. Once the state becomes closed or errored, it will | |
| 23 // never change. | |
| 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 enum class State { | |
| 33 Readable, | |
| 34 Waiting, | |
| 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 { | |
|
hiroshige
2016/07/08 05:32:37
Are the callbacks to Client all async?
yhirano
2016/07/12 10:50:57
I recovered "This function is not called when the
| |
| 56 public: | |
| 57 virtual ~Client() {} | |
| 58 | |
| 59 // The associated consumer gets readable. This function will be called | |
| 60 // when the consumer was waiting but is seemingly readable now. This | |
| 61 // means that this function will not be called even when some data | |
| 62 // arrives if the consumer already has non-empty readable data. | |
| 63 // It is NOT guaranteed that the consumer is not waiting when this | |
| 64 // function is called, i.e. it can be called more than needed. | |
| 65 // One can call the associated consumer's methods in this function. | |
| 66 virtual void onReadable() = 0; | |
| 67 | |
| 68 // This function will be called when the consumer was not closed but is | |
| 69 // closed now. | |
| 70 // This function will not be called when the associated consumer gets | |
| 71 // closed by BytesConsumer's public methods such as beginRead. | |
|
hiroshige
2016/07/08 05:32:37
How about describing the concrete cases of when on
yhirano
2016/07/12 10:50:57
Hmm, I'm not sure if it helps users. And I think s
| |
| 72 virtual void onClose() = 0; | |
| 73 | |
| 74 // This function will be called when the consumer was not errored but is | |
| 75 // errored now. | |
| 76 // This function will not be called when the associated consumer gets | |
| 77 // errored by BytesConsumer's public methods such as beginRead. | |
|
hiroshige
2016/07/08 05:32:37
Ditto.
From the comments, drainAsBlobDataHandle()
yhirano
2016/07/12 10:50:57
ditto
| |
| 78 virtual void onError(const Error&) = 0; | |
| 79 }; | |
| 80 | |
| 81 virtual ~BytesConsumer() {} | |
| 82 | |
| 83 // Reads data into |buffer| up to |size| bytes. The actual read size will | |
| 84 // be stored in |*readSize|. This function cannot be called when a two-phase | |
| 85 // read is in progress. | |
| 86 // Returns Ok when readable. | |
| 87 // Returns ShouldWait when it's waiting. | |
| 88 // Returns Done when closed. | |
| 89 // Returns Error when errored. | |
| 90 virtual Result read(char* buffer, size_t /* size */, size_t* readSize); | |
|
hiroshige
2016/07/08 05:32:37
Do we allow |buffer| == nullptr when |size| == 0?
yhirano
2016/07/12 10:50:57
true/false/true
| |
| 91 | |
| 92 // Begins a two-phase read. On success, the function stores a buffer | |
| 93 // that contains the read data of length |*available| into |*buffer|. | |
| 94 // Returns Ok when readable. | |
| 95 // Returns ShouldWait when it's waiting. | |
| 96 // Returns Done when it's closed. | |
| 97 // Returns Error when errored. | |
| 98 // When not readable, the caller don't have to (and must not) call | |
| 99 // endRead, because the read session implicitly ends in that case. | |
| 100 virtual Result beginRead(const char** buffer, size_t* available) = 0; | |
|
hiroshige
2016/07/08 05:32:37
Do we allow returning |Ok| with |*avaliable| == 0?
yhirano
2016/07/12 10:50:57
true/true/true
| |
| 101 | |
| 102 // Ends a two-phase read. | |
| 103 virtual Result endRead(size_t readSize) = 0; | |
| 104 | |
| 105 // Drains the data as a BlobDataHandle. | |
| 106 // When this function returns a non-null value, the returned blob handle | |
| 107 // contains bytes that would be read through read, beginRead and | |
| 108 // endRead functions without calling this function. In such a case, this | |
| 109 // object becomes closed. | |
| 110 // When this function returns null value, this function does nothing. | |
| 111 // When |policy| is DisallowBlobWithInvalidSize, this function doesn't | |
| 112 // return a non-null blob handle with unspecified size. | |
| 113 // The type of the returned handle may not be meaningful. | |
| 114 virtual PassRefPtr<BlobDataHandle> drainAsBlobDataHandle(BlobSizePolicy = Bl obSizePolicy::DisallowBlobWithInvalidSize) { return nullptr; } | |
| 115 | |
| 116 // Drains the data as an EncodedFormData. | |
| 117 // When this function returns a non-null value, the returned form data | |
| 118 // contains bytes that would be read through read, beginRead and | |
| 119 // endRead functions without calling this function. In such a case, this | |
| 120 // object becomes closed. | |
| 121 // When this function returns null value, this function does nothing. | |
| 122 // This function returns a non-null form data when the handle is made | |
| 123 // from an EncodedFormData-convertible value. | |
| 124 virtual PassRefPtr<EncodedFormData> drainAsFormData() { return nullptr; } | |
| 125 | |
| 126 // Sets a client. At most one client can be set on a BytesConsumer. | |
| 127 virtual void setClient(Client*) = 0; | |
| 128 // Clears the set client. This can be called only when a client is set. | |
| 129 virtual void clearClient() = 0; | |
| 130 | |
| 131 // Cancels this ByteConsumer. This function does nothing when |this| is | |
| 132 // already closed or errored. Otherwise, this object becomes closed. | |
| 133 virtual void cancel() = 0; | |
| 134 | |
| 135 // Returns the associated error of this object. This function can be called | |
| 136 // only when errored. | |
| 137 virtual Error getError() const = 0; | |
| 138 | |
| 139 // Each implementation should return a string that represents the | |
| 140 // implementation for debug purpose. | |
| 141 virtual String debugName() const = 0; | |
| 142 | |
| 143 DEFINE_INLINE_VIRTUAL_TRACE() {} | |
| 144 }; | |
| 145 | |
| 146 } // namespace blink | |
| 147 | |
| 148 #endif // BytesConsumer_h | |
| OLD | NEW |