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 "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 InterfaceProvider { | 19 class InterfaceProvider { |
20 public: | 20 public: |
21 using ForwardCallback = base::Callback<void(const mojo::String&, | |
22 mojo::ScopedMessagePipeHandle)>; | |
23 class TestApi { | 21 class TestApi { |
24 public: | 22 public: |
25 explicit TestApi(InterfaceProvider* provider) : provider_(provider) {} | 23 explicit TestApi(InterfaceProvider* provider) : provider_(provider) {} |
26 ~TestApi() {} | 24 ~TestApi() {} |
27 | 25 |
28 void SetBinderForName( | 26 void SetBinderForName( |
29 const std::string& name, | 27 const std::string& name, |
30 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { | 28 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) { |
31 provider_->SetBinderForName(name, binder); | 29 provider_->SetBinderForName(name, binder); |
32 } | 30 } |
33 | 31 |
34 void ClearBinders() { | 32 void ClearBinders() { |
35 provider_->ClearBinders(); | 33 provider_->ClearBinders(); |
36 } | 34 } |
37 | 35 |
38 private: | 36 private: |
39 InterfaceProvider* provider_; | 37 InterfaceProvider* provider_; |
40 DISALLOW_COPY_AND_ASSIGN(TestApi); | 38 DISALLOW_COPY_AND_ASSIGN(TestApi); |
41 }; | 39 }; |
42 | 40 |
43 InterfaceProvider(); | 41 InterfaceProvider(); |
44 ~InterfaceProvider(); | 42 ~InterfaceProvider(); |
45 | 43 |
46 // Binds this InterfaceProvider to an actual mojom::InterfaceProvider pipe. | |
47 // It is an error to call this on a forwarding InterfaceProvider, i.e. this | |
48 // call is exclusive to Forward(). | |
49 void Bind(mojom::InterfaceProviderPtr interface_provider); | 44 void Bind(mojom::InterfaceProviderPtr interface_provider); |
50 | 45 |
51 // Sets this InterfaceProvider to forward all GetInterface() requests to | |
52 // |callback|. It is an error to call this on a bound InterfaceProvider, i.e. | |
53 // this call is exclusive to Bind(). In addition, and unlike Bind(), this MUST | |
54 // be called before any calls to GetInterface() are made. | |
55 void Forward(const ForwardCallback& callback); | |
56 | |
57 // Returns a raw pointer to the remote InterfaceProvider. | 46 // Returns a raw pointer to the remote InterfaceProvider. |
58 mojom::InterfaceProvider* get() { return interface_provider_.get(); } | 47 mojom::InterfaceProvider* get() { return interface_provider_.get(); } |
59 | 48 |
60 // Sets a closure to be run when the remote InterfaceProvider pipe is closed. | 49 // Sets a closure to be run when the remote InterfaceProvider pipe is closed. |
61 void SetConnectionLostClosure(const base::Closure& connection_lost_closure); | 50 void SetConnectionLostClosure(const base::Closure& connection_lost_closure); |
62 | 51 |
63 base::WeakPtr<InterfaceProvider> GetWeakPtr(); | 52 base::WeakPtr<InterfaceProvider> GetWeakPtr(); |
64 | 53 |
65 // Binds |ptr| to an implementation of Interface in the remote application. | 54 // Binds |ptr| to an implementation of Interface in the remote application. |
66 // |ptr| can immediately be used to start sending requests to the remote | 55 // |ptr| can immediately be used to start sending requests to the remote |
(...skipping 20 matching lines...) Expand all Loading... |
87 } | 76 } |
88 void ClearBinders(); | 77 void ClearBinders(); |
89 | 78 |
90 using BinderMap = std::map< | 79 using BinderMap = std::map< |
91 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; | 80 std::string, base::Callback<void(mojo::ScopedMessagePipeHandle)>>; |
92 BinderMap binders_; | 81 BinderMap binders_; |
93 | 82 |
94 mojom::InterfaceProviderPtr interface_provider_; | 83 mojom::InterfaceProviderPtr interface_provider_; |
95 mojom::InterfaceProviderRequest pending_request_; | 84 mojom::InterfaceProviderRequest pending_request_; |
96 | 85 |
97 // A callback to receive all GetInterface() requests in lieu of the | |
98 // InterfaceProvider pipe. | |
99 ForwardCallback forward_callback_; | |
100 | |
101 base::WeakPtrFactory<InterfaceProvider> weak_factory_; | 86 base::WeakPtrFactory<InterfaceProvider> weak_factory_; |
102 | 87 |
103 DISALLOW_COPY_AND_ASSIGN(InterfaceProvider); | 88 DISALLOW_COPY_AND_ASSIGN(InterfaceProvider); |
104 }; | 89 }; |
105 | 90 |
106 } // namespace shell | 91 } // namespace shell |
107 | 92 |
108 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ | 93 #endif // SERVICES_SHELL_PUBLIC_CPP_INTERFACE_PROVIDER_H_ |
OLD | NEW |