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 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/weak_ptr.h" |
13 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
14 #include "mojo/public/cpp/bindings/callback.h" | 15 #include "mojo/public/cpp/bindings/callback.h" |
15 #include "mojo/public/cpp/bindings/lib/connector.h" | 16 #include "mojo/public/cpp/bindings/lib/connector.h" |
16 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 17 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
17 #include "mojo/public/cpp/bindings/lib/shared_data.h" | |
18 #include "mojo/public/cpp/environment/environment.h" | 18 #include "mojo/public/cpp/environment/environment.h" |
19 | 19 |
20 namespace mojo { | 20 namespace mojo { |
21 namespace internal { | 21 namespace internal { |
22 | 22 |
23 class Router : public MessageReceiverWithResponder { | 23 class Router : public MessageReceiverWithResponder { |
24 public: | 24 public: |
25 Router(ScopedMessagePipeHandle message_pipe, | 25 Router(ScopedMessagePipeHandle message_pipe, |
26 FilterChain filters, | 26 FilterChain filters, |
27 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); | 27 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 // - the object is more tolerant of unrecognized response messages; | 97 // - the object is more tolerant of unrecognized response messages; |
98 // - the connector continues working after seeing errors from its incoming | 98 // - the connector continues working after seeing errors from its incoming |
99 // receiver. | 99 // receiver. |
100 void EnableTestingMode(); | 100 void EnableTestingMode(); |
101 | 101 |
102 MessagePipeHandle handle() const { return connector_.handle(); } | 102 MessagePipeHandle handle() const { return connector_.handle(); } |
103 | 103 |
104 // Returns true if this Router has any pending callbacks. | 104 // Returns true if this Router has any pending callbacks. |
105 bool has_pending_responders() const { | 105 bool has_pending_responders() const { |
106 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
107 return !responders_.empty(); | 107 return !async_responders_.empty() || !sync_responders_.empty(); |
108 } | 108 } |
109 | 109 |
110 private: | 110 private: |
| 111 // Maps from the id of a response to the MessageReceiver that handles the |
| 112 // response. |
111 typedef std::map<uint64_t, MessageReceiver*> ResponderMap; | 113 typedef std::map<uint64_t, MessageReceiver*> ResponderMap; |
112 | 114 |
113 class HandleIncomingMessageThunk : public MessageReceiver { | 115 class HandleIncomingMessageThunk : public MessageReceiver { |
114 public: | 116 public: |
115 HandleIncomingMessageThunk(Router* router); | 117 HandleIncomingMessageThunk(Router* router); |
116 ~HandleIncomingMessageThunk() override; | 118 ~HandleIncomingMessageThunk() override; |
117 | 119 |
118 // MessageReceiver implementation: | 120 // MessageReceiver implementation: |
119 bool Accept(Message* message) override; | 121 bool Accept(Message* message) override; |
120 | 122 |
121 private: | 123 private: |
122 Router* router_; | 124 Router* router_; |
123 }; | 125 }; |
124 | 126 |
125 bool HandleIncomingMessage(Message* message); | 127 bool HandleIncomingMessage(Message* message); |
126 | 128 |
127 HandleIncomingMessageThunk thunk_; | 129 HandleIncomingMessageThunk thunk_; |
128 FilterChain filters_; | 130 FilterChain filters_; |
129 Connector connector_; | 131 Connector connector_; |
130 SharedData<Router*> weak_self_; | |
131 MessageReceiverWithResponderStatus* incoming_receiver_; | 132 MessageReceiverWithResponderStatus* incoming_receiver_; |
132 // Maps from the id of a response to the MessageReceiver that handles the | 133 ResponderMap async_responders_; |
133 // response. | 134 ResponderMap sync_responders_; |
134 ResponderMap responders_; | |
135 uint64_t next_request_id_; | 135 uint64_t next_request_id_; |
136 bool testing_mode_; | 136 bool testing_mode_; |
137 base::ThreadChecker thread_checker_; | 137 base::ThreadChecker thread_checker_; |
| 138 base::WeakPtrFactory<Router> weak_factory_; |
138 }; | 139 }; |
139 | 140 |
140 } // namespace internal | 141 } // namespace internal |
141 } // namespace mojo | 142 } // namespace mojo |
142 | 143 |
143 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | 144 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ |
OLD | NEW |