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 Reset(); |
25 | 27 |
26 void AllocData(uint32_t num_bytes); | 28 // These methods preallocate capacity in underlying storage, but they *DO NOT* |
27 void AllocUninitializedData(uint32_t num_bytes); | 29 // claim the allocated space. Use the interface exposed by buffer() to claim |
| 30 // bytes. |
| 31 // |
| 32 // Exactly one call is permitted to either of these methods on a single |
| 33 // instance of Message, and it must be made prior to accessing message |
| 34 // data (including header) or using the buffer() interface. |
| 35 void ReserveCapacity(uint32_t num_bytes); |
| 36 void ReserveUninitializedCapacity(uint32_t num_bytes); |
28 | 37 |
29 // Transfers data and handles to |destination|. | 38 // Transfers data and handles to |destination|. |
30 void MoveTo(Message* destination); | 39 void MoveTo(Message* destination); |
31 | 40 |
32 uint32_t data_num_bytes() const { return data_num_bytes_; } | 41 uint32_t data_num_bytes() const { return buffer_->data_num_bytes(); } |
33 | 42 |
34 // Access the raw bytes of the message. | 43 // Access the raw bytes of the message. |
35 const uint8_t* data() const { | 44 const uint8_t* data() const { |
36 return reinterpret_cast<const uint8_t*>(data_); | 45 return reinterpret_cast<const uint8_t*>(buffer_->data()); |
37 } | 46 } |
38 uint8_t* mutable_data() { return reinterpret_cast<uint8_t*>(data_); } | 47 uint8_t* mutable_data() { |
| 48 return reinterpret_cast<uint8_t*>(buffer_->data()); |
| 49 } |
39 | 50 |
40 // Access the header. | 51 // Access the header. |
41 const internal::MessageHeader* header() const { return &data_->header; } | 52 const internal::MessageHeader* header() const { |
| 53 return reinterpret_cast<const internal::MessageHeader*>(buffer_->data()); |
| 54 } |
42 | 55 |
43 uint32_t interface_id() const { return data_->header.interface_id; } | 56 internal::MessageHeader* header() { |
44 void set_interface_id(uint32_t id) { data_->header.interface_id = id; } | 57 return const_cast<internal::MessageHeader*>( |
| 58 static_cast<const Message*>(this)->header()); |
| 59 } |
45 | 60 |
46 uint32_t name() const { return data_->header.name; } | 61 uint32_t interface_id() const { return header()->interface_id; } |
47 bool has_flag(uint32_t flag) const { return !!(data_->header.flags & flag); } | 62 void set_interface_id(uint32_t id) { header()->interface_id = id; } |
| 63 |
| 64 uint32_t name() const { return header()->name; } |
| 65 bool has_flag(uint32_t flag) const { return !!(header()->flags & flag); } |
48 | 66 |
49 // Access the request_id field (if present). | 67 // Access the request_id field (if present). |
50 bool has_request_id() const { return data_->header.version >= 1; } | 68 bool has_request_id() const { return header()->version >= 1; } |
51 uint64_t request_id() const { | 69 uint64_t request_id() const { |
52 MOJO_DCHECK(has_request_id()); | 70 MOJO_DCHECK(has_request_id()); |
53 return static_cast<const internal::MessageHeaderWithRequestID*>( | 71 return static_cast<const internal::MessageHeaderWithRequestID*>( |
54 &data_->header)->request_id; | 72 header())->request_id; |
55 } | 73 } |
56 void set_request_id(uint64_t request_id) { | 74 void set_request_id(uint64_t request_id) { |
57 MOJO_DCHECK(has_request_id()); | 75 MOJO_DCHECK(has_request_id()); |
58 static_cast<internal::MessageHeaderWithRequestID*>(&data_->header) | 76 static_cast<internal::MessageHeaderWithRequestID*>(header()) |
59 ->request_id = request_id; | 77 ->request_id = request_id; |
60 } | 78 } |
61 | 79 |
62 // Access the payload. | 80 // Access the payload. |
63 const uint8_t* payload() const { | 81 const uint8_t* payload() const { return data() + header()->num_bytes; } |
64 return reinterpret_cast<const uint8_t*>(data_) + data_->header.num_bytes; | 82 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 { | 83 uint32_t payload_num_bytes() const { |
70 MOJO_DCHECK(data_num_bytes_ >= data_->header.num_bytes); | 84 MOJO_DCHECK(buffer_->data_num_bytes() >= header()->num_bytes); |
71 return data_num_bytes_ - data_->header.num_bytes; | 85 return buffer_->data_num_bytes() - header()->num_bytes; |
72 } | 86 } |
73 | 87 |
74 // Access the handles. | 88 // Access the handles. |
75 const std::vector<Handle>* handles() const { return &handles_; } | 89 const std::vector<Handle>* handles() const { return &handles_; } |
76 std::vector<Handle>* mutable_handles() { return &handles_; } | 90 std::vector<Handle>* mutable_handles() { return &handles_; } |
77 | 91 |
| 92 // Access the underlying Buffer interface. |
| 93 internal::Buffer* buffer() { return buffer_.get(); } |
| 94 |
78 private: | 95 private: |
79 void Initialize(); | 96 void Initialize(); |
80 void FreeDataAndCloseHandles(); | 97 void CloseHandles(); |
81 | 98 |
82 uint32_t data_num_bytes_; | 99 scoped_ptr<internal::PickleBuffer> buffer_; |
83 internal::MessageData* data_; | |
84 std::vector<Handle> handles_; | 100 std::vector<Handle> handles_; |
85 | 101 |
86 MOJO_DISALLOW_COPY_AND_ASSIGN(Message); | 102 MOJO_DISALLOW_COPY_AND_ASSIGN(Message); |
87 }; | 103 }; |
88 | 104 |
89 class MessageReceiver { | 105 class MessageReceiver { |
90 public: | 106 public: |
91 virtual ~MessageReceiver() {} | 107 virtual ~MessageReceiver() {} |
92 | 108 |
93 // The receiver may mutate the given message. Returns true if the message | 109 // 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. | 169 // otherwise returns an error code if something went wrong. |
154 // | 170 // |
155 // NOTE: The message hasn't been validated and may be malformed! | 171 // NOTE: The message hasn't been validated and may be malformed! |
156 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle, | 172 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle, |
157 MessageReceiver* receiver, | 173 MessageReceiver* receiver, |
158 bool* receiver_result); | 174 bool* receiver_result); |
159 | 175 |
160 } // namespace mojo | 176 } // namespace mojo |
161 | 177 |
162 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | 178 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ |
OLD | NEW |