Chromium Code Reviews| 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 |
| 48 #if defined(OS_CHROMEOS) | |
|
Ken Rockot(use gerrit already)
2016/03/03 02:34:45
I think we need to verify that this is a sufficien
Anand Mistry (off Chromium)
2016/03/03 02:36:55
I had the same thought, so I've commented on the i
elijahtaylor1
2016/03/03 18:33:46
If this was the question, ARC only builds Chrome f
| |
| 49 // Old message wire format for ChromeOS. | |
| 47 // Number of attached handles. | 50 // Number of attached handles. |
| 48 uint16_t num_handles; | 51 uint16_t num_handles; |
| 49 | 52 |
| 50 MessageType message_type; | 53 MessageType message_type; |
| 54 #else | |
| 55 // Total size of header, including extra header data (i.e. HANDLEs on | |
| 56 // windows). | |
| 57 uint16_t num_header_bytes; | |
| 58 | |
| 59 // Number of attached handles. May be less than the reserved handle | |
| 60 // storage size in this message on platforms that serialise handles as | |
| 61 // data (i.e. HANDLEs on Windows, Mach ports on OSX). | |
| 62 uint16_t num_handles; | |
| 63 | |
| 64 MessageType message_type; | |
| 65 | |
| 66 char padding[6]; | |
| 67 #endif // defined(OS_CHROMEOS) | |
| 51 }; | 68 }; |
| 69 #pragma pack(pop) | |
| 52 | 70 |
| 53 // Allocates and owns a buffer for message data with enough capacity for | 71 // Allocates and owns a buffer for message data with enough capacity for |
| 54 // |payload_size| bytes plus a header. Takes ownership of |handles|, which | 72 // |payload_size| bytes plus a header, plus |max_handles| platform handles. |
| 55 // may be null. | |
| 56 Message(size_t payload_size, | 73 Message(size_t payload_size, |
| 57 size_t num_handles, | 74 size_t max_handles, |
| 58 Header::MessageType message_type = Header::MessageType::NORMAL); | 75 Header::MessageType message_type = Header::MessageType::NORMAL); |
| 59 | 76 |
| 60 ~Message(); | 77 ~Message(); |
| 61 | 78 |
| 62 // Constructs a Message from serialized message data. | 79 // Constructs a Message from serialized message data. |
| 63 static MessagePtr Deserialize(const void* data, size_t data_num_bytes); | 80 static MessagePtr Deserialize(const void* data, size_t data_num_bytes); |
| 64 | 81 |
| 65 const void* data() const { return data_; } | 82 const void* data() const { return data_; } |
| 66 size_t data_num_bytes() const { return size_; } | 83 size_t data_num_bytes() const { return size_; } |
| 67 | 84 |
| 85 #if defined(OS_CHROMEOS) | |
| 68 void* mutable_payload() { return static_cast<void*>(header_ + 1); } | 86 void* mutable_payload() { return static_cast<void*>(header_ + 1); } |
| 69 const void* payload() const { | 87 const void* payload() const { |
| 70 return static_cast<const void*>(header_ + 1); | 88 return static_cast<const void*>(header_ + 1); |
| 71 } | 89 } |
| 72 size_t payload_size() const; | 90 size_t payload_size() const; |
| 91 #else | |
| 92 const void* extra_header() const { return data_ + sizeof(Header); } | |
| 93 void* mutable_extra_header() { return data_ + sizeof(Header); } | |
| 94 size_t extra_header_size() const { | |
| 95 return header_->num_header_bytes - sizeof(Header); | |
| 96 } | |
| 97 | |
| 98 void* mutable_payload() { return data_ + header_->num_header_bytes; } | |
| 99 const void* payload() const { return data_ + header_->num_header_bytes; } | |
| 100 size_t payload_size() const; | |
| 101 #endif // defined(OS_CHROMEOS) | |
| 73 | 102 |
| 74 size_t num_handles() const { return header_->num_handles; } | 103 size_t num_handles() const { return header_->num_handles; } |
| 75 bool has_handles() const { return header_->num_handles > 0; } | 104 bool has_handles() const { return header_->num_handles > 0; } |
| 76 PlatformHandle* handles(); | 105 PlatformHandle* handles(); |
| 106 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
| 107 bool has_mach_ports() const; | |
| 108 #endif | |
| 77 | 109 |
| 78 // Note: SetHandles() and TakeHandles() invalidate any previous value of | 110 // Note: SetHandles() and TakeHandles() invalidate any previous value of |
| 79 // handles(). | 111 // handles(). |
| 80 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); | 112 void SetHandles(ScopedPlatformHandleVectorPtr new_handles); |
| 81 ScopedPlatformHandleVectorPtr TakeHandles(); | 113 ScopedPlatformHandleVectorPtr TakeHandles(); |
| 82 | 114 |
| 83 #if defined(OS_WIN) | 115 #if defined(OS_WIN) |
| 84 // Prepares the handles in this message for use in a different process. | 116 // 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 | 117 // 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 | 118 // 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 | 119 // closed by this call. Returns false iff one or more handles failed |
| 88 // duplication. | 120 // duplication. |
| 89 static bool RewriteHandles(base::ProcessHandle from_process, | 121 static bool RewriteHandles(base::ProcessHandle from_process, |
| 90 base::ProcessHandle to_process, | 122 base::ProcessHandle to_process, |
| 91 PlatformHandle* handles, | 123 PlatformHandle* handles, |
| 92 size_t num_handles); | 124 size_t num_handles); |
| 93 #endif | 125 #endif |
| 94 | 126 |
| 95 private: | 127 private: |
| 96 size_t size_; | 128 size_t size_; |
| 129 size_t max_handles_; | |
| 97 char* data_; | 130 char* data_; |
| 98 Header* header_; | 131 Header* header_; |
| 99 | 132 |
| 100 #if defined(OS_WIN) | 133 #if defined(OS_WIN) |
| 101 // On Windows, handles are serialized in the data buffer along with the | 134 // On Windows, handles are serialized in the data buffer along with the |
| 102 // rest of the payload. | 135 // rest of the payload. |
| 103 PlatformHandle* handles_ = nullptr; | 136 PlatformHandle* handles_ = nullptr; |
| 104 #else | 137 #else |
| 105 ScopedPlatformHandleVectorPtr handle_vector_; | 138 ScopedPlatformHandleVectorPtr handle_vector_; |
| 106 #endif | 139 #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 | 201 // Called by the implementation when new data is available in the read |
| 169 // buffer. Returns false to indicate an error. Upon success, | 202 // buffer. Returns false to indicate an error. Upon success, |
| 170 // |*next_read_size_hint| will be set to a recommended size for the next | 203 // |*next_read_size_hint| will be set to a recommended size for the next |
| 171 // read done by the implementation. | 204 // read done by the implementation. |
| 172 bool OnReadComplete(size_t bytes_read, size_t* next_read_size_hint); | 205 bool OnReadComplete(size_t bytes_read, size_t* next_read_size_hint); |
| 173 | 206 |
| 174 // Called by the implementation when something goes horribly wrong. It is NOT | 207 // Called by the implementation when something goes horribly wrong. It is NOT |
| 175 // OK to call this synchronously from any public interface methods. | 208 // OK to call this synchronously from any public interface methods. |
| 176 void OnError(); | 209 void OnError(); |
| 177 | 210 |
| 178 // Retrieves the set of platform handles read for a given message. |payload| | 211 // Retrieves the set of platform handles read for a given message. |
| 179 // and |payload_size| correspond to the full message body. Depending on | 212 // |extra_header| and |extra_header_size| correspond to the extra header data. |
| 180 // the Channel implementation, this body may encode platform handles, or | 213 // Depending on the Channel implementation, this body may encode platform |
| 181 // handles may be stored and managed elsewhere by the implementation. | 214 // handles, or handles may be stored and managed elsewhere by the |
| 182 // If |num_handles| handles cannot be returned, this must return null. | 215 // implementation. If |num_handles| handles cannot be returned, this must |
| 183 // The implementation may also adjust the values of |*payload| and/or | 216 // return null. |
| 184 // |*payload_size| to hide handle data from the user. | |
| 185 virtual ScopedPlatformHandleVectorPtr GetReadPlatformHandles( | 217 virtual ScopedPlatformHandleVectorPtr GetReadPlatformHandles( |
| 186 size_t num_handles, | 218 size_t num_handles, |
| 187 void** payload, | 219 const void* extra_header, |
| 188 size_t* payload_size) = 0; | 220 size_t extra_header_size) = 0; |
| 189 | 221 |
| 190 virtual void OnControlMessage(Message::Header::MessageType message_type, | 222 virtual void OnControlMessage(Message::Header::MessageType message_type, |
| 191 const void* payload, | 223 const void* payload, |
| 192 size_t payload_size, | 224 size_t payload_size, |
| 193 ScopedPlatformHandleVectorPtr handles) {} | 225 ScopedPlatformHandleVectorPtr handles) {} |
| 194 | 226 |
| 195 private: | 227 private: |
| 196 friend class base::RefCountedThreadSafe<Channel>; | 228 friend class base::RefCountedThreadSafe<Channel>; |
| 197 | 229 |
| 198 class ReadBuffer; | 230 class ReadBuffer; |
| 199 | 231 |
| 200 Delegate* delegate_; | 232 Delegate* delegate_; |
| 201 const scoped_ptr<ReadBuffer> read_buffer_; | 233 const scoped_ptr<ReadBuffer> read_buffer_; |
| 202 | 234 |
| 203 DISALLOW_COPY_AND_ASSIGN(Channel); | 235 DISALLOW_COPY_AND_ASSIGN(Channel); |
| 204 }; | 236 }; |
| 205 | 237 |
| 206 } // namespace edk | 238 } // namespace edk |
| 207 } // namespace mojo | 239 } // namespace mojo |
| 208 | 240 |
| 209 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 241 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
| OLD | NEW |