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