OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_CHANNEL_H_ | 5 #ifndef MOJO_EDK_SYSTEM_CHANNEL_H_ |
6 #define MOJO_EDK_SYSTEM_CHANNEL_H_ | 6 #define MOJO_EDK_SYSTEM_CHANNEL_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/process/process_handle.h" | 11 #include "base/process/process_handle.h" |
12 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
| 13 #include "mojo/edk/embedder/connection_param.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 | 16 |
16 namespace mojo { | 17 namespace mojo { |
17 namespace edk { | 18 namespace edk { |
18 | 19 |
19 const size_t kChannelMessageAlignment = 8; | 20 const size_t kChannelMessageAlignment = 8; |
20 | 21 |
21 constexpr bool IsAlignedForChannelMessage(size_t n) { | 22 constexpr bool IsAlignedForChannelMessage(size_t n) { |
22 return n % kChannelMessageAlignment == 0; | 23 return n % kChannelMessageAlignment == 0; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 "mach_port_t must be no larger than uint32_t"); | 96 "mach_port_t must be no larger than uint32_t"); |
96 }; | 97 }; |
97 static_assert(sizeof(MachPortsEntry) == 6, | 98 static_assert(sizeof(MachPortsEntry) == 6, |
98 "sizeof(MachPortsEntry) must be 6 bytes"); | 99 "sizeof(MachPortsEntry) must be 6 bytes"); |
99 | 100 |
100 // Structure of the extra header field when present on OSX. | 101 // Structure of the extra header field when present on OSX. |
101 struct MachPortsExtraHeader { | 102 struct MachPortsExtraHeader { |
102 // Actual number of Mach ports encoded in the extra header. | 103 // Actual number of Mach ports encoded in the extra header. |
103 uint16_t num_ports; | 104 uint16_t num_ports; |
104 | 105 |
105 // Array of encoded Mach ports. If |num_ports| > 0, |entires[0]| through | 106 // Array of encoded Mach ports. If |num_ports| > 0, |entries[0]| through |
106 // to |entries[num_ports-1]| inclusive are valid. | 107 // to |entries[num_ports-1]| inclusive are valid. |
107 MachPortsEntry entries[0]; | 108 MachPortsEntry entries[0]; |
108 }; | 109 }; |
109 static_assert(sizeof(MachPortsExtraHeader) == 2, | 110 static_assert(sizeof(MachPortsExtraHeader) == 2, |
110 "sizeof(MachPortsExtraHeader) must be 2 bytes"); | 111 "sizeof(MachPortsExtraHeader) must be 2 bytes"); |
111 #elif defined(OS_WIN) | 112 #elif defined(OS_WIN) |
112 struct HandleEntry { | 113 struct HandleEntry { |
113 // The windows HANDLE. HANDLEs are guaranteed to fit inside 32-bits. | 114 // The windows HANDLE. HANDLEs are guaranteed to fit inside 32-bits. |
114 // See: https://msdn.microsoft.com/en-us/library/aa384203(VS.85).aspx | 115 // See: https://msdn.microsoft.com/en-us/library/aa384203(VS.85).aspx |
115 uint32_t handle; | 116 uint32_t handle; |
116 }; | 117 }; |
117 static_assert(sizeof(HandleEntry) == 4, | 118 static_assert(sizeof(HandleEntry) == 4, |
118 "sizeof(HandleEntry) must be 4 bytes"); | 119 "sizeof(HandleEntry) must be 4 bytes"); |
| 120 #elif defined(OS_ANDROID) |
| 121 struct ParcelableEntry { |
| 122 // Index of the parcelable in the original vector of PlatformHandles. |
| 123 uint16_t index; |
| 124 |
| 125 // The ID of a parcelable sent through the side channel. |
| 126 uint32_t id; |
| 127 }; |
| 128 static_assert(sizeof(ParcelableEntry) == 6, |
| 129 "sizeof(ParcelableEntry) must be 6 bytes"); |
| 130 |
| 131 // Structure of the extra header field when present on Android. |
| 132 struct ParcelableExtraHeader { |
| 133 // Actual number of parcelables encoded in the extra header. |
| 134 uint16_t num_parcelables; |
| 135 |
| 136 // Array of parcelable IDs. If |num_ports| > 0, |entries[0]| through |
| 137 // to |entries[num_ports-1]| inclusive are valid. |
| 138 ParcelableEntry entries[0]; |
| 139 }; |
| 140 static_assert(sizeof(ParcelableExtraHeader) == 2, |
| 141 "sizeof(ParcelableExtraHeader) must be 2 bytes"); |
119 #endif | 142 #endif |
120 #pragma pack(pop) | 143 #pragma pack(pop) |
121 | 144 |
122 // Allocates and owns a buffer for message data with enough capacity for | 145 // Allocates and owns a buffer for message data with enough capacity for |
123 // |payload_size| bytes plus a header, plus |max_handles| platform handles. | 146 // |payload_size| bytes plus a header, plus |max_handles| platform handles. |
124 Message(size_t payload_size, size_t max_handles); | 147 Message(size_t payload_size, size_t max_handles); |
125 Message(size_t payload_size, size_t max_handles, MessageType message_type); | 148 Message(size_t payload_size, size_t max_handles, MessageType message_type); |
126 ~Message(); | 149 ~Message(); |
127 | 150 |
128 // Constructs a Message from serialized message data. | 151 // Constructs a Message from serialized message data. |
(...skipping 23 matching lines...) Expand all Loading... |
152 // Note: SetHandles() and TakeHandles() invalidate any previous value of | 175 // Note: SetHandles() and TakeHandles() invalidate any previous value of |
153 // handles(). | 176 // handles(). |
154 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); | 177 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); |
155 ScopedPlatformHandleVectorPtr TakeHandles(); | 178 ScopedPlatformHandleVectorPtr TakeHandles(); |
156 // Version of TakeHandles that returns a vector of platform handles suitable | 179 // Version of TakeHandles that returns a vector of platform handles suitable |
157 // for transfer over an underlying OS mechanism. i.e. file descriptors over | 180 // for transfer over an underlying OS mechanism. i.e. file descriptors over |
158 // a unix domain socket. Any handle that cannot be transferred this way, | 181 // a unix domain socket. Any handle that cannot be transferred this way, |
159 // such as Mach ports, will be removed. | 182 // such as Mach ports, will be removed. |
160 ScopedPlatformHandleVectorPtr TakeHandlesForTransport(); | 183 ScopedPlatformHandleVectorPtr TakeHandlesForTransport(); |
161 | 184 |
| 185 #if defined(OS_ANDROID) |
| 186 using IDAndParcelableVector = std::vector< |
| 187 std::pair<uint32_t, base::android::ScopedJavaLocalRef<jobject>>>; |
| 188 IDAndParcelableVector TakeParcelablesForTransport(); |
| 189 #endif |
| 190 |
162 #if defined(OS_WIN) | 191 #if defined(OS_WIN) |
163 // Prepares the handles in this message for use in a different process. | 192 // Prepares the handles in this message for use in a different process. |
164 // Upon calling this the handles should belong to |from_process|; after the | 193 // Upon calling this the handles should belong to |from_process|; after the |
165 // call they'll belong to |to_process|. The source handles are always | 194 // call they'll belong to |to_process|. The source handles are always |
166 // closed by this call. Returns false iff one or more handles failed | 195 // closed by this call. Returns false iff one or more handles failed |
167 // duplication. | 196 // duplication. |
168 static bool RewriteHandles(base::ProcessHandle from_process, | 197 static bool RewriteHandles(base::ProcessHandle from_process, |
169 base::ProcessHandle to_process, | 198 base::ProcessHandle to_process, |
170 PlatformHandleVector* handles); | 199 PlatformHandleVector* handles); |
171 #endif | 200 #endif |
172 | 201 |
173 void SetVersionForTest(uint16_t version_number); | 202 void SetVersionForTest(uint16_t version_number); |
174 | 203 |
175 private: | 204 private: |
176 size_t size_ = 0; | 205 size_t size_ = 0; |
177 size_t max_handles_ = 0; | 206 size_t max_handles_ = 0; |
178 char* data_ = nullptr; | 207 char* data_ = nullptr; |
179 | 208 |
180 ScopedPlatformHandleVectorPtr handle_vector_; | 209 ScopedPlatformHandleVectorPtr handle_vector_; |
181 | 210 |
182 #if defined(OS_WIN) | 211 #if defined(OS_WIN) |
183 // On Windows, handles are serialised into the extra header section. | 212 // On Windows, handles are serialised into the extra header section. |
184 HandleEntry* handles_ = nullptr; | 213 HandleEntry* handles_ = nullptr; |
185 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 214 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
186 // On OSX, handles are serialised into the extra header section. | 215 // On OSX, handles are serialised into the extra header section. |
187 MachPortsExtraHeader* mach_ports_header_ = nullptr; | 216 MachPortsExtraHeader* mach_ports_header_ = nullptr; |
| 217 #elif defined(OS_ANDROID) |
| 218 // On Android, handles for parcelable are sent on a side channel, IDs are |
| 219 // sent on the mian channel. |
| 220 ParcelableExtraHeader* parcelable_header_ = nullptr; |
188 #endif | 221 #endif |
189 | 222 |
190 DISALLOW_COPY_AND_ASSIGN(Message); | 223 DISALLOW_COPY_AND_ASSIGN(Message); |
191 }; | 224 }; |
192 | 225 |
193 // Delegate methods are called from the I/O task runner with which the Channel | 226 // Delegate methods are called from the I/O task runner with which the Channel |
194 // was created (see Channel::Create). | 227 // was created (see Channel::Create). |
195 class Delegate { | 228 class Delegate { |
196 public: | 229 public: |
197 virtual ~Delegate() {} | 230 virtual ~Delegate() {} |
198 | 231 |
199 // Notify of a received message. |payload| is not owned and must not be | 232 // Notify of a received message. |payload| is not owned and must not be |
200 // retained; it will be null if |payload_size| is 0. |handles| are | 233 // retained; it will be null if |payload_size| is 0. |handles| are |
201 // transferred to the callee. | 234 // transferred to the callee. |
202 virtual void OnChannelMessage(const void* payload, | 235 virtual void OnChannelMessage(const void* payload, |
203 size_t payload_size, | 236 size_t payload_size, |
204 ScopedPlatformHandleVectorPtr handles) = 0; | 237 ScopedPlatformHandleVectorPtr handles) = 0; |
205 | 238 |
206 // Notify that an error has occured and the Channel will cease operation. | 239 // Notify that an error has occured and the Channel will cease operation. |
207 virtual void OnChannelError() = 0; | 240 virtual void OnChannelError() = 0; |
208 }; | 241 }; |
209 | 242 |
210 // Creates a new Channel around a |platform_handle|, taking ownership of the | 243 // Creates a new Channel around a |platform_handle|, taking ownership of the |
211 // handle. All I/O on the handle will be performed on |io_task_runner|. | 244 // handle. All I/O on the handle will be performed on |io_task_runner|. |
212 // Note that ShutDown() MUST be called on the Channel some time before | 245 // Note that ShutDown() MUST be called on the Channel some time before |
213 // |delegate| is destroyed. | 246 // |delegate| is destroyed. |
214 static scoped_refptr<Channel> Create( | 247 static scoped_refptr<Channel> Create( |
215 Delegate* delegate, | 248 Delegate* delegate, |
216 ScopedPlatformHandle platform_handle, | 249 ConnectionParam connection_param, |
217 scoped_refptr<base::TaskRunner> io_task_runner); | 250 scoped_refptr<base::TaskRunner> io_task_runner); |
218 | 251 |
219 // Request that the channel be shut down. This should always be called before | 252 // Request that the channel be shut down. This should always be called before |
220 // releasing the last reference to a Channel to ensure that it's cleaned up | 253 // releasing the last reference to a Channel to ensure that it's cleaned up |
221 // on its I/O task runner's thread. | 254 // on its I/O task runner's thread. |
222 // | 255 // |
223 // Delegate methods will no longer be invoked after this call. | 256 // Delegate methods will no longer be invoked after this call. |
224 void ShutDown(); | 257 void ShutDown(); |
225 | 258 |
226 // Begin processing I/O events. Delegate methods must only be invoked after | 259 // Begin processing I/O events. Delegate methods must only be invoked after |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 Delegate* delegate_; | 326 Delegate* delegate_; |
294 const std::unique_ptr<ReadBuffer> read_buffer_; | 327 const std::unique_ptr<ReadBuffer> read_buffer_; |
295 | 328 |
296 DISALLOW_COPY_AND_ASSIGN(Channel); | 329 DISALLOW_COPY_AND_ASSIGN(Channel); |
297 }; | 330 }; |
298 | 331 |
299 } // namespace edk | 332 } // namespace edk |
300 } // namespace mojo | 333 } // namespace mojo |
301 | 334 |
302 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 335 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
OLD | NEW |