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