| 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 <memory> | 11 #include <memory> | 
|  | 12 #include <string> | 
| 11 #include <vector> | 13 #include <vector> | 
| 12 | 14 | 
|  | 15 #include "base/callback.h" | 
| 13 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" | 
| 14 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" | 
| 15 #include "base/threading/non_thread_safe.h" | 18 #include "base/threading/non_thread_safe.h" | 
| 16 #include "build/build_config.h" | 19 #include "build/build_config.h" | 
| 17 #include "ipc/ipc_channel.h" | 20 #include "ipc/ipc_channel.h" | 
| 18 #include "ipc/ipc_channel_handle.h" | 21 #include "ipc/ipc_channel_handle.h" | 
| 19 #include "ipc/ipc_endpoint.h" | 22 #include "ipc/ipc_endpoint.h" | 
| 20 #include "ipc/ipc_listener.h" | 23 #include "ipc/ipc_listener.h" | 
| 21 #include "ipc/ipc_sender.h" | 24 #include "ipc/ipc_sender.h" | 
|  | 25 #include "mojo/public/cpp/bindings/associated_group.h" | 
|  | 26 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 
|  | 27 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 
| 22 | 28 | 
| 23 namespace base { | 29 namespace base { | 
| 24 class SingleThreadTaskRunner; | 30 class SingleThreadTaskRunner; | 
| 25 } | 31 } | 
| 26 | 32 | 
| 27 namespace IPC { | 33 namespace IPC { | 
| 28 | 34 | 
| 29 class ChannelFactory; | 35 class ChannelFactory; | 
| 30 class MessageFilter; | 36 class MessageFilter; | 
| 31 class MessageFilterRouter; | 37 class MessageFilterRouter; | 
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 132   // Ordinarily, messages sent to the ChannelProxy are routed to the matching | 138   // Ordinarily, messages sent to the ChannelProxy are routed to the matching | 
| 133   // listener on the worker thread.  This API allows code to intercept messages | 139   // listener on the worker thread.  This API allows code to intercept messages | 
| 134   // before they are sent to the worker thread. | 140   // before they are sent to the worker thread. | 
| 135   // If you call this before the target process is launched, then you're | 141   // If you call this before the target process is launched, then you're | 
| 136   // guaranteed to not miss any messages.  But if you call this anytime after, | 142   // guaranteed to not miss any messages.  But if you call this anytime after, | 
| 137   // then some messages might be missed since the filter is added internally on | 143   // then some messages might be missed since the filter is added internally on | 
| 138   // the IO thread. | 144   // the IO thread. | 
| 139   void AddFilter(MessageFilter* filter); | 145   void AddFilter(MessageFilter* filter); | 
| 140   void RemoveFilter(MessageFilter* filter); | 146   void RemoveFilter(MessageFilter* filter); | 
| 141 | 147 | 
|  | 148   using GenericAssociatedInterfaceFactory = | 
|  | 149       base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>; | 
|  | 150 | 
|  | 151   // Adds a generic associated interface factory to bind incoming interface | 
|  | 152   // requests directly on the IO thread. MUST be called before Init(). | 
|  | 153   void AddGenericAssociatedInterfaceForIOThread( | 
|  | 154       const std::string& name, | 
|  | 155       const GenericAssociatedInterfaceFactory& factory); | 
|  | 156 | 
|  | 157   // Adds a generic associated interface factory to bind incoming interface | 
|  | 158   // requests on the ChannelProxy's thread. MUST be called before Init(). | 
|  | 159   void AddGenericAssociatedInterface( | 
|  | 160       const std::string& name, | 
|  | 161       const GenericAssociatedInterfaceFactory& factory); | 
|  | 162 | 
|  | 163   template <typename Interface> | 
|  | 164   using AssociatedInterfaceFactory = | 
|  | 165       base::Callback<void(mojo::AssociatedInterfaceRequest<Interface>)>; | 
|  | 166 | 
|  | 167   // Helper to bind an IO-thread associated interface factory, inferring the | 
|  | 168   // interface name from the callback argument's type. MUST be called before | 
|  | 169   // Init(). | 
|  | 170   template <typename Interface> | 
|  | 171   void AddAssociatedInterfaceForIOThread( | 
|  | 172       const AssociatedInterfaceFactory<Interface>& factory) { | 
|  | 173     AddGenericAssociatedInterfaceForIOThread( | 
|  | 174         Interface::Name_, | 
|  | 175         base::Bind(&ChannelProxy::BindAssociatedInterfaceRequest<Interface>, | 
|  | 176                    factory)); | 
|  | 177   } | 
|  | 178 | 
|  | 179   // Helper to bind a ChannelProxy-thread associated interface factory, | 
|  | 180   // inferring the interface name from the callback argument's type. MUST be | 
|  | 181   // called before Init(). | 
|  | 182   template <typename Interface> | 
|  | 183   void AddAssociatedInterface( | 
|  | 184       const AssociatedInterfaceFactory<Interface>& factory) { | 
|  | 185     AddGenericAssociatedInterface( | 
|  | 186         Interface::Name_, | 
|  | 187         base::Bind(&ChannelProxy::BindAssociatedInterfaceRequest<Interface>, | 
|  | 188                    factory)); | 
|  | 189   } | 
|  | 190 | 
|  | 191   // Gets the AssociatedGroup used to create new associated endpoints on this | 
|  | 192   // ChannelProxy. This must only be called after the listener's | 
|  | 193   // OnChannelConnected is called. | 
|  | 194   mojo::AssociatedGroup* GetAssociatedGroup(); | 
|  | 195 | 
|  | 196   // Requests an associated interface from the remote endpoint. | 
|  | 197   void GetGenericRemoteAssociatedInterface( | 
|  | 198       const std::string& name, | 
|  | 199       mojo::ScopedInterfaceEndpointHandle handle); | 
|  | 200 | 
|  | 201   // Template helper to request associated interfaces from the remote endpoint. | 
|  | 202   // Must only be called after the listener's OnChannelConnected is called. | 
|  | 203   template <typename Interface> | 
|  | 204   void GetRemoteAssociatedInterface( | 
|  | 205       mojo::AssociatedInterfacePtr<Interface>* proxy) { | 
|  | 206     mojo::AssociatedInterfaceRequest<Interface> request = | 
|  | 207         mojo::GetProxy(proxy, GetAssociatedGroup()); | 
|  | 208     GetGenericRemoteAssociatedInterface(Interface::Name_, request.PassHandle()); | 
|  | 209   } | 
|  | 210 | 
| 142 #if defined(ENABLE_IPC_FUZZER) | 211 #if defined(ENABLE_IPC_FUZZER) | 
| 143   void set_outgoing_message_filter(OutgoingMessageFilter* filter) { | 212   void set_outgoing_message_filter(OutgoingMessageFilter* filter) { | 
| 144     outgoing_message_filter_ = filter; | 213     outgoing_message_filter_ = filter; | 
| 145   } | 214   } | 
| 146 #endif | 215 #endif | 
| 147 | 216 | 
| 148   // Called to clear the pointer to the IPC task runner when it's going away. | 217   // Called to clear the pointer to the IPC task runner when it's going away. | 
| 149   void ClearIPCTaskRunner(); | 218   void ClearIPCTaskRunner(); | 
| 150 | 219 | 
| 151   // Endpoint overrides. | 220   // Endpoint overrides. | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
| 179 | 248 | 
| 180     // Dispatches a message on the listener thread. | 249     // Dispatches a message on the listener thread. | 
| 181     void OnDispatchMessage(const Message& message); | 250     void OnDispatchMessage(const Message& message); | 
| 182 | 251 | 
| 183     // Sends |message| from appropriate thread. | 252     // Sends |message| from appropriate thread. | 
| 184     void Send(Message* message); | 253     void Send(Message* message); | 
| 185 | 254 | 
| 186     // Indicates if the underlying channel's Send is thread-safe. | 255     // Indicates if the underlying channel's Send is thread-safe. | 
| 187     bool IsChannelSendThreadSafe() const; | 256     bool IsChannelSendThreadSafe() const; | 
| 188 | 257 | 
|  | 258     // Requests a remote associated interface on the IPC thread. | 
|  | 259     void GetRemoteAssociatedInterface( | 
|  | 260         const std::string& name, | 
|  | 261         mojo::ScopedInterfaceEndpointHandle handle); | 
|  | 262 | 
| 189    protected: | 263    protected: | 
| 190     friend class base::RefCountedThreadSafe<Context>; | 264     friend class base::RefCountedThreadSafe<Context>; | 
| 191     ~Context() override; | 265     ~Context() override; | 
| 192 | 266 | 
| 193     // IPC::Listener methods: | 267     // IPC::Listener methods: | 
| 194     bool OnMessageReceived(const Message& message) override; | 268     bool OnMessageReceived(const Message& message) override; | 
| 195     void OnChannelConnected(int32_t peer_pid) override; | 269     void OnChannelConnected(int32_t peer_pid) override; | 
| 196     void OnChannelError() override; | 270     void OnChannelError() override; | 
| 197 | 271 | 
| 198     // Like OnMessageReceived but doesn't try the filters. | 272     // Like OnMessageReceived but doesn't try the filters. | 
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 270     // Lock for pending_filters_. | 344     // Lock for pending_filters_. | 
| 271     base::Lock pending_filters_lock_; | 345     base::Lock pending_filters_lock_; | 
| 272 | 346 | 
| 273     // Cached copy of the peer process ID. Set on IPC but read on both IPC and | 347     // Cached copy of the peer process ID. Set on IPC but read on both IPC and | 
| 274     // listener threads. | 348     // listener threads. | 
| 275     base::ProcessId peer_pid_; | 349     base::ProcessId peer_pid_; | 
| 276 | 350 | 
| 277     // Whether this channel is used as an endpoint for sending and receiving | 351     // Whether this channel is used as an endpoint for sending and receiving | 
| 278     // brokerable attachment messages to/from the broker process. | 352     // brokerable attachment messages to/from the broker process. | 
| 279     bool attachment_broker_endpoint_; | 353     bool attachment_broker_endpoint_; | 
|  | 354 | 
|  | 355     // Modified only on the listener's thread before Init() is called. | 
|  | 356     std::map<std::string, GenericAssociatedInterfaceFactory> | 
|  | 357         io_thread_interfaces_; | 
|  | 358     std::map<std::string, GenericAssociatedInterfaceFactory> | 
|  | 359         proxy_thread_interfaces_; | 
|  | 360 | 
|  | 361     // Valid and constant any time after the ChannelProxy's Listener receives | 
|  | 362     // OnChannelConnected on its own thread. | 
|  | 363     std::unique_ptr<mojo::AssociatedGroup> channel_associated_group_; | 
| 280   }; | 364   }; | 
| 281 | 365 | 
| 282   Context* context() { return context_.get(); } | 366   Context* context() { return context_.get(); } | 
| 283 | 367 | 
| 284 #if defined(ENABLE_IPC_FUZZER) | 368 #if defined(ENABLE_IPC_FUZZER) | 
| 285   OutgoingMessageFilter* outgoing_message_filter() const { | 369   OutgoingMessageFilter* outgoing_message_filter() const { | 
| 286     return outgoing_message_filter_; | 370     return outgoing_message_filter_; | 
| 287   } | 371   } | 
| 288 #endif | 372 #endif | 
| 289 | 373 | 
| 290  protected: | 374  protected: | 
| 291   bool did_init() const { return did_init_; } | 375   bool did_init() const { return did_init_; } | 
| 292 | 376 | 
| 293  private: | 377  private: | 
| 294   friend class IpcSecurityTestUtil; | 378   friend class IpcSecurityTestUtil; | 
| 295 | 379 | 
|  | 380   template <typename Interface> | 
|  | 381   static void BindAssociatedInterfaceRequest( | 
|  | 382       const AssociatedInterfaceFactory<Interface>& factory, | 
|  | 383       mojo::ScopedInterfaceEndpointHandle handle) { | 
|  | 384     mojo::AssociatedInterfaceRequest<Interface> request; | 
|  | 385     request.Bind(std::move(handle)); | 
|  | 386     factory.Run(std::move(request)); | 
|  | 387   } | 
|  | 388 | 
| 296   // Always called once immediately after Init. | 389   // Always called once immediately after Init. | 
| 297   virtual void OnChannelInit(); | 390   virtual void OnChannelInit(); | 
| 298 | 391 | 
| 299   // By maintaining this indirection (ref-counted) to our internal state, we | 392   // By maintaining this indirection (ref-counted) to our internal state, we | 
| 300   // can safely be destroyed while the background thread continues to do stuff | 393   // can safely be destroyed while the background thread continues to do stuff | 
| 301   // that involves this data. | 394   // that involves this data. | 
| 302   scoped_refptr<Context> context_; | 395   scoped_refptr<Context> context_; | 
| 303 | 396 | 
| 304   // Whether the channel has been initialized. | 397   // Whether the channel has been initialized. | 
| 305   bool did_init_; | 398   bool did_init_; | 
| 306 | 399 | 
| 307 #if defined(ENABLE_IPC_FUZZER) | 400 #if defined(ENABLE_IPC_FUZZER) | 
| 308   OutgoingMessageFilter* outgoing_message_filter_; | 401   OutgoingMessageFilter* outgoing_message_filter_; | 
| 309 #endif | 402 #endif | 
| 310 }; | 403 }; | 
| 311 | 404 | 
| 312 }  // namespace IPC | 405 }  // namespace IPC | 
| 313 | 406 | 
| 314 #endif  // IPC_IPC_CHANNEL_PROXY_H_ | 407 #endif  // IPC_IPC_CHANNEL_PROXY_H_ | 
| OLD | NEW | 
|---|