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" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" |
12 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
13 #include "mojo/edk/embedder/platform_handle_vector.h" | 14 #include "mojo/edk/embedder/platform_handle_vector.h" |
14 #include "mojo/edk/embedder/scoped_platform_handle.h" | 15 #include "mojo/edk/embedder/scoped_platform_handle.h" |
15 #include "mojo/edk/system/message_in_transit.h" | 16 #include "mojo/edk/system/message_in_transit.h" |
16 #include "mojo/edk/system/message_in_transit_queue.h" | 17 #include "mojo/edk/system/message_in_transit_queue.h" |
17 #include "mojo/edk/system/system_impl_export.h" | 18 #include "mojo/edk/system/system_impl_export.h" |
18 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
19 | 20 |
20 namespace mojo { | 21 namespace mojo { |
21 namespace edk { | 22 namespace edk { |
22 | 23 |
23 // |RawChannel| is an interface and base class for objects that wrap an OS | 24 // |RawChannel| is an interface and base class for objects that wrap an OS |
24 // "pipe". It presents the following interface to users: | 25 // "pipe". It presents the following interface to users: |
25 // - Receives and dispatches messages on an I/O thread (running a | 26 // - Receives and dispatches messages on an I/O thread (running a |
26 // |MessageLoopForIO|. | 27 // |MessageLoopForIO|. |
27 // - Provides a thread-safe way of writing messages (|WriteMessage()|); | 28 // - Provides a thread-safe way of writing messages (|WriteMessage()|); |
28 // writing/queueing messages will not block and is atomic from the point of | 29 // writing/queueing messages will not block and is atomic from the point of |
29 // view of the caller. If necessary, messages are queued (to be written on | 30 // view of the caller. If necessary, messages are queued (to be written on |
30 // the aforementioned thread). | 31 // the aforementioned thread). |
31 // | 32 // |
32 // OS-specific implementation subclasses are to be instantiated using the | 33 // OS-specific implementation subclasses are to be instantiated using the |
33 // |Create()| static factory method. | 34 // |Create()| static factory method. |
34 // | 35 // |
35 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in | 36 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in |
36 // general its methods should only be used on the I/O thread, i.e., the thread | 37 // general its methods should only be used on the I/O thread, i.e., the thread |
37 // on which |Init()| is called). | 38 // on which |Init()| is called). |
38 class MOJO_SYSTEM_IMPL_EXPORT RawChannel { | 39 class MOJO_SYSTEM_IMPL_EXPORT RawChannel : |
| 40 public base::MessageLoop::DestructionObserver { |
39 public: | 41 public: |
40 | 42 |
41 // The |Delegate| is only accessed on the same thread as the message loop | 43 // The |Delegate| is only accessed on the same thread as the message loop |
42 // (passed in on creation). | 44 // (passed in on creation). |
43 class MOJO_SYSTEM_IMPL_EXPORT Delegate { | 45 class MOJO_SYSTEM_IMPL_EXPORT Delegate { |
44 public: | 46 public: |
45 enum Error { | 47 enum Error { |
46 // Failed read due to raw channel shutdown (e.g., on the other side). | 48 // Failed read due to raw channel shutdown (e.g., on the other side). |
47 ERROR_READ_SHUTDOWN, | 49 ERROR_READ_SHUTDOWN, |
48 // Failed read due to raw channel being broken (e.g., if the other side | 50 // Failed read due to raw channel being broken (e.g., if the other side |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 size_t data_offset_; | 234 size_t data_offset_; |
233 | 235 |
234 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteBuffer); | 236 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteBuffer); |
235 }; | 237 }; |
236 | 238 |
237 RawChannel(); | 239 RawChannel(); |
238 | 240 |
239 // Shutdown must be called on the IO thread. This object deletes itself once | 241 // Shutdown must be called on the IO thread. This object deletes itself once |
240 // it's flushed all pending writes and insured that the other side of the pipe | 242 // it's flushed all pending writes and insured that the other side of the pipe |
241 // read them. | 243 // read them. |
242 virtual ~RawChannel(); | 244 ~RawChannel() override; |
243 | 245 |
244 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 246 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
245 // |write_lock_| held. This object may be destroyed by this call. This | 247 // |write_lock_| held. This object may be destroyed by this call. This |
246 // acquires |read_lock_| inside of it. The caller needs to acquire read_lock_ | 248 // acquires |write_lock_| inside of it. The caller needs to acquire read_lock_ |
247 // first. | 249 // first. |
248 void OnReadCompletedNoLock(IOResult io_result, size_t bytes_read); | 250 void OnReadCompletedNoLock(IOResult io_result, size_t bytes_read); |
249 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT | 251 // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT |
250 // |write_lock_| held. This object may be destroyed by this call. The caller | 252 // |write_lock_| held. This object may be destroyed by this call. The caller |
251 // needs to acquire write_lock_ first. | 253 // needs to acquire write_lock_ first. |
252 void OnWriteCompletedNoLock(IOResult io_result, | 254 void OnWriteCompletedNoLock(IOResult io_result, |
253 size_t platform_handles_written, | 255 size_t platform_handles_written, |
254 size_t bytes_written); | 256 size_t bytes_written); |
255 | 257 |
256 // Serialize the read buffer into the given array so that it can be sent to | 258 // Serialize the read buffer into the given array so that it can be sent to |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 | 409 |
408 // Acquires read_lock_ and calls OnReadCompletedNoLock. | 410 // Acquires read_lock_ and calls OnReadCompletedNoLock. |
409 void CallOnReadCompleted(IOResult io_result, size_t bytes_read); | 411 void CallOnReadCompleted(IOResult io_result, size_t bytes_read); |
410 | 412 |
411 // Used with PostTask to acquire both locks and call LazyInitialize. | 413 // Used with PostTask to acquire both locks and call LazyInitialize. |
412 void LockAndCallLazyInitialize(); | 414 void LockAndCallLazyInitialize(); |
413 | 415 |
414 // Connects to the OS pipe. | 416 // Connects to the OS pipe. |
415 void LazyInitialize(); | 417 void LazyInitialize(); |
416 | 418 |
| 419 // base::MessageLoop::DestructionObserver: |
| 420 void WillDestroyCurrentMessageLoop() override; |
| 421 |
417 | 422 |
418 | 423 |
419 | 424 |
420 | 425 |
421 // TODO(jam): one lock only... but profile first to ensure it doesn't slow | 426 // TODO(jam): one lock only... but profile first to ensure it doesn't slow |
422 // things down compared to fine grained locks. | 427 // things down compared to fine grained locks. |
423 | 428 |
424 | 429 |
425 | 430 |
426 | 431 |
(...skipping 26 matching lines...) Expand all Loading... |
453 // are only used/invalidated on the I/O thread. | 458 // are only used/invalidated on the I/O thread. |
454 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; | 459 base::WeakPtrFactory<RawChannel> weak_ptr_factory_; |
455 | 460 |
456 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); | 461 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); |
457 }; | 462 }; |
458 | 463 |
459 } // namespace edk | 464 } // namespace edk |
460 } // namespace mojo | 465 } // namespace mojo |
461 | 466 |
462 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 467 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
OLD | NEW |