| 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 <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "mojo/public/cpp/bindings/lib/message_internal.h" | 11 #include "mojo/public/cpp/bindings/lib/message_internal.h" |
| 12 #include "mojo/public/cpp/bindings/lib/pickle_buffer.h" |
| 11 #include "mojo/public/cpp/environment/logging.h" | 13 #include "mojo/public/cpp/environment/logging.h" |
| 12 | 14 |
| 13 namespace mojo { | 15 namespace mojo { |
| 14 | 16 |
| 15 // Message is a holder for the data and handles to be sent over a MessagePipe. | 17 // Message is a holder for the data and handles to be sent over a MessagePipe. |
| 16 // Message owns its data and handles, but a consumer of Message is free to | 18 // Message owns its data and handles, but a consumer of Message is free to |
| 17 // mutate the data and handles. The message's data is comprised of a header | 19 // mutate the data and handles. The message's data is comprised of a header |
| 18 // followed by payload. | 20 // followed by payload. |
| 19 class Message { | 21 class Message { |
| 20 public: | 22 public: |
| 21 Message(); | 23 Message(); |
| 22 ~Message(); | 24 ~Message(); |
| 23 | 25 |
| 24 void Reset(); | 26 void Initialize(size_t capacity, bool zero_initialized); |
| 25 | |
| 26 void AllocData(uint32_t num_bytes); | |
| 27 void AllocUninitializedData(uint32_t num_bytes); | |
| 28 | 27 |
| 29 // Transfers data and handles to |destination|. | 28 // Transfers data and handles to |destination|. |
| 30 void MoveTo(Message* destination); | 29 void MoveTo(Message* destination); |
| 31 | 30 |
| 32 uint32_t data_num_bytes() const { return data_num_bytes_; } | 31 uint32_t data_num_bytes() const { return buffer_->data_num_bytes(); } |
| 33 | 32 |
| 34 // Access the raw bytes of the message. | 33 // Access the raw bytes of the message. |
| 35 const uint8_t* data() const { | 34 const uint8_t* data() const { |
| 36 return reinterpret_cast<const uint8_t*>(data_); | 35 return static_cast<const uint8_t*>(buffer_->data()); |
| 37 } | 36 } |
| 38 uint8_t* mutable_data() { return reinterpret_cast<uint8_t*>(data_); } | 37 |
| 38 uint8_t* mutable_data() { return static_cast<uint8_t*>(buffer_->data()); } |
| 39 | 39 |
| 40 // Access the header. | 40 // Access the header. |
| 41 const internal::MessageHeader* header() const { return &data_->header; } | 41 const internal::MessageHeader* header() const { |
| 42 return static_cast<const internal::MessageHeader*>(buffer_->data()); |
| 43 } |
| 42 | 44 |
| 43 uint32_t interface_id() const { return data_->header.interface_id; } | 45 internal::MessageHeader* header() { |
| 44 void set_interface_id(uint32_t id) { data_->header.interface_id = id; } | 46 return const_cast<internal::MessageHeader*>( |
| 47 static_cast<const Message*>(this)->header()); |
| 48 } |
| 45 | 49 |
| 46 uint32_t name() const { return data_->header.name; } | 50 uint32_t interface_id() const { return header()->interface_id; } |
| 47 bool has_flag(uint32_t flag) const { return !!(data_->header.flags & flag); } | 51 void set_interface_id(uint32_t id) { header()->interface_id = id; } |
| 52 |
| 53 uint32_t name() const { return header()->name; } |
| 54 bool has_flag(uint32_t flag) const { return !!(header()->flags & flag); } |
| 48 | 55 |
| 49 // Access the request_id field (if present). | 56 // Access the request_id field (if present). |
| 50 bool has_request_id() const { return data_->header.version >= 1; } | 57 bool has_request_id() const { return header()->version >= 1; } |
| 51 uint64_t request_id() const { | 58 uint64_t request_id() const { |
| 52 MOJO_DCHECK(has_request_id()); | 59 MOJO_DCHECK(has_request_id()); |
| 53 return static_cast<const internal::MessageHeaderWithRequestID*>( | 60 return static_cast<const internal::MessageHeaderWithRequestID*>( |
| 54 &data_->header)->request_id; | 61 header())->request_id; |
| 55 } | 62 } |
| 56 void set_request_id(uint64_t request_id) { | 63 void set_request_id(uint64_t request_id) { |
| 57 MOJO_DCHECK(has_request_id()); | 64 MOJO_DCHECK(has_request_id()); |
| 58 static_cast<internal::MessageHeaderWithRequestID*>(&data_->header) | 65 static_cast<internal::MessageHeaderWithRequestID*>(header()) |
| 59 ->request_id = request_id; | 66 ->request_id = request_id; |
| 60 } | 67 } |
| 61 | 68 |
| 62 // Access the payload. | 69 // Access the payload. |
| 63 const uint8_t* payload() const { | 70 const uint8_t* payload() const { return data() + header()->num_bytes; } |
| 64 return reinterpret_cast<const uint8_t*>(data_) + data_->header.num_bytes; | 71 uint8_t* mutable_payload() { return const_cast<uint8_t*>(payload()); } |
| 65 } | |
| 66 uint8_t* mutable_payload() { | |
| 67 return reinterpret_cast<uint8_t*>(data_) + data_->header.num_bytes; | |
| 68 } | |
| 69 uint32_t payload_num_bytes() const { | 72 uint32_t payload_num_bytes() const { |
| 70 MOJO_DCHECK(data_num_bytes_ >= data_->header.num_bytes); | 73 MOJO_DCHECK(buffer_->data_num_bytes() >= header()->num_bytes); |
| 71 return data_num_bytes_ - data_->header.num_bytes; | 74 return buffer_->data_num_bytes() - header()->num_bytes; |
| 72 } | 75 } |
| 73 | 76 |
| 74 // Access the handles. | 77 // Access the handles. |
| 75 const std::vector<Handle>* handles() const { return &handles_; } | 78 const std::vector<Handle>* handles() const { return &handles_; } |
| 76 std::vector<Handle>* mutable_handles() { return &handles_; } | 79 std::vector<Handle>* mutable_handles() { return &handles_; } |
| 77 | 80 |
| 81 // Access the underlying Buffer interface. |
| 82 internal::Buffer* buffer() { return buffer_.get(); } |
| 83 |
| 78 private: | 84 private: |
| 79 void Initialize(); | 85 void CloseHandles(); |
| 80 void FreeDataAndCloseHandles(); | |
| 81 | 86 |
| 82 uint32_t data_num_bytes_; | 87 scoped_ptr<internal::PickleBuffer> buffer_; |
| 83 internal::MessageData* data_; | |
| 84 std::vector<Handle> handles_; | 88 std::vector<Handle> handles_; |
| 85 | 89 |
| 86 MOJO_DISALLOW_COPY_AND_ASSIGN(Message); | 90 MOJO_DISALLOW_COPY_AND_ASSIGN(Message); |
| 87 }; | 91 }; |
| 88 | 92 |
| 89 class MessageReceiver { | 93 class MessageReceiver { |
| 90 public: | 94 public: |
| 91 virtual ~MessageReceiver() {} | 95 virtual ~MessageReceiver() {} |
| 92 | 96 |
| 93 // The receiver may mutate the given message. Returns true if the message | 97 // The receiver may mutate the given message. Returns true if the message |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 // otherwise returns an error code if something went wrong. | 157 // otherwise returns an error code if something went wrong. |
| 154 // | 158 // |
| 155 // NOTE: The message hasn't been validated and may be malformed! | 159 // NOTE: The message hasn't been validated and may be malformed! |
| 156 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle, | 160 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle, |
| 157 MessageReceiver* receiver, | 161 MessageReceiver* receiver, |
| 158 bool* receiver_result); | 162 bool* receiver_result); |
| 159 | 163 |
| 160 } // namespace mojo | 164 } // namespace mojo |
| 161 | 165 |
| 162 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | 166 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ |
| OLD | NEW |