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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 FATAL_ERROR_FAILED_READ, | 52 FATAL_ERROR_FAILED_READ, |
53 FATAL_ERROR_FAILED_WRITE | 53 FATAL_ERROR_FAILED_WRITE |
54 }; | 54 }; |
55 | 55 |
56 // Called when a message is read. This may call |Shutdown()| (on the | 56 // Called when a message is read. This may call |Shutdown()| (on the |
57 // |RawChannel|), but must not destroy it. | 57 // |RawChannel|), but must not destroy it. |
58 virtual void OnReadMessage(const MessageInTransit::View& message_view) = 0; | 58 virtual void OnReadMessage(const MessageInTransit::View& message_view) = 0; |
59 | 59 |
60 // Called when there's a fatal error, which leads to the channel no longer | 60 // Called when there's a fatal error, which leads to the channel no longer |
61 // being viable. This may call |Shutdown()| (on the |RawChannel()|), but | 61 // being viable. This may call |Shutdown()| (on the |RawChannel()|), but |
62 // must now destroy it. | 62 // must not destroy it. |
63 // | 63 // |
64 // For each raw channel, at most one |FATAL_ERROR_FAILED_READ| and at most | 64 // For each raw channel, at most one |FATAL_ERROR_FAILED_READ| and at most |
65 // one |FATAL_ERROR_FAILED_WRITE| notification will be issued (both may be | 65 // one |FATAL_ERROR_FAILED_WRITE| notification will be issued (both may be |
66 // issued). After a |OnFatalError(FATAL_ERROR_FAILED_READ)|, there will be | 66 // issued). After a |OnFatalError(FATAL_ERROR_FAILED_READ)|, there will be |
67 // no further calls to |OnReadMessage()|. | 67 // no further calls to |OnReadMessage()|. |
68 virtual void OnFatalError(FatalError fatal_error) = 0; | 68 virtual void OnFatalError(FatalError fatal_error) = 0; |
69 | 69 |
70 protected: | 70 protected: |
71 virtual ~Delegate() {} | 71 virtual ~Delegate() {} |
72 }; | 72 }; |
73 | 73 |
74 // Static factory method. |handle| should be a handle to a | 74 // Static factory method. |handle| should be a handle to a |
75 // (platform-appropriate) bidirectional communication channel (e.g., a socket | 75 // (platform-appropriate) bidirectional communication channel (e.g., a socket |
76 // on POSIX, a named pipe on Windows). | 76 // on POSIX, a named pipe on Windows). |
77 static scoped_ptr<RawChannel> Create(embedder::ScopedPlatformHandle handle); | 77 static scoped_ptr<RawChannel> Create(embedder::ScopedPlatformHandle handle); |
78 | 78 |
79 // This must be called (on an I/O thread) before this object is used. Does | 79 // This must be called (on an I/O thread) before this object is used. Does |
80 // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must | 80 // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must |
81 // remain alive for the lifetime of this object. Returns true on success. On | 81 // remain alive until |Shutdown()| is called (unless this fails); |delegate| |
| 82 // will no longer be used after |Shutdown()|. Returns true on success. On |
82 // failure, |Shutdown()| should *not* be called. | 83 // failure, |Shutdown()| should *not* be called. |
83 bool Init(Delegate* delegate); | 84 bool Init(Delegate* delegate); |
84 | 85 |
85 // This must be called (on the I/O thread) before this object is destroyed. | 86 // This must be called (on the I/O thread) before this object is destroyed. |
86 void Shutdown(); | 87 void Shutdown(); |
87 | 88 |
88 // Writes the given message (or schedules it to be written). This is | 89 // Writes the given message (or schedules it to be written). This is |
89 // thread-safe. Returns true on success. | 90 // thread-safe. Returns true on success. |
90 bool WriteMessage(scoped_ptr<MessageInTransit> message); | 91 bool WriteMessage(scoped_ptr<MessageInTransit> message); |
91 | 92 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 // - if the method returns IO_PENDING, |OnWriteCompleted()| will be called on | 193 // - if the method returns IO_PENDING, |OnWriteCompleted()| will be called on |
193 // the I/O thread to report the result, unless |Shutdown()| is called. | 194 // the I/O thread to report the result, unless |Shutdown()| is called. |
194 virtual IOResult WriteNoLock(size_t* bytes_written) = 0; | 195 virtual IOResult WriteNoLock(size_t* bytes_written) = 0; |
195 // Similar to |WriteNoLock()|, except that the implementing subclass must also | 196 // Similar to |WriteNoLock()|, except that the implementing subclass must also |
196 // guarantee that the method doesn't succeed synchronously, i.e., it only | 197 // guarantee that the method doesn't succeed synchronously, i.e., it only |
197 // returns IO_FAILED or IO_PENDING. | 198 // returns IO_FAILED or IO_PENDING. |
198 virtual IOResult ScheduleWriteNoLock() = 0; | 199 virtual IOResult ScheduleWriteNoLock() = 0; |
199 | 200 |
200 // Must be called on the I/O thread WITHOUT |write_lock_| held. | 201 // Must be called on the I/O thread WITHOUT |write_lock_| held. |
201 virtual bool OnInit() = 0; | 202 virtual bool OnInit() = 0; |
202 // On shutdown, passes the ownership of the buffers to subclasses, who may | 203 // On shutdown, passes the ownership of the buffers to subclasses, which may |
203 // want to preserve them if there are pending read/write. | 204 // want to preserve them if there are pending read/write. Must be called on |
204 // Must be called on the I/O thread under |write_lock_|. | 205 // the I/O thread under |write_lock_|. |
205 virtual void OnShutdownNoLock( | 206 virtual void OnShutdownNoLock( |
206 scoped_ptr<ReadBuffer> read_buffer, | 207 scoped_ptr<ReadBuffer> read_buffer, |
207 scoped_ptr<WriteBuffer> write_buffer) = 0; | 208 scoped_ptr<WriteBuffer> write_buffer) = 0; |
208 | 209 |
209 // Must be called on the I/O thread WITHOUT |write_lock_| held. | 210 // Must be called on the I/O thread WITHOUT |write_lock_| held. |
210 void OnReadCompleted(bool result, size_t bytes_read); | 211 void OnReadCompleted(bool result, size_t bytes_read); |
211 // Must be called on the I/O thread WITHOUT |write_lock_| held. | 212 // Must be called on the I/O thread WITHOUT |write_lock_| held. |
212 void OnWriteCompleted(bool result, size_t bytes_written); | 213 void OnWriteCompleted(bool result, size_t bytes_written); |
213 | 214 |
214 private: | 215 private: |
215 // Calls |delegate_->OnFatalError(fatal_error)|. Must be called on the I/O | 216 // Calls |delegate_->OnFatalError(fatal_error)|. Must be called on the I/O |
216 // thread WITHOUT |write_lock_| held. | 217 // thread WITHOUT |write_lock_| held. |
217 void CallOnFatalError(Delegate::FatalError fatal_error); | 218 void CallOnFatalError(Delegate::FatalError fatal_error); |
218 | 219 |
219 // If |result| is true, updates the write buffer and schedules a write | 220 // 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 | 221 // 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 | 222 // false or any error occurs during the method execution, cancels pending |
222 // writes and returns false. | 223 // writes and returns false. |
223 // Must be called only if |write_stopped_| is false and under |write_lock_|. | 224 // Must be called only if |write_stopped_| is false and under |write_lock_|. |
224 bool OnWriteCompletedNoLock(bool result, size_t bytes_written); | 225 bool OnWriteCompletedNoLock(bool result, size_t bytes_written); |
225 | 226 |
226 // Set in |Init()| and never changed (hence usable on any thread without | 227 // Set in |Init()| and never changed (hence usable on any thread without |
227 // locking): | 228 // locking): |
228 Delegate* delegate_; | |
229 base::MessageLoopForIO* message_loop_for_io_; | 229 base::MessageLoopForIO* message_loop_for_io_; |
230 | 230 |
231 // Only used on the I/O thread: | 231 // Only used on the I/O thread: |
| 232 Delegate* delegate_; |
232 bool read_stopped_; | 233 bool read_stopped_; |
233 scoped_ptr<ReadBuffer> read_buffer_; | 234 scoped_ptr<ReadBuffer> read_buffer_; |
234 | 235 |
235 base::Lock write_lock_; // Protects the following members. | 236 base::Lock write_lock_; // Protects the following members. |
236 bool write_stopped_; | 237 bool write_stopped_; |
237 scoped_ptr<WriteBuffer> write_buffer_; | 238 scoped_ptr<WriteBuffer> write_buffer_; |
238 | 239 |
239 // This is used for posting tasks from write threads to the I/O thread. It | 240 // This is used for posting tasks from write threads to the I/O thread. It |
240 // must only be accessed under |write_lock_|. The weak pointers it produces | 241 // must only be accessed under |write_lock_|. The weak pointers it produces |
241 // are only used/invalidated on the I/O thread. | 242 // are only used/invalidated on the I/O thread. |
242 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | 243 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; |
243 | 244 |
244 DISALLOW_COPY_AND_ASSIGN(RawChannel); | 245 DISALLOW_COPY_AND_ASSIGN(RawChannel); |
245 }; | 246 }; |
246 | 247 |
247 } // namespace system | 248 } // namespace system |
248 } // namespace mojo | 249 } // namespace mojo |
249 | 250 |
250 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ | 251 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ |
OLD | NEW |