| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 SERVICES_SHELL_PUBLIC_CPP_REMOTE_INTERFACE_REGISTRY_H_ | 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
| 6 #define SERVICES_SHELL_PUBLIC_CPP_REMOTE_INTERFACE_REGISTRY_H_ | 6 #define SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
| 7 | 7 |
| 8 #include "services/shell/public/interfaces/interface_provider.mojom.h" | 8 #include "services/shell/public/interfaces/interface_provider.mojom.h" |
| 9 | 9 |
| 10 namespace shell { | 10 namespace shell { |
| 11 | 11 |
| 12 // Encapsulates a mojom::InterfaceProviderPtr implemented in a remote | 12 // Encapsulates a mojom::InterfaceProviderPtr implemented in a remote |
| 13 // application. Provides two main features: | 13 // application. Provides two main features: |
| 14 // - a typesafe GetInterface() method for binding InterfacePtrs. | 14 // - a typesafe GetInterface() method for binding InterfacePtrs. |
| 15 // - a testing API that allows local callbacks to be registered that bind | 15 // - a testing API that allows local callbacks to be registered that bind |
| 16 // requests for remote interfaces. | 16 // requests for remote interfaces. |
| 17 // An instance of this class is used by the GetInterface() methods on | 17 // An instance of this class is used by the GetInterface() methods on |
| 18 // Connection. | 18 // Connection. |
| 19 class RemoteInterfaceRegistry { | 19 class InterfaceProvider { |
| 20 public: | 20 public: |
| 21 class TestApi { | 21 class TestApi { |
| 22 public: | 22 public: |
| 23 explicit TestApi(RemoteInterfaceRegistry* registry) : registry_(registry) {} | 23 explicit TestApi(InterfaceProvider* provider) : provider_(provider) {} |
| 24 ~TestApi() {} | 24 ~TestApi() {} |
| 25 | 25 |
| 26 template <typename Interface> | |
| 27 void SetBinderForName( | 26 void SetBinderForName( |
| 28 const std::string& name, | 27 const std::string& name, |
| 29 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& binder) { | 28 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { |
| 30 registry_->SetBinderForName(name, binder); | 29 provider_->SetBinderForName(name, binder); |
| 31 } | 30 } |
| 32 | 31 |
| 33 void ClearBinders() { | 32 void ClearBinders() { |
| 34 registry_->ClearBinders(); | 33 provider_->ClearBinders(); |
| 35 } | 34 } |
| 36 | 35 |
| 37 private: | 36 private: |
| 38 RemoteInterfaceRegistry* registry_; | 37 InterfaceProvider* provider_; |
| 39 DISALLOW_COPY_AND_ASSIGN(TestApi); | 38 DISALLOW_COPY_AND_ASSIGN(TestApi); |
| 40 }; | 39 }; |
| 41 | 40 |
| 42 explicit RemoteInterfaceRegistry( | 41 explicit InterfaceProvider(mojom::InterfaceProviderPtr interface_provider); |
| 43 mojom::InterfaceProviderPtr remote_interfaces); | 42 ~InterfaceProvider(); |
| 44 ~RemoteInterfaceRegistry(); | |
| 45 | 43 |
| 46 // Returns a raw pointer to the remote InterfaceProvider. | 44 // Returns a raw pointer to the remote InterfaceProvider. |
| 47 mojom::InterfaceProvider* GetInterfaceProvider(); | 45 mojom::InterfaceProvider* get() { return interface_provider_.get(); } |
| 48 | 46 |
| 49 // Sets a closure to be run when the remote InterfaceProvider pipe is closed. | 47 // Sets a closure to be run when the remote InterfaceProvider pipe is closed. |
| 50 void SetConnectionLostClosure(const base::Closure& connection_lost_closure); | 48 void SetConnectionLostClosure(const base::Closure& connection_lost_closure); |
| 51 | 49 |
| 50 base::WeakPtr<InterfaceProvider> GetWeakPtr(); |
| 51 |
| 52 // Binds |ptr| to an implementation of Interface in the remote application. | 52 // Binds |ptr| to an implementation of Interface in the remote application. |
| 53 // |ptr| can immediately be used to start sending requests to the remote | 53 // |ptr| can immediately be used to start sending requests to the remote |
| 54 // interface. | 54 // interface. |
| 55 template <typename Interface> | 55 template <typename Interface> |
| 56 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { | 56 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { |
| 57 mojo::MessagePipe pipe; | 57 mojo::MessagePipe pipe; |
| 58 ptr->Bind(mojo::InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); | 58 ptr->Bind(mojo::InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); |
| 59 | 59 |
| 60 // Local binders can be registered via TestApi. | 60 // Local binders can be registered via TestApi. |
| 61 auto it = binders_.find(Interface::Name_); | 61 auto it = binders_.find(Interface::Name_); |
| 62 if (it != binders_.end()) { | 62 if (it != binders_.end()) { |
| 63 it->second.Run(std::move(pipe.handle1)); | 63 it->second.Run(std::move(pipe.handle1)); |
| 64 return; | 64 return; |
| 65 } | 65 } |
| 66 remote_interfaces_->GetInterface(Interface::Name_, std::move(pipe.handle1)); | 66 interface_provider_->GetInterface(Interface::Name_, |
| 67 std::move(pipe.handle1)); |
| 67 } | 68 } |
| 68 template <typename Interface> | 69 template <typename Interface> |
| 69 void GetInterface(mojo::InterfaceRequest<Interface> request) { | 70 void GetInterface(mojo::InterfaceRequest<Interface> request) { |
| 70 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); | 71 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); |
| 71 } | 72 } |
| 72 void GetInterface(const std::string& name, | 73 void GetInterface(const std::string& name, |
| 73 mojo::ScopedMessagePipeHandle request_handle); | 74 mojo::ScopedMessagePipeHandle request_handle); |
| 74 | 75 |
| 75 private: | 76 private: |
| 76 template <typename Interface> | |
| 77 void SetBinderForName( | 77 void SetBinderForName( |
| 78 const std::string& name, | 78 const std::string& name, |
| 79 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& binder) { | 79 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { |
| 80 binders_[name] = binder; | 80 binders_[name] = binder; |
| 81 } | 81 } |
| 82 void ClearBinders(); | 82 void ClearBinders(); |
| 83 | 83 |
| 84 using BinderMap = std::map< | 84 using BinderMap = std::map< |
| 85 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; | 85 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; |
| 86 BinderMap binders_; | 86 BinderMap binders_; |
| 87 | 87 |
| 88 mojom::InterfaceProviderPtr remote_interfaces_; | 88 mojom::InterfaceProviderPtr interface_provider_; |
| 89 | 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(RemoteInterfaceRegistry); | 90 base::WeakPtrFactory<InterfaceProvider> weak_factory_; |
| 91 |
| 92 DISALLOW_COPY_AND_ASSIGN(InterfaceProvider); |
| 91 }; | 93 }; |
| 92 | 94 |
| 93 } // namespace shell | 95 } // namespace shell |
| 94 | 96 |
| 95 #endif // SERVICES_SHELL_PUBLIC_CPP_REMOTE_INTERFACE_REGISTRY_H_ | 97 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
| OLD | NEW |