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" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // Number of attached handles. May be less than the reserved handle | 59 // Number of attached handles. May be less than the reserved handle |
60 // storage size in this message on platforms that serialise handles as | 60 // storage size in this message on platforms that serialise handles as |
61 // data (i.e. HANDLEs on Windows, Mach ports on OSX). | 61 // data (i.e. HANDLEs on Windows, Mach ports on OSX). |
62 uint16_t num_handles; | 62 uint16_t num_handles; |
63 | 63 |
64 MessageType message_type; | 64 MessageType message_type; |
65 | 65 |
66 char padding[6]; | 66 char padding[6]; |
67 #endif // defined(OS_CHROMEOS) || defined(OS_ANDROID) | 67 #endif // defined(OS_CHROMEOS) || defined(OS_ANDROID) |
68 }; | 68 }; |
| 69 |
| 70 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 71 struct MachPortsEntry { |
| 72 uint16_t index; |
| 73 uint32_t mach_port; |
| 74 static_assert(sizeof(mach_port_t) <= sizeof(uint32_t), |
| 75 "mach_port_t must be no larger than uint32_t"); |
| 76 }; |
| 77 static_assert(sizeof(MachPortsEntry) == 6, |
| 78 "sizeof(MachPortsEntry) must be 6 bytes"); |
| 79 #endif |
69 #pragma pack(pop) | 80 #pragma pack(pop) |
70 | 81 |
71 // Allocates and owns a buffer for message data with enough capacity for | 82 // Allocates and owns a buffer for message data with enough capacity for |
72 // |payload_size| bytes plus a header, plus |max_handles| platform handles. | 83 // |payload_size| bytes plus a header, plus |max_handles| platform handles. |
73 Message(size_t payload_size, | 84 Message(size_t payload_size, |
74 size_t max_handles, | 85 size_t max_handles, |
75 Header::MessageType message_type = Header::MessageType::NORMAL); | 86 Header::MessageType message_type = Header::MessageType::NORMAL); |
76 | 87 |
77 ~Message(); | 88 ~Message(); |
78 | 89 |
(...skipping 25 matching lines...) Expand all Loading... |
104 bool has_handles() const { return header_->num_handles > 0; } | 115 bool has_handles() const { return header_->num_handles > 0; } |
105 PlatformHandle* handles(); | 116 PlatformHandle* handles(); |
106 #if defined(OS_MACOSX) && !defined(OS_IOS) | 117 #if defined(OS_MACOSX) && !defined(OS_IOS) |
107 bool has_mach_ports() const; | 118 bool has_mach_ports() const; |
108 #endif | 119 #endif |
109 | 120 |
110 // Note: SetHandles() and TakeHandles() invalidate any previous value of | 121 // Note: SetHandles() and TakeHandles() invalidate any previous value of |
111 // handles(). | 122 // handles(). |
112 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); | 123 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); |
113 ScopedPlatformHandleVectorPtr TakeHandles(); | 124 ScopedPlatformHandleVectorPtr TakeHandles(); |
| 125 // Version of TakeHandles that returns a vector of platform handles suitable |
| 126 // for transfer over an underlying OS mechanism. i.e. file descriptors over |
| 127 // a unix domain socket. Any handle that cannot be transferred this way, |
| 128 // such as Mach ports, will be removed. |
| 129 ScopedPlatformHandleVectorPtr TakeHandlesForTransport(); |
114 | 130 |
115 #if defined(OS_WIN) | 131 #if defined(OS_WIN) |
116 // Prepares the handles in this message for use in a different process. | 132 // Prepares the handles in this message for use in a different process. |
117 // Upon calling this the handles should belong to |from_process|; after the | 133 // Upon calling this the handles should belong to |from_process|; after the |
118 // call they'll belong to |to_process|. The source handles are always | 134 // call they'll belong to |to_process|. The source handles are always |
119 // closed by this call. Returns false iff one or more handles failed | 135 // closed by this call. Returns false iff one or more handles failed |
120 // duplication. | 136 // duplication. |
121 static bool RewriteHandles(base::ProcessHandle from_process, | 137 static bool RewriteHandles(base::ProcessHandle from_process, |
122 base::ProcessHandle to_process, | 138 base::ProcessHandle to_process, |
123 PlatformHandle* handles, | 139 PlatformHandle* handles, |
124 size_t num_handles); | 140 size_t num_handles); |
125 #endif | 141 #endif |
126 | 142 |
127 private: | 143 private: |
128 size_t size_; | 144 size_t size_; |
129 size_t max_handles_; | 145 size_t max_handles_; |
130 char* data_; | 146 char* data_; |
131 Header* header_; | 147 Header* header_; |
132 | 148 |
133 #if defined(OS_WIN) | 149 #if defined(OS_WIN) |
134 // On Windows, handles are serialized in the data buffer along with the | 150 // On Windows, handles are serialised into the extra header section. |
135 // rest of the payload. | |
136 PlatformHandle* handles_ = nullptr; | 151 PlatformHandle* handles_ = nullptr; |
137 #else | 152 #else |
138 ScopedPlatformHandleVectorPtr handle_vector_; | 153 ScopedPlatformHandleVectorPtr handle_vector_; |
139 #endif | 154 #endif |
140 | 155 |
| 156 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 157 // On OSX, handles are serialised into the extra header section. |
| 158 MachPortsEntry* mach_ports_ = nullptr; |
| 159 #endif |
| 160 |
141 DISALLOW_COPY_AND_ASSIGN(Message); | 161 DISALLOW_COPY_AND_ASSIGN(Message); |
142 }; | 162 }; |
143 | 163 |
144 // Delegate methods are called from the I/O task runner with which the Channel | 164 // Delegate methods are called from the I/O task runner with which the Channel |
145 // was created (see Channel::Create). | 165 // was created (see Channel::Create). |
146 class Delegate { | 166 class Delegate { |
147 public: | 167 public: |
148 virtual ~Delegate() {} | 168 virtual ~Delegate() {} |
149 | 169 |
150 // Notify of a received message. |payload| is not owned and must not be | 170 // Notify of a received message. |payload| is not owned and must not be |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 Delegate* delegate_; | 252 Delegate* delegate_; |
233 const scoped_ptr<ReadBuffer> read_buffer_; | 253 const scoped_ptr<ReadBuffer> read_buffer_; |
234 | 254 |
235 DISALLOW_COPY_AND_ASSIGN(Channel); | 255 DISALLOW_COPY_AND_ASSIGN(Channel); |
236 }; | 256 }; |
237 | 257 |
238 } // namespace edk | 258 } // namespace edk |
239 } // namespace mojo | 259 } // namespace mojo |
240 | 260 |
241 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 261 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
OLD | NEW |