| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <limits> | 11 #include <limits> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "mojo/public/cpp/bindings/bindings_export.h" | 19 #include "mojo/public/cpp/bindings/bindings_export.h" |
| 20 #include "mojo/public/cpp/bindings/lib/message_buffer.h" | 20 #include "mojo/public/cpp/bindings/lib/message_buffer.h" |
| 21 #include "mojo/public/cpp/bindings/lib/message_internal.h" | 21 #include "mojo/public/cpp/bindings/lib/message_internal.h" |
| 22 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
| 22 #include "mojo/public/cpp/system/message.h" | 23 #include "mojo/public/cpp/system/message.h" |
| 23 | 24 |
| 24 namespace mojo { | 25 namespace mojo { |
| 25 | 26 |
| 27 class AssociatedGroupController; |
| 28 |
| 26 using ReportBadMessageCallback = base::Callback<void(const std::string& error)>; | 29 using ReportBadMessageCallback = base::Callback<void(const std::string& error)>; |
| 27 | 30 |
| 28 // Message is a holder for the data and handles to be sent over a MessagePipe. | 31 // Message is a holder for the data and handles to be sent over a MessagePipe. |
| 29 // Message owns its data and handles, but a consumer of Message is free to | 32 // Message owns its data and handles, but a consumer of Message is free to |
| 30 // mutate the data and handles. The message's data is comprised of a header | 33 // mutate the data and handles. The message's data is comprised of a header |
| 31 // followed by payload. | 34 // followed by payload. |
| 32 class MOJO_CPP_BINDINGS_EXPORT Message { | 35 class MOJO_CPP_BINDINGS_EXPORT Message { |
| 33 public: | 36 public: |
| 34 static const uint32_t kFlagExpectsResponse = 1 << 0; | 37 static const uint32_t kFlagExpectsResponse = 1 << 0; |
| 35 static const uint32_t kFlagIsResponse = 1 << 1; | 38 static const uint32_t kFlagIsResponse = 1 << 1; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 66 const uint8_t* data() const { | 69 const uint8_t* data() const { |
| 67 return static_cast<const uint8_t*>(buffer_->data()); | 70 return static_cast<const uint8_t*>(buffer_->data()); |
| 68 } | 71 } |
| 69 | 72 |
| 70 uint8_t* mutable_data() { return static_cast<uint8_t*>(buffer_->data()); } | 73 uint8_t* mutable_data() { return static_cast<uint8_t*>(buffer_->data()); } |
| 71 | 74 |
| 72 // Access the header. | 75 // Access the header. |
| 73 const internal::MessageHeader* header() const { | 76 const internal::MessageHeader* header() const { |
| 74 return static_cast<const internal::MessageHeader*>(buffer_->data()); | 77 return static_cast<const internal::MessageHeader*>(buffer_->data()); |
| 75 } | 78 } |
| 79 internal::MessageHeader* header() { |
| 80 return static_cast<internal::MessageHeader*>(buffer_->data()); |
| 81 } |
| 76 | 82 |
| 77 internal::MessageHeader* header() { | 83 const internal::MessageHeaderV1* header_v1() const { |
| 78 return const_cast<internal::MessageHeader*>( | 84 DCHECK_GE(version(), 1u); |
| 79 static_cast<const Message*>(this)->header()); | 85 return static_cast<const internal::MessageHeaderV1*>(buffer_->data()); |
| 80 } | 86 } |
| 87 internal::MessageHeaderV1* header_v1() { |
| 88 DCHECK_GE(version(), 1u); |
| 89 return static_cast<internal::MessageHeaderV1*>(buffer_->data()); |
| 90 } |
| 91 |
| 92 const internal::MessageHeaderV2* header_v2() const { |
| 93 DCHECK_GE(version(), 2u); |
| 94 return static_cast<const internal::MessageHeaderV2*>(buffer_->data()); |
| 95 } |
| 96 internal::MessageHeaderV2* header_v2() { |
| 97 DCHECK_GE(version(), 2u); |
| 98 return static_cast<internal::MessageHeaderV2*>(buffer_->data()); |
| 99 } |
| 100 |
| 101 uint32_t version() const { return header()->version; } |
| 81 | 102 |
| 82 uint32_t interface_id() const { return header()->interface_id; } | 103 uint32_t interface_id() const { return header()->interface_id; } |
| 83 void set_interface_id(uint32_t id) { header()->interface_id = id; } | 104 void set_interface_id(uint32_t id) { header()->interface_id = id; } |
| 84 | 105 |
| 85 uint32_t name() const { return header()->name; } | 106 uint32_t name() const { return header()->name; } |
| 86 bool has_flag(uint32_t flag) const { return !!(header()->flags & flag); } | 107 bool has_flag(uint32_t flag) const { return !!(header()->flags & flag); } |
| 87 | 108 |
| 88 // Access the request_id field (if present). | 109 // Access the request_id field (if present). |
| 89 bool has_request_id() const { return header()->version >= 1; } | 110 uint64_t request_id() const { return header_v1()->request_id; } |
| 90 uint64_t request_id() const { | |
| 91 DCHECK(has_request_id()); | |
| 92 return static_cast<const internal::MessageHeaderWithRequestID*>( | |
| 93 header())->request_id; | |
| 94 } | |
| 95 void set_request_id(uint64_t request_id) { | 111 void set_request_id(uint64_t request_id) { |
| 96 DCHECK(has_request_id()); | 112 header_v1()->request_id = request_id; |
| 97 static_cast<internal::MessageHeaderWithRequestID*>(header()) | |
| 98 ->request_id = request_id; | |
| 99 } | 113 } |
| 100 | 114 |
| 101 // Access the payload. | 115 // Access the payload. |
| 102 const uint8_t* payload() const { return data() + header()->num_bytes; } | 116 const uint8_t* payload() const; |
| 103 uint8_t* mutable_payload() { return const_cast<uint8_t*>(payload()); } | 117 uint8_t* mutable_payload() { return const_cast<uint8_t*>(payload()); } |
| 104 uint32_t payload_num_bytes() const { | 118 uint32_t payload_num_bytes() const; |
| 105 DCHECK(data_num_bytes() >= header()->num_bytes); | 119 |
| 106 size_t num_bytes = data_num_bytes() - header()->num_bytes; | 120 uint32_t payload_num_interface_ids() const; |
| 107 DCHECK(num_bytes <= std::numeric_limits<uint32_t>::max()); | 121 const uint32_t* payload_interface_ids() const; |
| 108 return static_cast<uint32_t>(num_bytes); | |
| 109 } | |
| 110 | 122 |
| 111 // Access the handles. | 123 // Access the handles. |
| 112 const std::vector<Handle>* handles() const { return &handles_; } | 124 const std::vector<Handle>* handles() const { return &handles_; } |
| 113 std::vector<Handle>* mutable_handles() { return &handles_; } | 125 std::vector<Handle>* mutable_handles() { return &handles_; } |
| 114 | 126 |
| 127 const std::vector<ScopedInterfaceEndpointHandle>* |
| 128 associated_endpoint_handles() const { |
| 129 return &associated_endpoint_handles_; |
| 130 } |
| 131 std::vector<ScopedInterfaceEndpointHandle>* |
| 132 mutable_associated_endpoint_handles() { |
| 133 return &associated_endpoint_handles_; |
| 134 } |
| 135 |
| 115 // Access the underlying Buffer interface. | 136 // Access the underlying Buffer interface. |
| 116 internal::Buffer* buffer() { return buffer_.get(); } | 137 internal::Buffer* buffer() { return buffer_.get(); } |
| 117 | 138 |
| 118 // Takes a scoped MessageHandle which may be passed to |WriteMessageNew()| for | 139 // Takes a scoped MessageHandle which may be passed to |WriteMessageNew()| for |
| 119 // transmission. Note that this invalidates this Message object, taking | 140 // transmission. Note that this invalidates this Message object, taking |
| 120 // ownership of its internal storage and any attached handles. | 141 // ownership of its internal storage and any attached handles. |
| 121 ScopedMessageHandle TakeMojoMessage(); | 142 ScopedMessageHandle TakeMojoMessage(); |
| 122 | 143 |
| 123 // Notifies the system that this message is "bad," in this case meaning it was | 144 // Notifies the system that this message is "bad," in this case meaning it was |
| 124 // rejected by bindings validation code. | 145 // rejected by bindings validation code. |
| 125 void NotifyBadMessage(const std::string& error); | 146 void NotifyBadMessage(const std::string& error); |
| 126 | 147 |
| 148 // Serializes |associated_endpoint_handles_| into the payload_interface_ids |
| 149 // field. |
| 150 void SerializeAssociatedEndpointHandles( |
| 151 AssociatedGroupController* group_controller); |
| 152 |
| 153 // Deserializes |associated_endpoint_handles_| from the payload_interface_ids |
| 154 // field. |
| 155 bool DeserializeAssociatedEndpointHandles( |
| 156 AssociatedGroupController* group_controller); |
| 157 |
| 127 private: | 158 private: |
| 128 void CloseHandles(); | 159 void CloseHandles(); |
| 129 | 160 |
| 130 std::unique_ptr<internal::MessageBuffer> buffer_; | 161 std::unique_ptr<internal::MessageBuffer> buffer_; |
| 131 std::vector<Handle> handles_; | 162 std::vector<Handle> handles_; |
| 163 std::vector<ScopedInterfaceEndpointHandle> associated_endpoint_handles_; |
| 132 | 164 |
| 133 DISALLOW_COPY_AND_ASSIGN(Message); | 165 DISALLOW_COPY_AND_ASSIGN(Message); |
| 134 }; | 166 }; |
| 135 | 167 |
| 136 class MessageReceiver { | 168 class MessageReceiver { |
| 137 public: | 169 public: |
| 138 virtual ~MessageReceiver() {} | 170 virtual ~MessageReceiver() {} |
| 139 | 171 |
| 140 // The receiver may mutate the given message. Returns true if the message | 172 // The receiver may mutate the given message. Returns true if the message |
| 141 // was accepted and false otherwise, indicating that the message was invalid | 173 // was accepted and false otherwise, indicating that the message was invalid |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 // Message as bad. Note that this is only legal to call from directly within the | 306 // Message as bad. Note that this is only legal to call from directly within the |
| 275 // stack frame of a message dispatch, but the returned callback may be called | 307 // stack frame of a message dispatch, but the returned callback may be called |
| 276 // exactly once any time thereafter to report the message as bad. This may only | 308 // exactly once any time thereafter to report the message as bad. This may only |
| 277 // be called once per message. | 309 // be called once per message. |
| 278 MOJO_CPP_BINDINGS_EXPORT | 310 MOJO_CPP_BINDINGS_EXPORT |
| 279 ReportBadMessageCallback GetBadMessageCallback(); | 311 ReportBadMessageCallback GetBadMessageCallback(); |
| 280 | 312 |
| 281 } // namespace mojo | 313 } // namespace mojo |
| 282 | 314 |
| 283 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | 315 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ |
| OLD | NEW |