Chromium Code Reviews| 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 DCHECK(GetAssociatedGroup()); |
| 217 template <typename Interface> | 224 mojo::AssociatedInterfacePtr<Interface> ptr; |
| 218 void RetrieveAssociatedInterfaceOnIOThread( | 225 mojom::GenericInterfaceAssociatedRequest request; |
| 219 const AssociatedInterfaceRetrievedCallback<Interface>& callback) { | 226 request.Bind(mojo::MakeRequest(&ptr, GetAssociatedGroup()).PassHandle()); |
| 220 context_->ipc_task_runner()->PostTask( | 227 context()->thread_safe_channel().GetAssociatedInterface( |
|
yzshen1
2017/02/07 00:06:26
nit: you could call GetGenericRemoteAssociatedInte
Ken Rockot(use gerrit already)
2017/02/07 01:25:32
done (called GetRemoteAssociatedInterface instead)
yzshen1
2017/02/07 06:05:52
Using GetRemoteAssociatedInterface() will setup an
Ken Rockot(use gerrit already)
2017/02/08 00:37:11
Ah good point. Changed to use GetGenericRemoteAsso
| |
| 221 FROM_HERE, base::Bind(&Context::RetrieveAssociatedInterface<Interface>, | 228 Interface::Name_, std::move(request)); |
| 222 context_, callback)); | 229 *out_ptr = mojo::ThreadSafeAssociatedInterfacePtr<Interface>::Create( |
| 230 ptr.PassInterface(), ipc_task_runner()); | |
| 223 } | 231 } |
| 224 | 232 |
| 225 base::SingleThreadTaskRunner* ipc_task_runner() const { | 233 base::SingleThreadTaskRunner* ipc_task_runner() const { |
| 226 return context_->ipc_task_runner(); | 234 return context_->ipc_task_runner(); |
| 227 } | 235 } |
| 228 | 236 |
| 229 // Called to clear the pointer to the IPC task runner when it's going away. | 237 // Called to clear the pointer to the IPC task runner when it's going away. |
| 230 void ClearIPCTaskRunner(); | 238 void ClearIPCTaskRunner(); |
| 231 | 239 |
| 232 protected: | 240 protected: |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 244 void ClearIPCTaskRunner(); | 252 void ClearIPCTaskRunner(); |
| 245 base::SingleThreadTaskRunner* ipc_task_runner() const { | 253 base::SingleThreadTaskRunner* ipc_task_runner() const { |
| 246 return ipc_task_runner_.get(); | 254 return ipc_task_runner_.get(); |
| 247 } | 255 } |
| 248 // Dispatches a message on the listener thread. | 256 // Dispatches a message on the listener thread. |
| 249 void OnDispatchMessage(const Message& message); | 257 void OnDispatchMessage(const Message& message); |
| 250 | 258 |
| 251 // Sends |message| from appropriate thread. | 259 // Sends |message| from appropriate thread. |
| 252 void Send(Message* message); | 260 void Send(Message* message); |
| 253 | 261 |
| 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: | 262 protected: |
| 260 friend class base::RefCountedThreadSafe<Context>; | 263 friend class base::RefCountedThreadSafe<Context>; |
| 261 ~Context() override; | 264 ~Context() override; |
| 262 | 265 |
| 263 // IPC::Listener methods: | 266 // IPC::Listener methods: |
| 264 bool OnMessageReceived(const Message& message) override; | 267 bool OnMessageReceived(const Message& message) override; |
| 265 void OnChannelConnected(int32_t peer_pid) override; | 268 void OnChannelConnected(int32_t peer_pid) override; |
| 266 void OnChannelError() override; | 269 void OnChannelError() override; |
| 267 void OnAssociatedInterfaceRequest( | 270 void OnAssociatedInterfaceRequest( |
| 268 const std::string& interface_name, | 271 const std::string& interface_name, |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 292 friend class ChannelProxy; | 295 friend class ChannelProxy; |
| 293 friend class IpcSecurityTestUtil; | 296 friend class IpcSecurityTestUtil; |
| 294 | 297 |
| 295 // Create the Channel | 298 // Create the Channel |
| 296 void CreateChannel(std::unique_ptr<ChannelFactory> factory); | 299 void CreateChannel(std::unique_ptr<ChannelFactory> factory); |
| 297 | 300 |
| 298 // Methods called on the IO thread. | 301 // Methods called on the IO thread. |
| 299 void OnSendMessage(std::unique_ptr<Message> message_ptr); | 302 void OnSendMessage(std::unique_ptr<Message> message_ptr); |
| 300 void OnAddFilter(); | 303 void OnAddFilter(); |
| 301 void OnRemoveFilter(MessageFilter* filter); | 304 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 | 305 |
| 311 // Methods called on the listener thread. | 306 // Methods called on the listener thread. |
| 312 void AddFilter(MessageFilter* filter); | 307 void AddFilter(MessageFilter* filter); |
| 313 void OnDispatchConnected(); | 308 void OnDispatchConnected(); |
| 314 void OnDispatchError(); | 309 void OnDispatchError(); |
| 315 void OnDispatchBadMessage(const Message& message); | 310 void OnDispatchBadMessage(const Message& message); |
| 316 void OnDispatchAssociatedInterfaceRequest( | 311 void OnDispatchAssociatedInterfaceRequest( |
| 317 const std::string& interface_name, | 312 const std::string& interface_name, |
| 318 mojo::ScopedInterfaceEndpointHandle handle); | 313 mojo::ScopedInterfaceEndpointHandle handle); |
| 319 | 314 |
| 320 void ClearChannel(); | 315 void ClearChannel(); |
| 321 | 316 |
| 322 mojo::AssociatedGroup* associated_group() { return &associated_group_; } | 317 mojo::AssociatedGroup* associated_group() { return &associated_group_; } |
| 318 mojom::Channel& thread_safe_channel() { | |
| 319 return thread_safe_channel_->proxy(); | |
| 320 } | |
| 323 | 321 |
| 324 void AddGenericAssociatedInterfaceForIOThread( | 322 void AddGenericAssociatedInterfaceForIOThread( |
| 325 const std::string& name, | 323 const std::string& name, |
| 326 const GenericAssociatedInterfaceFactory& factory); | 324 const GenericAssociatedInterfaceFactory& factory); |
| 327 | 325 |
| 328 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; | 326 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_; |
| 329 Listener* listener_; | 327 Listener* listener_; |
| 330 | 328 |
| 331 // List of filters. This is only accessed on the IPC thread. | 329 // List of filters. This is only accessed on the IPC thread. |
| 332 std::vector<scoped_refptr<MessageFilter> > filters_; | 330 std::vector<scoped_refptr<MessageFilter> > filters_; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 353 // Lock for pending_filters_. | 351 // Lock for pending_filters_. |
| 354 base::Lock pending_filters_lock_; | 352 base::Lock pending_filters_lock_; |
| 355 | 353 |
| 356 // Cached copy of the peer process ID. Set on IPC but read on both IPC and | 354 // Cached copy of the peer process ID. Set on IPC but read on both IPC and |
| 357 // listener threads. | 355 // listener threads. |
| 358 base::ProcessId peer_pid_; | 356 base::ProcessId peer_pid_; |
| 359 base::Lock peer_pid_lock_; | 357 base::Lock peer_pid_lock_; |
| 360 | 358 |
| 361 mojo::AssociatedGroup associated_group_; | 359 mojo::AssociatedGroup associated_group_; |
| 362 | 360 |
| 361 // A thread-safe mojom::Channel interface we ues to make remote interface | |
|
yzshen1
2017/02/07 00:06:26
ues -> use please.
Ken Rockot(use gerrit already)
2017/02/07 01:25:32
done
| |
| 362 // requests from the proxy thread. | |
| 363 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>> | |
| 364 thread_safe_channel_; | |
| 365 | |
| 363 // Holds associated interface binders added by | 366 // Holds associated interface binders added by |
| 364 // AddGenericAssociatedInterfaceForIOThread until the underlying channel has | 367 // AddGenericAssociatedInterfaceForIOThread until the underlying channel has |
| 365 // been initialized. | 368 // been initialized. |
| 366 base::Lock pending_io_thread_interfaces_lock_; | 369 base::Lock pending_io_thread_interfaces_lock_; |
| 367 std::vector<std::pair<std::string, GenericAssociatedInterfaceFactory>> | 370 std::vector<std::pair<std::string, GenericAssociatedInterfaceFactory>> |
| 368 pending_io_thread_interfaces_; | 371 pending_io_thread_interfaces_; |
| 369 }; | 372 }; |
| 370 | 373 |
| 371 Context* context() { return context_.get(); } | 374 Context* context() { return context_.get(); } |
| 372 | 375 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 403 bool did_init_; | 406 bool did_init_; |
| 404 | 407 |
| 405 #if defined(ENABLE_IPC_FUZZER) | 408 #if defined(ENABLE_IPC_FUZZER) |
| 406 OutgoingMessageFilter* outgoing_message_filter_; | 409 OutgoingMessageFilter* outgoing_message_filter_; |
| 407 #endif | 410 #endif |
| 408 }; | 411 }; |
| 409 | 412 |
| 410 } // namespace IPC | 413 } // namespace IPC |
| 411 | 414 |
| 412 #endif // IPC_IPC_CHANNEL_PROXY_H_ | 415 #endif // IPC_IPC_CHANNEL_PROXY_H_ |
| OLD | NEW |