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