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> |
(...skipping 20 matching lines...) Expand all Loading... | |
31 class SingleThreadTaskRunner; | 31 class SingleThreadTaskRunner; |
32 } | 32 } |
33 | 33 |
34 namespace mojo { | 34 namespace mojo { |
35 | 35 |
36 class AssociatedGroup; | 36 class AssociatedGroup; |
37 | 37 |
38 namespace internal { | 38 namespace internal { |
39 | 39 |
40 class InterfaceEndpointClient; | 40 class InterfaceEndpointClient; |
41 class InterfaceEndpointController; | |
41 | 42 |
42 // MultiplexRouter supports routing messages for multiple interfaces over a | 43 // MultiplexRouter supports routing messages for multiple interfaces over a |
43 // single message pipe. | 44 // single message pipe. |
44 // | 45 // |
45 // It is created on the thread where the master interface of the message pipe | 46 // It is created on the thread where the master interface of the message pipe |
46 // lives. Although it is ref-counted, it is guarateed to be destructed on the | 47 // lives. Although it is ref-counted, it is guarateed to be destructed on the |
47 // same thread. | 48 // same thread. |
48 // Some public methods are only allowed to be called on the creating thread; | 49 // Some public methods are only allowed to be called on the creating thread; |
49 // while the others are safe to call from any threads. Please see the method | 50 // while the others are safe to call from any threads. Please see the method |
50 // comments for more details. | 51 // comments for more details. |
(...skipping 19 matching lines...) Expand all Loading... | |
70 // Creates an interface endpoint handle from a given interface ID. The handle | 71 // Creates an interface endpoint handle from a given interface ID. The handle |
71 // is used locally. | 72 // is used locally. |
72 // Typically, this method is used to (1) create an endpoint handle for the | 73 // Typically, this method is used to (1) create an endpoint handle for the |
73 // master interface; or (2) create an endpoint handle on receiving an | 74 // master interface; or (2) create an endpoint handle on receiving an |
74 // interface ID from the message pipe. | 75 // interface ID from the message pipe. |
75 ScopedInterfaceEndpointHandle CreateLocalEndpointHandle(InterfaceId id); | 76 ScopedInterfaceEndpointHandle CreateLocalEndpointHandle(InterfaceId id); |
76 | 77 |
77 // Closes an interface endpoint handle. | 78 // Closes an interface endpoint handle. |
78 void CloseEndpointHandle(InterfaceId id, bool is_local); | 79 void CloseEndpointHandle(InterfaceId id, bool is_local); |
79 | 80 |
80 // Attaches an client to the specified endpoint to send and receive messages. | 81 // Attaches an client to the specified endpoint to send and receive messages. |
Ken Rockot(use gerrit already)
2016/03/29 06:36:49
nit: While you're here, "Attaches a client"
yzshen1
2016/03/29 16:19:01
Done.
| |
81 void AttachEndpointClient(const ScopedInterfaceEndpointHandle& handle, | 82 // The returned object is still owned by the router. It should only be used on |
Ken Rockot(use gerrit already)
2016/03/29 06:36:49
nit: should = must?
yzshen1
2016/03/29 16:19:01
Done.
| |
82 InterfaceEndpointClient* endpoint_client); | 83 // the same thread as this call, and only before the client is detached using |
84 // DetachEndpointClient(). | |
85 InterfaceEndpointController* AttachEndpointClient( | |
86 const ScopedInterfaceEndpointHandle& handle, | |
87 InterfaceEndpointClient* endpoint_client); | |
88 | |
83 // Detaches the client attached to the specified endpoint. It should be called | 89 // Detaches the client attached to the specified endpoint. It should be called |
84 // on the same thread as the corresponding AttachEndpointClient() call. | 90 // on the same thread as the corresponding AttachEndpointClient() call. |
85 void DetachEndpointClient(const ScopedInterfaceEndpointHandle& handle); | 91 void DetachEndpointClient(const ScopedInterfaceEndpointHandle& handle); |
86 | 92 |
87 bool SendMessage(const ScopedInterfaceEndpointHandle& handle, | |
88 Message* message); | |
89 | |
90 // Raises an error on the underlying message pipe. It disconnects the pipe | 93 // Raises an error on the underlying message pipe. It disconnects the pipe |
91 // and notifies all interfaces running on this pipe. | 94 // and notifies all interfaces running on this pipe. |
92 void RaiseError(); | 95 void RaiseError(); |
93 | 96 |
94 scoped_ptr<AssociatedGroup> CreateAssociatedGroup(); | 97 scoped_ptr<AssociatedGroup> CreateAssociatedGroup(); |
95 | 98 |
96 static MultiplexRouter* GetRouter(AssociatedGroup* associated_group); | 99 static MultiplexRouter* GetRouter(AssociatedGroup* associated_group); |
97 | 100 |
98 // --------------------------------------------------------------------------- | 101 // --------------------------------------------------------------------------- |
99 // The following public methods are called on the creating thread. | 102 // The following public methods are called on the creating thread. |
100 | 103 |
101 // Please note that this method shouldn't be called unless it results from an | 104 // Please note that this method shouldn't be called unless it results from an |
102 // explicit request of the user of bindings (e.g., the user sets an | 105 // explicit request of the user of bindings (e.g., the user sets an |
103 // InterfacePtr to null or closes a Binding). | 106 // InterfacePtr to null or closes a Binding). |
104 void CloseMessagePipe() { | 107 void CloseMessagePipe(); |
105 DCHECK(thread_checker_.CalledOnValidThread()); | |
106 connector_.CloseMessagePipe(); | |
107 } | |
108 | 108 |
109 // Extracts the underlying message pipe. | 109 // Extracts the underlying message pipe. |
110 ScopedMessagePipeHandle PassMessagePipe() { | 110 ScopedMessagePipeHandle PassMessagePipe() { |
111 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
112 DCHECK(!HasAssociatedEndpoints()); | 112 DCHECK(!HasAssociatedEndpoints()); |
113 return connector_.PassMessagePipe(); | 113 return connector_.PassMessagePipe(); |
114 } | 114 } |
115 | 115 |
116 // Blocks the current thread until the first incoming message, or |deadline|. | 116 // Blocks the current thread until the first incoming message, or |deadline|. |
117 bool WaitForIncomingMessage(MojoDeadline deadline) { | 117 bool WaitForIncomingMessage(MojoDeadline deadline) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 | 160 |
161 // MessageReceiver implementation: | 161 // MessageReceiver implementation: |
162 bool Accept(Message* message) override; | 162 bool Accept(Message* message) override; |
163 | 163 |
164 // PipeControlMessageHandlerDelegate implementation: | 164 // PipeControlMessageHandlerDelegate implementation: |
165 bool OnPeerAssociatedEndpointClosed(InterfaceId id) override; | 165 bool OnPeerAssociatedEndpointClosed(InterfaceId id) override; |
166 bool OnAssociatedEndpointClosedBeforeSent(InterfaceId id) override; | 166 bool OnAssociatedEndpointClosedBeforeSent(InterfaceId id) override; |
167 | 167 |
168 void OnPipeConnectionError(); | 168 void OnPipeConnectionError(); |
169 | 169 |
170 // Specifies whether we are allowed to directly call into | |
171 // InterfaceEndpointClient (given that we are already on the same thread as | |
172 // the client). | |
173 enum ClientCallBehavior { | |
174 // Don't call any InterfaceEndpointClient methods directly. | |
175 NO_DIRECT_CLIENT_CALLS, | |
176 // Only call InterfaceEndpointClient::HandleIncomingMessage directly to | |
177 // handle sync messages. | |
178 ALLOW_DIRECT_CLIENT_CALLS_FOR_SYNC_MESSAGES, | |
179 // Allow to call any InterfaceEndpointClient methods directly. | |
180 ALLOW_DIRECT_CLIENT_CALLS | |
181 }; | |
182 | |
170 // Processes enqueued tasks (incoming messages and error notifications). | 183 // Processes enqueued tasks (incoming messages and error notifications). |
171 // If |force_async| is true, it guarantees not to call any | |
172 // InterfaceEndpointClient methods directly. | |
173 // | 184 // |
174 // Note: Because calling into InterfaceEndpointClient may lead to destruction | 185 // Note: Because calling into InterfaceEndpointClient may lead to destruction |
175 // of this object, if |force_async| is set to false, the caller needs to hold | 186 // of this object, if direct calls are allowed, the caller needs to hold on to |
176 // on to a ref outside of |lock_| before calling this method. | 187 // a ref outside of |lock_| before calling this method. |
177 void ProcessTasks(bool force_async); | 188 void ProcessTasks(ClientCallBehavior client_call_behavior); |
189 | |
190 // Processes the first queued sync message for the endpoint corresponding to | |
191 // |id|; returns whether there are more sync messages for that endpoint in the | |
192 // queue. | |
193 // | |
194 // This method is only used by enpoints during sync watching. Therefore, not | |
195 // all sync messages are handled by it. | |
196 bool ProcessFirstSyncMessageForEndpoint(InterfaceId id); | |
178 | 197 |
179 // Returns true to indicate that |task|/|message| has been processed. | 198 // Returns true to indicate that |task|/|message| has been processed. |
180 bool ProcessNotifyErrorTask(Task* task, bool force_async); | 199 bool ProcessNotifyErrorTask(Task* task, |
181 bool ProcessIncomingMessage(Message* message, bool force_async); | 200 ClientCallBehavior client_call_behavior); |
201 bool ProcessIncomingMessage(Message* message, | |
202 ClientCallBehavior client_call_behavior); | |
182 | 203 |
183 void MaybePostToProcessTasks(base::SingleThreadTaskRunner* task_runner); | 204 void MaybePostToProcessTasks(base::SingleThreadTaskRunner* task_runner); |
184 void LockAndCallProcessTasks(); | 205 void LockAndCallProcessTasks(); |
185 | 206 |
186 // Updates the state of |endpoint|. If both the endpoint and its peer have | 207 // Updates the state of |endpoint|. If both the endpoint and its peer have |
187 // been closed, removes it from |endpoints_|. | 208 // been closed, removes it from |endpoints_|. |
188 // NOTE: The method may invalidate |endpoint|. | 209 // NOTE: The method may invalidate |endpoint|. |
189 enum EndpointStateUpdateType { ENDPOINT_CLOSED, PEER_ENDPOINT_CLOSED }; | 210 enum EndpointStateUpdateType { ENDPOINT_CLOSED, PEER_ENDPOINT_CLOSED }; |
190 void UpdateEndpointStateMayRemove(InterfaceEndpoint* endpoint, | 211 void UpdateEndpointStateMayRemove(InterfaceEndpoint* endpoint, |
191 EndpointStateUpdateType type); | 212 EndpointStateUpdateType type); |
192 | 213 |
193 void RaiseErrorInNonTestingMode(); | 214 void RaiseErrorInNonTestingMode(); |
194 | 215 |
195 InterfaceEndpoint* FindOrInsertEndpoint(InterfaceId id, bool* inserted); | 216 InterfaceEndpoint* FindOrInsertEndpoint(InterfaceId id, bool* inserted); |
196 | 217 |
197 // Whether to set the namespace bit when generating interface IDs. Please see | 218 // Whether to set the namespace bit when generating interface IDs. Please see |
198 // comments of kInterfaceIdNamespaceMask. | 219 // comments of kInterfaceIdNamespaceMask. |
199 const bool set_interface_id_namespace_bit_; | 220 const bool set_interface_id_namespace_bit_; |
200 | 221 |
201 MessageHeaderValidator header_validator_; | 222 MessageHeaderValidator header_validator_; |
202 Connector connector_; | 223 Connector connector_; |
203 bool encountered_error_; | |
204 | 224 |
205 base::ThreadChecker thread_checker_; | 225 base::ThreadChecker thread_checker_; |
206 | 226 |
207 // Protects the following members. | 227 // Protects the following members. |
208 mutable base::Lock lock_; | 228 mutable base::Lock lock_; |
209 PipeControlMessageHandler control_message_handler_; | 229 PipeControlMessageHandler control_message_handler_; |
210 PipeControlMessageProxy control_message_proxy_; | 230 PipeControlMessageProxy control_message_proxy_; |
211 | 231 |
212 std::map<InterfaceId, scoped_refptr<InterfaceEndpoint>> endpoints_; | 232 std::map<InterfaceId, scoped_refptr<InterfaceEndpoint>> endpoints_; |
213 uint32_t next_interface_id_value_; | 233 uint32_t next_interface_id_value_; |
214 | 234 |
215 std::deque<scoped_ptr<Task>> tasks_; | 235 std::deque<scoped_ptr<Task>> tasks_; |
236 // It refers to tasks in |tasks_| and doesn't own any of them. | |
237 std::map<InterfaceId, std::deque<Task*>> sync_message_tasks_; | |
216 | 238 |
217 bool posted_to_process_tasks_; | 239 bool posted_to_process_tasks_; |
218 | 240 |
241 bool encountered_error_; | |
242 | |
219 bool testing_mode_; | 243 bool testing_mode_; |
220 | 244 |
221 DISALLOW_COPY_AND_ASSIGN(MultiplexRouter); | 245 DISALLOW_COPY_AND_ASSIGN(MultiplexRouter); |
222 }; | 246 }; |
223 | 247 |
224 } // namespace internal | 248 } // namespace internal |
225 } // namespace mojo | 249 } // namespace mojo |
226 | 250 |
227 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ | 251 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MULTIPLEX_ROUTER_H_ |
OLD | NEW |