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 MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 5 #ifndef MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
6 #define 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" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 | 62 |
63 // Called when a message is read. The delegate may not call back into this | 63 // Called when a message is read. The delegate may not call back into this |
64 // object synchronously. | 64 // object synchronously. |
65 virtual void OnReadMessage( | 65 virtual void OnReadMessage( |
66 const MessageInTransit::View& message_view, | 66 const MessageInTransit::View& message_view, |
67 ScopedPlatformHandleVectorPtr platform_handles) = 0; | 67 ScopedPlatformHandleVectorPtr platform_handles) = 0; |
68 | 68 |
69 // Called when there's a (fatal) error. The delegate may not call back into | 69 // Called when there's a (fatal) error. The delegate may not call back into |
70 // this object synchronously. | 70 // this object synchronously. |
71 // | 71 // |
72 // 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 OnError notification. |
73 // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|, | |
74 // |OnReadMessage()| won't be called again. | |
75 virtual void OnError(Error error) = 0; | 73 virtual void OnError(Error error) = 0; |
76 | 74 |
77 protected: | 75 protected: |
78 virtual ~Delegate() {} | 76 virtual ~Delegate() {} |
79 }; | 77 }; |
80 | 78 |
81 // Static factory method. |handle| should be a handle to a | 79 // Static factory method. |handle| should be a handle to a |
82 // (platform-appropriate) bidirectional communication channel (e.g., a socket | 80 // (platform-appropriate) bidirectional communication channel (e.g., a socket |
83 // on POSIX, a named pipe on Windows). | 81 // on POSIX, a named pipe on Windows). |
84 static RawChannel* Create(ScopedPlatformHandle handle); | 82 static RawChannel* Create(ScopedPlatformHandle handle); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 // TODO(vtl): Maybe this method should be const, but | 183 // TODO(vtl): Maybe this method should be const, but |
186 // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies | 184 // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies |
187 // state). | 185 // state). |
188 void GetPlatformHandlesToSend(size_t* num_platform_handles, | 186 void GetPlatformHandlesToSend(size_t* num_platform_handles, |
189 PlatformHandle** platform_handles, | 187 PlatformHandle** platform_handles, |
190 void** serialization_data); | 188 void** serialization_data); |
191 | 189 |
192 // Gets buffers to be written. These buffers will always come from the front | 190 // Gets buffers to be written. These buffers will always come from the front |
193 // of |message_queue_|. Once they are completely written, the front | 191 // of |message_queue_|. Once they are completely written, the front |
194 // |MessageInTransit| should be popped (and destroyed); this is done in | 192 // |MessageInTransit| should be popped (and destroyed); this is done in |
195 // |OnWriteCompletedNoLock()|. | 193 // |OnWriteCompletedInternalNoLock()|. |
196 void GetBuffers(std::vector<Buffer>* buffers); | 194 void GetBuffers(std::vector<Buffer>* buffers); |
197 | 195 |
198 bool IsEmpty() const { return message_queue_.IsEmpty(); } | 196 bool IsEmpty() const { return message_queue_.IsEmpty(); } |
199 | 197 |
200 private: | 198 private: |
201 friend class RawChannel; | 199 friend class RawChannel; |
202 | 200 |
203 size_t serialized_platform_handle_size_; | 201 size_t serialized_platform_handle_size_; |
204 | 202 |
205 MessageInTransitQueue message_queue_; | 203 MessageInTransitQueue message_queue_; |
(...skipping 10 matching lines...) Expand all Loading... | |
216 }; | 214 }; |
217 | 215 |
218 RawChannel(); | 216 RawChannel(); |
219 | 217 |
220 // Shutdown must be called on the IO thread. This object deletes itself once | 218 // Shutdown must be called on the IO thread. This object deletes itself once |
221 // it's flushed all pending writes and insured that the other side of the pipe | 219 // it's flushed all pending writes and insured that the other side of the pipe |
222 // read them. | 220 // read them. |
223 virtual ~RawChannel(); | 221 virtual ~RawChannel(); |
224 | 222 |
225 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 223 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
226 // |write_lock_| held. This object may be destroyed by this call. | 224 // |write_lock_| held. This object may be destroyed by this call. This |
225 // acquires |read_lock_| inside of it. | |
227 void OnReadCompleted(IOResult io_result, size_t bytes_read); | 226 void OnReadCompleted(IOResult io_result, size_t bytes_read); |
yzshen1
2015/10/14 18:23:46
Does it make sense tor remove On(Read|Write)Comple
jam
2015/10/14 20:01:44
good point. done.
| |
227 // Like above, except that the caller needs to acquire read_lock_ first. | |
yzshen1
2015/10/14 18:23:46
style nit: please use |some_variable_name_| (and l
jam
2015/10/14 20:01:44
I stopped this since it's not in the style guide;
yzshen1
2015/10/14 21:37:00
I see, although I think consistency within the fil
| |
228 void OnReadCompletedNoLock(IOResult io_result, size_t bytes_read); | |
228 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 229 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
229 // |write_lock_| held. This object may be destroyed by this call. | 230 // |write_lock_| held. This object may be destroyed by this call. |
230 void OnWriteCompleted(IOResult io_result, | 231 void OnWriteCompleted(IOResult io_result, |
231 size_t platform_handles_written, | 232 size_t platform_handles_written, |
232 size_t bytes_written); | 233 size_t bytes_written); |
234 // Like above, except that the caller needs to acquire write_lock_ first. | |
235 void OnWriteCompletedNoLock(IOResult io_result, | |
236 size_t platform_handles_written, | |
237 size_t bytes_written); | |
233 | 238 |
234 // Serialize the read buffer into the given array so that it can be sent to | 239 // Serialize the read buffer into the given array so that it can be sent to |
235 // another process. Increments |num_valid_bytes_| by |additional_bytes_read| | 240 // another process. Increments |num_valid_bytes_| by |additional_bytes_read| |
236 // before serialization.. | 241 // before serialization.. |
237 void SerializeReadBuffer(size_t additional_bytes_read, | 242 void SerializeReadBuffer(size_t additional_bytes_read, |
238 std::vector<char>* buffer); | 243 std::vector<char>* buffer); |
239 | 244 |
240 // Serialize the pending messages to be written to the OS pipe to the given | 245 // Serialize the pending messages to be written to the OS pipe to the given |
241 // buffer so that it can be sent to another process. | 246 // buffer so that it can be sent to another process. |
242 void SerializeWriteBuffer(std::vector<char>* buffer, | 247 void SerializeWriteBuffer(size_t additional_bytes_written, |
243 size_t additional_bytes_written, | 248 size_t additional_platform_handles_written, |
244 size_t additional_platform_handles_written); | 249 std::vector<char>* buffer); |
245 | 250 |
246 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } | 251 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } |
247 base::Lock& write_lock() { return write_lock_; } | 252 base::Lock& write_lock() { return write_lock_; } |
248 base::Lock& read_lock() { return read_lock_; } | 253 base::Lock& read_lock() { return read_lock_; } |
249 | 254 |
250 // Should only be called on the I/O thread. | 255 // Should only be called on the I/O thread. |
251 ReadBuffer* read_buffer() { return read_buffer_.get(); } | 256 ReadBuffer* read_buffer() { return read_buffer_.get(); } |
252 | 257 |
253 // Only called under |write_lock_|. | 258 // Only called under |write_lock_|. |
254 WriteBuffer* write_buffer_no_lock() { | 259 WriteBuffer* write_buffer_no_lock() { |
255 write_lock_.AssertAcquired(); | 260 write_lock_.AssertAcquired(); |
256 return write_buffer_.get(); | 261 return write_buffer_.get(); |
257 } | 262 } |
258 | 263 |
264 bool pending_error() { return pending_error_; } | |
265 | |
259 // Adds |message| to the write message queue. Implementation subclasses may | 266 // Adds |message| to the write message queue. Implementation subclasses may |
260 // override this to add any additional "control" messages needed. This is | 267 // override this to add any additional "control" messages needed. This is |
261 // called (on any thread) with |write_lock_| held. | 268 // called (on any thread) with |write_lock_| held. |
262 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message); | 269 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message); |
263 | 270 |
264 // Handles any control messages targeted to the |RawChannel| (or | 271 // Handles any control messages targeted to the |RawChannel| (or |
265 // implementation subclass). Implementation subclasses may override this to | 272 // implementation subclass). Implementation subclasses may override this to |
266 // handle any implementation-specific control messages, but should call | 273 // handle any implementation-specific control messages, but should call |
267 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages. | 274 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages. |
268 // Returns true on success and false on error (e.g., invalid control message). | 275 // Returns true on success and false on error (e.g., invalid control message). |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 // |write_lock_| held. This object may be destroyed by this call. | 361 // |write_lock_| held. This object may be destroyed by this call. |
355 void CallOnError(Delegate::Error error); | 362 void CallOnError(Delegate::Error error); |
356 | 363 |
357 void LockAndCallOnError(Delegate::Error error); | 364 void LockAndCallOnError(Delegate::Error error); |
358 | 365 |
359 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a | 366 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a |
360 // write operation to run later if there is more to write. If |io_result| is | 367 // write operation to run later if there is more to write. If |io_result| is |
361 // failure or any other error occurs, cancels pending writes and returns | 368 // failure or any other error occurs, cancels pending writes and returns |
362 // false. Must be called under |write_lock_| and only if |write_stopped_| is | 369 // false. Must be called under |write_lock_| and only if |write_stopped_| is |
363 // false. | 370 // false. |
364 bool OnWriteCompletedNoLock(IOResult io_result, | 371 bool OnWriteCompletedInternalNoLock(IOResult io_result, |
365 size_t platform_handles_written, | 372 size_t platform_handles_written, |
366 size_t bytes_written); | 373 size_t bytes_written); |
367 | 374 |
368 // Helper method to dispatch messages from the read buffer. | 375 // Helper method to dispatch messages from the read buffer. |
369 // |did_dispatch_message| is true iff it dispatched any messages. | 376 // |did_dispatch_message| is true iff it dispatched any messages. |
370 // |stop_dispatching| is set to true if the code calling this should stop | 377 // |stop_dispatching| is set to true if the code calling this should stop |
371 // dispatching, either because we hit an erorr or the delegate shutdown the | 378 // dispatching, either because we hit an erorr or the delegate shutdown the |
372 // channel. | 379 // channel. |
373 void DispatchMessages(bool* did_dispatch_message, bool* stop_dispatching); | 380 void DispatchMessages(bool* did_dispatch_message, bool* stop_dispatching); |
374 | 381 |
375 void UpdateWriteBuffer(size_t platform_handles_written, size_t bytes_written); | 382 void UpdateWriteBuffer(size_t platform_handles_written, size_t bytes_written); |
376 | 383 |
(...skipping 22 matching lines...) Expand all Loading... | |
399 | 406 |
400 // If grabbing both locks, grab read first. | 407 // If grabbing both locks, grab read first. |
401 | 408 |
402 base::Lock write_lock_; // Protects the following members. | 409 base::Lock write_lock_; // Protects the following members. |
403 bool write_ready_; | 410 bool write_ready_; |
404 bool write_stopped_; | 411 bool write_stopped_; |
405 scoped_ptr<WriteBuffer> write_buffer_; | 412 scoped_ptr<WriteBuffer> write_buffer_; |
406 | 413 |
407 bool error_occurred_; | 414 bool error_occurred_; |
408 | 415 |
416 // True iff a PostTask has been called to set an error. Can be written under | |
417 // either read or write lock. It's read with both acquired. | |
418 bool pending_error_; | |
419 | |
409 // This is used for posting tasks from write threads to the I/O thread. It | 420 // This is used for posting tasks from write threads to the I/O thread. It |
410 // must only be accessed under |write_lock_|. The weak pointers it produces | 421 // must only be accessed under |write_lock_|. The weak pointers it produces |
411 // are only used/invalidated on the I/O thread. | 422 // are only used/invalidated on the I/O thread. |
412 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | 423 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; |
413 | 424 |
414 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); | 425 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); |
415 }; | 426 }; |
416 | 427 |
417 } // namespace edk | 428 } // namespace edk |
418 } // namespace mojo | 429 } // namespace mojo |
419 | 430 |
420 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 431 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
OLD | NEW |