| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef IPC_IPC_SYNC_CHANNEL_H_ | 5 #ifndef IPC_IPC_SYNC_CHANNEL_H_ |
| 6 #define IPC_IPC_SYNC_CHANNEL_H_ | 6 #define IPC_IPC_SYNC_CHANNEL_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/synchronization/waitable_event_watcher.h" | 16 #include "base/synchronization/waitable_event_watcher.h" |
| 17 #include "ipc/ipc_channel_handle.h" | 17 #include "ipc/ipc_channel_handle.h" |
| 18 #include "ipc/ipc_channel_proxy.h" | 18 #include "ipc/ipc_channel_proxy.h" |
| 19 #include "ipc/ipc_sync_message.h" | 19 #include "ipc/ipc_sync_message.h" |
| 20 #include "ipc/ipc_sync_message_filter.h" | 20 #include "ipc/ipc_sync_message_filter.h" |
| 21 #include "mojo/public/c/system/types.h" |
| 22 #include "mojo/public/cpp/system/watcher.h" |
| 21 | 23 |
| 22 namespace base { | 24 namespace base { |
| 23 class WaitableEvent; | 25 class WaitableEvent; |
| 24 }; | 26 }; |
| 25 | 27 |
| 28 namespace mojo { |
| 29 class SyncHandleRegistry; |
| 30 class Watcher; |
| 31 } |
| 32 |
| 26 namespace IPC { | 33 namespace IPC { |
| 27 | 34 |
| 35 class ChannelFactory; |
| 36 class MojoEvent; |
| 28 class SyncMessage; | 37 class SyncMessage; |
| 29 class ChannelFactory; | |
| 30 | 38 |
| 31 // This is similar to ChannelProxy, with the added feature of supporting sending | 39 // This is similar to ChannelProxy, with the added feature of supporting sending |
| 32 // synchronous messages. | 40 // synchronous messages. |
| 33 // | 41 // |
| 34 // Overview of how the sync channel works | 42 // Overview of how the sync channel works |
| 35 // -------------------------------------- | 43 // -------------------------------------- |
| 36 // When the sending thread sends a synchronous message, we create a bunch | 44 // When the sending thread sends a synchronous message, we create a bunch |
| 37 // of tracking info (created in Send, stored in the PendingSyncMsg | 45 // of tracking info (created in Send, stored in the PendingSyncMsg |
| 38 // structure) associated with the message that we identify by the unique | 46 // structure) associated with the message that we identify by the unique |
| 39 // "MessageId" on the SyncMessage. Among the things we save is the | 47 // "MessageId" on the SyncMessage. Among the things we save is the |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 // ChannelProxy::Context for more information. | 137 // ChannelProxy::Context for more information. |
| 130 class SyncContext : public Context { | 138 class SyncContext : public Context { |
| 131 public: | 139 public: |
| 132 SyncContext( | 140 SyncContext( |
| 133 Listener* listener, | 141 Listener* listener, |
| 134 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, | 142 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 135 base::WaitableEvent* shutdown_event); | 143 base::WaitableEvent* shutdown_event); |
| 136 | 144 |
| 137 // Adds information about an outgoing sync message to the context so that | 145 // Adds information about an outgoing sync message to the context so that |
| 138 // we know how to deserialize the reply. | 146 // we know how to deserialize the reply. |
| 139 void Push(SyncMessage* sync_msg); | 147 bool Push(SyncMessage* sync_msg); |
| 140 | 148 |
| 141 // Cleanly remove the top deserializer (and throw it away). Returns the | 149 // Cleanly remove the top deserializer (and throw it away). Returns the |
| 142 // result of the Send call for that message. | 150 // result of the Send call for that message. |
| 143 bool Pop(); | 151 bool Pop(); |
| 144 | 152 |
| 145 // Returns an event that's set when the send is complete, timed out or the | 153 // Returns a Mojo Event that signals when a sync send is complete or timed |
| 146 // process shut down. | 154 // out or the process shut down. |
| 147 base::WaitableEvent* GetSendDoneEvent(); | 155 MojoEvent* GetSendDoneEvent(); |
| 148 | 156 |
| 149 // Returns an event that's set when an incoming message that's not the reply | 157 // Returns a Mojo Event that signals when an incoming message that's not the |
| 150 // needs to get dispatched (by calling SyncContext::DispatchMessages). | 158 // pending reply needs to get dispatched (by calling DispatchMessages.) |
| 151 base::WaitableEvent* GetDispatchEvent(); | 159 MojoEvent* GetDispatchEvent(); |
| 152 | 160 |
| 153 void DispatchMessages(); | 161 void DispatchMessages(); |
| 154 | 162 |
| 155 // Checks if the given message is blocking the listener thread because of a | 163 // Checks if the given message is blocking the listener thread because of a |
| 156 // synchronous send. If it is, the thread is unblocked and true is | 164 // synchronous send. If it is, the thread is unblocked and true is |
| 157 // returned. Otherwise the function returns false. | 165 // returned. Otherwise the function returns false. |
| 158 bool TryToUnblockListener(const Message* msg); | 166 bool TryToUnblockListener(const Message* msg); |
| 159 | 167 |
| 160 base::WaitableEvent* shutdown_event() { return shutdown_event_; } | 168 base::WaitableEvent* shutdown_event() { return shutdown_event_; } |
| 161 | 169 |
| 162 ReceivedSyncMsgQueue* received_sync_msgs() { | 170 ReceivedSyncMsgQueue* received_sync_msgs() { |
| 163 return received_sync_msgs_.get(); | 171 return received_sync_msgs_.get(); |
| 164 } | 172 } |
| 165 | 173 |
| 166 void set_restrict_dispatch_group(int group) { | 174 void set_restrict_dispatch_group(int group) { |
| 167 restrict_dispatch_group_ = group; | 175 restrict_dispatch_group_ = group; |
| 168 } | 176 } |
| 169 | 177 |
| 170 int restrict_dispatch_group() const { | 178 int restrict_dispatch_group() const { |
| 171 return restrict_dispatch_group_; | 179 return restrict_dispatch_group_; |
| 172 } | 180 } |
| 173 | 181 |
| 174 base::WaitableEventWatcher::EventCallback MakeWaitableEventCallback(); | |
| 175 | |
| 176 private: | 182 private: |
| 177 ~SyncContext() override; | 183 ~SyncContext() override; |
| 178 // ChannelProxy methods that we override. | 184 // ChannelProxy methods that we override. |
| 179 | 185 |
| 180 // Called on the listener thread. | 186 // Called on the listener thread. |
| 181 void Clear() override; | 187 void Clear() override; |
| 182 | 188 |
| 183 // Called on the IPC thread. | 189 // Called on the IPC thread. |
| 184 bool OnMessageReceived(const Message& msg) override; | 190 bool OnMessageReceived(const Message& msg) override; |
| 185 void OnChannelError() override; | 191 void OnChannelError() override; |
| 186 void OnChannelOpened() override; | 192 void OnChannelOpened() override; |
| 187 void OnChannelClosed() override; | 193 void OnChannelClosed() override; |
| 188 | 194 |
| 189 // Cancels all pending Send calls. | 195 // Cancels all pending Send calls. |
| 190 void CancelPendingSends(); | 196 void CancelPendingSends(); |
| 191 | 197 |
| 192 void OnWaitableEventSignaled(base::WaitableEvent* event); | 198 void OnShutdownEventSignaled(base::WaitableEvent* event); |
| 193 | 199 |
| 194 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; | 200 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; |
| 195 PendingSyncMessageQueue deserializers_; | 201 PendingSyncMessageQueue deserializers_; |
| 202 bool reject_new_deserializers_ = false; |
| 196 base::Lock deserializers_lock_; | 203 base::Lock deserializers_lock_; |
| 197 | 204 |
| 198 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; | 205 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; |
| 199 | 206 |
| 200 base::WaitableEvent* shutdown_event_; | 207 base::WaitableEvent* shutdown_event_; |
| 201 base::WaitableEventWatcher shutdown_watcher_; | 208 base::WaitableEventWatcher shutdown_watcher_; |
| 202 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_; | 209 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_; |
| 203 int restrict_dispatch_group_; | 210 int restrict_dispatch_group_; |
| 204 }; | 211 }; |
| 205 | 212 |
| 206 private: | 213 private: |
| 207 SyncChannel( | 214 SyncChannel( |
| 208 Listener* listener, | 215 Listener* listener, |
| 209 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, | 216 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 210 base::WaitableEvent* shutdown_event); | 217 base::WaitableEvent* shutdown_event); |
| 211 | 218 |
| 212 void OnWaitableEventSignaled(base::WaitableEvent* arg); | 219 void OnDispatchHandleReady(MojoResult result); |
| 213 | 220 |
| 214 SyncContext* sync_context() { | 221 SyncContext* sync_context() { |
| 215 return reinterpret_cast<SyncContext*>(context()); | 222 return reinterpret_cast<SyncContext*>(context()); |
| 216 } | 223 } |
| 217 | 224 |
| 218 // Both these functions wait for a reply, timeout or process shutdown. The | 225 // Both these functions wait for a reply, timeout or process shutdown. The |
| 219 // latter one also runs a nested message loop in the meantime. | 226 // latter one also runs a nested message loop in the meantime. |
| 220 static void WaitForReply( | 227 static void WaitForReply(mojo::SyncHandleRegistry* registry, |
| 221 SyncContext* context, base::WaitableEvent* pump_messages_event); | 228 SyncContext* context, |
| 229 bool pump_messages); |
| 222 | 230 |
| 223 // Runs a nested message loop until a reply arrives, times out, or the process | 231 // Runs a nested message loop until a reply arrives, times out, or the process |
| 224 // shuts down. | 232 // shuts down. |
| 225 static void WaitForReplyWithNestedMessageLoop(SyncContext* context); | 233 static void WaitForReplyWithNestedMessageLoop(SyncContext* context); |
| 226 | 234 |
| 227 // Starts the dispatch watcher. | 235 // Starts the dispatch watcher. |
| 228 void StartWatching(); | 236 void StartWatching(); |
| 229 | 237 |
| 230 // ChannelProxy overrides: | 238 // ChannelProxy overrides: |
| 231 void OnChannelInit() override; | 239 void OnChannelInit() override; |
| 232 | 240 |
| 241 scoped_refptr<mojo::SyncHandleRegistry> sync_handle_registry_; |
| 242 |
| 233 // Used to signal events between the IPC and listener threads. | 243 // Used to signal events between the IPC and listener threads. |
| 234 base::WaitableEventWatcher dispatch_watcher_; | 244 mojo::Watcher dispatch_watcher_; |
| 235 base::WaitableEventWatcher::EventCallback dispatch_watcher_callback_; | |
| 236 | 245 |
| 237 // Tracks SyncMessageFilters created before complete channel initialization. | 246 // Tracks SyncMessageFilters created before complete channel initialization. |
| 238 std::vector<scoped_refptr<SyncMessageFilter>> pre_init_sync_message_filters_; | 247 std::vector<scoped_refptr<SyncMessageFilter>> pre_init_sync_message_filters_; |
| 239 | 248 |
| 240 DISALLOW_COPY_AND_ASSIGN(SyncChannel); | 249 DISALLOW_COPY_AND_ASSIGN(SyncChannel); |
| 241 }; | 250 }; |
| 242 | 251 |
| 243 } // namespace IPC | 252 } // namespace IPC |
| 244 | 253 |
| 245 #endif // IPC_IPC_SYNC_CHANNEL_H_ | 254 #endif // IPC_IPC_SYNC_CHANNEL_H_ |
| OLD | NEW |