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