| 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_CHANNEL_PROXY_H_ | 5 #ifndef IPC_IPC_CHANNEL_PROXY_H_ |
| 6 #define IPC_IPC_CHANNEL_PROXY_H_ | 6 #define IPC_IPC_CHANNEL_PROXY_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/threading/non_thread_safe.h" | 18 #include "base/threading/non_thread_safe.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "ipc/ipc_channel.h" | 20 #include "ipc/ipc_channel.h" |
| 21 #include "ipc/ipc_channel_handle.h" | 21 #include "ipc/ipc_channel_handle.h" |
| 22 #include "ipc/ipc_listener.h" | 22 #include "ipc/ipc_listener.h" |
| 23 #include "ipc/ipc_sender.h" | 23 #include "ipc/ipc_sender.h" |
| 24 #include "mojo/public/cpp/bindings/associated_group.h" | |
| 25 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" | 24 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 26 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 25 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
| 27 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 26 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
| 28 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" | 27 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" |
| 29 | 28 |
| 30 namespace base { | 29 namespace base { |
| 31 class SingleThreadTaskRunner; | 30 class SingleThreadTaskRunner; |
| 32 } | 31 } |
| 33 | 32 |
| 34 namespace IPC { | 33 namespace IPC { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 // Init(). | 179 // Init(). |
| 181 template <typename Interface> | 180 template <typename Interface> |
| 182 void AddAssociatedInterfaceForIOThread( | 181 void AddAssociatedInterfaceForIOThread( |
| 183 const AssociatedInterfaceFactory<Interface>& factory) { | 182 const AssociatedInterfaceFactory<Interface>& factory) { |
| 184 AddGenericAssociatedInterfaceForIOThread( | 183 AddGenericAssociatedInterfaceForIOThread( |
| 185 Interface::Name_, | 184 Interface::Name_, |
| 186 base::Bind(&ChannelProxy::BindAssociatedInterfaceRequest<Interface>, | 185 base::Bind(&ChannelProxy::BindAssociatedInterfaceRequest<Interface>, |
| 187 factory)); | 186 factory)); |
| 188 } | 187 } |
| 189 | 188 |
| 190 // Gets the AssociatedGroup used to create new associated endpoints on this | |
| 191 // ChannelProxy. | |
| 192 mojo::AssociatedGroup* GetAssociatedGroup(); | |
| 193 | |
| 194 // Requests an associated interface from the remote endpoint. | 189 // Requests an associated interface from the remote endpoint. |
| 195 void GetGenericRemoteAssociatedInterface( | 190 void GetGenericRemoteAssociatedInterface( |
| 196 const std::string& name, | 191 const std::string& name, |
| 197 mojo::ScopedInterfaceEndpointHandle handle); | 192 mojo::ScopedInterfaceEndpointHandle handle); |
| 198 | 193 |
| 199 // Template helper to request associated interfaces from the remote endpoint. | 194 // Template helper to request associated interfaces from the remote endpoint. |
| 200 template <typename Interface> | 195 template <typename Interface> |
| 201 void GetRemoteAssociatedInterface( | 196 void GetRemoteAssociatedInterface( |
| 202 mojo::AssociatedInterfacePtr<Interface>* proxy) { | 197 mojo::AssociatedInterfacePtr<Interface>* proxy) { |
| 203 mojo::AssociatedInterfaceRequest<Interface> request = | 198 auto request = mojo::MakeRequest(proxy); |
| 204 mojo::MakeRequest(proxy, GetAssociatedGroup()); | |
| 205 GetGenericRemoteAssociatedInterface(Interface::Name_, request.PassHandle()); | 199 GetGenericRemoteAssociatedInterface(Interface::Name_, request.PassHandle()); |
| 206 } | 200 } |
| 207 | 201 |
| 208 #if defined(ENABLE_IPC_FUZZER) | 202 #if defined(ENABLE_IPC_FUZZER) |
| 209 void set_outgoing_message_filter(OutgoingMessageFilter* filter) { | 203 void set_outgoing_message_filter(OutgoingMessageFilter* filter) { |
| 210 outgoing_message_filter_ = filter; | 204 outgoing_message_filter_ = filter; |
| 211 } | 205 } |
| 212 #endif | 206 #endif |
| 213 | 207 |
| 214 // Creates a ThreadSafeAssociatedInterfacePtr for |Interface|. This object | 208 // Creates a ThreadSafeAssociatedInterfacePtr for |Interface|. This object |
| 215 // may be used to send messages on the interface from any thread and those | 209 // may be used to send messages on the interface from any thread and those |
| 216 // messages will remain ordered with respect to other messages sent on the | 210 // messages will remain ordered with respect to other messages sent on the |
| 217 // same thread over other ThreadSafeAssociatedInterfacePtrs associated with | 211 // same thread over other ThreadSafeAssociatedInterfacePtrs associated with |
| 218 // the same Channel. | 212 // the same Channel. |
| 219 template <typename Interface> | 213 template <typename Interface> |
| 220 void GetThreadSafeRemoteAssociatedInterface( | 214 void GetThreadSafeRemoteAssociatedInterface( |
| 221 scoped_refptr<mojo::ThreadSafeAssociatedInterfacePtr<Interface>>* | 215 scoped_refptr<mojo::ThreadSafeAssociatedInterfacePtr<Interface>>* |
| 222 out_ptr) { | 216 out_ptr) { |
| 223 mojo::AssociatedInterfacePtrInfo<Interface> ptr_info; | 217 mojo::AssociatedInterfacePtrInfo<Interface> ptr_info; |
| 224 mojo::AssociatedInterfaceRequest<Interface> request; | 218 auto request = mojo::MakeRequest(&ptr_info); |
| 225 GetAssociatedGroup()->CreateAssociatedInterface( | |
| 226 mojo::AssociatedGroup::WILL_PASS_REQUEST, &ptr_info, &request); | |
| 227 GetGenericRemoteAssociatedInterface(Interface::Name_, request.PassHandle()); | 219 GetGenericRemoteAssociatedInterface(Interface::Name_, request.PassHandle()); |
| 228 *out_ptr = mojo::ThreadSafeAssociatedInterfacePtr<Interface>::Create( | 220 *out_ptr = mojo::ThreadSafeAssociatedInterfacePtr<Interface>::Create( |
| 229 std::move(ptr_info), ipc_task_runner()); | 221 std::move(ptr_info), ipc_task_runner()); |
| 230 } | 222 } |
| 231 | 223 |
| 232 base::SingleThreadTaskRunner* ipc_task_runner() const { | 224 base::SingleThreadTaskRunner* ipc_task_runner() const { |
| 233 return context_->ipc_task_runner(); | 225 return context_->ipc_task_runner(); |
| 234 } | 226 } |
| 235 | 227 |
| 236 // Called to clear the pointer to the IPC task runner when it's going away. | 228 // Called to clear the pointer to the IPC task runner when it's going away. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 void AddFilter(MessageFilter* filter); | 298 void AddFilter(MessageFilter* filter); |
| 307 void OnDispatchConnected(); | 299 void OnDispatchConnected(); |
| 308 void OnDispatchError(); | 300 void OnDispatchError(); |
| 309 void OnDispatchBadMessage(const Message& message); | 301 void OnDispatchBadMessage(const Message& message); |
| 310 void OnDispatchAssociatedInterfaceRequest( | 302 void OnDispatchAssociatedInterfaceRequest( |
| 311 const std::string& interface_name, | 303 const std::string& interface_name, |
| 312 mojo::ScopedInterfaceEndpointHandle handle); | 304 mojo::ScopedInterfaceEndpointHandle handle); |
| 313 | 305 |
| 314 void ClearChannel(); | 306 void ClearChannel(); |
| 315 | 307 |
| 316 mojo::AssociatedGroup* associated_group() { return &associated_group_; } | |
| 317 mojom::Channel& thread_safe_channel() { | 308 mojom::Channel& thread_safe_channel() { |
| 318 return thread_safe_channel_->proxy(); | 309 return thread_safe_channel_->proxy(); |
| 319 } | 310 } |
| 320 | 311 |
| 321 void AddGenericAssociatedInterfaceForIOThread( | 312 void AddGenericAssociatedInterfaceForIOThread( |
| 322 const std::string& name, | 313 const std::string& name, |
| 323 const GenericAssociatedInterfaceFactory& factory); | 314 const GenericAssociatedInterfaceFactory& factory); |
| 324 | 315 |
| 325 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; | 316 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; |
| 326 Listener* listener_; | 317 Listener* listener_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 348 // IPC thread when they're added to filters_. | 339 // IPC thread when they're added to filters_. |
| 349 std::vector<scoped_refptr<MessageFilter> > pending_filters_; | 340 std::vector<scoped_refptr<MessageFilter> > pending_filters_; |
| 350 // Lock for pending_filters_. | 341 // Lock for pending_filters_. |
| 351 base::Lock pending_filters_lock_; | 342 base::Lock pending_filters_lock_; |
| 352 | 343 |
| 353 // Cached copy of the peer process ID. Set on IPC but read on both IPC and | 344 // Cached copy of the peer process ID. Set on IPC but read on both IPC and |
| 354 // listener threads. | 345 // listener threads. |
| 355 base::ProcessId peer_pid_; | 346 base::ProcessId peer_pid_; |
| 356 base::Lock peer_pid_lock_; | 347 base::Lock peer_pid_lock_; |
| 357 | 348 |
| 358 mojo::AssociatedGroup associated_group_; | |
| 359 | |
| 360 // A thread-safe mojom::Channel interface we use to make remote interface | 349 // A thread-safe mojom::Channel interface we use to make remote interface |
| 361 // requests from the proxy thread. | 350 // requests from the proxy thread. |
| 362 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>> | 351 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>> |
| 363 thread_safe_channel_; | 352 thread_safe_channel_; |
| 364 | 353 |
| 365 // Holds associated interface binders added by | 354 // Holds associated interface binders added by |
| 366 // AddGenericAssociatedInterfaceForIOThread until the underlying channel has | 355 // AddGenericAssociatedInterfaceForIOThread until the underlying channel has |
| 367 // been initialized. | 356 // been initialized. |
| 368 base::Lock pending_io_thread_interfaces_lock_; | 357 base::Lock pending_io_thread_interfaces_lock_; |
| 369 std::vector<std::pair<std::string, GenericAssociatedInterfaceFactory>> | 358 std::vector<std::pair<std::string, GenericAssociatedInterfaceFactory>> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 bool did_init_; | 394 bool did_init_; |
| 406 | 395 |
| 407 #if defined(ENABLE_IPC_FUZZER) | 396 #if defined(ENABLE_IPC_FUZZER) |
| 408 OutgoingMessageFilter* outgoing_message_filter_; | 397 OutgoingMessageFilter* outgoing_message_filter_; |
| 409 #endif | 398 #endif |
| 410 }; | 399 }; |
| 411 | 400 |
| 412 } // namespace IPC | 401 } // namespace IPC |
| 413 | 402 |
| 414 #endif // IPC_IPC_CHANNEL_PROXY_H_ | 403 #endif // IPC_IPC_CHANNEL_PROXY_H_ |
| OLD | NEW |