[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
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 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 8 | #include <deque> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 9 | #include <memory> |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 12 | |
tfarina | 4da8275d | 2015-09-16 09:56:21 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 16 | #include "base/synchronization/waitable_event_watcher.h" |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 17 | #include "ipc/ipc_channel_handle.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 18 | #include "ipc/ipc_channel_proxy.h" |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 19 | #include "ipc/ipc_sync_message.h" |
rockot | ac64ae92f | 2015-08-06 00:32:29 | [diff] [blame] | 20 | #include "ipc/ipc_sync_message_filter.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | |
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 22 | namespace base { |
| 23 | class WaitableEvent; |
| 24 | }; |
| 25 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | namespace IPC { |
| 27 | |
rockot | 64e65de | 2016-06-21 20:17:49 | [diff] [blame] | 28 | class SyncMessage; |
rockot | ee0123e2 | 2016-06-24 17:27:33 | [diff] [blame^] | 29 | class ChannelFactory; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 31 | // This is similar to ChannelProxy, with the added feature of supporting sending |
| 32 | // synchronous messages. |
[email protected] | 4a180a5 | 2011-04-15 19:07:43 | [diff] [blame] | 33 | // |
| 34 | // Overview of how the sync channel works |
| 35 | // -------------------------------------- |
| 36 | // When the sending thread sends a synchronous message, we create a bunch |
[email protected] | 5855f1d | 2014-04-16 16:50:43 | [diff] [blame] | 37 | // of tracking info (created in Send, stored in the PendingSyncMsg |
[email protected] | 4a180a5 | 2011-04-15 19:07:43 | [diff] [blame] | 38 | // structure) associated with the message that we identify by the unique |
| 39 | // "MessageId" on the SyncMessage. Among the things we save is the |
| 40 | // "Deserializer" which is provided by the sync message. This object is in |
| 41 | // charge of reading the parameters from the reply message and putting them in |
| 42 | // the output variables provided by its caller. |
| 43 | // |
| 44 | // The info gets stashed in a queue since we could have a nested stack of sync |
| 45 | // messages (each side could send sync messages in response to sync messages, |
| 46 | // so it works like calling a function). The message is sent to the I/O thread |
| 47 | // for dispatch and the original thread blocks waiting for the reply. |
| 48 | // |
| 49 | // SyncContext maintains the queue in a threadsafe way and listens for replies |
| 50 | // on the I/O thread. When a reply comes in that matches one of the messages |
| 51 | // it's looking for (using the unique message ID), it will execute the |
| 52 | // deserializer stashed from before, and unblock the original thread. |
| 53 | // |
| 54 | // |
| 55 | // Significant complexity results from the fact that messages are still coming |
| 56 | // in while the original thread is blocked. Normal async messages are queued |
| 57 | // and dispatched after the blocking call is complete. Sync messages must |
| 58 | // be dispatched in a reentrant manner to avoid deadlock. |
| 59 | // |
| 60 | // |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | // Note that care must be taken that the lifetime of the ipc_thread argument |
| 62 | // is more than this object. If the message loop goes away while this object |
| 63 | // is running and it's used to send a message, then it will use the invalid |
| 64 | // message loop pointer to proxy it to the ipc thread. |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 65 | class IPC_EXPORT SyncChannel : public ChannelProxy { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | public: |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 67 | enum RestrictDispatchGroup { |
| 68 | kRestrictDispatchGroup_None = 0, |
| 69 | }; |
| 70 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 71 | // Creates and initializes a sync channel. If create_pipe_now is specified, |
| 72 | // the channel will be initialized synchronously. |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 73 | // The naming pattern follows IPC::Channel. |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 74 | static std::unique_ptr<SyncChannel> Create( |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 75 | const IPC::ChannelHandle& channel_handle, |
| 76 | IPC::Channel::Mode mode, |
| 77 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 78 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 79 | bool create_pipe_now, |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 80 | base::WaitableEvent* shutdown_event); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 81 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 82 | static std::unique_ptr<SyncChannel> Create( |
| 83 | std::unique_ptr<ChannelFactory> factory, |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 84 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 85 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 86 | bool create_pipe_now, |
| 87 | base::WaitableEvent* shutdown_event); |
| 88 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 89 | // Creates an uninitialized sync channel. Call ChannelProxy::Init to |
| 90 | // initialize the channel. This two-step setup allows message filters to be |
| 91 | // added before any messages are sent or received. |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 92 | static std::unique_ptr<SyncChannel> Create( |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 93 | Listener* listener, |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 94 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 95 | base::WaitableEvent* shutdown_event); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 96 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 97 | ~SyncChannel() override; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 99 | bool Send(Message* message) override; |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 100 | |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 101 | // Sets the dispatch group for this channel, to only allow re-entrant dispatch |
| 102 | // of messages to other channels in the same group. |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 103 | // |
| 104 | // Normally, any unblocking message coming from any channel can be dispatched |
| 105 | // when any (possibly other) channel is blocked on sending a message. This is |
| 106 | // needed in some cases to unblock certain loops (e.g. necessary when some |
| 107 | // processes share a window hierarchy), but may cause re-entrancy issues in |
| 108 | // some cases where such loops are not possible. This flags allows the tagging |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 109 | // of some particular channels to only re-enter in known correct cases. |
| 110 | // |
| 111 | // Incoming messages on channels belonging to a group that is not |
| 112 | // kRestrictDispatchGroup_None will only be dispatched while a sync message is |
| 113 | // being sent on a channel of the *same* group. |
| 114 | // Incoming messages belonging to the kRestrictDispatchGroup_None group (the |
| 115 | // default) will be dispatched in any case. |
| 116 | void SetRestrictDispatchChannelGroup(int group); |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 117 | |
rockot | ac64ae92f | 2015-08-06 00:32:29 | [diff] [blame] | 118 | // Creates a new IPC::SyncMessageFilter and adds it to this SyncChannel. |
| 119 | // This should be used instead of directly constructing a new |
| 120 | // SyncMessageFilter. |
| 121 | scoped_refptr<IPC::SyncMessageFilter> CreateSyncMessageFilter(); |
| 122 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | protected: |
| 124 | class ReceivedSyncMsgQueue; |
| 125 | friend class ReceivedSyncMsgQueue; |
| 126 | |
| 127 | // SyncContext holds the per object data for SyncChannel, so that SyncChannel |
| 128 | // can be deleted while it's being used in a different thread. See |
| 129 | // ChannelProxy::Context for more information. |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 130 | class SyncContext : public Context { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | public: |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 132 | SyncContext( |
| 133 | Listener* listener, |
| 134 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 135 | base::WaitableEvent* shutdown_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | // Adds information about an outgoing sync message to the context so that |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 138 | // we know how to deserialize the reply. |
rockot | ee0123e2 | 2016-06-24 17:27:33 | [diff] [blame^] | 139 | void Push(SyncMessage* sync_msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 141 | // Cleanly remove the top deserializer (and throw it away). Returns the |
| 142 | // result of the Send call for that message. |
| 143 | bool Pop(); |
| 144 | |
rockot | ee0123e2 | 2016-06-24 17:27:33 | [diff] [blame^] | 145 | // Returns an event that's set when the send is complete, timed out or the |
| 146 | // process shut down. |
| 147 | base::WaitableEvent* GetSendDoneEvent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | |
rockot | ee0123e2 | 2016-06-24 17:27:33 | [diff] [blame^] | 149 | // Returns an event that's set when an incoming message that's not the reply |
| 150 | // needs to get dispatched (by calling SyncContext::DispatchMessages). |
| 151 | base::WaitableEvent* GetDispatchEvent(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | |
| 153 | void DispatchMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | |
| 155 | // Checks if the given message is blocking the listener thread because of a |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 156 | // synchronous send. If it is, the thread is unblocked and true is |
| 157 | // returned. Otherwise the function returns false. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 158 | bool TryToUnblockListener(const Message* msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 160 | base::WaitableEvent* shutdown_event() { return shutdown_event_; } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 161 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 162 | ReceivedSyncMsgQueue* received_sync_msgs() { |
[email protected] | 1757164 | 2013-06-01 04:11:27 | [diff] [blame] | 163 | return received_sync_msgs_.get(); |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 166 | void set_restrict_dispatch_group(int group) { |
| 167 | restrict_dispatch_group_ = group; |
| 168 | } |
| 169 | |
| 170 | int restrict_dispatch_group() const { |
| 171 | return restrict_dispatch_group_; |
| 172 | } |
[email protected] | 54af05f | 2011-04-08 03:38:21 | [diff] [blame] | 173 | |
rockot | ee0123e2 | 2016-06-24 17:27:33 | [diff] [blame^] | 174 | base::WaitableEventWatcher::EventCallback MakeWaitableEventCallback(); |
| 175 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | private: |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 177 | ~SyncContext() override; |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 178 | // ChannelProxy methods that we override. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 179 | |
| 180 | // Called on the listener thread. |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 181 | void Clear() override; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 182 | |
| 183 | // Called on the IPC thread. |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 184 | bool OnMessageReceived(const Message& msg) override; |
| 185 | void OnChannelError() override; |
| 186 | void OnChannelOpened() override; |
| 187 | void OnChannelClosed() override; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 188 | |
| 189 | // Cancels all pending Send calls. |
| 190 | void CancelPendingSends(); |
| 191 | |
rockot | ee0123e2 | 2016-06-24 17:27:33 | [diff] [blame^] | 192 | void OnWaitableEventSignaled(base::WaitableEvent* event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 194 | typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | PendingSyncMessageQueue deserializers_; |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 196 | base::Lock deserializers_lock_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 197 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 198 | scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 200 | base::WaitableEvent* shutdown_event_; |
| 201 | base::WaitableEventWatcher shutdown_watcher_; |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 202 | base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_; |
[email protected] | 298ee7d | 2012-03-30 21:29:30 | [diff] [blame] | 203 | int restrict_dispatch_group_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | private: |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 207 | SyncChannel( |
| 208 | Listener* listener, |
| 209 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 210 | base::WaitableEvent* shutdown_event); |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 211 | |
rockot | ee0123e2 | 2016-06-24 17:27:33 | [diff] [blame^] | 212 | void OnWaitableEventSignaled(base::WaitableEvent* arg); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 213 | |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 214 | SyncContext* sync_context() { |
| 215 | return reinterpret_cast<SyncContext*>(context()); |
| 216 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 218 | // Both these functions wait for a reply, timeout or process shutdown. The |
| 219 | // latter one also runs a nested message loop in the meantime. |
rockot | ee0123e2 | 2016-06-24 17:27:33 | [diff] [blame^] | 220 | static void WaitForReply( |
| 221 | SyncContext* context, base::WaitableEvent* pump_messages_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 223 | // Runs a nested message loop until a reply arrives, times out, or the process |
| 224 | // shuts down. |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 225 | static void WaitForReplyWithNestedMessageLoop(SyncContext* context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 226 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 227 | // Starts the dispatch watcher. |
| 228 | void StartWatching(); |
| 229 | |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 230 | // ChannelProxy overrides: |
| 231 | void OnChannelInit() override; |
| 232 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 233 | // Used to signal events between the IPC and listener threads. |
rockot | ee0123e2 | 2016-06-24 17:27:33 | [diff] [blame^] | 234 | base::WaitableEventWatcher dispatch_watcher_; |
| 235 | base::WaitableEventWatcher::EventCallback dispatch_watcher_callback_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 236 | |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 237 | // Tracks SyncMessageFilters created before complete channel initialization. |
| 238 | std::vector<scoped_refptr<SyncMessageFilter>> pre_init_sync_message_filters_; |
| 239 | |
[email protected] | 73a797fb | 2010-06-07 02:10:18 | [diff] [blame] | 240 | DISALLOW_COPY_AND_ASSIGN(SyncChannel); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | } // namespace IPC |
| 244 | |
[email protected] | 320eff4 | 2011-11-15 00:29:48 | [diff] [blame] | 245 | #endif // IPC_IPC_SYNC_CHANNEL_H_ |