| 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" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/synchronization/lock.h" | |
| 13 #include "mojo/edk/embedder/platform_handle_vector.h" | 12 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 14 #include "mojo/edk/embedder/scoped_platform_handle.h" | 13 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 15 #include "mojo/edk/system/message_in_transit.h" | 14 #include "mojo/edk/system/message_in_transit.h" |
| 16 #include "mojo/edk/system/message_in_transit_queue.h" | 15 #include "mojo/edk/system/message_in_transit_queue.h" |
| 16 #include "mojo/edk/system/mutex.h" |
| 17 #include "mojo/edk/system/system_impl_export.h" | 17 #include "mojo/edk/system/system_impl_export.h" |
| 18 #include "mojo/edk/system/thread_annotations.h" |
| 18 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 class MessageLoopForIO; | 22 class MessageLoopForIO; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace mojo { | 25 namespace mojo { |
| 25 namespace system { | 26 namespace system { |
| 26 | 27 |
| 27 // |RawChannel| is an interface and base class for objects that wrap an OS | 28 // |RawChannel| is an interface and base class for objects that wrap an OS |
| 28 // "pipe". It presents the following interface to users: | 29 // "pipe". It presents the following interface to users: |
| 29 // - Receives and dispatches messages on an I/O thread (running a | 30 // - Receives and dispatches messages on an I/O thread (running a |
| 30 // |MessageLoopForIO|. | 31 // |MessageLoopForIO|. |
| 31 // - Provides a thread-safe way of writing messages (|WriteMessage()|); | 32 // - Provides a thread-safe way of writing messages (|WriteMessage()|); |
| 32 // writing/queueing messages will not block and is atomic from the point of | 33 // 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 | 34 // view of the caller. If necessary, messages are queued (to be written on |
| 34 // the aforementioned thread). | 35 // the aforementioned thread). |
| 35 // | 36 // |
| 36 // OS-specific implementation subclasses are to be instantiated using the | 37 // OS-specific implementation subclasses are to be instantiated using the |
| 37 // |Create()| static factory method. | 38 // |Create()| static factory method. |
| 38 // | 39 // |
| 39 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in | 40 // With the exception of |WriteMessage()| and |IsWriteBufferEmpty()|, this class |
| 40 // general its methods should only be used on the I/O thread, i.e., the thread | 41 // is thread-unsafe (and in general its methods should only be used on the I/O |
| 41 // on which |Init()| is called). | 42 // thread, i.e., the thread on which |Init()| is called). |
| 42 class MOJO_SYSTEM_IMPL_EXPORT RawChannel { | 43 class MOJO_SYSTEM_IMPL_EXPORT RawChannel { |
| 43 public: | 44 public: |
| 44 // This object may be destroyed on any thread (if |Init()| was called, after | 45 // This object may be destroyed on any thread (if |Init()| was called, after |
| 45 // |Shutdown()| was called). | 46 // |Shutdown()| was called). |
| 46 virtual ~RawChannel(); | 47 virtual ~RawChannel(); |
| 47 | 48 |
| 48 // The |Delegate| is only accessed on the same thread as the message loop | 49 // The |Delegate| is only accessed on the same thread as the message loop |
| 49 // (passed in on creation). | 50 // (passed in on creation). |
| 50 class MOJO_SYSTEM_IMPL_EXPORT Delegate { | 51 class MOJO_SYSTEM_IMPL_EXPORT Delegate { |
| 51 public: | 52 public: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 84 |
| 84 // Static factory method. |handle| should be a handle to a | 85 // Static factory method. |handle| should be a handle to a |
| 85 // (platform-appropriate) bidirectional communication channel (e.g., a socket | 86 // (platform-appropriate) bidirectional communication channel (e.g., a socket |
| 86 // on POSIX, a named pipe on Windows). | 87 // on POSIX, a named pipe on Windows). |
| 87 static scoped_ptr<RawChannel> Create(embedder::ScopedPlatformHandle handle); | 88 static scoped_ptr<RawChannel> Create(embedder::ScopedPlatformHandle handle); |
| 88 | 89 |
| 89 // This must be called (on an I/O thread) before this object is used. Does | 90 // 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 | 91 // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must |
| 91 // remain alive until |Shutdown()| is called (unless this fails); |delegate| | 92 // remain alive until |Shutdown()| is called (unless this fails); |delegate| |
| 92 // will no longer be used after |Shutdown()|. | 93 // will no longer be used after |Shutdown()|. |
| 93 void Init(Delegate* delegate); | 94 void Init(Delegate* delegate) MOJO_NOT_THREAD_SAFE; |
| 94 | 95 |
| 95 // This must be called (on the I/O thread) before this object is destroyed. | 96 // This must be called (on the I/O thread) before this object is destroyed. |
| 96 void Shutdown(); | 97 void Shutdown() MOJO_NOT_THREAD_SAFE; |
| 97 | 98 |
| 98 // Writes the given message (or schedules it to be written). |message| must | 99 // Writes the given message (or schedules it to be written). |message| must |
| 99 // have no |Dispatcher|s still attached (i.e., | 100 // have no |Dispatcher|s still attached (i.e., |
| 100 // |SerializeAndCloseDispatchers()| should have been called). This method is | 101 // |SerializeAndCloseDispatchers()| should have been called). This method is |
| 101 // thread-safe and may be called from any thread. Returns true on success. | 102 // thread-safe and may be called from any thread. Returns true on success. |
| 102 bool WriteMessage(scoped_ptr<MessageInTransit> message); | 103 bool WriteMessage(scoped_ptr<MessageInTransit> message); |
| 103 | 104 |
| 104 // Returns true if the write buffer is empty (i.e., all messages written using | 105 // Returns true if the write buffer is empty (i.e., all messages written using |
| 105 // |WriteMessage()| have actually been sent. | 106 // |WriteMessage()| have actually been sent. |
| 106 // TODO(vtl): We should really also notify our delegate when the write buffer | 107 // TODO(vtl): We should really also notify our delegate when the write buffer |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 // The first message's data may have been partially sent. |data_offset_| | 193 // 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 | 194 // indicates the position in the first message's data to start the next |
| 194 // write. | 195 // write. |
| 195 size_t data_offset_; | 196 size_t data_offset_; |
| 196 | 197 |
| 197 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteBuffer); | 198 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteBuffer); |
| 198 }; | 199 }; |
| 199 | 200 |
| 200 RawChannel(); | 201 RawChannel(); |
| 201 | 202 |
| 202 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 203 // |result| must not be |IO_PENDING|. Must be called on the I/O thread. This |
| 203 // |write_lock_| held. This object may be destroyed by this call. | 204 // object may be destroyed by this call. |
| 204 void OnReadCompleted(IOResult io_result, size_t bytes_read); | 205 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 | 206 MOJO_LOCKS_EXCLUDED(write_mutex_); |
| 206 // |write_lock_| held. This object may be destroyed by this call. | 207 // |result| must not be |IO_PENDING|. Must be called on the I/O thread. This |
| 208 // object may be destroyed by this call. |
| 207 void OnWriteCompleted(IOResult io_result, | 209 void OnWriteCompleted(IOResult io_result, |
| 208 size_t platform_handles_written, | 210 size_t platform_handles_written, |
| 209 size_t bytes_written); | 211 size_t bytes_written) MOJO_LOCKS_EXCLUDED(write_mutex_); |
| 210 | 212 |
| 211 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } | 213 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } |
| 212 base::Lock& write_lock() { return write_lock_; } | 214 Mutex& write_mutex() MOJO_LOCK_RETURNED(write_mutex_) { return write_mutex_; } |
| 213 | 215 |
| 214 // Should only be called on the I/O thread. | 216 // Should only be called on the I/O thread. |
| 215 ReadBuffer* read_buffer() { return read_buffer_.get(); } | 217 ReadBuffer* read_buffer() { return read_buffer_.get(); } |
| 216 | 218 |
| 217 // Only called under |write_lock_|. | 219 WriteBuffer* write_buffer_no_lock() |
| 218 WriteBuffer* write_buffer_no_lock() { | 220 MOJO_EXCLUSIVE_LOCKS_REQUIRED(write_mutex_) { |
| 219 write_lock_.AssertAcquired(); | |
| 220 return write_buffer_.get(); | 221 return write_buffer_.get(); |
| 221 } | 222 } |
| 222 | 223 |
| 223 // Adds |message| to the write message queue. Implementation subclasses may | 224 // Adds |message| to the write message queue. Implementation subclasses may |
| 224 // override this to add any additional "control" messages needed. This is | 225 // override this to add any additional "control" messages needed. This is |
| 225 // called (on any thread) with |write_lock_| held. | 226 // called (on any thread). |
| 226 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message); | 227 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message) |
| 228 MOJO_EXCLUSIVE_LOCKS_REQUIRED(write_mutex_); |
| 227 | 229 |
| 228 // Handles any control messages targeted to the |RawChannel| (or | 230 // Handles any control messages targeted to the |RawChannel| (or |
| 229 // implementation subclass). Implementation subclasses may override this to | 231 // implementation subclass). Implementation subclasses may override this to |
| 230 // handle any implementation-specific control messages, but should call | 232 // handle any implementation-specific control messages, but should call |
| 231 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages. | 233 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages. |
| 232 // Returns true on success and false on error (e.g., invalid control message). | 234 // Returns true on success and false on error (e.g., invalid control message). |
| 233 // This is only called on the I/O thread. | 235 // This is only called on the I/O thread. |
| 234 virtual bool OnReadMessageForRawChannel( | 236 virtual bool OnReadMessageForRawChannel( |
| 235 const MessageInTransit::View& message_view); | 237 const MessageInTransit::View& message_view); |
| 236 | 238 |
| 237 // Reads into |read_buffer()|. | 239 // Reads into |read_buffer()|. |
| 238 // This class guarantees that: | 240 // This class guarantees that: |
| 239 // - the area indicated by |GetBuffer()| will stay valid until read completion | 241 // - the area indicated by |GetBuffer()| will stay valid until read completion |
| 240 // (but please also see the comments for |OnShutdownNoLock()|); | 242 // (but please also see the comments for |OnShutdownNoLock()|); |
| 241 // - a second read is not started if there is a pending read; | 243 // - 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. | 244 // - the method is called on the I/O thread. |
| 243 // | 245 // |
| 244 // The implementing subclass must guarantee that: | 246 // The implementing subclass must guarantee that: |
| 245 // - |bytes_read| is untouched unless |Read()| returns |IO_SUCCEEDED|; | 247 // - |bytes_read| is untouched unless |Read()| returns |IO_SUCCEEDED|; |
| 246 // - if the method returns |IO_PENDING|, |OnReadCompleted()| will be called on | 248 // - if the method returns |IO_PENDING|, |OnReadCompleted()| will be called on |
| 247 // the I/O thread to report the result, unless |Shutdown()| is called. | 249 // the I/O thread to report the result, unless |Shutdown()| is called. |
| 248 virtual IOResult Read(size_t* bytes_read) = 0; | 250 virtual IOResult Read(size_t* bytes_read) |
| 251 MOJO_LOCKS_EXCLUDED(write_mutex_) = 0; |
| 249 // Similar to |Read()|, except that the implementing subclass must also | 252 // Similar to |Read()|, except that the implementing subclass must also |
| 250 // guarantee that the method doesn't succeed synchronously, i.e., it only | 253 // guarantee that the method doesn't succeed synchronously, i.e., it only |
| 251 // returns |IO_FAILED_...| or |IO_PENDING|. | 254 // returns |IO_FAILED_...| or |IO_PENDING|. |
| 252 virtual IOResult ScheduleRead() = 0; | 255 virtual IOResult ScheduleRead() MOJO_LOCKS_EXCLUDED(write_mutex_) = 0; |
| 253 | 256 |
| 254 // Called by |OnReadCompleted()| to get the platform handles associated with | 257 // Called by |OnReadCompleted()| to get the platform handles associated with |
| 255 // the given platform handle table (from a message). This should only be | 258 // the given platform handle table (from a message). This should only be |
| 256 // called when |num_platform_handles| is nonzero. Returns null if the | 259 // 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 | 260 // |num_platform_handles| handles are not available. Only called on the I/O |
| 258 // thread (without |write_lock_| held). | 261 // thread. |
| 259 virtual embedder::ScopedPlatformHandleVectorPtr GetReadPlatformHandles( | 262 virtual embedder::ScopedPlatformHandleVectorPtr GetReadPlatformHandles( |
| 260 size_t num_platform_handles, | 263 size_t num_platform_handles, |
| 261 const void* platform_handle_table) = 0; | 264 const void* platform_handle_table) MOJO_LOCKS_EXCLUDED(write_mutex_) = 0; |
| 262 | 265 |
| 263 // Writes contents in |write_buffer_no_lock()|. | 266 // Writes contents in |write_buffer_no_lock()|. |
| 264 // This class guarantees that: | 267 // This class guarantees that: |
| 265 // - the |PlatformHandle|s given by |GetPlatformHandlesToSend()| and the | 268 // - the |PlatformHandle|s given by |GetPlatformHandlesToSend()| and the |
| 266 // buffer(s) given by |GetBuffers()| will remain valid until write | 269 // buffer(s) given by |GetBuffers()| will remain valid until write |
| 267 // completion (see also the comments for |OnShutdownNoLock()|); | 270 // completion (see also the comments for |OnShutdownNoLock()|); |
| 268 // - a second write is not started if there is a pending write; | 271 // - a second write is not started if there is a pending write. |
| 269 // - the method is called under |write_lock_|. | |
| 270 // | 272 // |
| 271 // The implementing subclass must guarantee that: | 273 // The implementing subclass must guarantee that: |
| 272 // - |platform_handles_written| and |bytes_written| are untouched unless | 274 // - |platform_handles_written| and |bytes_written| are untouched unless |
| 273 // |WriteNoLock()| returns |IO_SUCCEEDED|; | 275 // |WriteNoLock()| returns |IO_SUCCEEDED|; |
| 274 // - if the method returns |IO_PENDING|, |OnWriteCompleted()| will be called | 276 // - if the method returns |IO_PENDING|, |OnWriteCompleted()| will be called |
| 275 // on the I/O thread to report the result, unless |Shutdown()| is called. | 277 // on the I/O thread to report the result, unless |Shutdown()| is called. |
| 276 virtual IOResult WriteNoLock(size_t* platform_handles_written, | 278 virtual IOResult WriteNoLock(size_t* platform_handles_written, |
| 277 size_t* bytes_written) = 0; | 279 size_t* bytes_written) |
| 280 MOJO_EXCLUSIVE_LOCKS_REQUIRED(write_mutex_) = 0; |
| 278 // Similar to |WriteNoLock()|, except that the implementing subclass must also | 281 // Similar to |WriteNoLock()|, except that the implementing subclass must also |
| 279 // guarantee that the method doesn't succeed synchronously, i.e., it only | 282 // guarantee that the method doesn't succeed synchronously, i.e., it only |
| 280 // returns |IO_FAILED_...| or |IO_PENDING|. | 283 // returns |IO_FAILED_...| or |IO_PENDING|. |
| 281 virtual IOResult ScheduleWriteNoLock() = 0; | 284 virtual IOResult ScheduleWriteNoLock() = 0; |
| 282 | 285 |
| 283 // Must be called on the I/O thread WITHOUT |write_lock_| held. | 286 // Must be called on the I/O thread. |
| 284 virtual void OnInit() = 0; | 287 virtual void OnInit() MOJO_LOCKS_EXCLUDED(write_mutex_) = 0; |
| 285 // On shutdown, passes the ownership of the buffers to subclasses, which may | 288 // 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 | 289 // 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 | 290 // called, |OnReadCompleted()| must no longer be called. Must be called on the |
| 288 // I/O thread under |write_lock_|. | 291 // I/O thread. |
| 289 virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer, | 292 virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer, |
| 290 scoped_ptr<WriteBuffer> write_buffer) = 0; | 293 scoped_ptr<WriteBuffer> write_buffer) |
| 294 MOJO_EXCLUSIVE_LOCKS_REQUIRED(write_mutex_) = 0; |
| 291 | 295 |
| 292 private: | 296 private: |
| 293 // Converts an |IO_FAILED_...| for a read to a |Delegate::Error|. | 297 // Converts an |IO_FAILED_...| for a read to a |Delegate::Error|. |
| 294 static Delegate::Error ReadIOResultToError(IOResult io_result); | 298 static Delegate::Error ReadIOResultToError(IOResult io_result); |
| 295 | 299 |
| 296 // Calls |delegate_->OnError(error)|. Must be called on the I/O thread WITHOUT | 300 // Calls |delegate_->OnError(error)|. Must be called on the I/O thread. This |
| 297 // |write_lock_| held. This object may be destroyed by this call. | 301 // object may be destroyed by this call. |
| 298 void CallOnError(Delegate::Error error); | 302 void CallOnError(Delegate::Error error) MOJO_LOCKS_EXCLUDED(write_mutex_); |
| 299 | 303 |
| 300 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a | 304 // 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 | 305 // 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 | 306 // 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 | 307 // false. May only be called if |write_stopped_| is false. |
| 304 // false. | |
| 305 bool OnWriteCompletedNoLock(IOResult io_result, | 308 bool OnWriteCompletedNoLock(IOResult io_result, |
| 306 size_t platform_handles_written, | 309 size_t platform_handles_written, |
| 307 size_t bytes_written); | 310 size_t bytes_written) |
| 311 MOJO_EXCLUSIVE_LOCKS_REQUIRED(write_mutex_); |
| 308 | 312 |
| 309 // Set in |Init()| and never changed (hence usable on any thread without | 313 // Set in |Init()| and never changed (hence usable on any thread without |
| 310 // locking): | 314 // locking): |
| 311 base::MessageLoopForIO* message_loop_for_io_; | 315 base::MessageLoopForIO* message_loop_for_io_; |
| 312 | 316 |
| 313 // Only used on the I/O thread: | 317 // Only used on the I/O thread: |
| 314 Delegate* delegate_; | 318 Delegate* delegate_; |
| 315 bool* set_on_shutdown_; | 319 bool* set_on_shutdown_; |
| 316 scoped_ptr<ReadBuffer> read_buffer_; | 320 scoped_ptr<ReadBuffer> read_buffer_; |
| 317 | 321 |
| 318 base::Lock write_lock_; // Protects the following members. | 322 Mutex write_mutex_; // Protects the following members. |
| 319 bool write_stopped_; | 323 bool write_stopped_ MOJO_GUARDED_BY(write_mutex_); |
| 320 scoped_ptr<WriteBuffer> write_buffer_; | 324 scoped_ptr<WriteBuffer> write_buffer_ MOJO_GUARDED_BY(write_mutex_); |
| 321 | 325 |
| 322 // This is used for posting tasks from write threads to the I/O thread. It | 326 // This is used for posting tasks from write threads to the I/O thread. The |
| 323 // must only be accessed under |write_lock_|. The weak pointers it produces | 327 // weak pointers it produces are only used/invalidated on the I/O thread. |
| 324 // are only used/invalidated on the I/O thread. | 328 base::WeakPtrFactory<RawChannel> weak_ptr_factory_ |
| 325 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | 329 MOJO_GUARDED_BY(write_mutex_); |
| 326 | 330 |
| 327 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); | 331 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); |
| 328 }; | 332 }; |
| 329 | 333 |
| 330 } // namespace system | 334 } // namespace system |
| 331 } // namespace mojo | 335 } // namespace mojo |
| 332 | 336 |
| 333 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 337 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
| OLD | NEW |