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