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 { |
viettrungluu
2014/02/27 07:36:35
nit: class
yzshen1
2014/02/27 16:19:33
Done.
| |
102 public: | |
103 ReadBuffer(); | |
104 ~ReadBuffer(); | |
105 | |
106 void GetBuffer(char** addr, size_t* size); | |
107 | |
108 private: | |
109 friend class RawChannel; | |
110 | |
111 // We store data from |[Schedule]Read()|s in |buffer_|. The start of | |
112 // |buffer_| is always aligned with a message boundary (we will copy memory | |
113 // to ensure this), but |buffer_| may be larger than the actual number of | |
114 // bytes we have. | |
115 std::vector<char> buffer_; | |
116 size_t num_valid_bytes_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(ReadBuffer); | |
119 }; | |
120 | |
121 struct WriteBuffer { | |
viettrungluu
2014/02/27 07:36:35
"
yzshen1
2014/02/27 16:19:33
Done.
| |
122 public: | |
123 struct Buffer { | |
124 const char* addr; | |
125 size_t size; | |
126 }; | |
127 | |
128 WriteBuffer(); | |
129 ~WriteBuffer(); | |
130 | |
131 void GetBuffers(std::vector<Buffer>* buffers) const; | |
132 // Returns the total size of all buffers returned by |GetBuffers()|. | |
133 size_t GetTotalBytesToWrite() const; | |
134 | |
135 private: | |
136 friend class RawChannel; | |
137 | |
138 // TODO(vtl): When C++11 is available, switch this to a deque of | |
139 // |scoped_ptr|/|unique_ptr|s. | |
140 std::deque<MessageInTransit*> message_queue_; | |
141 // The first message may have been partially sent. |offset_| indicates the | |
142 // position in the first message where to start the next write. | |
143 size_t offset_; | |
144 | |
145 DISALLOW_COPY_AND_ASSIGN(WriteBuffer); | |
146 }; | |
147 | |
148 RawChannel(Delegate* delegate, base::MessageLoopForIO* message_loop_for_io); | |
149 | |
95 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } | 150 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } |
151 base::Lock& write_lock() { return write_lock_; } | |
152 | |
153 // Only accessed on the I/O thread. | |
154 ReadBuffer* read_buffer(); | |
155 | |
156 // Only accessed under |write_lock_|. | |
157 WriteBuffer* write_buffer_no_lock(); | |
158 | |
159 // Reads into |read_buffer()|. | |
160 // This class guarantees that: | |
161 // - the area indicated by |GetBuffer()| will stay valid until read completion | |
162 // (but please also see the comments for |OnShutdownNoLock()|); | |
163 // - a second read is not started if there is a pending read; | |
164 // - the method is called on the I/O thread WITHOUT |write_lock_| held. | |
165 // | |
166 // The implementing subclass must guarantee that: | |
167 // - |bytes_read| is untouched if the method returns values other than | |
168 // IO_SUCCEEDED; | |
169 // - if the method returns IO_PENDING, |OnReadCompleted()| will be called on | |
170 // the I/O thread to report the result, unless |Shutdown()| is called. | |
171 virtual IOResult Read(size_t* bytes_read) = 0; | |
172 // Similar to |Read()|, except that the implementing subclass must also | |
173 // guarantee that the method doesn't succeed synchronously, i.e., it only | |
174 // returns IO_FAILED or IO_PENDING. | |
175 virtual IOResult ScheduleRead() = 0; | |
176 | |
177 // Writes contents in |write_buffer_no_lock()|. | |
178 // This class guarantees that: | |
179 // - the area indicated by |GetBuffers()| will stay valid until write | |
180 // completion (but please also see the comments for |OnShutdownNoLock()|); | |
181 // - a second write is not started if there is a pending write; | |
182 // - the method is called under |write_lock_|. | |
183 // | |
184 // The implementing subclass must guarantee that: | |
185 // - |bytes_written| is untouched if the method returns values other than | |
186 // IO_SUCCEEDED; | |
187 // - if the method returns IO_PENDING, |OnWriteCompleted()| will be called on | |
188 // the I/O thread to report the result, unless |Shutdown()| is called. | |
189 virtual IOResult WriteNoLock(size_t* bytes_written) = 0; | |
190 // Similar to |WriteNoLock()|, except that the implementing subclass must also | |
191 // guarantee that the method doesn't succeed synchronously, i.e., it only | |
192 // returns IO_FAILED or IO_PENDING. | |
193 virtual IOResult ScheduleWriteNoLock() = 0; | |
194 | |
195 // Must be called on the I/O thread WITHOUT |write_lock_| held. | |
196 virtual bool OnInit() = 0; | |
197 // On shutdown, passes the ownership of the buffers to subclasses, who may | |
198 // want to preserve them if there are pending read/write. | |
199 // Must be called on the I/O thread under |write_lock_|. | |
200 virtual void OnShutdownNoLock( | |
201 scoped_ptr<ReadBuffer> read_buffer, | |
202 scoped_ptr<WriteBuffer> write_buffer) = 0; | |
203 | |
204 // Must be called on the I/O thread WITHOUT |write_lock_| held. | |
205 void OnReadCompleted(bool result, size_t bytes_read); | |
206 // Must be called on the I/O thread WITHOUT |write_lock_| held. | |
207 void OnWriteCompleted(bool result, size_t bytes_written); | |
96 | 208 |
97 private: | 209 private: |
210 // Calls |delegate_->OnFatalError(fatal_error)|. Must be called on the I/O | |
211 // thread WITHOUT |write_lock_| held. | |
212 void CallOnFatalError(Delegate::FatalError fatal_error); | |
213 | |
214 // If |result| is true, updates the write buffer and schedules a write | |
215 // operation to run later if there are more contents to write. If |result| is | |
216 // false or any error occurs during the method execution, cancels pending | |
217 // writes and returns false. | |
218 // Must be called only if |write_stopped_| is false and under |write_lock_|. | |
219 bool OnWriteCompletedNoLock(bool result, size_t bytes_written); | |
220 | |
98 Delegate* const delegate_; | 221 Delegate* const delegate_; |
99 base::MessageLoopForIO* const message_loop_for_io_; | 222 base::MessageLoopForIO* const message_loop_for_io_; |
100 | 223 |
224 // Only used on the I/O thread: | |
225 bool read_stopped_; | |
226 scoped_ptr<ReadBuffer> read_buffer_; | |
227 | |
228 base::Lock write_lock_; // Protects the following members. | |
229 bool write_stopped_; | |
230 scoped_ptr<WriteBuffer> write_buffer_; | |
231 | |
232 // This is used for posting tasks from write threads to the I/O thread. It | |
233 // must only be accessed under |write_lock_|. The weak pointers it produces | |
234 // are only used/invalidated on the I/O thread. | |
235 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | |
236 | |
101 DISALLOW_COPY_AND_ASSIGN(RawChannel); | 237 DISALLOW_COPY_AND_ASSIGN(RawChannel); |
102 }; | 238 }; |
103 | 239 |
104 } // namespace system | 240 } // namespace system |
105 } // namespace mojo | 241 } // namespace mojo |
106 | 242 |
107 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ | 243 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ |
OLD | NEW |