blob: d1942112d9a237a754720ba9c06f63603427e9a6 [file] [log] [blame]
[email protected]522cc10d2012-01-11 22:39:541// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]946d1b22009-07-22 23:57:215#include "ipc/ipc_sync_channel.h"
initial.commit09911bf2008-07-26 23:55:296
avi246998d82015-12-22 02:39:047#include <stddef.h>
8#include <stdint.h>
dchenge48600452015-12-28 02:24:509#include <utility>
avi246998d82015-12-22 02:39:0410
[email protected]72b6f8e22011-11-12 21:16:4111#include "base/bind.h"
[email protected]f886b7bf2008-09-10 10:54:0612#include "base/lazy_instance.h"
[email protected]c62dd9d2011-09-21 18:05:4113#include "base/location.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/logging.h"
[email protected]44f9c952011-01-02 06:05:3915#include "base/synchronization/waitable_event.h"
16#include "base/synchronization/waitable_event_watcher.h"
[email protected]b2432302012-07-02 21:15:5217#include "base/thread_task_runner_handle.h"
18#include "base/threading/thread_local.h"
primiano7182d7b2015-01-30 18:02:0319#include "base/trace_event/trace_event.h"
[email protected]64860882014-08-04 23:44:1720#include "ipc/ipc_channel_factory.h"
[email protected]60b2c61f2012-08-22 22:39:5721#include "ipc/ipc_logging.h"
22#include "ipc/ipc_message_macros.h"
[email protected]946d1b22009-07-22 23:57:2123#include "ipc/ipc_sync_message.h"
initial.commit09911bf2008-07-26 23:55:2924
[email protected]e1acf6f2008-10-27 20:43:3325using base::TimeDelta;
26using base::TimeTicks;
[email protected]1c4947f2009-01-15 22:25:1127using base::WaitableEvent;
initial.commit09911bf2008-07-26 23:55:2928
29namespace IPC {
30// When we're blocked in a Send(), we need to process incoming synchronous
31// messages right away because it could be blocking our reply (either
32// directly from the same object we're calling, or indirectly through one or
33// more other channels). That means that in SyncContext's OnMessageReceived,
34// we need to process sync message right away if we're blocked. However a
35// simple check isn't sufficient, because the listener thread can be in the
36// process of calling Send.
37// To work around this, when SyncChannel filters a sync message, it sets
38// an event that the listener thread waits on during its Send() call. This
39// allows us to dispatch incoming sync messages when blocked. The race
40// condition is handled because if Send is in the process of being called, it
41// will check the event. In case the listener thread isn't sending a message,
42// we queue a task on the listener thread to dispatch the received messages.
43// The messages are stored in this queue object that's shared among all
44// SyncChannel objects on the same thread (since one object can receive a
45// sync message while another one is blocked).
46
initial.commit09911bf2008-07-26 23:55:2947class SyncChannel::ReceivedSyncMsgQueue :
48 public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> {
49 public:
[email protected]3cdb7af812008-10-24 19:21:1350 // Returns the ReceivedSyncMsgQueue instance for this thread, creating one
[email protected]d3ae7a072008-12-05 20:27:2051 // if necessary. Call RemoveContext on the same thread when done.
52 static ReceivedSyncMsgQueue* AddContext() {
[email protected]3cdb7af812008-10-24 19:21:1353 // We want one ReceivedSyncMsgQueue per listener thread (i.e. since multiple
54 // SyncChannel objects can block the same thread).
55 ReceivedSyncMsgQueue* rv = lazy_tls_ptr_.Pointer()->Get();
56 if (!rv) {
57 rv = new ReceivedSyncMsgQueue();
58 ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(rv);
59 }
60 rv->listener_count_++;
61 return rv;
initial.commit09911bf2008-07-26 23:55:2962 }
63
initial.commit09911bf2008-07-26 23:55:2964 // Called on IPC thread when a synchronous message or reply arrives.
[email protected]d3ae7a072008-12-05 20:27:2065 void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2966 bool was_task_pending;
67 {
[email protected]20305ec2011-01-21 04:55:5268 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:2969
70 was_task_pending = task_pending_;
71 task_pending_ = true;
72
73 // We set the event in case the listener thread is blocked (or is about
74 // to). In case it's not, the PostTask dispatches the messages.
[email protected]d3ae7a072008-12-05 20:27:2075 message_queue_.push_back(QueuedMessage(new Message(msg), context));
[email protected]522cc10d2012-01-11 22:39:5476 message_queue_version_++;
initial.commit09911bf2008-07-26 23:55:2977 }
78
[email protected]1c4947f2009-01-15 22:25:1179 dispatch_event_.Signal();
initial.commit09911bf2008-07-26 23:55:2980 if (!was_task_pending) {
[email protected]b2432302012-07-02 21:15:5281 listener_task_runner_->PostTask(
[email protected]72b6f8e22011-11-12 21:16:4182 FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchMessagesTask,
83 this, scoped_refptr<SyncContext>(context)));
initial.commit09911bf2008-07-26 23:55:2984 }
85 }
86
87 void QueueReply(const Message &msg, SyncChannel::SyncContext* context) {
[email protected]d3ae7a072008-12-05 20:27:2088 received_replies_.push_back(QueuedMessage(new Message(msg), context));
initial.commit09911bf2008-07-26 23:55:2989 }
90
[email protected]d3ae7a072008-12-05 20:27:2091 // Called on the listener's thread to process any queues synchronous
initial.commit09911bf2008-07-26 23:55:2992 // messages.
[email protected]54af05f2011-04-08 03:38:2193 void DispatchMessagesTask(SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2994 {
[email protected]20305ec2011-01-21 04:55:5295 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:2996 task_pending_ = false;
97 }
[email protected]54af05f2011-04-08 03:38:2198 context->DispatchMessages();
initial.commit09911bf2008-07-26 23:55:2999 }
100
[email protected]54af05f2011-04-08 03:38:21101 void DispatchMessages(SyncContext* dispatching_context) {
[email protected]522cc10d2012-01-11 22:39:54102 bool first_time = true;
tfarina10a5c062015-09-04 18:47:57103 uint32_t expected_version = 0;
[email protected]522cc10d2012-01-11 22:39:54104 SyncMessageQueue::iterator it;
initial.commit09911bf2008-07-26 23:55:29105 while (true) {
[email protected]522cc10d2012-01-11 22:39:54106 Message* message = NULL;
[email protected]d3ae7a072008-12-05 20:27:20107 scoped_refptr<SyncChannel::SyncContext> context;
initial.commit09911bf2008-07-26 23:55:29108 {
[email protected]20305ec2011-01-21 04:55:52109 base::AutoLock auto_lock(message_lock_);
[email protected]522cc10d2012-01-11 22:39:54110 if (first_time || message_queue_version_ != expected_version) {
111 it = message_queue_.begin();
112 first_time = false;
[email protected]54af05f2011-04-08 03:38:21113 }
[email protected]522cc10d2012-01-11 22:39:54114 for (; it != message_queue_.end(); it++) {
[email protected]298ee7d2012-03-30 21:29:30115 int message_group = it->context->restrict_dispatch_group();
116 if (message_group == kRestrictDispatchGroup_None ||
117 message_group == dispatching_context->restrict_dispatch_group()) {
[email protected]522cc10d2012-01-11 22:39:54118 message = it->message;
119 context = it->context;
120 it = message_queue_.erase(it);
121 message_queue_version_++;
122 expected_version = message_queue_version_;
123 break;
124 }
125 }
126 }
initial.commit09911bf2008-07-26 23:55:29127
[email protected]522cc10d2012-01-11 22:39:54128 if (message == NULL)
129 break;
130 context->OnDispatchMessage(*message);
131 delete message;
initial.commit09911bf2008-07-26 23:55:29132 }
133 }
134
initial.commit09911bf2008-07-26 23:55:29135 // SyncChannel calls this in its destructor.
[email protected]d3ae7a072008-12-05 20:27:20136 void RemoveContext(SyncContext* context) {
[email protected]20305ec2011-01-21 04:55:52137 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:29138
[email protected]d3ae7a072008-12-05 20:27:20139 SyncMessageQueue::iterator iter = message_queue_.begin();
140 while (iter != message_queue_.end()) {
[email protected]17571642013-06-01 04:11:27141 if (iter->context.get() == context) {
[email protected]d3ae7a072008-12-05 20:27:20142 delete iter->message;
143 iter = message_queue_.erase(iter);
[email protected]522cc10d2012-01-11 22:39:54144 message_queue_version_++;
initial.commit09911bf2008-07-26 23:55:29145 } else {
[email protected]d3ae7a072008-12-05 20:27:20146 iter++;
initial.commit09911bf2008-07-26 23:55:29147 }
initial.commit09911bf2008-07-26 23:55:29148 }
[email protected]3cdb7af812008-10-24 19:21:13149
150 if (--listener_count_ == 0) {
151 DCHECK(lazy_tls_ptr_.Pointer()->Get());
152 lazy_tls_ptr_.Pointer()->Set(NULL);
153 }
initial.commit09911bf2008-07-26 23:55:29154 }
155
[email protected]1c4947f2009-01-15 22:25:11156 WaitableEvent* dispatch_event() { return &dispatch_event_; }
[email protected]b2432302012-07-02 21:15:52157 base::SingleThreadTaskRunner* listener_task_runner() {
[email protected]17571642013-06-01 04:11:27158 return listener_task_runner_.get();
[email protected]92bf9062011-05-02 18:00:49159 }
initial.commit09911bf2008-07-26 23:55:29160
[email protected]f886b7bf2008-09-10 10:54:06161 // Holds a pointer to the per-thread ReceivedSyncMsgQueue object.
162 static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> >
163 lazy_tls_ptr_;
164
initial.commit09911bf2008-07-26 23:55:29165 // Called on the ipc thread to check if we can unblock any current Send()
166 // calls based on a queued reply.
167 void DispatchReplies() {
initial.commit09911bf2008-07-26 23:55:29168 for (size_t i = 0; i < received_replies_.size(); ++i) {
169 Message* message = received_replies_[i].message;
[email protected]3cdb7af812008-10-24 19:21:13170 if (received_replies_[i].context->TryToUnblockListener(message)) {
initial.commit09911bf2008-07-26 23:55:29171 delete message;
172 received_replies_.erase(received_replies_.begin() + i);
173 return;
174 }
175 }
176 }
177
[email protected]ac0efda2009-10-14 16:22:02178 base::WaitableEventWatcher* top_send_done_watcher() {
179 return top_send_done_watcher_;
180 }
181
182 void set_top_send_done_watcher(base::WaitableEventWatcher* watcher) {
183 top_send_done_watcher_ = watcher;
184 }
185
[email protected]63a7bb82008-10-25 00:46:00186 private:
[email protected]877d55d2009-11-05 21:53:08187 friend class base::RefCountedThreadSafe<ReceivedSyncMsgQueue>;
188
[email protected]4df10d612008-11-12 00:38:26189 // See the comment in SyncChannel::SyncChannel for why this event is created
190 // as manual reset.
[email protected]63a7bb82008-10-25 00:46:00191 ReceivedSyncMsgQueue() :
[email protected]522cc10d2012-01-11 22:39:54192 message_queue_version_(0),
[email protected]1c4947f2009-01-15 22:25:11193 dispatch_event_(true, false),
[email protected]b2432302012-07-02 21:15:52194 listener_task_runner_(base::ThreadTaskRunnerHandle::Get()),
[email protected]1c4947f2009-01-15 22:25:11195 task_pending_(false),
[email protected]ac0efda2009-10-14 16:22:02196 listener_count_(0),
197 top_send_done_watcher_(NULL) {
[email protected]63a7bb82008-10-25 00:46:00198 }
199
[email protected]877d55d2009-11-05 21:53:08200 ~ReceivedSyncMsgQueue() {}
201
[email protected]d3ae7a072008-12-05 20:27:20202 // Holds information about a queued synchronous message or reply.
203 struct QueuedMessage {
204 QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { }
initial.commit09911bf2008-07-26 23:55:29205 Message* message;
206 scoped_refptr<SyncChannel::SyncContext> context;
207 };
208
[email protected]522cc10d2012-01-11 22:39:54209 typedef std::list<QueuedMessage> SyncMessageQueue;
[email protected]1c4947f2009-01-15 22:25:11210 SyncMessageQueue message_queue_;
tfarina10a5c062015-09-04 18:47:57211 uint32_t message_queue_version_; // Used to signal DispatchMessages to rescan
[email protected]d3ae7a072008-12-05 20:27:20212
213 std::vector<QueuedMessage> received_replies_;
[email protected]3cdb7af812008-10-24 19:21:13214
215 // Set when we got a synchronous message that we must respond to as the
216 // sender needs its reply before it can reply to our original synchronous
217 // message.
[email protected]1c4947f2009-01-15 22:25:11218 WaitableEvent dispatch_event_;
[email protected]b2432302012-07-02 21:15:52219 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_;
[email protected]20305ec2011-01-21 04:55:52220 base::Lock message_lock_;
[email protected]3cdb7af812008-10-24 19:21:13221 bool task_pending_;
222 int listener_count_;
[email protected]ac0efda2009-10-14 16:22:02223
224 // The current send done event watcher for this thread. Used to maintain
225 // a local global stack of send done watchers to ensure that nested sync
226 // message loops complete correctly.
227 base::WaitableEventWatcher* top_send_done_watcher_;
initial.commit09911bf2008-07-26 23:55:29228};
229
[email protected]f886b7bf2008-09-10 10:54:06230base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> >
[email protected]6de0fd1d2011-11-15 13:31:49231 SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_ =
232 LAZY_INSTANCE_INITIALIZER;
initial.commit09911bf2008-07-26 23:55:29233
234SyncChannel::SyncContext::SyncContext(
[email protected]b7f59e822012-06-29 22:05:26235 Listener* listener,
dchengfd033702014-08-28 16:59:29236 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
[email protected]1c4947f2009-01-15 22:25:11237 WaitableEvent* shutdown_event)
[email protected]b2432302012-07-02 21:15:52238 : ChannelProxy::Context(listener, ipc_task_runner),
[email protected]1c4947f2009-01-15 22:25:11239 received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()),
[email protected]54af05f2011-04-08 03:38:21240 shutdown_event_(shutdown_event),
[email protected]298ee7d2012-03-30 21:29:30241 restrict_dispatch_group_(kRestrictDispatchGroup_None) {
initial.commit09911bf2008-07-26 23:55:29242}
243
244SyncChannel::SyncContext::~SyncContext() {
245 while (!deserializers_.empty())
[email protected]3cdb7af812008-10-24 19:21:13246 Pop();
initial.commit09911bf2008-07-26 23:55:29247}
248
249// Adds information about an outgoing sync message to the context so that
250// we know how to deserialize the reply. Returns a handle that's set when
251// the reply has arrived.
[email protected]3cdb7af812008-10-24 19:21:13252void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) {
[email protected]4a180a52011-04-15 19:07:43253 // Create the tracking information for this message. This object is stored
254 // by value since all members are pointers that are cheap to copy. These
255 // pointers are cleaned up in the Pop() function.
256 //
[email protected]1c4947f2009-01-15 22:25:11257 // The event is created as manual reset because in between Signal and
[email protected]4df10d612008-11-12 00:38:26258 // OnObjectSignalled, another Send can happen which would stop the watcher
259 // from being called. The event would get watched later, when the nested
260 // Send completes, so the event will need to remain set.
[email protected]3cdb7af812008-10-24 19:21:13261 PendingSyncMsg pending(SyncMessage::GetMessageId(*sync_msg),
initial.commit09911bf2008-07-26 23:55:29262 sync_msg->GetReplyDeserializer(),
[email protected]1c4947f2009-01-15 22:25:11263 new WaitableEvent(true, false));
[email protected]20305ec2011-01-21 04:55:52264 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13265 deserializers_.push_back(pending);
initial.commit09911bf2008-07-26 23:55:29266}
267
[email protected]3cdb7af812008-10-24 19:21:13268bool SyncChannel::SyncContext::Pop() {
[email protected]63a7bb82008-10-25 00:46:00269 bool result;
270 {
[email protected]20305ec2011-01-21 04:55:52271 base::AutoLock auto_lock(deserializers_lock_);
[email protected]63a7bb82008-10-25 00:46:00272 PendingSyncMsg msg = deserializers_.back();
273 delete msg.deserializer;
[email protected]1c4947f2009-01-15 22:25:11274 delete msg.done_event;
275 msg.done_event = NULL;
[email protected]63a7bb82008-10-25 00:46:00276 deserializers_.pop_back();
277 result = msg.send_result;
278 }
279
280 // We got a reply to a synchronous Send() call that's blocking the listener
281 // thread. However, further down the call stack there could be another
282 // blocking Send() call, whose reply we received after we made this last
283 // Send() call. So check if we have any queued replies available that
284 // can now unblock the listener thread.
[email protected]b2432302012-07-02 21:15:52285 ipc_task_runner()->PostTask(
[email protected]72b6f8e22011-11-12 21:16:41286 FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchReplies,
287 received_sync_msgs_.get()));
[email protected]63a7bb82008-10-25 00:46:00288
289 return result;
[email protected]3cdb7af812008-10-24 19:21:13290}
291
[email protected]1c4947f2009-01-15 22:25:11292WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() {
[email protected]20305ec2011-01-21 04:55:52293 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13294 return deserializers_.back().done_event;
295}
296
[email protected]1c4947f2009-01-15 22:25:11297WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() {
[email protected]3cdb7af812008-10-24 19:21:13298 return received_sync_msgs_->dispatch_event();
initial.commit09911bf2008-07-26 23:55:29299}
300
301void SyncChannel::SyncContext::DispatchMessages() {
[email protected]54af05f2011-04-08 03:38:21302 received_sync_msgs_->DispatchMessages(this);
initial.commit09911bf2008-07-26 23:55:29303}
304
[email protected]3cdb7af812008-10-24 19:21:13305bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) {
[email protected]20305ec2011-01-21 04:55:52306 base::AutoLock auto_lock(deserializers_lock_);
[email protected]63a7bb82008-10-25 00:46:00307 if (deserializers_.empty() ||
308 !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) {
309 return false;
[email protected]3cdb7af812008-10-24 19:21:13310 }
initial.commit09911bf2008-07-26 23:55:29311
[email protected]63a7bb82008-10-25 00:46:00312 if (!msg->is_reply_error()) {
[email protected]211142cd2012-08-13 09:41:19313 bool send_result = deserializers_.back().deserializer->
[email protected]63a7bb82008-10-25 00:46:00314 SerializeOutputParameters(*msg);
[email protected]211142cd2012-08-13 09:41:19315 deserializers_.back().send_result = send_result;
bauerb3e9be732015-11-03 18:17:47316 DVLOG_IF(1, !send_result) << "Couldn't deserialize reply message";
[email protected]211142cd2012-08-13 09:41:19317 } else {
bauerb3e9be732015-11-03 18:17:47318 DVLOG(1) << "Received error reply";
[email protected]63a7bb82008-10-25 00:46:00319 }
[email protected]1c4947f2009-01-15 22:25:11320 deserializers_.back().done_event->Signal();
initial.commit09911bf2008-07-26 23:55:29321
[email protected]3cdb7af812008-10-24 19:21:13322 return true;
initial.commit09911bf2008-07-26 23:55:29323}
324
[email protected]3cdb7af812008-10-24 19:21:13325void SyncChannel::SyncContext::Clear() {
326 CancelPendingSends();
[email protected]d3ae7a072008-12-05 20:27:20327 received_sync_msgs_->RemoveContext(this);
[email protected]3cdb7af812008-10-24 19:21:13328 Context::Clear();
329}
330
[email protected]a95986a82010-12-24 06:19:28331bool SyncChannel::SyncContext::OnMessageReceived(const Message& msg) {
[email protected]d65cab7a2008-08-12 01:25:41332 // Give the filters a chance at processing this message.
333 if (TryFilters(msg))
[email protected]a95986a82010-12-24 06:19:28334 return true;
[email protected]d65cab7a2008-08-12 01:25:41335
[email protected]3cdb7af812008-10-24 19:21:13336 if (TryToUnblockListener(&msg))
[email protected]a95986a82010-12-24 06:19:28337 return true;
initial.commit09911bf2008-07-26 23:55:29338
[email protected]9134cce6d2012-04-10 20:07:53339 if (msg.is_reply()) {
340 received_sync_msgs_->QueueReply(msg, this);
[email protected]a95986a82010-12-24 06:19:28341 return true;
initial.commit09911bf2008-07-26 23:55:29342 }
343
[email protected]9134cce6d2012-04-10 20:07:53344 if (msg.should_unblock()) {
345 received_sync_msgs_->QueueMessage(msg, this);
[email protected]a95986a82010-12-24 06:19:28346 return true;
initial.commit09911bf2008-07-26 23:55:29347 }
348
[email protected]3cdb7af812008-10-24 19:21:13349 return Context::OnMessageReceivedNoFilter(msg);
initial.commit09911bf2008-07-26 23:55:29350}
351
initial.commit09911bf2008-07-26 23:55:29352void SyncChannel::SyncContext::OnChannelError() {
[email protected]3cdb7af812008-10-24 19:21:13353 CancelPendingSends();
[email protected]a4f822702009-02-06 00:44:53354 shutdown_watcher_.StopWatching();
initial.commit09911bf2008-07-26 23:55:29355 Context::OnChannelError();
356}
357
[email protected]3cdb7af812008-10-24 19:21:13358void SyncChannel::SyncContext::OnChannelOpened() {
[email protected]329be052013-02-04 18:14:28359 shutdown_watcher_.StartWatching(
360 shutdown_event_,
361 base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled,
362 base::Unretained(this)));
[email protected]3cdb7af812008-10-24 19:21:13363 Context::OnChannelOpened();
initial.commit09911bf2008-07-26 23:55:29364}
365
[email protected]3cdb7af812008-10-24 19:21:13366void SyncChannel::SyncContext::OnChannelClosed() {
[email protected]87339f02010-09-02 21:45:50367 CancelPendingSends();
[email protected]3cdb7af812008-10-24 19:21:13368 shutdown_watcher_.StopWatching();
369 Context::OnChannelClosed();
370}
371
372void SyncChannel::SyncContext::OnSendTimeout(int message_id) {
[email protected]20305ec2011-01-21 04:55:52373 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13374 PendingSyncMessageQueue::iterator iter;
bauerb3e9be732015-11-03 18:17:47375 DVLOG(1) << "Send timeout";
[email protected]3cdb7af812008-10-24 19:21:13376 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) {
[email protected]d3ae7a072008-12-05 20:27:20377 if (iter->id == message_id) {
[email protected]1c4947f2009-01-15 22:25:11378 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13379 break;
380 }
381 }
382}
383
384void SyncChannel::SyncContext::CancelPendingSends() {
[email protected]20305ec2011-01-21 04:55:52385 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13386 PendingSyncMessageQueue::iterator iter;
bauerb3e9be732015-11-03 18:17:47387 DVLOG(1) << "Canceling pending sends";
[email protected]3cdb7af812008-10-24 19:21:13388 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++)
[email protected]1c4947f2009-01-15 22:25:11389 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13390}
391
[email protected]1c4947f2009-01-15 22:25:11392void SyncChannel::SyncContext::OnWaitableEventSignaled(WaitableEvent* event) {
[email protected]9eec2252009-12-01 02:34:18393 if (event == shutdown_event_) {
394 // Process shut down before we can get a reply to a synchronous message.
395 // Cancel pending Send calls, which will end up setting the send done event.
396 CancelPendingSends();
397 } else {
398 // We got the reply, timed out or the process shutdown.
[email protected]5e0be642011-04-28 18:20:09399 DCHECK_EQ(GetSendDoneEvent(), event);
[email protected]fd0a773a2013-04-30 20:55:03400 base::MessageLoop::current()->QuitNow();
[email protected]9eec2252009-12-01 02:34:18401 }
[email protected]3cdb7af812008-10-24 19:21:13402}
403
[email protected]329be052013-02-04 18:14:28404base::WaitableEventWatcher::EventCallback
405 SyncChannel::SyncContext::MakeWaitableEventCallback() {
406 return base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled, this);
407}
[email protected]3cdb7af812008-10-24 19:21:13408
[email protected]fca876a12014-06-05 16:15:38409// static
410scoped_ptr<SyncChannel> SyncChannel::Create(
[email protected]42ce94e2010-12-08 19:28:09411 const IPC::ChannelHandle& channel_handle,
[email protected]3b0e4662014-06-02 20:29:30412 Channel::Mode mode,
[email protected]b7f59e822012-06-29 22:05:26413 Listener* listener,
dchengfd033702014-08-28 16:59:29414 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
[email protected]4b580bf2010-12-02 19:16:07415 bool create_pipe_now,
erikchen30dc2812015-09-24 03:26:38416 base::WaitableEvent* shutdown_event) {
[email protected]fca876a12014-06-05 16:15:38417 scoped_ptr<SyncChannel> channel =
418 Create(listener, ipc_task_runner, shutdown_event);
erikchen30dc2812015-09-24 03:26:38419 channel->Init(channel_handle, mode, create_pipe_now);
dchenge48600452015-12-28 02:24:50420 return channel;
[email protected]fca876a12014-06-05 16:15:38421}
422
423// static
424scoped_ptr<SyncChannel> SyncChannel::Create(
[email protected]64860882014-08-04 23:44:17425 scoped_ptr<ChannelFactory> factory,
426 Listener* listener,
dchengfd033702014-08-28 16:59:29427 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
[email protected]64860882014-08-04 23:44:17428 bool create_pipe_now,
429 base::WaitableEvent* shutdown_event) {
430 scoped_ptr<SyncChannel> channel =
431 Create(listener, ipc_task_runner, shutdown_event);
dchenge48600452015-12-28 02:24:50432 channel->Init(std::move(factory), create_pipe_now);
433 return channel;
[email protected]64860882014-08-04 23:44:17434}
435
436// static
437scoped_ptr<SyncChannel> SyncChannel::Create(
[email protected]fca876a12014-06-05 16:15:38438 Listener* listener,
dchengfd033702014-08-28 16:59:29439 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
[email protected]fca876a12014-06-05 16:15:38440 WaitableEvent* shutdown_event) {
441 return make_scoped_ptr(
442 new SyncChannel(listener, ipc_task_runner, shutdown_event));
[email protected]952394af2011-11-16 01:06:46443}
444
445SyncChannel::SyncChannel(
[email protected]b7f59e822012-06-29 22:05:26446 Listener* listener,
dchengfd033702014-08-28 16:59:29447 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
[email protected]952394af2011-11-16 01:06:46448 WaitableEvent* shutdown_event)
[email protected]5855f1d2014-04-16 16:50:43449 : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)) {
[email protected]1f9cf472014-04-17 05:07:18450 // The current (listener) thread must be distinct from the IPC thread, or else
451 // sending synchronous messages will deadlock.
dchengff04843f2014-09-03 18:01:16452 DCHECK_NE(ipc_task_runner.get(), base::ThreadTaskRunnerHandle::Get().get());
[email protected]952394af2011-11-16 01:06:46453 StartWatching();
initial.commit09911bf2008-07-26 23:55:29454}
455
456SyncChannel::~SyncChannel() {
initial.commit09911bf2008-07-26 23:55:29457}
458
[email protected]298ee7d2012-03-30 21:29:30459void SyncChannel::SetRestrictDispatchChannelGroup(int group) {
460 sync_context()->set_restrict_dispatch_group(group);
[email protected]54af05f2011-04-08 03:38:21461}
462
rockotac64ae92f2015-08-06 00:32:29463scoped_refptr<SyncMessageFilter> SyncChannel::CreateSyncMessageFilter() {
464 scoped_refptr<SyncMessageFilter> filter = new SyncMessageFilter(
465 sync_context()->shutdown_event(),
466 sync_context()->IsChannelSendThreadSafe());
467 AddFilter(filter.get());
rockot29ade1b2015-08-07 06:23:59468 if (!did_init())
469 pre_init_sync_message_filters_.push_back(filter);
rockotac64ae92f2015-08-06 00:32:29470 return filter;
471}
472
[email protected]3cdb7af812008-10-24 19:21:13473bool SyncChannel::Send(Message* message) {
[email protected]60b2c61f2012-08-22 22:39:57474#ifdef IPC_MESSAGE_LOG_ENABLED
[email protected]60b2c61f2012-08-22 22:39:57475 std::string name;
pkasting7bc277b2014-10-13 20:58:39476 Logging::GetInstance()->GetMessageText(message->type(), &name, message, NULL);
[email protected]d6cbf052014-05-02 21:29:24477 TRACE_EVENT1("ipc", "SyncChannel::Send", "name", name);
[email protected]60b2c61f2012-08-22 22:39:57478#else
[email protected]d6cbf052014-05-02 21:29:24479 TRACE_EVENT2("ipc", "SyncChannel::Send",
[email protected]60b2c61f2012-08-22 22:39:57480 "class", IPC_MESSAGE_ID_CLASS(message->type()),
481 "line", IPC_MESSAGE_ID_LINE(message->type()));
482#endif
[email protected]3cdb7af812008-10-24 19:21:13483 if (!message->is_sync()) {
484 ChannelProxy::Send(message);
485 return true;
initial.commit09911bf2008-07-26 23:55:29486 }
487
[email protected]3cdb7af812008-10-24 19:21:13488 // *this* might get deleted in WaitForReply.
489 scoped_refptr<SyncContext> context(sync_context());
[email protected]1c4947f2009-01-15 22:25:11490 if (context->shutdown_event()->IsSignaled()) {
bauerb3e9be732015-11-03 18:17:47491 DVLOG(1) << "shutdown event is signaled";
[email protected]3cdb7af812008-10-24 19:21:13492 delete message;
493 return false;
494 }
495
[email protected]3cdb7af812008-10-24 19:21:13496 SyncMessage* sync_msg = static_cast<SyncMessage*>(message);
497 context->Push(sync_msg);
[email protected]1c4947f2009-01-15 22:25:11498 WaitableEvent* pump_messages_event = sync_msg->pump_messages_event();
[email protected]3cdb7af812008-10-24 19:21:13499
initial.commit09911bf2008-07-26 23:55:29500 ChannelProxy::Send(message);
initial.commit09911bf2008-07-26 23:55:29501
[email protected]3cdb7af812008-10-24 19:21:13502 // Wait for reply, or for any other incoming synchronous messages.
[email protected]9eec2252009-12-01 02:34:18503 // *this* might get deleted, so only call static functions at this point.
[email protected]17571642013-06-01 04:11:27504 WaitForReply(context.get(), pump_messages_event);
initial.commit09911bf2008-07-26 23:55:29505
[email protected]3cdb7af812008-10-24 19:21:13506 return context->Pop();
initial.commit09911bf2008-07-26 23:55:29507}
508
[email protected]9eec2252009-12-01 02:34:18509void SyncChannel::WaitForReply(
510 SyncContext* context, WaitableEvent* pump_messages_event) {
[email protected]54af05f2011-04-08 03:38:21511 context->DispatchMessages();
[email protected]3cdb7af812008-10-24 19:21:13512 while (true) {
[email protected]1c4947f2009-01-15 22:25:11513 WaitableEvent* objects[] = {
[email protected]9eec2252009-12-01 02:34:18514 context->GetDispatchEvent(),
515 context->GetSendDoneEvent(),
[email protected]1c4947f2009-01-15 22:25:11516 pump_messages_event
517 };
518
519 unsigned count = pump_messages_event ? 3: 2;
[email protected]7dc8d792009-11-20 17:30:44520 size_t result = WaitableEvent::WaitMany(objects, count);
[email protected]1c4947f2009-01-15 22:25:11521 if (result == 0 /* dispatch event */) {
[email protected]3cdb7af812008-10-24 19:21:13522 // We're waiting for a reply, but we received a blocking synchronous
523 // call. We must process it or otherwise a deadlock might occur.
[email protected]9eec2252009-12-01 02:34:18524 context->GetDispatchEvent()->Reset();
525 context->DispatchMessages();
[email protected]3cdb7af812008-10-24 19:21:13526 continue;
527 }
528
[email protected]1c4947f2009-01-15 22:25:11529 if (result == 2 /* pump_messages_event */)
[email protected]9eec2252009-12-01 02:34:18530 WaitForReplyWithNestedMessageLoop(context); // Run a nested message loop.
[email protected]3cdb7af812008-10-24 19:21:13531
532 break;
533 }
534}
535
[email protected]9eec2252009-12-01 02:34:18536void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) {
[email protected]ac0efda2009-10-14 16:22:02537 base::WaitableEventWatcher send_done_watcher;
538
[email protected]9eec2252009-12-01 02:34:18539 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs();
[email protected]ac0efda2009-10-14 16:22:02540 DCHECK(sync_msg_queue != NULL);
541
542 base::WaitableEventWatcher* old_send_done_event_watcher =
543 sync_msg_queue->top_send_done_watcher();
544
[email protected]329be052013-02-04 18:14:28545 base::WaitableEventWatcher::EventCallback old_callback;
[email protected]ac0efda2009-10-14 16:22:02546 base::WaitableEvent* old_event = NULL;
547
548 // Maintain a local global stack of send done delegates to ensure that
549 // nested sync calls complete in the correct sequence, i.e. the
550 // outermost call completes first, etc.
551 if (old_send_done_event_watcher) {
[email protected]329be052013-02-04 18:14:28552 old_callback = old_send_done_event_watcher->callback();
[email protected]ac0efda2009-10-14 16:22:02553 old_event = old_send_done_event_watcher->GetWatchedEvent();
554 old_send_done_event_watcher->StopWatching();
555 }
556
557 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher);
558
[email protected]329be052013-02-04 18:14:28559 send_done_watcher.StartWatching(context->GetSendDoneEvent(),
560 context->MakeWaitableEventCallback());
[email protected]ac0efda2009-10-14 16:22:02561
[email protected]b5717a42012-02-14 19:33:52562 {
[email protected]fd0a773a2013-04-30 20:55:03563 base::MessageLoop::ScopedNestableTaskAllower allow(
564 base::MessageLoop::current());
565 base::MessageLoop::current()->Run();
[email protected]b5717a42012-02-14 19:33:52566 }
[email protected]ac0efda2009-10-14 16:22:02567
568 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher);
[email protected]424379802009-10-14 19:58:13569 if (old_send_done_event_watcher && old_event) {
[email protected]329be052013-02-04 18:14:28570 old_send_done_event_watcher->StartWatching(old_event, old_callback);
[email protected]ac0efda2009-10-14 16:22:02571 }
[email protected]3cdb7af812008-10-24 19:21:13572}
573
[email protected]1c4947f2009-01-15 22:25:11574void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) {
[email protected]9eec2252009-12-01 02:34:18575 DCHECK(event == sync_context()->GetDispatchEvent());
576 // The call to DispatchMessages might delete this object, so reregister
577 // the object watcher first.
578 event->Reset();
[email protected]329be052013-02-04 18:14:28579 dispatch_watcher_.StartWatching(event, dispatch_watcher_callback_);
[email protected]9eec2252009-12-01 02:34:18580 sync_context()->DispatchMessages();
initial.commit09911bf2008-07-26 23:55:29581}
582
[email protected]952394af2011-11-16 01:06:46583void SyncChannel::StartWatching() {
584 // Ideally we only want to watch this object when running a nested message
585 // loop. However, we don't know when it exits if there's another nested
586 // message loop running under it or not, so we wouldn't know whether to
587 // stop or keep watching. So we always watch it, and create the event as
588 // manual reset since the object watcher might otherwise reset the event
589 // when we're doing a WaitMany.
[email protected]329be052013-02-04 18:14:28590 dispatch_watcher_callback_ =
591 base::Bind(&SyncChannel::OnWaitableEventSignaled,
592 base::Unretained(this));
593 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(),
594 dispatch_watcher_callback_);
[email protected]952394af2011-11-16 01:06:46595}
596
rockot29ade1b2015-08-07 06:23:59597void SyncChannel::OnChannelInit() {
598 for (const auto& filter : pre_init_sync_message_filters_) {
599 filter->set_is_channel_send_thread_safe(
600 context()->IsChannelSendThreadSafe());
601 }
602 pre_init_sync_message_filters_.clear();
603}
604
initial.commit09911bf2008-07-26 23:55:29605} // namespace IPC