| 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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <queue> | 12 #include <queue> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 19 #include "mojo/public/cpp/bindings/callback.h" | 19 #include "mojo/public/cpp/bindings/callback.h" |
| 20 #include "mojo/public/cpp/bindings/error.h" |
| 20 #include "mojo/public/cpp/bindings/lib/connector.h" | 21 #include "mojo/public/cpp/bindings/lib/connector.h" |
| 21 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 22 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 22 | 23 |
| 23 namespace mojo { | 24 namespace mojo { |
| 24 namespace internal { | 25 namespace internal { |
| 25 | 26 |
| 26 // TODO(yzshen): Consider removing this class and use MultiplexRouter in all | 27 // TODO(yzshen): Consider removing this class and use MultiplexRouter in all |
| 27 // cases. crbug.com/594244 | 28 // cases. crbug.com/594244 |
| 28 class Router : public MessageReceiverWithResponder { | 29 class Router : public MessageReceiverWithResponder { |
| 29 public: | 30 public: |
| 30 Router(ScopedMessagePipeHandle message_pipe, | 31 Router(ScopedMessagePipeHandle message_pipe, |
| 31 FilterChain filters, | 32 FilterChain filters, |
| 32 bool expects_sync_requests, | 33 bool expects_sync_requests, |
| 33 scoped_refptr<base::SingleThreadTaskRunner> runner); | 34 scoped_refptr<base::SingleThreadTaskRunner> runner); |
| 34 ~Router() override; | 35 ~Router() override; |
| 35 | 36 |
| 36 // Sets the receiver to handle messages read from the message pipe that do | 37 // Sets the receiver to handle messages read from the message pipe that do |
| 37 // not have the kMessageIsResponse flag set. | 38 // not have the kMessageIsResponse flag set. |
| 38 void set_incoming_receiver(MessageReceiverWithResponderStatus* receiver) { | 39 void set_incoming_receiver(MessageReceiverWithResponderStatus* receiver) { |
| 39 incoming_receiver_ = receiver; | 40 incoming_receiver_ = receiver; |
| 40 } | 41 } |
| 41 | 42 |
| 43 // Sets the interface name for this Router. Used only for debugging. |
| 44 void set_interface_name(const std::string& name) { |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); |
| 46 interface_name_ = name; |
| 47 } |
| 48 |
| 42 // Sets the error handler to receive notifications when an error is | 49 // Sets the error handler to receive notifications when an error is |
| 43 // encountered while reading from the pipe or waiting to read from the pipe. | 50 // encountered while reading from the pipe or waiting to read from the pipe. |
| 44 void set_connection_error_handler(const Closure& error_handler) { | 51 void set_connection_error_handler(const Closure& error_handler) { |
| 45 error_handler_ = error_handler; | 52 error_handler_ = error_handler; |
| 46 } | 53 } |
| 47 | 54 |
| 48 // Returns true if an error was encountered while reading from the pipe or | 55 // Returns true if an error was encountered while reading from the pipe or |
| 49 // waiting to read from the pipe. | 56 // waiting to read from the pipe. |
| 50 bool encountered_error() const { | 57 bool encountered_error() const { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | 58 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 64 void CloseMessagePipe() { | 71 void CloseMessagePipe() { |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); | 72 DCHECK(thread_checker_.CalledOnValidThread()); |
| 66 connector_.CloseMessagePipe(); | 73 connector_.CloseMessagePipe(); |
| 67 } | 74 } |
| 68 | 75 |
| 69 ScopedMessagePipeHandle PassMessagePipe() { | 76 ScopedMessagePipeHandle PassMessagePipe() { |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); | 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 71 return connector_.PassMessagePipe(); | 78 return connector_.PassMessagePipe(); |
| 72 } | 79 } |
| 73 | 80 |
| 74 void RaiseError() { | 81 void RaiseError(Error error) { |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); | 82 DCHECK(thread_checker_.CalledOnValidThread()); |
| 76 connector_.RaiseError(); | 83 connector_.RaiseError(std::move(error)); |
| 77 } | 84 } |
| 78 | 85 |
| 79 // MessageReceiver implementation: | 86 // MessageReceiver implementation: |
| 80 bool Accept(Message* message) override; | 87 bool Accept(Message* message, Error* error) override; |
| 81 bool AcceptWithResponder(Message* message, | 88 bool AcceptWithResponder(Message* message, |
| 82 MessageReceiver* responder) override; | 89 MessageReceiver* responder, |
| 90 Error* error) override; |
| 83 | 91 |
| 84 // Blocks the current thread until the first incoming method call, i.e., | 92 // Blocks the current thread until the first incoming method call, i.e., |
| 85 // either a call to a client method or a callback method, or |deadline|. | 93 // either a call to a client method or a callback method, or |deadline|. |
| 86 bool WaitForIncomingMessage(MojoDeadline deadline) { | 94 bool WaitForIncomingMessage(MojoDeadline deadline) { |
| 87 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 88 return connector_.WaitForIncomingMessage(deadline); | 96 return connector_.WaitForIncomingMessage(deadline); |
| 89 } | 97 } |
| 90 | 98 |
| 91 // See Binding for details of pause/resume. | 99 // See Binding for details of pause/resume. |
| 92 void PauseIncomingMethodCallProcessing() { | 100 void PauseIncomingMethodCallProcessing() { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 }; | 142 }; |
| 135 | 143 |
| 136 using SyncResponseMap = std::map<uint64_t, std::unique_ptr<SyncResponseInfo>>; | 144 using SyncResponseMap = std::map<uint64_t, std::unique_ptr<SyncResponseInfo>>; |
| 137 | 145 |
| 138 class HandleIncomingMessageThunk : public MessageReceiver { | 146 class HandleIncomingMessageThunk : public MessageReceiver { |
| 139 public: | 147 public: |
| 140 HandleIncomingMessageThunk(Router* router); | 148 HandleIncomingMessageThunk(Router* router); |
| 141 ~HandleIncomingMessageThunk() override; | 149 ~HandleIncomingMessageThunk() override; |
| 142 | 150 |
| 143 // MessageReceiver implementation: | 151 // MessageReceiver implementation: |
| 144 bool Accept(Message* message) override; | 152 bool Accept(Message* message, Error* error) override; |
| 145 | 153 |
| 146 private: | 154 private: |
| 147 Router* router_; | 155 Router* router_; |
| 148 }; | 156 }; |
| 149 | 157 |
| 150 bool HandleIncomingMessage(Message* message); | 158 bool HandleIncomingMessage(Message* message, Error* error); |
| 151 void HandleQueuedMessages(); | 159 void HandleQueuedMessages(); |
| 152 bool HandleMessageInternal(Message* message); | 160 bool HandleMessageInternal(Message* message, Error* error); |
| 153 | 161 |
| 154 void OnConnectionError(); | 162 void OnConnectionError(); |
| 155 | 163 |
| 156 HandleIncomingMessageThunk thunk_; | 164 HandleIncomingMessageThunk thunk_; |
| 157 FilterChain filters_; | 165 FilterChain filters_; |
| 158 Connector connector_; | 166 Connector connector_; |
| 159 MessageReceiverWithResponderStatus* incoming_receiver_; | 167 MessageReceiverWithResponderStatus* incoming_receiver_; |
| 160 AsyncResponderMap async_responders_; | 168 AsyncResponderMap async_responders_; |
| 161 SyncResponseMap sync_responses_; | 169 SyncResponseMap sync_responses_; |
| 162 uint64_t next_request_id_; | 170 uint64_t next_request_id_; |
| 163 bool testing_mode_; | 171 bool testing_mode_; |
| 172 std::string interface_name_; |
| 164 std::queue<std::unique_ptr<Message>> pending_messages_; | 173 std::queue<std::unique_ptr<Message>> pending_messages_; |
| 165 // Whether a task has been posted to trigger processing of | 174 // Whether a task has been posted to trigger processing of |
| 166 // |pending_messages_|. | 175 // |pending_messages_|. |
| 167 bool pending_task_for_messages_; | 176 bool pending_task_for_messages_; |
| 168 bool encountered_error_; | 177 bool encountered_error_; |
| 169 Closure error_handler_; | 178 Closure error_handler_; |
| 170 base::ThreadChecker thread_checker_; | 179 base::ThreadChecker thread_checker_; |
| 171 base::WeakPtrFactory<Router> weak_factory_; | 180 base::WeakPtrFactory<Router> weak_factory_; |
| 172 }; | 181 }; |
| 173 | 182 |
| 174 } // namespace internal | 183 } // namespace internal |
| 175 } // namespace mojo | 184 } // namespace mojo |
| 176 | 185 |
| 177 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | 186 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ |
| OLD | NEW |