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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/cpp/bindings/message.h
diff --git a/mojo/public/cpp/bindings/message.h b/mojo/public/cpp/bindings/message.h
index d8eed2bfc3ec00a96948954a86ade3b99fdc088c..cbd24461939c9f1f20e55bbfed83c2dc363904d2 100644
--- a/mojo/public/cpp/bindings/message.h
+++ b/mojo/public/cpp/bindings/message.h
@@ -10,6 +10,7 @@
#include <limits>
#include <memory>
+#include <string>
#include <vector>
#include "base/logging.h"
@@ -99,6 +100,10 @@ class Message {
// ownership of its internal storage and any attached handles.
ScopedMessageHandle TakeMojoMessage();
+ // Notifies the system that this message is "bad," in this case meaning it was
+ // rejected by bindings validation code.
+ void NotifyBadMessage(const std::string& error);
+
private:
void CloseHandles();
@@ -110,12 +115,87 @@ class Message {
class MessageReceiver {
public:
+ // The result of an Accept() or related operation.
+ class Result {
+ public:
+ enum class Type {
+ // The message was accepted successfully.
+ SUCCESS,
+
+ // A request was dropped because it didn't have a bound receiver after
+ // being read from a pipe. This may be raised on the receiving end of a
+ // request.
+ REQUEST_DROPPED,
+
+ // A request expecting a response has become impossible to fulfill (i.e.
+ // a response callback was destroyed without being Run.) This may be
+ // raised on the receiving side of a request, i.e. the responder.
+ RESPONSE_DROPPED,
+
+ // A message could not be sent for some reason, e.g., peer closure
+ // detected on a pipe.
+ SEND_FAILED,
+
+ // An unexpected or malformed message was received.
+ BAD_MESSAGE,
+
+ // An unknown error occurred. This may be raised in exceptional cases,
+ // e.g. when an code is reached which should be unreachable.
yzshen1 2016/06/15 16:22:33 nit: an code -> some code ?
+ UNKNOWN_ERROR,
+ };
+
+ explicit Result(Type type);
+ Result(Type type, const std::string& details);
+ Result(Result&& other);
+
+ ~Result();
+
+ Result& operator=(Result&& other);
+
+ // Set the Message object associated with this result, if any. Takes
+ // ownership of the contents of |*message|.
+ void set_message(Message* message) {
+ DCHECK(message);
+ message->MoveTo(&message_);
+ }
+
+ bool Succeeded() const { return type_ == Type::SUCCESS; }
+
+ Type type() const { return type_; }
+ const std::string& details() const { return details_; }
+ Message& message() { return message_; }
+
+ static Result ForSuccess() { return Result(Type::SUCCESS); }
+ static Result ForUnknownError() { return Result(Type::UNKNOWN_ERROR); }
+
+ static Result ForBadMessage(const std::string& details, Message* message);
+ static Result ForBadRequest(const std::string& interface_name,
+ const std::string& method_name,
+ Message* message);
+ static Result ForBadResponse(const std::string& interface_name,
+ const std::string& method_name,
+ Message* message);
+ static Result ForUnexpectedRequest(const std::string& interface_name,
+ Message* message);
+ static Result ForUnexpectedResponse(const std::string& interface_name,
+ Message* message);
+ static Result ForBadControlMessage(const std::string& interface_name,
+ Message* message);
+
+ private:
+ Type type_;
+ std::string details_;
+ Message message_;
+
+ DISALLOW_COPY_AND_ASSIGN(Result);
+ };
+
virtual ~MessageReceiver() {}
// 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.
// was accepted and false otherwise, indicating that the message was invalid
// or malformed.
- virtual bool Accept(Message* message) WARN_UNUSED_RESULT = 0;
+ virtual Result Accept(Message* message) WARN_UNUSED_RESULT = 0;
};
class MessageReceiverWithResponder : public MessageReceiver {
@@ -133,7 +213,8 @@ class MessageReceiverWithResponder : public MessageReceiver {
//
// TODO(yzshen): consider changing |responder| to
// std::unique_ptr<MessageReceiver>.
- virtual bool AcceptWithResponder(Message* message, MessageReceiver* responder)
+ virtual Result AcceptWithResponder(Message* message,
+ MessageReceiver* responder)
WARN_UNUSED_RESULT = 0;
};
@@ -172,8 +253,8 @@ class MessageReceiverWithResponderStatus : public MessageReceiver {
//
// TODO(yzshen): consider changing |responder| to
// std::unique_ptr<MessageReceiver>.
- virtual bool AcceptWithResponder(Message* message,
- MessageReceiverWithStatus* responder)
+ virtual Result AcceptWithResponder(Message* message,
+ MessageReceiverWithStatus* responder)
WARN_UNUSED_RESULT = 0;
};

Powered by Google App Engine
This is Rietveld 408576698