| 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 11 matching lines...) Expand all Loading... |
| 22 // delimited messages over an underlying I/O channel, optionally transferring | 22 // delimited messages over an underlying I/O channel, optionally transferring |
| 23 // one or more platform handles in the process. | 23 // one or more platform handles in the process. |
| 24 class Channel : public base::RefCountedThreadSafe<Channel> { | 24 class Channel : public base::RefCountedThreadSafe<Channel> { |
| 25 public: | 25 public: |
| 26 struct Message; | 26 struct Message; |
| 27 | 27 |
| 28 using MessagePtr = scoped_ptr<Message>; | 28 using MessagePtr = scoped_ptr<Message>; |
| 29 | 29 |
| 30 // A message to be written to a channel. | 30 // A message to be written to a channel. |
| 31 struct Message { | 31 struct Message { |
| 32 #pragma pack(push, 1) | |
| 33 struct Header { | 32 struct Header { |
| 34 enum class MessageType : uint16_t { | 33 enum class MessageType : uint16_t { |
| 35 // A normal message. | 34 // A normal message. |
| 36 NORMAL = 0, | 35 NORMAL = 0, |
| 37 #if defined(OS_MACOSX) | 36 #if defined(OS_MACOSX) |
| 38 // A control message containing handles to echo back. | 37 // A control message containing handles to echo back. |
| 39 HANDLES_SENT, | 38 HANDLES_SENT, |
| 40 // A control message containing handles that can now be closed. | 39 // A control message containing handles that can now be closed. |
| 41 HANDLES_SENT_ACK, | 40 HANDLES_SENT_ACK, |
| 42 #endif | 41 #endif |
| 43 }; | 42 }; |
| 44 | 43 |
| 45 // Message size in bytes, including the header. | 44 // Message size in bytes, including the header. |
| 46 uint32_t num_bytes; | 45 uint32_t num_bytes; |
| 47 | 46 |
| 48 // Total size of header, including extra header data (i.e. HANDLEs on | 47 // Number of attached handles. |
| 49 // windows). | |
| 50 uint16_t num_header_bytes; | |
| 51 | |
| 52 // Number of attached handles. May be less than the reserved handle | |
| 53 // storage size in this message on platforms that serialise handles as | |
| 54 // data (i.e. HANDLEs on Windows, Mach ports on OSX). | |
| 55 uint16_t num_handles; | 48 uint16_t num_handles; |
| 56 | 49 |
| 57 MessageType message_type; | 50 MessageType message_type; |
| 58 | |
| 59 char padding[6]; | |
| 60 }; | 51 }; |
| 61 #pragma pack(pop) | |
| 62 | 52 |
| 63 // Allocates and owns a buffer for message data with enough capacity for | 53 // Allocates and owns a buffer for message data with enough capacity for |
| 64 // |payload_size| bytes plus a header, plus |max_handles| platform handles. | 54 // |payload_size| bytes plus a header. Takes ownership of |handles|, which |
| 55 // may be null. |
| 65 Message(size_t payload_size, | 56 Message(size_t payload_size, |
| 66 size_t max_handles, | 57 size_t num_handles, |
| 67 Header::MessageType message_type = Header::MessageType::NORMAL); | 58 Header::MessageType message_type = Header::MessageType::NORMAL); |
| 68 | 59 |
| 69 ~Message(); | 60 ~Message(); |
| 70 | 61 |
| 71 // Constructs a Message from serialized message data. | 62 // Constructs a Message from serialized message data. |
| 72 static MessagePtr Deserialize(const void* data, size_t data_num_bytes); | 63 static MessagePtr Deserialize(const void* data, size_t data_num_bytes); |
| 73 | 64 |
| 74 const void* data() const { return data_; } | 65 const void* data() const { return data_; } |
| 75 size_t data_num_bytes() const { return size_; } | 66 size_t data_num_bytes() const { return size_; } |
| 76 | 67 |
| 77 const void* extra_header() const { return data_ + sizeof(Header); } | 68 void* mutable_payload() { return static_cast<void*>(header_ + 1); } |
| 78 void* mutable_extra_header() { return data_ + sizeof(Header); } | 69 const void* payload() const { |
| 79 size_t extra_header_size() const { | 70 return static_cast<const void*>(header_ + 1); |
| 80 return header_->num_header_bytes - sizeof(Header); | |
| 81 } | 71 } |
| 82 | |
| 83 void* mutable_payload() { return data_ + header_->num_header_bytes; } | |
| 84 const void* payload() const { return data_ + header_->num_header_bytes; } | |
| 85 size_t payload_size() const; | 72 size_t payload_size() const; |
| 86 | 73 |
| 87 size_t num_handles() const { return header_->num_handles; } | 74 size_t num_handles() const { return header_->num_handles; } |
| 88 bool has_handles() const { return header_->num_handles > 0; } | 75 bool has_handles() const { return header_->num_handles > 0; } |
| 89 PlatformHandle* handles(); | 76 PlatformHandle* handles(); |
| 90 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
| 91 bool has_mach_ports() const; | |
| 92 #endif | |
| 93 | 77 |
| 94 // Note: SetHandles() and TakeHandles() invalidate any previous value of | 78 // Note: SetHandles() and TakeHandles() invalidate any previous value of |
| 95 // handles(). | 79 // handles(). |
| 96 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); | 80 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); |
| 97 ScopedPlatformHandleVectorPtr TakeHandles(); | 81 ScopedPlatformHandleVectorPtr TakeHandles(); |
| 98 | 82 |
| 99 #if defined(OS_WIN) | 83 #if defined(OS_WIN) |
| 100 // Prepares the handles in this message for use in a different process. | 84 // Prepares the handles in this message for use in a different process. |
| 101 // Upon calling this the handles should belong to |from_process|; after the | 85 // Upon calling this the handles should belong to |from_process|; after the |
| 102 // call they'll belong to |to_process|. The source handles are always | 86 // call they'll belong to |to_process|. The source handles are always |
| 103 // closed by this call. Returns false iff one or more handles failed | 87 // closed by this call. Returns false iff one or more handles failed |
| 104 // duplication. | 88 // duplication. |
| 105 static bool RewriteHandles(base::ProcessHandle from_process, | 89 static bool RewriteHandles(base::ProcessHandle from_process, |
| 106 base::ProcessHandle to_process, | 90 base::ProcessHandle to_process, |
| 107 PlatformHandle* handles, | 91 PlatformHandle* handles, |
| 108 size_t num_handles); | 92 size_t num_handles); |
| 109 #endif | 93 #endif |
| 110 | 94 |
| 111 private: | 95 private: |
| 112 size_t size_; | 96 size_t size_; |
| 113 size_t max_handles_; | |
| 114 char* data_; | 97 char* data_; |
| 115 Header* header_; | 98 Header* header_; |
| 116 | 99 |
| 117 #if defined(OS_WIN) | 100 #if defined(OS_WIN) |
| 118 // On Windows, handles are serialized in the data buffer along with the | 101 // On Windows, handles are serialized in the data buffer along with the |
| 119 // rest of the payload. | 102 // rest of the payload. |
| 120 PlatformHandle* handles_ = nullptr; | 103 PlatformHandle* handles_ = nullptr; |
| 121 #else | 104 #else |
| 122 ScopedPlatformHandleVectorPtr handle_vector_; | 105 ScopedPlatformHandleVectorPtr handle_vector_; |
| 123 #endif | 106 #endif |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 // Called by the implementation when new data is available in the read | 168 // Called by the implementation when new data is available in the read |
| 186 // buffer. Returns false to indicate an error. Upon success, | 169 // buffer. Returns false to indicate an error. Upon success, |
| 187 // |*next_read_size_hint| will be set to a recommended size for the next | 170 // |*next_read_size_hint| will be set to a recommended size for the next |
| 188 // read done by the implementation. | 171 // read done by the implementation. |
| 189 bool OnReadComplete(size_t bytes_read, size_t* next_read_size_hint); | 172 bool OnReadComplete(size_t bytes_read, size_t* next_read_size_hint); |
| 190 | 173 |
| 191 // Called by the implementation when something goes horribly wrong. It is NOT | 174 // Called by the implementation when something goes horribly wrong. It is NOT |
| 192 // OK to call this synchronously from any public interface methods. | 175 // OK to call this synchronously from any public interface methods. |
| 193 void OnError(); | 176 void OnError(); |
| 194 | 177 |
| 195 // Retrieves the set of platform handles read for a given message. | 178 // Retrieves the set of platform handles read for a given message. |payload| |
| 196 // |extra_header| and |extra_header_size| correspond to the extra header data. | 179 // and |payload_size| correspond to the full message body. Depending on |
| 197 // Depending on the Channel implementation, this body may encode platform | 180 // the Channel implementation, this body may encode platform handles, or |
| 198 // handles, or handles may be stored and managed elsewhere by the | 181 // handles may be stored and managed elsewhere by the implementation. |
| 199 // implementation. If |num_handles| handles cannot be returned, this must | 182 // If |num_handles| handles cannot be returned, this must return null. |
| 200 // return null. | 183 // The implementation may also adjust the values of |*payload| and/or |
| 184 // |*payload_size| to hide handle data from the user. |
| 201 virtual ScopedPlatformHandleVectorPtr GetReadPlatformHandles( | 185 virtual ScopedPlatformHandleVectorPtr GetReadPlatformHandles( |
| 202 size_t num_handles, | 186 size_t num_handles, |
| 203 const void* extra_header, | 187 void** payload, |
| 204 size_t extra_header_size) = 0; | 188 size_t* payload_size) = 0; |
| 205 | 189 |
| 206 virtual void OnControlMessage(Message::Header::MessageType message_type, | 190 virtual void OnControlMessage(Message::Header::MessageType message_type, |
| 207 const void* payload, | 191 const void* payload, |
| 208 size_t payload_size, | 192 size_t payload_size, |
| 209 ScopedPlatformHandleVectorPtr handles) {} | 193 ScopedPlatformHandleVectorPtr handles) {} |
| 210 | 194 |
| 211 private: | 195 private: |
| 212 friend class base::RefCountedThreadSafe<Channel>; | 196 friend class base::RefCountedThreadSafe<Channel>; |
| 213 | 197 |
| 214 class ReadBuffer; | 198 class ReadBuffer; |
| 215 | 199 |
| 216 Delegate* delegate_; | 200 Delegate* delegate_; |
| 217 const scoped_ptr<ReadBuffer> read_buffer_; | 201 const scoped_ptr<ReadBuffer> read_buffer_; |
| 218 | 202 |
| 219 DISALLOW_COPY_AND_ASSIGN(Channel); | 203 DISALLOW_COPY_AND_ASSIGN(Channel); |
| 220 }; | 204 }; |
| 221 | 205 |
| 222 } // namespace edk | 206 } // namespace edk |
| 223 } // namespace mojo | 207 } // namespace mojo |
| 224 | 208 |
| 225 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 209 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
| OLD | NEW |