Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WebDataConsumerHandle_h | 5 #ifndef WebDataConsumerHandle_h |
| 6 #define WebDataConsumerHandle_h | 6 #define WebDataConsumerHandle_h |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #if INSIDE_BLINK | |
| 11 #include "wtf/PassOwnPtr.h" | |
| 12 #else | |
| 13 #include <base/memory/scoped_ptr.h> | |
|
tkent
2015/06/12 07:51:49
We don't allow base/ dependency yet though there
yhirano
2015/06/12 08:13:13
Done.
| |
| 14 #endif | |
| 15 | |
| 16 #include "public/platform/WebCommon.h" | |
| 17 | |
| 10 namespace blink { | 18 namespace blink { |
| 11 | 19 |
| 12 // WebDataConsumerHandle represents the "consumer" side of a data pipe. A user | 20 // WebDataConsumerHandle represents the "consumer" side of a data pipe. A user |
| 13 // can read data from it. | 21 // can read data from it. |
| 14 // This data type is basically a wrapper of mojo data pipe consumer handle. | |
| 15 // See mojo/public/c/system/data_pipe.h for details. | |
| 16 // | 22 // |
| 17 // Note: | 23 // A WebDataConsumerHandle is a thread-safe object. A user can call |
| 18 // - If you register a client, all of read / beginRead / endRead / | 24 // |obtainReader| or destruct the object on any thread. |
| 19 // registerClient / unregisterClient must be called on the same thread. | 25 // A WebDataConsumerHandle having a reader is called "locked". A |
| 20 // Client notification is called on the thread. | 26 // WebDataConsumerHandle or its reader are called "waiting" when reading from |
| 27 // the handle or reader returns ShouldWait. | |
| 21 class WebDataConsumerHandle { | 28 class WebDataConsumerHandle { |
| 22 public: | 29 public: |
| 23 // This corresponds to MojoReadDataFlags. | |
| 24 using Flags = unsigned; | 30 using Flags = unsigned; |
| 25 static const Flags FlagNone = 0; | 31 static const Flags FlagNone = 0; |
| 26 | 32 |
| 27 enum Result { | 33 enum Result { |
| 28 Ok, | 34 Ok, |
| 29 Done, | 35 Done, |
| 30 Busy, | 36 Busy, |
| 31 ShouldWait, | 37 ShouldWait, |
| 32 ResourceExhausted, | 38 ResourceExhausted, |
| 33 UnexpectedError, | 39 UnexpectedError, |
| 34 }; | 40 }; |
| 35 | 41 |
| 36 // Client gets notification from the pipe. | 42 // Client gets notification from the pipe. |
| 37 class Client { | 43 class Client { |
| 38 public: | 44 public: |
| 39 virtual ~Client() { } | 45 virtual ~Client() { } |
| 40 // The associated handle gets readable. | 46 // The associated handle gets readable. This function will be called |
| 47 // when the associated reader was waiting but is not waiting any more. | |
| 48 // This means this function can be called when handle gets errored or | |
| 49 // closed. This also means that this function will not be called even | |
| 50 // when some data arrives if the handle already has non-empty readable | |
| 51 // data. | |
| 52 // It is not guaranteed that the handle is not waiting when this | |
| 53 // function is called, i.e. it can be called more than needed. | |
| 54 // One can use / destruct the associated reader in this function. | |
| 41 virtual void didGetReadable() = 0; | 55 virtual void didGetReadable() = 0; |
| 42 }; | 56 }; |
| 43 | 57 |
| 58 // This class provides a means to read data from the associated handle. A | |
| 59 // Reader object is bound to the thread on which |obtainReader| is called. | |
| 60 // Any functions including the destructor should be called on the thread. | |
| 61 // A reader can outlive the associated handle. In such a case, the handle | |
| 62 // destruction will not affect the reader functionality. | |
| 63 // Reading functions may success (i.e. return Ok) or fail (otherwise), and | |
| 64 // the behavior which is not specified is unspecified. | |
| 65 class Reader { | |
| 66 public: | |
| 67 // Destructing a reader means it is released and a user can get another | |
| 68 // Reader by calling |obtainReader| on any thread again. | |
| 69 virtual ~Reader() { } | |
| 70 | |
| 71 // Reads data into |data| up to |size| bytes. The actual read size will | |
| 72 // be stored in |*readSize|. This function cannot be called when a | |
| 73 // two-phase read is in progress. | |
| 74 // Returns Done when it reaches to the end of the data. | |
| 75 // Returns ShouldWait when the handle does not have data to read but | |
| 76 // it is not closed or errored. | |
| 77 virtual Result read(void* data, size_t /* size */, Flags, size_t* readSi ze) | |
| 78 { | |
| 79 BLINK_ASSERT_NOT_REACHED(); | |
| 80 return UnexpectedError; | |
| 81 } | |
| 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 Done when it reaches to the end of the data. | |
| 86 // Returns ShouldWait when the handle does not have data to read but | |
| 87 // it is not closed or errored. | |
| 88 // On fail, you don't have to (and should not) call endRead, because the | |
| 89 // read session implicitly ends in that case. | |
| 90 virtual Result beginRead(const void** buffer, Flags, size_t* available) | |
| 91 { | |
| 92 BLINK_ASSERT_NOT_REACHED(); | |
| 93 return UnexpectedError; | |
| 94 } | |
| 95 | |
| 96 // Ends a two-phase read. | |
| 97 // |readSize| indicates the actual read size. | |
| 98 virtual Result endRead(size_t readSize) | |
| 99 { | |
| 100 BLINK_ASSERT_NOT_REACHED(); | |
| 101 return UnexpectedError; | |
| 102 } | |
| 103 }; | |
| 104 | |
| 44 virtual ~WebDataConsumerHandle() { } | 105 virtual ~WebDataConsumerHandle() { } |
| 45 | 106 |
| 107 // Returns a non-null reader. This function can be called only when this | |
| 108 // handle is not locked. |client| can be null. Otherwise, |*client| must be | |
| 109 // valid as long as the reader is valid. The returned reader is bound to | |
| 110 // the calling thread and client notification will be called on the thread | |
| 111 // if |client| is not null. | |
| 112 // If |client| is not null and the handle is not waiting, client | |
| 113 // notification is called asynchronously. | |
| 114 #if INSIDE_BLINK | |
| 115 PassOwnPtr<Reader> obtainReader(Client* client) { return adoptPtr(obtainRead erInternal(client)); } | |
| 116 #else | |
| 117 scoped_ptr<Reader> obtainReader(Client* client) { return scoped_ptr<Reader>( obtainReaderInternal(client)); } | |
| 118 #endif | |
| 119 | |
| 120 private: | |
| 121 // The caller takes ownership of the returned object. | |
| 122 virtual Reader* obtainReaderInternal(Client* client) | |
| 123 { | |
| 124 BLINK_ASSERT_NOT_REACHED(); | |
| 125 return nullptr; | |
| 126 } | |
| 127 | |
| 128 // Below are deprecated functions that will be removed shortly. Use Reader | |
| 129 // instead. | |
| 130 public: | |
| 131 // Note: read / beginRead / endRead / unregisterClient must not be called | |
| 132 // when a client is regsitered. They must be called on the thread on which | |
| 133 // registerClient is called. registerClient must not be called when a client | |
| 134 // is already registered. | |
| 135 | |
| 46 // Reads data into |data| up to |size| bytes. The actual read size will be | 136 // Reads data into |data| up to |size| bytes. The actual read size will be |
| 47 // stored in |*readSize|. This function cannot be called when a two-phase | 137 // stored in |*readSize|. This function cannot be called when a two-phase |
| 48 // read is in progress. | 138 // read is in progress. |
| 49 // Returns Done when it reaches to the end of the data. | 139 // Returns Done when it reaches to the end of the data. |
| 50 virtual Result read(void* data, size_t /* size */, Flags, size_t* readSize) { return UnexpectedError; } | 140 virtual Result read(void* data, size_t /* size */, Flags, size_t* readSize) { return UnexpectedError; } |
| 51 | 141 |
| 52 // Begins a two-phase read. On success, the function stores a buffer that | 142 // Begins a two-phase read. On success, the function stores a buffer that |
| 53 // contains the read data of length |*available| into |*buffer|. | 143 // contains the read data of length |*available| into |*buffer|. |
| 54 // Returns Done when it reaches to the end of the data. | 144 // Returns Done when it reaches to the end of the data. |
| 55 // On fail, you don't have to (and should not) call endRead, because the | 145 // On fail, you don't have to (and should not) call endRead, because the |
| 56 // read session implicitly ends in that case. | 146 // read session implicitly ends in that case. |
| 57 virtual Result beginRead(const void** buffer, Flags, size_t* available) { re turn UnexpectedError; } | 147 virtual Result beginRead(const void** buffer, Flags, size_t* available) { re turn UnexpectedError; } |
| 58 | 148 |
| 59 // Ends a two-phase read. | 149 // Ends a two-phase read. |
| 60 // |readSize| indicates the actual read size. | 150 // |readSize| indicates the actual read size. |
| 61 virtual Result endRead(size_t readSize) { return UnexpectedError; } | 151 virtual Result endRead(size_t readSize) { return UnexpectedError; } |
| 62 | 152 |
| 63 // Registers |client| to this handle. The client must not be null and must | 153 // Registers |client| to this handle. The client must not be null and must |
| 64 // be valid until it is unregistered (or the handle is destructed). | 154 // be valid until it is unregistered (or the handle is destructed). |
| 65 // Only one registration can be made for a handle at a time. | 155 // Only one registration can be made for a handle at a time. |
| 66 virtual void registerClient(Client* /* client */) { } | 156 virtual void registerClient(Client* /* client */) { } |
| 67 | 157 |
| 68 // Unregisters |client| from this handle. | 158 // Unregisters |client| from this handle. |
| 69 virtual void unregisterClient() { } | 159 virtual void unregisterClient() { } |
| 70 }; | 160 }; |
| 71 | 161 |
| 72 } // namespace blink | 162 } // namespace blink |
| 73 | 163 |
| 74 #endif // WebDataConsumerHandle_h | 164 #endif // WebDataConsumerHandle_h |
| OLD | NEW |