blob: a54dbaa599bc566373a47356d897784491562464 [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
danakj03de39b22016-04-23 04:21:098#include <memory>
rockot29ade1b2015-08-07 06:23:599#include <string>
10#include <vector>
[email protected]7bf730952009-05-29 09:31:1511
Ken Rockot3044d212018-01-23 02:44:3912#include "base/component_export.h"
Brett Wilson55ff1475e2017-09-26 00:28:4813#include "base/containers/circular_deque.h"
tfarina4da8275d2015-09-16 09:56:2114#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
[email protected]20305ec2011-01-21 04:55:5216#include "base/synchronization/lock.h"
[email protected]44f9c952011-01-02 06:05:3917#include "base/synchronization/waitable_event_watcher.h"
[email protected]42ce94e2010-12-08 19:28:0918#include "ipc/ipc_channel_handle.h"
[email protected]946d1b22009-07-22 23:57:2119#include "ipc/ipc_channel_proxy.h"
[email protected]1e9499c2010-04-06 20:33:3620#include "ipc/ipc_sync_message.h"
rockotac64ae92f2015-08-06 00:32:2921#include "ipc/ipc_sync_message_filter.h"
rockot3c236292016-07-07 20:26:0222#include "mojo/public/c/system/types.h"
rockot9eadaba2017-03-15 23:57:4723#include "mojo/public/cpp/system/simple_watcher.h"
initial.commit09911bf2008-07-26 23:55:2924
[email protected]7bf730952009-05-29 09:31:1525namespace base {
rockotb62e2e32017-03-24 18:36:4426class RunLoop;
[email protected]7bf730952009-05-29 09:31:1527class WaitableEvent;
28};
29
rockot3c236292016-07-07 20:26:0230namespace mojo {
31class SyncHandleRegistry;
rockot3c236292016-07-07 20:26:0232}
33
initial.commit09911bf2008-07-26 23:55:2934namespace IPC {
35
rockotee0123e22016-06-24 17:27:3336class ChannelFactory;
rockot3c236292016-07-07 20:26:0237class SyncMessage;
initial.commit09911bf2008-07-26 23:55:2938
[email protected]1e9499c2010-04-06 20:33:3639// This is similar to ChannelProxy, with the added feature of supporting sending
40// synchronous messages.
[email protected]4a180a52011-04-15 19:07:4341//
42// Overview of how the sync channel works
43// --------------------------------------
44// When the sending thread sends a synchronous message, we create a bunch
[email protected]5855f1d2014-04-16 16:50:4345// of tracking info (created in Send, stored in the PendingSyncMsg
[email protected]4a180a52011-04-15 19:07:4346// structure) associated with the message that we identify by the unique
47// "MessageId" on the SyncMessage. Among the things we save is the
48// "Deserializer" which is provided by the sync message. This object is in
49// charge of reading the parameters from the reply message and putting them in
50// the output variables provided by its caller.
51//
52// The info gets stashed in a queue since we could have a nested stack of sync
53// messages (each side could send sync messages in response to sync messages,
54// so it works like calling a function). The message is sent to the I/O thread
55// for dispatch and the original thread blocks waiting for the reply.
56//
57// SyncContext maintains the queue in a threadsafe way and listens for replies
58// on the I/O thread. When a reply comes in that matches one of the messages
59// it's looking for (using the unique message ID), it will execute the
60// deserializer stashed from before, and unblock the original thread.
61//
62//
63// Significant complexity results from the fact that messages are still coming
64// in while the original thread is blocked. Normal async messages are queued
65// and dispatched after the blocking call is complete. Sync messages must
66// be dispatched in a reentrant manner to avoid deadlock.
67//
68//
initial.commit09911bf2008-07-26 23:55:2969// Note that care must be taken that the lifetime of the ipc_thread argument
70// is more than this object. If the message loop goes away while this object
71// is running and it's used to send a message, then it will use the invalid
72// message loop pointer to proxy it to the ipc thread.
Ken Rockot3044d212018-01-23 02:44:3973class COMPONENT_EXPORT(IPC) SyncChannel : public ChannelProxy {
initial.commit09911bf2008-07-26 23:55:2974 public:
[email protected]298ee7d2012-03-30 21:29:3075 enum RestrictDispatchGroup {
76 kRestrictDispatchGroup_None = 0,
77 };
78
[email protected]952394af2011-11-16 01:06:4679 // Creates and initializes a sync channel. If create_pipe_now is specified,
80 // the channel will be initialized synchronously.
[email protected]fca876a12014-06-05 16:15:3881 // The naming pattern follows IPC::Channel.
danakj03de39b22016-04-23 04:21:0982 static std::unique_ptr<SyncChannel> Create(
[email protected]fca876a12014-06-05 16:15:3883 const IPC::ChannelHandle& channel_handle,
84 IPC::Channel::Mode mode,
85 Listener* listener,
dchengfd033702014-08-28 16:59:2986 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Hajime Hoshiff15e972017-11-09 06:37:0987 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner,
[email protected]fca876a12014-06-05 16:15:3888 bool create_pipe_now,
erikchen30dc2812015-09-24 03:26:3889 base::WaitableEvent* shutdown_event);
[email protected]952394af2011-11-16 01:06:4690
danakj03de39b22016-04-23 04:21:0991 static std::unique_ptr<SyncChannel> Create(
92 std::unique_ptr<ChannelFactory> factory,
[email protected]64860882014-08-04 23:44:1793 Listener* listener,
dchengfd033702014-08-28 16:59:2994 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Hajime Hoshiff15e972017-11-09 06:37:0995 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner,
[email protected]64860882014-08-04 23:44:1796 bool create_pipe_now,
97 base::WaitableEvent* shutdown_event);
98
[email protected]952394af2011-11-16 01:06:4699 // Creates an uninitialized sync channel. Call ChannelProxy::Init to
100 // initialize the channel. This two-step setup allows message filters to be
101 // added before any messages are sent or received.
danakj03de39b22016-04-23 04:21:09102 static std::unique_ptr<SyncChannel> Create(
[email protected]fca876a12014-06-05 16:15:38103 Listener* listener,
dchengfd033702014-08-28 16:59:29104 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Hajime Hoshiff15e972017-11-09 06:37:09105 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner,
[email protected]fca876a12014-06-05 16:15:38106 base::WaitableEvent* shutdown_event);
[email protected]952394af2011-11-16 01:06:46107
dchengfe61fca2014-10-22 02:29:52108 ~SyncChannel() override;
initial.commit09911bf2008-07-26 23:55:29109
dchengfe61fca2014-10-22 02:29:52110 bool Send(Message* message) override;
[email protected]d65cab7a2008-08-12 01:25:41111
[email protected]298ee7d2012-03-30 21:29:30112 // Sets the dispatch group for this channel, to only allow re-entrant dispatch
113 // of messages to other channels in the same group.
[email protected]54af05f2011-04-08 03:38:21114 //
115 // Normally, any unblocking message coming from any channel can be dispatched
116 // when any (possibly other) channel is blocked on sending a message. This is
117 // needed in some cases to unblock certain loops (e.g. necessary when some
118 // processes share a window hierarchy), but may cause re-entrancy issues in
119 // some cases where such loops are not possible. This flags allows the tagging
[email protected]298ee7d2012-03-30 21:29:30120 // of some particular channels to only re-enter in known correct cases.
121 //
122 // Incoming messages on channels belonging to a group that is not
123 // kRestrictDispatchGroup_None will only be dispatched while a sync message is
124 // being sent on a channel of the *same* group.
125 // Incoming messages belonging to the kRestrictDispatchGroup_None group (the
126 // default) will be dispatched in any case.
127 void SetRestrictDispatchChannelGroup(int group);
[email protected]54af05f2011-04-08 03:38:21128
rockotac64ae92f2015-08-06 00:32:29129 // Creates a new IPC::SyncMessageFilter and adds it to this SyncChannel.
130 // This should be used instead of directly constructing a new
131 // SyncMessageFilter.
132 scoped_refptr<IPC::SyncMessageFilter> CreateSyncMessageFilter();
133
initial.commit09911bf2008-07-26 23:55:29134 protected:
135 class ReceivedSyncMsgQueue;
136 friend class ReceivedSyncMsgQueue;
137
138 // SyncContext holds the per object data for SyncChannel, so that SyncChannel
139 // can be deleted while it's being used in a different thread. See
140 // ChannelProxy::Context for more information.
[email protected]329be052013-02-04 18:14:28141 class SyncContext : public Context {
initial.commit09911bf2008-07-26 23:55:29142 public:
dchengfd033702014-08-28 16:59:29143 SyncContext(
144 Listener* listener,
145 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Hajime Hoshiff15e972017-11-09 06:37:09146 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner,
dchengfd033702014-08-28 16:59:29147 base::WaitableEvent* shutdown_event);
initial.commit09911bf2008-07-26 23:55:29148
initial.commit09911bf2008-07-26 23:55:29149 // Adds information about an outgoing sync message to the context so that
[email protected]3cdb7af812008-10-24 19:21:13150 // we know how to deserialize the reply.
rockot3c236292016-07-07 20:26:02151 bool Push(SyncMessage* sync_msg);
initial.commit09911bf2008-07-26 23:55:29152
[email protected]3cdb7af812008-10-24 19:21:13153 // Cleanly remove the top deserializer (and throw it away). Returns the
154 // result of the Send call for that message.
155 bool Pop();
156
rockot3c236292016-07-07 20:26:02157 // Returns a Mojo Event that signals when a sync send is complete or timed
158 // out or the process shut down.
rockotb62e2e32017-03-24 18:36:44159 base::WaitableEvent* GetSendDoneEvent();
initial.commit09911bf2008-07-26 23:55:29160
rockot3c236292016-07-07 20:26:02161 // Returns a Mojo Event that signals when an incoming message that's not the
162 // pending reply needs to get dispatched (by calling DispatchMessages.)
rockotb62e2e32017-03-24 18:36:44163 base::WaitableEvent* GetDispatchEvent();
initial.commit09911bf2008-07-26 23:55:29164
165 void DispatchMessages();
initial.commit09911bf2008-07-26 23:55:29166
167 // Checks if the given message is blocking the listener thread because of a
[email protected]d3216442009-03-05 21:07:27168 // synchronous send. If it is, the thread is unblocked and true is
169 // returned. Otherwise the function returns false.
[email protected]3cdb7af812008-10-24 19:21:13170 bool TryToUnblockListener(const Message* msg);
initial.commit09911bf2008-07-26 23:55:29171
[email protected]1c4947f2009-01-15 22:25:11172 base::WaitableEvent* shutdown_event() { return shutdown_event_; }
[email protected]d65cab7a2008-08-12 01:25:41173
[email protected]ac0efda2009-10-14 16:22:02174 ReceivedSyncMsgQueue* received_sync_msgs() {
[email protected]17571642013-06-01 04:11:27175 return received_sync_msgs_.get();
[email protected]ac0efda2009-10-14 16:22:02176 }
177
[email protected]298ee7d2012-03-30 21:29:30178 void set_restrict_dispatch_group(int group) {
179 restrict_dispatch_group_ = group;
180 }
181
182 int restrict_dispatch_group() const {
183 return restrict_dispatch_group_;
184 }
[email protected]54af05f2011-04-08 03:38:21185
rockotb62e2e32017-03-24 18:36:44186 void OnSendDoneEventSignaled(base::RunLoop* nested_loop,
187 base::WaitableEvent* event);
188
initial.commit09911bf2008-07-26 23:55:29189 private:
dchengfe61fca2014-10-22 02:29:52190 ~SyncContext() override;
[email protected]1e9499c2010-04-06 20:33:36191 // ChannelProxy methods that we override.
[email protected]3cdb7af812008-10-24 19:21:13192
193 // Called on the listener thread.
dchengfe61fca2014-10-22 02:29:52194 void Clear() override;
[email protected]3cdb7af812008-10-24 19:21:13195
196 // Called on the IPC thread.
dchengfe61fca2014-10-22 02:29:52197 bool OnMessageReceived(const Message& msg) override;
198 void OnChannelError() override;
rockot10188752016-09-08 18:24:56199 void OnChannelOpened() override;
dchengfe61fca2014-10-22 02:29:52200 void OnChannelClosed() override;
[email protected]3cdb7af812008-10-24 19:21:13201
202 // Cancels all pending Send calls.
203 void CancelPendingSends();
204
rockot3c236292016-07-07 20:26:02205 void OnShutdownEventSignaled(base::WaitableEvent* event);
initial.commit09911bf2008-07-26 23:55:29206
Brett Wilson55ff1475e2017-09-26 00:28:48207 using PendingSyncMessageQueue = base::circular_deque<PendingSyncMsg>;
initial.commit09911bf2008-07-26 23:55:29208 PendingSyncMessageQueue deserializers_;
rockot3c236292016-07-07 20:26:02209 bool reject_new_deserializers_ = false;
[email protected]20305ec2011-01-21 04:55:52210 base::Lock deserializers_lock_;
initial.commit09911bf2008-07-26 23:55:29211
[email protected]3cdb7af812008-10-24 19:21:13212 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_;
initial.commit09911bf2008-07-26 23:55:29213
[email protected]1c4947f2009-01-15 22:25:11214 base::WaitableEvent* shutdown_event_;
215 base::WaitableEventWatcher shutdown_watcher_;
[email protected]329be052013-02-04 18:14:28216 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_;
[email protected]298ee7d2012-03-30 21:29:30217 int restrict_dispatch_group_;
initial.commit09911bf2008-07-26 23:55:29218 };
219
220 private:
dchengfd033702014-08-28 16:59:29221 SyncChannel(
222 Listener* listener,
223 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Hajime Hoshiff15e972017-11-09 06:37:09224 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner,
dchengfd033702014-08-28 16:59:29225 base::WaitableEvent* shutdown_event);
[email protected]fca876a12014-06-05 16:15:38226
rockotb62e2e32017-03-24 18:36:44227 void OnDispatchEventSignaled(base::WaitableEvent* event);
[email protected]3cdb7af812008-10-24 19:21:13228
[email protected]d3216442009-03-05 21:07:27229 SyncContext* sync_context() {
230 return reinterpret_cast<SyncContext*>(context());
231 }
initial.commit09911bf2008-07-26 23:55:29232
[email protected]3cdb7af812008-10-24 19:21:13233 // Both these functions wait for a reply, timeout or process shutdown. The
gab2998ee72017-05-05 16:23:50234 // latter one also runs a nested run loop in the meantime.
rockot3c236292016-07-07 20:26:02235 static void WaitForReply(mojo::SyncHandleRegistry* registry,
236 SyncContext* context,
237 bool pump_messages);
initial.commit09911bf2008-07-26 23:55:29238
gab2998ee72017-05-05 16:23:50239 // Runs a nested run loop until a reply arrives, times out, or the process
[email protected]3cdb7af812008-10-24 19:21:13240 // shuts down.
[email protected]9eec2252009-12-01 02:34:18241 static void WaitForReplyWithNestedMessageLoop(SyncContext* context);
initial.commit09911bf2008-07-26 23:55:29242
[email protected]952394af2011-11-16 01:06:46243 // Starts the dispatch watcher.
244 void StartWatching();
245
rockot29ade1b2015-08-07 06:23:59246 // ChannelProxy overrides:
247 void OnChannelInit() override;
248
rockot3c236292016-07-07 20:26:02249 scoped_refptr<mojo::SyncHandleRegistry> sync_handle_registry_;
250
[email protected]3cdb7af812008-10-24 19:21:13251 // Used to signal events between the IPC and listener threads.
rockotb62e2e32017-03-24 18:36:44252 base::WaitableEventWatcher dispatch_watcher_;
253 base::WaitableEventWatcher::EventCallback dispatch_watcher_callback_;
[email protected]3cdb7af812008-10-24 19:21:13254
rockot29ade1b2015-08-07 06:23:59255 // Tracks SyncMessageFilters created before complete channel initialization.
256 std::vector<scoped_refptr<SyncMessageFilter>> pre_init_sync_message_filters_;
257
[email protected]73a797fb2010-06-07 02:10:18258 DISALLOW_COPY_AND_ASSIGN(SyncChannel);
initial.commit09911bf2008-07-26 23:55:29259};
260
261} // namespace IPC
262
[email protected]320eff42011-11-15 00:29:48263#endif // IPC_IPC_SYNC_CHANNEL_H_