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