| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 struct MachPortsExtraHeader { | 84 struct MachPortsExtraHeader { |
| 85 // Actual number of Mach ports encoded in the extra header. | 85 // Actual number of Mach ports encoded in the extra header. |
| 86 uint16_t num_ports; | 86 uint16_t num_ports; |
| 87 | 87 |
| 88 // Array of encoded Mach ports. If |num_ports| > 0, |entires[0]| through | 88 // Array of encoded Mach ports. If |num_ports| > 0, |entires[0]| through |
| 89 // to |entries[num_ports-1]| inclusive are valid. | 89 // to |entries[num_ports-1]| inclusive are valid. |
| 90 MachPortsEntry entries[0]; | 90 MachPortsEntry entries[0]; |
| 91 }; | 91 }; |
| 92 static_assert(sizeof(MachPortsExtraHeader) == 2, | 92 static_assert(sizeof(MachPortsExtraHeader) == 2, |
| 93 "sizeof(MachPortsExtraHeader) must be 2 bytes"); | 93 "sizeof(MachPortsExtraHeader) must be 2 bytes"); |
| 94 #elif defined(OS_WIN) |
| 95 struct HandleEntry { |
| 96 // The windows HANDLE. HANDLEs are guaranteed to fit inside 32-bits. |
| 97 // See: https://msdn.microsoft.com/en-us/library/aa384203(VS.85).aspx |
| 98 uint32_t handle; |
| 99 }; |
| 100 static_assert(sizeof(HandleEntry) == 4, |
| 101 "sizeof(HandleEntry) must be 4 bytes"); |
| 94 #endif | 102 #endif |
| 95 #pragma pack(pop) | 103 #pragma pack(pop) |
| 96 | 104 |
| 97 // Allocates and owns a buffer for message data with enough capacity for | 105 // Allocates and owns a buffer for message data with enough capacity for |
| 98 // |payload_size| bytes plus a header, plus |max_handles| platform handles. | 106 // |payload_size| bytes plus a header, plus |max_handles| platform handles. |
| 99 Message(size_t payload_size, | 107 Message(size_t payload_size, |
| 100 size_t max_handles, | 108 size_t max_handles, |
| 101 Header::MessageType message_type = Header::MessageType::NORMAL); | 109 Header::MessageType message_type = Header::MessageType::NORMAL); |
| 102 | 110 |
| 103 ~Message(); | 111 ~Message(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 121 return header_->num_header_bytes - sizeof(Header); | 129 return header_->num_header_bytes - sizeof(Header); |
| 122 } | 130 } |
| 123 | 131 |
| 124 void* mutable_payload() { return data_ + header_->num_header_bytes; } | 132 void* mutable_payload() { return data_ + header_->num_header_bytes; } |
| 125 const void* payload() const { return data_ + header_->num_header_bytes; } | 133 const void* payload() const { return data_ + header_->num_header_bytes; } |
| 126 size_t payload_size() const; | 134 size_t payload_size() const; |
| 127 #endif // defined(OS_CHROMEOS) || defined(OS_ANDROID) | 135 #endif // defined(OS_CHROMEOS) || defined(OS_ANDROID) |
| 128 | 136 |
| 129 size_t num_handles() const { return header_->num_handles; } | 137 size_t num_handles() const { return header_->num_handles; } |
| 130 bool has_handles() const { return header_->num_handles > 0; } | 138 bool has_handles() const { return header_->num_handles > 0; } |
| 131 PlatformHandle* handles(); | |
| 132 #if defined(OS_MACOSX) && !defined(OS_IOS) | 139 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 133 bool has_mach_ports() const; | 140 bool has_mach_ports() const; |
| 134 #endif | 141 #endif |
| 135 | 142 |
| 136 // Note: SetHandles() and TakeHandles() invalidate any previous value of | 143 // Note: SetHandles() and TakeHandles() invalidate any previous value of |
| 137 // handles(). | 144 // handles(). |
| 138 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); | 145 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); |
| 139 ScopedPlatformHandleVectorPtr TakeHandles(); | 146 ScopedPlatformHandleVectorPtr TakeHandles(); |
| 140 // Version of TakeHandles that returns a vector of platform handles suitable | 147 // Version of TakeHandles that returns a vector of platform handles suitable |
| 141 // for transfer over an underlying OS mechanism. i.e. file descriptors over | 148 // for transfer over an underlying OS mechanism. i.e. file descriptors over |
| 142 // a unix domain socket. Any handle that cannot be transferred this way, | 149 // a unix domain socket. Any handle that cannot be transferred this way, |
| 143 // such as Mach ports, will be removed. | 150 // such as Mach ports, will be removed. |
| 144 ScopedPlatformHandleVectorPtr TakeHandlesForTransport(); | 151 ScopedPlatformHandleVectorPtr TakeHandlesForTransport(); |
| 145 | 152 |
| 146 #if defined(OS_WIN) | 153 #if defined(OS_WIN) |
| 147 // Prepares the handles in this message for use in a different process. | 154 // Prepares the handles in this message for use in a different process. |
| 148 // Upon calling this the handles should belong to |from_process|; after the | 155 // Upon calling this the handles should belong to |from_process|; after the |
| 149 // call they'll belong to |to_process|. The source handles are always | 156 // call they'll belong to |to_process|. The source handles are always |
| 150 // closed by this call. Returns false iff one or more handles failed | 157 // closed by this call. Returns false iff one or more handles failed |
| 151 // duplication. | 158 // duplication. |
| 152 static bool RewriteHandles(base::ProcessHandle from_process, | 159 static bool RewriteHandles(base::ProcessHandle from_process, |
| 153 base::ProcessHandle to_process, | 160 base::ProcessHandle to_process, |
| 154 PlatformHandle* handles, | 161 PlatformHandleVector* handles); |
| 155 size_t num_handles); | |
| 156 #endif | 162 #endif |
| 157 | 163 |
| 158 private: | 164 private: |
| 159 size_t size_; | 165 size_t size_; |
| 160 size_t max_handles_; | 166 size_t max_handles_; |
| 161 char* data_; | 167 char* data_; |
| 162 Header* header_; | 168 Header* header_; |
| 163 | 169 |
| 170 ScopedPlatformHandleVectorPtr handle_vector_; |
| 171 |
| 164 #if defined(OS_WIN) | 172 #if defined(OS_WIN) |
| 165 // On Windows, handles are serialised into the extra header section. | 173 // On Windows, handles are serialised into the extra header section. |
| 166 PlatformHandle* handles_ = nullptr; | 174 HandleEntry* handles_ = nullptr; |
| 167 #else | 175 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 168 ScopedPlatformHandleVectorPtr handle_vector_; | |
| 169 #endif | |
| 170 | |
| 171 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
| 172 // On OSX, handles are serialised into the extra header section. | 176 // On OSX, handles are serialised into the extra header section. |
| 173 MachPortsExtraHeader* mach_ports_header_ = nullptr; | 177 MachPortsExtraHeader* mach_ports_header_ = nullptr; |
| 174 #endif | 178 #endif |
| 175 | 179 |
| 176 DISALLOW_COPY_AND_ASSIGN(Message); | 180 DISALLOW_COPY_AND_ASSIGN(Message); |
| 177 }; | 181 }; |
| 178 | 182 |
| 179 // Delegate methods are called from the I/O task runner with which the Channel | 183 // Delegate methods are called from the I/O task runner with which the Channel |
| 180 // was created (see Channel::Create). | 184 // was created (see Channel::Create). |
| 181 class Delegate { | 185 class Delegate { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 Delegate* delegate_; | 279 Delegate* delegate_; |
| 276 const std::unique_ptr<ReadBuffer> read_buffer_; | 280 const std::unique_ptr<ReadBuffer> read_buffer_; |
| 277 | 281 |
| 278 DISALLOW_COPY_AND_ASSIGN(Channel); | 282 DISALLOW_COPY_AND_ASSIGN(Channel); |
| 279 }; | 283 }; |
| 280 | 284 |
| 281 } // namespace edk | 285 } // namespace edk |
| 282 } // namespace mojo | 286 } // namespace mojo |
| 283 | 287 |
| 284 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 288 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
| OLD | NEW |