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