| 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" | 24 #include "mojo/public/cpp/bindings/associated_group.h" |
| 25 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 25 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 26 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
| 26 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 27 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
| 28 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" |
| 27 | 29 |
| 28 namespace base { | 30 namespace base { |
| 29 class SingleThreadTaskRunner; | 31 class SingleThreadTaskRunner; |
| 30 } | 32 } |
| 31 | 33 |
| 32 namespace IPC { | 34 namespace IPC { |
| 33 | 35 |
| 34 class ChannelFactory; | 36 class ChannelFactory; |
| 35 class MessageFilter; | 37 class MessageFilter; |
| 36 class MessageFilterRouter; | 38 class MessageFilterRouter; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 mojo::MakeRequest(proxy, GetAssociatedGroup()); | 204 mojo::MakeRequest(proxy, GetAssociatedGroup()); |
| 203 GetGenericRemoteAssociatedInterface(Interface::Name_, request.PassHandle()); | 205 GetGenericRemoteAssociatedInterface(Interface::Name_, request.PassHandle()); |
| 204 } | 206 } |
| 205 | 207 |
| 206 #if defined(ENABLE_IPC_FUZZER) | 208 #if defined(ENABLE_IPC_FUZZER) |
| 207 void set_outgoing_message_filter(OutgoingMessageFilter* filter) { | 209 void set_outgoing_message_filter(OutgoingMessageFilter* filter) { |
| 208 outgoing_message_filter_ = filter; | 210 outgoing_message_filter_ = filter; |
| 209 } | 211 } |
| 210 #endif | 212 #endif |
| 211 | 213 |
| 214 // Creates a ThreadSafeAssociatedInterfacePtr for |Interface|. This object |
| 215 // 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 |
| 217 // same thread over other ThreadSafeAssociatedInterfacePtrs associated with |
| 218 // the same Channel. |
| 212 template <typename Interface> | 219 template <typename Interface> |
| 213 using AssociatedInterfaceRetrievedCallback = | 220 void GetThreadSafeRemoteAssociatedInterface( |
| 214 base::Callback<void(mojo::AssociatedInterfacePtr<Interface>)>; | 221 scoped_refptr<mojo::ThreadSafeAssociatedInterfacePtr<Interface>>* |
| 215 // Creates an AssociatedInterfacePtr to |Interface| on the IO thread and | 222 out_ptr) { |
| 216 // passes it to |callback|, also invoked on the IO thread. | 223 mojo::AssociatedInterfacePtrInfo<Interface> ptr_info; |
| 217 template <typename Interface> | 224 mojo::AssociatedInterfaceRequest<Interface> request; |
| 218 void RetrieveAssociatedInterfaceOnIOThread( | 225 GetAssociatedGroup()->CreateAssociatedInterface( |
| 219 const AssociatedInterfaceRetrievedCallback<Interface>& callback) { | 226 mojo::AssociatedGroup::WILL_PASS_REQUEST, &ptr_info, &request); |
| 220 context_->ipc_task_runner()->PostTask( | 227 GetGenericRemoteAssociatedInterface(Interface::Name_, request.PassHandle()); |
| 221 FROM_HERE, base::Bind(&Context::RetrieveAssociatedInterface<Interface>, | 228 *out_ptr = mojo::ThreadSafeAssociatedInterfacePtr<Interface>::Create( |
| 222 context_, callback)); | 229 std::move(ptr_info), ipc_task_runner()); |
| 223 } | 230 } |
| 224 | 231 |
| 225 base::SingleThreadTaskRunner* ipc_task_runner() const { | 232 base::SingleThreadTaskRunner* ipc_task_runner() const { |
| 226 return context_->ipc_task_runner(); | 233 return context_->ipc_task_runner(); |
| 227 } | 234 } |
| 228 | 235 |
| 229 // Called to clear the pointer to the IPC task runner when it's going away. | 236 // Called to clear the pointer to the IPC task runner when it's going away. |
| 230 void ClearIPCTaskRunner(); | 237 void ClearIPCTaskRunner(); |
| 231 | 238 |
| 232 protected: | 239 protected: |
| (...skipping 11 matching lines...) Expand all Loading... |
| 244 void ClearIPCTaskRunner(); | 251 void ClearIPCTaskRunner(); |
| 245 base::SingleThreadTaskRunner* ipc_task_runner() const { | 252 base::SingleThreadTaskRunner* ipc_task_runner() const { |
| 246 return ipc_task_runner_.get(); | 253 return ipc_task_runner_.get(); |
| 247 } | 254 } |
| 248 // Dispatches a message on the listener thread. | 255 // Dispatches a message on the listener thread. |
| 249 void OnDispatchMessage(const Message& message); | 256 void OnDispatchMessage(const Message& message); |
| 250 | 257 |
| 251 // Sends |message| from appropriate thread. | 258 // Sends |message| from appropriate thread. |
| 252 void Send(Message* message); | 259 void Send(Message* message); |
| 253 | 260 |
| 254 // Requests a remote associated interface on the IPC thread. | |
| 255 void GetRemoteAssociatedInterface( | |
| 256 const std::string& name, | |
| 257 mojo::ScopedInterfaceEndpointHandle handle); | |
| 258 | |
| 259 protected: | 261 protected: |
| 260 friend class base::RefCountedThreadSafe<Context>; | 262 friend class base::RefCountedThreadSafe<Context>; |
| 261 ~Context() override; | 263 ~Context() override; |
| 262 | 264 |
| 263 // IPC::Listener methods: | 265 // IPC::Listener methods: |
| 264 bool OnMessageReceived(const Message& message) override; | 266 bool OnMessageReceived(const Message& message) override; |
| 265 void OnChannelConnected(int32_t peer_pid) override; | 267 void OnChannelConnected(int32_t peer_pid) override; |
| 266 void OnChannelError() override; | 268 void OnChannelError() override; |
| 267 void OnAssociatedInterfaceRequest( | 269 void OnAssociatedInterfaceRequest( |
| 268 const std::string& interface_name, | 270 const std::string& interface_name, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 292 friend class ChannelProxy; | 294 friend class ChannelProxy; |
| 293 friend class IpcSecurityTestUtil; | 295 friend class IpcSecurityTestUtil; |
| 294 | 296 |
| 295 // Create the Channel | 297 // Create the Channel |
| 296 void CreateChannel(std::unique_ptr<ChannelFactory> factory); | 298 void CreateChannel(std::unique_ptr<ChannelFactory> factory); |
| 297 | 299 |
| 298 // Methods called on the IO thread. | 300 // Methods called on the IO thread. |
| 299 void OnSendMessage(std::unique_ptr<Message> message_ptr); | 301 void OnSendMessage(std::unique_ptr<Message> message_ptr); |
| 300 void OnAddFilter(); | 302 void OnAddFilter(); |
| 301 void OnRemoveFilter(MessageFilter* filter); | 303 void OnRemoveFilter(MessageFilter* filter); |
| 302 template <typename Interface> | |
| 303 void RetrieveAssociatedInterface( | |
| 304 const AssociatedInterfaceRetrievedCallback<Interface>& callback) { | |
| 305 mojo::AssociatedInterfacePtr<Interface> interface_ptr; | |
| 306 channel_->GetAssociatedInterfaceSupport()->GetRemoteAssociatedInterface( | |
| 307 &interface_ptr); | |
| 308 callback.Run(std::move(interface_ptr)); | |
| 309 } | |
| 310 | 304 |
| 311 // Methods called on the listener thread. | 305 // Methods called on the listener thread. |
| 312 void AddFilter(MessageFilter* filter); | 306 void AddFilter(MessageFilter* filter); |
| 313 void OnDispatchConnected(); | 307 void OnDispatchConnected(); |
| 314 void OnDispatchError(); | 308 void OnDispatchError(); |
| 315 void OnDispatchBadMessage(const Message& message); | 309 void OnDispatchBadMessage(const Message& message); |
| 316 void OnDispatchAssociatedInterfaceRequest( | 310 void OnDispatchAssociatedInterfaceRequest( |
| 317 const std::string& interface_name, | 311 const std::string& interface_name, |
| 318 mojo::ScopedInterfaceEndpointHandle handle); | 312 mojo::ScopedInterfaceEndpointHandle handle); |
| 319 | 313 |
| 320 void ClearChannel(); | 314 void ClearChannel(); |
| 321 | 315 |
| 322 mojo::AssociatedGroup* associated_group() { return &associated_group_; } | 316 mojo::AssociatedGroup* associated_group() { return &associated_group_; } |
| 317 mojom::Channel& thread_safe_channel() { |
| 318 return thread_safe_channel_->proxy(); |
| 319 } |
| 323 | 320 |
| 324 void AddGenericAssociatedInterfaceForIOThread( | 321 void AddGenericAssociatedInterfaceForIOThread( |
| 325 const std::string& name, | 322 const std::string& name, |
| 326 const GenericAssociatedInterfaceFactory& factory); | 323 const GenericAssociatedInterfaceFactory& factory); |
| 327 | 324 |
| 328 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; | 325 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; |
| 329 Listener* listener_; | 326 Listener* listener_; |
| 330 | 327 |
| 331 // List of filters. This is only accessed on the IPC thread. | 328 // List of filters. This is only accessed on the IPC thread. |
| 332 std::vector<scoped_refptr<MessageFilter> > filters_; | 329 std::vector<scoped_refptr<MessageFilter> > filters_; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 353 // Lock for pending_filters_. | 350 // Lock for pending_filters_. |
| 354 base::Lock pending_filters_lock_; | 351 base::Lock pending_filters_lock_; |
| 355 | 352 |
| 356 // Cached copy of the peer process ID. Set on IPC but read on both IPC and | 353 // Cached copy of the peer process ID. Set on IPC but read on both IPC and |
| 357 // listener threads. | 354 // listener threads. |
| 358 base::ProcessId peer_pid_; | 355 base::ProcessId peer_pid_; |
| 359 base::Lock peer_pid_lock_; | 356 base::Lock peer_pid_lock_; |
| 360 | 357 |
| 361 mojo::AssociatedGroup associated_group_; | 358 mojo::AssociatedGroup associated_group_; |
| 362 | 359 |
| 360 // A thread-safe mojom::Channel interface we use to make remote interface |
| 361 // requests from the proxy thread. |
| 362 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>> |
| 363 thread_safe_channel_; |
| 364 |
| 363 // Holds associated interface binders added by | 365 // Holds associated interface binders added by |
| 364 // AddGenericAssociatedInterfaceForIOThread until the underlying channel has | 366 // AddGenericAssociatedInterfaceForIOThread until the underlying channel has |
| 365 // been initialized. | 367 // been initialized. |
| 366 base::Lock pending_io_thread_interfaces_lock_; | 368 base::Lock pending_io_thread_interfaces_lock_; |
| 367 std::vector<std::pair<std::string, GenericAssociatedInterfaceFactory>> | 369 std::vector<std::pair<std::string, GenericAssociatedInterfaceFactory>> |
| 368 pending_io_thread_interfaces_; | 370 pending_io_thread_interfaces_; |
| 369 }; | 371 }; |
| 370 | 372 |
| 371 Context* context() { return context_.get(); } | 373 Context* context() { return context_.get(); } |
| 372 | 374 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 403 bool did_init_; | 405 bool did_init_; |
| 404 | 406 |
| 405 #if defined(ENABLE_IPC_FUZZER) | 407 #if defined(ENABLE_IPC_FUZZER) |
| 406 OutgoingMessageFilter* outgoing_message_filter_; | 408 OutgoingMessageFilter* outgoing_message_filter_; |
| 407 #endif | 409 #endif |
| 408 }; | 410 }; |
| 409 | 411 |
| 410 } // namespace IPC | 412 } // namespace IPC |
| 411 | 413 |
| 412 #endif // IPC_IPC_CHANNEL_PROXY_H_ | 414 #endif // IPC_IPC_CHANNEL_PROXY_H_ |
| OLD | NEW |