| 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" | 21 #include "mojo/public/c/system/types.h" |
| 22 #include "mojo/public/cpp/system/simple_watcher.h" | 22 #include "mojo/public/cpp/system/simple_watcher.h" |
| 23 | 23 |
| 24 namespace base { | 24 namespace base { |
| 25 class RunLoop; |
| 25 class WaitableEvent; | 26 class WaitableEvent; |
| 26 }; | 27 }; |
| 27 | 28 |
| 28 namespace mojo { | 29 namespace mojo { |
| 29 class SyncHandleRegistry; | 30 class SyncHandleRegistry; |
| 30 } | 31 } |
| 31 | 32 |
| 32 namespace IPC { | 33 namespace IPC { |
| 33 | 34 |
| 34 class ChannelFactory; | 35 class ChannelFactory; |
| 35 class MojoEvent; | |
| 36 class SyncMessage; | 36 class SyncMessage; |
| 37 | 37 |
| 38 // 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 |
| 39 // synchronous messages. | 39 // synchronous messages. |
| 40 // | 40 // |
| 41 // Overview of how the sync channel works | 41 // Overview of how the sync channel works |
| 42 // -------------------------------------- | 42 // -------------------------------------- |
| 43 // 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 |
| 44 // of tracking info (created in Send, stored in the PendingSyncMsg | 44 // of tracking info (created in Send, stored in the PendingSyncMsg |
| 45 // structure) associated with the message that we identify by the unique | 45 // structure) associated with the message that we identify by the unique |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 // 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 |
| 145 // we know how to deserialize the reply. | 145 // we know how to deserialize the reply. |
| 146 bool Push(SyncMessage* sync_msg); | 146 bool Push(SyncMessage* sync_msg); |
| 147 | 147 |
| 148 // Cleanly remove the top deserializer (and throw it away). Returns the | 148 // Cleanly remove the top deserializer (and throw it away). Returns the |
| 149 // result of the Send call for that message. | 149 // result of the Send call for that message. |
| 150 bool Pop(); | 150 bool Pop(); |
| 151 | 151 |
| 152 // Returns a Mojo Event that signals when a sync send is complete or timed | 152 // Returns a Mojo Event that signals when a sync send is complete or timed |
| 153 // out or the process shut down. | 153 // out or the process shut down. |
| 154 MojoEvent* GetSendDoneEvent(); | 154 base::WaitableEvent* GetSendDoneEvent(); |
| 155 | 155 |
| 156 // Returns a Mojo Event that signals when an incoming message that's not the | 156 // Returns a Mojo Event that signals when an incoming message that's not the |
| 157 // pending reply needs to get dispatched (by calling DispatchMessages.) | 157 // pending reply needs to get dispatched (by calling DispatchMessages.) |
| 158 MojoEvent* GetDispatchEvent(); | 158 base::WaitableEvent* GetDispatchEvent(); |
| 159 | 159 |
| 160 void DispatchMessages(); | 160 void DispatchMessages(); |
| 161 | 161 |
| 162 // 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 |
| 163 // 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 |
| 164 // returned. Otherwise the function returns false. | 164 // returned. Otherwise the function returns false. |
| 165 bool TryToUnblockListener(const Message* msg); | 165 bool TryToUnblockListener(const Message* msg); |
| 166 | 166 |
| 167 base::WaitableEvent* shutdown_event() { return shutdown_event_; } | 167 base::WaitableEvent* shutdown_event() { return shutdown_event_; } |
| 168 | 168 |
| 169 ReceivedSyncMsgQueue* received_sync_msgs() { | 169 ReceivedSyncMsgQueue* received_sync_msgs() { |
| 170 return received_sync_msgs_.get(); | 170 return received_sync_msgs_.get(); |
| 171 } | 171 } |
| 172 | 172 |
| 173 void set_restrict_dispatch_group(int group) { | 173 void set_restrict_dispatch_group(int group) { |
| 174 restrict_dispatch_group_ = group; | 174 restrict_dispatch_group_ = group; |
| 175 } | 175 } |
| 176 | 176 |
| 177 int restrict_dispatch_group() const { | 177 int restrict_dispatch_group() const { |
| 178 return restrict_dispatch_group_; | 178 return restrict_dispatch_group_; |
| 179 } | 179 } |
| 180 | 180 |
| 181 void OnSendDoneEventSignaled(base::RunLoop* nested_loop, |
| 182 base::WaitableEvent* event); |
| 183 |
| 181 private: | 184 private: |
| 182 ~SyncContext() override; | 185 ~SyncContext() override; |
| 183 // ChannelProxy methods that we override. | 186 // ChannelProxy methods that we override. |
| 184 | 187 |
| 185 // Called on the listener thread. | 188 // Called on the listener thread. |
| 186 void Clear() override; | 189 void Clear() override; |
| 187 | 190 |
| 188 // Called on the IPC thread. | 191 // Called on the IPC thread. |
| 189 bool OnMessageReceived(const Message& msg) override; | 192 bool OnMessageReceived(const Message& msg) override; |
| 190 void OnChannelError() override; | 193 void OnChannelError() override; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 208 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_; | 211 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_; |
| 209 int restrict_dispatch_group_; | 212 int restrict_dispatch_group_; |
| 210 }; | 213 }; |
| 211 | 214 |
| 212 private: | 215 private: |
| 213 SyncChannel( | 216 SyncChannel( |
| 214 Listener* listener, | 217 Listener* listener, |
| 215 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, | 218 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 216 base::WaitableEvent* shutdown_event); | 219 base::WaitableEvent* shutdown_event); |
| 217 | 220 |
| 218 void OnDispatchHandleReady(MojoResult result); | 221 void OnDispatchEventSignaled(base::WaitableEvent* event); |
| 219 | 222 |
| 220 SyncContext* sync_context() { | 223 SyncContext* sync_context() { |
| 221 return reinterpret_cast<SyncContext*>(context()); | 224 return reinterpret_cast<SyncContext*>(context()); |
| 222 } | 225 } |
| 223 | 226 |
| 224 // Both these functions wait for a reply, timeout or process shutdown. The | 227 // Both these functions wait for a reply, timeout or process shutdown. The |
| 225 // latter one also runs a nested message loop in the meantime. | 228 // latter one also runs a nested message loop in the meantime. |
| 226 static void WaitForReply(mojo::SyncHandleRegistry* registry, | 229 static void WaitForReply(mojo::SyncHandleRegistry* registry, |
| 227 SyncContext* context, | 230 SyncContext* context, |
| 228 bool pump_messages); | 231 bool pump_messages); |
| 229 | 232 |
| 230 // Runs a nested message loop until a reply arrives, times out, or the process | 233 // Runs a nested message loop until a reply arrives, times out, or the process |
| 231 // shuts down. | 234 // shuts down. |
| 232 static void WaitForReplyWithNestedMessageLoop(SyncContext* context); | 235 static void WaitForReplyWithNestedMessageLoop(SyncContext* context); |
| 233 | 236 |
| 234 // Starts the dispatch watcher. | 237 // Starts the dispatch watcher. |
| 235 void StartWatching(); | 238 void StartWatching(); |
| 236 | 239 |
| 237 // ChannelProxy overrides: | 240 // ChannelProxy overrides: |
| 238 void OnChannelInit() override; | 241 void OnChannelInit() override; |
| 239 | 242 |
| 240 scoped_refptr<mojo::SyncHandleRegistry> sync_handle_registry_; | 243 scoped_refptr<mojo::SyncHandleRegistry> sync_handle_registry_; |
| 241 | 244 |
| 242 // Used to signal events between the IPC and listener threads. | 245 // Used to signal events between the IPC and listener threads. |
| 243 mojo::SimpleWatcher dispatch_watcher_; | 246 base::WaitableEventWatcher dispatch_watcher_; |
| 247 base::WaitableEventWatcher::EventCallback dispatch_watcher_callback_; |
| 244 | 248 |
| 245 // Tracks SyncMessageFilters created before complete channel initialization. | 249 // Tracks SyncMessageFilters created before complete channel initialization. |
| 246 std::vector<scoped_refptr<SyncMessageFilter>> pre_init_sync_message_filters_; | 250 std::vector<scoped_refptr<SyncMessageFilter>> pre_init_sync_message_filters_; |
| 247 | 251 |
| 248 DISALLOW_COPY_AND_ASSIGN(SyncChannel); | 252 DISALLOW_COPY_AND_ASSIGN(SyncChannel); |
| 249 }; | 253 }; |
| 250 | 254 |
| 251 } // namespace IPC | 255 } // namespace IPC |
| 252 | 256 |
| 253 #endif // IPC_IPC_SYNC_CHANNEL_H_ | 257 #endif // IPC_IPC_SYNC_CHANNEL_H_ |
| OLD | NEW |