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 <queue> | |
12 | 11 |
13 #include "base/macros.h" | 12 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
16 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
17 #include "mojo/public/cpp/bindings/callback.h" | 15 #include "mojo/public/cpp/bindings/callback.h" |
18 #include "mojo/public/cpp/bindings/lib/connector.h" | 16 #include "mojo/public/cpp/bindings/lib/connector.h" |
19 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 17 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
20 #include "mojo/public/cpp/environment/environment.h" | 18 #include "mojo/public/cpp/environment/environment.h" |
21 | 19 |
22 namespace mojo { | 20 namespace mojo { |
23 namespace internal { | 21 namespace internal { |
24 | 22 |
25 class Router : public MessageReceiverWithResponder { | 23 class Router : public MessageReceiverWithResponder { |
26 public: | 24 public: |
27 Router(ScopedMessagePipeHandle message_pipe, | 25 Router(ScopedMessagePipeHandle message_pipe, |
28 FilterChain filters, | 26 FilterChain filters, |
29 bool expects_sync_requests, | |
30 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); | 27 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); |
31 ~Router() override; | 28 ~Router() override; |
32 | 29 |
33 // Sets the receiver to handle messages read from the message pipe that do | 30 // Sets the receiver to handle messages read from the message pipe that do |
34 // not have the kMessageIsResponse flag set. | 31 // not have the kMessageIsResponse flag set. |
35 void set_incoming_receiver(MessageReceiverWithResponderStatus* receiver) { | 32 void set_incoming_receiver(MessageReceiverWithResponderStatus* receiver) { |
36 incoming_receiver_ = receiver; | 33 incoming_receiver_ = receiver; |
37 } | 34 } |
38 | 35 |
39 // Sets the error handler to receive notifications when an error is | 36 // Sets the error handler to receive notifications when an error is |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // - the object is more tolerant of unrecognized response messages; | 97 // - the object is more tolerant of unrecognized response messages; |
101 // - the connector continues working after seeing errors from its incoming | 98 // - the connector continues working after seeing errors from its incoming |
102 // receiver. | 99 // receiver. |
103 void EnableTestingMode(); | 100 void EnableTestingMode(); |
104 | 101 |
105 MessagePipeHandle handle() const { return connector_.handle(); } | 102 MessagePipeHandle handle() const { return connector_.handle(); } |
106 | 103 |
107 // Returns true if this Router has any pending callbacks. | 104 // Returns true if this Router has any pending callbacks. |
108 bool has_pending_responders() const { | 105 bool has_pending_responders() const { |
109 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
110 return !async_responders_.empty() || !sync_responses_.empty(); | 107 return !async_responders_.empty() || !sync_responders_.empty(); |
111 } | 108 } |
112 | 109 |
113 private: | 110 private: |
114 // Maps from the id of a response to the MessageReceiver that handles the | 111 // Maps from the id of a response to the MessageReceiver that handles the |
115 // response. | 112 // response. |
116 using AsyncResponderMap = std::map<uint64_t, scoped_ptr<MessageReceiver>>; | 113 typedef std::map<uint64_t, MessageReceiver*> ResponderMap; |
117 | |
118 struct SyncResponseInfo { | |
119 public: | |
120 explicit SyncResponseInfo(bool* in_response_received); | |
121 ~SyncResponseInfo(); | |
122 | |
123 scoped_ptr<Message> response; | |
124 | |
125 // Points to a stack-allocated variable. | |
126 bool* response_received; | |
127 | |
128 private: | |
129 DISALLOW_COPY_AND_ASSIGN(SyncResponseInfo); | |
130 }; | |
131 | |
132 using SyncResponseMap = std::map<uint64_t, scoped_ptr<SyncResponseInfo>>; | |
133 | 114 |
134 class HandleIncomingMessageThunk : public MessageReceiver { | 115 class HandleIncomingMessageThunk : public MessageReceiver { |
135 public: | 116 public: |
136 HandleIncomingMessageThunk(Router* router); | 117 HandleIncomingMessageThunk(Router* router); |
137 ~HandleIncomingMessageThunk() override; | 118 ~HandleIncomingMessageThunk() override; |
138 | 119 |
139 // MessageReceiver implementation: | 120 // MessageReceiver implementation: |
140 bool Accept(Message* message) override; | 121 bool Accept(Message* message) override; |
141 | 122 |
142 private: | 123 private: |
143 Router* router_; | 124 Router* router_; |
144 }; | 125 }; |
145 | 126 |
146 bool HandleIncomingMessage(Message* message); | 127 bool HandleIncomingMessage(Message* message); |
147 void HandleQueuedMessages(); | |
148 | |
149 bool HandleMessageInternal(Message* message); | |
150 | 128 |
151 HandleIncomingMessageThunk thunk_; | 129 HandleIncomingMessageThunk thunk_; |
152 FilterChain filters_; | 130 FilterChain filters_; |
153 Connector connector_; | 131 Connector connector_; |
154 MessageReceiverWithResponderStatus* incoming_receiver_; | 132 MessageReceiverWithResponderStatus* incoming_receiver_; |
155 AsyncResponderMap async_responders_; | 133 ResponderMap async_responders_; |
156 SyncResponseMap sync_responses_; | 134 ResponderMap sync_responders_; |
157 uint64_t next_request_id_; | 135 uint64_t next_request_id_; |
158 bool testing_mode_; | 136 bool testing_mode_; |
159 std::queue<scoped_ptr<Message>> pending_messages_; | |
160 // Whether a task has been posted to trigger processing of | |
161 // |pending_messages_|. | |
162 bool pending_task_for_messages_; | |
163 base::ThreadChecker thread_checker_; | 137 base::ThreadChecker thread_checker_; |
164 base::WeakPtrFactory<Router> weak_factory_; | 138 base::WeakPtrFactory<Router> weak_factory_; |
165 }; | 139 }; |
166 | 140 |
167 } // namespace internal | 141 } // namespace internal |
168 } // namespace mojo | 142 } // namespace mojo |
169 | 143 |
170 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ | 144 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_ |
OLD | NEW |