OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "mojo/public/cpp/bindings/lib/message_internal.h" | |
11 #include "mojo/public/cpp/environment/logging.h" | |
12 | |
13 namespace mojo { | |
14 | |
15 // 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 | |
17 // mutate the data and handles. The message's data is comprised of a header | |
18 // followed by payload. | |
19 class Message { | |
20 public: | |
21 Message(); | |
22 ~Message(); | |
23 | |
24 void Reset(); | |
25 | |
26 void AllocData(uint32_t num_bytes); | |
27 void AllocUninitializedData(uint32_t num_bytes); | |
28 | |
29 // Transfers data and handles to |destination|. | |
30 void MoveTo(Message* destination); | |
31 | |
32 uint32_t data_num_bytes() const { return data_num_bytes_; } | |
33 | |
34 // Access the raw bytes of the message. | |
35 const uint8_t* data() const { | |
36 return reinterpret_cast<const uint8_t*>(data_); | |
37 } | |
38 uint8_t* mutable_data() { return reinterpret_cast<uint8_t*>(data_); } | |
39 | |
40 // Access the header. | |
41 const internal::MessageHeader* header() const { return &data_->header; } | |
42 | |
43 uint32_t name() const { return data_->header.name; } | |
44 bool has_flag(uint32_t flag) const { return !!(data_->header.flags & flag); } | |
45 | |
46 // Access the request_id field (if present). | |
47 bool has_request_id() const { return data_->header.version >= 1; } | |
48 uint64_t request_id() const { | |
49 MOJO_DCHECK(has_request_id()); | |
50 return static_cast<const internal::MessageHeaderWithRequestID*>( | |
51 &data_->header)->request_id; | |
52 } | |
53 void set_request_id(uint64_t request_id) { | |
54 MOJO_DCHECK(has_request_id()); | |
55 static_cast<internal::MessageHeaderWithRequestID*>(&data_->header) | |
56 ->request_id = request_id; | |
57 } | |
58 | |
59 // Access the payload. | |
60 const uint8_t* payload() const { | |
61 return reinterpret_cast<const uint8_t*>(data_) + data_->header.num_bytes; | |
62 } | |
63 uint8_t* mutable_payload() { | |
64 return reinterpret_cast<uint8_t*>(data_) + data_->header.num_bytes; | |
65 } | |
66 uint32_t payload_num_bytes() const { | |
67 MOJO_DCHECK(data_num_bytes_ >= data_->header.num_bytes); | |
68 return data_num_bytes_ - data_->header.num_bytes; | |
69 } | |
70 | |
71 // Access the handles. | |
72 const std::vector<Handle>* handles() const { return &handles_; } | |
73 std::vector<Handle>* mutable_handles() { return &handles_; } | |
74 | |
75 private: | |
76 void Initialize(); | |
77 void FreeDataAndCloseHandles(); | |
78 | |
79 uint32_t data_num_bytes_; | |
80 internal::MessageData* data_; | |
81 std::vector<Handle> handles_; | |
82 | |
83 MOJO_DISALLOW_COPY_AND_ASSIGN(Message); | |
84 }; | |
85 | |
86 class MessageReceiver { | |
87 public: | |
88 virtual ~MessageReceiver() {} | |
89 | |
90 // The receiver may mutate the given message. Returns true if the message | |
91 // was accepted and false otherwise, indicating that the message was invalid | |
92 // or malformed. | |
93 virtual bool Accept(Message* message) MOJO_WARN_UNUSED_RESULT = 0; | |
94 }; | |
95 | |
96 class MessageReceiverWithResponder : public MessageReceiver { | |
97 public: | |
98 ~MessageReceiverWithResponder() override {} | |
99 | |
100 // A variant on Accept that registers a MessageReceiver (known as the | |
101 // responder) to handle the response message generated from the given | |
102 // message. The responder's Accept method may be called during | |
103 // AcceptWithResponder or some time after its return. | |
104 // | |
105 // NOTE: Upon returning true, AcceptWithResponder assumes ownership of | |
106 // |responder| and will delete it after calling |responder->Accept| or upon | |
107 // its own destruction. | |
108 // | |
109 virtual bool AcceptWithResponder(Message* message, MessageReceiver* responder) | |
110 MOJO_WARN_UNUSED_RESULT = 0; | |
111 }; | |
112 | |
113 // A MessageReceiver that is also able to provide status about the state | |
114 // of the underlying MessagePipe to which it will be forwarding messages | |
115 // received via the |Accept()| call. | |
116 class MessageReceiverWithStatus : public MessageReceiver { | |
117 public: | |
118 ~MessageReceiverWithStatus() override {} | |
119 | |
120 // Returns |true| if this MessageReceiver is currently bound to a MessagePipe, | |
121 // the pipe has not been closed, and the pipe has not encountered an error. | |
122 virtual bool IsValid() = 0; | |
123 }; | |
124 | |
125 // An alternative to MessageReceiverWithResponder for cases in which it | |
126 // is necessary for the implementor of this interface to know about the status | |
127 // of the MessagePipe which will carry the responses. | |
128 class MessageReceiverWithResponderStatus : public MessageReceiver { | |
129 public: | |
130 ~MessageReceiverWithResponderStatus() override {} | |
131 | |
132 // A variant on Accept that registers a MessageReceiverWithStatus (known as | |
133 // the responder) to handle the response message generated from the given | |
134 // message. Any of the responder's methods (Accept or IsValid) may be called | |
135 // during AcceptWithResponder or some time after its return. | |
136 // | |
137 // NOTE: Upon returning true, AcceptWithResponder assumes ownership of | |
138 // |responder| and will delete it after calling |responder->Accept| or upon | |
139 // its own destruction. | |
140 // | |
141 virtual bool AcceptWithResponder(Message* message, | |
142 MessageReceiverWithStatus* responder) | |
143 MOJO_WARN_UNUSED_RESULT = 0; | |
144 }; | |
145 | |
146 // Read a single message from the pipe into the supplied |message|. |handle| | |
147 // must be valid. |message| must be non-null and empty (i.e., clear of any data | |
148 // and handles). | |
149 // | |
150 // This method calls into |MojoReadMessage()| and propagates any errors it | |
151 // produces. See mojo/public/c/include/mojo/system/message_pipe.h for a | |
152 // description of its possible return values. | |
153 // | |
154 // NOTE: The message isn't validated and may be malformed! | |
155 MojoResult ReadMessage(MessagePipeHandle handle, Message* message); | |
156 | |
157 // Read a single message from the pipe and dispatch to the given receiver. | |
158 // |handle| must be valid. |receiver| may be null, in which case the read | |
159 // message is simply discarded. If |receiver| is not null, then | |
160 // |receiver_result| should be non-null, and will be set the receiver's return | |
161 // value. | |
162 // | |
163 // This method calls into |MojoReadMessage()| and propagates any errors it | |
164 // produces. See mojo/public/c/include/mojo/system/message_pipe.h for a | |
165 // description of its possible return values. | |
166 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle, | |
167 MessageReceiver* receiver, | |
168 bool* receiver_result); | |
169 | |
170 } // namespace mojo | |
171 | |
172 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ | |
OLD | NEW |