Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: mojo/public/cpp/bindings/lib/router.h

Issue 2532053004: Mojo C++ bindings: remove the single-threaded Router. (Closed)
Patch Set: . Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_
7
8 #include <stdint.h>
9
10 #include <map>
11 #include <memory>
12 #include <queue>
13
14 #include "base/callback.h"
15 #include "base/compiler_specific.h"
16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/single_thread_task_runner.h"
20 #include "base/threading/thread_checker.h"
21 #include "mojo/public/cpp/bindings/bindings_export.h"
22 #include "mojo/public/cpp/bindings/connection_error_callback.h"
23 #include "mojo/public/cpp/bindings/connector.h"
24 #include "mojo/public/cpp/bindings/filter_chain.h"
25 #include "mojo/public/cpp/bindings/lib/control_message_handler.h"
26 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h"
27 #include "mojo/public/cpp/bindings/message.h"
28
29 namespace mojo {
30 namespace internal {
31
32 // TODO(yzshen): Consider removing this class and use MultiplexRouter in all
33 // cases. crbug.com/594244
34 class MOJO_CPP_BINDINGS_EXPORT Router
35 : NON_EXPORTED_BASE(public MessageReceiverWithResponder) {
36 public:
37 Router(ScopedMessagePipeHandle message_pipe,
38 FilterChain filters,
39 bool expects_sync_requests,
40 scoped_refptr<base::SingleThreadTaskRunner> runner,
41 int interface_version);
42 ~Router() override;
43
44 // Sets the receiver to handle messages read from the message pipe that do
45 // not have the Message::kFlagIsResponse flag set.
46 void set_incoming_receiver(MessageReceiverWithResponderStatus* receiver) {
47 incoming_receiver_ = receiver;
48 }
49
50 // Sets the error handler to receive notifications when an error is
51 // encountered while reading from the pipe or waiting to read from the pipe.
52 void set_connection_error_handler(const base::Closure& error_handler) {
53 error_handler_ = error_handler;
54 error_with_reason_handler_.Reset();
55 }
56 void set_connection_error_with_reason_handler(
57 const ConnectionErrorWithReasonCallback& error_handler) {
58 error_with_reason_handler_ = error_handler;
59 error_handler_.Reset();
60 }
61
62 // Returns true if an error was encountered while reading from the pipe or
63 // waiting to read from the pipe.
64 bool encountered_error() const {
65 DCHECK(thread_checker_.CalledOnValidThread());
66 return encountered_error_;
67 }
68
69 // Is the router bound to a MessagePipe handle?
70 bool is_valid() const {
71 DCHECK(thread_checker_.CalledOnValidThread());
72 return connector_.is_valid();
73 }
74
75 void AddFilter(std::unique_ptr<MessageReceiver> filter);
76
77 // Please note that this method shouldn't be called unless it results from an
78 // explicit request of the user of bindings (e.g., the user sets an
79 // InterfacePtr to null or closes a Binding).
80 void CloseMessagePipe() {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 connector_.CloseMessagePipe();
83 }
84
85 ScopedMessagePipeHandle PassMessagePipe() {
86 DCHECK(thread_checker_.CalledOnValidThread());
87 return connector_.PassMessagePipe();
88 }
89
90 void RaiseError() {
91 DCHECK(thread_checker_.CalledOnValidThread());
92 connector_.RaiseError();
93 }
94
95 // MessageReceiver implementation:
96 bool Accept(Message* message) override;
97 bool AcceptWithResponder(Message* message,
98 MessageReceiver* responder) override;
99
100 // Blocks the current thread until the first incoming method call, i.e.,
101 // either a call to a client method or a callback method, or |deadline|.
102 bool WaitForIncomingMessage(MojoDeadline deadline) {
103 DCHECK(thread_checker_.CalledOnValidThread());
104 return connector_.WaitForIncomingMessage(deadline);
105 }
106
107 // See Binding for details of pause/resume.
108 // Note: This doesn't strictly pause incoming calls. If there are
109 // queued messages, they may be dispatched during pause.
110 void PauseIncomingMethodCallProcessing() {
111 DCHECK(thread_checker_.CalledOnValidThread());
112 connector_.PauseIncomingMethodCallProcessing();
113 }
114 void ResumeIncomingMethodCallProcessing() {
115 DCHECK(thread_checker_.CalledOnValidThread());
116 connector_.ResumeIncomingMethodCallProcessing();
117 }
118
119 // Sets this object to testing mode.
120 // In testing mode:
121 // - the object is more tolerant of unrecognized response messages;
122 // - the connector continues working after seeing errors from its incoming
123 // receiver.
124 void EnableTestingMode();
125
126 MessagePipeHandle handle() const { return connector_.handle(); }
127
128 // Returns true if this Router has any pending callbacks.
129 bool has_pending_responders() const {
130 DCHECK(thread_checker_.CalledOnValidThread());
131 return !async_responders_.empty() || !sync_responses_.empty();
132 }
133
134 ControlMessageProxy* control_message_proxy() {
135 return &control_message_proxy_;
136 }
137
138 bool SimulateReceivingMessageForTesting(Message* message) {
139 return filters_.Accept(message);
140 }
141
142 private:
143 // Maps from the id of a response to the MessageReceiver that handles the
144 // response.
145 using AsyncResponderMap =
146 std::map<uint64_t, std::unique_ptr<MessageReceiver>>;
147
148 struct SyncResponseInfo {
149 public:
150 explicit SyncResponseInfo(bool* in_response_received);
151 ~SyncResponseInfo();
152
153 Message response;
154
155 // Points to a stack-allocated variable.
156 bool* response_received;
157
158 private:
159 DISALLOW_COPY_AND_ASSIGN(SyncResponseInfo);
160 };
161
162 using SyncResponseMap = std::map<uint64_t, std::unique_ptr<SyncResponseInfo>>;
163
164 class HandleIncomingMessageThunk : public MessageReceiver {
165 public:
166 HandleIncomingMessageThunk(Router* router);
167 ~HandleIncomingMessageThunk() override;
168
169 // MessageReceiver implementation:
170 bool Accept(Message* message) override;
171
172 private:
173 Router* router_;
174 };
175
176 bool HandleIncomingMessage(Message* message);
177 void HandleQueuedMessages();
178 bool HandleMessageInternal(Message* message);
179
180 void OnConnectionError();
181
182 HandleIncomingMessageThunk thunk_;
183 FilterChain filters_;
184 Connector connector_;
185 MessageReceiverWithResponderStatus* incoming_receiver_;
186 AsyncResponderMap async_responders_;
187 SyncResponseMap sync_responses_;
188 uint64_t next_request_id_;
189 bool testing_mode_;
190 std::queue<Message> pending_messages_;
191 // Whether a task has been posted to trigger processing of
192 // |pending_messages_|.
193 bool pending_task_for_messages_;
194 bool encountered_error_;
195 base::Closure error_handler_;
196 ConnectionErrorWithReasonCallback error_with_reason_handler_;
197 ControlMessageProxy control_message_proxy_;
198 ControlMessageHandler control_message_handler_;
199 base::ThreadChecker thread_checker_;
200 base::WeakPtrFactory<Router> weak_factory_;
201 };
202
203 } // namespace internal
204 } // namespace mojo
205
206 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ROUTER_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/interface_ptr_state.h ('k') | mojo/public/cpp/bindings/lib/router.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698