| 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_INTERFACE_PROVIDER_H_ | 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
| 6 #define SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ | 6 #define SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
| 7 | 7 |
| 8 #include "base/bind.h" | |
| 9 #include "services/shell/public/interfaces/interface_provider.mojom.h" | 8 #include "services/shell/public/interfaces/interface_provider.mojom.h" |
| 10 | 9 |
| 11 namespace shell { | 10 namespace shell { |
| 12 | 11 |
| 13 // Encapsulates a mojom::InterfaceProviderPtr implemented in a remote | 12 // Encapsulates a mojom::InterfaceProviderPtr implemented in a remote |
| 14 // application. Provides two main features: | 13 // application. Provides two main features: |
| 15 // - a typesafe GetInterface() method for binding InterfacePtrs. | 14 // - a typesafe GetInterface() method for binding InterfacePtrs. |
| 16 // - 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 |
| 17 // requests for remote interfaces. | 16 // requests for remote interfaces. |
| 18 // 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 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 72 |
| 74 GetInterface(Interface::Name_, std::move(pipe.handle1)); | 73 GetInterface(Interface::Name_, std::move(pipe.handle1)); |
| 75 } | 74 } |
| 76 template <typename Interface> | 75 template <typename Interface> |
| 77 void GetInterface(mojo::InterfaceRequest<Interface> request) { | 76 void GetInterface(mojo::InterfaceRequest<Interface> request) { |
| 78 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); | 77 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); |
| 79 } | 78 } |
| 80 void GetInterface(const std::string& name, | 79 void GetInterface(const std::string& name, |
| 81 mojo::ScopedMessagePipeHandle request_handle); | 80 mojo::ScopedMessagePipeHandle request_handle); |
| 82 | 81 |
| 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: | 82 private: |
| 97 void SetBinderForName( | 83 void SetBinderForName( |
| 98 const std::string& name, | 84 const std::string& name, |
| 99 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { | 85 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { |
| 100 binders_[name] = binder; | 86 binders_[name] = binder; |
| 101 } | 87 } |
| 102 void ClearBinders(); | 88 void ClearBinders(); |
| 103 | 89 |
| 104 using BinderMap = std::map< | 90 using BinderMap = std::map< |
| 105 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; | 91 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; |
| 106 BinderMap binders_; | 92 BinderMap binders_; |
| 107 | 93 |
| 108 mojom::InterfaceProviderPtr interface_provider_; | 94 mojom::InterfaceProviderPtr interface_provider_; |
| 109 mojom::InterfaceProviderRequest pending_request_; | 95 mojom::InterfaceProviderRequest pending_request_; |
| 110 | 96 |
| 111 // A callback to receive all GetInterface() requests in lieu of the | 97 // A callback to receive all GetInterface() requests in lieu of the |
| 112 // InterfaceProvider pipe. | 98 // InterfaceProvider pipe. |
| 113 ForwardCallback forward_callback_; | 99 ForwardCallback forward_callback_; |
| 114 | 100 |
| 115 base::WeakPtrFactory<InterfaceProvider> weak_factory_; | 101 base::WeakPtrFactory<InterfaceProvider> weak_factory_; |
| 116 | 102 |
| 117 DISALLOW_COPY_AND_ASSIGN(InterfaceProvider); | 103 DISALLOW_COPY_AND_ASSIGN(InterfaceProvider); |
| 118 }; | 104 }; |
| 119 | 105 |
| 120 } // namespace shell | 106 } // namespace shell |
| 121 | 107 |
| 122 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ | 108 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
| OLD | NEW |