| OLD | NEW | 
 | (Empty) | 
|    1 // Copyright 2016 The Chromium Authors. All rights reserved. |  | 
|    2 // Use of this source code is governed by a BSD-style license that can be |  | 
|    3 // found in the LICENSE file. |  | 
|    4  |  | 
|    5 #ifndef SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |  | 
|    6 #define SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |  | 
|    7  |  | 
|    8 #include "base/bind.h" |  | 
|    9 #include "services/shell/public/interfaces/interface_provider.mojom.h" |  | 
|   10  |  | 
|   11 namespace shell { |  | 
|   12  |  | 
|   13 // Encapsulates a mojom::InterfaceProviderPtr implemented in a remote |  | 
|   14 // application. Provides two main features: |  | 
|   15 // - a typesafe GetInterface() method for binding InterfacePtrs. |  | 
|   16 // - a testing API that allows local callbacks to be registered that bind |  | 
|   17 //   requests for remote interfaces. |  | 
|   18 // An instance of this class is used by the GetInterface() methods on |  | 
|   19 // Connection. |  | 
|   20 class InterfaceProvider { |  | 
|   21  public: |  | 
|   22   using ForwardCallback = base::Callback<void(const std::string&, |  | 
|   23                                               mojo::ScopedMessagePipeHandle)>; |  | 
|   24   class TestApi { |  | 
|   25    public: |  | 
|   26     explicit TestApi(InterfaceProvider* provider) : provider_(provider) {} |  | 
|   27     ~TestApi() {} |  | 
|   28  |  | 
|   29     void SetBinderForName( |  | 
|   30         const std::string& name, |  | 
|   31         const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { |  | 
|   32       provider_->SetBinderForName(name, binder); |  | 
|   33     } |  | 
|   34  |  | 
|   35     void ClearBinders() { |  | 
|   36       provider_->ClearBinders(); |  | 
|   37     } |  | 
|   38  |  | 
|   39    private: |  | 
|   40     InterfaceProvider* provider_; |  | 
|   41     DISALLOW_COPY_AND_ASSIGN(TestApi); |  | 
|   42   }; |  | 
|   43  |  | 
|   44   InterfaceProvider(); |  | 
|   45   ~InterfaceProvider(); |  | 
|   46  |  | 
|   47   // Binds this InterfaceProvider to an actual mojom::InterfaceProvider pipe. |  | 
|   48   // It is an error to call this on a forwarding InterfaceProvider, i.e. this |  | 
|   49   // call is exclusive to Forward(). |  | 
|   50   void Bind(mojom::InterfaceProviderPtr interface_provider); |  | 
|   51  |  | 
|   52   // Sets this InterfaceProvider to forward all GetInterface() requests to |  | 
|   53   // |callback|. It is an error to call this on a bound InterfaceProvider, i.e. |  | 
|   54   // this call is exclusive to Bind(). In addition, and unlike Bind(), this MUST |  | 
|   55   // be called before any calls to GetInterface() are made. |  | 
|   56   void Forward(const ForwardCallback& callback); |  | 
|   57  |  | 
|   58   // Returns a raw pointer to the remote InterfaceProvider. |  | 
|   59   mojom::InterfaceProvider* get() { return interface_provider_.get(); } |  | 
|   60  |  | 
|   61   // Sets a closure to be run when the remote InterfaceProvider pipe is closed. |  | 
|   62   void SetConnectionLostClosure(const base::Closure& connection_lost_closure); |  | 
|   63  |  | 
|   64   base::WeakPtr<InterfaceProvider> GetWeakPtr(); |  | 
|   65  |  | 
|   66   // Binds |ptr| to an implementation of Interface in the remote application. |  | 
|   67   // |ptr| can immediately be used to start sending requests to the remote |  | 
|   68   // interface. |  | 
|   69   template <typename Interface> |  | 
|   70   void GetInterface(mojo::InterfacePtr<Interface>* ptr) { |  | 
|   71     mojo::MessagePipe pipe; |  | 
|   72     ptr->Bind(mojo::InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); |  | 
|   73  |  | 
|   74     GetInterface(Interface::Name_, std::move(pipe.handle1)); |  | 
|   75   } |  | 
|   76   template <typename Interface> |  | 
|   77   void GetInterface(mojo::InterfaceRequest<Interface> request) { |  | 
|   78     GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); |  | 
|   79   } |  | 
|   80   void GetInterface(const std::string& name, |  | 
|   81                     mojo::ScopedMessagePipeHandle request_handle); |  | 
|   82  |  | 
|   83   // Returns a callback to GetInterface<Interface>(). This can be passed to |  | 
|   84   // InterfaceRegistry::AddInterface() to forward requests. |  | 
|   85   template <typename Interface> |  | 
|   86   base::Callback<void(mojo::InterfaceRequest<Interface>)> |  | 
|   87   CreateInterfaceFactory() { |  | 
|   88     // InterfaceProvider::GetInterface() is overloaded, so static_cast to select |  | 
|   89     // the overload that takes an mojo::InterfaceRequest<Interface>. |  | 
|   90     return base::Bind(static_cast<void (InterfaceProvider::*)( |  | 
|   91                           mojo::InterfaceRequest<Interface>)>( |  | 
|   92                           &InterfaceProvider::GetInterface<Interface>), |  | 
|   93                       GetWeakPtr()); |  | 
|   94   } |  | 
|   95  |  | 
|   96  private: |  | 
|   97   void SetBinderForName( |  | 
|   98       const std::string& name, |  | 
|   99       const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { |  | 
|  100     binders_[name] = binder; |  | 
|  101   } |  | 
|  102   void ClearBinders(); |  | 
|  103  |  | 
|  104   using BinderMap = std::map< |  | 
|  105       std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; |  | 
|  106   BinderMap binders_; |  | 
|  107  |  | 
|  108   mojom::InterfaceProviderPtr interface_provider_; |  | 
|  109   mojom::InterfaceProviderRequest pending_request_; |  | 
|  110  |  | 
|  111   // A callback to receive all GetInterface() requests in lieu of the |  | 
|  112   // InterfaceProvider pipe. |  | 
|  113   ForwardCallback forward_callback_; |  | 
|  114  |  | 
|  115   base::WeakPtrFactory<InterfaceProvider> weak_factory_; |  | 
|  116  |  | 
|  117   DISALLOW_COPY_AND_ASSIGN(InterfaceProvider); |  | 
|  118 }; |  | 
|  119  |  | 
|  120 }  // namespace shell |  | 
|  121  |  | 
|  122 #endif  // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |  | 
| OLD | NEW |