| 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_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" |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 // TODO(vtl): Maybe this method should be const, but | 185 // TODO(vtl): Maybe this method should be const, but |
| 186 // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies | 186 // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies |
| 187 // state). | 187 // state). |
| 188 void GetPlatformHandlesToSend(size_t* num_platform_handles, | 188 void GetPlatformHandlesToSend(size_t* num_platform_handles, |
| 189 PlatformHandle** platform_handles, | 189 PlatformHandle** platform_handles, |
| 190 void** serialization_data); | 190 void** serialization_data); |
| 191 | 191 |
| 192 // Gets buffers to be written. These buffers will always come from the front | 192 // Gets buffers to be written. These buffers will always come from the front |
| 193 // of |message_queue_|. Once they are completely written, the front | 193 // of |message_queue_|. Once they are completely written, the front |
| 194 // |MessageInTransit| should be popped (and destroyed); this is done in | 194 // |MessageInTransit| should be popped (and destroyed); this is done in |
| 195 // |OnWriteCompletedNoLock()|. | 195 // |OnWriteCompletedInternalNoLock()|. |
| 196 void GetBuffers(std::vector<Buffer>* buffers); | 196 void GetBuffers(std::vector<Buffer>* buffers); |
| 197 | 197 |
| 198 bool IsEmpty() const { return message_queue_.IsEmpty(); } | 198 bool IsEmpty() const { return message_queue_.IsEmpty(); } |
| 199 | 199 |
| 200 private: | 200 private: |
| 201 friend class RawChannel; | 201 friend class RawChannel; |
| 202 | 202 |
| 203 size_t serialized_platform_handle_size_; | 203 size_t serialized_platform_handle_size_; |
| 204 | 204 |
| 205 MessageInTransitQueue message_queue_; | 205 MessageInTransitQueue message_queue_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 216 }; | 216 }; |
| 217 | 217 |
| 218 RawChannel(); | 218 RawChannel(); |
| 219 | 219 |
| 220 // Shutdown must be called on the IO thread. This object deletes itself once | 220 // Shutdown must be called on the IO thread. This object deletes itself once |
| 221 // it's flushed all pending writes and insured that the other side of the pipe | 221 // it's flushed all pending writes and insured that the other side of the pipe |
| 222 // read them. | 222 // read them. |
| 223 virtual ~RawChannel(); | 223 virtual ~RawChannel(); |
| 224 | 224 |
| 225 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 225 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
| 226 // |write_lock_| held. This object may be destroyed by this call. | 226 // |write_lock_| held. This object may be destroyed by this call. This |
| 227 void OnReadCompleted(IOResult io_result, size_t bytes_read); | 227 // acquires |read_lock_| inside of it. The caller needs to acquire read_lock_ |
| 228 // first. |
| 229 void OnReadCompletedNoLock(IOResult io_result, size_t bytes_read); |
| 228 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 230 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
| 229 // |write_lock_| held. This object may be destroyed by this call. | 231 // |write_lock_| held. This object may be destroyed by this call. The caller |
| 230 void OnWriteCompleted(IOResult io_result, | 232 // needs to acquire write_lock_ first. |
| 231 size_t platform_handles_written, | 233 void OnWriteCompletedNoLock(IOResult io_result, |
| 232 size_t bytes_written); | 234 size_t platform_handles_written, |
| 235 size_t bytes_written); |
| 233 | 236 |
| 234 // Serialize the read buffer into the given array so that it can be sent to | 237 // Serialize the read buffer into the given array so that it can be sent to |
| 235 // another process. Increments |num_valid_bytes_| by |additional_bytes_read| | 238 // another process. Increments |num_valid_bytes_| by |additional_bytes_read| |
| 236 // before serialization.. | 239 // before serialization.. |
| 237 void SerializeReadBuffer(size_t additional_bytes_read, | 240 void SerializeReadBuffer(size_t additional_bytes_read, |
| 238 std::vector<char>* buffer); | 241 std::vector<char>* buffer); |
| 239 | 242 |
| 240 // Serialize the pending messages to be written to the OS pipe to the given | 243 // Serialize the pending messages to be written to the OS pipe to the given |
| 241 // buffer so that it can be sent to another process. | 244 // buffer so that it can be sent to another process. |
| 242 void SerializeWriteBuffer(std::vector<char>* buffer, | 245 void SerializeWriteBuffer(size_t additional_bytes_written, |
| 243 size_t additional_bytes_written, | 246 size_t additional_platform_handles_written, |
| 244 size_t additional_platform_handles_written); | 247 std::vector<char>* buffer); |
| 245 | 248 |
| 246 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } | 249 base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; } |
| 247 base::Lock& write_lock() { return write_lock_; } | 250 base::Lock& write_lock() { return write_lock_; } |
| 248 base::Lock& read_lock() { return read_lock_; } | 251 base::Lock& read_lock() { return read_lock_; } |
| 249 | 252 |
| 250 // Should only be called on the I/O thread. | 253 // Should only be called on the I/O thread. |
| 251 ReadBuffer* read_buffer() { return read_buffer_.get(); } | 254 ReadBuffer* read_buffer() { return read_buffer_.get(); } |
| 252 | 255 |
| 253 // Only called under |write_lock_|. | 256 // Only called under |write_lock_|. |
| 254 WriteBuffer* write_buffer_no_lock() { | 257 WriteBuffer* write_buffer_no_lock() { |
| 255 write_lock_.AssertAcquired(); | 258 write_lock_.AssertAcquired(); |
| 256 return write_buffer_.get(); | 259 return write_buffer_.get(); |
| 257 } | 260 } |
| 258 | 261 |
| 262 bool pending_error() { return pending_error_; } |
| 263 |
| 259 // Adds |message| to the write message queue. Implementation subclasses may | 264 // Adds |message| to the write message queue. Implementation subclasses may |
| 260 // override this to add any additional "control" messages needed. This is | 265 // override this to add any additional "control" messages needed. This is |
| 261 // called (on any thread) with |write_lock_| held. | 266 // called (on any thread) with |write_lock_| held. |
| 262 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message); | 267 virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message); |
| 263 | 268 |
| 264 // Handles any control messages targeted to the |RawChannel| (or | 269 // Handles any control messages targeted to the |RawChannel| (or |
| 265 // implementation subclass). Implementation subclasses may override this to | 270 // implementation subclass). Implementation subclasses may override this to |
| 266 // handle any implementation-specific control messages, but should call | 271 // handle any implementation-specific control messages, but should call |
| 267 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages. | 272 // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages. |
| 268 // Returns true on success and false on error (e.g., invalid control message). | 273 // Returns true on success and false on error (e.g., invalid control message). |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 // |write_lock_| held. This object may be destroyed by this call. | 359 // |write_lock_| held. This object may be destroyed by this call. |
| 355 void CallOnError(Delegate::Error error); | 360 void CallOnError(Delegate::Error error); |
| 356 | 361 |
| 357 void LockAndCallOnError(Delegate::Error error); | 362 void LockAndCallOnError(Delegate::Error error); |
| 358 | 363 |
| 359 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a | 364 // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a |
| 360 // write operation to run later if there is more to write. If |io_result| is | 365 // write operation to run later if there is more to write. If |io_result| is |
| 361 // failure or any other error occurs, cancels pending writes and returns | 366 // failure or any other error occurs, cancels pending writes and returns |
| 362 // false. Must be called under |write_lock_| and only if |write_stopped_| is | 367 // false. Must be called under |write_lock_| and only if |write_stopped_| is |
| 363 // false. | 368 // false. |
| 364 bool OnWriteCompletedNoLock(IOResult io_result, | 369 bool OnWriteCompletedInternalNoLock(IOResult io_result, |
| 365 size_t platform_handles_written, | 370 size_t platform_handles_written, |
| 366 size_t bytes_written); | 371 size_t bytes_written); |
| 367 | 372 |
| 368 // Helper method to dispatch messages from the read buffer. | 373 // Helper method to dispatch messages from the read buffer. |
| 369 // |did_dispatch_message| is true iff it dispatched any messages. | 374 // |did_dispatch_message| is true iff it dispatched any messages. |
| 370 // |stop_dispatching| is set to true if the code calling this should stop | 375 // |stop_dispatching| is set to true if the code calling this should stop |
| 371 // dispatching, either because we hit an erorr or the delegate shutdown the | 376 // dispatching, either because we hit an erorr or the delegate shutdown the |
| 372 // channel. | 377 // channel. |
| 373 void DispatchMessages(bool* did_dispatch_message, bool* stop_dispatching); | 378 void DispatchMessages(bool* did_dispatch_message, bool* stop_dispatching); |
| 374 | 379 |
| 375 void UpdateWriteBuffer(size_t platform_handles_written, size_t bytes_written); | 380 void UpdateWriteBuffer(size_t platform_handles_written, size_t bytes_written); |
| 376 | 381 |
| 382 // Acquires read_lock_ and calls OnReadCompletedNoLock. |
| 383 void CallOnReadCompleted(IOResult io_result, size_t bytes_read); |
| 384 |
| 377 // Set in |Init()| and never changed (hence usable on any thread without | 385 // Set in |Init()| and never changed (hence usable on any thread without |
| 378 // locking): | 386 // locking): |
| 379 base::MessageLoopForIO* message_loop_for_io_; | 387 base::MessageLoopForIO* message_loop_for_io_; |
| 380 | 388 |
| 381 | 389 |
| 382 | 390 |
| 383 | 391 |
| 384 | 392 |
| 385 // TODO(jam): one lock only... but profile first to ensure it doesn't slow | 393 // TODO(jam): one lock only... but profile first to ensure it doesn't slow |
| 386 // things down compared to fine grained locks. | 394 // things down compared to fine grained locks. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 399 | 407 |
| 400 // If grabbing both locks, grab read first. | 408 // If grabbing both locks, grab read first. |
| 401 | 409 |
| 402 base::Lock write_lock_; // Protects the following members. | 410 base::Lock write_lock_; // Protects the following members. |
| 403 bool write_ready_; | 411 bool write_ready_; |
| 404 bool write_stopped_; | 412 bool write_stopped_; |
| 405 scoped_ptr<WriteBuffer> write_buffer_; | 413 scoped_ptr<WriteBuffer> write_buffer_; |
| 406 | 414 |
| 407 bool error_occurred_; | 415 bool error_occurred_; |
| 408 | 416 |
| 417 // True iff a PostTask has been called to set an error. Can be written under |
| 418 // either read or write lock. It's read with both acquired. |
| 419 bool pending_error_; |
| 420 |
| 409 // This is used for posting tasks from write threads to the I/O thread. It | 421 // This is used for posting tasks from write threads to the I/O thread. It |
| 410 // must only be accessed under |write_lock_|. The weak pointers it produces | 422 // must only be accessed under |write_lock_|. The weak pointers it produces |
| 411 // are only used/invalidated on the I/O thread. | 423 // are only used/invalidated on the I/O thread. |
| 412 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | 424 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; |
| 413 | 425 |
| 414 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); | 426 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); |
| 415 }; | 427 }; |
| 416 | 428 |
| 417 } // namespace edk | 429 } // namespace edk |
| 418 } // namespace mojo | 430 } // namespace mojo |
| 419 | 431 |
| 420 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 432 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
| OLD | NEW |