| 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 <deque> | 8 #include <deque> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 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). |
| 44 virtual ~RawChannel(); | 46 virtual ~RawChannel(); |
| 45 | 47 |
| 46 // The |Delegate| is only accessed on the same thread as the message loop | 48 // The |Delegate| is only accessed on the same thread as the message loop |
| 47 // (passed in on creation). | 49 // (passed in on creation). |
| 48 class MOJO_SYSTEM_IMPL_EXPORT Delegate { | 50 class MOJO_SYSTEM_IMPL_EXPORT Delegate { |
| 49 public: | 51 public: |
| 50 enum Error { | 52 enum Error { |
| 51 // Failed read due to raw channel shutdown (e.g., on the other side). | 53 // Failed read due to raw channel shutdown (e.g., on the other side). |
| 52 ERROR_READ_SHUTDOWN, | 54 ERROR_READ_SHUTDOWN, |
| 53 // Failed read due to raw channel being broken (e.g., if the other side | 55 // Failed read due to raw channel being broken (e.g., if the other side |
| 54 // died without shutting down). | 56 // died without shutting down). |
| 55 ERROR_READ_BROKEN, | 57 ERROR_READ_BROKEN, |
| 56 // Received a bad message. | 58 // Received a bad message. |
| 57 ERROR_READ_BAD_MESSAGE, | 59 ERROR_READ_BAD_MESSAGE, |
| 58 // Unknown read error. | 60 // Unknown read error. |
| 59 ERROR_READ_UNKNOWN, | 61 ERROR_READ_UNKNOWN, |
| 60 // Generic write error. | 62 // Generic write error. |
| 61 ERROR_WRITE | 63 ERROR_WRITE |
| 62 }; | 64 }; |
| 63 | 65 |
| 64 // Called when a message is read. This may call |Shutdown()| (on the | 66 // Called when a message is read. This may call the |RawChannel|'s |
| 65 // |RawChannel|), but must not destroy it. | 67 // |Shutdown()| and then (if desired) destroy it. |
| 66 virtual void OnReadMessage( | 68 virtual void OnReadMessage( |
| 67 const MessageInTransit::View& message_view, | 69 const MessageInTransit::View& message_view, |
| 68 embedder::ScopedPlatformHandleVectorPtr platform_handles) = 0; | 70 embedder::ScopedPlatformHandleVectorPtr platform_handles) = 0; |
| 69 | 71 |
| 70 // Called when there's a (fatal) error. This may call the raw channel's | 72 // Called when there's a (fatal) error. This may call the |RawChannel|'s |
| 71 // |Shutdown()|, but must not destroy it. | 73 // |Shutdown()| and then (if desired) destroy it. |
| 72 // | 74 // |
| 73 // For each raw channel, there'll be at most one |ERROR_READ_...| and at | 75 // For each raw channel, there'll be at most one |ERROR_READ_...| and at |
| 74 // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|, | 76 // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|, |
| 75 // |OnReadMessage()| won't be called again. | 77 // |OnReadMessage()| won't be called again. |
| 76 virtual void OnError(Error error) = 0; | 78 virtual void OnError(Error error) = 0; |
| 77 | 79 |
| 78 protected: | 80 protected: |
| 79 virtual ~Delegate() {} | 81 virtual ~Delegate() {} |
| 80 }; | 82 }; |
| 81 | 83 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 // indicates the position in the first message's data to start the next | 192 // indicates the position in the first message's data to start the next |
| 191 // write. | 193 // write. |
| 192 size_t data_offset_; | 194 size_t data_offset_; |
| 193 | 195 |
| 194 DISALLOW_COPY_AND_ASSIGN(WriteBuffer); | 196 DISALLOW_COPY_AND_ASSIGN(WriteBuffer); |
| 195 }; | 197 }; |
| 196 | 198 |
| 197 RawChannel(); | 199 RawChannel(); |
| 198 | 200 |
| 199 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 201 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
| 200 // |write_lock_| held. | 202 // |write_lock_| held. This object may be destroyed by this call. |
| 201 void OnReadCompleted(IOResult io_result, size_t bytes_read); | 203 void OnReadCompleted(IOResult io_result, size_t bytes_read); |
| 202 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 204 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
| 203 // |write_lock_| held. | 205 // |write_lock_| held. This object may be destroyed by this call. |
| 204 void OnWriteCompleted(IOResult io_result, | 206 void OnWriteCompleted(IOResult io_result, |
| 205 size_t platform_handles_written, | 207 size_t platform_handles_written, |
| 206 size_t bytes_written); | 208 size_t bytes_written); |
| 207 | 209 |
| 208 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } | 210 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } |
| 209 base::Lock& write_lock() { return write_lock_; } | 211 base::Lock& write_lock() { return write_lock_; } |
| 210 | 212 |
| 211 // Should only be called on the I/O thread. | 213 // Should only be called on the I/O thread. |
| 212 ReadBuffer* read_buffer() { return read_buffer_.get(); } | 214 ReadBuffer* read_buffer() { return read_buffer_.get(); } |
| 213 | 215 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 virtual IOResult WriteNoLock(size_t* platform_handles_written, | 275 virtual IOResult WriteNoLock(size_t* platform_handles_written, |
| 274 size_t* bytes_written) = 0; | 276 size_t* bytes_written) = 0; |
| 275 // Similar to |WriteNoLock()|, except that the implementing subclass must also | 277 // Similar to |WriteNoLock()|, except that the implementing subclass must also |
| 276 // guarantee that the method doesn't succeed synchronously, i.e., it only | 278 // guarantee that the method doesn't succeed synchronously, i.e., it only |
| 277 // returns |IO_FAILED_...| or |IO_PENDING|. | 279 // returns |IO_FAILED_...| or |IO_PENDING|. |
| 278 virtual IOResult ScheduleWriteNoLock() = 0; | 280 virtual IOResult ScheduleWriteNoLock() = 0; |
| 279 | 281 |
| 280 // Must be called on the I/O thread WITHOUT |write_lock_| held. | 282 // Must be called on the I/O thread WITHOUT |write_lock_| held. |
| 281 virtual void OnInit() = 0; | 283 virtual void OnInit() = 0; |
| 282 // On shutdown, passes the ownership of the buffers to subclasses, which may | 284 // On shutdown, passes the ownership of the buffers to subclasses, which may |
| 283 // want to preserve them if there are pending read/write. Must be called on | 285 // want to preserve them if there are pending read/writes. After this is |
| 284 // the I/O thread under |write_lock_|. | 286 // called, |OnReadCompleted()| must no longer be called. Must be called on the |
| 287 // I/O thread under |write_lock_|. |
| 285 virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer, | 288 virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer, |
| 286 scoped_ptr<WriteBuffer> write_buffer) = 0; | 289 scoped_ptr<WriteBuffer> write_buffer) = 0; |
| 287 | 290 |
| 288 private: | 291 private: |
| 289 // Converts an |IO_FAILED_...| for a read to a |Delegate::Error|. | 292 // Converts an |IO_FAILED_...| for a read to a |Delegate::Error|. |
| 290 static Delegate::Error ReadIOResultToError(IOResult io_result); | 293 static Delegate::Error ReadIOResultToError(IOResult io_result); |
| 291 | 294 |
| 292 // Calls |delegate_->OnError(error)|. Must be called on the I/O thread WITHOUT | 295 // Calls |delegate_->OnError(error)|. Must be called on the I/O thread WITHOUT |
| 293 // |write_lock_| held. | 296 // |write_lock_| held. This object may be destroyed by this call. |
| 294 void CallOnError(Delegate::Error error); | 297 void CallOnError(Delegate::Error error); |
| 295 | 298 |
| 296 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a | 299 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a |
| 297 // write operation to run later if there is more to write. If |io_result| is | 300 // write operation to run later if there is more to write. If |io_result| is |
| 298 // failure or any other error occurs, cancels pending writes and returns | 301 // failure or any other error occurs, cancels pending writes and returns |
| 299 // false. Must be called under |write_lock_| and only if |write_stopped_| is | 302 // false. Must be called under |write_lock_| and only if |write_stopped_| is |
| 300 // false. | 303 // false. |
| 301 bool OnWriteCompletedNoLock(IOResult io_result, | 304 bool OnWriteCompletedNoLock(IOResult io_result, |
| 302 size_t platform_handles_written, | 305 size_t platform_handles_written, |
| 303 size_t bytes_written); | 306 size_t bytes_written); |
| 304 | 307 |
| 305 // Set in |Init()| and never changed (hence usable on any thread without | 308 // Set in |Init()| and never changed (hence usable on any thread without |
| 306 // locking): | 309 // locking): |
| 307 base::MessageLoopForIO* message_loop_for_io_; | 310 base::MessageLoopForIO* message_loop_for_io_; |
| 308 | 311 |
| 309 // Only used on the I/O thread: | 312 // Only used on the I/O thread: |
| 310 Delegate* delegate_; | 313 Delegate* delegate_; |
| 311 bool read_stopped_; | 314 bool* set_on_shutdown_; |
| 312 scoped_ptr<ReadBuffer> read_buffer_; | 315 scoped_ptr<ReadBuffer> read_buffer_; |
| 313 | 316 |
| 314 base::Lock write_lock_; // Protects the following members. | 317 base::Lock write_lock_; // Protects the following members. |
| 315 bool write_stopped_; | 318 bool write_stopped_; |
| 316 scoped_ptr<WriteBuffer> write_buffer_; | 319 scoped_ptr<WriteBuffer> write_buffer_; |
| 317 | 320 |
| 318 // This is used for posting tasks from write threads to the I/O thread. It | 321 // This is used for posting tasks from write threads to the I/O thread. It |
| 319 // must only be accessed under |write_lock_|. The weak pointers it produces | 322 // must only be accessed under |write_lock_|. The weak pointers it produces |
| 320 // are only used/invalidated on the I/O thread. | 323 // are only used/invalidated on the I/O thread. |
| 321 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | 324 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; |
| 322 | 325 |
| 323 DISALLOW_COPY_AND_ASSIGN(RawChannel); | 326 DISALLOW_COPY_AND_ASSIGN(RawChannel); |
| 324 }; | 327 }; |
| 325 | 328 |
| 326 } // namespace system | 329 } // namespace system |
| 327 } // namespace mojo | 330 } // namespace mojo |
| 328 | 331 |
| 329 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 332 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
| OLD | NEW |