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 <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/synchronization/lock.h" | |
12 #include "mojo/system/constants.h" | 15 #include "mojo/system/constants.h" |
13 #include "mojo/system/embedder/scoped_platform_handle.h" | 16 #include "mojo/system/embedder/scoped_platform_handle.h" |
14 #include "mojo/system/system_impl_export.h" | 17 #include "mojo/system/system_impl_export.h" |
15 | 18 |
16 namespace base { | 19 namespace base { |
17 class MessageLoopForIO; | 20 class MessageLoopForIO; |
18 } | 21 } |
19 | 22 |
20 namespace mojo { | 23 namespace mojo { |
21 namespace system { | 24 namespace system { |
(...skipping 10 matching lines...) Expand all Loading... | |
32 // the aforementioned thread). | 35 // the aforementioned thread). |
33 // | 36 // |
34 // OS-specific implementation subclasses are to be instantiated using the | 37 // OS-specific implementation subclasses are to be instantiated using the |
35 // |Create()| static factory method. | 38 // |Create()| static factory method. |
36 // | 39 // |
37 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in | 40 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in |
38 // general its methods should only be used on the I/O thread, i.e., the thread | 41 // general its methods should only be used on the I/O thread, i.e., the thread |
39 // on which |Init()| is called). | 42 // on which |Init()| is called). |
40 class MOJO_SYSTEM_IMPL_EXPORT RawChannel { | 43 class MOJO_SYSTEM_IMPL_EXPORT RawChannel { |
41 public: | 44 public: |
42 virtual ~RawChannel() {} | 45 virtual ~RawChannel(); |
43 | 46 |
44 // The |Delegate| is only accessed on the same thread as the message loop | 47 // The |Delegate| is only accessed on the same thread as the message loop |
45 // (passed in on creation). | 48 // (passed in on creation). |
46 class MOJO_SYSTEM_IMPL_EXPORT Delegate { | 49 class MOJO_SYSTEM_IMPL_EXPORT Delegate { |
47 public: | 50 public: |
48 enum FatalError { | 51 enum FatalError { |
49 FATAL_ERROR_UNKNOWN = 0, | 52 FATAL_ERROR_UNKNOWN = 0, |
50 FATAL_ERROR_FAILED_READ, | 53 FATAL_ERROR_FAILED_READ, |
51 FATAL_ERROR_FAILED_WRITE | 54 FATAL_ERROR_FAILED_WRITE |
52 }; | 55 }; |
(...skipping 18 matching lines...) Expand all Loading... | |
71 // Static factory method. |handle| should be a handle to a | 74 // Static factory method. |handle| should be a handle to a |
72 // (platform-appropriate) bidirectional communication channel (e.g., a socket | 75 // (platform-appropriate) bidirectional communication channel (e.g., a socket |
73 // on POSIX, a named pipe on Windows). Does *not* take ownership of |delegate| | 76 // on POSIX, a named pipe on Windows). Does *not* take ownership of |delegate| |
74 // and |message_loop_for_io|, which must remain alive while this object does. | 77 // and |message_loop_for_io|, which must remain alive while this object does. |
75 static RawChannel* Create(embedder::ScopedPlatformHandle handle, | 78 static RawChannel* Create(embedder::ScopedPlatformHandle handle, |
76 Delegate* delegate, | 79 Delegate* delegate, |
77 base::MessageLoopForIO* message_loop_for_io); | 80 base::MessageLoopForIO* message_loop_for_io); |
78 | 81 |
79 // This must be called (on an I/O thread) before this object is used. Returns | 82 // This must be called (on an I/O thread) before this object is used. Returns |
80 // true on success. On failure, |Shutdown()| should *not* be called. | 83 // true on success. On failure, |Shutdown()| should *not* be called. |
81 virtual bool Init() = 0; | 84 bool Init(); |
82 | 85 |
83 // 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. |
84 virtual void Shutdown() = 0; | 87 void Shutdown(); |
85 | 88 |
86 // This is thread-safe. It takes ownership of |message| (always, even on | 89 // This is thread-safe. It takes ownership of |message| (always, even on |
87 // failure). Returns true on success. | 90 // failure). Returns true on success. |
88 virtual bool WriteMessage(scoped_ptr<MessageInTransit> message) = 0; | 91 bool WriteMessage(scoped_ptr<MessageInTransit> message); |
89 | 92 |
90 protected: | 93 protected: |
91 RawChannel(Delegate* delegate, base::MessageLoopForIO* message_loop_for_io) | 94 // Return values of |[Schedule]Read()| and |[Schedule]WriteNoLock()|. |
92 : delegate_(delegate), message_loop_for_io_(message_loop_for_io) {} | 95 enum IOResult { |
96 IO_SUCCEEDED, | |
97 IO_FAILED, | |
98 IO_PENDING | |
99 }; | |
93 | 100 |
94 Delegate* delegate() { return delegate_; } | 101 struct ReadBuffer { |
102 public: | |
103 ReadBuffer(); | |
104 ~ReadBuffer(); | |
105 | |
106 char* GetPosition(); | |
107 size_t GetBytesToRead() const; | |
108 | |
109 private: | |
110 friend class RawChannel; | |
111 | |
112 // We store data from |[Schedule]Read()|s in |buffer_|. The start of | |
113 // |buffer_| is always aligned with a message boundary (we will copy memory | |
114 // to ensure this), but |buffer_| may be larger than the actual number of | |
115 // bytes we have. | |
116 std::vector<char> buffer_; | |
117 size_t num_valid_bytes_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(ReadBuffer); | |
120 }; | |
121 | |
122 struct WriteBuffer { | |
123 public: | |
124 WriteBuffer(); | |
125 ~WriteBuffer(); | |
126 | |
127 const char* GetPosition() const; | |
128 size_t GetBytesToWrite() const; | |
129 | |
130 private: | |
131 friend class RawChannel; | |
132 | |
133 // TODO(vtl): When C++11 is available, switch this to a deque of | |
134 // |scoped_ptr|/|unique_ptr|s. | |
135 std::deque<MessageInTransit*> message_queue_; | |
136 // The first message may have been partially sent. |offset_| indicates the | |
137 // position in the first message where to start the next write. | |
138 size_t offset_; | |
139 | |
140 DISALLOW_COPY_AND_ASSIGN(WriteBuffer); | |
141 }; | |
142 | |
143 RawChannel(Delegate* delegate, base::MessageLoopForIO* message_loop_for_io); | |
144 | |
95 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } | 145 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } |
146 base::Lock& write_lock() { return write_lock_; } | |
147 | |
148 // Only accessed on the I/O thread. | |
149 ReadBuffer* read_buffer(); | |
150 | |
151 // Only accessed under |write_lock_|. | |
152 WriteBuffer* write_buffer(); | |
viettrungluu
2014/02/26 23:03:39
-> write_buffer_no_lock
yzshen1
2014/02/27 02:00:30
Done.
| |
153 | |
154 // Reads into |read_buffer()|, the area indicated by |GetPosition()| and | |
viettrungluu
2014/02/26 23:03:39
nit: comma-splice: , the -> . The
yzshen1
2014/02/27 02:00:30
Done.
| |
155 // |GetBytesToRead()| will stay valid until read completion. (But please also | |
viettrungluu
2014/02/26 23:03:39
nit: "completion. (But [...] .)" -> "completion (b
yzshen1
2014/02/27 02:00:30
Done. Thanks! :)
| |
156 // see comments for |OnShutdownNoLock()|.) | |
157 // |bytes_read| is untouched if the method returns values other than | |
158 // IO_SUCCEEDED. | |
159 // If the method returns IO_PENDING, |OnReadCompleted()| will be called on the | |
viettrungluu
2014/02/26 23:03:39
nit: -> If IO_PENDING is returned, |OnReadComplete
yzshen1
2014/02/27 02:00:30
Good point. Done.
| |
160 // I/O thread to report the result, unless |Shutdown()| happens. | |
viettrungluu
2014/02/26 23:03:39
happens -> is called
yzshen1
2014/02/27 02:00:30
Done.
| |
161 // A second read shouldn't be started if there is a pending read. | |
162 // Must be called on the I/O thread WITHOUT |write_lock_| held. | |
163 virtual IOResult Read(size_t* bytes_read) = 0; | |
164 // Similar to |Read()|, except that this method won't succeed synchronously, | |
165 // i.e., it is guaranteed to only return IO_FAILED or IO_PENDING. | |
166 virtual IOResult ScheduleRead() = 0; | |
167 | |
168 // Writes contents in |write_buffer()|, the area indicated by |GetPosition()| | |
viettrungluu
2014/02/26 23:03:39
(similarly)
I suggest that you merge the comments
yzshen1
2014/02/27 02:00:30
I made similar changes.
| |
169 // and |GetBytesToWrite()| will stay valid until write completion. (But please | |
170 // also see comments for |OnShutdownNoLock()|.) | |
171 // |bytes_written| is untouched if the method returns values other than | |
172 // IO_SUCCEEDED. | |
173 // If the method returns IO_PENDING, |OnWriteCompleted()| will be called on | |
174 // the I/O thread to report the result, unless |Shutdown()| happens. | |
175 // A second write shouldn't be started if there is a pending write. | |
176 // Must be called under |write_lock_|. | |
177 virtual IOResult WriteNoLock(size_t* bytes_written) = 0; | |
178 // Similar to |WriteNoLock()|, except that this method won't succeed | |
179 // synchronously, i.e., it is guaranteed to only returns IO_FAILED or | |
180 // IO_PENDING. | |
181 virtual IOResult ScheduleWriteNoLock() = 0; | |
182 | |
183 // Must be called on the I/O thread WITHOUT |write_lock_| held. | |
184 virtual bool OnInit() = 0; | |
185 // On shutdown, passes the ownership of the buffers to subclasses, who may | |
186 // want to preserve them if there are pending read/write. | |
187 // Must be called on the I/O thread under |write_lock_|. | |
188 virtual void OnShutdownNoLock( | |
189 scoped_ptr<ReadBuffer> read_buffer, | |
190 scoped_ptr<WriteBuffer> write_buffer) = 0; | |
191 | |
192 // Must be called on the I/O thread WITHOUT |write_lock_| held. | |
193 void OnReadCompleted(bool result, size_t bytes_read); | |
194 // Must be called on the I/O thread WITHOUT |write_lock_| held. | |
195 void OnWriteCompleted(bool result, size_t bytes_written); | |
96 | 196 |
97 private: | 197 private: |
198 // Calls |delegate_->OnFatalError(fatal_error)|. Must be called on the I/O | |
199 // thread WITHOUT |write_lock_| held. | |
200 void CallOnFatalError(Delegate::FatalError fatal_error); | |
201 | |
202 // If |result| is true, updates the write buffer and schedules a write | |
203 // operation to run later if there are more contents to write. If |result| is | |
204 // false or any error occurs during the method execution, cancels pending | |
205 // writes and returns false. | |
206 // Must be called only if |write_stopped_| is false and under |write_lock_|. | |
207 bool OnWriteCompletedNoLock(bool result, size_t bytes_written); | |
208 | |
209 // The following members are only used on the I/O thread: | |
98 Delegate* const delegate_; | 210 Delegate* const delegate_; |
211 | |
212 bool read_stopped_; | |
213 | |
214 scoped_ptr<ReadBuffer> read_buffer_; | |
215 | |
216 // The following members are used on mutiple threads: | |
99 base::MessageLoopForIO* const message_loop_for_io_; | 217 base::MessageLoopForIO* const message_loop_for_io_; |
viettrungluu
2014/02/26 23:03:39
nit: I think I'd prefer if you grouped the const p
yzshen1
2014/02/27 02:00:30
Done.
| |
100 | 218 |
219 base::Lock write_lock_; // Protects the following members. | |
220 bool write_stopped_; | |
221 | |
222 scoped_ptr<WriteBuffer> write_buffer_; | |
223 | |
224 // This is used for posting tasks from write threads to the I/O thread. It | |
225 // must only be accessed under |write_lock_|. The weak pointers it produces | |
226 // are only used/invalidated on the I/O thread. | |
227 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | |
228 | |
101 DISALLOW_COPY_AND_ASSIGN(RawChannel); | 229 DISALLOW_COPY_AND_ASSIGN(RawChannel); |
102 }; | 230 }; |
103 | 231 |
104 } // namespace system | 232 } // namespace system |
105 } // namespace mojo | 233 } // namespace mojo |
106 | 234 |
107 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ | 235 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ |
OLD | NEW |