blob: 8984184da293ae81508293db635a12c4b6bac164 [file] [log] [blame]
[email protected]298ee7d2012-03-30 21:29:301// 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]320eff42011-11-15 00:29:485#ifndef IPC_IPC_SYNC_CHANNEL_H_
6#define IPC_IPC_SYNC_CHANNEL_H_
initial.commit09911bf2008-07-26 23:55:297
initial.commit09911bf2008-07-26 23:55:298#include <string>
[email protected]3cdb7af812008-10-24 19:21:139#include <deque>
[email protected]7bf730952009-05-29 09:31:1510
initial.commit09911bf2008-07-26 23:55:2911#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
[email protected]20305ec2011-01-21 04:55:5213#include "base/synchronization/lock.h"
[email protected]44f9c952011-01-02 06:05:3914#include "base/synchronization/waitable_event_watcher.h"
[email protected]42ce94e2010-12-08 19:28:0915#include "ipc/ipc_channel_handle.h"
[email protected]946d1b22009-07-22 23:57:2116#include "ipc/ipc_channel_proxy.h"
[email protected]1e9499c2010-04-06 20:33:3617#include "ipc/ipc_sync_message.h"
initial.commit09911bf2008-07-26 23:55:2918
[email protected]7bf730952009-05-29 09:31:1519namespace base {
20class WaitableEvent;
21};
22
initial.commit09911bf2008-07-26 23:55:2923namespace IPC {
24
25class SyncMessage;
26
[email protected]1e9499c2010-04-06 20:33:3627// This is similar to ChannelProxy, with the added feature of supporting sending
28// synchronous messages.
[email protected]4a180a52011-04-15 19:07:4329//
30// Overview of how the sync channel works
31// --------------------------------------
32// When the sending thread sends a synchronous message, we create a bunch
[email protected]5855f1d2014-04-16 16:50:4333// of tracking info (created in Send, stored in the PendingSyncMsg
[email protected]4a180a52011-04-15 19:07:4334// structure) associated with the message that we identify by the unique
35// "MessageId" on the SyncMessage. Among the things we save is the
36// "Deserializer" which is provided by the sync message. This object is in
37// charge of reading the parameters from the reply message and putting them in
38// the output variables provided by its caller.
39//
40// The info gets stashed in a queue since we could have a nested stack of sync
41// messages (each side could send sync messages in response to sync messages,
42// so it works like calling a function). The message is sent to the I/O thread
43// for dispatch and the original thread blocks waiting for the reply.
44//
45// SyncContext maintains the queue in a threadsafe way and listens for replies
46// on the I/O thread. When a reply comes in that matches one of the messages
47// it's looking for (using the unique message ID), it will execute the
48// deserializer stashed from before, and unblock the original thread.
49//
50//
51// Significant complexity results from the fact that messages are still coming
52// in while the original thread is blocked. Normal async messages are queued
53// and dispatched after the blocking call is complete. Sync messages must
54// be dispatched in a reentrant manner to avoid deadlock.
55//
56//
initial.commit09911bf2008-07-26 23:55:2957// Note that care must be taken that the lifetime of the ipc_thread argument
58// is more than this object. If the message loop goes away while this object
59// is running and it's used to send a message, then it will use the invalid
60// message loop pointer to proxy it to the ipc thread.
[email protected]329be052013-02-04 18:14:2861class IPC_EXPORT SyncChannel : public ChannelProxy {
initial.commit09911bf2008-07-26 23:55:2962 public:
[email protected]298ee7d2012-03-30 21:29:3063 enum RestrictDispatchGroup {
64 kRestrictDispatchGroup_None = 0,
65 };
66
[email protected]952394af2011-11-16 01:06:4667 // Creates and initializes a sync channel. If create_pipe_now is specified,
68 // the channel will be initialized synchronously.
[email protected]fca876a12014-06-05 16:15:3869 // The naming pattern follows IPC::Channel.
70 static scoped_ptr<SyncChannel> Create(
71 const IPC::ChannelHandle& channel_handle,
72 IPC::Channel::Mode mode,
73 Listener* listener,
74 base::SingleThreadTaskRunner* ipc_task_runner,
75 bool create_pipe_now,
76 base::WaitableEvent* shutdown_event);
[email protected]952394af2011-11-16 01:06:4677
78 // Creates an uninitialized sync channel. Call ChannelProxy::Init to
79 // initialize the channel. This two-step setup allows message filters to be
80 // added before any messages are sent or received.
[email protected]fca876a12014-06-05 16:15:3881 static scoped_ptr<SyncChannel> Create(
82 Listener* listener,
83 base::SingleThreadTaskRunner* ipc_task_runner,
84 base::WaitableEvent* shutdown_event);
[email protected]952394af2011-11-16 01:06:4685
[email protected]17acbb52009-08-28 17:29:0386 virtual ~SyncChannel();
initial.commit09911bf2008-07-26 23:55:2987
[email protected]2a026e52011-11-17 16:09:4488 virtual bool Send(Message* message) OVERRIDE;
[email protected]d65cab7a2008-08-12 01:25:4189
[email protected]298ee7d2012-03-30 21:29:3090 // Sets the dispatch group for this channel, to only allow re-entrant dispatch
91 // of messages to other channels in the same group.
[email protected]54af05f2011-04-08 03:38:2192 //
93 // Normally, any unblocking message coming from any channel can be dispatched
94 // when any (possibly other) channel is blocked on sending a message. This is
95 // needed in some cases to unblock certain loops (e.g. necessary when some
96 // processes share a window hierarchy), but may cause re-entrancy issues in
97 // some cases where such loops are not possible. This flags allows the tagging
[email protected]298ee7d2012-03-30 21:29:3098 // of some particular channels to only re-enter in known correct cases.
99 //
100 // Incoming messages on channels belonging to a group that is not
101 // kRestrictDispatchGroup_None will only be dispatched while a sync message is
102 // being sent on a channel of the *same* group.
103 // Incoming messages belonging to the kRestrictDispatchGroup_None group (the
104 // default) will be dispatched in any case.
105 void SetRestrictDispatchChannelGroup(int group);
[email protected]54af05f2011-04-08 03:38:21106
initial.commit09911bf2008-07-26 23:55:29107 protected:
108 class ReceivedSyncMsgQueue;
109 friend class ReceivedSyncMsgQueue;
110
111 // SyncContext holds the per object data for SyncChannel, so that SyncChannel
112 // can be deleted while it's being used in a different thread. See
113 // ChannelProxy::Context for more information.
[email protected]329be052013-02-04 18:14:28114 class SyncContext : public Context {
initial.commit09911bf2008-07-26 23:55:29115 public:
[email protected]b7f59e822012-06-29 22:05:26116 SyncContext(Listener* listener,
[email protected]b2432302012-07-02 21:15:52117 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]1c4947f2009-01-15 22:25:11118 base::WaitableEvent* shutdown_event);
initial.commit09911bf2008-07-26 23:55:29119
initial.commit09911bf2008-07-26 23:55:29120 // Adds information about an outgoing sync message to the context so that
[email protected]3cdb7af812008-10-24 19:21:13121 // we know how to deserialize the reply.
[email protected]1e9499c2010-04-06 20:33:36122 void Push(SyncMessage* sync_msg);
initial.commit09911bf2008-07-26 23:55:29123
[email protected]3cdb7af812008-10-24 19:21:13124 // Cleanly remove the top deserializer (and throw it away). Returns the
125 // result of the Send call for that message.
126 bool Pop();
127
128 // Returns an event that's set when the send is complete, timed out or the
129 // process shut down.
[email protected]1c4947f2009-01-15 22:25:11130 base::WaitableEvent* GetSendDoneEvent();
initial.commit09911bf2008-07-26 23:55:29131
132 // Returns an event that's set when an incoming message that's not the reply
133 // needs to get dispatched (by calling SyncContext::DispatchMessages).
[email protected]1c4947f2009-01-15 22:25:11134 base::WaitableEvent* GetDispatchEvent();
initial.commit09911bf2008-07-26 23:55:29135
136 void DispatchMessages();
initial.commit09911bf2008-07-26 23:55:29137
138 // Checks if the given message is blocking the listener thread because of a
[email protected]d3216442009-03-05 21:07:27139 // synchronous send. If it is, the thread is unblocked and true is
140 // returned. Otherwise the function returns false.
[email protected]3cdb7af812008-10-24 19:21:13141 bool TryToUnblockListener(const Message* msg);
initial.commit09911bf2008-07-26 23:55:29142
[email protected]3cdb7af812008-10-24 19:21:13143 // Called on the IPC thread when a sync send that runs a nested message loop
144 // times out.
145 void OnSendTimeout(int message_id);
146
[email protected]1c4947f2009-01-15 22:25:11147 base::WaitableEvent* shutdown_event() { return shutdown_event_; }
[email protected]d65cab7a2008-08-12 01:25:41148
[email protected]ac0efda2009-10-14 16:22:02149 ReceivedSyncMsgQueue* received_sync_msgs() {
[email protected]17571642013-06-01 04:11:27150 return received_sync_msgs_.get();
[email protected]ac0efda2009-10-14 16:22:02151 }
152
[email protected]298ee7d2012-03-30 21:29:30153 void set_restrict_dispatch_group(int group) {
154 restrict_dispatch_group_ = group;
155 }
156
157 int restrict_dispatch_group() const {
158 return restrict_dispatch_group_;
159 }
[email protected]54af05f2011-04-08 03:38:21160
[email protected]329be052013-02-04 18:14:28161 base::WaitableEventWatcher::EventCallback MakeWaitableEventCallback();
162
initial.commit09911bf2008-07-26 23:55:29163 private:
[email protected]3690ebe02011-05-25 09:08:19164 virtual ~SyncContext();
[email protected]1e9499c2010-04-06 20:33:36165 // ChannelProxy methods that we override.
[email protected]3cdb7af812008-10-24 19:21:13166
167 // Called on the listener thread.
[email protected]2a026e52011-11-17 16:09:44168 virtual void Clear() OVERRIDE;
[email protected]3cdb7af812008-10-24 19:21:13169
170 // Called on the IPC thread.
[email protected]2a026e52011-11-17 16:09:44171 virtual bool OnMessageReceived(const Message& msg) OVERRIDE;
172 virtual void OnChannelError() OVERRIDE;
173 virtual void OnChannelOpened() OVERRIDE;
174 virtual void OnChannelClosed() OVERRIDE;
[email protected]3cdb7af812008-10-24 19:21:13175
176 // Cancels all pending Send calls.
177 void CancelPendingSends();
178
[email protected]329be052013-02-04 18:14:28179 void OnWaitableEventSignaled(base::WaitableEvent* event);
initial.commit09911bf2008-07-26 23:55:29180
[email protected]3cdb7af812008-10-24 19:21:13181 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue;
initial.commit09911bf2008-07-26 23:55:29182 PendingSyncMessageQueue deserializers_;
[email protected]20305ec2011-01-21 04:55:52183 base::Lock deserializers_lock_;
initial.commit09911bf2008-07-26 23:55:29184
[email protected]3cdb7af812008-10-24 19:21:13185 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_;
initial.commit09911bf2008-07-26 23:55:29186
[email protected]1c4947f2009-01-15 22:25:11187 base::WaitableEvent* shutdown_event_;
188 base::WaitableEventWatcher shutdown_watcher_;
[email protected]329be052013-02-04 18:14:28189 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_;
[email protected]298ee7d2012-03-30 21:29:30190 int restrict_dispatch_group_;
initial.commit09911bf2008-07-26 23:55:29191 };
192
193 private:
[email protected]fca876a12014-06-05 16:15:38194 SyncChannel(Listener* listener,
195 base::SingleThreadTaskRunner* ipc_task_runner,
196 base::WaitableEvent* shutdown_event);
197
[email protected]329be052013-02-04 18:14:28198 void OnWaitableEventSignaled(base::WaitableEvent* arg);
[email protected]3cdb7af812008-10-24 19:21:13199
[email protected]d3216442009-03-05 21:07:27200 SyncContext* sync_context() {
201 return reinterpret_cast<SyncContext*>(context());
202 }
initial.commit09911bf2008-07-26 23:55:29203
[email protected]3cdb7af812008-10-24 19:21:13204 // Both these functions wait for a reply, timeout or process shutdown. The
205 // latter one also runs a nested message loop in the meantime.
[email protected]9eec2252009-12-01 02:34:18206 static void WaitForReply(
207 SyncContext* context, base::WaitableEvent* pump_messages_event);
initial.commit09911bf2008-07-26 23:55:29208
[email protected]3cdb7af812008-10-24 19:21:13209 // Runs a nested message loop until a reply arrives, times out, or the process
210 // shuts down.
[email protected]9eec2252009-12-01 02:34:18211 static void WaitForReplyWithNestedMessageLoop(SyncContext* context);
initial.commit09911bf2008-07-26 23:55:29212
[email protected]952394af2011-11-16 01:06:46213 // Starts the dispatch watcher.
214 void StartWatching();
215
[email protected]3cdb7af812008-10-24 19:21:13216 // Used to signal events between the IPC and listener threads.
[email protected]1c4947f2009-01-15 22:25:11217 base::WaitableEventWatcher dispatch_watcher_;
[email protected]329be052013-02-04 18:14:28218 base::WaitableEventWatcher::EventCallback dispatch_watcher_callback_;
[email protected]3cdb7af812008-10-24 19:21:13219
[email protected]73a797fb2010-06-07 02:10:18220 DISALLOW_COPY_AND_ASSIGN(SyncChannel);
initial.commit09911bf2008-07-26 23:55:29221};
222
223} // namespace IPC
224
[email protected]320eff42011-11-15 00:29:48225#endif // IPC_IPC_SYNC_CHANNEL_H_