blob: 1ce9f6cf7bc25fabf5d7c6bc348bf215d309ebc0 [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
[email protected]72b6f8e22011-11-12 21:16:417#include "base/bind.h"
[email protected]60b2c61f2012-08-22 22:39:578#include "base/debug/trace_event.h"
[email protected]f886b7bf2008-09-10 10:54:069#include "base/lazy_instance.h"
[email protected]c62dd9d2011-09-21 18:05:4110#include "base/location.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/logging.h"
[email protected]44f9c952011-01-02 06:05:3912#include "base/synchronization/waitable_event.h"
13#include "base/synchronization/waitable_event_watcher.h"
[email protected]b2432302012-07-02 21:15:5214#include "base/thread_task_runner_handle.h"
15#include "base/threading/thread_local.h"
[email protected]60b2c61f2012-08-22 22:39:5716#include "ipc/ipc_logging.h"
17#include "ipc/ipc_message_macros.h"
[email protected]946d1b22009-07-22 23:57:2118#include "ipc/ipc_sync_message.h"
initial.commit09911bf2008-07-26 23:55:2919
[email protected]e1acf6f2008-10-27 20:43:3320using base::TimeDelta;
21using base::TimeTicks;
[email protected]1c4947f2009-01-15 22:25:1122using base::WaitableEvent;
initial.commit09911bf2008-07-26 23:55:2923
24namespace IPC {
25// When we're blocked in a Send(), we need to process incoming synchronous
26// messages right away because it could be blocking our reply (either
27// directly from the same object we're calling, or indirectly through one or
28// more other channels). That means that in SyncContext's OnMessageReceived,
29// we need to process sync message right away if we're blocked. However a
30// simple check isn't sufficient, because the listener thread can be in the
31// process of calling Send.
32// To work around this, when SyncChannel filters a sync message, it sets
33// an event that the listener thread waits on during its Send() call. This
34// allows us to dispatch incoming sync messages when blocked. The race
35// condition is handled because if Send is in the process of being called, it
36// will check the event. In case the listener thread isn't sending a message,
37// we queue a task on the listener thread to dispatch the received messages.
38// The messages are stored in this queue object that's shared among all
39// SyncChannel objects on the same thread (since one object can receive a
40// sync message while another one is blocked).
41
initial.commit09911bf2008-07-26 23:55:2942class SyncChannel::ReceivedSyncMsgQueue :
43 public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> {
44 public:
[email protected]3cdb7af812008-10-24 19:21:1345 // Returns the ReceivedSyncMsgQueue instance for this thread, creating one
[email protected]d3ae7a072008-12-05 20:27:2046 // if necessary. Call RemoveContext on the same thread when done.
47 static ReceivedSyncMsgQueue* AddContext() {
[email protected]3cdb7af812008-10-24 19:21:1348 // We want one ReceivedSyncMsgQueue per listener thread (i.e. since multiple
49 // SyncChannel objects can block the same thread).
50 ReceivedSyncMsgQueue* rv = lazy_tls_ptr_.Pointer()->Get();
51 if (!rv) {
52 rv = new ReceivedSyncMsgQueue();
53 ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(rv);
54 }
55 rv->listener_count_++;
56 return rv;
initial.commit09911bf2008-07-26 23:55:2957 }
58
initial.commit09911bf2008-07-26 23:55:2959 // Called on IPC thread when a synchronous message or reply arrives.
[email protected]d3ae7a072008-12-05 20:27:2060 void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2961 bool was_task_pending;
62 {
[email protected]20305ec2011-01-21 04:55:5263 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:2964
65 was_task_pending = task_pending_;
66 task_pending_ = true;
67
68 // We set the event in case the listener thread is blocked (or is about
69 // to). In case it's not, the PostTask dispatches the messages.
[email protected]d3ae7a072008-12-05 20:27:2070 message_queue_.push_back(QueuedMessage(new Message(msg), context));
[email protected]522cc10d2012-01-11 22:39:5471 message_queue_version_++;
initial.commit09911bf2008-07-26 23:55:2972 }
73
[email protected]1c4947f2009-01-15 22:25:1174 dispatch_event_.Signal();
initial.commit09911bf2008-07-26 23:55:2975 if (!was_task_pending) {
[email protected]b2432302012-07-02 21:15:5276 listener_task_runner_->PostTask(
[email protected]72b6f8e22011-11-12 21:16:4177 FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchMessagesTask,
78 this, scoped_refptr<SyncContext>(context)));
initial.commit09911bf2008-07-26 23:55:2979 }
80 }
81
82 void QueueReply(const Message &msg, SyncChannel::SyncContext* context) {
[email protected]d3ae7a072008-12-05 20:27:2083 received_replies_.push_back(QueuedMessage(new Message(msg), context));
initial.commit09911bf2008-07-26 23:55:2984 }
85
[email protected]d3ae7a072008-12-05 20:27:2086 // Called on the listener's thread to process any queues synchronous
initial.commit09911bf2008-07-26 23:55:2987 // messages.
[email protected]54af05f2011-04-08 03:38:2188 void DispatchMessagesTask(SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2989 {
[email protected]20305ec2011-01-21 04:55:5290 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:2991 task_pending_ = false;
92 }
[email protected]54af05f2011-04-08 03:38:2193 context->DispatchMessages();
initial.commit09911bf2008-07-26 23:55:2994 }
95
[email protected]54af05f2011-04-08 03:38:2196 void DispatchMessages(SyncContext* dispatching_context) {
[email protected]522cc10d2012-01-11 22:39:5497 bool first_time = true;
98 uint32 expected_version = 0;
99 SyncMessageQueue::iterator it;
initial.commit09911bf2008-07-26 23:55:29100 while (true) {
[email protected]522cc10d2012-01-11 22:39:54101 Message* message = NULL;
[email protected]d3ae7a072008-12-05 20:27:20102 scoped_refptr<SyncChannel::SyncContext> context;
initial.commit09911bf2008-07-26 23:55:29103 {
[email protected]20305ec2011-01-21 04:55:52104 base::AutoLock auto_lock(message_lock_);
[email protected]522cc10d2012-01-11 22:39:54105 if (first_time || message_queue_version_ != expected_version) {
106 it = message_queue_.begin();
107 first_time = false;
[email protected]54af05f2011-04-08 03:38:21108 }
[email protected]522cc10d2012-01-11 22:39:54109 for (; it != message_queue_.end(); it++) {
[email protected]298ee7d2012-03-30 21:29:30110 int message_group = it->context->restrict_dispatch_group();
111 if (message_group == kRestrictDispatchGroup_None ||
112 message_group == dispatching_context->restrict_dispatch_group()) {
[email protected]522cc10d2012-01-11 22:39:54113 message = it->message;
114 context = it->context;
115 it = message_queue_.erase(it);
116 message_queue_version_++;
117 expected_version = message_queue_version_;
118 break;
119 }
120 }
121 }
initial.commit09911bf2008-07-26 23:55:29122
[email protected]522cc10d2012-01-11 22:39:54123 if (message == NULL)
124 break;
125 context->OnDispatchMessage(*message);
126 delete message;
initial.commit09911bf2008-07-26 23:55:29127 }
128 }
129
initial.commit09911bf2008-07-26 23:55:29130 // SyncChannel calls this in its destructor.
[email protected]d3ae7a072008-12-05 20:27:20131 void RemoveContext(SyncContext* context) {
[email protected]20305ec2011-01-21 04:55:52132 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:29133
[email protected]d3ae7a072008-12-05 20:27:20134 SyncMessageQueue::iterator iter = message_queue_.begin();
135 while (iter != message_queue_.end()) {
136 if (iter->context == context) {
137 delete iter->message;
138 iter = message_queue_.erase(iter);
[email protected]522cc10d2012-01-11 22:39:54139 message_queue_version_++;
initial.commit09911bf2008-07-26 23:55:29140 } else {
[email protected]d3ae7a072008-12-05 20:27:20141 iter++;
initial.commit09911bf2008-07-26 23:55:29142 }
initial.commit09911bf2008-07-26 23:55:29143 }
[email protected]3cdb7af812008-10-24 19:21:13144
145 if (--listener_count_ == 0) {
146 DCHECK(lazy_tls_ptr_.Pointer()->Get());
147 lazy_tls_ptr_.Pointer()->Set(NULL);
148 }
initial.commit09911bf2008-07-26 23:55:29149 }
150
[email protected]1c4947f2009-01-15 22:25:11151 WaitableEvent* dispatch_event() { return &dispatch_event_; }
[email protected]b2432302012-07-02 21:15:52152 base::SingleThreadTaskRunner* listener_task_runner() {
153 return listener_task_runner_;
[email protected]92bf9062011-05-02 18:00:49154 }
initial.commit09911bf2008-07-26 23:55:29155
[email protected]f886b7bf2008-09-10 10:54:06156 // Holds a pointer to the per-thread ReceivedSyncMsgQueue object.
157 static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> >
158 lazy_tls_ptr_;
159
initial.commit09911bf2008-07-26 23:55:29160 // Called on the ipc thread to check if we can unblock any current Send()
161 // calls based on a queued reply.
162 void DispatchReplies() {
initial.commit09911bf2008-07-26 23:55:29163 for (size_t i = 0; i < received_replies_.size(); ++i) {
164 Message* message = received_replies_[i].message;
[email protected]3cdb7af812008-10-24 19:21:13165 if (received_replies_[i].context->TryToUnblockListener(message)) {
initial.commit09911bf2008-07-26 23:55:29166 delete message;
167 received_replies_.erase(received_replies_.begin() + i);
168 return;
169 }
170 }
171 }
172
[email protected]ac0efda2009-10-14 16:22:02173 base::WaitableEventWatcher* top_send_done_watcher() {
174 return top_send_done_watcher_;
175 }
176
177 void set_top_send_done_watcher(base::WaitableEventWatcher* watcher) {
178 top_send_done_watcher_ = watcher;
179 }
180
[email protected]63a7bb82008-10-25 00:46:00181 private:
[email protected]877d55d2009-11-05 21:53:08182 friend class base::RefCountedThreadSafe<ReceivedSyncMsgQueue>;
183
[email protected]4df10d612008-11-12 00:38:26184 // See the comment in SyncChannel::SyncChannel for why this event is created
185 // as manual reset.
[email protected]63a7bb82008-10-25 00:46:00186 ReceivedSyncMsgQueue() :
[email protected]522cc10d2012-01-11 22:39:54187 message_queue_version_(0),
[email protected]1c4947f2009-01-15 22:25:11188 dispatch_event_(true, false),
[email protected]b2432302012-07-02 21:15:52189 listener_task_runner_(base::ThreadTaskRunnerHandle::Get()),
[email protected]1c4947f2009-01-15 22:25:11190 task_pending_(false),
[email protected]ac0efda2009-10-14 16:22:02191 listener_count_(0),
192 top_send_done_watcher_(NULL) {
[email protected]63a7bb82008-10-25 00:46:00193 }
194
[email protected]877d55d2009-11-05 21:53:08195 ~ReceivedSyncMsgQueue() {}
196
[email protected]d3ae7a072008-12-05 20:27:20197 // Holds information about a queued synchronous message or reply.
198 struct QueuedMessage {
199 QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { }
initial.commit09911bf2008-07-26 23:55:29200 Message* message;
201 scoped_refptr<SyncChannel::SyncContext> context;
202 };
203
[email protected]522cc10d2012-01-11 22:39:54204 typedef std::list<QueuedMessage> SyncMessageQueue;
[email protected]1c4947f2009-01-15 22:25:11205 SyncMessageQueue message_queue_;
[email protected]522cc10d2012-01-11 22:39:54206 uint32 message_queue_version_; // Used to signal DispatchMessages to rescan
[email protected]d3ae7a072008-12-05 20:27:20207
208 std::vector<QueuedMessage> received_replies_;
[email protected]3cdb7af812008-10-24 19:21:13209
210 // Set when we got a synchronous message that we must respond to as the
211 // sender needs its reply before it can reply to our original synchronous
212 // message.
[email protected]1c4947f2009-01-15 22:25:11213 WaitableEvent dispatch_event_;
[email protected]b2432302012-07-02 21:15:52214 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_;
[email protected]20305ec2011-01-21 04:55:52215 base::Lock message_lock_;
[email protected]3cdb7af812008-10-24 19:21:13216 bool task_pending_;
217 int listener_count_;
[email protected]ac0efda2009-10-14 16:22:02218
219 // The current send done event watcher for this thread. Used to maintain
220 // a local global stack of send done watchers to ensure that nested sync
221 // message loops complete correctly.
222 base::WaitableEventWatcher* top_send_done_watcher_;
initial.commit09911bf2008-07-26 23:55:29223};
224
[email protected]f886b7bf2008-09-10 10:54:06225base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> >
[email protected]6de0fd1d2011-11-15 13:31:49226 SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_ =
227 LAZY_INSTANCE_INITIALIZER;
initial.commit09911bf2008-07-26 23:55:29228
229SyncChannel::SyncContext::SyncContext(
[email protected]b7f59e822012-06-29 22:05:26230 Listener* listener,
[email protected]b2432302012-07-02 21:15:52231 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]1c4947f2009-01-15 22:25:11232 WaitableEvent* shutdown_event)
[email protected]b2432302012-07-02 21:15:52233 : ChannelProxy::Context(listener, ipc_task_runner),
[email protected]1c4947f2009-01-15 22:25:11234 received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()),
[email protected]54af05f2011-04-08 03:38:21235 shutdown_event_(shutdown_event),
[email protected]298ee7d2012-03-30 21:29:30236 restrict_dispatch_group_(kRestrictDispatchGroup_None) {
initial.commit09911bf2008-07-26 23:55:29237}
238
239SyncChannel::SyncContext::~SyncContext() {
240 while (!deserializers_.empty())
[email protected]3cdb7af812008-10-24 19:21:13241 Pop();
initial.commit09911bf2008-07-26 23:55:29242}
243
244// Adds information about an outgoing sync message to the context so that
245// we know how to deserialize the reply. Returns a handle that's set when
246// the reply has arrived.
[email protected]3cdb7af812008-10-24 19:21:13247void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) {
[email protected]4a180a52011-04-15 19:07:43248 // Create the tracking information for this message. This object is stored
249 // by value since all members are pointers that are cheap to copy. These
250 // pointers are cleaned up in the Pop() function.
251 //
[email protected]1c4947f2009-01-15 22:25:11252 // The event is created as manual reset because in between Signal and
[email protected]4df10d612008-11-12 00:38:26253 // OnObjectSignalled, another Send can happen which would stop the watcher
254 // from being called. The event would get watched later, when the nested
255 // Send completes, so the event will need to remain set.
[email protected]3cdb7af812008-10-24 19:21:13256 PendingSyncMsg pending(SyncMessage::GetMessageId(*sync_msg),
initial.commit09911bf2008-07-26 23:55:29257 sync_msg->GetReplyDeserializer(),
[email protected]1c4947f2009-01-15 22:25:11258 new WaitableEvent(true, false));
[email protected]20305ec2011-01-21 04:55:52259 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13260 deserializers_.push_back(pending);
initial.commit09911bf2008-07-26 23:55:29261}
262
[email protected]3cdb7af812008-10-24 19:21:13263bool SyncChannel::SyncContext::Pop() {
[email protected]63a7bb82008-10-25 00:46:00264 bool result;
265 {
[email protected]20305ec2011-01-21 04:55:52266 base::AutoLock auto_lock(deserializers_lock_);
[email protected]63a7bb82008-10-25 00:46:00267 PendingSyncMsg msg = deserializers_.back();
268 delete msg.deserializer;
[email protected]1c4947f2009-01-15 22:25:11269 delete msg.done_event;
270 msg.done_event = NULL;
[email protected]63a7bb82008-10-25 00:46:00271 deserializers_.pop_back();
272 result = msg.send_result;
273 }
274
275 // We got a reply to a synchronous Send() call that's blocking the listener
276 // thread. However, further down the call stack there could be another
277 // blocking Send() call, whose reply we received after we made this last
278 // Send() call. So check if we have any queued replies available that
279 // can now unblock the listener thread.
[email protected]b2432302012-07-02 21:15:52280 ipc_task_runner()->PostTask(
[email protected]72b6f8e22011-11-12 21:16:41281 FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchReplies,
282 received_sync_msgs_.get()));
[email protected]63a7bb82008-10-25 00:46:00283
284 return result;
[email protected]3cdb7af812008-10-24 19:21:13285}
286
[email protected]1c4947f2009-01-15 22:25:11287WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() {
[email protected]20305ec2011-01-21 04:55:52288 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13289 return deserializers_.back().done_event;
290}
291
[email protected]1c4947f2009-01-15 22:25:11292WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() {
[email protected]3cdb7af812008-10-24 19:21:13293 return received_sync_msgs_->dispatch_event();
initial.commit09911bf2008-07-26 23:55:29294}
295
296void SyncChannel::SyncContext::DispatchMessages() {
[email protected]54af05f2011-04-08 03:38:21297 received_sync_msgs_->DispatchMessages(this);
initial.commit09911bf2008-07-26 23:55:29298}
299
[email protected]3cdb7af812008-10-24 19:21:13300bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) {
[email protected]20305ec2011-01-21 04:55:52301 base::AutoLock auto_lock(deserializers_lock_);
[email protected]63a7bb82008-10-25 00:46:00302 if (deserializers_.empty() ||
303 !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) {
304 return false;
[email protected]3cdb7af812008-10-24 19:21:13305 }
initial.commit09911bf2008-07-26 23:55:29306
[email protected]211142cd2012-08-13 09:41:19307 // TODO(bauerb): Remove logging once investigation of http://crbug.com/141055
308 // has finished.
[email protected]63a7bb82008-10-25 00:46:00309 if (!msg->is_reply_error()) {
[email protected]211142cd2012-08-13 09:41:19310 bool send_result = deserializers_.back().deserializer->
[email protected]63a7bb82008-10-25 00:46:00311 SerializeOutputParameters(*msg);
[email protected]211142cd2012-08-13 09:41:19312 deserializers_.back().send_result = send_result;
[email protected]33c66e32012-08-20 22:10:08313 VLOG_IF(1, !send_result) << "Couldn't deserialize reply message";
[email protected]211142cd2012-08-13 09:41:19314 } else {
[email protected]33c66e32012-08-20 22:10:08315 VLOG(1) << "Received error reply";
[email protected]63a7bb82008-10-25 00:46:00316 }
[email protected]1c4947f2009-01-15 22:25:11317 deserializers_.back().done_event->Signal();
initial.commit09911bf2008-07-26 23:55:29318
[email protected]3cdb7af812008-10-24 19:21:13319 return true;
initial.commit09911bf2008-07-26 23:55:29320}
321
[email protected]3cdb7af812008-10-24 19:21:13322void SyncChannel::SyncContext::Clear() {
323 CancelPendingSends();
[email protected]d3ae7a072008-12-05 20:27:20324 received_sync_msgs_->RemoveContext(this);
[email protected]3cdb7af812008-10-24 19:21:13325 Context::Clear();
326}
327
[email protected]a95986a82010-12-24 06:19:28328bool SyncChannel::SyncContext::OnMessageReceived(const Message& msg) {
[email protected]d65cab7a2008-08-12 01:25:41329 // Give the filters a chance at processing this message.
330 if (TryFilters(msg))
[email protected]a95986a82010-12-24 06:19:28331 return true;
[email protected]d65cab7a2008-08-12 01:25:41332
[email protected]3cdb7af812008-10-24 19:21:13333 if (TryToUnblockListener(&msg))
[email protected]a95986a82010-12-24 06:19:28334 return true;
initial.commit09911bf2008-07-26 23:55:29335
[email protected]9134cce6d2012-04-10 20:07:53336 if (msg.is_reply()) {
337 received_sync_msgs_->QueueReply(msg, this);
[email protected]a95986a82010-12-24 06:19:28338 return true;
initial.commit09911bf2008-07-26 23:55:29339 }
340
[email protected]9134cce6d2012-04-10 20:07:53341 if (msg.should_unblock()) {
342 received_sync_msgs_->QueueMessage(msg, this);
[email protected]a95986a82010-12-24 06:19:28343 return true;
initial.commit09911bf2008-07-26 23:55:29344 }
345
[email protected]3cdb7af812008-10-24 19:21:13346 return Context::OnMessageReceivedNoFilter(msg);
initial.commit09911bf2008-07-26 23:55:29347}
348
initial.commit09911bf2008-07-26 23:55:29349void SyncChannel::SyncContext::OnChannelError() {
[email protected]3cdb7af812008-10-24 19:21:13350 CancelPendingSends();
[email protected]a4f822702009-02-06 00:44:53351 shutdown_watcher_.StopWatching();
initial.commit09911bf2008-07-26 23:55:29352 Context::OnChannelError();
353}
354
[email protected]3cdb7af812008-10-24 19:21:13355void SyncChannel::SyncContext::OnChannelOpened() {
[email protected]329be052013-02-04 18:14:28356 shutdown_watcher_.StartWatching(
357 shutdown_event_,
358 base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled,
359 base::Unretained(this)));
[email protected]3cdb7af812008-10-24 19:21:13360 Context::OnChannelOpened();
initial.commit09911bf2008-07-26 23:55:29361}
362
[email protected]3cdb7af812008-10-24 19:21:13363void SyncChannel::SyncContext::OnChannelClosed() {
[email protected]87339f02010-09-02 21:45:50364 CancelPendingSends();
[email protected]3cdb7af812008-10-24 19:21:13365 shutdown_watcher_.StopWatching();
366 Context::OnChannelClosed();
367}
368
369void SyncChannel::SyncContext::OnSendTimeout(int message_id) {
[email protected]20305ec2011-01-21 04:55:52370 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13371 PendingSyncMessageQueue::iterator iter;
[email protected]33c66e32012-08-20 22:10:08372 VLOG(1) << "Send timeout";
[email protected]3cdb7af812008-10-24 19:21:13373 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) {
[email protected]d3ae7a072008-12-05 20:27:20374 if (iter->id == message_id) {
[email protected]1c4947f2009-01-15 22:25:11375 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13376 break;
377 }
378 }
379}
380
381void SyncChannel::SyncContext::CancelPendingSends() {
[email protected]20305ec2011-01-21 04:55:52382 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13383 PendingSyncMessageQueue::iterator iter;
[email protected]33c66e32012-08-20 22:10:08384 // TODO(bauerb): Remove once http://crbug/141055 is fixed.
385 VLOG(1) << "Canceling pending sends";
[email protected]3cdb7af812008-10-24 19:21:13386 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++)
[email protected]1c4947f2009-01-15 22:25:11387 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13388}
389
[email protected]1c4947f2009-01-15 22:25:11390void SyncChannel::SyncContext::OnWaitableEventSignaled(WaitableEvent* event) {
[email protected]9eec2252009-12-01 02:34:18391 if (event == shutdown_event_) {
392 // Process shut down before we can get a reply to a synchronous message.
393 // Cancel pending Send calls, which will end up setting the send done event.
394 CancelPendingSends();
395 } else {
396 // We got the reply, timed out or the process shutdown.
[email protected]5e0be642011-04-28 18:20:09397 DCHECK_EQ(GetSendDoneEvent(), event);
[email protected]781a7ed2010-02-23 07:12:22398 MessageLoop::current()->QuitNow();
[email protected]9eec2252009-12-01 02:34:18399 }
[email protected]3cdb7af812008-10-24 19:21:13400}
401
[email protected]329be052013-02-04 18:14:28402base::WaitableEventWatcher::EventCallback
403 SyncChannel::SyncContext::MakeWaitableEventCallback() {
404 return base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled, this);
405}
[email protected]3cdb7af812008-10-24 19:21:13406
407SyncChannel::SyncChannel(
[email protected]42ce94e2010-12-08 19:28:09408 const IPC::ChannelHandle& channel_handle,
[email protected]4b580bf2010-12-02 19:16:07409 Channel::Mode mode,
[email protected]b7f59e822012-06-29 22:05:26410 Listener* listener,
[email protected]b2432302012-07-02 21:15:52411 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]4b580bf2010-12-02 19:16:07412 bool create_pipe_now,
[email protected]1c4947f2009-01-15 22:25:11413 WaitableEvent* shutdown_event)
[email protected]b2432302012-07-02 21:15:52414 : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)),
[email protected]d65cab7a2008-08-12 01:25:41415 sync_messages_with_no_timeout_allowed_(true) {
[email protected]952394af2011-11-16 01:06:46416 ChannelProxy::Init(channel_handle, mode, create_pipe_now);
417 StartWatching();
418}
419
420SyncChannel::SyncChannel(
[email protected]b7f59e822012-06-29 22:05:26421 Listener* listener,
[email protected]b2432302012-07-02 21:15:52422 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]952394af2011-11-16 01:06:46423 WaitableEvent* shutdown_event)
[email protected]b2432302012-07-02 21:15:52424 : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)),
[email protected]952394af2011-11-16 01:06:46425 sync_messages_with_no_timeout_allowed_(true) {
426 StartWatching();
initial.commit09911bf2008-07-26 23:55:29427}
428
429SyncChannel::~SyncChannel() {
initial.commit09911bf2008-07-26 23:55:29430}
431
[email protected]298ee7d2012-03-30 21:29:30432void SyncChannel::SetRestrictDispatchChannelGroup(int group) {
433 sync_context()->set_restrict_dispatch_group(group);
[email protected]54af05f2011-04-08 03:38:21434}
435
[email protected]3cdb7af812008-10-24 19:21:13436bool SyncChannel::Send(Message* message) {
[email protected]aa96ae772009-01-20 22:08:15437 return SendWithTimeout(message, base::kNoTimeout);
[email protected]d65cab7a2008-08-12 01:25:41438}
439
[email protected]3cdb7af812008-10-24 19:21:13440bool SyncChannel::SendWithTimeout(Message* message, int timeout_ms) {
[email protected]60b2c61f2012-08-22 22:39:57441#ifdef IPC_MESSAGE_LOG_ENABLED
442 Logging* logger = Logging::GetInstance();
443 std::string name;
444 logger->GetMessageText(message->type(), &name, message, NULL);
445 TRACE_EVENT1("task", "SyncChannel::SendWithTimeout",
446 "name", name);
447#else
448 TRACE_EVENT2("task", "SyncChannel::SendWithTimeout",
449 "class", IPC_MESSAGE_ID_CLASS(message->type()),
450 "line", IPC_MESSAGE_ID_LINE(message->type()));
451#endif
[email protected]3cdb7af812008-10-24 19:21:13452 if (!message->is_sync()) {
453 ChannelProxy::Send(message);
454 return true;
initial.commit09911bf2008-07-26 23:55:29455 }
456
[email protected]3cdb7af812008-10-24 19:21:13457 // *this* might get deleted in WaitForReply.
458 scoped_refptr<SyncContext> context(sync_context());
[email protected]1c4947f2009-01-15 22:25:11459 if (context->shutdown_event()->IsSignaled()) {
[email protected]33c66e32012-08-20 22:10:08460 VLOG(1) << "shutdown event is signaled";
[email protected]3cdb7af812008-10-24 19:21:13461 delete message;
462 return false;
463 }
464
[email protected]d3216442009-03-05 21:07:27465 DCHECK(sync_messages_with_no_timeout_allowed_ ||
466 timeout_ms != base::kNoTimeout);
[email protected]3cdb7af812008-10-24 19:21:13467 SyncMessage* sync_msg = static_cast<SyncMessage*>(message);
468 context->Push(sync_msg);
469 int message_id = SyncMessage::GetMessageId(*sync_msg);
[email protected]1c4947f2009-01-15 22:25:11470 WaitableEvent* pump_messages_event = sync_msg->pump_messages_event();
[email protected]3cdb7af812008-10-24 19:21:13471
initial.commit09911bf2008-07-26 23:55:29472 ChannelProxy::Send(message);
initial.commit09911bf2008-07-26 23:55:29473
[email protected]aa96ae772009-01-20 22:08:15474 if (timeout_ms != base::kNoTimeout) {
[email protected]3cdb7af812008-10-24 19:21:13475 // We use the sync message id so that when a message times out, we don't
476 // confuse it with another send that is either above/below this Send in
477 // the call stack.
[email protected]b2432302012-07-02 21:15:52478 context->ipc_task_runner()->PostDelayedTask(
[email protected]72b6f8e22011-11-12 21:16:41479 FROM_HERE,
480 base::Bind(&SyncContext::OnSendTimeout, context.get(), message_id),
[email protected]5896fa042012-03-07 04:41:40481 base::TimeDelta::FromMilliseconds(timeout_ms));
[email protected]3cdb7af812008-10-24 19:21:13482 }
initial.commit09911bf2008-07-26 23:55:29483
[email protected]3cdb7af812008-10-24 19:21:13484 // Wait for reply, or for any other incoming synchronous messages.
[email protected]9eec2252009-12-01 02:34:18485 // *this* might get deleted, so only call static functions at this point.
486 WaitForReply(context, pump_messages_event);
initial.commit09911bf2008-07-26 23:55:29487
[email protected]3cdb7af812008-10-24 19:21:13488 return context->Pop();
initial.commit09911bf2008-07-26 23:55:29489}
490
[email protected]9eec2252009-12-01 02:34:18491void SyncChannel::WaitForReply(
492 SyncContext* context, WaitableEvent* pump_messages_event) {
[email protected]54af05f2011-04-08 03:38:21493 context->DispatchMessages();
[email protected]3cdb7af812008-10-24 19:21:13494 while (true) {
[email protected]1c4947f2009-01-15 22:25:11495 WaitableEvent* objects[] = {
[email protected]9eec2252009-12-01 02:34:18496 context->GetDispatchEvent(),
497 context->GetSendDoneEvent(),
[email protected]1c4947f2009-01-15 22:25:11498 pump_messages_event
499 };
500
501 unsigned count = pump_messages_event ? 3: 2;
[email protected]7dc8d792009-11-20 17:30:44502 size_t result = WaitableEvent::WaitMany(objects, count);
[email protected]1c4947f2009-01-15 22:25:11503 if (result == 0 /* dispatch event */) {
[email protected]3cdb7af812008-10-24 19:21:13504 // We're waiting for a reply, but we received a blocking synchronous
505 // call. We must process it or otherwise a deadlock might occur.
[email protected]9eec2252009-12-01 02:34:18506 context->GetDispatchEvent()->Reset();
507 context->DispatchMessages();
[email protected]3cdb7af812008-10-24 19:21:13508 continue;
509 }
510
[email protected]1c4947f2009-01-15 22:25:11511 if (result == 2 /* pump_messages_event */)
[email protected]9eec2252009-12-01 02:34:18512 WaitForReplyWithNestedMessageLoop(context); // Run a nested message loop.
[email protected]3cdb7af812008-10-24 19:21:13513
514 break;
515 }
516}
517
[email protected]9eec2252009-12-01 02:34:18518void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) {
[email protected]ac0efda2009-10-14 16:22:02519 base::WaitableEventWatcher send_done_watcher;
520
[email protected]9eec2252009-12-01 02:34:18521 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs();
[email protected]ac0efda2009-10-14 16:22:02522 DCHECK(sync_msg_queue != NULL);
523
524 base::WaitableEventWatcher* old_send_done_event_watcher =
525 sync_msg_queue->top_send_done_watcher();
526
[email protected]329be052013-02-04 18:14:28527 base::WaitableEventWatcher::EventCallback old_callback;
[email protected]ac0efda2009-10-14 16:22:02528 base::WaitableEvent* old_event = NULL;
529
530 // Maintain a local global stack of send done delegates to ensure that
531 // nested sync calls complete in the correct sequence, i.e. the
532 // outermost call completes first, etc.
533 if (old_send_done_event_watcher) {
[email protected]329be052013-02-04 18:14:28534 old_callback = old_send_done_event_watcher->callback();
[email protected]ac0efda2009-10-14 16:22:02535 old_event = old_send_done_event_watcher->GetWatchedEvent();
536 old_send_done_event_watcher->StopWatching();
537 }
538
539 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher);
540
[email protected]329be052013-02-04 18:14:28541 send_done_watcher.StartWatching(context->GetSendDoneEvent(),
542 context->MakeWaitableEventCallback());
[email protected]ac0efda2009-10-14 16:22:02543
[email protected]b5717a42012-02-14 19:33:52544 {
545 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
546 MessageLoop::current()->Run();
547 }
[email protected]ac0efda2009-10-14 16:22:02548
549 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher);
[email protected]424379802009-10-14 19:58:13550 if (old_send_done_event_watcher && old_event) {
[email protected]329be052013-02-04 18:14:28551 old_send_done_event_watcher->StartWatching(old_event, old_callback);
[email protected]ac0efda2009-10-14 16:22:02552 }
[email protected]3cdb7af812008-10-24 19:21:13553}
554
[email protected]1c4947f2009-01-15 22:25:11555void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) {
[email protected]9eec2252009-12-01 02:34:18556 DCHECK(event == sync_context()->GetDispatchEvent());
557 // The call to DispatchMessages might delete this object, so reregister
558 // the object watcher first.
559 event->Reset();
[email protected]329be052013-02-04 18:14:28560 dispatch_watcher_.StartWatching(event, dispatch_watcher_callback_);
[email protected]9eec2252009-12-01 02:34:18561 sync_context()->DispatchMessages();
initial.commit09911bf2008-07-26 23:55:29562}
563
[email protected]952394af2011-11-16 01:06:46564void SyncChannel::StartWatching() {
565 // Ideally we only want to watch this object when running a nested message
566 // loop. However, we don't know when it exits if there's another nested
567 // message loop running under it or not, so we wouldn't know whether to
568 // stop or keep watching. So we always watch it, and create the event as
569 // manual reset since the object watcher might otherwise reset the event
570 // when we're doing a WaitMany.
[email protected]329be052013-02-04 18:14:28571 dispatch_watcher_callback_ =
572 base::Bind(&SyncChannel::OnWaitableEventSignaled,
573 base::Unretained(this));
574 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(),
575 dispatch_watcher_callback_);
[email protected]952394af2011-11-16 01:06:46576}
577
initial.commit09911bf2008-07-26 23:55:29578} // namespace IPC