OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_MULTIPLEX_ROUTER_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <deque> | 10 #include <deque> |
11 #include <map> | 11 #include <map> |
12 #include <memory> | 12 #include <memory> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/memory/ref_counted_delete_on_message_loop.h" | 17 #include "base/memory/ref_counted_delete_on_message_loop.h" |
18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
21 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
22 #include "mojo/public/cpp/bindings/callback.h" | 22 #include "mojo/public/cpp/bindings/callback.h" |
| 23 #include "mojo/public/cpp/bindings/error.h" |
23 #include "mojo/public/cpp/bindings/lib/connector.h" | 24 #include "mojo/public/cpp/bindings/lib/connector.h" |
24 #include "mojo/public/cpp/bindings/lib/interface_id.h" | 25 #include "mojo/public/cpp/bindings/lib/interface_id.h" |
25 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 26 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
26 #include "mojo/public/cpp/bindings/lib/pipe_control_message_handler.h" | 27 #include "mojo/public/cpp/bindings/lib/pipe_control_message_handler.h" |
27 #include "mojo/public/cpp/bindings/lib/pipe_control_message_handler_delegate.h" | 28 #include "mojo/public/cpp/bindings/lib/pipe_control_message_handler_delegate.h" |
28 #include "mojo/public/cpp/bindings/lib/pipe_control_message_proxy.h" | 29 #include "mojo/public/cpp/bindings/lib/pipe_control_message_proxy.h" |
29 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 30 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
30 | 31 |
31 namespace base { | 32 namespace base { |
32 class SingleThreadTaskRunner; | 33 class SingleThreadTaskRunner; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 const ScopedInterfaceEndpointHandle& handle, | 89 const ScopedInterfaceEndpointHandle& handle, |
89 InterfaceEndpointClient* endpoint_client, | 90 InterfaceEndpointClient* endpoint_client, |
90 scoped_refptr<base::SingleThreadTaskRunner> runner); | 91 scoped_refptr<base::SingleThreadTaskRunner> runner); |
91 | 92 |
92 // Detaches the client attached to the specified endpoint. It must be called | 93 // Detaches the client attached to the specified endpoint. It must be called |
93 // on the same thread as the corresponding AttachEndpointClient() call. | 94 // on the same thread as the corresponding AttachEndpointClient() call. |
94 void DetachEndpointClient(const ScopedInterfaceEndpointHandle& handle); | 95 void DetachEndpointClient(const ScopedInterfaceEndpointHandle& handle); |
95 | 96 |
96 // Raises an error on the underlying message pipe. It disconnects the pipe | 97 // Raises an error on the underlying message pipe. It disconnects the pipe |
97 // and notifies all interfaces running on this pipe. | 98 // and notifies all interfaces running on this pipe. |
98 void RaiseError(); | 99 void RaiseError(Error error); |
99 | 100 |
100 std::unique_ptr<AssociatedGroup> CreateAssociatedGroup(); | 101 std::unique_ptr<AssociatedGroup> CreateAssociatedGroup(); |
101 | 102 |
102 static MultiplexRouter* GetRouter(AssociatedGroup* associated_group); | 103 static MultiplexRouter* GetRouter(AssociatedGroup* associated_group); |
103 | 104 |
104 // --------------------------------------------------------------------------- | 105 // --------------------------------------------------------------------------- |
105 // The following public methods are called on the creating thread. | 106 // The following public methods are called on the creating thread. |
106 | 107 |
107 // Please note that this method shouldn't be called unless it results from an | 108 // Please note that this method shouldn't be called unless it results from an |
108 // explicit request of the user of bindings (e.g., the user sets an | 109 // explicit request of the user of bindings (e.g., the user sets an |
(...skipping 19 matching lines...) Expand all Loading... |
128 connector_.PauseIncomingMethodCallProcessing(); | 129 connector_.PauseIncomingMethodCallProcessing(); |
129 } | 130 } |
130 void ResumeIncomingMethodCallProcessing() { | 131 void ResumeIncomingMethodCallProcessing() { |
131 DCHECK(thread_checker_.CalledOnValidThread()); | 132 DCHECK(thread_checker_.CalledOnValidThread()); |
132 connector_.ResumeIncomingMethodCallProcessing(); | 133 connector_.ResumeIncomingMethodCallProcessing(); |
133 } | 134 } |
134 | 135 |
135 // Whether there are any associated interfaces running currently. | 136 // Whether there are any associated interfaces running currently. |
136 bool HasAssociatedEndpoints() const; | 137 bool HasAssociatedEndpoints() const; |
137 | 138 |
| 139 // Sets the master interface name. Used only for debugging information. |
| 140 void SetMasterInterfaceName(const std::string& name); |
| 141 |
138 // Sets this object to testing mode. | 142 // Sets this object to testing mode. |
139 // In testing mode, the object doesn't disconnect the underlying message pipe | 143 // In testing mode, the object doesn't disconnect the underlying message pipe |
140 // when it receives unexpected or invalid messages. | 144 // when it receives unexpected or invalid messages. |
141 void EnableTestingMode(); | 145 void EnableTestingMode(); |
142 | 146 |
143 // Is the router bound to a message pipe handle? | 147 // Is the router bound to a message pipe handle? |
144 bool is_valid() const { | 148 bool is_valid() const { |
145 DCHECK(thread_checker_.CalledOnValidThread()); | 149 DCHECK(thread_checker_.CalledOnValidThread()); |
146 return connector_.is_valid(); | 150 return connector_.is_valid(); |
147 } | 151 } |
148 | 152 |
149 // TODO(yzshen): consider removing this getter. | 153 // TODO(yzshen): consider removing this getter. |
150 MessagePipeHandle handle() const { | 154 MessagePipeHandle handle() const { |
151 DCHECK(thread_checker_.CalledOnValidThread()); | 155 DCHECK(thread_checker_.CalledOnValidThread()); |
152 return connector_.handle(); | 156 return connector_.handle(); |
153 } | 157 } |
154 | 158 |
155 private: | 159 private: |
156 friend class base::RefCountedDeleteOnMessageLoop<MultiplexRouter>; | 160 friend class base::RefCountedDeleteOnMessageLoop<MultiplexRouter>; |
157 friend class base::DeleteHelper<MultiplexRouter>; | 161 friend class base::DeleteHelper<MultiplexRouter>; |
158 | 162 |
159 class InterfaceEndpoint; | 163 class InterfaceEndpoint; |
160 struct Task; | 164 struct Task; |
161 | 165 |
162 ~MultiplexRouter() override; | 166 ~MultiplexRouter() override; |
163 | 167 |
164 // MessageReceiver implementation: | 168 // MessageReceiver implementation: |
165 bool Accept(Message* message) override; | 169 bool Accept(Message* message, Error* error) override; |
166 | 170 |
167 // PipeControlMessageHandlerDelegate implementation: | 171 // PipeControlMessageHandlerDelegate implementation: |
168 bool OnPeerAssociatedEndpointClosed(InterfaceId id) override; | 172 bool OnPeerAssociatedEndpointClosed(InterfaceId id) override; |
169 bool OnAssociatedEndpointClosedBeforeSent(InterfaceId id) override; | 173 bool OnAssociatedEndpointClosedBeforeSent(InterfaceId id) override; |
170 | 174 |
171 void OnPipeConnectionError(); | 175 void OnPipeConnectionError(); |
172 | 176 |
173 // Specifies whether we are allowed to directly call into | 177 // Specifies whether we are allowed to directly call into |
174 // InterfaceEndpointClient (given that we are already on the same thread as | 178 // InterfaceEndpointClient (given that we are already on the same thread as |
175 // the client). | 179 // the client). |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 void MaybePostToProcessTasks(base::SingleThreadTaskRunner* task_runner); | 220 void MaybePostToProcessTasks(base::SingleThreadTaskRunner* task_runner); |
217 void LockAndCallProcessTasks(); | 221 void LockAndCallProcessTasks(); |
218 | 222 |
219 // Updates the state of |endpoint|. If both the endpoint and its peer have | 223 // Updates the state of |endpoint|. If both the endpoint and its peer have |
220 // been closed, removes it from |endpoints_|. | 224 // been closed, removes it from |endpoints_|. |
221 // NOTE: The method may invalidate |endpoint|. | 225 // NOTE: The method may invalidate |endpoint|. |
222 enum EndpointStateUpdateType { ENDPOINT_CLOSED, PEER_ENDPOINT_CLOSED }; | 226 enum EndpointStateUpdateType { ENDPOINT_CLOSED, PEER_ENDPOINT_CLOSED }; |
223 void UpdateEndpointStateMayRemove(InterfaceEndpoint* endpoint, | 227 void UpdateEndpointStateMayRemove(InterfaceEndpoint* endpoint, |
224 EndpointStateUpdateType type); | 228 EndpointStateUpdateType type); |
225 | 229 |
226 void RaiseErrorInNonTestingMode(); | 230 void RaiseErrorInNonTestingMode(Error error); |
227 | 231 |
228 InterfaceEndpoint* FindOrInsertEndpoint(InterfaceId id, bool* inserted); | 232 InterfaceEndpoint* FindOrInsertEndpoint(InterfaceId id, bool* inserted); |
229 | 233 |
230 // Whether to set the namespace bit when generating interface IDs. Please see | 234 // Whether to set the namespace bit when generating interface IDs. Please see |
231 // comments of kInterfaceIdNamespaceMask. | 235 // comments of kInterfaceIdNamespaceMask. |
232 const bool set_interface_id_namespace_bit_; | 236 const bool set_interface_id_namespace_bit_; |
233 | 237 |
234 MessageHeaderValidator header_validator_; | 238 MessageHeaderValidator header_validator_; |
235 Connector connector_; | 239 Connector connector_; |
236 | 240 |
237 base::ThreadChecker thread_checker_; | 241 base::ThreadChecker thread_checker_; |
238 | 242 |
239 // Protects the following members. | 243 // Protects the following members. |
240 mutable base::Lock lock_; | 244 mutable base::Lock lock_; |
241 PipeControlMessageHandler control_message_handler_; | 245 PipeControlMessageHandler control_message_handler_; |
242 PipeControlMessageProxy control_message_proxy_; | 246 PipeControlMessageProxy control_message_proxy_; |
243 | 247 |
244 std::map<InterfaceId, scoped_refptr<InterfaceEndpoint>> endpoints_; | 248 std::map<InterfaceId, scoped_refptr<InterfaceEndpoint>> endpoints_; |
245 uint32_t next_interface_id_value_; | 249 uint32_t next_interface_id_value_; |
| 250 std::string master_interface_name_; |
246 | 251 |
247 std::deque<std::unique_ptr<Task>> tasks_; | 252 std::deque<std::unique_ptr<Task>> tasks_; |
248 // It refers to tasks in |tasks_| and doesn't own any of them. | 253 // It refers to tasks in |tasks_| and doesn't own any of them. |
249 std::map<InterfaceId, std::deque<Task*>> sync_message_tasks_; | 254 std::map<InterfaceId, std::deque<Task*>> sync_message_tasks_; |
250 | 255 |
251 bool posted_to_process_tasks_; | 256 bool posted_to_process_tasks_; |
252 scoped_refptr<base::SingleThreadTaskRunner> posted_to_task_runner_; | 257 scoped_refptr<base::SingleThreadTaskRunner> posted_to_task_runner_; |
253 | 258 |
254 bool encountered_error_; | 259 bool encountered_error_; |
255 | 260 |
256 bool testing_mode_; | 261 bool testing_mode_; |
257 | 262 |
258 DISALLOW_COPY_AND_ASSIGN(MultiplexRouter); | 263 DISALLOW_COPY_AND_ASSIGN(MultiplexRouter); |
259 }; | 264 }; |
260 | 265 |
261 } // namespace internal | 266 } // namespace internal |
262 } // namespace mojo | 267 } // namespace mojo |
263 | 268 |
264 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ | 269 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ |
OLD | NEW |