| 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_LIB_ROUTER_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "mojo/public/cpp/bindings/lib/connector.h" | 10 #include "mojo/public/cpp/bindings/lib/connector.h" |
| 11 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 11 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 12 #include "mojo/public/cpp/bindings/lib/shared_data.h" | 12 #include "mojo/public/cpp/bindings/lib/shared_data.h" |
| 13 #include "mojo/public/cpp/environment/environment.h" | 13 #include "mojo/public/cpp/environment/environment.h" |
| 14 | 14 |
| 15 namespace mojo { | 15 namespace mojo { |
| 16 namespace internal { | 16 namespace internal { |
| 17 | 17 |
| 18 class Router : public MessageReceiverWithResponder { | 18 class Router : public MessageReceiverWithResponder { |
| 19 public: | 19 public: |
| 20 Router(ScopedMessagePipeHandle message_pipe, | 20 Router(ScopedMessagePipeHandle message_pipe, |
| 21 FilterChain filters, | 21 FilterChain filters, |
| 22 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); | 22 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); |
| 23 virtual ~Router(); | 23 ~Router() override; |
| 24 | 24 |
| 25 // Sets the receiver to handle messages read from the message pipe that do | 25 // Sets the receiver to handle messages read from the message pipe that do |
| 26 // not have the kMessageIsResponse flag set. | 26 // not have the kMessageIsResponse flag set. |
| 27 void set_incoming_receiver(MessageReceiverWithResponder* receiver) { | 27 void set_incoming_receiver(MessageReceiverWithResponder* receiver) { |
| 28 incoming_receiver_ = receiver; | 28 incoming_receiver_ = receiver; |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Sets the error handler to receive notifications when an error is | 31 // Sets the error handler to receive notifications when an error is |
| 32 // encountered while reading from the pipe or waiting to read from the pipe. | 32 // encountered while reading from the pipe or waiting to read from the pipe. |
| 33 void set_error_handler(ErrorHandler* error_handler) { | 33 void set_error_handler(ErrorHandler* error_handler) { |
| 34 connector_.set_error_handler(error_handler); | 34 connector_.set_error_handler(error_handler); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // Returns true if an error was encountered while reading from the pipe or | 37 // Returns true if an error was encountered while reading from the pipe or |
| 38 // waiting to read from the pipe. | 38 // waiting to read from the pipe. |
| 39 bool encountered_error() const { return connector_.encountered_error(); } | 39 bool encountered_error() const { return connector_.encountered_error(); } |
| 40 | 40 |
| 41 void CloseMessagePipe() { connector_.CloseMessagePipe(); } | 41 void CloseMessagePipe() { connector_.CloseMessagePipe(); } |
| 42 | 42 |
| 43 ScopedMessagePipeHandle PassMessagePipe() { | 43 ScopedMessagePipeHandle PassMessagePipe() { |
| 44 return connector_.PassMessagePipe(); | 44 return connector_.PassMessagePipe(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 // MessageReceiver implementation: | 47 // MessageReceiver implementation: |
| 48 virtual bool Accept(Message* message) override; | 48 bool Accept(Message* message) override; |
| 49 virtual bool AcceptWithResponder(Message* message, | 49 bool AcceptWithResponder(Message* message, |
| 50 MessageReceiver* responder) override; | 50 MessageReceiver* responder) override; |
| 51 | 51 |
| 52 // Blocks the current thread for the first incoming method call, i.e., either | 52 // Blocks the current thread for the first incoming method call, i.e., either |
| 53 // a call to a client method or a callback method. | 53 // a call to a client method or a callback method. |
| 54 bool WaitForIncomingMessage() { return connector_.WaitForIncomingMessage(); } | 54 bool WaitForIncomingMessage() { return connector_.WaitForIncomingMessage(); } |
| 55 | 55 |
| 56 // Sets this object to testing mode. | 56 // Sets this object to testing mode. |
| 57 // In testing mode: | 57 // In testing mode: |
| 58 // - the object is more tolerant of unrecognized response messages; | 58 // - the object is more tolerant of unrecognized response messages; |
| 59 // - the connector continues working after seeing errors from its incoming | 59 // - the connector continues working after seeing errors from its incoming |
| 60 // receiver. | 60 // receiver. |
| 61 void EnableTestingMode(); | 61 void EnableTestingMode(); |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 typedef std::map<uint64_t, MessageReceiver*> ResponderMap; | 64 typedef std::map<uint64_t, MessageReceiver*> ResponderMap; |
| 65 | 65 |
| 66 class HandleIncomingMessageThunk : public MessageReceiver { | 66 class HandleIncomingMessageThunk : public MessageReceiver { |
| 67 public: | 67 public: |
| 68 HandleIncomingMessageThunk(Router* router); | 68 HandleIncomingMessageThunk(Router* router); |
| 69 virtual ~HandleIncomingMessageThunk(); | 69 ~HandleIncomingMessageThunk() override; |
| 70 | 70 |
| 71 // MessageReceiver implementation: | 71 // MessageReceiver implementation: |
| 72 virtual bool Accept(Message* message) override; | 72 bool Accept(Message* message) override; |
| 73 | 73 |
| 74 private: | 74 private: |
| 75 Router* router_; | 75 Router* router_; |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 bool HandleIncomingMessage(Message* message); | 78 bool HandleIncomingMessage(Message* message); |
| 79 | 79 |
| 80 HandleIncomingMessageThunk thunk_; | 80 HandleIncomingMessageThunk thunk_; |
| 81 FilterChain filters_; | 81 FilterChain filters_; |
| 82 Connector connector_; | 82 Connector connector_; |
| 83 SharedData<Router*> weak_self_; | 83 SharedData<Router*> weak_self_; |
| 84 MessageReceiverWithResponder* incoming_receiver_; | 84 MessageReceiverWithResponder* incoming_receiver_; |
| 85 ResponderMap responders_; | 85 ResponderMap responders_; |
| 86 uint64_t next_request_id_; | 86 uint64_t next_request_id_; |
| 87 bool testing_mode_; | 87 bool testing_mode_; |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 } // namespace internal | 90 } // namespace internal |
| 91 } // namespace mojo | 91 } // namespace mojo |
| 92 | 92 |
| 93 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | 93 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ |
| OLD | NEW |