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 #endif | |
| 13 | |
| 10 namespace blink { | 14 namespace blink { |
| 11 | 15 |
| 12 // WebDataConsumerHandle represents the "consumer" side of a data pipe. A user | 16 // WebDataConsumerHandle represents the "consumer" side of a data pipe. A user |
| 13 // can read data from it. | 17 // 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 // | 18 // |
| 17 // Note: | 19 // A WebDataConsumerHandle is a thread-safe object. A user can call |
| 18 // - If you register a client, all of read / beginRead / endRead / | 20 // |obtainReader| or destruct the object on any thread. |
| 19 // registerClient / unregisterClient must be called on the same thread. | 21 // A WebDataConsumerHandle which has an active reader is called "locked". |
|
hiroshige
2015/06/04 10:06:14
nit: the word "active reader" might be confusing (
yhirano
2015/06/04 12:04:09
Done.
| |
| 20 // Client notification is called on the thread. | |
| 21 class WebDataConsumerHandle { | 22 class WebDataConsumerHandle { |
| 22 public: | 23 public: |
| 23 // This corresponds to MojoReadDataFlags. | |
| 24 using Flags = unsigned; | 24 using Flags = unsigned; |
| 25 static const Flags FlagNone = 0; | 25 static const Flags FlagNone = 0; |
| 26 | 26 |
| 27 enum Result { | 27 enum Result { |
| 28 Ok, | 28 Ok, |
| 29 Done, | 29 Done, |
| 30 Busy, | 30 Busy, |
| 31 ShouldWait, | 31 ShouldWait, |
| 32 ResourceExhausted, | 32 ResourceExhausted, |
| 33 UnexpectedError, | 33 UnexpectedError, |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 // Client gets notification from the pipe. | 36 // Client gets notification from the pipe. |
| 37 class Client { | 37 class Client { |
| 38 public: | 38 public: |
| 39 virtual ~Client() { } | 39 virtual ~Client() { } |
| 40 // The associated handle gets readable. | 40 // The associated handle gets readable. |
| 41 virtual void didGetReadable() = 0; | 41 virtual void didGetReadable() = 0; |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 // This class provides a means to read data from the associated handle. A | |
| 45 // Reader object is bound to the thread on which |obtainReader| is called. | |
| 46 // Any functions including the destructor should be called on the thread. | |
| 47 class Reader { | |
| 48 public: | |
| 49 // Destructing a reader means it is released and a user can get another | |
| 50 // Reader by calling |obtainReader| on any thread again. | |
| 51 virtual ~Reader() { } | |
| 52 | |
| 53 // Reads data into |data| up to |size| bytes. The actual read size will | |
| 54 // be stored in |*readSize|. This function cannot be called when a | |
| 55 // two-phase read is in progress. | |
| 56 // Returns Done when it reaches to the end of the data. | |
| 57 virtual Result read(void* data, size_t /* size */, Flags, size_t* readSi ze) { return UnexpectedError; } | |
| 58 | |
| 59 // Begins a two-phase read. On success, the function stores a buffer | |
| 60 // that contains the read data of length |*available| into |*buffer|. | |
| 61 // Returns Done when it reaches to the end of the data. | |
| 62 // On fail, you don't have to (and should not) call endRead, because the | |
| 63 // read session implicitly ends in that case. | |
| 64 virtual Result beginRead(const void** buffer, Flags, size_t* available) { return UnexpectedError; } | |
| 65 | |
| 66 // Ends a two-phase read. | |
| 67 // |readSize| indicates the actual read size. | |
| 68 virtual Result endRead(size_t readSize) { return UnexpectedError; } | |
| 69 }; | |
| 70 | |
| 44 virtual ~WebDataConsumerHandle() { } | 71 virtual ~WebDataConsumerHandle() { } |
| 45 | 72 |
| 73 // Returns a reader. This function can be called when there aren't any | |
| 74 // other readers. The returned reader is bound to the calling thread and | |
|
hiroshige
2015/06/04 10:06:15
What occurs when there is another reader when we c
yhirano
2015/06/04 12:04:09
It is simply not allowed. I will put an assertion,
| |
| 75 // client notification will be called on the thread. | |
| 76 #if INSIDE_BLINK | |
| 77 PassOwnPtr<Reader> obtainReader(Client* client) { return adoptPtr(obtainRead erInternal(client)); } | |
|
hiroshige
2015/06/04 10:06:14
Can |client| be nullptr? (I assume yes)
yhirano
2015/06/04 12:04:09
Done.
| |
| 78 #endif | |
| 79 | |
| 80 private: | |
| 81 // The caller takes ownership of the returned object. | |
| 82 virtual Reader* obtainReaderInternal(Client* client) { return nullptr; } | |
| 83 | |
| 84 // Below are deprecated functions. Use Reader instaed. | |
| 85 public: | |
| 86 // Note: read / beginRead / endRead / unregisterClient must not be called | |
| 87 // when a client is regsitered. They must be called on the thread on which | |
| 88 // registerClient is called. registerClient must not be called when a client | |
| 89 // is already registered. | |
| 90 | |
| 46 // Reads data into |data| up to |size| bytes. The actual read size will be | 91 // 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 | 92 // stored in |*readSize|. This function cannot be called when a two-phase |
| 48 // read is in progress. | 93 // read is in progress. |
| 49 // Returns Done when it reaches to the end of the data. | 94 // 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; } | 95 virtual Result read(void* data, size_t /* size */, Flags, size_t* readSize) { return UnexpectedError; } |
| 51 | 96 |
| 52 // Begins a two-phase read. On success, the function stores a buffer that | 97 // Begins a two-phase read. On success, the function stores a buffer that |
| 53 // contains the read data of length |*available| into |*buffer|. | 98 // contains the read data of length |*available| into |*buffer|. |
| 54 // Returns Done when it reaches to the end of the data. | 99 // 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 | 100 // On fail, you don't have to (and should not) call endRead, because the |
| 56 // read session implicitly ends in that case. | 101 // read session implicitly ends in that case. |
| 57 virtual Result beginRead(const void** buffer, Flags, size_t* available) { re turn UnexpectedError; } | 102 virtual Result beginRead(const void** buffer, Flags, size_t* available) { re turn UnexpectedError; } |
| 58 | 103 |
| 59 // Ends a two-phase read. | 104 // Ends a two-phase read. |
| 60 // |readSize| indicates the actual read size. | 105 // |readSize| indicates the actual read size. |
| 61 virtual Result endRead(size_t readSize) { return UnexpectedError; } | 106 virtual Result endRead(size_t readSize) { return UnexpectedError; } |
| 62 | 107 |
| 63 // Registers |client| to this handle. The client must not be null and must | 108 // 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). | 109 // be valid until it is unregistered (or the handle is destructed). |
| 65 // Only one registration can be made for a handle at a time. | 110 // Only one registration can be made for a handle at a time. |
| 66 virtual void registerClient(Client* /* client */) { } | 111 virtual void registerClient(Client* /* client */) { } |
| 67 | 112 |
| 68 // Unregisters |client| from this handle. | 113 // Unregisters |client| from this handle. |
| 69 virtual void unregisterClient() { } | 114 virtual void unregisterClient() { } |
| 70 }; | 115 }; |
| 71 | 116 |
| 72 } // namespace blink | 117 } // namespace blink |
| 73 | 118 |
| 74 #endif // WebDataConsumerHandle_h | 119 #endif // WebDataConsumerHandle_h |
| OLD | NEW |