Avi Drissman | ea1be23 | 2022-09-14 23:29:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 320eff4 | 2011-11-15 00:29:48 | [diff] [blame] | 5 | #ifndef IPC_IPC_SYNC_CHANNEL_H_ |
| 6 | #define IPC_IPC_SYNC_CHANNEL_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 8 | #include <memory> |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 11 | |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 12 | #include "base/component_export.h" |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 13 | #include "base/containers/circular_deque.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 17 | #include "base/synchronization/waitable_event_watcher.h" |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 18 | #include "base/task/single_thread_task_runner.h" |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 19 | #include "ipc/ipc_channel_handle.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 20 | #include "ipc/ipc_channel_proxy.h" |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 21 | #include "ipc/ipc_sync_message.h" |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 22 | #include "mojo/public/c/system/types.h" |
rockot | 9eadaba | 2017-03-15 23:57:47 | [diff] [blame] | 23 | #include "mojo/public/cpp/system/simple_watcher.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | |
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 25 | namespace base { |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 26 | class RunLoop; |
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 27 | class WaitableEvent; |
Nico Weber | 40cd26fd | 2019-02-09 17:37:02 | [diff] [blame] | 28 | } // namespace base |
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 29 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 30 | namespace mojo { |
| 31 | class SyncHandleRegistry; |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 32 | } |
| 33 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | namespace IPC { |
| 35 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 36 | class SyncMessage; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 38 | // This is similar to ChannelProxy, with the added feature of supporting sending |
| 39 | // synchronous messages. |
[email protected] | 4a180a5 | 2011-04-15 19:07:43 | [diff] [blame] | 40 | // |
| 41 | // Overview of how the sync channel works |
| 42 | // -------------------------------------- |
| 43 | // When the sending thread sends a synchronous message, we create a bunch |
[email protected] | 5855f1d | 2014-04-16 16:50:43 | [diff] [blame] | 44 | // of tracking info (created in Send, stored in the PendingSyncMsg |
[email protected] | 4a180a5 | 2011-04-15 19:07:43 | [diff] [blame] | 45 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | // 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 Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 72 | class COMPONENT_EXPORT(IPC) SyncChannel : public ChannelProxy { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | public: |
Peter Kasting | 525a014 | 2023-03-08 21:56:27 | [diff] [blame] | 74 | class ReceivedSyncMsgQueue; |
| 75 | |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 76 | enum RestrictDispatchGroup { |
| 77 | kRestrictDispatchGroup_None = 0, |
| 78 | }; |
| 79 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 80 | // Creates and initializes a sync channel. If create_pipe_now is specified, |
| 81 | // the channel will be initialized synchronously. |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 82 | // The naming pattern follows IPC::Channel. |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 83 | static std::unique_ptr<SyncChannel> Create( |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 84 | const IPC::ChannelHandle& channel_handle, |
| 85 | IPC::Channel::Mode mode, |
| 86 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 87 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 88 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner, |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 89 | bool create_pipe_now, |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 90 | base::WaitableEvent* shutdown_event); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 91 | |
| 92 | // Creates an uninitialized sync channel. Call ChannelProxy::Init to |
| 93 | // initialize the channel. This two-step setup allows message filters to be |
| 94 | // added before any messages are sent or received. |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 95 | static std::unique_ptr<SyncChannel> Create( |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 96 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 97 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 98 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner, |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 99 | base::WaitableEvent* shutdown_event); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 100 | |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 101 | void AddListenerTaskRunner( |
| 102 | int32_t routing_id, |
| 103 | scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 104 | |
| 105 | void RemoveListenerTaskRunner(int32_t routing_id); |
| 106 | |
Peter Boström | c68c5aa | 2021-09-28 00:28:00 | [diff] [blame] | 107 | SyncChannel(const SyncChannel&) = delete; |
| 108 | SyncChannel& operator=(const SyncChannel&) = delete; |
| 109 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 110 | ~SyncChannel() override; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 112 | bool Send(Message* message) override; |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 113 | |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 114 | // Sets the dispatch group for this channel, to only allow re-entrant dispatch |
| 115 | // of messages to other channels in the same group. |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 116 | // |
| 117 | // Normally, any unblocking message coming from any channel can be dispatched |
| 118 | // when any (possibly other) channel is blocked on sending a message. This is |
| 119 | // needed in some cases to unblock certain loops (e.g. necessary when some |
| 120 | // processes share a window hierarchy), but may cause re-entrancy issues in |
| 121 | // some cases where such loops are not possible. This flags allows the tagging |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 122 | // of some particular channels to only re-enter in known correct cases. |
| 123 | // |
| 124 | // Incoming messages on channels belonging to a group that is not |
| 125 | // kRestrictDispatchGroup_None will only be dispatched while a sync message is |
| 126 | // being sent on a channel of the *same* group. |
| 127 | // Incoming messages belonging to the kRestrictDispatchGroup_None group (the |
| 128 | // default) will be dispatched in any case. |
| 129 | void SetRestrictDispatchChannelGroup(int group); |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 130 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | protected: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | friend class ReceivedSyncMsgQueue; |
| 133 | |
| 134 | // SyncContext holds the per object data for SyncChannel, so that SyncChannel |
| 135 | // can be deleted while it's being used in a different thread. See |
| 136 | // ChannelProxy::Context for more information. |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 137 | class SyncContext : public Context { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | public: |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 139 | SyncContext( |
| 140 | Listener* listener, |
| 141 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 142 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 143 | base::WaitableEvent* shutdown_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | // Adds information about an outgoing sync message to the context so that |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 146 | // we know how to deserialize the reply. |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 147 | bool Push(SyncMessage* sync_msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 149 | // Cleanly remove the top deserializer (and throw it away). Returns the |
| 150 | // result of the Send call for that message. |
| 151 | bool Pop(); |
| 152 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 153 | // Returns a Mojo Event that signals when a sync send is complete or timed |
| 154 | // out or the process shut down. |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 155 | base::WaitableEvent* GetSendDoneEvent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 157 | // Returns a Mojo Event that signals when an incoming message that's not the |
| 158 | // pending reply needs to get dispatched (by calling DispatchMessages.) |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 159 | base::WaitableEvent* GetDispatchEvent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 160 | |
| 161 | void DispatchMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | |
| 163 | // Checks if the given message is blocking the listener thread because of a |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 164 | // synchronous send. If it is, the thread is unblocked and true is |
| 165 | // returned. Otherwise the function returns false. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 166 | bool TryToUnblockListener(const Message* msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 168 | base::WaitableEvent* shutdown_event() { return shutdown_event_; } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 169 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 170 | ReceivedSyncMsgQueue* received_sync_msgs() { |
[email protected] | 1757164 | 2013-06-01 04:11:27 | [diff] [blame] | 171 | return received_sync_msgs_.get(); |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 174 | void set_restrict_dispatch_group(int group) { |
| 175 | restrict_dispatch_group_ = group; |
| 176 | } |
| 177 | |
| 178 | int restrict_dispatch_group() const { |
| 179 | return restrict_dispatch_group_; |
| 180 | } |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 181 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 182 | void OnSendDoneEventSignaled(base::RunLoop* nested_loop, |
| 183 | base::WaitableEvent* event); |
| 184 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | private: |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 186 | ~SyncContext() override; |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 187 | // ChannelProxy methods that we override. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 188 | |
| 189 | // Called on the listener thread. |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 190 | void Clear() override; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 191 | |
| 192 | // Called on the IPC thread. |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 193 | bool OnMessageReceived(const Message& msg) override; |
| 194 | void OnChannelError() override; |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 195 | void OnChannelOpened() override; |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 196 | void OnChannelClosed() override; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 197 | |
| 198 | // Cancels all pending Send calls. |
| 199 | void CancelPendingSends(); |
| 200 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 201 | void OnShutdownEventSignaled(base::WaitableEvent* event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 203 | using PendingSyncMessageQueue = base::circular_deque<PendingSyncMsg>; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | PendingSyncMessageQueue deserializers_; |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 205 | bool reject_new_deserializers_ = false; |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 206 | base::Lock deserializers_lock_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 208 | scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | |
Pâris | e6361d0 | 2023-07-19 09:00:43 | [diff] [blame] | 210 | raw_ptr<base::WaitableEvent, AcrossTasksDanglingUntriaged> shutdown_event_; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 211 | base::WaitableEventWatcher shutdown_watcher_; |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 212 | base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_; |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 213 | int restrict_dispatch_group_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | private: |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 217 | SyncChannel( |
| 218 | Listener* listener, |
| 219 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 220 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 221 | base::WaitableEvent* shutdown_event); |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 222 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 223 | void OnDispatchEventSignaled(base::WaitableEvent* event); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 224 | |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 225 | SyncContext* sync_context() { |
| 226 | return reinterpret_cast<SyncContext*>(context()); |
| 227 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 228 | |
Julie Jeongeun Kim | 84f3cfa1 | 2021-02-17 10:00:51 | [diff] [blame] | 229 | // Waits for a reply, timeout or process shutdown. |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 230 | static void WaitForReply(mojo::SyncHandleRegistry* registry, |
Julie Jeongeun Kim | 84f3cfa1 | 2021-02-17 10:00:51 | [diff] [blame] | 231 | SyncContext* context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 233 | // Starts the dispatch watcher. |
| 234 | void StartWatching(); |
| 235 | |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 236 | // ChannelProxy overrides: |
| 237 | void OnChannelInit() override; |
| 238 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 239 | scoped_refptr<mojo::SyncHandleRegistry> sync_handle_registry_; |
| 240 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 241 | // Used to signal events between the IPC and listener threads. |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 242 | base::WaitableEventWatcher dispatch_watcher_; |
| 243 | base::WaitableEventWatcher::EventCallback dispatch_watcher_callback_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | } // namespace IPC |
| 247 | |
[email protected] | 320eff4 | 2011-11-15 00:29:48 | [diff] [blame] | 248 | #endif // IPC_IPC_SYNC_CHANNEL_H_ |