OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_CONNECTOR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/sequence_checker.h" |
15 #include "base/threading/thread_checker.h" | 15 #include "base/sequenced_task_runner.h" |
16 #include "mojo/public/cpp/bindings/bindings_export.h" | 16 #include "mojo/public/cpp/bindings/bindings_export.h" |
17 #include "mojo/public/cpp/bindings/message.h" | 17 #include "mojo/public/cpp/bindings/message.h" |
18 #include "mojo/public/cpp/bindings/sync_handle_watcher.h" | 18 #include "mojo/public/cpp/bindings/sync_handle_watcher.h" |
19 #include "mojo/public/cpp/system/core.h" | 19 #include "mojo/public/cpp/system/core.h" |
20 #include "mojo/public/cpp/system/watcher.h" | 20 #include "mojo/public/cpp/system/watcher.h" |
21 | 21 |
22 namespace base { | 22 namespace base { |
23 class Lock; | 23 class Lock; |
24 } | 24 } |
25 | 25 |
(...skipping 15 matching lines...) Expand all Loading... |
41 enum ConnectorConfig { | 41 enum ConnectorConfig { |
42 // Connector::Accept() is only called from a single thread. | 42 // Connector::Accept() is only called from a single thread. |
43 SINGLE_THREADED_SEND, | 43 SINGLE_THREADED_SEND, |
44 // Connector::Accept() is allowed to be called from multiple threads. | 44 // Connector::Accept() is allowed to be called from multiple threads. |
45 MULTI_THREADED_SEND | 45 MULTI_THREADED_SEND |
46 }; | 46 }; |
47 | 47 |
48 // The Connector takes ownership of |message_pipe|. | 48 // The Connector takes ownership of |message_pipe|. |
49 Connector(ScopedMessagePipeHandle message_pipe, | 49 Connector(ScopedMessagePipeHandle message_pipe, |
50 ConnectorConfig config, | 50 ConnectorConfig config, |
51 scoped_refptr<base::SingleThreadTaskRunner> runner); | 51 scoped_refptr<base::SequencedTaskRunner> runner); |
52 ~Connector() override; | 52 ~Connector() override; |
53 | 53 |
54 // Sets the receiver to handle messages read from the message pipe. The | 54 // Sets the receiver to handle messages read from the message pipe. The |
55 // Connector will read messages from the pipe regardless of whether or not an | 55 // Connector will read messages from the pipe regardless of whether or not an |
56 // incoming receiver has been set. | 56 // incoming receiver has been set. |
57 void set_incoming_receiver(MessageReceiver* receiver) { | 57 void set_incoming_receiver(MessageReceiver* receiver) { |
58 DCHECK(thread_checker_.CalledOnValidThread()); | 58 DCHECK(sequence_checker_.CalledOnValidSequence()); |
59 incoming_receiver_ = receiver; | 59 incoming_receiver_ = receiver; |
60 } | 60 } |
61 | 61 |
62 // Errors from incoming receivers will force the connector into an error | 62 // Errors from incoming receivers will force the connector into an error |
63 // state, where no more messages will be processed. This method is used | 63 // state, where no more messages will be processed. This method is used |
64 // during testing to prevent that from happening. | 64 // during testing to prevent that from happening. |
65 void set_enforce_errors_from_incoming_receiver(bool enforce) { | 65 void set_enforce_errors_from_incoming_receiver(bool enforce) { |
66 DCHECK(thread_checker_.CalledOnValidThread()); | 66 DCHECK(sequence_checker_.CalledOnValidSequence()); |
67 enforce_errors_from_incoming_receiver_ = enforce; | 67 enforce_errors_from_incoming_receiver_ = enforce; |
68 } | 68 } |
69 | 69 |
70 // Sets the error handler to receive notifications when an error is | 70 // Sets the error handler to receive notifications when an error is |
71 // encountered while reading from the pipe or waiting to read from the pipe. | 71 // encountered while reading from the pipe or waiting to read from the pipe. |
72 void set_connection_error_handler(const base::Closure& error_handler) { | 72 void set_connection_error_handler(const base::Closure& error_handler) { |
73 DCHECK(thread_checker_.CalledOnValidThread()); | 73 DCHECK(sequence_checker_.CalledOnValidSequence()); |
74 connection_error_handler_ = error_handler; | 74 connection_error_handler_ = error_handler; |
75 } | 75 } |
76 | 76 |
77 // Returns true if an error was encountered while reading from the pipe or | 77 // Returns true if an error was encountered while reading from the pipe or |
78 // waiting to read from the pipe. | 78 // waiting to read from the pipe. |
79 bool encountered_error() const { | 79 bool encountered_error() const { |
80 DCHECK(thread_checker_.CalledOnValidThread()); | 80 DCHECK(sequence_checker_.CalledOnValidSequence()); |
81 return error_; | 81 return error_; |
82 } | 82 } |
83 | 83 |
84 // Closes the pipe. The connector is put into a quiescent state. | 84 // Closes the pipe. The connector is put into a quiescent state. |
85 // | 85 // |
86 // Please note that this method shouldn't be called unless it results from an | 86 // Please note that this method shouldn't be called unless it results from an |
87 // explicit request of the user of bindings (e.g., the user sets an | 87 // explicit request of the user of bindings (e.g., the user sets an |
88 // InterfacePtr to null or closes a Binding). | 88 // InterfacePtr to null or closes a Binding). |
89 void CloseMessagePipe(); | 89 void CloseMessagePipe(); |
90 | 90 |
91 // Releases the pipe. Connector is put into a quiescent state. | 91 // Releases the pipe. Connector is put into a quiescent state. |
92 ScopedMessagePipeHandle PassMessagePipe(); | 92 ScopedMessagePipeHandle PassMessagePipe(); |
93 | 93 |
94 // Enters the error state. The upper layer may do this for unrecoverable | 94 // Enters the error state. The upper layer may do this for unrecoverable |
95 // issues such as invalid messages are received. If a connection error handler | 95 // issues such as invalid messages are received. If a connection error handler |
96 // has been set, it will be called asynchronously. | 96 // has been set, it will be called asynchronously. |
97 // | 97 // |
98 // It is a no-op if the connector is already in the error state or there isn't | 98 // It is a no-op if the connector is already in the error state or there isn't |
99 // a bound message pipe. Otherwise, it closes the message pipe, which notifies | 99 // a bound message pipe. Otherwise, it closes the message pipe, which notifies |
100 // the other end and also prevents potential danger (say, the caller raises | 100 // the other end and also prevents potential danger (say, the caller raises |
101 // an error because it believes the other end is malicious). In order to | 101 // an error because it believes the other end is malicious). In order to |
102 // appear to the user that the connector still binds to a message pipe, it | 102 // appear to the user that the connector still binds to a message pipe, it |
103 // creates a new message pipe, closes one end and binds to the other. | 103 // creates a new message pipe, closes one end and binds to the other. |
104 void RaiseError(); | 104 void RaiseError(); |
105 | 105 |
106 // Is the connector bound to a MessagePipe handle? | 106 // Is the connector bound to a MessagePipe handle? |
107 bool is_valid() const { | 107 bool is_valid() const { |
108 DCHECK(thread_checker_.CalledOnValidThread()); | 108 DCHECK(sequence_checker_.CalledOnValidSequence()); |
109 return message_pipe_.is_valid(); | 109 return message_pipe_.is_valid(); |
110 } | 110 } |
111 | 111 |
112 // Waits for the next message on the pipe, blocking until one arrives, | 112 // Waits for the next message on the pipe, blocking until one arrives, |
113 // |deadline| elapses, or an error happens. Returns |true| if a message has | 113 // |deadline| elapses, or an error happens. Returns |true| if a message has |
114 // been delivered, |false| otherwise. | 114 // been delivered, |false| otherwise. |
115 bool WaitForIncomingMessage(MojoDeadline deadline); | 115 bool WaitForIncomingMessage(MojoDeadline deadline); |
116 | 116 |
117 // See Binding for details of pause/resume. | 117 // See Binding for details of pause/resume. |
118 void PauseIncomingMethodCallProcessing(); | 118 void PauseIncomingMethodCallProcessing(); |
119 void ResumeIncomingMethodCallProcessing(); | 119 void ResumeIncomingMethodCallProcessing(); |
120 | 120 |
121 // MessageReceiver implementation: | 121 // MessageReceiver implementation: |
122 bool Accept(Message* message) override; | 122 bool Accept(Message* message) override; |
123 | 123 |
124 MessagePipeHandle handle() const { | 124 MessagePipeHandle handle() const { |
125 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(sequence_checker_.CalledOnValidSequence()); |
126 return message_pipe_.get(); | 126 return message_pipe_.get(); |
127 } | 127 } |
128 | 128 |
129 // Allows |message_pipe_| to be watched while others perform sync handle | 129 // Allows |message_pipe_| to be watched while others perform sync handle |
130 // watching on the same thread. Please see comments of | 130 // watching on the same thread. Please see comments of |
131 // SyncHandleWatcher::AllowWokenUpBySyncWatchOnSameThread(). | 131 // SyncHandleWatcher::AllowWokenUpBySyncWatchOnSameThread(). |
132 void AllowWokenUpBySyncWatchOnSameThread(); | 132 void AllowWokenUpBySyncWatchOnSameThread(); |
133 | 133 |
134 // Watches |message_pipe_| (as well as other handles registered to be watched | 134 // Watches |message_pipe_| (as well as other handles registered to be watched |
135 // together) synchronously. | 135 // together) synchronously. |
136 // This method: | 136 // This method: |
137 // - returns true when |should_stop| is set to true; | 137 // - returns true when |should_stop| is set to true; |
138 // - return false when any error occurs, including |message_pipe_| being | 138 // - return false when any error occurs, including |message_pipe_| being |
139 // closed. | 139 // closed. |
140 bool SyncWatch(const bool* should_stop); | 140 bool SyncWatch(const bool* should_stop); |
141 | 141 |
142 // Whether currently the control flow is inside the sync handle watcher | 142 // Whether currently the control flow is inside the sync handle watcher |
143 // callback. | 143 // callback. |
144 // It always returns false after CloseMessagePipe()/PassMessagePipe(). | 144 // It always returns false after CloseMessagePipe()/PassMessagePipe(). |
145 bool during_sync_handle_watcher_callback() const { | 145 bool during_sync_handle_watcher_callback() const { |
146 return sync_handle_watcher_callback_count_ > 0; | 146 return sync_handle_watcher_callback_count_ > 0; |
147 } | 147 } |
148 | 148 |
149 base::SingleThreadTaskRunner* task_runner() const { | 149 base::SequencedTaskRunner* task_runner() const { return task_runner_.get(); } |
150 return task_runner_.get(); | |
151 } | |
152 | 150 |
153 // Sets the tag used by the heap profiler. | 151 // Sets the tag used by the heap profiler. |
154 // |tag| must be a const string literal. | 152 // |tag| must be a const string literal. |
155 void SetWatcherHeapProfilerTag(const char* tag); | 153 void SetWatcherHeapProfilerTag(const char* tag); |
156 | 154 |
157 private: | 155 private: |
158 // Callback of mojo::Watcher. | 156 // Callback of mojo::Watcher. |
159 void OnWatcherHandleReady(MojoResult result); | 157 void OnWatcherHandleReady(MojoResult result); |
160 // Callback of SyncHandleWatcher. | 158 // Callback of SyncHandleWatcher. |
161 void OnSyncHandleWatcherHandleReady(MojoResult result); | 159 void OnSyncHandleWatcherHandleReady(MojoResult result); |
(...skipping 17 matching lines...) Expand all Loading... |
179 // Cancels any calls made to |waiter_|. | 177 // Cancels any calls made to |waiter_|. |
180 void CancelWait(); | 178 void CancelWait(); |
181 | 179 |
182 void EnsureSyncWatcherExists(); | 180 void EnsureSyncWatcherExists(); |
183 | 181 |
184 base::Closure connection_error_handler_; | 182 base::Closure connection_error_handler_; |
185 | 183 |
186 ScopedMessagePipeHandle message_pipe_; | 184 ScopedMessagePipeHandle message_pipe_; |
187 MessageReceiver* incoming_receiver_ = nullptr; | 185 MessageReceiver* incoming_receiver_ = nullptr; |
188 | 186 |
189 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 187 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
190 std::unique_ptr<Watcher> handle_watcher_; | 188 std::unique_ptr<Watcher> handle_watcher_; |
191 | 189 |
192 bool error_ = false; | 190 bool error_ = false; |
193 bool drop_writes_ = false; | 191 bool drop_writes_ = false; |
194 bool enforce_errors_from_incoming_receiver_ = true; | 192 bool enforce_errors_from_incoming_receiver_ = true; |
195 | 193 |
196 bool paused_ = false; | 194 bool paused_ = false; |
197 | 195 |
198 // If sending messages is allowed from multiple threads, |lock_| is used to | 196 // If sending messages is allowed from multiple threads, |lock_| is used to |
199 // protect modifications to |message_pipe_| and |drop_writes_|. | 197 // protect modifications to |message_pipe_| and |drop_writes_|. |
200 std::unique_ptr<base::Lock> lock_; | 198 std::unique_ptr<base::Lock> lock_; |
201 | 199 |
202 std::unique_ptr<SyncHandleWatcher> sync_watcher_; | 200 std::unique_ptr<SyncHandleWatcher> sync_watcher_; |
203 bool allow_woken_up_by_others_ = false; | 201 bool allow_woken_up_by_others_ = false; |
204 // If non-zero, currently the control flow is inside the sync handle watcher | 202 // If non-zero, currently the control flow is inside the sync handle watcher |
205 // callback. | 203 // callback. |
206 size_t sync_handle_watcher_callback_count_ = 0; | 204 size_t sync_handle_watcher_callback_count_ = 0; |
207 | 205 |
208 base::ThreadChecker thread_checker_; | 206 base::SequenceChecker sequence_checker_; |
209 | 207 |
210 base::Lock connected_lock_; | 208 base::Lock connected_lock_; |
211 bool connected_ = true; | 209 bool connected_ = true; |
212 | 210 |
213 // The tag used to track heap allocations that originated from a Watcher | 211 // The tag used to track heap allocations that originated from a Watcher |
214 // notification. | 212 // notification. |
215 const char* heap_profiler_tag_ = nullptr; | 213 const char* heap_profiler_tag_ = nullptr; |
216 | 214 |
217 // Create a single weak ptr and use it everywhere, to avoid the malloc/free | 215 // Create a single weak ptr and use it everywhere, to avoid the malloc/free |
218 // cost of creating a new weak ptr whenever it is needed. | 216 // cost of creating a new weak ptr whenever it is needed. |
219 // NOTE: This weak pointer is invalidated when the message pipe is closed or | 217 // NOTE: This weak pointer is invalidated when the message pipe is closed or |
220 // transferred (i.e., when |connected_| is set to false). | 218 // transferred (i.e., when |connected_| is set to false). |
221 base::WeakPtr<Connector> weak_self_; | 219 base::WeakPtr<Connector> weak_self_; |
222 base::WeakPtrFactory<Connector> weak_factory_; | 220 base::WeakPtrFactory<Connector> weak_factory_; |
223 | 221 |
224 DISALLOW_COPY_AND_ASSIGN(Connector); | 222 DISALLOW_COPY_AND_ASSIGN(Connector); |
225 }; | 223 }; |
226 | 224 |
227 } // namespace mojo | 225 } // namespace mojo |
228 | 226 |
229 #endif // MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ | 227 #endif // MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ |
OLD | NEW |