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