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

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

Issue 2114523002: Move more Mojo bindings helpers out of internal namespace (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@group-controller
Patch Set: rebase Created 4 years, 5 months 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
« no previous file with comments | « mojo/public/cpp/bindings/lib/binding_state.h ('k') | mojo/public/cpp/bindings/lib/connector.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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_CONNECTOR_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
7
8 #include <memory>
9
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/threading/thread_checker.h"
15 #include "mojo/public/cpp/bindings/lib/sync_handle_watcher.h"
16 #include "mojo/public/cpp/bindings/message.h"
17 #include "mojo/public/cpp/system/core.h"
18 #include "mojo/public/cpp/system/watcher.h"
19
20 namespace base {
21 class Lock;
22 }
23
24 namespace mojo {
25 namespace internal {
26
27 // The Connector class is responsible for performing read/write operations on a
28 // MessagePipe. It writes messages it receives through the MessageReceiver
29 // interface that it subclasses, and it forwards messages it reads through the
30 // MessageReceiver interface assigned as its incoming receiver.
31 //
32 // NOTE:
33 // - MessagePipe I/O is non-blocking.
34 // - Sending messages can be configured to be thread safe (please see comments
35 // of the constructor). Other than that, the object should only be accessed
36 // on the creating thread.
37 class Connector : public MessageReceiver {
38 public:
39 enum ConnectorConfig {
40 // Connector::Accept() is only called from a single thread.
41 SINGLE_THREADED_SEND,
42 // Connector::Accept() is allowed to be called from multiple threads.
43 MULTI_THREADED_SEND
44 };
45
46 // The Connector takes ownership of |message_pipe|.
47 Connector(ScopedMessagePipeHandle message_pipe,
48 ConnectorConfig config,
49 scoped_refptr<base::SingleThreadTaskRunner> runner);
50 ~Connector() override;
51
52 // Sets the receiver to handle messages read from the message pipe. The
53 // Connector will read messages from the pipe regardless of whether or not an
54 // incoming receiver has been set.
55 void set_incoming_receiver(MessageReceiver* receiver) {
56 DCHECK(thread_checker_.CalledOnValidThread());
57 incoming_receiver_ = receiver;
58 }
59
60 // Errors from incoming receivers will force the connector into an error
61 // state, where no more messages will be processed. This method is used
62 // during testing to prevent that from happening.
63 void set_enforce_errors_from_incoming_receiver(bool enforce) {
64 DCHECK(thread_checker_.CalledOnValidThread());
65 enforce_errors_from_incoming_receiver_ = enforce;
66 }
67
68 // Sets the error handler to receive notifications when an error is
69 // encountered while reading from the pipe or waiting to read from the pipe.
70 void set_connection_error_handler(const base::Closure& error_handler) {
71 DCHECK(thread_checker_.CalledOnValidThread());
72 connection_error_handler_ = error_handler;
73 }
74
75 // Returns true if an error was encountered while reading from the pipe or
76 // waiting to read from the pipe.
77 bool encountered_error() const {
78 DCHECK(thread_checker_.CalledOnValidThread());
79 return error_;
80 }
81
82 // Closes the pipe. The connector is put into a quiescent state.
83 //
84 // Please note that this method shouldn't be called unless it results from an
85 // explicit request of the user of bindings (e.g., the user sets an
86 // InterfacePtr to null or closes a Binding).
87 void CloseMessagePipe();
88
89 // Releases the pipe. Connector is put into a quiescent state.
90 ScopedMessagePipeHandle PassMessagePipe();
91
92 // Enters the error state. The upper layer may do this for unrecoverable
93 // issues such as invalid messages are received. If a connection error handler
94 // has been set, it will be called asynchronously.
95 //
96 // It is a no-op if the connector is already in the error state or there isn't
97 // a bound message pipe. Otherwise, it closes the message pipe, which notifies
98 // the other end and also prevents potential danger (say, the caller raises
99 // an error because it believes the other end is malicious). In order to
100 // appear to the user that the connector still binds to a message pipe, it
101 // creates a new message pipe, closes one end and binds to the other.
102 void RaiseError();
103
104 // Is the connector bound to a MessagePipe handle?
105 bool is_valid() const {
106 DCHECK(thread_checker_.CalledOnValidThread());
107 return message_pipe_.is_valid();
108 }
109
110 // Waits for the next message on the pipe, blocking until one arrives,
111 // |deadline| elapses, or an error happens. Returns |true| if a message has
112 // been delivered, |false| otherwise.
113 bool WaitForIncomingMessage(MojoDeadline deadline);
114
115 // See Binding for details of pause/resume.
116 void PauseIncomingMethodCallProcessing();
117 void ResumeIncomingMethodCallProcessing();
118
119 // MessageReceiver implementation:
120 bool Accept(Message* message) override;
121
122 MessagePipeHandle handle() const {
123 DCHECK(thread_checker_.CalledOnValidThread());
124 return message_pipe_.get();
125 }
126
127 // Allows |message_pipe_| to be watched while others perform sync handle
128 // watching on the same thread. Please see comments of
129 // SyncHandleWatcher::AllowWokenUpBySyncWatchOnSameThread().
130 void AllowWokenUpBySyncWatchOnSameThread();
131
132 // Watches |message_pipe_| (as well as other handles registered to be watched
133 // together) synchronously.
134 // This method:
135 // - returns true when |should_stop| is set to true;
136 // - return false when any error occurs, including |message_pipe_| being
137 // closed.
138 bool SyncWatch(const bool* should_stop);
139
140 // Whether currently the control flow is inside the sync handle watcher
141 // callback.
142 bool during_sync_handle_watcher_callback() const {
143 return sync_handle_watcher_callback_count_ > 0;
144 }
145
146 base::SingleThreadTaskRunner* task_runner() const {
147 return task_runner_.get();
148 }
149
150 private:
151 // Callback of mojo::Watcher.
152 void OnWatcherHandleReady(MojoResult result);
153 // Callback of SyncHandleWatcher.
154 void OnSyncHandleWatcherHandleReady(MojoResult result);
155 void OnHandleReadyInternal(MojoResult result);
156
157 void WaitToReadMore();
158
159 // Returns false if |this| was destroyed during message dispatch.
160 WARN_UNUSED_RESULT bool ReadSingleMessage(MojoResult* read_result);
161
162 // |this| can be destroyed during message dispatch.
163 void ReadAllAvailableMessages();
164
165 // If |force_pipe_reset| is true, this method replaces the existing
166 // |message_pipe_| with a dummy message pipe handle (whose peer is closed).
167 // If |force_async_handler| is true, |connection_error_handler_| is called
168 // asynchronously.
169 void HandleError(bool force_pipe_reset, bool force_async_handler);
170
171 // Cancels any calls made to |waiter_|.
172 void CancelWait();
173
174 void EnsureSyncWatcherExists();
175
176 base::Closure connection_error_handler_;
177
178 ScopedMessagePipeHandle message_pipe_;
179 MessageReceiver* incoming_receiver_;
180
181 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
182 Watcher handle_watcher_;
183
184 bool error_;
185 bool drop_writes_;
186 bool enforce_errors_from_incoming_receiver_;
187
188 bool paused_;
189
190 // If sending messages is allowed from multiple threads, |lock_| is used to
191 // protect modifications to |message_pipe_| and |drop_writes_|.
192 std::unique_ptr<base::Lock> lock_;
193
194 std::unique_ptr<SyncHandleWatcher> sync_watcher_;
195 bool allow_woken_up_by_others_;
196 // If non-zero, currently the control flow is inside the sync handle watcher
197 // callback.
198 size_t sync_handle_watcher_callback_count_;
199
200 base::ThreadChecker thread_checker_;
201
202 // Create a single weak ptr and use it everywhere, to avoid the malloc/free
203 // cost of creating a new weak ptr whenever it is needed.
204 base::WeakPtr<Connector> weak_self_;
205 base::WeakPtrFactory<Connector> weak_factory_;
206
207 DISALLOW_COPY_AND_ASSIGN(Connector);
208 };
209
210 } // namespace internal
211 } // namespace mojo
212
213 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/binding_state.h ('k') | mojo/public/cpp/bindings/lib/connector.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698