Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: mojo/edk/system/raw_channel.h

Issue 1350023003: Add a Mojo EDK for Chrome that uses one OS pipe per message pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move to mojo::edk namespace in preparation for runtim flag Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 12 #include "base/synchronization/lock.h"
13 #include "mojo/edk/embedder/platform_handle_vector.h" 13 #include "mojo/edk/embedder/platform_handle_vector.h"
14 #include "mojo/edk/embedder/scoped_platform_handle.h" 14 #include "mojo/edk/embedder/scoped_platform_handle.h"
15 #include "mojo/edk/system/message_in_transit.h" 15 #include "mojo/edk/system/message_in_transit.h"
16 #include "mojo/edk/system/message_in_transit_queue.h" 16 #include "mojo/edk/system/message_in_transit_queue.h"
17 #include "mojo/edk/system/system_impl_export.h" 17 #include "mojo/edk/system/system_impl_export.h"
18 #include "mojo/public/cpp/system/macros.h" 18 #include "mojo/public/cpp/system/macros.h"
19 19
20 namespace base { 20 namespace base {
21 class MessageLoopForIO; 21 class MessageLoopForIO;
22 } 22 }
23 23
24 namespace mojo { 24 namespace mojo {
25 namespace system { 25 namespace edk {
26 26
27 // |RawChannel| is an interface and base class for objects that wrap an OS 27 // |RawChannel| is an interface and base class for objects that wrap an OS
28 // "pipe". It presents the following interface to users: 28 // "pipe". It presents the following interface to users:
29 // - Receives and dispatches messages on an I/O thread (running a 29 // - Receives and dispatches messages on an I/O thread (running a
30 // |MessageLoopForIO|. 30 // |MessageLoopForIO|.
31 // - Provides a thread-safe way of writing messages (|WriteMessage()|); 31 // - Provides a thread-safe way of writing messages (|WriteMessage()|);
32 // writing/queueing messages will not block and is atomic from the point of 32 // 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 33 // view of the caller. If necessary, messages are queued (to be written on
34 // the aforementioned thread). 34 // the aforementioned thread).
35 // 35 //
36 // OS-specific implementation subclasses are to be instantiated using the 36 // OS-specific implementation subclasses are to be instantiated using the
37 // |Create()| static factory method. 37 // |Create()| static factory method.
38 // 38 //
39 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in 39 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in
40 // general its methods should only be used on the I/O thread, i.e., the thread 40 // general its methods should only be used on the I/O thread, i.e., the thread
41 // on which |Init()| is called). 41 // on which |Init()| is called).
42 class MOJO_SYSTEM_IMPL_EXPORT RawChannel { 42 class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
43 public: 43 public:
44 // This object may be destroyed on any thread (if |Init()| was called, after
45 // |Shutdown()| was called).
46 virtual ~RawChannel();
47 44
48 // The |Delegate| is only accessed on the same thread as the message loop 45 // The |Delegate| is only accessed on the same thread as the message loop
49 // (passed in on creation). 46 // (passed in on creation).
50 class MOJO_SYSTEM_IMPL_EXPORT Delegate { 47 class MOJO_SYSTEM_IMPL_EXPORT Delegate {
51 public: 48 public:
52 enum Error { 49 enum Error {
53 // Failed read due to raw channel shutdown (e.g., on the other side). 50 // Failed read due to raw channel shutdown (e.g., on the other side).
54 ERROR_READ_SHUTDOWN, 51 ERROR_READ_SHUTDOWN,
55 // Failed read due to raw channel being broken (e.g., if the other side 52 // Failed read due to raw channel being broken (e.g., if the other side
56 // died without shutting down). 53 // died without shutting down).
57 ERROR_READ_BROKEN, 54 ERROR_READ_BROKEN,
58 // Received a bad message. 55 // Received a bad message.
59 ERROR_READ_BAD_MESSAGE, 56 ERROR_READ_BAD_MESSAGE,
60 // Unknown read error. 57 // Unknown read error.
61 ERROR_READ_UNKNOWN, 58 ERROR_READ_UNKNOWN,
62 // Generic write error. 59 // Generic write error.
63 ERROR_WRITE 60 ERROR_WRITE
64 }; 61 };
65 62
66 // Called when a message is read. This may call the |RawChannel|'s 63 // Called when a message is read. This may call the |RawChannel|'s
67 // |Shutdown()| and then (if desired) destroy it. 64 // |Shutdown()| and then (if desired) destroy it.
68 virtual void OnReadMessage( 65 virtual void OnReadMessage(
69 const MessageInTransit::View& message_view, 66 const MessageInTransit::View& message_view,
70 embedder::ScopedPlatformHandleVectorPtr platform_handles) = 0; 67 ScopedPlatformHandleVectorPtr platform_handles) = 0;
71 68
72 // Called when there's a (fatal) error. This may call the |RawChannel|'s 69 // Called when there's a (fatal) error. This may call the |RawChannel|'s
73 // |Shutdown()| and then (if desired) destroy it. 70 // |Shutdown()| and then (if desired) destroy it.
74 // 71 //
75 // For each raw channel, there'll be at most one |ERROR_READ_...| and at 72 // For each raw channel, there'll be at most one |ERROR_READ_...| and at
76 // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|, 73 // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|,
77 // |OnReadMessage()| won't be called again. 74 // |OnReadMessage()| won't be called again.
78 virtual void OnError(Error error) = 0; 75 virtual void OnError(Error error) = 0;
79 76
80 protected: 77 protected:
81 virtual ~Delegate() {} 78 virtual ~Delegate() {}
82 }; 79 };
83 80
84 // Static factory method. |handle| should be a handle to a 81 // Static factory method. |handle| should be a handle to a
85 // (platform-appropriate) bidirectional communication channel (e.g., a socket 82 // (platform-appropriate) bidirectional communication channel (e.g., a socket
86 // on POSIX, a named pipe on Windows). 83 // on POSIX, a named pipe on Windows).
87 static scoped_ptr<RawChannel> Create(embedder::ScopedPlatformHandle handle); 84 static RawChannel* Create(ScopedPlatformHandle handle);
85
86 // Returns the amount of space needed in the |MessageInTransit|'s
87 // |TransportData|'s "platform handle table" per platform handle (to be
88 // attached to a message). (This amount may be zero.)
89 static size_t GetSerializedPlatformHandleSize();
88 90
89 // This must be called (on an I/O thread) before this object is used. Does 91 // 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 92 // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must
91 // remain alive until |Shutdown()| is called (unless this fails); |delegate| 93 // remain alive until |Shutdown()| is called (unless this fails); |delegate|
92 // will no longer be used after |Shutdown()|. 94 // will no longer be used after |Shutdown()|.
93 void Init(Delegate* delegate); 95 void Init(Delegate* delegate);
94 96
95 // This must be called (on the I/O thread) before this object is destroyed. 97 // This must be called (on the I/O thread) before this object is destroyed.
96 void Shutdown(); 98 void Shutdown();
97 99
100 // Returns the platform handle for the pipe synchronously.
101
102
103 // Flushes all pending writes synchronously.
104
105
106 // TODO(jam): change above to write it to shared memory and return handle as
107 // writing synchronously won't work for lots of pending data.
108 // Returns any partially read data.
109 // NOTE: After calling this, consider the channel shutdown and don't call into
110 // it anymore
111 ScopedPlatformHandle ReleaseHandle(std::vector<char>* read_buffer);
112
98 // Writes the given message (or schedules it to be written). |message| must 113 // Writes the given message (or schedules it to be written). |message| must
99 // have no |Dispatcher|s still attached (i.e., 114 // have no |Dispatcher|s still attached (i.e.,
100 // |SerializeAndCloseDispatchers()| should have been called). This method is 115 // |SerializeAndCloseDispatchers()| should have been called). This method is
101 // thread-safe and may be called from any thread. Returns true on success. 116 // thread-safe and may be called from any thread. Returns true on success.
102 bool WriteMessage(scoped_ptr<MessageInTransit> message); 117 bool WriteMessage(scoped_ptr<MessageInTransit> message);
103 118
104 // Returns true if the write buffer is empty (i.e., all messages written using 119 // Returns true if the write buffer is empty (i.e., all messages written using
105 // |WriteMessage()| have actually been sent. 120 // |WriteMessage()| have actually been sent.
106 // TODO(vtl): We should really also notify our delegate when the write buffer 121 // TODO(vtl): We should really also notify our delegate when the write buffer
107 // becomes empty (or something like that). 122 // becomes empty (or something like that).
108 bool IsWriteBufferEmpty(); 123 bool IsWriteBufferEmpty();
109 124
110 // Returns the amount of space needed in the |MessageInTransit|'s 125 bool IsReadBufferEmpty();
111 // |TransportData|'s "platform handle table" per platform handle (to be 126
112 // attached to a message). (This amount may be zero.) 127 void SetInitialReadBufferData(char* data, size_t size);
113 virtual size_t GetSerializedPlatformHandleSize() const = 0; 128
129 // Checks if this RawChannel is the other endpoint to |other|.
130 bool IsOtherEndOf(RawChannel* other);
114 131
115 protected: 132 protected:
116 // Result of I/O operations. 133 // Result of I/O operations.
117 enum IOResult { 134 enum IOResult {
118 IO_SUCCEEDED, 135 IO_SUCCEEDED,
119 // Failed due to a (probably) clean shutdown (e.g., of the other end). 136 // Failed due to a (probably) clean shutdown (e.g., of the other end).
120 IO_FAILED_SHUTDOWN, 137 IO_FAILED_SHUTDOWN,
121 // Failed due to the connection being broken (e.g., the other end dying). 138 // Failed due to the connection being broken (e.g., the other end dying).
122 IO_FAILED_BROKEN, 139 IO_FAILED_BROKEN,
123 // Failed due to some other (unexpected) reason. 140 // Failed due to some other (unexpected) reason.
124 IO_FAILED_UNKNOWN, 141 IO_FAILED_UNKNOWN,
125 IO_PENDING 142 IO_PENDING
126 }; 143 };
127 144
128 class MOJO_SYSTEM_IMPL_EXPORT ReadBuffer { 145 class MOJO_SYSTEM_IMPL_EXPORT ReadBuffer {
129 public: 146 public:
130 ReadBuffer(); 147 ReadBuffer();
131 ~ReadBuffer(); 148 ~ReadBuffer();
132 149
133 void GetBuffer(char** addr, size_t* size); 150 void GetBuffer(char** addr, size_t* size);
134 151
152 void Reset() {num_valid_bytes_ = 0; }
153
154 // temp for debugging
155 // TODO(jam): pass in a cleaner way to ReleaseHandle, just like shutdown
156 // case.
157 char* buffer() { return &buffer_[0]; }
158 size_t num_valid_bytes() {return num_valid_bytes_;}
159
135 private: 160 private:
136 friend class RawChannel; 161 friend class RawChannel;
137 162
138 // We store data from |[Schedule]Read()|s in |buffer_|. The start of 163 // We store data from |[Schedule]Read()|s in |buffer_|. The start of
139 // |buffer_| is always aligned with a message boundary (we will copy memory 164 // |buffer_| is always aligned with a message boundary (we will copy memory
140 // to ensure this), but |buffer_| may be larger than the actual number of 165 // to ensure this), but |buffer_| may be larger than the actual number of
141 // bytes we have. 166 // bytes we have.
142 std::vector<char> buffer_; 167 std::vector<char> buffer_;
143 size_t num_valid_bytes_; 168 size_t num_valid_bytes_;
144 169
145 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadBuffer); 170 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadBuffer);
146 }; 171 };
147 172
148 class MOJO_SYSTEM_IMPL_EXPORT WriteBuffer { 173 class MOJO_SYSTEM_IMPL_EXPORT WriteBuffer {
149 public: 174 public:
150 struct Buffer { 175 struct Buffer {
151 const char* addr; 176 const char* addr;
152 size_t size; 177 size_t size;
153 }; 178 };
154 179
155 explicit WriteBuffer(size_t serialized_platform_handle_size); 180 WriteBuffer();
156 ~WriteBuffer(); 181 ~WriteBuffer();
157 182
158 // Returns true if there are (more) platform handles to be sent (from the 183 // Returns true if there are (more) platform handles to be sent (from the
159 // front of |message_queue_|). 184 // front of |message_queue_|).
160 bool HavePlatformHandlesToSend() const; 185 bool HavePlatformHandlesToSend() const;
161 // Gets platform handles to be sent (from the front of |message_queue_|). 186 // Gets platform handles to be sent (from the front of |message_queue_|).
162 // This should only be called if |HavePlatformHandlesToSend()| returned 187 // This should only be called if |HavePlatformHandlesToSend()| returned
163 // true. There are two components to this: the actual |PlatformHandle|s 188 // true. There are two components to this: the actual |PlatformHandle|s
164 // (which should be closed once sent) and any additional serialization 189 // (which should be closed once sent) and any additional serialization
165 // information (which will be embedded in the message's data; there are 190 // information (which will be embedded in the message's data; there are
166 // |GetSerializedPlatformHandleSize()| bytes per handle). Once all platform 191 // |GetSerializedPlatformHandleSize()| bytes per handle). Once all platform
167 // handles have been sent, the message data should be written next (see 192 // handles have been sent, the message data should be written next (see
168 // |GetBuffers()|). 193 // |GetBuffers()|).
169 // TODO(vtl): Maybe this method should be const, but 194 // TODO(vtl): Maybe this method should be const, but
170 // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies 195 // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies
171 // state). 196 // state).
172 void GetPlatformHandlesToSend(size_t* num_platform_handles, 197 void GetPlatformHandlesToSend(size_t* num_platform_handles,
173 embedder::PlatformHandle** platform_handles, 198 PlatformHandle** platform_handles,
174 void** serialization_data); 199 void** serialization_data);
175 200
176 // Gets buffers to be written. These buffers will always come from the front 201 // Gets buffers to be written. These buffers will always come from the front
177 // of |message_queue_|. Once they are completely written, the front 202 // of |message_queue_|. Once they are completely written, the front
178 // |MessageInTransit| should be popped (and destroyed); this is done in 203 // |MessageInTransit| should be popped (and destroyed); this is done in
179 // |OnWriteCompletedNoLock()|. 204 // |OnWriteCompletedNoLock()|.
180 void GetBuffers(std::vector<Buffer>* buffers) const; 205 void GetBuffers(std::vector<Buffer>* buffers) const;
181 206
182 private: 207
208
209
210
211 // temp for testing
212 size_t queue_size() {return message_queue_.Size();}
213
214 // TODO(jam): better way of giving buffer on release handle
215 MessageInTransitQueue* message_queue() { return &message_queue_; }
216
217
218
219 // TODO JAM REMOVE AND ADD METHODS
220 // private:
183 friend class RawChannel; 221 friend class RawChannel;
184 222
185 const size_t serialized_platform_handle_size_; 223 size_t serialized_platform_handle_size_;
186 224
187 MessageInTransitQueue message_queue_; 225 MessageInTransitQueue message_queue_;
188 // Platform handles are sent before the message data, but doing so may 226 // Platform handles are sent before the message data, but doing so may
189 // require several passes. |platform_handles_offset_| indicates the position 227 // require several passes. |platform_handles_offset_| indicates the position
190 // in the first message's vector of platform handles to send next. 228 // in the first message's vector of platform handles to send next.
191 size_t platform_handles_offset_; 229 size_t platform_handles_offset_;
192 // The first message's data may have been partially sent. |data_offset_| 230 // 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 231 // indicates the position in the first message's data to start the next
194 // write. 232 // write.
195 size_t data_offset_; 233 size_t data_offset_;
196 234
197 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteBuffer); 235 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteBuffer);
198 }; 236 };
199 237
200 RawChannel(); 238 RawChannel();
201 239
240 // Shutdown must be called on the IO thread. This object deletes itself once
241 // it's flushed all pending writes and insured that the other side of the pipe
242 // read them.
243 virtual ~RawChannel();
244
202 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT 245 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT
203 // |write_lock_| held. This object may be destroyed by this call. 246 // |write_lock_| held. This object may be destroyed by this call.
204 void OnReadCompleted(IOResult io_result, size_t bytes_read); 247 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 248 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT
206 // |write_lock_| held. This object may be destroyed by this call. 249 // |write_lock_| held. This object may be destroyed by this call.
207 void OnWriteCompleted(IOResult io_result, 250 void OnWriteCompleted(IOResult io_result,
208 size_t platform_handles_written, 251 size_t platform_handles_written,
209 size_t bytes_written); 252 size_t bytes_written);
210 253
211 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } 254 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; }
212 base::Lock& write_lock() { return write_lock_; } 255 base::Lock& write_lock() { return write_lock_; }
256 base::Lock& read_lock() { return read_lock_; }
213 257
214 // Should only be called on the I/O thread. 258 // Should only be called on the I/O thread.
215 ReadBuffer* read_buffer() { return read_buffer_.get(); } 259 ReadBuffer* read_buffer() { return read_buffer_.get(); }
216 260
217 // Only called under |write_lock_|. 261 // Only called under |write_lock_|.
218 WriteBuffer* write_buffer_no_lock() { 262 WriteBuffer* write_buffer_no_lock() {
219 write_lock_.AssertAcquired(); 263 write_lock_.AssertAcquired();
220 return write_buffer_.get(); 264 return write_buffer_.get();
221 } 265 }
222 266
223 // Adds |message| to the write message queue. Implementation subclasses may 267 // Adds |message| to the write message queue. Implementation subclasses may
224 // override this to add any additional "control" messages needed. This is 268 // override this to add any additional "control" messages needed. This is
225 // called (on any thread) with |write_lock_| held. 269 // called (on any thread) with |write_lock_| held.
226 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message); 270 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message);
227 271
228 // Handles any control messages targeted to the |RawChannel| (or 272 // Handles any control messages targeted to the |RawChannel| (or
229 // implementation subclass). Implementation subclasses may override this to 273 // implementation subclass). Implementation subclasses may override this to
230 // handle any implementation-specific control messages, but should call 274 // handle any implementation-specific control messages, but should call
231 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages. 275 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages.
232 // Returns true on success and false on error (e.g., invalid control message). 276 // Returns true on success and false on error (e.g., invalid control message).
233 // This is only called on the I/O thread. 277 // This is only called on the I/O thread.
234 virtual bool OnReadMessageForRawChannel( 278 virtual bool OnReadMessageForRawChannel(
235 const MessageInTransit::View& message_view); 279 const MessageInTransit::View& message_view);
236 280
281 virtual PlatformHandle HandleForDebuggingNoLock() = 0;
282
283 // Implementation must write any pending messages synchronously.
284 // TODO(jam): change to return shared memory with pending serialized msgs.
285 virtual ScopedPlatformHandle ReleaseHandleNoLock(
286 std::vector<char>* read_buffer) = 0;
287
237 // Reads into |read_buffer()|. 288 // Reads into |read_buffer()|.
238 // This class guarantees that: 289 // This class guarantees that:
239 // - the area indicated by |GetBuffer()| will stay valid until read completion 290 // - the area indicated by |GetBuffer()| will stay valid until read completion
240 // (but please also see the comments for |OnShutdownNoLock()|); 291 // (but please also see the comments for |OnShutdownNoLock()|);
241 // - a second read is not started if there is a pending read; 292 // - 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. 293 // - the method is called on the I/O thread WITHOUT |write_lock_| held.
243 // 294 //
244 // The implementing subclass must guarantee that: 295 // The implementing subclass must guarantee that:
245 // - |bytes_read| is untouched unless |Read()| returns |IO_SUCCEEDED|; 296 // - |bytes_read| is untouched unless |Read()| returns |IO_SUCCEEDED|;
246 // - if the method returns |IO_PENDING|, |OnReadCompleted()| will be called on 297 // - if the method returns |IO_PENDING|, |OnReadCompleted()| will be called on
247 // the I/O thread to report the result, unless |Shutdown()| is called. 298 // the I/O thread to report the result, unless |Shutdown()| is called.
248 virtual IOResult Read(size_t* bytes_read) = 0; 299 virtual IOResult Read(size_t* bytes_read) = 0;
249 // Similar to |Read()|, except that the implementing subclass must also 300 // Similar to |Read()|, except that the implementing subclass must also
250 // guarantee that the method doesn't succeed synchronously, i.e., it only 301 // guarantee that the method doesn't succeed synchronously, i.e., it only
251 // returns |IO_FAILED_...| or |IO_PENDING|. 302 // returns |IO_FAILED_...| or |IO_PENDING|.
252 virtual IOResult ScheduleRead() = 0; 303 virtual IOResult ScheduleRead() = 0;
253 304
254 // Called by |OnReadCompleted()| to get the platform handles associated with 305 // Called by |OnReadCompleted()| to get the platform handles associated with
255 // the given platform handle table (from a message). This should only be 306 // the given platform handle table (from a message). This should only be
256 // called when |num_platform_handles| is nonzero. Returns null if the 307 // 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 308 // |num_platform_handles| handles are not available. Only called on the I/O
258 // thread (without |write_lock_| held). 309 // thread (without |write_lock_| held).
259 virtual embedder::ScopedPlatformHandleVectorPtr GetReadPlatformHandles( 310 virtual ScopedPlatformHandleVectorPtr GetReadPlatformHandles(
260 size_t num_platform_handles, 311 size_t num_platform_handles,
261 const void* platform_handle_table) = 0; 312 const void* platform_handle_table) = 0;
262 313
263 // Writes contents in |write_buffer_no_lock()|. 314 // Writes contents in |write_buffer_no_lock()|.
264 // This class guarantees that: 315 // This class guarantees that:
265 // - the |PlatformHandle|s given by |GetPlatformHandlesToSend()| and the 316 // - the |PlatformHandle|s given by |GetPlatformHandlesToSend()| and the
266 // buffer(s) given by |GetBuffers()| will remain valid until write 317 // buffer(s) given by |GetBuffers()| will remain valid until write
267 // completion (see also the comments for |OnShutdownNoLock()|); 318 // completion (see also the comments for |OnShutdownNoLock()|);
268 // - a second write is not started if there is a pending write; 319 // - a second write is not started if there is a pending write;
269 // - the method is called under |write_lock_|. 320 // - the method is called under |write_lock_|.
(...skipping 12 matching lines...) Expand all
282 333
283 // Must be called on the I/O thread WITHOUT |write_lock_| held. 334 // Must be called on the I/O thread WITHOUT |write_lock_| held.
284 virtual void OnInit() = 0; 335 virtual void OnInit() = 0;
285 // On shutdown, passes the ownership of the buffers to subclasses, which may 336 // 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 337 // 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 338 // called, |OnReadCompleted()| must no longer be called. Must be called on the
288 // I/O thread under |write_lock_|. 339 // I/O thread under |write_lock_|.
289 virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer, 340 virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer,
290 scoped_ptr<WriteBuffer> write_buffer) = 0; 341 scoped_ptr<WriteBuffer> write_buffer) = 0;
291 342
343 void SendQueuedMessagesNoLock();
344
292 private: 345 private:
346 friend class base::DeleteHelper<RawChannel>;
347
293 // Converts an |IO_FAILED_...| for a read to a |Delegate::Error|. 348 // Converts an |IO_FAILED_...| for a read to a |Delegate::Error|.
294 static Delegate::Error ReadIOResultToError(IOResult io_result); 349 static Delegate::Error ReadIOResultToError(IOResult io_result);
295 350
296 // Calls |delegate_->OnError(error)|. Must be called on the I/O thread WITHOUT 351 // Calls |delegate_->OnError(error)|. Must be called on the I/O thread WITHOUT
297 // |write_lock_| held. This object may be destroyed by this call. 352 // |write_lock_| held. This object may be destroyed by this call.
298 void CallOnError(Delegate::Error error); 353 void CallOnError(Delegate::Error error);
299 354
355 void LockAndCallOnError(Delegate::Error error);
356
300 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a 357 // 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 358 // 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 359 // 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 360 // false. Must be called under |write_lock_| and only if |write_stopped_| is
304 // false. 361 // false.
305 bool OnWriteCompletedNoLock(IOResult io_result, 362 bool OnWriteCompletedNoLock(IOResult io_result,
306 size_t platform_handles_written, 363 size_t platform_handles_written,
307 size_t bytes_written); 364 size_t bytes_written);
308 365
366 // Helper method to dispatch messages from the read buffer.
367 // |did_dispatch_message| is true iff it dispatched any messages.
368 // |stop_dispatching| is set to true if the code calling this should stop
369 // dispatching, either because we hit an erorr or the delegate shutdown the
370 // channel.
371 void DispatchMessages(bool* did_dispatch_message, bool* stop_dispatching);
372
309 // Set in |Init()| and never changed (hence usable on any thread without 373 // Set in |Init()| and never changed (hence usable on any thread without
310 // locking): 374 // locking):
311 base::MessageLoopForIO* message_loop_for_io_; 375 base::MessageLoopForIO* message_loop_for_io_;
312 376
377
378
379
380
381 // TODO(jam): one lock only....
382
383
384
385
313 // Only used on the I/O thread: 386 // Only used on the I/O thread:
387
388 bool* set_on_shutdown_;
389
390 base::Lock read_lock_; // Protects read_buffer_.
391 // This is usually only accessed on IO thread, except when ReleaseHandle is
392 // called.
393 scoped_ptr<ReadBuffer> read_buffer_;
394 // ditto: usually used on io thread except ReleaseHandle
314 Delegate* delegate_; 395 Delegate* delegate_;
315 bool* set_on_shutdown_; 396
316 scoped_ptr<ReadBuffer> read_buffer_; 397 // If grabbing both locks, grab read first.
317 398
318 base::Lock write_lock_; // Protects the following members. 399 base::Lock write_lock_; // Protects the following members.
400 bool write_ready_;
319 bool write_stopped_; 401 bool write_stopped_;
320 scoped_ptr<WriteBuffer> write_buffer_; 402 scoped_ptr<WriteBuffer> write_buffer_;
321 403
404 bool error_occurred_;
405
322 // This is used for posting tasks from write threads to the I/O thread. It 406 // This is used for posting tasks from write threads to the I/O thread. It
323 // must only be accessed under |write_lock_|. The weak pointers it produces 407 // must only be accessed under |write_lock_|. The weak pointers it produces
324 // are only used/invalidated on the I/O thread. 408 // are only used/invalidated on the I/O thread.
325 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; 409 base::WeakPtrFactory<RawChannel> weak_ptr_factory_;
326 410
327 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); 411 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel);
328 }; 412 };
329 413
330 } // namespace system 414 } // namespace edk
331 } // namespace mojo 415 } // namespace mojo
332 416
333 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ 417 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698