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_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_PROVIDER_H_ | 5 #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_PROVIDER_H_ | 6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" | 9 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" |
10 | 10 |
11 namespace service_manager { | 11 namespace service_manager { |
12 | 12 |
| 13 struct BindSourceInfo; |
| 14 |
13 // Encapsulates a mojom::InterfaceProviderPtr implemented in a remote | 15 // Encapsulates a mojom::InterfaceProviderPtr implemented in a remote |
14 // application. Provides two main features: | 16 // application. Provides two main features: |
15 // - a typesafe GetInterface() method for binding InterfacePtrs. | 17 // - a typesafe GetInterface() method for binding InterfacePtrs. |
16 // - a testing API that allows local callbacks to be registered that bind | 18 // - a testing API that allows local callbacks to be registered that bind |
17 // requests for remote interfaces. | 19 // requests for remote interfaces. |
18 // An instance of this class is used by the GetInterface() methods on | 20 // An instance of this class is used by the GetInterface() methods on |
19 // Connection. | 21 // Connection. |
20 class InterfaceProvider { | 22 class InterfaceProvider { |
21 public: | 23 public: |
22 using ForwardCallback = base::Callback<void(const std::string&, | 24 using ForwardCallback = base::Callback<void(const std::string&, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 GetInterface(Interface::Name_, std::move(pipe.handle1)); | 76 GetInterface(Interface::Name_, std::move(pipe.handle1)); |
75 } | 77 } |
76 template <typename Interface> | 78 template <typename Interface> |
77 void GetInterface(mojo::InterfaceRequest<Interface> request) { | 79 void GetInterface(mojo::InterfaceRequest<Interface> request) { |
78 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); | 80 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); |
79 } | 81 } |
80 void GetInterface(const std::string& name, | 82 void GetInterface(const std::string& name, |
81 mojo::ScopedMessagePipeHandle request_handle); | 83 mojo::ScopedMessagePipeHandle request_handle); |
82 | 84 |
83 // Returns a callback to GetInterface<Interface>(). This can be passed to | 85 // Returns a callback to GetInterface<Interface>(). This can be passed to |
84 // InterfaceRegistry::AddInterface() to forward requests. | 86 // BinderRegistry::AddInterface() to forward requests. |
85 template <typename Interface> | 87 template <typename Interface> |
86 base::Callback<void(mojo::InterfaceRequest<Interface>)> | 88 base::Callback<void(const BindSourceInfo&, mojo::InterfaceRequest<Interface>)> |
87 CreateInterfaceFactory() { | 89 CreateInterfaceFactory() { |
88 // InterfaceProvider::GetInterface() is overloaded, so static_cast to select | 90 return base::Bind( |
89 // the overload that takes an mojo::InterfaceRequest<Interface>. | 91 &InterfaceProvider::BindInterfaceRequestFromSource<Interface>, |
90 return base::Bind(static_cast<void (InterfaceProvider::*)( | 92 GetWeakPtr()); |
91 mojo::InterfaceRequest<Interface>)>( | |
92 &InterfaceProvider::GetInterface<Interface>), | |
93 GetWeakPtr()); | |
94 } | 93 } |
95 | 94 |
96 private: | 95 private: |
| 96 template <typename Interface> |
| 97 void BindInterfaceRequestFromSource( |
| 98 const BindSourceInfo& source_info, |
| 99 mojo::InterfaceRequest<Interface> request) { |
| 100 GetInterface<Interface>(std::move(request)); |
| 101 } |
| 102 |
97 void SetBinderForName( | 103 void SetBinderForName( |
98 const std::string& name, | 104 const std::string& name, |
99 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { | 105 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { |
100 binders_[name] = binder; | 106 binders_[name] = binder; |
101 } | 107 } |
102 void ClearBinders(); | 108 void ClearBinders(); |
103 | 109 |
104 using BinderMap = std::map< | 110 using BinderMap = std::map< |
105 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; | 111 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; |
106 BinderMap binders_; | 112 BinderMap binders_; |
107 | 113 |
108 mojom::InterfaceProviderPtr interface_provider_; | 114 mojom::InterfaceProviderPtr interface_provider_; |
109 mojom::InterfaceProviderRequest pending_request_; | 115 mojom::InterfaceProviderRequest pending_request_; |
110 | 116 |
111 // A callback to receive all GetInterface() requests in lieu of the | 117 // A callback to receive all GetInterface() requests in lieu of the |
112 // InterfaceProvider pipe. | 118 // InterfaceProvider pipe. |
113 ForwardCallback forward_callback_; | 119 ForwardCallback forward_callback_; |
114 | 120 |
115 base::WeakPtrFactory<InterfaceProvider> weak_factory_; | 121 base::WeakPtrFactory<InterfaceProvider> weak_factory_; |
116 | 122 |
117 DISALLOW_COPY_AND_ASSIGN(InterfaceProvider); | 123 DISALLOW_COPY_AND_ASSIGN(InterfaceProvider); |
118 }; | 124 }; |
119 | 125 |
120 } // namespace service_manager | 126 } // namespace service_manager |
121 | 127 |
122 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_PROVIDER_H_ | 128 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
OLD | NEW |