| 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_SYSTEM_RAW_CHANNEL_H_ | 5 #ifndef MOJO_SYSTEM_RAW_CHANNEL_H_ |
| 6 #define MOJO_SYSTEM_RAW_CHANNEL_H_ | 6 #define MOJO_SYSTEM_RAW_CHANNEL_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // After |OnFatalError(FATAL_ERROR_FAILED_READ)| there won't be further | 65 // After |OnFatalError(FATAL_ERROR_FAILED_READ)| there won't be further |
| 66 // |OnReadMessage()| calls. | 66 // |OnReadMessage()| calls. |
| 67 virtual void OnFatalError(FatalError fatal_error) = 0; | 67 virtual void OnFatalError(FatalError fatal_error) = 0; |
| 68 | 68 |
| 69 protected: | 69 protected: |
| 70 virtual ~Delegate() {} | 70 virtual ~Delegate() {} |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 // Static factory method. |handle| should be a handle to a | 73 // Static factory method. |handle| should be a handle to a |
| 74 // (platform-appropriate) bidirectional communication channel (e.g., a socket | 74 // (platform-appropriate) bidirectional communication channel (e.g., a socket |
| 75 // on POSIX, a named pipe on Windows). Does *not* take ownership of |delegate| | 75 // on POSIX, a named pipe on Windows). |
| 76 // and |message_loop_for_io|, which must remain alive while this object does. | 76 static scoped_ptr<RawChannel> Create(embedder::ScopedPlatformHandle handle); |
| 77 static RawChannel* Create(embedder::ScopedPlatformHandle handle, | |
| 78 Delegate* delegate, | |
| 79 base::MessageLoopForIO* message_loop_for_io); | |
| 80 | 77 |
| 81 // This must be called (on an I/O thread) before this object is used. Returns | 78 // This must be called (on an I/O thread) before this object is used. Does |
| 82 // true on success. On failure, |Shutdown()| should *not* be called. | 79 // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must |
| 83 bool Init(); | 80 // remain alive for the lifetime of this object. Returns true on success. On |
| 81 // failure, |Shutdown()| should *not* be called. |
| 82 bool Init(Delegate* delegate); |
| 84 | 83 |
| 85 // This must be called (on the I/O thread) before this object is destroyed. | 84 // This must be called (on the I/O thread) before this object is destroyed. |
| 86 void Shutdown(); | 85 void Shutdown(); |
| 87 | 86 |
| 88 // Writes the given message (or schedules it to be written). This is | 87 // Writes the given message (or schedules it to be written). This is |
| 89 // thread-safe. Returns true on success. | 88 // thread-safe. Returns true on success. |
| 90 bool WriteMessage(scoped_ptr<MessageInTransit> message); | 89 bool WriteMessage(scoped_ptr<MessageInTransit> message); |
| 91 | 90 |
| 92 // Returns true if the write buffer is empty (i.e., all messages written using | 91 // Returns true if the write buffer is empty (i.e., all messages written using |
| 93 // |WriteMessage()| have actually been sent. | 92 // |WriteMessage()| have actually been sent. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 // TODO(vtl): When C++11 is available, switch this to a deque of | 142 // TODO(vtl): When C++11 is available, switch this to a deque of |
| 144 // |scoped_ptr|/|unique_ptr|s. | 143 // |scoped_ptr|/|unique_ptr|s. |
| 145 std::deque<MessageInTransit*> message_queue_; | 144 std::deque<MessageInTransit*> message_queue_; |
| 146 // The first message may have been partially sent. |offset_| indicates the | 145 // The first message may have been partially sent. |offset_| indicates the |
| 147 // position in the first message where to start the next write. | 146 // position in the first message where to start the next write. |
| 148 size_t offset_; | 147 size_t offset_; |
| 149 | 148 |
| 150 DISALLOW_COPY_AND_ASSIGN(WriteBuffer); | 149 DISALLOW_COPY_AND_ASSIGN(WriteBuffer); |
| 151 }; | 150 }; |
| 152 | 151 |
| 153 RawChannel(Delegate* delegate, base::MessageLoopForIO* message_loop_for_io); | 152 RawChannel(); |
| 154 | 153 |
| 155 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } | 154 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } |
| 156 base::Lock& write_lock() { return write_lock_; } | 155 base::Lock& write_lock() { return write_lock_; } |
| 157 | 156 |
| 158 // Only accessed on the I/O thread. | 157 // Only accessed on the I/O thread. |
| 159 ReadBuffer* read_buffer(); | 158 ReadBuffer* read_buffer(); |
| 160 | 159 |
| 161 // Only accessed under |write_lock_|. | 160 // Only accessed under |write_lock_|. |
| 162 WriteBuffer* write_buffer_no_lock(); | 161 WriteBuffer* write_buffer_no_lock(); |
| 163 | 162 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 // thread WITHOUT |write_lock_| held. | 215 // thread WITHOUT |write_lock_| held. |
| 217 void CallOnFatalError(Delegate::FatalError fatal_error); | 216 void CallOnFatalError(Delegate::FatalError fatal_error); |
| 218 | 217 |
| 219 // If |result| is true, updates the write buffer and schedules a write | 218 // If |result| is true, updates the write buffer and schedules a write |
| 220 // operation to run later if there are more contents to write. If |result| is | 219 // operation to run later if there are more contents to write. If |result| is |
| 221 // false or any error occurs during the method execution, cancels pending | 220 // false or any error occurs during the method execution, cancels pending |
| 222 // writes and returns false. | 221 // writes and returns false. |
| 223 // Must be called only if |write_stopped_| is false and under |write_lock_|. | 222 // Must be called only if |write_stopped_| is false and under |write_lock_|. |
| 224 bool OnWriteCompletedNoLock(bool result, size_t bytes_written); | 223 bool OnWriteCompletedNoLock(bool result, size_t bytes_written); |
| 225 | 224 |
| 226 Delegate* const delegate_; | 225 // Set in |Init()| and never changed (hence usable on any thread without |
| 227 base::MessageLoopForIO* const message_loop_for_io_; | 226 // locking): |
| 227 Delegate* delegate_; |
| 228 base::MessageLoopForIO* message_loop_for_io_; |
| 228 | 229 |
| 229 // Only used on the I/O thread: | 230 // Only used on the I/O thread: |
| 230 bool read_stopped_; | 231 bool read_stopped_; |
| 231 scoped_ptr<ReadBuffer> read_buffer_; | 232 scoped_ptr<ReadBuffer> read_buffer_; |
| 232 | 233 |
| 233 base::Lock write_lock_; // Protects the following members. | 234 base::Lock write_lock_; // Protects the following members. |
| 234 bool write_stopped_; | 235 bool write_stopped_; |
| 235 scoped_ptr<WriteBuffer> write_buffer_; | 236 scoped_ptr<WriteBuffer> write_buffer_; |
| 236 | 237 |
| 237 // This is used for posting tasks from write threads to the I/O thread. It | 238 // This is used for posting tasks from write threads to the I/O thread. It |
| 238 // must only be accessed under |write_lock_|. The weak pointers it produces | 239 // must only be accessed under |write_lock_|. The weak pointers it produces |
| 239 // are only used/invalidated on the I/O thread. | 240 // are only used/invalidated on the I/O thread. |
| 240 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | 241 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; |
| 241 | 242 |
| 242 DISALLOW_COPY_AND_ASSIGN(RawChannel); | 243 DISALLOW_COPY_AND_ASSIGN(RawChannel); |
| 243 }; | 244 }; |
| 244 | 245 |
| 245 } // namespace system | 246 } // namespace system |
| 246 } // namespace mojo | 247 } // namespace mojo |
| 247 | 248 |
| 248 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ | 249 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ |
| OLD | NEW |