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 |
28 // These methods preallocate capacity in underlying storage, but they *DO NOT* | |
29 // claim that space. Use the interface exposed by |buffer()| to claim bytes. | |
26 void AllocData(uint32_t num_bytes); | 30 void AllocData(uint32_t num_bytes); |
27 void AllocUninitializedData(uint32_t num_bytes); | 31 void AllocUninitializedData(uint32_t num_bytes); |
28 | 32 |
29 // Transfers data and handles to |destination|. | 33 // Transfers data and handles to |destination|. |
30 void MoveTo(Message* destination); | 34 void MoveTo(Message* destination); |
31 | 35 |
32 uint32_t data_num_bytes() const { return data_num_bytes_; } | 36 uint32_t data_num_bytes() const { return buffer_->data_num_bytes(); } |
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 reinterpret_cast<const uint8_t*>(buffer_->data()); |
37 } | 41 } |
38 uint8_t* mutable_data() { return reinterpret_cast<uint8_t*>(data_); } | 42 uint8_t* mutable_data() { |
43 return reinterpret_cast<uint8_t*>(buffer_->data()); | |
44 } | |
39 | 45 |
40 // Access the header. | 46 // Access the header. |
41 const internal::MessageHeader* header() const { return &data_->header; } | 47 const internal::MessageHeader* header() const { |
48 return reinterpret_cast<const internal::MessageHeader*>(buffer_->data()); | |
yzshen1
2015/12/15 19:07:02
nit: static_cast here and the two methods above?
| |
49 } | |
42 | 50 |
43 uint32_t interface_id() const { return data_->header.interface_id; } | 51 internal::MessageHeader* header() { |
44 void set_interface_id(uint32_t id) { data_->header.interface_id = id; } | 52 return const_cast<internal::MessageHeader*>( |
53 static_cast<const Message*>(this)->header()); | |
54 } | |
45 | 55 |
46 uint32_t name() const { return data_->header.name; } | 56 uint32_t interface_id() const { return header()->interface_id; } |
47 bool has_flag(uint32_t flag) const { return !!(data_->header.flags & flag); } | 57 void set_interface_id(uint32_t id) { header()->interface_id = id; } |
58 | |
59 uint32_t name() const { return header()->name; } | |
60 bool has_flag(uint32_t flag) const { return !!(header()->flags & flag); } | |
48 | 61 |
49 // Access the request_id field (if present). | 62 // Access the request_id field (if present). |
50 bool has_request_id() const { return data_->header.version >= 1; } | 63 bool has_request_id() const { return header()->version >= 1; } |
51 uint64_t request_id() const { | 64 uint64_t request_id() const { |
52 MOJO_DCHECK(has_request_id()); | 65 MOJO_DCHECK(has_request_id()); |
53 return static_cast<const internal::MessageHeaderWithRequestID*>( | 66 return static_cast<const internal::MessageHeaderWithRequestID*>( |
54 &data_->header)->request_id; | 67 header())->request_id; |
55 } | 68 } |
56 void set_request_id(uint64_t request_id) { | 69 void set_request_id(uint64_t request_id) { |
57 MOJO_DCHECK(has_request_id()); | 70 MOJO_DCHECK(has_request_id()); |
58 static_cast<internal::MessageHeaderWithRequestID*>(&data_->header) | 71 static_cast<internal::MessageHeaderWithRequestID*>(header()) |
59 ->request_id = request_id; | 72 ->request_id = request_id; |
60 } | 73 } |
61 | 74 |
62 // Access the payload. | 75 // Access the payload. |
63 const uint8_t* payload() const { | 76 const uint8_t* payload() const { return data() + header()->num_bytes; } |
64 return reinterpret_cast<const uint8_t*>(data_) + data_->header.num_bytes; | 77 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 { | 78 uint32_t payload_num_bytes() const { |
70 MOJO_DCHECK(data_num_bytes_ >= data_->header.num_bytes); | 79 MOJO_DCHECK(buffer_->data_num_bytes() >= header()->num_bytes); |
71 return data_num_bytes_ - data_->header.num_bytes; | 80 return buffer_->data_num_bytes() - header()->num_bytes; |
72 } | 81 } |
73 | 82 |
74 // Access the handles. | 83 // Access the handles. |
75 const std::vector<Handle>* handles() const { return &handles_; } | 84 const std::vector<Handle>* handles() const { return &handles_; } |
76 std::vector<Handle>* mutable_handles() { return &handles_; } | 85 std::vector<Handle>* mutable_handles() { return &handles_; } |
77 | 86 |
87 // Access the underlying Buffer interface. | |
88 internal::Buffer* buffer() { return buffer_.get(); } | |
89 | |
78 private: | 90 private: |
79 void Initialize(); | 91 void Initialize(); |
80 void FreeDataAndCloseHandles(); | 92 void CloseHandles(); |
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 |