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 <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "mojo/edk/embedder/platform_handle_vector.h" | 12 #include "mojo/edk/embedder/platform_handle_vector.h" |
13 #include "mojo/edk/embedder/scoped_platform_handle.h" | 13 #include "mojo/edk/embedder/scoped_platform_handle.h" |
14 #include "mojo/edk/system/message_in_transit.h" | 14 #include "mojo/edk/system/message_in_transit.h" |
15 #include "mojo/edk/system/message_in_transit_queue.h" | 15 #include "mojo/edk/system/message_in_transit_queue.h" |
16 #include "mojo/edk/system/mutex.h" | 16 #include "mojo/edk/system/mutex.h" |
17 #include "mojo/edk/system/system_impl_export.h" | |
18 #include "mojo/edk/system/thread_annotations.h" | 17 #include "mojo/edk/system/thread_annotations.h" |
19 #include "mojo/public/cpp/system/macros.h" | 18 #include "mojo/public/cpp/system/macros.h" |
20 | 19 |
21 namespace base { | 20 namespace base { |
22 class MessageLoopForIO; | 21 class MessageLoopForIO; |
23 } | 22 } |
24 | 23 |
25 namespace mojo { | 24 namespace mojo { |
26 namespace system { | 25 namespace system { |
27 | 26 |
28 // |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 |
29 // "pipe". It presents the following interface to users: | 28 // "pipe". It presents the following interface to users: |
30 // - Receives and dispatches messages on an I/O thread (running a | 29 // - Receives and dispatches messages on an I/O thread (running a |
31 // |MessageLoopForIO|. | 30 // |MessageLoopForIO|. |
32 // - Provides a thread-safe way of writing messages (|WriteMessage()|); | 31 // - Provides a thread-safe way of writing messages (|WriteMessage()|); |
33 // 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 |
34 // 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 |
35 // the aforementioned thread). | 34 // the aforementioned thread). |
36 // | 35 // |
37 // OS-specific implementation subclasses are to be instantiated using the | 36 // OS-specific implementation subclasses are to be instantiated using the |
38 // |Create()| static factory method. | 37 // |Create()| static factory method. |
39 // | 38 // |
40 // With the exception of |WriteMessage()| and |IsWriteBufferEmpty()|, this class | 39 // With the exception of |WriteMessage()| and |IsWriteBufferEmpty()|, this class |
41 // is thread-unsafe (and in general its methods should only be used on the I/O | 40 // is thread-unsafe (and in general its methods should only be used on the I/O |
42 // thread, i.e., the thread on which |Init()| is called). | 41 // thread, i.e., the thread on which |Init()| is called). |
43 class MOJO_SYSTEM_IMPL_EXPORT RawChannel { | 42 class RawChannel { |
44 public: | 43 public: |
45 // This object may be destroyed on any thread (if |Init()| was called, after | 44 // This object may be destroyed on any thread (if |Init()| was called, after |
46 // |Shutdown()| was called). | 45 // |Shutdown()| was called). |
47 virtual ~RawChannel(); | 46 virtual ~RawChannel(); |
48 | 47 |
49 // The |Delegate| is only accessed on the same thread as the message loop | 48 // The |Delegate| is only accessed on the same thread as the message loop |
50 // (passed in on creation). | 49 // (passed in on creation). |
51 class MOJO_SYSTEM_IMPL_EXPORT Delegate { | 50 class Delegate { |
52 public: | 51 public: |
53 enum Error { | 52 enum Error { |
54 // Failed read due to raw channel shutdown (e.g., on the other side). | 53 // Failed read due to raw channel shutdown (e.g., on the other side). |
55 ERROR_READ_SHUTDOWN, | 54 ERROR_READ_SHUTDOWN, |
56 // Failed read due to raw channel being broken (e.g., if the other side | 55 // Failed read due to raw channel being broken (e.g., if the other side |
57 // died without shutting down). | 56 // died without shutting down). |
58 ERROR_READ_BROKEN, | 57 ERROR_READ_BROKEN, |
59 // Received a bad message. | 58 // Received a bad message. |
60 ERROR_READ_BAD_MESSAGE, | 59 ERROR_READ_BAD_MESSAGE, |
61 // Unknown read error. | 60 // Unknown read error. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 IO_SUCCEEDED, | 119 IO_SUCCEEDED, |
121 // Failed due to a (probably) clean shutdown (e.g., of the other end). | 120 // Failed due to a (probably) clean shutdown (e.g., of the other end). |
122 IO_FAILED_SHUTDOWN, | 121 IO_FAILED_SHUTDOWN, |
123 // Failed due to the connection being broken (e.g., the other end dying). | 122 // Failed due to the connection being broken (e.g., the other end dying). |
124 IO_FAILED_BROKEN, | 123 IO_FAILED_BROKEN, |
125 // Failed due to some other (unexpected) reason. | 124 // Failed due to some other (unexpected) reason. |
126 IO_FAILED_UNKNOWN, | 125 IO_FAILED_UNKNOWN, |
127 IO_PENDING | 126 IO_PENDING |
128 }; | 127 }; |
129 | 128 |
130 class MOJO_SYSTEM_IMPL_EXPORT ReadBuffer { | 129 class ReadBuffer { |
131 public: | 130 public: |
132 ReadBuffer(); | 131 ReadBuffer(); |
133 ~ReadBuffer(); | 132 ~ReadBuffer(); |
134 | 133 |
135 void GetBuffer(char** addr, size_t* size); | 134 void GetBuffer(char** addr, size_t* size); |
136 | 135 |
137 private: | 136 private: |
138 friend class RawChannel; | 137 friend class RawChannel; |
139 | 138 |
140 // We store data from |[Schedule]Read()|s in |buffer_|. The start of | 139 // We store data from |[Schedule]Read()|s in |buffer_|. The start of |
141 // |buffer_| is always aligned with a message boundary (we will copy memory | 140 // |buffer_| is always aligned with a message boundary (we will copy memory |
142 // to ensure this), but |buffer_| may be larger than the actual number of | 141 // to ensure this), but |buffer_| may be larger than the actual number of |
143 // bytes we have. | 142 // bytes we have. |
144 std::vector<char> buffer_; | 143 std::vector<char> buffer_; |
145 size_t num_valid_bytes_; | 144 size_t num_valid_bytes_; |
146 | 145 |
147 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadBuffer); | 146 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadBuffer); |
148 }; | 147 }; |
149 | 148 |
150 class MOJO_SYSTEM_IMPL_EXPORT WriteBuffer { | 149 class WriteBuffer { |
151 public: | 150 public: |
152 struct Buffer { | 151 struct Buffer { |
153 const char* addr; | 152 const char* addr; |
154 size_t size; | 153 size_t size; |
155 }; | 154 }; |
156 | 155 |
157 explicit WriteBuffer(size_t serialized_platform_handle_size); | 156 explicit WriteBuffer(size_t serialized_platform_handle_size); |
158 ~WriteBuffer(); | 157 ~WriteBuffer(); |
159 | 158 |
160 // Returns true if there are (more) platform handles to be sent (from the | 159 // Returns true if there are (more) platform handles to be sent (from the |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 base::WeakPtrFactory<RawChannel> weak_ptr_factory_ | 328 base::WeakPtrFactory<RawChannel> weak_ptr_factory_ |
330 MOJO_GUARDED_BY(write_mutex_); | 329 MOJO_GUARDED_BY(write_mutex_); |
331 | 330 |
332 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); | 331 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); |
333 }; | 332 }; |
334 | 333 |
335 } // namespace system | 334 } // namespace system |
336 } // namespace mojo | 335 } // namespace mojo |
337 | 336 |
338 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 337 #endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ |
OLD | NEW |