OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 5 #ifndef MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
6 #define THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 6 #define MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 14 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 15 #include "mojo/edk/system/message_in_transit.h" |
| 16 #include "mojo/edk/system/message_in_transit_queue.h" |
| 17 #include "mojo/edk/system/system_impl_export.h" |
13 #include "mojo/public/cpp/system/macros.h" | 18 #include "mojo/public/cpp/system/macros.h" |
14 #include "third_party/mojo/src/mojo/edk/embedder/platform_handle_vector.h" | |
15 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h" | |
16 #include "third_party/mojo/src/mojo/edk/system/message_in_transit.h" | |
17 #include "third_party/mojo/src/mojo/edk/system/message_in_transit_queue.h" | |
18 #include "third_party/mojo/src/mojo/edk/system/system_impl_export.h" | |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 class MessageLoopForIO; | 21 class MessageLoopForIO; |
22 } | 22 } |
23 | 23 |
24 namespace mojo { | 24 namespace mojo { |
25 namespace system { | 25 namespace edk { |
26 | 26 |
27 // |RawChannel| is an interface and base class for objects that wrap an OS | 27 // |RawChannel| is an interface and base class for objects that wrap an OS |
28 // "pipe". It presents the following interface to users: | 28 // "pipe". It presents the following interface to users: |
29 // - Receives and dispatches messages on an I/O thread (running a | 29 // - Receives and dispatches messages on an I/O thread (running a |
30 // |MessageLoopForIO|. | 30 // |MessageLoopForIO|. |
31 // - Provides a thread-safe way of writing messages (|WriteMessage()|); | 31 // - Provides a thread-safe way of writing messages (|WriteMessage()|); |
32 // writing/queueing messages will not block and is atomic from the point of | 32 // writing/queueing messages will not block and is atomic from the point of |
33 // view of the caller. If necessary, messages are queued (to be written on | 33 // view of the caller. If necessary, messages are queued (to be written on |
34 // the aforementioned thread). | 34 // the aforementioned thread). |
35 // | 35 // |
36 // OS-specific implementation subclasses are to be instantiated using the | 36 // OS-specific implementation subclasses are to be instantiated using the |
37 // |Create()| static factory method. | 37 // |Create()| static factory method. |
38 // | 38 // |
39 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in | 39 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in |
40 // general its methods should only be used on the I/O thread, i.e., the thread | 40 // general its methods should only be used on the I/O thread, i.e., the thread |
41 // on which |Init()| is called). | 41 // on which |Init()| is called). |
42 class MOJO_SYSTEM_IMPL_EXPORT RawChannel { | 42 class MOJO_SYSTEM_IMPL_EXPORT RawChannel { |
43 public: | 43 public: |
44 // This object may be destroyed on any thread (if |Init()| was called, after | |
45 // |Shutdown()| was called). | |
46 virtual ~RawChannel(); | |
47 | 44 |
48 // The |Delegate| is only accessed on the same thread as the message loop | 45 // The |Delegate| is only accessed on the same thread as the message loop |
49 // (passed in on creation). | 46 // (passed in on creation). |
50 class MOJO_SYSTEM_IMPL_EXPORT Delegate { | 47 class MOJO_SYSTEM_IMPL_EXPORT Delegate { |
51 public: | 48 public: |
52 enum Error { | 49 enum Error { |
53 // Failed read due to raw channel shutdown (e.g., on the other side). | 50 // Failed read due to raw channel shutdown (e.g., on the other side). |
54 ERROR_READ_SHUTDOWN, | 51 ERROR_READ_SHUTDOWN, |
55 // Failed read due to raw channel being broken (e.g., if the other side | 52 // Failed read due to raw channel being broken (e.g., if the other side |
56 // died without shutting down). | 53 // died without shutting down). |
57 ERROR_READ_BROKEN, | 54 ERROR_READ_BROKEN, |
58 // Received a bad message. | 55 // Received a bad message. |
59 ERROR_READ_BAD_MESSAGE, | 56 ERROR_READ_BAD_MESSAGE, |
60 // Unknown read error. | 57 // Unknown read error. |
61 ERROR_READ_UNKNOWN, | 58 ERROR_READ_UNKNOWN, |
62 // Generic write error. | 59 // Generic write error. |
63 ERROR_WRITE | 60 ERROR_WRITE |
64 }; | 61 }; |
65 | 62 |
66 // Called when a message is read. This may call the |RawChannel|'s | 63 // Called when a message is read. The delegate may not call back into this |
67 // |Shutdown()| and then (if desired) destroy it. | 64 // object synchronously. |
68 virtual void OnReadMessage( | 65 virtual void OnReadMessage( |
69 const MessageInTransit::View& message_view, | 66 const MessageInTransit::View& message_view, |
70 embedder::ScopedPlatformHandleVectorPtr platform_handles) = 0; | 67 ScopedPlatformHandleVectorPtr platform_handles) = 0; |
71 | 68 |
72 // Called when there's a (fatal) error. This may call the |RawChannel|'s | 69 // Called when there's a (fatal) error. The delegate may not call back into |
73 // |Shutdown()| and then (if desired) destroy it. | 70 // this object synchronously. |
74 // | 71 // |
75 // For each raw channel, there'll be at most one |ERROR_READ_...| and at | 72 // For each raw channel, there'll be at most one |ERROR_READ_...| and at |
76 // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|, | 73 // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|, |
77 // |OnReadMessage()| won't be called again. | 74 // |OnReadMessage()| won't be called again. |
78 virtual void OnError(Error error) = 0; | 75 virtual void OnError(Error error) = 0; |
79 | 76 |
80 protected: | 77 protected: |
81 virtual ~Delegate() {} | 78 virtual ~Delegate() {} |
82 }; | 79 }; |
83 | 80 |
84 // Static factory method. |handle| should be a handle to a | 81 // Static factory method. |handle| should be a handle to a |
85 // (platform-appropriate) bidirectional communication channel (e.g., a socket | 82 // (platform-appropriate) bidirectional communication channel (e.g., a socket |
86 // on POSIX, a named pipe on Windows). | 83 // on POSIX, a named pipe on Windows). |
87 static scoped_ptr<RawChannel> Create(embedder::ScopedPlatformHandle handle); | 84 static RawChannel* Create(ScopedPlatformHandle handle); |
| 85 |
| 86 // Returns the amount of space needed in the |MessageInTransit|'s |
| 87 // |TransportData|'s "platform handle table" per platform handle (to be |
| 88 // attached to a message). (This amount may be zero.) |
| 89 static size_t GetSerializedPlatformHandleSize(); |
88 | 90 |
89 // This must be called (on an I/O thread) before this object is used. Does | 91 // This must be called (on an I/O thread) before this object is used. Does |
90 // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must | 92 // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must |
91 // remain alive until |Shutdown()| is called (unless this fails); |delegate| | 93 // remain alive until |Shutdown()| is called (unless this fails); |delegate| |
92 // will no longer be used after |Shutdown()|. | 94 // will no longer be used after |Shutdown()|. |
93 void Init(Delegate* delegate); | 95 void Init(Delegate* delegate); |
94 | 96 |
95 // This must be called (on the I/O thread) before this object is destroyed. | 97 // This must be called (on the I/O thread) before this object is destroyed. |
96 void Shutdown(); | 98 void Shutdown(); |
97 | 99 |
| 100 // Returns the platform handle for the pipe synchronously. |
| 101 // |read_buffer| contains partially read data, if any. |
| 102 // NOTE: After calling this, consider the channel shutdown and don't call into |
| 103 // it anymore |
| 104 ScopedPlatformHandle ReleaseHandle(std::vector<char>* read_buffer); |
| 105 |
98 // Writes the given message (or schedules it to be written). |message| must | 106 // Writes the given message (or schedules it to be written). |message| must |
99 // have no |Dispatcher|s still attached (i.e., | 107 // have no |Dispatcher|s still attached (i.e., |
100 // |SerializeAndCloseDispatchers()| should have been called). This method is | 108 // |SerializeAndCloseDispatchers()| should have been called). This method is |
101 // thread-safe and may be called from any thread. Returns true on success. | 109 // thread-safe and may be called from any thread. Returns true on success. |
102 bool WriteMessage(scoped_ptr<MessageInTransit> message); | 110 bool WriteMessage(scoped_ptr<MessageInTransit> message); |
103 | 111 |
104 // Returns true if the write buffer is empty (i.e., all messages written using | 112 // Returns true if the write buffer is empty (i.e., all messages written using |
105 // |WriteMessage()| have actually been sent. | 113 // |WriteMessage()| have actually been sent. |
106 // TODO(vtl): We should really also notify our delegate when the write buffer | 114 // TODO(vtl): We should really also notify our delegate when the write buffer |
107 // becomes empty (or something like that). | 115 // becomes empty (or something like that). |
108 bool IsWriteBufferEmpty(); | 116 bool IsWriteBufferEmpty(); |
109 | 117 |
110 // Returns the amount of space needed in the |MessageInTransit|'s | 118 bool IsReadBufferEmpty(); |
111 // |TransportData|'s "platform handle table" per platform handle (to be | 119 |
112 // attached to a message). (This amount may be zero.) | 120 void SetInitialReadBufferData(char* data, size_t size); |
113 virtual size_t GetSerializedPlatformHandleSize() const = 0; | 121 |
| 122 // Checks if this RawChannel is the other endpoint to |other|. |
| 123 bool IsOtherEndOf(RawChannel* other); |
114 | 124 |
115 protected: | 125 protected: |
116 // Result of I/O operations. | 126 // Result of I/O operations. |
117 enum IOResult { | 127 enum IOResult { |
118 IO_SUCCEEDED, | 128 IO_SUCCEEDED, |
119 // Failed due to a (probably) clean shutdown (e.g., of the other end). | 129 // Failed due to a (probably) clean shutdown (e.g., of the other end). |
120 IO_FAILED_SHUTDOWN, | 130 IO_FAILED_SHUTDOWN, |
121 // Failed due to the connection being broken (e.g., the other end dying). | 131 // Failed due to the connection being broken (e.g., the other end dying). |
122 IO_FAILED_BROKEN, | 132 IO_FAILED_BROKEN, |
123 // Failed due to some other (unexpected) reason. | 133 // Failed due to some other (unexpected) reason. |
124 IO_FAILED_UNKNOWN, | 134 IO_FAILED_UNKNOWN, |
125 IO_PENDING | 135 IO_PENDING |
126 }; | 136 }; |
127 | 137 |
128 class MOJO_SYSTEM_IMPL_EXPORT ReadBuffer { | 138 class MOJO_SYSTEM_IMPL_EXPORT ReadBuffer { |
129 public: | 139 public: |
130 ReadBuffer(); | 140 ReadBuffer(); |
131 ~ReadBuffer(); | 141 ~ReadBuffer(); |
132 | 142 |
133 void GetBuffer(char** addr, size_t* size); | 143 void GetBuffer(char** addr, size_t* size); |
134 | 144 |
| 145 void Reset() {num_valid_bytes_ = 0; } |
| 146 |
| 147 // temp for debugging |
| 148 // TODO(jam): pass in a cleaner way to ReleaseHandle, just like shutdown |
| 149 // case. |
| 150 char* buffer() { return &buffer_[0]; } |
| 151 size_t num_valid_bytes() {return num_valid_bytes_;} |
| 152 |
135 private: | 153 private: |
136 friend class RawChannel; | 154 friend class RawChannel; |
137 | 155 |
138 // We store data from |[Schedule]Read()|s in |buffer_|. The start of | 156 // We store data from |[Schedule]Read()|s in |buffer_|. The start of |
139 // |buffer_| is always aligned with a message boundary (we will copy memory | 157 // |buffer_| is always aligned with a message boundary (we will copy memory |
140 // to ensure this), but |buffer_| may be larger than the actual number of | 158 // to ensure this), but |buffer_| may be larger than the actual number of |
141 // bytes we have. | 159 // bytes we have. |
142 std::vector<char> buffer_; | 160 std::vector<char> buffer_; |
143 size_t num_valid_bytes_; | 161 size_t num_valid_bytes_; |
144 | 162 |
145 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadBuffer); | 163 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadBuffer); |
146 }; | 164 }; |
147 | 165 |
148 class MOJO_SYSTEM_IMPL_EXPORT WriteBuffer { | 166 class MOJO_SYSTEM_IMPL_EXPORT WriteBuffer { |
149 public: | 167 public: |
150 struct Buffer { | 168 struct Buffer { |
151 const char* addr; | 169 const char* addr; |
152 size_t size; | 170 size_t size; |
153 }; | 171 }; |
154 | 172 |
155 explicit WriteBuffer(size_t serialized_platform_handle_size); | 173 WriteBuffer(); |
156 ~WriteBuffer(); | 174 ~WriteBuffer(); |
157 | 175 |
158 // Returns true if there are (more) platform handles to be sent (from the | 176 // Returns true if there are (more) platform handles to be sent (from the |
159 // front of |message_queue_|). | 177 // front of |message_queue_|). |
160 bool HavePlatformHandlesToSend() const; | 178 bool HavePlatformHandlesToSend() const; |
161 // Gets platform handles to be sent (from the front of |message_queue_|). | 179 // Gets platform handles to be sent (from the front of |message_queue_|). |
162 // This should only be called if |HavePlatformHandlesToSend()| returned | 180 // This should only be called if |HavePlatformHandlesToSend()| returned |
163 // true. There are two components to this: the actual |PlatformHandle|s | 181 // true. There are two components to this: the actual |PlatformHandle|s |
164 // (which should be closed once sent) and any additional serialization | 182 // (which should be closed once sent) and any additional serialization |
165 // information (which will be embedded in the message's data; there are | 183 // information (which will be embedded in the message's data; there are |
166 // |GetSerializedPlatformHandleSize()| bytes per handle). Once all platform | 184 // |GetSerializedPlatformHandleSize()| bytes per handle). Once all platform |
167 // handles have been sent, the message data should be written next (see | 185 // handles have been sent, the message data should be written next (see |
168 // |GetBuffers()|). | 186 // |GetBuffers()|). |
169 // TODO(vtl): Maybe this method should be const, but | 187 // TODO(vtl): Maybe this method should be const, but |
170 // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies | 188 // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies |
171 // state). | 189 // state). |
172 void GetPlatformHandlesToSend(size_t* num_platform_handles, | 190 void GetPlatformHandlesToSend(size_t* num_platform_handles, |
173 embedder::PlatformHandle** platform_handles, | 191 PlatformHandle** platform_handles, |
174 void** serialization_data); | 192 void** serialization_data); |
175 | 193 |
176 // Gets buffers to be written. These buffers will always come from the front | 194 // Gets buffers to be written. These buffers will always come from the front |
177 // of |message_queue_|. Once they are completely written, the front | 195 // of |message_queue_|. Once they are completely written, the front |
178 // |MessageInTransit| should be popped (and destroyed); this is done in | 196 // |MessageInTransit| should be popped (and destroyed); this is done in |
179 // |OnWriteCompletedNoLock()|. | 197 // |OnWriteCompletedNoLock()|. |
180 void GetBuffers(std::vector<Buffer>* buffers) const; | 198 void GetBuffers(std::vector<Buffer>* buffers) const; |
181 | 199 |
182 private: | 200 |
| 201 |
| 202 |
| 203 |
| 204 // temp for testing |
| 205 size_t queue_size() {return message_queue_.Size();} |
| 206 |
| 207 // TODO(jam): better way of giving buffer on release handle |
| 208 MessageInTransitQueue* message_queue() { return &message_queue_; } |
| 209 |
| 210 |
| 211 |
| 212 // TODO JAM REMOVE AND ADD METHODS |
| 213 // private: |
183 friend class RawChannel; | 214 friend class RawChannel; |
184 | 215 |
185 const size_t serialized_platform_handle_size_; | 216 size_t serialized_platform_handle_size_; |
186 | 217 |
187 MessageInTransitQueue message_queue_; | 218 MessageInTransitQueue message_queue_; |
188 // Platform handles are sent before the message data, but doing so may | 219 // Platform handles are sent before the message data, but doing so may |
189 // require several passes. |platform_handles_offset_| indicates the position | 220 // require several passes. |platform_handles_offset_| indicates the position |
190 // in the first message's vector of platform handles to send next. | 221 // in the first message's vector of platform handles to send next. |
191 size_t platform_handles_offset_; | 222 size_t platform_handles_offset_; |
192 // The first message's data may have been partially sent. |data_offset_| | 223 // The first message's data may have been partially sent. |data_offset_| |
193 // indicates the position in the first message's data to start the next | 224 // indicates the position in the first message's data to start the next |
194 // write. | 225 // write. |
195 size_t data_offset_; | 226 size_t data_offset_; |
196 | 227 |
197 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteBuffer); | 228 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteBuffer); |
198 }; | 229 }; |
199 | 230 |
200 RawChannel(); | 231 RawChannel(); |
201 | 232 |
| 233 // Shutdown must be called on the IO thread. This object deletes itself once |
| 234 // it's flushed all pending writes and insured that the other side of the pipe |
| 235 // read them. |
| 236 virtual ~RawChannel(); |
| 237 |
202 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 238 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
203 // |write_lock_| held. This object may be destroyed by this call. | 239 // |write_lock_| held. This object may be destroyed by this call. |
204 void OnReadCompleted(IOResult io_result, size_t bytes_read); | 240 void OnReadCompleted(IOResult io_result, size_t bytes_read); |
205 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 241 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
206 // |write_lock_| held. This object may be destroyed by this call. | 242 // |write_lock_| held. This object may be destroyed by this call. |
207 void OnWriteCompleted(IOResult io_result, | 243 void OnWriteCompleted(IOResult io_result, |
208 size_t platform_handles_written, | 244 size_t platform_handles_written, |
209 size_t bytes_written); | 245 size_t bytes_written); |
210 | 246 |
211 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } | 247 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } |
212 base::Lock& write_lock() { return write_lock_; } | 248 base::Lock& write_lock() { return write_lock_; } |
| 249 base::Lock& read_lock() { return read_lock_; } |
213 | 250 |
214 // Should only be called on the I/O thread. | 251 // Should only be called on the I/O thread. |
215 ReadBuffer* read_buffer() { return read_buffer_.get(); } | 252 ReadBuffer* read_buffer() { return read_buffer_.get(); } |
216 | 253 |
217 // Only called under |write_lock_|. | 254 // Only called under |write_lock_|. |
218 WriteBuffer* write_buffer_no_lock() { | 255 WriteBuffer* write_buffer_no_lock() { |
219 write_lock_.AssertAcquired(); | 256 write_lock_.AssertAcquired(); |
220 return write_buffer_.get(); | 257 return write_buffer_.get(); |
221 } | 258 } |
222 | 259 |
223 // Adds |message| to the write message queue. Implementation subclasses may | 260 // Adds |message| to the write message queue. Implementation subclasses may |
224 // override this to add any additional "control" messages needed. This is | 261 // override this to add any additional "control" messages needed. This is |
225 // called (on any thread) with |write_lock_| held. | 262 // called (on any thread) with |write_lock_| held. |
226 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message); | 263 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message); |
227 | 264 |
228 // Handles any control messages targeted to the |RawChannel| (or | 265 // Handles any control messages targeted to the |RawChannel| (or |
229 // implementation subclass). Implementation subclasses may override this to | 266 // implementation subclass). Implementation subclasses may override this to |
230 // handle any implementation-specific control messages, but should call | 267 // handle any implementation-specific control messages, but should call |
231 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages. | 268 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages. |
232 // Returns true on success and false on error (e.g., invalid control message). | 269 // Returns true on success and false on error (e.g., invalid control message). |
233 // This is only called on the I/O thread. | 270 // This is only called on the I/O thread. |
234 virtual bool OnReadMessageForRawChannel( | 271 virtual bool OnReadMessageForRawChannel( |
235 const MessageInTransit::View& message_view); | 272 const MessageInTransit::View& message_view); |
236 | 273 |
| 274 virtual PlatformHandle HandleForDebuggingNoLock() = 0; |
| 275 |
| 276 // Implementation must write any pending messages synchronously. |
| 277 // TODO(jam): change to return shared memory with pending serialized msgs. |
| 278 virtual ScopedPlatformHandle ReleaseHandleNoLock( |
| 279 std::vector<char>* read_buffer) = 0; |
| 280 |
237 // Reads into |read_buffer()|. | 281 // Reads into |read_buffer()|. |
238 // This class guarantees that: | 282 // This class guarantees that: |
239 // - the area indicated by |GetBuffer()| will stay valid until read completion | 283 // - the area indicated by |GetBuffer()| will stay valid until read completion |
240 // (but please also see the comments for |OnShutdownNoLock()|); | 284 // (but please also see the comments for |OnShutdownNoLock()|); |
241 // - a second read is not started if there is a pending read; | 285 // - a second read is not started if there is a pending read; |
242 // - the method is called on the I/O thread WITHOUT |write_lock_| held. | 286 // - the method is called on the I/O thread WITHOUT |write_lock_| held. |
243 // | 287 // |
244 // The implementing subclass must guarantee that: | 288 // The implementing subclass must guarantee that: |
245 // - |bytes_read| is untouched unless |Read()| returns |IO_SUCCEEDED|; | 289 // - |bytes_read| is untouched unless |Read()| returns |IO_SUCCEEDED|; |
246 // - if the method returns |IO_PENDING|, |OnReadCompleted()| will be called on | 290 // - if the method returns |IO_PENDING|, |OnReadCompleted()| will be called on |
247 // the I/O thread to report the result, unless |Shutdown()| is called. | 291 // the I/O thread to report the result, unless |Shutdown()| is called. |
248 virtual IOResult Read(size_t* bytes_read) = 0; | 292 virtual IOResult Read(size_t* bytes_read) = 0; |
249 // Similar to |Read()|, except that the implementing subclass must also | 293 // Similar to |Read()|, except that the implementing subclass must also |
250 // guarantee that the method doesn't succeed synchronously, i.e., it only | 294 // guarantee that the method doesn't succeed synchronously, i.e., it only |
251 // returns |IO_FAILED_...| or |IO_PENDING|. | 295 // returns |IO_FAILED_...| or |IO_PENDING|. |
252 virtual IOResult ScheduleRead() = 0; | 296 virtual IOResult ScheduleRead() = 0; |
253 | 297 |
254 // Called by |OnReadCompleted()| to get the platform handles associated with | 298 // Called by |OnReadCompleted()| to get the platform handles associated with |
255 // the given platform handle table (from a message). This should only be | 299 // the given platform handle table (from a message). This should only be |
256 // called when |num_platform_handles| is nonzero. Returns null if the | 300 // called when |num_platform_handles| is nonzero. Returns null if the |
257 // |num_platform_handles| handles are not available. Only called on the I/O | 301 // |num_platform_handles| handles are not available. Only called on the I/O |
258 // thread (without |write_lock_| held). | 302 // thread (without |write_lock_| held). |
259 virtual embedder::ScopedPlatformHandleVectorPtr GetReadPlatformHandles( | 303 virtual ScopedPlatformHandleVectorPtr GetReadPlatformHandles( |
260 size_t num_platform_handles, | 304 size_t num_platform_handles, |
261 const void* platform_handle_table) = 0; | 305 const void* platform_handle_table) = 0; |
262 | 306 |
263 // Writes contents in |write_buffer_no_lock()|. | 307 // Writes contents in |write_buffer_no_lock()|. |
264 // This class guarantees that: | 308 // This class guarantees that: |
265 // - the |PlatformHandle|s given by |GetPlatformHandlesToSend()| and the | 309 // - the |PlatformHandle|s given by |GetPlatformHandlesToSend()| and the |
266 // buffer(s) given by |GetBuffers()| will remain valid until write | 310 // buffer(s) given by |GetBuffers()| will remain valid until write |
267 // completion (see also the comments for |OnShutdownNoLock()|); | 311 // completion (see also the comments for |OnShutdownNoLock()|); |
268 // - a second write is not started if there is a pending write; | 312 // - a second write is not started if there is a pending write; |
269 // - the method is called under |write_lock_|. | 313 // - the method is called under |write_lock_|. |
(...skipping 12 matching lines...) Expand all Loading... |
282 | 326 |
283 // Must be called on the I/O thread WITHOUT |write_lock_| held. | 327 // Must be called on the I/O thread WITHOUT |write_lock_| held. |
284 virtual void OnInit() = 0; | 328 virtual void OnInit() = 0; |
285 // On shutdown, passes the ownership of the buffers to subclasses, which may | 329 // On shutdown, passes the ownership of the buffers to subclasses, which may |
286 // want to preserve them if there are pending read/writes. After this is | 330 // want to preserve them if there are pending read/writes. After this is |
287 // called, |OnReadCompleted()| must no longer be called. Must be called on the | 331 // called, |OnReadCompleted()| must no longer be called. Must be called on the |
288 // I/O thread under |write_lock_|. | 332 // I/O thread under |write_lock_|. |
289 virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer, | 333 virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer, |
290 scoped_ptr<WriteBuffer> write_buffer) = 0; | 334 scoped_ptr<WriteBuffer> write_buffer) = 0; |
291 | 335 |
| 336 bool SendQueuedMessagesNoLock(); |
| 337 |
292 private: | 338 private: |
| 339 friend class base::DeleteHelper<RawChannel>; |
| 340 |
293 // Converts an |IO_FAILED_...| for a read to a |Delegate::Error|. | 341 // Converts an |IO_FAILED_...| for a read to a |Delegate::Error|. |
294 static Delegate::Error ReadIOResultToError(IOResult io_result); | 342 static Delegate::Error ReadIOResultToError(IOResult io_result); |
295 | 343 |
296 // Calls |delegate_->OnError(error)|. Must be called on the I/O thread WITHOUT | 344 // Calls |delegate_->OnError(error)|. Must be called on the I/O thread WITHOUT |
297 // |write_lock_| held. This object may be destroyed by this call. | 345 // |write_lock_| held. This object may be destroyed by this call. |
298 void CallOnError(Delegate::Error error); | 346 void CallOnError(Delegate::Error error); |
299 | 347 |
| 348 void LockAndCallOnError(Delegate::Error error); |
| 349 |
300 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a | 350 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a |
301 // write operation to run later if there is more to write. If |io_result| is | 351 // write operation to run later if there is more to write. If |io_result| is |
302 // failure or any other error occurs, cancels pending writes and returns | 352 // failure or any other error occurs, cancels pending writes and returns |
303 // false. Must be called under |write_lock_| and only if |write_stopped_| is | 353 // false. Must be called under |write_lock_| and only if |write_stopped_| is |
304 // false. | 354 // false. |
305 bool OnWriteCompletedNoLock(IOResult io_result, | 355 bool OnWriteCompletedNoLock(IOResult io_result, |
306 size_t platform_handles_written, | 356 size_t platform_handles_written, |
307 size_t bytes_written); | 357 size_t bytes_written); |
308 | 358 |
| 359 // Helper method to dispatch messages from the read buffer. |
| 360 // |did_dispatch_message| is true iff it dispatched any messages. |
| 361 // |stop_dispatching| is set to true if the code calling this should stop |
| 362 // dispatching, either because we hit an erorr or the delegate shutdown the |
| 363 // channel. |
| 364 void DispatchMessages(bool* did_dispatch_message, bool* stop_dispatching); |
| 365 |
309 // Set in |Init()| and never changed (hence usable on any thread without | 366 // Set in |Init()| and never changed (hence usable on any thread without |
310 // locking): | 367 // locking): |
311 base::MessageLoopForIO* message_loop_for_io_; | 368 base::MessageLoopForIO* message_loop_for_io_; |
312 | 369 |
| 370 |
| 371 |
| 372 |
| 373 |
| 374 // TODO(jam): one lock only... but profile first to ensure it doesn't slow |
| 375 // things down compared to fine grained locks. |
| 376 |
| 377 |
| 378 |
| 379 |
313 // Only used on the I/O thread: | 380 // Only used on the I/O thread: |
| 381 |
| 382 base::Lock read_lock_; // Protects read_buffer_. |
| 383 // This is usually only accessed on IO thread, except when ReleaseHandle is |
| 384 // called. |
| 385 scoped_ptr<ReadBuffer> read_buffer_; |
| 386 // ditto: usually used on io thread except ReleaseHandle |
314 Delegate* delegate_; | 387 Delegate* delegate_; |
315 bool* set_on_shutdown_; | 388 |
316 scoped_ptr<ReadBuffer> read_buffer_; | 389 // If grabbing both locks, grab read first. |
317 | 390 |
318 base::Lock write_lock_; // Protects the following members. | 391 base::Lock write_lock_; // Protects the following members. |
| 392 bool write_ready_; |
319 bool write_stopped_; | 393 bool write_stopped_; |
320 scoped_ptr<WriteBuffer> write_buffer_; | 394 scoped_ptr<WriteBuffer> write_buffer_; |
321 | 395 |
| 396 bool error_occurred_; |
| 397 |
322 // This is used for posting tasks from write threads to the I/O thread. It | 398 // This is used for posting tasks from write threads to the I/O thread. It |
323 // must only be accessed under |write_lock_|. The weak pointers it produces | 399 // must only be accessed under |write_lock_|. The weak pointers it produces |
324 // are only used/invalidated on the I/O thread. | 400 // are only used/invalidated on the I/O thread. |
325 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | 401 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; |
326 | 402 |
327 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); | 403 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); |
328 }; | 404 }; |
329 | 405 |
330 } // namespace system | 406 } // namespace edk |
331 } // namespace mojo | 407 } // namespace mojo |
332 | 408 |
333 #endif // THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 409 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
OLD | NEW |