Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: public/platform/WebDataConsumerHandle.h

Issue 1149563007: Add WebDataConsumerHandle::Reader interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
14 #include "public/platform/WebCommon.h"
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". A
20 // Client notification is called on the thread. 24 // WebDataConsumerHandle or its reader are called "waiting" when reading from
25 // the handle or reader returns ShouldWait.
21 class WebDataConsumerHandle { 26 class WebDataConsumerHandle {
22 public: 27 public:
23 // This corresponds to MojoReadDataFlags.
24 using Flags = unsigned; 28 using Flags = unsigned;
25 static const Flags FlagNone = 0; 29 static const Flags FlagNone = 0;
26 30
27 enum Result { 31 enum Result {
28 Ok, 32 Ok,
29 Done, 33 Done,
30 Busy, 34 Busy,
31 ShouldWait, 35 ShouldWait,
32 ResourceExhausted, 36 ResourceExhausted,
33 UnexpectedError, 37 UnexpectedError,
34 }; 38 };
35 39
36 // Client gets notification from the pipe. 40 // Client gets notification from the pipe.
37 class Client { 41 class Client {
38 public: 42 public:
39 virtual ~Client() { } 43 virtual ~Client() { }
40 // The associated handle gets readable. 44 // The associated handle gets readable. This function will be called
45 // when the associated reader was waiting but is not waiting any more.
46 // This means this function can be called when handle gets errored or
47 // closed. This also means that this function will not be called even
48 // when some data arrives if the handle already has non-empty readable
49 // data.
50 // It is not guaranteed that the handle is not waiting when this
51 // function is called, i.e. it can be called more than needed.
52 // One can use / destruct the associated reader in this function.
41 virtual void didGetReadable() = 0; 53 virtual void didGetReadable() = 0;
42 }; 54 };
43 55
56 // This class provides a means to read data from the associated handle. A
57 // Reader object is bound to the thread on which |obtainReader| is called.
58 // Any functions including the destructor should be called on the thread.
59 // A reader can outlive the associated handle. In such a case, the handle
60 // destruction will not affect the reader functionality.
61 // Reading functions may success (i.e. return Ok) or fail (otherwise), and
62 // the behavior which is not specified is unspecified.
63 class Reader {
64 public:
65 // Destructing a reader means it is released and a user can get another
66 // Reader by calling |obtainReader| on any thread again.
67 virtual ~Reader() { }
68
69 // Reads data into |data| up to |size| bytes. The actual read size will
70 // be stored in |*readSize|. This function cannot be called when a
71 // two-phase read is in progress.
72 // Returns Done when it reaches to the end of the data.
73 // Returns ShouldWait when the handle does not have data to read but
74 // it is not closed or errored.
75 virtual Result read(void* data, size_t /* size */, Flags, size_t* readSi ze)
76 {
77 BLINK_ASSERT_NOT_REACHED();
78 return UnexpectedError;
79 }
80
81 // Begins a two-phase read. On success, the function stores a buffer
82 // that contains the read data of length |*available| into |*buffer|.
83 // Returns Done when it reaches to the end of the data.
84 // Returns ShouldWait when the handle does not have data to read but
85 // it is not closed or errored.
86 // On fail, you don't have to (and should not) call endRead, because the
87 // read session implicitly ends in that case.
88 virtual Result beginRead(const void** buffer, Flags, size_t* available)
89 {
90 BLINK_ASSERT_NOT_REACHED();
91 return UnexpectedError;
92 }
93
94 // Ends a two-phase read.
95 // |readSize| indicates the actual read size.
96 virtual Result endRead(size_t readSize)
97 {
98 BLINK_ASSERT_NOT_REACHED();
99 return UnexpectedError;
100 }
101 };
102
44 virtual ~WebDataConsumerHandle() { } 103 virtual ~WebDataConsumerHandle() { }
45 104
105 // Returns a non-null reader. This function can be called only when this
106 // handle is not locked. |client| can be null. Otherwise, |*client| must be
107 // valid as long as the reader is valid. The returned reader is bound to
108 // the calling thread and client notification will be called on the thread
109 // if |client| is not null.
110 // If |client| is not null and the handle is not waiting, client
111 // notification is called asynchronously.
112 #if INSIDE_BLINK
113 PassOwnPtr<Reader> obtainReader(Client* client) { return adoptPtr(obtainRead erInternal(client)); }
114 #endif
115
116 private:
117 // The caller takes ownership of the returned object.
118 virtual Reader* obtainReaderInternal(Client* client)
119 {
120 BLINK_ASSERT_NOT_REACHED();
121 return nullptr;
122 }
123
124 // Below are deprecated functions that will be removed shortly. Use Reader
125 // instead.
126 public:
127 // Note: read / beginRead / endRead / unregisterClient must not be called
128 // when a client is regsitered. They must be called on the thread on which
129 // registerClient is called. registerClient must not be called when a client
130 // is already registered.
131
46 // Reads data into |data| up to |size| bytes. The actual read size will be 132 // 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 133 // stored in |*readSize|. This function cannot be called when a two-phase
48 // read is in progress. 134 // read is in progress.
49 // Returns Done when it reaches to the end of the data. 135 // 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; } 136 virtual Result read(void* data, size_t /* size */, Flags, size_t* readSize) { return UnexpectedError; }
51 137
52 // Begins a two-phase read. On success, the function stores a buffer that 138 // Begins a two-phase read. On success, the function stores a buffer that
53 // contains the read data of length |*available| into |*buffer|. 139 // contains the read data of length |*available| into |*buffer|.
54 // Returns Done when it reaches to the end of the data. 140 // 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 141 // On fail, you don't have to (and should not) call endRead, because the
56 // read session implicitly ends in that case. 142 // read session implicitly ends in that case.
57 virtual Result beginRead(const void** buffer, Flags, size_t* available) { re turn UnexpectedError; } 143 virtual Result beginRead(const void** buffer, Flags, size_t* available) { re turn UnexpectedError; }
58 144
59 // Ends a two-phase read. 145 // Ends a two-phase read.
60 // |readSize| indicates the actual read size. 146 // |readSize| indicates the actual read size.
61 virtual Result endRead(size_t readSize) { return UnexpectedError; } 147 virtual Result endRead(size_t readSize) { return UnexpectedError; }
62 148
63 // Registers |client| to this handle. The client must not be null and must 149 // 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). 150 // be valid until it is unregistered (or the handle is destructed).
65 // Only one registration can be made for a handle at a time. 151 // Only one registration can be made for a handle at a time.
66 virtual void registerClient(Client* /* client */) { } 152 virtual void registerClient(Client* /* client */) { }
67 153
68 // Unregisters |client| from this handle. 154 // Unregisters |client| from this handle.
69 virtual void unregisterClient() { } 155 virtual void unregisterClient() { }
70 }; 156 };
71 157
72 } // namespace blink 158 } // namespace blink
73 159
74 #endif // WebDataConsumerHandle_h 160 #endif // WebDataConsumerHandle_h
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698