[go: nahoru, domu]

blob: f83f4d626caa61a093cdf968fc8ff74e6a9b75d8 [file] [log] [blame]
piman@chromium.org298ee7d2012-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
thestig@chromium.org320eff42011-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
danakj03de39b2016-04-23 04:21:098#include <memory>
rockot29ade1b2015-08-07 06:23:599#include <string>
10#include <vector>
phajdan.jr@chromium.org7bf730952009-05-29 09:31:1511
Ken Rockot3044d212018-01-23 02:44:3912#include "base/component_export.h"
Brett Wilson55ff1472017-09-26 00:28:4813#include "base/containers/circular_deque.h"
tfarina4da82752015-09-16 09:56:2114#include "base/macros.h"
levin@chromium.org3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
brettw@chromium.org20305ec2011-01-21 04:55:5216#include "base/synchronization/lock.h"
brettw@chromium.org44f9c952011-01-02 06:05:3917#include "base/synchronization/waitable_event_watcher.h"
dmaclach@chromium.org42ce94e2010-12-08 19:28:0918#include "ipc/ipc_channel_handle.h"
agl@chromium.org946d1b22009-07-22 23:57:2119#include "ipc/ipc_channel_proxy.h"
jabdelmalek@google.com1e9499c2010-04-06 20:33:3620#include "ipc/ipc_sync_message.h"
rockotac64ae92015-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
phajdan.jr@chromium.org7bf730952009-05-29 09:31:1525namespace base {
rockotb62e2e32017-03-24 18:36:4426class RunLoop;
phajdan.jr@chromium.org7bf730952009-05-29 09:31:1527class WaitableEvent;
Nico Weber40cd26fd2019-02-09 17:37:0228} // namespace base
phajdan.jr@chromium.org7bf730952009-05-29 09:31:1529
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
rockot3c236292016-07-07 20:26:0236class SyncMessage;
initial.commit09911bf2008-07-26 23:55:2937
jabdelmalek@google.com1e9499c2010-04-06 20:33:3638// This is similar to ChannelProxy, with the added feature of supporting sending
39// synchronous messages.
brettw@chromium.org4a180a52011-04-15 19:07:4340//
41// Overview of how the sync channel works
42// --------------------------------------
43// When the sending thread sends a synchronous message, we create a bunch
piman@chromium.org5855f1d2014-04-16 16:50:4344// of tracking info (created in Send, stored in the PendingSyncMsg
brettw@chromium.org4a180a52011-04-15 19:07:4345// structure) associated with the message that we identify by the unique
46// "MessageId" on the SyncMessage. Among the things we save is the
47// "Deserializer" which is provided by the sync message. This object is in
48// charge of reading the parameters from the reply message and putting them in
49// the output variables provided by its caller.
50//
51// The info gets stashed in a queue since we could have a nested stack of sync
52// messages (each side could send sync messages in response to sync messages,
53// so it works like calling a function). The message is sent to the I/O thread
54// for dispatch and the original thread blocks waiting for the reply.
55//
56// SyncContext maintains the queue in a threadsafe way and listens for replies
57// on the I/O thread. When a reply comes in that matches one of the messages
58// it's looking for (using the unique message ID), it will execute the
59// deserializer stashed from before, and unblock the original thread.
60//
61//
62// Significant complexity results from the fact that messages are still coming
63// in while the original thread is blocked. Normal async messages are queued
64// and dispatched after the blocking call is complete. Sync messages must
65// be dispatched in a reentrant manner to avoid deadlock.
66//
67//
initial.commit09911bf2008-07-26 23:55:2968// Note that care must be taken that the lifetime of the ipc_thread argument
69// is more than this object. If the message loop goes away while this object
70// is running and it's used to send a message, then it will use the invalid
71// message loop pointer to proxy it to the ipc thread.
Ken Rockot3044d212018-01-23 02:44:3972class COMPONENT_EXPORT(IPC) SyncChannel : public ChannelProxy {
initial.commit09911bf2008-07-26 23:55:2973 public:
piman@chromium.org298ee7d2012-03-30 21:29:3074 enum RestrictDispatchGroup {
75 kRestrictDispatchGroup_None = 0,
76 };
77
kkania@chromium.org952394af2011-11-16 01:06:4678 // Creates and initializes a sync channel. If create_pipe_now is specified,
79 // the channel will be initialized synchronously.
morrita@chromium.orgfca876a12014-06-05 16:15:3880 // The naming pattern follows IPC::Channel.
danakj03de39b2016-04-23 04:21:0981 static std::unique_ptr<SyncChannel> Create(
morrita@chromium.orgfca876a12014-06-05 16:15:3882 const IPC::ChannelHandle& channel_handle,
83 IPC::Channel::Mode mode,
84 Listener* listener,
dchengfd033702014-08-28 16:59:2985 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Hajime Hoshiff15e972017-11-09 06:37:0986 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner,
morrita@chromium.orgfca876a12014-06-05 16:15:3887 bool create_pipe_now,
erikchen30dc2812015-09-24 03:26:3888 base::WaitableEvent* shutdown_event);
kkania@chromium.org952394af2011-11-16 01:06:4689
90 // Creates an uninitialized sync channel. Call ChannelProxy::Init to
91 // initialize the channel. This two-step setup allows message filters to be
92 // added before any messages are sent or received.
danakj03de39b2016-04-23 04:21:0993 static std::unique_ptr<SyncChannel> Create(
morrita@chromium.orgfca876a12014-06-05 16:15:3894 Listener* listener,
dchengfd033702014-08-28 16:59:2995 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Hajime Hoshiff15e972017-11-09 06:37:0996 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner,
morrita@chromium.orgfca876a12014-06-05 16:15:3897 base::WaitableEvent* shutdown_event);
kkania@chromium.org952394af2011-11-16 01:06:4698
Yuzu Saijo8866b1d2019-05-24 03:23:2899 void AddListenerTaskRunner(
100 int32_t routing_id,
101 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
102
103 void RemoveListenerTaskRunner(int32_t routing_id);
104
dchengfe61fca2014-10-22 02:29:52105 ~SyncChannel() override;
initial.commit09911bf2008-07-26 23:55:29106
dchengfe61fca2014-10-22 02:29:52107 bool Send(Message* message) override;
jcampan@google.comd65cab7a2008-08-12 01:25:41108
piman@chromium.org298ee7d2012-03-30 21:29:30109 // Sets the dispatch group for this channel, to only allow re-entrant dispatch
110 // of messages to other channels in the same group.
piman@google.com54af05f2011-04-08 03:38:21111 //
112 // Normally, any unblocking message coming from any channel can be dispatched
113 // when any (possibly other) channel is blocked on sending a message. This is
114 // needed in some cases to unblock certain loops (e.g. necessary when some
115 // processes share a window hierarchy), but may cause re-entrancy issues in
116 // some cases where such loops are not possible. This flags allows the tagging
piman@chromium.org298ee7d2012-03-30 21:29:30117 // of some particular channels to only re-enter in known correct cases.
118 //
119 // Incoming messages on channels belonging to a group that is not
120 // kRestrictDispatchGroup_None will only be dispatched while a sync message is
121 // being sent on a channel of the *same* group.
122 // Incoming messages belonging to the kRestrictDispatchGroup_None group (the
123 // default) will be dispatched in any case.
124 void SetRestrictDispatchChannelGroup(int group);
piman@google.com54af05f2011-04-08 03:38:21125
rockotac64ae92015-08-06 00:32:29126 // Creates a new IPC::SyncMessageFilter and adds it to this SyncChannel.
127 // This should be used instead of directly constructing a new
128 // SyncMessageFilter.
129 scoped_refptr<IPC::SyncMessageFilter> CreateSyncMessageFilter();
130
initial.commit09911bf2008-07-26 23:55:29131 protected:
132 class ReceivedSyncMsgQueue;
133 friend class ReceivedSyncMsgQueue;
134
135 // SyncContext holds the per object data for SyncChannel, so that SyncChannel
136 // can be deleted while it's being used in a different thread. See
137 // ChannelProxy::Context for more information.
teravest@chromium.org329be052013-02-04 18:14:28138 class SyncContext : public Context {
initial.commit09911bf2008-07-26 23:55:29139 public:
dchengfd033702014-08-28 16:59:29140 SyncContext(
141 Listener* listener,
142 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Hajime Hoshiff15e972017-11-09 06:37:09143 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner,
dchengfd033702014-08-28 16:59:29144 base::WaitableEvent* shutdown_event);
initial.commit09911bf2008-07-26 23:55:29145
initial.commit09911bf2008-07-26 23:55:29146 // Adds information about an outgoing sync message to the context so that
jam@chromium.org3cdb7af812008-10-24 19:21:13147 // we know how to deserialize the reply.
rockot3c236292016-07-07 20:26:02148 bool Push(SyncMessage* sync_msg);
initial.commit09911bf2008-07-26 23:55:29149
jam@chromium.org3cdb7af812008-10-24 19:21:13150 // Cleanly remove the top deserializer (and throw it away). Returns the
151 // result of the Send call for that message.
152 bool Pop();
153
rockot3c236292016-07-07 20:26:02154 // Returns a Mojo Event that signals when a sync send is complete or timed
155 // out or the process shut down.
rockotb62e2e32017-03-24 18:36:44156 base::WaitableEvent* GetSendDoneEvent();
initial.commit09911bf2008-07-26 23:55:29157
rockot3c236292016-07-07 20:26:02158 // Returns a Mojo Event that signals when an incoming message that's not the
159 // pending reply needs to get dispatched (by calling DispatchMessages.)
rockotb62e2e32017-03-24 18:36:44160 base::WaitableEvent* GetDispatchEvent();
initial.commit09911bf2008-07-26 23:55:29161
162 void DispatchMessages();
initial.commit09911bf2008-07-26 23:55:29163
164 // Checks if the given message is blocking the listener thread because of a
maruel@google.comd3216442009-03-05 21:07:27165 // synchronous send. If it is, the thread is unblocked and true is
166 // returned. Otherwise the function returns false.
jam@chromium.org3cdb7af812008-10-24 19:21:13167 bool TryToUnblockListener(const Message* msg);
initial.commit09911bf2008-07-26 23:55:29168
agl@chromium.org1c4947f2009-01-15 22:25:11169 base::WaitableEvent* shutdown_event() { return shutdown_event_; }
jcampan@google.comd65cab7a2008-08-12 01:25:41170
ananta@chromium.orgac0efda2009-10-14 16:22:02171 ReceivedSyncMsgQueue* received_sync_msgs() {
rsleevi@chromium.org17571642013-06-01 04:11:27172 return received_sync_msgs_.get();
ananta@chromium.orgac0efda2009-10-14 16:22:02173 }
174
piman@chromium.org298ee7d2012-03-30 21:29:30175 void set_restrict_dispatch_group(int group) {
176 restrict_dispatch_group_ = group;
177 }
178
179 int restrict_dispatch_group() const {
180 return restrict_dispatch_group_;
181 }
piman@google.com54af05f2011-04-08 03:38:21182
rockotb62e2e32017-03-24 18:36:44183 void OnSendDoneEventSignaled(base::RunLoop* nested_loop,
184 base::WaitableEvent* event);
185
initial.commit09911bf2008-07-26 23:55:29186 private:
dchengfe61fca2014-10-22 02:29:52187 ~SyncContext() override;
jabdelmalek@google.com1e9499c2010-04-06 20:33:36188 // ChannelProxy methods that we override.
jam@chromium.org3cdb7af812008-10-24 19:21:13189
190 // Called on the listener thread.
dchengfe61fca2014-10-22 02:29:52191 void Clear() override;
jam@chromium.org3cdb7af812008-10-24 19:21:13192
193 // Called on the IPC thread.
dchengfe61fca2014-10-22 02:29:52194 bool OnMessageReceived(const Message& msg) override;
195 void OnChannelError() override;
rockot10188752016-09-08 18:24:56196 void OnChannelOpened() override;
dchengfe61fca2014-10-22 02:29:52197 void OnChannelClosed() override;
jam@chromium.org3cdb7af812008-10-24 19:21:13198
199 // Cancels all pending Send calls.
200 void CancelPendingSends();
201
rockot3c236292016-07-07 20:26:02202 void OnShutdownEventSignaled(base::WaitableEvent* event);
initial.commit09911bf2008-07-26 23:55:29203
Brett Wilson55ff1472017-09-26 00:28:48204 using PendingSyncMessageQueue = base::circular_deque<PendingSyncMsg>;
initial.commit09911bf2008-07-26 23:55:29205 PendingSyncMessageQueue deserializers_;
rockot3c236292016-07-07 20:26:02206 bool reject_new_deserializers_ = false;
brettw@chromium.org20305ec2011-01-21 04:55:52207 base::Lock deserializers_lock_;
initial.commit09911bf2008-07-26 23:55:29208
jam@chromium.org3cdb7af812008-10-24 19:21:13209 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_;
initial.commit09911bf2008-07-26 23:55:29210
agl@chromium.org1c4947f2009-01-15 22:25:11211 base::WaitableEvent* shutdown_event_;
212 base::WaitableEventWatcher shutdown_watcher_;
teravest@chromium.org329be052013-02-04 18:14:28213 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_;
piman@chromium.org298ee7d2012-03-30 21:29:30214 int restrict_dispatch_group_;
initial.commit09911bf2008-07-26 23:55:29215 };
216
217 private:
dchengfd033702014-08-28 16:59:29218 SyncChannel(
219 Listener* listener,
220 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
Hajime Hoshiff15e972017-11-09 06:37:09221 const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner,
dchengfd033702014-08-28 16:59:29222 base::WaitableEvent* shutdown_event);
morrita@chromium.orgfca876a12014-06-05 16:15:38223
rockotb62e2e32017-03-24 18:36:44224 void OnDispatchEventSignaled(base::WaitableEvent* event);
jam@chromium.org3cdb7af812008-10-24 19:21:13225
maruel@google.comd3216442009-03-05 21:07:27226 SyncContext* sync_context() {
227 return reinterpret_cast<SyncContext*>(context());
228 }
initial.commit09911bf2008-07-26 23:55:29229
Julie Jeongeun Kim84f3cfa12021-02-17 10:00:51230 // Waits for a reply, timeout or process shutdown.
rockot3c236292016-07-07 20:26:02231 static void WaitForReply(mojo::SyncHandleRegistry* registry,
Julie Jeongeun Kim84f3cfa12021-02-17 10:00:51232 SyncContext* context);
initial.commit09911bf2008-07-26 23:55:29233
kkania@chromium.org952394af2011-11-16 01:06:46234 // Starts the dispatch watcher.
235 void StartWatching();
236
rockot29ade1b2015-08-07 06:23:59237 // ChannelProxy overrides:
238 void OnChannelInit() override;
239
rockot3c236292016-07-07 20:26:02240 scoped_refptr<mojo::SyncHandleRegistry> sync_handle_registry_;
241
jam@chromium.org3cdb7af812008-10-24 19:21:13242 // Used to signal events between the IPC and listener threads.
rockotb62e2e32017-03-24 18:36:44243 base::WaitableEventWatcher dispatch_watcher_;
244 base::WaitableEventWatcher::EventCallback dispatch_watcher_callback_;
jam@chromium.org3cdb7af812008-10-24 19:21:13245
rockot29ade1b2015-08-07 06:23:59246 // Tracks SyncMessageFilters created before complete channel initialization.
247 std::vector<scoped_refptr<SyncMessageFilter>> pre_init_sync_message_filters_;
248
tfarina@chromium.org73a797fb2010-06-07 02:10:18249 DISALLOW_COPY_AND_ASSIGN(SyncChannel);
initial.commit09911bf2008-07-26 23:55:29250};
251
252} // namespace IPC
253
thestig@chromium.org320eff42011-11-15 00:29:48254#endif // IPC_IPC_SYNC_CHANNEL_H_