| 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_LIB_ROUTER_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 | |
| 10 #include "mojo/public/cpp/bindings/callback.h" | |
| 11 #include "mojo/public/cpp/bindings/lib/connector.h" | |
| 12 #include "mojo/public/cpp/bindings/lib/shared_data.h" | |
| 13 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | |
| 14 #include "mojo/public/cpp/bindings/message_validator.h" | |
| 15 #include "mojo/public/cpp/environment/environment.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 namespace internal { | |
| 19 | |
| 20 // Router provides a way for sending messages over a MessagePipe, and re-routing | |
| 21 // response messages back to the sender. | |
| 22 class Router : public MessageReceiverWithResponder { | |
| 23 public: | |
| 24 Router(ScopedMessagePipeHandle message_pipe, | |
| 25 MessageValidatorList validators, | |
| 26 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); | |
| 27 ~Router() override; | |
| 28 | |
| 29 // Sets the receiver to handle messages read from the message pipe that do | |
| 30 // not have the kMessageIsResponse flag set. | |
| 31 void set_incoming_receiver(MessageReceiverWithResponderStatus* receiver) { | |
| 32 incoming_receiver_ = receiver; | |
| 33 } | |
| 34 | |
| 35 // Sets the error handler to receive notifications when an error is | |
| 36 // encountered while reading from the pipe or waiting to read from the pipe. | |
| 37 void set_connection_error_handler(const Closure& error_handler) { | |
| 38 connector_.set_connection_error_handler(error_handler); | |
| 39 } | |
| 40 | |
| 41 // Returns true if an error was encountered while reading from the pipe or | |
| 42 // waiting to read from the pipe. | |
| 43 bool encountered_error() const { return connector_.encountered_error(); } | |
| 44 | |
| 45 // Is the router bound to a MessagePipe handle? | |
| 46 bool is_valid() const { return connector_.is_valid(); } | |
| 47 | |
| 48 void CloseMessagePipe() { connector_.CloseMessagePipe(); } | |
| 49 | |
| 50 ScopedMessagePipeHandle PassMessagePipe() { | |
| 51 return connector_.PassMessagePipe(); | |
| 52 } | |
| 53 | |
| 54 // MessageReceiver implementation: | |
| 55 bool Accept(Message* message) override; | |
| 56 bool AcceptWithResponder(Message* message, | |
| 57 MessageReceiver* responder) override; | |
| 58 | |
| 59 // Blocks the current thread until the first incoming method call, i.e., | |
| 60 // either a call to a client method or a callback method, or |deadline|. | |
| 61 // When returning |false| closes the message pipe, unless the reason for | |
| 62 // for returning |false| was |MOJO_RESULT_SHOULD_WAIT| or | |
| 63 // |MOJO_RESULT_DEADLINE_EXCEEDED|. | |
| 64 // Use |encountered_error| to see if an error occurred. | |
| 65 bool WaitForIncomingMessage(MojoDeadline deadline) { | |
| 66 return connector_.WaitForIncomingMessage(deadline); | |
| 67 } | |
| 68 | |
| 69 // Sets this object to testing mode. | |
| 70 // In testing mode: | |
| 71 // - the object is more tolerant of unrecognized response messages; | |
| 72 // - the connector continues working after seeing errors from its incoming | |
| 73 // receiver. | |
| 74 void EnableTestingMode(); | |
| 75 | |
| 76 MessagePipeHandle handle() const { return connector_.handle(); } | |
| 77 | |
| 78 private: | |
| 79 typedef std::map<uint64_t, MessageReceiver*> ResponderMap; | |
| 80 | |
| 81 // This class is registered for incoming messages from the |Connector|. It | |
| 82 // simply forwards them to |Router::HandleIncomingMessages|. | |
| 83 class HandleIncomingMessageThunk : public MessageReceiver { | |
| 84 public: | |
| 85 explicit HandleIncomingMessageThunk(Router* router); | |
| 86 ~HandleIncomingMessageThunk() override; | |
| 87 | |
| 88 // MessageReceiver implementation: | |
| 89 bool Accept(Message* message) override; | |
| 90 | |
| 91 private: | |
| 92 Router* router_; | |
| 93 }; | |
| 94 | |
| 95 bool HandleIncomingMessage(Message* message); | |
| 96 | |
| 97 HandleIncomingMessageThunk thunk_; | |
| 98 MessageValidatorList validators_; | |
| 99 Connector connector_; | |
| 100 SharedData<Router*> weak_self_; | |
| 101 MessageReceiverWithResponderStatus* incoming_receiver_; | |
| 102 ResponderMap responders_; | |
| 103 uint64_t next_request_id_; | |
| 104 bool testing_mode_; | |
| 105 }; | |
| 106 | |
| 107 } // namespace internal | |
| 108 } // namespace mojo | |
| 109 | |
| 110 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | |
| OLD | NEW |