Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: mojo/public/cpp/bindings/message.h

Issue 2064903002: Mojo: Report bindings validation errors via MojoNotifyBadMessage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
11 #include <limits> 11 #include <limits>
12 #include <memory> 12 #include <memory>
13 #include <string>
13 #include <vector> 14 #include <vector>
14 15
15 #include "base/logging.h" 16 #include "base/logging.h"
16 #include "mojo/public/cpp/bindings/lib/message_buffer.h" 17 #include "mojo/public/cpp/bindings/lib/message_buffer.h"
17 #include "mojo/public/cpp/bindings/lib/message_internal.h" 18 #include "mojo/public/cpp/bindings/lib/message_internal.h"
18 #include "mojo/public/cpp/system/message.h" 19 #include "mojo/public/cpp/system/message.h"
19 20
20 namespace mojo { 21 namespace mojo {
21 22
22 // Message is a holder for the data and handles to be sent over a MessagePipe. 23 // Message is a holder for the data and handles to be sent over a MessagePipe.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 std::vector<Handle>* mutable_handles() { return &handles_; } 93 std::vector<Handle>* mutable_handles() { return &handles_; }
93 94
94 // Access the underlying Buffer interface. 95 // Access the underlying Buffer interface.
95 internal::Buffer* buffer() { return buffer_.get(); } 96 internal::Buffer* buffer() { return buffer_.get(); }
96 97
97 // Takes a scoped MessageHandle which may be passed to |WriteMessageNew()| for 98 // Takes a scoped MessageHandle which may be passed to |WriteMessageNew()| for
98 // transmission. Note that this invalidates this Message object, taking 99 // transmission. Note that this invalidates this Message object, taking
99 // ownership of its internal storage and any attached handles. 100 // ownership of its internal storage and any attached handles.
100 ScopedMessageHandle TakeMojoMessage(); 101 ScopedMessageHandle TakeMojoMessage();
101 102
103 // Notifies the system that this message is "bad," in this case meaning it was
104 // rejected by bindings validation code.
105 void NotifyBadMessage(const std::string& error);
106
102 private: 107 private:
103 void CloseHandles(); 108 void CloseHandles();
104 109
105 std::unique_ptr<internal::MessageBuffer> buffer_; 110 std::unique_ptr<internal::MessageBuffer> buffer_;
106 std::vector<Handle> handles_; 111 std::vector<Handle> handles_;
107 112
108 DISALLOW_COPY_AND_ASSIGN(Message); 113 DISALLOW_COPY_AND_ASSIGN(Message);
109 }; 114 };
110 115
111 class MessageReceiver { 116 class MessageReceiver {
112 public: 117 public:
118 // The result of an Accept() or related operation.
119 class Result {
120 public:
121 enum class Type {
122 // The message was accepted successfully.
123 SUCCESS,
124
125 // A request was dropped because it didn't have a bound receiver after
126 // being read from a pipe. This may be raised on the receiving end of a
127 // request.
128 REQUEST_DROPPED,
129
130 // A request expecting a response has become impossible to fulfill (i.e.
131 // a response callback was destroyed without being Run.) This may be
132 // raised on the receiving side of a request, i.e. the responder.
133 RESPONSE_DROPPED,
134
135 // A message could not be sent for some reason, e.g., peer closure
136 // detected on a pipe.
137 SEND_FAILED,
138
139 // An unexpected or malformed message was received.
140 BAD_MESSAGE,
141
142 // An unknown error occurred. This may be raised in exceptional cases,
143 // e.g. when an code is reached which should be unreachable.
yzshen1 2016/06/15 16:22:33 nit: an code -> some code ?
144 UNKNOWN_ERROR,
145 };
146
147 explicit Result(Type type);
148 Result(Type type, const std::string& details);
149 Result(Result&& other);
150
151 ~Result();
152
153 Result& operator=(Result&& other);
154
155 // Set the Message object associated with this result, if any. Takes
156 // ownership of the contents of |*message|.
157 void set_message(Message* message) {
158 DCHECK(message);
159 message->MoveTo(&message_);
160 }
161
162 bool Succeeded() const { return type_ == Type::SUCCESS; }
163
164 Type type() const { return type_; }
165 const std::string& details() const { return details_; }
166 Message& message() { return message_; }
167
168 static Result ForSuccess() { return Result(Type::SUCCESS); }
169 static Result ForUnknownError() { return Result(Type::UNKNOWN_ERROR); }
170
171 static Result ForBadMessage(const std::string& details, Message* message);
172 static Result ForBadRequest(const std::string& interface_name,
173 const std::string& method_name,
174 Message* message);
175 static Result ForBadResponse(const std::string& interface_name,
176 const std::string& method_name,
177 Message* message);
178 static Result ForUnexpectedRequest(const std::string& interface_name,
179 Message* message);
180 static Result ForUnexpectedResponse(const std::string& interface_name,
181 Message* message);
182 static Result ForBadControlMessage(const std::string& interface_name,
183 Message* message);
184
185 private:
186 Type type_;
187 std::string details_;
188 Message message_;
189
190 DISALLOW_COPY_AND_ASSIGN(Result);
191 };
192
113 virtual ~MessageReceiver() {} 193 virtual ~MessageReceiver() {}
114 194
115 // The receiver may mutate the given message. Returns true if the message 195 // The receiver may mutate the given message. Returns true if the message
yzshen1 2016/06/15 16:22:33 Please update the comment about the return value.
116 // was accepted and false otherwise, indicating that the message was invalid 196 // was accepted and false otherwise, indicating that the message was invalid
117 // or malformed. 197 // or malformed.
118 virtual bool Accept(Message* message) WARN_UNUSED_RESULT = 0; 198 virtual Result Accept(Message* message) WARN_UNUSED_RESULT = 0;
119 }; 199 };
120 200
121 class MessageReceiverWithResponder : public MessageReceiver { 201 class MessageReceiverWithResponder : public MessageReceiver {
122 public: 202 public:
123 ~MessageReceiverWithResponder() override {} 203 ~MessageReceiverWithResponder() override {}
124 204
125 // A variant on Accept that registers a MessageReceiver (known as the 205 // A variant on Accept that registers a MessageReceiver (known as the
126 // responder) to handle the response message generated from the given 206 // responder) to handle the response message generated from the given
127 // message. The responder's Accept method may be called during 207 // message. The responder's Accept method may be called during
128 // AcceptWithResponder or some time after its return. 208 // AcceptWithResponder or some time after its return.
129 // 209 //
130 // NOTE: Upon returning true, AcceptWithResponder assumes ownership of 210 // NOTE: Upon returning true, AcceptWithResponder assumes ownership of
131 // |responder| and will delete it after calling |responder->Accept| or upon 211 // |responder| and will delete it after calling |responder->Accept| or upon
132 // its own destruction. 212 // its own destruction.
133 // 213 //
134 // TODO(yzshen): consider changing |responder| to 214 // TODO(yzshen): consider changing |responder| to
135 // std::unique_ptr<MessageReceiver>. 215 // std::unique_ptr<MessageReceiver>.
136 virtual bool AcceptWithResponder(Message* message, MessageReceiver* responder) 216 virtual Result AcceptWithResponder(Message* message,
217 MessageReceiver* responder)
137 WARN_UNUSED_RESULT = 0; 218 WARN_UNUSED_RESULT = 0;
138 }; 219 };
139 220
140 // A MessageReceiver that is also able to provide status about the state 221 // A MessageReceiver that is also able to provide status about the state
141 // of the underlying MessagePipe to which it will be forwarding messages 222 // of the underlying MessagePipe to which it will be forwarding messages
142 // received via the |Accept()| call. 223 // received via the |Accept()| call.
143 class MessageReceiverWithStatus : public MessageReceiver { 224 class MessageReceiverWithStatus : public MessageReceiver {
144 public: 225 public:
145 ~MessageReceiverWithStatus() override {} 226 ~MessageReceiverWithStatus() override {}
146 227
(...skipping 18 matching lines...) Expand all
165 // the responder) to handle the response message generated from the given 246 // the responder) to handle the response message generated from the given
166 // message. Any of the responder's methods (Accept or IsValid) may be called 247 // message. Any of the responder's methods (Accept or IsValid) may be called
167 // during AcceptWithResponder or some time after its return. 248 // during AcceptWithResponder or some time after its return.
168 // 249 //
169 // NOTE: Upon returning true, AcceptWithResponder assumes ownership of 250 // NOTE: Upon returning true, AcceptWithResponder assumes ownership of
170 // |responder| and will delete it after calling |responder->Accept| or upon 251 // |responder| and will delete it after calling |responder->Accept| or upon
171 // its own destruction. 252 // its own destruction.
172 // 253 //
173 // TODO(yzshen): consider changing |responder| to 254 // TODO(yzshen): consider changing |responder| to
174 // std::unique_ptr<MessageReceiver>. 255 // std::unique_ptr<MessageReceiver>.
175 virtual bool AcceptWithResponder(Message* message, 256 virtual Result AcceptWithResponder(Message* message,
176 MessageReceiverWithStatus* responder) 257 MessageReceiverWithStatus* responder)
177 WARN_UNUSED_RESULT = 0; 258 WARN_UNUSED_RESULT = 0;
178 }; 259 };
179 260
180 // Read a single message from the pipe. The caller should have created the 261 // Read a single message from the pipe. The caller should have created the
181 // Message, but not called Initialize(). Returns MOJO_RESULT_SHOULD_WAIT if 262 // Message, but not called Initialize(). Returns MOJO_RESULT_SHOULD_WAIT if
182 // the caller should wait on the handle to become readable. Returns 263 // the caller should wait on the handle to become readable. Returns
183 // MOJO_RESULT_OK if the message was read successfully and should be 264 // MOJO_RESULT_OK if the message was read successfully and should be
184 // dispatched, otherwise returns an error code if something went wrong. 265 // dispatched, otherwise returns an error code if something went wrong.
185 // 266 //
186 // NOTE: The message hasn't been validated and may be malformed! 267 // NOTE: The message hasn't been validated and may be malformed!
187 MojoResult ReadMessage(MessagePipeHandle handle, Message* message); 268 MojoResult ReadMessage(MessagePipeHandle handle, Message* message);
188 269
189 } // namespace mojo 270 } // namespace mojo
190 271
191 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_ 272 #endif // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698