[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #include "ipc/ipc_sync_channel.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 9 | |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 10 | #include <utility> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 11 | |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 14 | #include "base/location.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/logging.h" |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 16 | #include "base/macros.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 17 | #include "base/memory/ptr_util.h" |
fdoray | 6d056ff | 2016-07-04 21:56:42 | [diff] [blame] | 18 | #include "base/run_loop.h" |
Gabriel Charette | 1452023 | 2018-04-30 23:27:22 | [diff] [blame] | 19 | #include "base/sequenced_task_runner.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 20 | #include "base/synchronization/waitable_event.h" |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 21 | #include "base/threading/thread_local.h" |
gab | f08ccc0 | 2016-05-11 18:51:11 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
primiano | 7182d7b | 2015-01-30 18:02:03 | [diff] [blame] | 23 | #include "base/trace_event/trace_event.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 24 | #include "ipc/ipc_channel_factory.h" |
[email protected] | 60b2c61f | 2012-08-22 22:39:57 | [diff] [blame] | 25 | #include "ipc/ipc_logging.h" |
| 26 | #include "ipc/ipc_message_macros.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 27 | #include "ipc/ipc_sync_message.h" |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 28 | #include "mojo/public/cpp/bindings/sync_event_watcher.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 30 | using base::WaitableEvent; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | |
| 32 | namespace IPC { |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
| 36 | // A generic callback used when watching handles synchronously. Sets |*signal| |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 37 | // to true. |
| 38 | void OnEventReady(bool* signal) { |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 39 | *signal = true; |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 40 | } |
| 41 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 42 | base::LazyInstance<std::unique_ptr<base::WaitableEvent>>::Leaky |
| 43 | g_pump_messages_event = LAZY_INSTANCE_INITIALIZER; |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 44 | |
| 45 | } // namespace |
| 46 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | // When we're blocked in a Send(), we need to process incoming synchronous |
| 48 | // messages right away because it could be blocking our reply (either |
| 49 | // directly from the same object we're calling, or indirectly through one or |
| 50 | // more other channels). That means that in SyncContext's OnMessageReceived, |
| 51 | // we need to process sync message right away if we're blocked. However a |
| 52 | // simple check isn't sufficient, because the listener thread can be in the |
| 53 | // process of calling Send. |
| 54 | // To work around this, when SyncChannel filters a sync message, it sets |
| 55 | // an event that the listener thread waits on during its Send() call. This |
| 56 | // allows us to dispatch incoming sync messages when blocked. The race |
| 57 | // condition is handled because if Send is in the process of being called, it |
| 58 | // will check the event. In case the listener thread isn't sending a message, |
| 59 | // we queue a task on the listener thread to dispatch the received messages. |
| 60 | // The messages are stored in this queue object that's shared among all |
| 61 | // SyncChannel objects on the same thread (since one object can receive a |
| 62 | // sync message while another one is blocked). |
| 63 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | class SyncChannel::ReceivedSyncMsgQueue : |
| 65 | public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> { |
| 66 | public: |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 67 | // SyncChannel::WaitForReplyWithNestedMessageLoop may be re-entered, i.e. we |
| 68 | // may nest waiting message loops arbitrarily deep on the SyncChannel's |
| 69 | // thread. Every such operation has a corresponding WaitableEvent to be |
| 70 | // watched which, when signalled for IPC completion, breaks out of the loop. |
| 71 | // A reference to the innermost (i.e. topmost) watcher is held in |
| 72 | // |ReceivedSyncMsgQueue::top_send_done_event_watcher_|. |
| 73 | // |
| 74 | // NestedSendDoneWatcher provides a simple scoper which is used by |
| 75 | // WaitForReplyWithNestedMessageLoop to begin watching a new local "send done" |
| 76 | // event, preserving the previous topmost state on the local stack until the |
| 77 | // new inner loop is broken. If yet another subsequent nested loop is started |
| 78 | // therein the process is repeated again in the new inner stack frame, and so |
| 79 | // on. |
| 80 | // |
| 81 | // When this object is destroyed on stack unwind, the previous topmost state |
| 82 | // is swapped back into |ReceivedSyncMsgQueue::top_send_done_event_watcher_|, |
| 83 | // and its watch is resumed immediately. |
| 84 | class NestedSendDoneWatcher { |
| 85 | public: |
| 86 | NestedSendDoneWatcher(SyncChannel::SyncContext* context, |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 87 | base::RunLoop* run_loop, |
| 88 | scoped_refptr<base::SequencedTaskRunner> task_runner) |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 89 | : sync_msg_queue_(context->received_sync_msgs()), |
| 90 | outer_state_(sync_msg_queue_->top_send_done_event_watcher_), |
| 91 | event_(context->GetSendDoneEvent()), |
| 92 | callback_( |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 93 | base::BindOnce(&SyncChannel::SyncContext::OnSendDoneEventSignaled, |
| 94 | context, |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 95 | run_loop)), |
| 96 | task_runner_(std::move(task_runner)) { |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 97 | sync_msg_queue_->top_send_done_event_watcher_ = this; |
| 98 | if (outer_state_) |
| 99 | outer_state_->StopWatching(); |
| 100 | StartWatching(); |
| 101 | } |
| 102 | |
| 103 | ~NestedSendDoneWatcher() { |
| 104 | sync_msg_queue_->top_send_done_event_watcher_ = outer_state_; |
| 105 | if (outer_state_) |
| 106 | outer_state_->StartWatching(); |
| 107 | } |
| 108 | |
| 109 | private: |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 110 | void Run(WaitableEvent* event) { |
| 111 | DCHECK(callback_); |
| 112 | std::move(callback_).Run(event); |
| 113 | } |
| 114 | |
| 115 | void StartWatching() { |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 116 | watcher_.StartWatching( |
| 117 | event_, |
| 118 | base::BindOnce(&NestedSendDoneWatcher::Run, base::Unretained(this)), |
| 119 | task_runner_); |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 120 | } |
| 121 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 122 | void StopWatching() { watcher_.StopWatching(); } |
| 123 | |
| 124 | ReceivedSyncMsgQueue* const sync_msg_queue_; |
| 125 | NestedSendDoneWatcher* const outer_state_; |
| 126 | |
| 127 | base::WaitableEvent* const event_; |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 128 | base::WaitableEventWatcher::EventCallback callback_; |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 129 | base::WaitableEventWatcher watcher_; |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 130 | scoped_refptr<base::SequencedTaskRunner> task_runner_; |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 131 | |
| 132 | DISALLOW_COPY_AND_ASSIGN(NestedSendDoneWatcher); |
| 133 | }; |
| 134 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 135 | // Returns the ReceivedSyncMsgQueue instance for this thread, creating one |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 136 | // if necessary. Call RemoveContext on the same thread when done. |
| 137 | static ReceivedSyncMsgQueue* AddContext() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 138 | // We want one ReceivedSyncMsgQueue per listener thread (i.e. since multiple |
| 139 | // SyncChannel objects can block the same thread). |
| 140 | ReceivedSyncMsgQueue* rv = lazy_tls_ptr_.Pointer()->Get(); |
| 141 | if (!rv) { |
| 142 | rv = new ReceivedSyncMsgQueue(); |
| 143 | ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(rv); |
| 144 | } |
| 145 | rv->listener_count_++; |
| 146 | return rv; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | } |
| 148 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 149 | // Prevents messages from being dispatched immediately when the dispatch event |
| 150 | // is signaled. Instead, |*dispatch_flag| will be set. |
| 151 | void BlockDispatch(bool* dispatch_flag) { dispatch_flag_ = dispatch_flag; } |
| 152 | |
| 153 | // Allows messages to be dispatched immediately when the dispatch event is |
| 154 | // signaled. |
| 155 | void UnblockDispatch() { dispatch_flag_ = nullptr; } |
| 156 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | // Called on IPC thread when a synchronous message or reply arrives. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 158 | void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | bool was_task_pending; |
| 160 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 161 | base::AutoLock auto_lock(message_lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | |
| 163 | was_task_pending = task_pending_; |
| 164 | task_pending_ = true; |
| 165 | |
| 166 | // We set the event in case the listener thread is blocked (or is about |
| 167 | // to). In case it's not, the PostTask dispatches the messages. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 168 | message_queue_.push_back(QueuedMessage(new Message(msg), context)); |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 169 | message_queue_version_++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 172 | dispatch_event_.Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 173 | if (!was_task_pending) { |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 174 | listener_task_runner_->PostTask( |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 175 | FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchMessagesTask, |
vmpstr | a34d1132 | 2016-03-21 20:28:47 | [diff] [blame] | 176 | this, base::RetainedRef(context))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | void QueueReply(const Message &msg, SyncChannel::SyncContext* context) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 181 | received_replies_.push_back(QueuedMessage(new Message(msg), context)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 184 | // Called on the listener's thread to process any queues synchronous |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | // messages. |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 186 | void DispatchMessagesTask(SyncContext* context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 188 | base::AutoLock auto_lock(message_lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | task_pending_ = false; |
| 190 | } |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 191 | context->DispatchMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | } |
| 193 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 194 | // Dispatches any queued incoming sync messages. If |dispatching_context| is |
| 195 | // not null, messages which target a restricted dispatch channel will only be |
| 196 | // dispatched if |dispatching_context| belongs to the same restricted dispatch |
| 197 | // group as that channel. If |dispatching_context| is null, all queued |
| 198 | // messages are dispatched. |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 199 | void DispatchMessages(SyncContext* dispatching_context) { |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 200 | bool first_time = true; |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 201 | uint32_t expected_version = 0; |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 202 | SyncMessageQueue::iterator it; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | while (true) { |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 204 | Message* message = nullptr; |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 205 | scoped_refptr<SyncChannel::SyncContext> context; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 207 | base::AutoLock auto_lock(message_lock_); |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 208 | if (first_time || message_queue_version_ != expected_version) { |
| 209 | it = message_queue_.begin(); |
| 210 | first_time = false; |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 211 | } |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 212 | for (; it != message_queue_.end(); it++) { |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 213 | int message_group = it->context->restrict_dispatch_group(); |
Sam McNally | ae42d32 | 2018-01-09 00:47:55 | [diff] [blame] | 214 | if (message_group == kRestrictDispatchGroup_None || |
| 215 | (dispatching_context && |
| 216 | message_group == |
| 217 | dispatching_context->restrict_dispatch_group())) { |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 218 | message = it->message; |
| 219 | context = it->context; |
| 220 | it = message_queue_.erase(it); |
| 221 | message_queue_version_++; |
| 222 | expected_version = message_queue_version_; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 228 | if (message == nullptr) |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 229 | break; |
| 230 | context->OnDispatchMessage(*message); |
| 231 | delete message; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | // SyncChannel calls this in its destructor. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 236 | void RemoveContext(SyncContext* context) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 237 | base::AutoLock auto_lock(message_lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 239 | SyncMessageQueue::iterator iter = message_queue_.begin(); |
| 240 | while (iter != message_queue_.end()) { |
[email protected] | 1757164 | 2013-06-01 04:11:27 | [diff] [blame] | 241 | if (iter->context.get() == context) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 242 | delete iter->message; |
| 243 | iter = message_queue_.erase(iter); |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 244 | message_queue_version_++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | } else { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 246 | iter++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 249 | |
| 250 | if (--listener_count_ == 0) { |
| 251 | DCHECK(lazy_tls_ptr_.Pointer()->Get()); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 252 | lazy_tls_ptr_.Pointer()->Set(nullptr); |
| 253 | sync_dispatch_watcher_.reset(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 254 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | } |
| 256 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 257 | base::WaitableEvent* dispatch_event() { return &dispatch_event_; } |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 258 | base::SingleThreadTaskRunner* listener_task_runner() { |
[email protected] | 1757164 | 2013-06-01 04:11:27 | [diff] [blame] | 259 | return listener_task_runner_.get(); |
[email protected] | 92bf906 | 2011-05-02 18:00:49 | [diff] [blame] | 260 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 261 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 262 | // Holds a pointer to the per-thread ReceivedSyncMsgQueue object. |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 263 | static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue>>:: |
| 264 | DestructorAtExit lazy_tls_ptr_; |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 265 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 266 | // Called on the ipc thread to check if we can unblock any current Send() |
| 267 | // calls based on a queued reply. |
| 268 | void DispatchReplies() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | for (size_t i = 0; i < received_replies_.size(); ++i) { |
| 270 | Message* message = received_replies_[i].message; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 271 | if (received_replies_[i].context->TryToUnblockListener(message)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 272 | delete message; |
| 273 | received_replies_.erase(received_replies_.begin() + i); |
| 274 | return; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 279 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 280 | friend class base::RefCountedThreadSafe<ReceivedSyncMsgQueue>; |
| 281 | |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 282 | // See the comment in SyncChannel::SyncChannel for why this event is created |
| 283 | // as manual reset. |
gab | 90c2c5c | 2016-06-01 20:34:06 | [diff] [blame] | 284 | ReceivedSyncMsgQueue() |
| 285 | : message_queue_version_(0), |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 286 | dispatch_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 287 | base::WaitableEvent::InitialState::NOT_SIGNALED), |
gab | 90c2c5c | 2016-06-01 20:34:06 | [diff] [blame] | 288 | listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 289 | sync_dispatch_watcher_(std::make_unique<mojo::SyncEventWatcher>( |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 290 | &dispatch_event_, |
| 291 | base::Bind(&ReceivedSyncMsgQueue::OnDispatchEventReady, |
| 292 | base::Unretained(this)))) { |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 293 | sync_dispatch_watcher_->AllowWokenUpBySyncWatchOnSameThread(); |
| 294 | } |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 295 | |
Chris Watkins | 2d879af | 2017-11-30 02:11:59 | [diff] [blame] | 296 | ~ReceivedSyncMsgQueue() = default; |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 297 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 298 | void OnDispatchEventReady() { |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 299 | if (dispatch_flag_) { |
| 300 | *dispatch_flag_ = true; |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | // We were woken up during a sync wait, but no specific SyncChannel is |
| 305 | // currently waiting. i.e., some other Mojo interface on this thread is |
| 306 | // waiting for a response. Since we don't support anything analogous to |
| 307 | // restricted dispatch on Mojo interfaces, in this case it's safe to |
| 308 | // dispatch sync messages for any context. |
| 309 | DispatchMessages(nullptr); |
| 310 | } |
| 311 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 312 | // Holds information about a queued synchronous message or reply. |
| 313 | struct QueuedMessage { |
| 314 | QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 315 | Message* message; |
| 316 | scoped_refptr<SyncChannel::SyncContext> context; |
| 317 | }; |
| 318 | |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 319 | typedef std::list<QueuedMessage> SyncMessageQueue; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 320 | SyncMessageQueue message_queue_; |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 321 | |
| 322 | // Used to signal DispatchMessages to rescan |
| 323 | uint32_t message_queue_version_ = 0; |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 324 | |
| 325 | std::vector<QueuedMessage> received_replies_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 326 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 327 | // Signaled when we get a synchronous message that we must respond to, as the |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 328 | // sender needs its reply before it can reply to our original synchronous |
| 329 | // message. |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 330 | base::WaitableEvent dispatch_event_; |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 331 | scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 332 | base::Lock message_lock_; |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 333 | bool task_pending_ = false; |
| 334 | int listener_count_ = 0; |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 335 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 336 | // The current NestedSendDoneWatcher for this thread, if we're currently |
| 337 | // in a SyncChannel::WaitForReplyWithNestedMessageLoop. See |
| 338 | // NestedSendDoneWatcher comments for more details. |
| 339 | NestedSendDoneWatcher* top_send_done_event_watcher_ = nullptr; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 340 | |
| 341 | // If not null, the address of a flag to set when the dispatch event signals, |
| 342 | // in lieu of actually dispatching messages. This is used by |
| 343 | // SyncChannel::WaitForReply to restrict the scope of queued messages we're |
| 344 | // allowed to process while it's waiting. |
| 345 | bool* dispatch_flag_ = nullptr; |
| 346 | |
| 347 | // Watches |dispatch_event_| during all sync handle watches on this thread. |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 348 | std::unique_ptr<mojo::SyncEventWatcher> sync_dispatch_watcher_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 349 | }; |
| 350 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 351 | base::LazyInstance<base::ThreadLocalPointer< |
| 352 | SyncChannel::ReceivedSyncMsgQueue>>::DestructorAtExit |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 353 | SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_ = |
| 354 | LAZY_INSTANCE_INITIALIZER; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 355 | |
| 356 | SyncChannel::SyncContext::SyncContext( |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 357 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 358 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 359 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 360 | WaitableEvent* shutdown_event) |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 361 | : ChannelProxy::Context(listener, ipc_task_runner, listener_task_runner), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 362 | received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()), |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 363 | shutdown_event_(shutdown_event), |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 364 | restrict_dispatch_group_(kRestrictDispatchGroup_None) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 365 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 366 | void SyncChannel::SyncContext::OnSendDoneEventSignaled( |
| 367 | base::RunLoop* nested_loop, |
| 368 | base::WaitableEvent* event) { |
| 369 | DCHECK_EQ(GetSendDoneEvent(), event); |
| 370 | nested_loop->Quit(); |
| 371 | } |
| 372 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | SyncChannel::SyncContext::~SyncContext() { |
| 374 | while (!deserializers_.empty()) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 375 | Pop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // Adds information about an outgoing sync message to the context so that |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 379 | // we know how to deserialize the reply. Returns |true| if the message was added |
| 380 | // to the context or |false| if it was rejected (e.g. due to shutdown.) |
| 381 | bool SyncChannel::SyncContext::Push(SyncMessage* sync_msg) { |
[email protected] | 4a180a5 | 2011-04-15 19:07:43 | [diff] [blame] | 382 | // Create the tracking information for this message. This object is stored |
| 383 | // by value since all members are pointers that are cheap to copy. These |
| 384 | // pointers are cleaned up in the Pop() function. |
| 385 | // |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 386 | // The event is created as manual reset because in between Signal and |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 387 | // OnObjectSignalled, another Send can happen which would stop the watcher |
| 388 | // from being called. The event would get watched later, when the nested |
| 389 | // Send completes, so the event will need to remain set. |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 390 | base::AutoLock auto_lock(deserializers_lock_); |
| 391 | if (reject_new_deserializers_) |
| 392 | return false; |
gab | 90c2c5c | 2016-06-01 20:34:06 | [diff] [blame] | 393 | PendingSyncMsg pending( |
| 394 | SyncMessage::GetMessageId(*sync_msg), sync_msg->GetReplyDeserializer(), |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 395 | new base::WaitableEvent(base::WaitableEvent::ResetPolicy::MANUAL, |
| 396 | base::WaitableEvent::InitialState::NOT_SIGNALED)); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 397 | deserializers_.push_back(pending); |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 398 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | } |
| 400 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 401 | bool SyncChannel::SyncContext::Pop() { |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 402 | bool result; |
| 403 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 404 | base::AutoLock auto_lock(deserializers_lock_); |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 405 | PendingSyncMsg msg = deserializers_.back(); |
| 406 | delete msg.deserializer; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 407 | delete msg.done_event; |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 408 | msg.done_event = nullptr; |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 409 | deserializers_.pop_back(); |
| 410 | result = msg.send_result; |
| 411 | } |
| 412 | |
| 413 | // We got a reply to a synchronous Send() call that's blocking the listener |
| 414 | // thread. However, further down the call stack there could be another |
| 415 | // blocking Send() call, whose reply we received after we made this last |
| 416 | // Send() call. So check if we have any queued replies available that |
| 417 | // can now unblock the listener thread. |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 418 | ipc_task_runner()->PostTask( |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 419 | FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchReplies, |
tzik | 07cace4 | 2016-09-01 04:21:25 | [diff] [blame] | 420 | received_sync_msgs_)); |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 421 | |
| 422 | return result; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 423 | } |
| 424 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 425 | base::WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 426 | base::AutoLock auto_lock(deserializers_lock_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 427 | return deserializers_.back().done_event; |
| 428 | } |
| 429 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 430 | base::WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 431 | return received_sync_msgs_->dispatch_event(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void SyncChannel::SyncContext::DispatchMessages() { |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 435 | received_sync_msgs_->DispatchMessages(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 436 | } |
| 437 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 438 | bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 439 | base::AutoLock auto_lock(deserializers_lock_); |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 440 | if (deserializers_.empty() || |
| 441 | !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) { |
| 442 | return false; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 443 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 445 | if (!msg->is_reply_error()) { |
[email protected] | 211142cd | 2012-08-13 09:41:19 | [diff] [blame] | 446 | bool send_result = deserializers_.back().deserializer-> |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 447 | SerializeOutputParameters(*msg); |
[email protected] | 211142cd | 2012-08-13 09:41:19 | [diff] [blame] | 448 | deserializers_.back().send_result = send_result; |
bauerb | 3e9be73 | 2015-11-03 18:17:47 | [diff] [blame] | 449 | DVLOG_IF(1, !send_result) << "Couldn't deserialize reply message"; |
[email protected] | 211142cd | 2012-08-13 09:41:19 | [diff] [blame] | 450 | } else { |
bauerb | 3e9be73 | 2015-11-03 18:17:47 | [diff] [blame] | 451 | DVLOG(1) << "Received error reply"; |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 452 | } |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 453 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 454 | base::WaitableEvent* done_event = deserializers_.back().done_event; |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 455 | TRACE_EVENT_FLOW_BEGIN0( |
| 456 | TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 457 | "SyncChannel::SyncContext::TryToUnblockListener", done_event); |
| 458 | |
| 459 | done_event->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 460 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 461 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 464 | void SyncChannel::SyncContext::Clear() { |
| 465 | CancelPendingSends(); |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 466 | received_sync_msgs_->RemoveContext(this); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 467 | Context::Clear(); |
| 468 | } |
| 469 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 470 | bool SyncChannel::SyncContext::OnMessageReceived(const Message& msg) { |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 471 | // Give the filters a chance at processing this message. |
| 472 | if (TryFilters(msg)) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 473 | return true; |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 474 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 475 | if (TryToUnblockListener(&msg)) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 476 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 477 | |
[email protected] | 9134cce6d | 2012-04-10 20:07:53 | [diff] [blame] | 478 | if (msg.is_reply()) { |
| 479 | received_sync_msgs_->QueueReply(msg, this); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 480 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 481 | } |
| 482 | |
[email protected] | 9134cce6d | 2012-04-10 20:07:53 | [diff] [blame] | 483 | if (msg.should_unblock()) { |
| 484 | received_sync_msgs_->QueueMessage(msg, this); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 485 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 486 | } |
| 487 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 488 | return Context::OnMessageReceivedNoFilter(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 489 | } |
| 490 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 491 | void SyncChannel::SyncContext::OnChannelError() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 492 | CancelPendingSends(); |
[email protected] | a4f82270 | 2009-02-06 00:44:53 | [diff] [blame] | 493 | shutdown_watcher_.StopWatching(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | Context::OnChannelError(); |
| 495 | } |
| 496 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 497 | void SyncChannel::SyncContext::OnChannelOpened() { |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 498 | shutdown_watcher_.StartWatching( |
| 499 | shutdown_event_, |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 500 | base::Bind(&SyncChannel::SyncContext::OnShutdownEventSignaled, |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 501 | base::Unretained(this)), |
Sergey Kuznetsov | 25cb493 | 2018-02-15 18:29:30 | [diff] [blame] | 502 | base::SequencedTaskRunnerHandle::Get()); |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 503 | Context::OnChannelOpened(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 506 | void SyncChannel::SyncContext::OnChannelClosed() { |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 507 | CancelPendingSends(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 508 | shutdown_watcher_.StopWatching(); |
| 509 | Context::OnChannelClosed(); |
| 510 | } |
| 511 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 512 | void SyncChannel::SyncContext::CancelPendingSends() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 513 | base::AutoLock auto_lock(deserializers_lock_); |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 514 | reject_new_deserializers_ = true; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 515 | PendingSyncMessageQueue::iterator iter; |
bauerb | 3e9be73 | 2015-11-03 18:17:47 | [diff] [blame] | 516 | DVLOG(1) << "Canceling pending sends"; |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 517 | for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) { |
| 518 | TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 519 | "SyncChannel::SyncContext::CancelPendingSends", |
| 520 | iter->done_event); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 521 | iter->done_event->Signal(); |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 522 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 523 | } |
| 524 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 525 | void SyncChannel::SyncContext::OnShutdownEventSignaled(WaitableEvent* event) { |
| 526 | DCHECK_EQ(event, shutdown_event_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 527 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 528 | // Process shut down before we can get a reply to a synchronous message. |
| 529 | // Cancel pending Send calls, which will end up setting the send done event. |
| 530 | CancelPendingSends(); |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 531 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 532 | |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 533 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 534 | std::unique_ptr<SyncChannel> SyncChannel::Create( |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 535 | const IPC::ChannelHandle& channel_handle, |
[email protected] | 3b0e466 | 2014-06-02 20:29:30 | [diff] [blame] | 536 | Channel::Mode mode, |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 537 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 538 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 539 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner, |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 540 | bool create_pipe_now, |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 541 | base::WaitableEvent* shutdown_event) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 542 | std::unique_ptr<SyncChannel> channel = |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 543 | Create(listener, ipc_task_runner, listener_task_runner, shutdown_event); |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 544 | channel->Init(channel_handle, mode, create_pipe_now); |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 545 | return channel; |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 549 | std::unique_ptr<SyncChannel> SyncChannel::Create( |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 550 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 551 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 552 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner, |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 553 | WaitableEvent* shutdown_event) { |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 554 | return base::WrapUnique(new SyncChannel( |
| 555 | listener, ipc_task_runner, listener_task_runner, shutdown_event)); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | SyncChannel::SyncChannel( |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 559 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 560 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 561 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner, |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 562 | WaitableEvent* shutdown_event) |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 563 | : ChannelProxy(new SyncContext(listener, |
| 564 | ipc_task_runner, |
| 565 | listener_task_runner, |
| 566 | shutdown_event)), |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 567 | sync_handle_registry_(mojo::SyncHandleRegistry::current()) { |
[email protected] | 1f9cf47 | 2014-04-17 05:07:18 | [diff] [blame] | 568 | // The current (listener) thread must be distinct from the IPC thread, or else |
| 569 | // sending synchronous messages will deadlock. |
dcheng | ff04843f | 2014-09-03 18:01:16 | [diff] [blame] | 570 | DCHECK_NE(ipc_task_runner.get(), base::ThreadTaskRunnerHandle::Get().get()); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 571 | StartWatching(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 572 | } |
| 573 | |
Chris Watkins | 2d879af | 2017-11-30 02:11:59 | [diff] [blame] | 574 | SyncChannel::~SyncChannel() = default; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 575 | |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 576 | void SyncChannel::SetRestrictDispatchChannelGroup(int group) { |
| 577 | sync_context()->set_restrict_dispatch_group(group); |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 578 | } |
| 579 | |
rockot | ac64ae92f | 2015-08-06 00:32:29 | [diff] [blame] | 580 | scoped_refptr<SyncMessageFilter> SyncChannel::CreateSyncMessageFilter() { |
| 581 | scoped_refptr<SyncMessageFilter> filter = new SyncMessageFilter( |
rockot | b97e3d3 | 2016-09-16 17:39:03 | [diff] [blame] | 582 | sync_context()->shutdown_event()); |
rockot | ac64ae92f | 2015-08-06 00:32:29 | [diff] [blame] | 583 | AddFilter(filter.get()); |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 584 | if (!did_init()) |
| 585 | pre_init_sync_message_filters_.push_back(filter); |
rockot | ac64ae92f | 2015-08-06 00:32:29 | [diff] [blame] | 586 | return filter; |
| 587 | } |
| 588 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 589 | bool SyncChannel::Send(Message* message) { |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 590 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | 60b2c61f | 2012-08-22 22:39:57 | [diff] [blame] | 591 | std::string name; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 592 | Logging::GetInstance()->GetMessageText( |
| 593 | message->type(), &name, message, nullptr); |
[email protected] | d6cbf05 | 2014-05-02 21:29:24 | [diff] [blame] | 594 | TRACE_EVENT1("ipc", "SyncChannel::Send", "name", name); |
[email protected] | 60b2c61f | 2012-08-22 22:39:57 | [diff] [blame] | 595 | #else |
[email protected] | d6cbf05 | 2014-05-02 21:29:24 | [diff] [blame] | 596 | TRACE_EVENT2("ipc", "SyncChannel::Send", |
[email protected] | 60b2c61f | 2012-08-22 22:39:57 | [diff] [blame] | 597 | "class", IPC_MESSAGE_ID_CLASS(message->type()), |
| 598 | "line", IPC_MESSAGE_ID_LINE(message->type())); |
| 599 | #endif |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 600 | if (!message->is_sync()) { |
John Abd-El-Malek | d1d6f6a | 2017-07-18 20:01:00 | [diff] [blame] | 601 | ChannelProxy::SendInternal(message); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 602 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 603 | } |
| 604 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 605 | SyncMessage* sync_msg = static_cast<SyncMessage*>(message); |
| 606 | bool pump_messages = sync_msg->ShouldPumpMessages(); |
| 607 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 608 | // *this* might get deleted in WaitForReply. |
| 609 | scoped_refptr<SyncContext> context(sync_context()); |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 610 | if (!context->Push(sync_msg)) { |
| 611 | DVLOG(1) << "Channel is shutting down. Dropping sync message."; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 612 | delete message; |
| 613 | return false; |
| 614 | } |
| 615 | |
John Abd-El-Malek | d1d6f6a | 2017-07-18 20:01:00 | [diff] [blame] | 616 | ChannelProxy::SendInternal(message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 617 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 618 | // Wait for reply, or for any other incoming synchronous messages. |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 619 | // |this| might get deleted, so only call static functions at this point. |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 620 | scoped_refptr<mojo::SyncHandleRegistry> registry = sync_handle_registry_; |
| 621 | WaitForReply(registry.get(), context.get(), pump_messages); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 622 | |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 623 | TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 624 | "SyncChannel::Send", context->GetSendDoneEvent()); |
| 625 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 626 | return context->Pop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 627 | } |
| 628 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 629 | void SyncChannel::WaitForReply(mojo::SyncHandleRegistry* registry, |
| 630 | SyncContext* context, |
| 631 | bool pump_messages) { |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 632 | context->DispatchMessages(); |
rockot | 64e65de | 2016-06-21 20:17:49 | [diff] [blame] | 633 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 634 | base::WaitableEvent* pump_messages_event = nullptr; |
| 635 | if (pump_messages) { |
| 636 | if (!g_pump_messages_event.Get()) { |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 637 | g_pump_messages_event.Get() = std::make_unique<base::WaitableEvent>( |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 638 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 639 | base::WaitableEvent::InitialState::SIGNALED); |
| 640 | } |
| 641 | pump_messages_event = g_pump_messages_event.Get().get(); |
| 642 | } |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 643 | |
| 644 | while (true) { |
| 645 | bool dispatch = false; |
| 646 | bool send_done = false; |
| 647 | bool should_pump_messages = false; |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 648 | base::Closure on_send_done_callback = base::Bind(&OnEventReady, &send_done); |
| 649 | registry->RegisterEvent(context->GetSendDoneEvent(), on_send_done_callback); |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 650 | |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 651 | base::Closure on_pump_messages_callback; |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 652 | if (pump_messages_event) { |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 653 | on_pump_messages_callback = |
| 654 | base::Bind(&OnEventReady, &should_pump_messages); |
| 655 | registry->RegisterEvent(pump_messages_event, on_pump_messages_callback); |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | const bool* stop_flags[] = { &dispatch, &send_done, &should_pump_messages }; |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 659 | context->received_sync_msgs()->BlockDispatch(&dispatch); |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 660 | registry->Wait(stop_flags, 3); |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 661 | context->received_sync_msgs()->UnblockDispatch(); |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 662 | |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 663 | registry->UnregisterEvent(context->GetSendDoneEvent(), |
| 664 | on_send_done_callback); |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 665 | if (pump_messages_event) |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 666 | registry->UnregisterEvent(pump_messages_event, on_pump_messages_callback); |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 667 | |
| 668 | if (dispatch) { |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 669 | // We're waiting for a reply, but we received a blocking synchronous call. |
| 670 | // We must process it to avoid potential deadlocks. |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 671 | context->GetDispatchEvent()->Reset(); |
| 672 | context->DispatchMessages(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 673 | continue; |
| 674 | } |
| 675 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 676 | if (should_pump_messages) |
gab | 2998ee7 | 2017-05-05 16:23:50 | [diff] [blame] | 677 | WaitForReplyWithNestedMessageLoop(context); // Run a nested run loop. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 678 | |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 683 | void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) { |
Gabriel Charette | 43b8bf3 | 2017-11-08 20:24:43 | [diff] [blame] | 684 | base::RunLoop nested_loop(base::RunLoop::Type::kNestableTasksAllowed); |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 685 | ReceivedSyncMsgQueue::NestedSendDoneWatcher watcher( |
| 686 | context, &nested_loop, context->listener_task_runner()); |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 687 | nested_loop.Run(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 688 | } |
| 689 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 690 | void SyncChannel::OnDispatchEventSignaled(base::WaitableEvent* event) { |
| 691 | DCHECK_EQ(sync_context()->GetDispatchEvent(), event); |
yzshen | 1bd2370 | 2016-11-30 21:54:51 | [diff] [blame] | 692 | sync_context()->GetDispatchEvent()->Reset(); |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 693 | |
| 694 | StartWatching(); |
| 695 | |
| 696 | // NOTE: May delete |this|. |
yzshen | 1bd2370 | 2016-11-30 21:54:51 | [diff] [blame] | 697 | sync_context()->DispatchMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 698 | } |
| 699 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 700 | void SyncChannel::StartWatching() { |
rockot | 9abe09b | 2016-08-02 20:57:34 | [diff] [blame] | 701 | // |dispatch_watcher_| watches the event asynchronously, only dispatching |
| 702 | // messages once the listener thread is unblocked and pumping its task queue. |
| 703 | // The ReceivedSyncMsgQueue also watches this event and may dispatch |
| 704 | // immediately if woken up by a message which it's allowed to dispatch. |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 705 | dispatch_watcher_.StartWatching( |
| 706 | sync_context()->GetDispatchEvent(), |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 707 | base::BindOnce(&SyncChannel::OnDispatchEventSignaled, |
| 708 | base::Unretained(this)), |
| 709 | sync_context()->listener_task_runner()); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 710 | } |
| 711 | |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 712 | void SyncChannel::OnChannelInit() { |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 713 | pre_init_sync_message_filters_.clear(); |
| 714 | } |
| 715 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 716 | } // namespace IPC |