blob: 2fd1b117efca76a75db3a773e35d32e0ba9de533 [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"
rockotac64ae92f2015-08-06 00:32:2918#include "ipc/ipc_sync_message_filter.h"
initial.commit09911bf2008-07-26 23:55:2919
[email protected]7bf730952009-05-29 09:31:1520namespace base {
21class WaitableEvent;
22};
23
initial.commit09911bf2008-07-26 23:55:2924namespace IPC {
25
26class SyncMessage;
[email protected]64860882014-08-04 23:44:1727class ChannelFactory;
initial.commit09911bf2008-07-26 23:55:2928
[email protected]1e9499c2010-04-06 20:33:3629// This is similar to ChannelProxy, with the added feature of supporting sending
30// synchronous messages.
[email protected]4a180a52011-04-15 19:07:4331//
32// Overview of how the sync channel works
33// --------------------------------------
34// When the sending thread sends a synchronous message, we create a bunch
[email protected]5855f1d2014-04-16 16:50:4335// of tracking info (created in Send, stored in the PendingSyncMsg
[email protected]4a180a52011-04-15 19:07:4336// structure) associated with the message that we identify by the unique
37// "MessageId" on the SyncMessage. Among the things we save is the
38// "Deserializer" which is provided by the sync message. This object is in
39// charge of reading the parameters from the reply message and putting them in
40// the output variables provided by its caller.
41//
42// The info gets stashed in a queue since we could have a nested stack of sync
43// messages (each side could send sync messages in response to sync messages,
44// so it works like calling a function). The message is sent to the I/O thread
45// for dispatch and the original thread blocks waiting for the reply.
46//
47// SyncContext maintains the queue in a threadsafe way and listens for replies
48// on the I/O thread. When a reply comes in that matches one of the messages
49// it's looking for (using the unique message ID), it will execute the
50// deserializer stashed from before, and unblock the original thread.
51//
52//
53// Significant complexity results from the fact that messages are still coming
54// in while the original thread is blocked. Normal async messages are queued
55// and dispatched after the blocking call is complete. Sync messages must
56// be dispatched in a reentrant manner to avoid deadlock.
57//
58//
initial.commit09911bf2008-07-26 23:55:2959// Note that care must be taken that the lifetime of the ipc_thread argument
60// is more than this object. If the message loop goes away while this object
61// is running and it's used to send a message, then it will use the invalid
62// message loop pointer to proxy it to the ipc thread.
[email protected]329be052013-02-04 18:14:2863class IPC_EXPORT SyncChannel : public ChannelProxy {
initial.commit09911bf2008-07-26 23:55:2964 public:
[email protected]298ee7d2012-03-30 21:29:3065 enum RestrictDispatchGroup {
66 kRestrictDispatchGroup_None = 0,
67 };
68
[email protected]952394af2011-11-16 01:06:4669 // Creates and initializes a sync channel. If create_pipe_now is specified,
70 // the channel will be initialized synchronously.
[email protected]fca876a12014-06-05 16:15:3871 // The naming pattern follows IPC::Channel.
erikchen27aa7d82015-06-16 21:21:0472 // TODO(erikchen): Remove default parameter for |broker|. It exists only to
73 // make the upcoming refactor decomposable into smaller CLs.
74 // http://crbug.com/493414.
[email protected]fca876a12014-06-05 16:15:3875 static scoped_ptr<SyncChannel> Create(
76 const IPC::ChannelHandle& channel_handle,
77 IPC::Channel::Mode mode,
78 Listener* listener,
dchengfd033702014-08-28 16:59:2979 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
[email protected]fca876a12014-06-05 16:15:3880 bool create_pipe_now,
erikchen27aa7d82015-06-16 21:21:0481 base::WaitableEvent* shutdown_event,
82 AttachmentBroker* broker = nullptr);
[email protected]952394af2011-11-16 01:06:4683
[email protected]64860882014-08-04 23:44:1784 static scoped_ptr<SyncChannel> Create(
85 scoped_ptr<ChannelFactory> factory,
86 Listener* listener,
dchengfd033702014-08-28 16:59:2987 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
[email protected]64860882014-08-04 23:44:1788 bool create_pipe_now,
89 base::WaitableEvent* shutdown_event);
90
[email protected]952394af2011-11-16 01:06:4691 // Creates an uninitialized sync channel. Call ChannelProxy::Init to
92 // initialize the channel. This two-step setup allows message filters to be
93 // added before any messages are sent or received.
[email protected]fca876a12014-06-05 16:15:3894 static scoped_ptr<SyncChannel> Create(
95 Listener* listener,
dchengfd033702014-08-28 16:59:2996 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
[email protected]fca876a12014-06-05 16:15:3897 base::WaitableEvent* shutdown_event);
[email protected]952394af2011-11-16 01:06:4698
dchengfe61fca2014-10-22 02:29:5299 ~SyncChannel() override;
initial.commit09911bf2008-07-26 23:55:29100
dchengfe61fca2014-10-22 02:29:52101 bool Send(Message* message) override;
[email protected]d65cab7a2008-08-12 01:25:41102
[email protected]298ee7d2012-03-30 21:29:30103 // Sets the dispatch group for this channel, to only allow re-entrant dispatch
104 // of messages to other channels in the same group.
[email protected]54af05f2011-04-08 03:38:21105 //
106 // Normally, any unblocking message coming from any channel can be dispatched
107 // when any (possibly other) channel is blocked on sending a message. This is
108 // needed in some cases to unblock certain loops (e.g. necessary when some
109 // processes share a window hierarchy), but may cause re-entrancy issues in
110 // some cases where such loops are not possible. This flags allows the tagging
[email protected]298ee7d2012-03-30 21:29:30111 // of some particular channels to only re-enter in known correct cases.
112 //
113 // Incoming messages on channels belonging to a group that is not
114 // kRestrictDispatchGroup_None will only be dispatched while a sync message is
115 // being sent on a channel of the *same* group.
116 // Incoming messages belonging to the kRestrictDispatchGroup_None group (the
117 // default) will be dispatched in any case.
118 void SetRestrictDispatchChannelGroup(int group);
[email protected]54af05f2011-04-08 03:38:21119
rockotac64ae92f2015-08-06 00:32:29120 // Creates a new IPC::SyncMessageFilter and adds it to this SyncChannel.
121 // This should be used instead of directly constructing a new
122 // SyncMessageFilter.
123 scoped_refptr<IPC::SyncMessageFilter> CreateSyncMessageFilter();
124
initial.commit09911bf2008-07-26 23:55:29125 protected:
126 class ReceivedSyncMsgQueue;
127 friend class ReceivedSyncMsgQueue;
128
129 // SyncContext holds the per object data for SyncChannel, so that SyncChannel
130 // can be deleted while it's being used in a different thread. See
131 // ChannelProxy::Context for more information.
[email protected]329be052013-02-04 18:14:28132 class SyncContext : public Context {
initial.commit09911bf2008-07-26 23:55:29133 public:
dchengfd033702014-08-28 16:59:29134 SyncContext(
135 Listener* listener,
136 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
137 base::WaitableEvent* shutdown_event);
initial.commit09911bf2008-07-26 23:55:29138
initial.commit09911bf2008-07-26 23:55:29139 // Adds information about an outgoing sync message to the context so that
[email protected]3cdb7af812008-10-24 19:21:13140 // we know how to deserialize the reply.
[email protected]1e9499c2010-04-06 20:33:36141 void Push(SyncMessage* sync_msg);
initial.commit09911bf2008-07-26 23:55:29142
[email protected]3cdb7af812008-10-24 19:21:13143 // Cleanly remove the top deserializer (and throw it away). Returns the
144 // result of the Send call for that message.
145 bool Pop();
146
147 // Returns an event that's set when the send is complete, timed out or the
148 // process shut down.
[email protected]1c4947f2009-01-15 22:25:11149 base::WaitableEvent* GetSendDoneEvent();
initial.commit09911bf2008-07-26 23:55:29150
151 // Returns an event that's set when an incoming message that's not the reply
152 // needs to get dispatched (by calling SyncContext::DispatchMessages).
[email protected]1c4947f2009-01-15 22:25:11153 base::WaitableEvent* GetDispatchEvent();
initial.commit09911bf2008-07-26 23:55:29154
155 void DispatchMessages();
initial.commit09911bf2008-07-26 23:55:29156
157 // Checks if the given message is blocking the listener thread because of a
[email protected]d3216442009-03-05 21:07:27158 // synchronous send. If it is, the thread is unblocked and true is
159 // returned. Otherwise the function returns false.
[email protected]3cdb7af812008-10-24 19:21:13160 bool TryToUnblockListener(const Message* msg);
initial.commit09911bf2008-07-26 23:55:29161
[email protected]3cdb7af812008-10-24 19:21:13162 // Called on the IPC thread when a sync send that runs a nested message loop
163 // times out.
164 void OnSendTimeout(int message_id);
165
[email protected]1c4947f2009-01-15 22:25:11166 base::WaitableEvent* shutdown_event() { return shutdown_event_; }
[email protected]d65cab7a2008-08-12 01:25:41167
[email protected]ac0efda2009-10-14 16:22:02168 ReceivedSyncMsgQueue* received_sync_msgs() {
[email protected]17571642013-06-01 04:11:27169 return received_sync_msgs_.get();
[email protected]ac0efda2009-10-14 16:22:02170 }
171
[email protected]298ee7d2012-03-30 21:29:30172 void set_restrict_dispatch_group(int group) {
173 restrict_dispatch_group_ = group;
174 }
175
176 int restrict_dispatch_group() const {
177 return restrict_dispatch_group_;
178 }
[email protected]54af05f2011-04-08 03:38:21179
[email protected]329be052013-02-04 18:14:28180 base::WaitableEventWatcher::EventCallback MakeWaitableEventCallback();
181
initial.commit09911bf2008-07-26 23:55:29182 private:
dchengfe61fca2014-10-22 02:29:52183 ~SyncContext() override;
[email protected]1e9499c2010-04-06 20:33:36184 // ChannelProxy methods that we override.
[email protected]3cdb7af812008-10-24 19:21:13185
186 // Called on the listener thread.
dchengfe61fca2014-10-22 02:29:52187 void Clear() override;
[email protected]3cdb7af812008-10-24 19:21:13188
189 // Called on the IPC thread.
dchengfe61fca2014-10-22 02:29:52190 bool OnMessageReceived(const Message& msg) override;
191 void OnChannelError() override;
192 void OnChannelOpened() override;
193 void OnChannelClosed() override;
[email protected]3cdb7af812008-10-24 19:21:13194
195 // Cancels all pending Send calls.
196 void CancelPendingSends();
197
[email protected]329be052013-02-04 18:14:28198 void OnWaitableEventSignaled(base::WaitableEvent* event);
initial.commit09911bf2008-07-26 23:55:29199
[email protected]3cdb7af812008-10-24 19:21:13200 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue;
initial.commit09911bf2008-07-26 23:55:29201 PendingSyncMessageQueue deserializers_;
[email protected]20305ec2011-01-21 04:55:52202 base::Lock deserializers_lock_;
initial.commit09911bf2008-07-26 23:55:29203
[email protected]3cdb7af812008-10-24 19:21:13204 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_;
initial.commit09911bf2008-07-26 23:55:29205
[email protected]1c4947f2009-01-15 22:25:11206 base::WaitableEvent* shutdown_event_;
207 base::WaitableEventWatcher shutdown_watcher_;
[email protected]329be052013-02-04 18:14:28208 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_;
[email protected]298ee7d2012-03-30 21:29:30209 int restrict_dispatch_group_;
initial.commit09911bf2008-07-26 23:55:29210 };
211
212 private:
dchengfd033702014-08-28 16:59:29213 SyncChannel(
214 Listener* listener,
215 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
216 base::WaitableEvent* shutdown_event);
[email protected]fca876a12014-06-05 16:15:38217
[email protected]329be052013-02-04 18:14:28218 void OnWaitableEventSignaled(base::WaitableEvent* arg);
[email protected]3cdb7af812008-10-24 19:21:13219
[email protected]d3216442009-03-05 21:07:27220 SyncContext* sync_context() {
221 return reinterpret_cast<SyncContext*>(context());
222 }
initial.commit09911bf2008-07-26 23:55:29223
[email protected]3cdb7af812008-10-24 19:21:13224 // Both these functions wait for a reply, timeout or process shutdown. The
225 // latter one also runs a nested message loop in the meantime.
[email protected]9eec2252009-12-01 02:34:18226 static void WaitForReply(
227 SyncContext* context, base::WaitableEvent* pump_messages_event);
initial.commit09911bf2008-07-26 23:55:29228
[email protected]3cdb7af812008-10-24 19:21:13229 // Runs a nested message loop until a reply arrives, times out, or the process
230 // shuts down.
[email protected]9eec2252009-12-01 02:34:18231 static void WaitForReplyWithNestedMessageLoop(SyncContext* context);
initial.commit09911bf2008-07-26 23:55:29232
[email protected]952394af2011-11-16 01:06:46233 // Starts the dispatch watcher.
234 void StartWatching();
235
[email protected]3cdb7af812008-10-24 19:21:13236 // Used to signal events between the IPC and listener threads.
[email protected]1c4947f2009-01-15 22:25:11237 base::WaitableEventWatcher dispatch_watcher_;
[email protected]329be052013-02-04 18:14:28238 base::WaitableEventWatcher::EventCallback dispatch_watcher_callback_;
[email protected]3cdb7af812008-10-24 19:21:13239
[email protected]73a797fb2010-06-07 02:10:18240 DISALLOW_COPY_AND_ASSIGN(SyncChannel);
initial.commit09911bf2008-07-26 23:55:29241};
242
243} // namespace IPC
244
[email protected]320eff42011-11-15 00:29:48245#endif // IPC_IPC_SYNC_CHANNEL_H_