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 GetInterface(Interface::Name_, std::move(pipe.handle1)); |
61 auto it = binders_.find(Interface::Name_); | |
62 if (it != binders_.end()) { | |
63 it->second.Run(std::move(pipe.handle1)); | |
64 return; | |
65 } | |
66 remote_interfaces_->GetInterface(Interface::Name_, std::move(pipe.handle1)); | |
67 } | 61 } |
68 template <typename Interface> | 62 template <typename Interface> |
69 void GetInterface(mojo::InterfaceRequest<Interface> request) { | 63 void GetInterface(mojo::InterfaceRequest<Interface> request) { |
70 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); | 64 GetInterface(Interface::Name_, std::move(request.PassMessagePipe())); |
71 } | 65 } |
72 void GetInterface(const std::string& name, | 66 void GetInterface(const std::string& name, |
73 mojo::ScopedMessagePipeHandle request_handle); | 67 mojo::ScopedMessagePipeHandle request_handle); |
74 | 68 |
75 private: | 69 private: |
76 template <typename Interface> | |
77 void SetBinderForName( | 70 void SetBinderForName( |
78 const std::string& name, | 71 const std::string& name, |
79 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& binder) { | 72 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { |
80 binders_[name] = binder; | 73 binders_[name] = binder; |
81 } | 74 } |
82 void ClearBinders(); | 75 void ClearBinders(); |
83 | 76 |
84 using BinderMap = std::map< | 77 using BinderMap = std::map< |
85 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; | 78 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; |
86 BinderMap binders_; | 79 BinderMap binders_; |
87 | 80 |
88 mojom::InterfaceProviderPtr remote_interfaces_; | 81 mojom::InterfaceProviderPtr interface_provider_; |
89 | 82 |
90 DISALLOW_COPY_AND_ASSIGN(RemoteInterfaceRegistry); | 83 base::WeakPtrFactory<InterfaceProvider> weak_factory_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(InterfaceProvider); |
91 }; | 86 }; |
92 | 87 |
93 } // namespace shell | 88 } // namespace shell |
94 | 89 |
95 #endif // SERVICES_SHELL_PUBLIC_CPP_REMOTE_INTERFACE_REGISTRY_H_ | 90 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
OLD | NEW |