[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 | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 16 | #include "base/macros.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 17 | #include "base/memory/ptr_util.h" |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [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 | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 27 | #include "ipc/mojo_event.h" |
| 28 | #include "mojo/public/cpp/bindings/sync_handle_registry.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 30 | using base::TimeDelta; |
| 31 | using base::TimeTicks; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 32 | using base::WaitableEvent; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | |
| 34 | namespace IPC { |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 35 | |
| 36 | namespace { |
| 37 | |
| 38 | // A lazy thread-local Mojo Event which is always signaled. Used to wake up the |
| 39 | // sync waiter when a SyncMessage requires the MessageLoop to be pumped while |
| 40 | // waiting for a reply. This object is created lazily and ref-counted so it can |
| 41 | // be cleaned up when no longer in use. |
| 42 | class PumpMessagesEvent { |
| 43 | public: |
| 44 | // Acquires the event for this thread. Creates a new instance if necessary. |
| 45 | static PumpMessagesEvent* Acquire() { |
| 46 | PumpMessagesEvent* pump_messages_event = g_event_.Pointer()->Get(); |
| 47 | if (!pump_messages_event) { |
| 48 | pump_messages_event = new PumpMessagesEvent; |
| 49 | pump_messages_event->event_.Signal(); |
| 50 | g_event_.Pointer()->Set(pump_messages_event); |
| 51 | } |
| 52 | pump_messages_event->ref_count_++; |
| 53 | return pump_messages_event; |
| 54 | } |
| 55 | |
| 56 | // Releases a handle to this event. There must be a 1:1 correspondence between |
| 57 | // calls to Acquire() and calls to Release(). |
| 58 | static void Release() { |
| 59 | PumpMessagesEvent* pump_messages_event = g_event_.Pointer()->Get(); |
| 60 | DCHECK(pump_messages_event); |
| 61 | DCHECK_GT(pump_messages_event->ref_count_, 0); |
| 62 | pump_messages_event->ref_count_--; |
| 63 | if (!pump_messages_event->ref_count_) { |
| 64 | g_event_.Pointer()->Set(nullptr); |
| 65 | delete pump_messages_event; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | const mojo::Handle& GetHandle() const { return event_.GetHandle(); } |
| 70 | |
| 71 | private: |
| 72 | PumpMessagesEvent() {} |
| 73 | ~PumpMessagesEvent() {} |
| 74 | |
| 75 | int ref_count_ = 0; |
| 76 | MojoEvent event_; |
| 77 | |
| 78 | static base::LazyInstance<base::ThreadLocalPointer<PumpMessagesEvent>> |
| 79 | g_event_; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(PumpMessagesEvent); |
| 82 | }; |
| 83 | |
| 84 | // A generic callback used when watching handles synchronously. Sets |*signal| |
| 85 | // to true. Also sets |*error| to true in case of an error. |
| 86 | void OnSyncHandleReady(bool* signal, bool* error, MojoResult result) { |
| 87 | *signal = true; |
| 88 | *error = result != MOJO_RESULT_OK; |
| 89 | } |
| 90 | |
| 91 | // A ReadyCallback for use with mojo::Watcher. Ignores the result (DCHECKs, but |
| 92 | // is only used in cases where failure should be impossible) and runs |
| 93 | // |callback|. |
| 94 | void RunOnHandleReady(const base::Closure& callback, MojoResult result) { |
| 95 | DCHECK(result == MOJO_RESULT_OK || result == MOJO_RESULT_ABORTED); |
| 96 | if (result == MOJO_RESULT_OK) |
| 97 | callback.Run(); |
| 98 | } |
| 99 | |
| 100 | } // namespace |
| 101 | |
| 102 | base::LazyInstance<base::ThreadLocalPointer<PumpMessagesEvent>> |
| 103 | PumpMessagesEvent::g_event_ = LAZY_INSTANCE_INITIALIZER; |
| 104 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | // When we're blocked in a Send(), we need to process incoming synchronous |
| 106 | // messages right away because it could be blocking our reply (either |
| 107 | // directly from the same object we're calling, or indirectly through one or |
| 108 | // more other channels). That means that in SyncContext's OnMessageReceived, |
| 109 | // we need to process sync message right away if we're blocked. However a |
| 110 | // simple check isn't sufficient, because the listener thread can be in the |
| 111 | // process of calling Send. |
| 112 | // To work around this, when SyncChannel filters a sync message, it sets |
| 113 | // an event that the listener thread waits on during its Send() call. This |
| 114 | // allows us to dispatch incoming sync messages when blocked. The race |
| 115 | // condition is handled because if Send is in the process of being called, it |
| 116 | // will check the event. In case the listener thread isn't sending a message, |
| 117 | // we queue a task on the listener thread to dispatch the received messages. |
| 118 | // The messages are stored in this queue object that's shared among all |
| 119 | // SyncChannel objects on the same thread (since one object can receive a |
| 120 | // sync message while another one is blocked). |
| 121 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | class SyncChannel::ReceivedSyncMsgQueue : |
| 123 | public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> { |
| 124 | public: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 125 | // Returns the ReceivedSyncMsgQueue instance for this thread, creating one |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 126 | // if necessary. Call RemoveContext on the same thread when done. |
| 127 | static ReceivedSyncMsgQueue* AddContext() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 128 | // We want one ReceivedSyncMsgQueue per listener thread (i.e. since multiple |
| 129 | // SyncChannel objects can block the same thread). |
| 130 | ReceivedSyncMsgQueue* rv = lazy_tls_ptr_.Pointer()->Get(); |
| 131 | if (!rv) { |
| 132 | rv = new ReceivedSyncMsgQueue(); |
| 133 | ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(rv); |
| 134 | } |
| 135 | rv->listener_count_++; |
| 136 | return rv; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | } |
| 138 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | // Called on IPC thread when a synchronous message or reply arrives. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 140 | void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | bool was_task_pending; |
| 142 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 143 | base::AutoLock auto_lock(message_lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | |
| 145 | was_task_pending = task_pending_; |
| 146 | task_pending_ = true; |
| 147 | |
| 148 | // We set the event in case the listener thread is blocked (or is about |
| 149 | // to). In case it's not, the PostTask dispatches the messages. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 150 | message_queue_.push_back(QueuedMessage(new Message(msg), context)); |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 151 | message_queue_version_++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 154 | dispatch_event_.Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | if (!was_task_pending) { |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 156 | listener_task_runner_->PostTask( |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 157 | FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchMessagesTask, |
vmpstr | a34d1132 | 2016-03-21 20:28:47 | [diff] [blame] | 158 | this, base::RetainedRef(context))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | void QueueReply(const Message &msg, SyncChannel::SyncContext* context) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 163 | received_replies_.push_back(QueuedMessage(new Message(msg), context)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 166 | // Called on the listener's thread to process any queues synchronous |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | // messages. |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 168 | void DispatchMessagesTask(SyncContext* context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 170 | base::AutoLock auto_lock(message_lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 171 | task_pending_ = false; |
| 172 | } |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 173 | context->DispatchMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 176 | void DispatchMessages(SyncContext* dispatching_context) { |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 177 | bool first_time = true; |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 178 | uint32_t expected_version = 0; |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 179 | SyncMessageQueue::iterator it; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 180 | while (true) { |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 181 | Message* message = NULL; |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 182 | scoped_refptr<SyncChannel::SyncContext> context; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 183 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 184 | base::AutoLock auto_lock(message_lock_); |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 185 | if (first_time || message_queue_version_ != expected_version) { |
| 186 | it = message_queue_.begin(); |
| 187 | first_time = false; |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 188 | } |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 189 | for (; it != message_queue_.end(); it++) { |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 190 | int message_group = it->context->restrict_dispatch_group(); |
| 191 | if (message_group == kRestrictDispatchGroup_None || |
| 192 | message_group == dispatching_context->restrict_dispatch_group()) { |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 193 | message = it->message; |
| 194 | context = it->context; |
| 195 | it = message_queue_.erase(it); |
| 196 | message_queue_version_++; |
| 197 | expected_version = message_queue_version_; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 203 | if (message == NULL) |
| 204 | break; |
| 205 | context->OnDispatchMessage(*message); |
| 206 | delete message; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | // SyncChannel calls this in its destructor. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 211 | void RemoveContext(SyncContext* context) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 212 | base::AutoLock auto_lock(message_lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 214 | SyncMessageQueue::iterator iter = message_queue_.begin(); |
| 215 | while (iter != message_queue_.end()) { |
[email protected] | 1757164 | 2013-06-01 04:11:27 | [diff] [blame] | 216 | if (iter->context.get() == context) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 217 | delete iter->message; |
| 218 | iter = message_queue_.erase(iter); |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 219 | message_queue_version_++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | } else { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 221 | iter++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 224 | |
| 225 | if (--listener_count_ == 0) { |
| 226 | DCHECK(lazy_tls_ptr_.Pointer()->Get()); |
| 227 | lazy_tls_ptr_.Pointer()->Set(NULL); |
| 228 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | } |
| 230 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 231 | MojoEvent* dispatch_event() { return &dispatch_event_; } |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 232 | base::SingleThreadTaskRunner* listener_task_runner() { |
[email protected] | 1757164 | 2013-06-01 04:11:27 | [diff] [blame] | 233 | return listener_task_runner_.get(); |
[email protected] | 92bf906 | 2011-05-02 18:00:49 | [diff] [blame] | 234 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 236 | // Holds a pointer to the per-thread ReceivedSyncMsgQueue object. |
| 237 | static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> > |
| 238 | lazy_tls_ptr_; |
| 239 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 240 | // Called on the ipc thread to check if we can unblock any current Send() |
| 241 | // calls based on a queued reply. |
| 242 | void DispatchReplies() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | for (size_t i = 0; i < received_replies_.size(); ++i) { |
| 244 | Message* message = received_replies_[i].message; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 245 | if (received_replies_[i].context->TryToUnblockListener(message)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 246 | delete message; |
| 247 | received_replies_.erase(received_replies_.begin() + i); |
| 248 | return; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 253 | mojo::Watcher* top_send_done_watcher() { |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 254 | return top_send_done_watcher_; |
| 255 | } |
| 256 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 257 | void set_top_send_done_watcher(mojo::Watcher* watcher) { |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 258 | top_send_done_watcher_ = watcher; |
| 259 | } |
| 260 | |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 261 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 262 | friend class base::RefCountedThreadSafe<ReceivedSyncMsgQueue>; |
| 263 | |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 264 | // See the comment in SyncChannel::SyncChannel for why this event is created |
| 265 | // as manual reset. |
gab | 90c2c5c | 2016-06-01 20:34:06 | [diff] [blame] | 266 | ReceivedSyncMsgQueue() |
| 267 | : message_queue_version_(0), |
gab | 90c2c5c | 2016-06-01 20:34:06 | [diff] [blame] | 268 | listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 269 | task_pending_(false), |
| 270 | listener_count_(0), |
| 271 | top_send_done_watcher_(NULL) {} |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 272 | |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 273 | ~ReceivedSyncMsgQueue() {} |
| 274 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 275 | // Holds information about a queued synchronous message or reply. |
| 276 | struct QueuedMessage { |
| 277 | QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | Message* message; |
| 279 | scoped_refptr<SyncChannel::SyncContext> context; |
| 280 | }; |
| 281 | |
[email protected] | 522cc10d | 2012-01-11 22:39:54 | [diff] [blame] | 282 | typedef std::list<QueuedMessage> SyncMessageQueue; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 283 | SyncMessageQueue message_queue_; |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 284 | uint32_t message_queue_version_; // Used to signal DispatchMessages to rescan |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 285 | |
| 286 | std::vector<QueuedMessage> received_replies_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 287 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 288 | // 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] | 289 | // sender needs its reply before it can reply to our original synchronous |
| 290 | // message. |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 291 | MojoEvent dispatch_event_; |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 292 | scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 293 | base::Lock message_lock_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 294 | bool task_pending_; |
| 295 | int listener_count_; |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 296 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 297 | // The current send done handle watcher for this thread. Used to maintain |
| 298 | // a thread-local stack of send done watchers to ensure that nested sync |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 299 | // message loops complete correctly. |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 300 | mojo::Watcher* top_send_done_watcher_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | }; |
| 302 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 303 | base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> > |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 304 | SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_ = |
| 305 | LAZY_INSTANCE_INITIALIZER; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | |
| 307 | SyncChannel::SyncContext::SyncContext( |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 308 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 309 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 310 | WaitableEvent* shutdown_event) |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 311 | : ChannelProxy::Context(listener, ipc_task_runner), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 312 | received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()), |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 313 | shutdown_event_(shutdown_event), |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 314 | restrict_dispatch_group_(kRestrictDispatchGroup_None) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | SyncChannel::SyncContext::~SyncContext() { |
| 318 | while (!deserializers_.empty()) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 319 | Pop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // Adds information about an outgoing sync message to the context so that |
| 323 | // we know how to deserialize the reply. Returns a handle that's set when |
| 324 | // the reply has arrived. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 325 | void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) { |
[email protected] | 4a180a5 | 2011-04-15 19:07:43 | [diff] [blame] | 326 | // Create the tracking information for this message. This object is stored |
| 327 | // by value since all members are pointers that are cheap to copy. These |
| 328 | // pointers are cleaned up in the Pop() function. |
| 329 | // |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 330 | // The event is created as manual reset because in between Signal and |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 331 | // OnObjectSignalled, another Send can happen which would stop the watcher |
| 332 | // from being called. The event would get watched later, when the nested |
| 333 | // Send completes, so the event will need to remain set. |
gab | 90c2c5c | 2016-06-01 20:34:06 | [diff] [blame] | 334 | PendingSyncMsg pending( |
| 335 | SyncMessage::GetMessageId(*sync_msg), sync_msg->GetReplyDeserializer(), |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 336 | new MojoEvent); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 337 | base::AutoLock auto_lock(deserializers_lock_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 338 | deserializers_.push_back(pending); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 339 | } |
| 340 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 341 | bool SyncChannel::SyncContext::Pop() { |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 342 | bool result; |
| 343 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 344 | base::AutoLock auto_lock(deserializers_lock_); |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 345 | PendingSyncMsg msg = deserializers_.back(); |
| 346 | delete msg.deserializer; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 347 | delete msg.done_event; |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 348 | msg.done_event = nullptr; |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 349 | deserializers_.pop_back(); |
| 350 | result = msg.send_result; |
| 351 | } |
| 352 | |
| 353 | // We got a reply to a synchronous Send() call that's blocking the listener |
| 354 | // thread. However, further down the call stack there could be another |
| 355 | // blocking Send() call, whose reply we received after we made this last |
| 356 | // Send() call. So check if we have any queued replies available that |
| 357 | // can now unblock the listener thread. |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 358 | ipc_task_runner()->PostTask( |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 359 | FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchReplies, |
| 360 | received_sync_msgs_.get())); |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 361 | |
| 362 | return result; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 363 | } |
| 364 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 365 | MojoEvent* SyncChannel::SyncContext::GetSendDoneEvent() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 366 | base::AutoLock auto_lock(deserializers_lock_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 367 | return deserializers_.back().done_event; |
| 368 | } |
| 369 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 370 | MojoEvent* SyncChannel::SyncContext::GetDispatchEvent() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 371 | return received_sync_msgs_->dispatch_event(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void SyncChannel::SyncContext::DispatchMessages() { |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 375 | received_sync_msgs_->DispatchMessages(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 376 | } |
| 377 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 378 | bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 379 | base::AutoLock auto_lock(deserializers_lock_); |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 380 | if (deserializers_.empty() || |
| 381 | !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) { |
| 382 | return false; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 383 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 384 | |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 385 | if (!msg->is_reply_error()) { |
[email protected] | 211142cd | 2012-08-13 09:41:19 | [diff] [blame] | 386 | bool send_result = deserializers_.back().deserializer-> |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 387 | SerializeOutputParameters(*msg); |
[email protected] | 211142cd | 2012-08-13 09:41:19 | [diff] [blame] | 388 | deserializers_.back().send_result = send_result; |
bauerb | 3e9be73 | 2015-11-03 18:17:47 | [diff] [blame] | 389 | DVLOG_IF(1, !send_result) << "Couldn't deserialize reply message"; |
[email protected] | 211142cd | 2012-08-13 09:41:19 | [diff] [blame] | 390 | } else { |
bauerb | 3e9be73 | 2015-11-03 18:17:47 | [diff] [blame] | 391 | DVLOG(1) << "Received error reply"; |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 392 | } |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 393 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 394 | MojoEvent* done_event = deserializers_.back().done_event; |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 395 | TRACE_EVENT_FLOW_BEGIN0( |
| 396 | TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 397 | "SyncChannel::SyncContext::TryToUnblockListener", done_event); |
| 398 | |
| 399 | done_event->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 401 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | } |
| 403 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 404 | void SyncChannel::SyncContext::Clear() { |
| 405 | CancelPendingSends(); |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 406 | received_sync_msgs_->RemoveContext(this); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 407 | Context::Clear(); |
| 408 | } |
| 409 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 410 | bool SyncChannel::SyncContext::OnMessageReceived(const Message& msg) { |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 411 | // Give the filters a chance at processing this message. |
| 412 | if (TryFilters(msg)) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 413 | return true; |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 414 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 415 | if (TryToUnblockListener(&msg)) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 416 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 417 | |
[email protected] | 9134cce6d | 2012-04-10 20:07:53 | [diff] [blame] | 418 | if (msg.is_reply()) { |
| 419 | received_sync_msgs_->QueueReply(msg, this); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 420 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | } |
| 422 | |
[email protected] | 9134cce6d | 2012-04-10 20:07:53 | [diff] [blame] | 423 | if (msg.should_unblock()) { |
| 424 | received_sync_msgs_->QueueMessage(msg, this); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 425 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | } |
| 427 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 428 | return Context::OnMessageReceivedNoFilter(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 429 | } |
| 430 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 431 | void SyncChannel::SyncContext::OnChannelError() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 432 | CancelPendingSends(); |
[email protected] | a4f82270 | 2009-02-06 00:44:53 | [diff] [blame] | 433 | shutdown_watcher_.StopWatching(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 434 | Context::OnChannelError(); |
| 435 | } |
| 436 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 437 | void SyncChannel::SyncContext::OnChannelOpened() { |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 438 | shutdown_watcher_.StartWatching( |
| 439 | shutdown_event_, |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 440 | base::Bind(&SyncChannel::SyncContext::OnShutdownEventSignaled, |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 441 | base::Unretained(this))); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 442 | Context::OnChannelOpened(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 445 | void SyncChannel::SyncContext::OnChannelClosed() { |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 446 | CancelPendingSends(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 447 | shutdown_watcher_.StopWatching(); |
| 448 | Context::OnChannelClosed(); |
| 449 | } |
| 450 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 451 | void SyncChannel::SyncContext::CancelPendingSends() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 452 | base::AutoLock auto_lock(deserializers_lock_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 453 | PendingSyncMessageQueue::iterator iter; |
bauerb | 3e9be73 | 2015-11-03 18:17:47 | [diff] [blame] | 454 | DVLOG(1) << "Canceling pending sends"; |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 455 | for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) { |
| 456 | TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 457 | "SyncChannel::SyncContext::CancelPendingSends", |
| 458 | iter->done_event); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 459 | iter->done_event->Signal(); |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 460 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 461 | } |
| 462 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 463 | void SyncChannel::SyncContext::OnShutdownEventSignaled(WaitableEvent* event) { |
| 464 | DCHECK_EQ(event, shutdown_event_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 465 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 466 | // Process shut down before we can get a reply to a synchronous message. |
| 467 | // Cancel pending Send calls, which will end up setting the send done event. |
| 468 | CancelPendingSends(); |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 469 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 470 | |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 471 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 472 | std::unique_ptr<SyncChannel> SyncChannel::Create( |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 473 | const IPC::ChannelHandle& channel_handle, |
[email protected] | 3b0e466 | 2014-06-02 20:29:30 | [diff] [blame] | 474 | Channel::Mode mode, |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 475 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 476 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 477 | bool create_pipe_now, |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 478 | base::WaitableEvent* shutdown_event) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 479 | std::unique_ptr<SyncChannel> channel = |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 480 | Create(listener, ipc_task_runner, shutdown_event); |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 481 | channel->Init(channel_handle, mode, create_pipe_now); |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 482 | return channel; |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 486 | std::unique_ptr<SyncChannel> SyncChannel::Create( |
| 487 | std::unique_ptr<ChannelFactory> factory, |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 488 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 489 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 490 | bool create_pipe_now, |
| 491 | base::WaitableEvent* shutdown_event) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 492 | std::unique_ptr<SyncChannel> channel = |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 493 | Create(listener, ipc_task_runner, shutdown_event); |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 494 | channel->Init(std::move(factory), create_pipe_now); |
| 495 | return channel; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 499 | std::unique_ptr<SyncChannel> SyncChannel::Create( |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 500 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 501 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 502 | WaitableEvent* shutdown_event) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 503 | return base::WrapUnique( |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 504 | new SyncChannel(listener, ipc_task_runner, shutdown_event)); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | SyncChannel::SyncChannel( |
[email protected] | b7f59e82 | 2012-06-29 22:05:26 | [diff] [blame] | 508 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 509 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 510 | WaitableEvent* shutdown_event) |
[email protected] | 5855f1d | 2014-04-16 16:50:43 | [diff] [blame] | 511 | : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)) { |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 512 | // Keep a thread-local PumpMessagesEvent alive at least as long as any |
| 513 | // SyncChannel exists. This is balanced in the SyncChannel destructor below. |
| 514 | PumpMessagesEvent::Acquire(); |
| 515 | |
[email protected] | 1f9cf47 | 2014-04-17 05:07:18 | [diff] [blame] | 516 | // The current (listener) thread must be distinct from the IPC thread, or else |
| 517 | // sending synchronous messages will deadlock. |
dcheng | ff04843f | 2014-09-03 18:01:16 | [diff] [blame] | 518 | DCHECK_NE(ipc_task_runner.get(), base::ThreadTaskRunnerHandle::Get().get()); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 519 | StartWatching(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | SyncChannel::~SyncChannel() { |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 523 | PumpMessagesEvent::Release(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 524 | } |
| 525 | |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 526 | void SyncChannel::SetRestrictDispatchChannelGroup(int group) { |
| 527 | sync_context()->set_restrict_dispatch_group(group); |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 528 | } |
| 529 | |
rockot | ac64ae92f | 2015-08-06 00:32:29 | [diff] [blame] | 530 | scoped_refptr<SyncMessageFilter> SyncChannel::CreateSyncMessageFilter() { |
| 531 | scoped_refptr<SyncMessageFilter> filter = new SyncMessageFilter( |
| 532 | sync_context()->shutdown_event(), |
| 533 | sync_context()->IsChannelSendThreadSafe()); |
| 534 | AddFilter(filter.get()); |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 535 | if (!did_init()) |
| 536 | pre_init_sync_message_filters_.push_back(filter); |
rockot | ac64ae92f | 2015-08-06 00:32:29 | [diff] [blame] | 537 | return filter; |
| 538 | } |
| 539 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 540 | bool SyncChannel::Send(Message* message) { |
[email protected] | 60b2c61f | 2012-08-22 22:39:57 | [diff] [blame] | 541 | #ifdef IPC_MESSAGE_LOG_ENABLED |
[email protected] | 60b2c61f | 2012-08-22 22:39:57 | [diff] [blame] | 542 | std::string name; |
pkasting | 7bc277b | 2014-10-13 20:58:39 | [diff] [blame] | 543 | Logging::GetInstance()->GetMessageText(message->type(), &name, message, NULL); |
[email protected] | d6cbf05 | 2014-05-02 21:29:24 | [diff] [blame] | 544 | TRACE_EVENT1("ipc", "SyncChannel::Send", "name", name); |
[email protected] | 60b2c61f | 2012-08-22 22:39:57 | [diff] [blame] | 545 | #else |
[email protected] | d6cbf05 | 2014-05-02 21:29:24 | [diff] [blame] | 546 | TRACE_EVENT2("ipc", "SyncChannel::Send", |
[email protected] | 60b2c61f | 2012-08-22 22:39:57 | [diff] [blame] | 547 | "class", IPC_MESSAGE_ID_CLASS(message->type()), |
| 548 | "line", IPC_MESSAGE_ID_LINE(message->type())); |
| 549 | #endif |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 550 | if (!message->is_sync()) { |
| 551 | ChannelProxy::Send(message); |
| 552 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 553 | } |
| 554 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 555 | // *this* might get deleted in WaitForReply. |
| 556 | scoped_refptr<SyncContext> context(sync_context()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 557 | if (context->shutdown_event()->IsSignaled()) { |
bauerb | 3e9be73 | 2015-11-03 18:17:47 | [diff] [blame] | 558 | DVLOG(1) << "shutdown event is signaled"; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 559 | delete message; |
| 560 | return false; |
| 561 | } |
| 562 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 563 | SyncMessage* sync_msg = static_cast<SyncMessage*>(message); |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 564 | bool pump_messages = sync_msg->ShouldPumpMessages(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 565 | context->Push(sync_msg); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 566 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 567 | ChannelProxy::Send(message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 568 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 569 | // Wait for reply, or for any other incoming synchronous messages. |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 570 | // *this* might get deleted, so only call static functions at this point. |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 571 | WaitForReply(context.get(), pump_messages); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 572 | |
tzik | a08b2fd | 2016-03-30 02:32:59 | [diff] [blame] | 573 | TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 574 | "SyncChannel::Send", context->GetSendDoneEvent()); |
| 575 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 576 | return context->Pop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 577 | } |
| 578 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 579 | void SyncChannel::WaitForReply(SyncContext* context, bool pump_messages) { |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 580 | context->DispatchMessages(); |
rockot | 9791271 | 2016-06-15 05:28:42 | [diff] [blame] | 581 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 582 | PumpMessagesEvent* pump_messages_event = nullptr; |
| 583 | if (pump_messages) |
| 584 | pump_messages_event = PumpMessagesEvent::Acquire(); |
| 585 | |
| 586 | scoped_refptr<mojo::SyncHandleRegistry> registry = |
| 587 | mojo::SyncHandleRegistry::current(); |
| 588 | |
| 589 | while (true) { |
| 590 | bool dispatch = false; |
| 591 | bool send_done = false; |
| 592 | bool should_pump_messages = false; |
| 593 | bool error = false; |
| 594 | registry->RegisterHandle(context->GetDispatchEvent()->GetHandle(), |
| 595 | MOJO_HANDLE_SIGNAL_READABLE, |
| 596 | base::Bind(&OnSyncHandleReady, &dispatch, &error)); |
| 597 | registry->RegisterHandle( |
| 598 | context->GetSendDoneEvent()->GetHandle(), |
| 599 | MOJO_HANDLE_SIGNAL_READABLE, |
| 600 | base::Bind(&OnSyncHandleReady, &send_done, &error)); |
| 601 | if (pump_messages_event) { |
| 602 | registry->RegisterHandle( |
| 603 | pump_messages_event->GetHandle(), MOJO_HANDLE_SIGNAL_READABLE, |
| 604 | base::Bind(&OnSyncHandleReady, &should_pump_messages, &error)); |
| 605 | } |
| 606 | |
| 607 | const bool* stop_flags[] = { &dispatch, &send_done, &should_pump_messages }; |
| 608 | bool result = registry->WatchAllHandles(stop_flags, 3); |
| 609 | DCHECK(result); |
| 610 | DCHECK(!error); |
| 611 | |
| 612 | registry->UnregisterHandle(context->GetDispatchEvent()->GetHandle()); |
| 613 | registry->UnregisterHandle(context->GetSendDoneEvent()->GetHandle()); |
| 614 | if (pump_messages_event) |
| 615 | registry->UnregisterHandle(pump_messages_event->GetHandle()); |
| 616 | |
| 617 | if (dispatch) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 618 | // We're waiting for a reply, but we received a blocking synchronous |
| 619 | // call. We must process it or otherwise a deadlock might occur. |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 620 | context->GetDispatchEvent()->Reset(); |
| 621 | context->DispatchMessages(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 622 | continue; |
| 623 | } |
| 624 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 625 | DCHECK(send_done || should_pump_messages); |
| 626 | |
| 627 | if (should_pump_messages) |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 628 | WaitForReplyWithNestedMessageLoop(context); // Run a nested message loop. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 629 | |
| 630 | break; |
| 631 | } |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 632 | |
| 633 | if (pump_messages_event) |
| 634 | PumpMessagesEvent::Release(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 635 | } |
| 636 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 637 | void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) { |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 638 | mojo::Watcher send_done_watcher; |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 639 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 640 | ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs(); |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 641 | DCHECK_NE(sync_msg_queue, nullptr); |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 642 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 643 | mojo::Watcher* old_watcher = sync_msg_queue->top_send_done_watcher(); |
| 644 | mojo::Handle old_handle(mojo::kInvalidHandleValue); |
| 645 | mojo::Watcher::ReadyCallback old_callback; |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 646 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 647 | // Maintain a thread-local stack of watchers to ensure nested calls complete |
| 648 | // in the correct sequence, i.e. the outermost call completes first, etc. |
| 649 | if (old_watcher) { |
| 650 | old_callback = old_watcher->ready_callback(); |
| 651 | old_handle = old_watcher->handle(); |
| 652 | old_watcher->Cancel(); |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | sync_msg_queue->set_top_send_done_watcher(&send_done_watcher); |
| 656 | |
tommycli | 5628ee3 | 2016-06-15 15:54:55 | [diff] [blame] | 657 | { |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 658 | base::RunLoop nested_loop; |
| 659 | send_done_watcher.Start( |
| 660 | context->GetSendDoneEvent()->GetHandle(), MOJO_HANDLE_SIGNAL_READABLE, |
| 661 | base::Bind(&RunOnHandleReady, nested_loop.QuitClosure())); |
| 662 | |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 663 | base::MessageLoop::ScopedNestableTaskAllower allow( |
| 664 | base::MessageLoop::current()); |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 665 | nested_loop.Run(); |
| 666 | send_done_watcher.Cancel(); |
[email protected] | b5717a4 | 2012-02-14 19:33:52 | [diff] [blame] | 667 | } |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 668 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 669 | sync_msg_queue->set_top_send_done_watcher(old_watcher); |
| 670 | if (old_watcher) |
| 671 | old_watcher->Start(old_handle, MOJO_HANDLE_SIGNAL_READABLE, old_callback); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 672 | } |
| 673 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 674 | void SyncChannel::OnDispatchHandleReady(MojoResult result) { |
| 675 | DCHECK(result == MOJO_RESULT_OK || result == MOJO_RESULT_ABORTED); |
| 676 | if (result == MOJO_RESULT_OK) { |
| 677 | sync_context()->GetDispatchEvent()->Reset(); |
| 678 | sync_context()->DispatchMessages(); |
| 679 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 680 | } |
| 681 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 682 | void SyncChannel::StartWatching() { |
| 683 | // Ideally we only want to watch this object when running a nested message |
| 684 | // loop. However, we don't know when it exits if there's another nested |
| 685 | // message loop running under it or not, so we wouldn't know whether to |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame^] | 686 | // stop or keep watching. So we always watch it. |
| 687 | dispatch_watcher_.Start(sync_context()->GetDispatchEvent()->GetHandle(), |
| 688 | MOJO_HANDLE_SIGNAL_READABLE, |
| 689 | base::Bind(&SyncChannel::OnDispatchHandleReady, |
| 690 | base::Unretained(this))); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 691 | } |
| 692 | |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 693 | void SyncChannel::OnChannelInit() { |
| 694 | for (const auto& filter : pre_init_sync_message_filters_) { |
| 695 | filter->set_is_channel_send_thread_safe( |
| 696 | context()->IsChannelSendThreadSafe()); |
| 697 | } |
| 698 | pre_init_sync_message_filters_.clear(); |
| 699 | } |
| 700 | |
rockot | 868f89e7 | 2016-05-25 16:25:45 | [diff] [blame] | 701 | bool SyncChannel::SendNow(std::unique_ptr<Message> message) { |
| 702 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 703 | std::string name; |
| 704 | Logging::GetInstance()->GetMessageText( |
| 705 | message->type(), &name, message.get(), nullptr); |
| 706 | TRACE_EVENT1("ipc", "SyncChannel::SendNow", "name", name); |
| 707 | #else |
| 708 | TRACE_EVENT2("ipc", "SyncChannel::SendNow", |
| 709 | "class", IPC_MESSAGE_ID_CLASS(message->type()), |
| 710 | "line", IPC_MESSAGE_ID_LINE(message->type())); |
| 711 | #endif |
| 712 | if (!message->is_sync()) |
| 713 | return ChannelProxy::SendNow(std::move(message)); |
| 714 | return Send(message.release()); |
| 715 | } |
| 716 | |
| 717 | bool SyncChannel::SendOnIPCThread(std::unique_ptr<Message> message) { |
| 718 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 719 | std::string name; |
| 720 | Logging::GetInstance()->GetMessageText( |
| 721 | message->type(), &name, message.get(), nullptr); |
| 722 | TRACE_EVENT1("ipc", "SyncChannel::SendOnIPCThread", "name", name); |
| 723 | #else |
| 724 | TRACE_EVENT2("ipc", "SyncChannel::SendOnIPCThread", |
| 725 | "class", IPC_MESSAGE_ID_CLASS(message->type()), |
| 726 | "line", IPC_MESSAGE_ID_LINE(message->type())); |
| 727 | #endif |
| 728 | if (!message->is_sync()) |
| 729 | return ChannelProxy::SendOnIPCThread(std::move(message)); |
| 730 | return Send(message.release()); |
| 731 | } |
| 732 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 733 | } // namespace IPC |