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