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