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_CONNECTOR_H_ | 5 #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_ |
6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_ | 6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "services/service_manager/public/cpp/connection.h" | |
11 #include "services/service_manager/public/cpp/identity.h" | 10 #include "services/service_manager/public/cpp/identity.h" |
12 #include "services/service_manager/public/interfaces/connector.mojom.h" | 11 #include "services/service_manager/public/interfaces/connector.mojom.h" |
13 #include "services/service_manager/public/interfaces/service.mojom.h" | 12 #include "services/service_manager/public/interfaces/service.mojom.h" |
14 #include "services/service_manager/public/interfaces/service_manager.mojom.h" | 13 #include "services/service_manager/public/interfaces/service_manager.mojom.h" |
15 | 14 |
16 namespace service_manager { | 15 namespace service_manager { |
17 | 16 |
18 // An interface that encapsulates the Service Manager's brokering interface, by | 17 // An interface that encapsulates the Service Manager's brokering interface, by |
19 // which | 18 // which |
20 // connections between services are established. Once Connect() is called, | 19 // connections between services are established. Once either StartService() or |
21 // this class is bound to the thread the call was made on and it cannot be | 20 // BindInterface() is called, this class is bound to the thread the call was |
22 // passed to another thread without calling Clone(). | 21 // made on and it cannot be passed to another thread without calling Clone(). |
23 // | 22 // |
24 // An instance of this class is created internally by ServiceContext for use | 23 // An instance of this class is created internally by ServiceContext for use |
25 // on the thread ServiceContext is instantiated on. | 24 // on the thread ServiceContext is instantiated on. |
26 // | 25 // |
27 // To use this interface on another thread, call Clone() and pass the new | 26 // To use this interface on another thread, call Clone() and pass the new |
28 // instance to the desired thread before calling Connect(). | 27 // instance to the desired thread before calling StartService() or |
| 28 // BindInterface(). |
29 // | 29 // |
30 // While instances of this object are owned by the caller, the underlying | 30 // While instances of this object are owned by the caller, the underlying |
31 // connection with the service manager is bound to the lifetime of the instance | 31 // connection with the service manager is bound to the lifetime of the instance |
32 // that | 32 // that created it, i.e. when the application is terminated the Connector pipe |
33 // created it, i.e. when the application is terminated the Connector pipe is | 33 // is closed. |
34 // closed. | |
35 class Connector { | 34 class Connector { |
36 public: | 35 public: |
| 36 using StartServiceCallback = |
| 37 base::Callback<void(mojom::ConnectResult, const Identity& identity)>; |
| 38 |
37 class TestApi { | 39 class TestApi { |
38 public: | 40 public: |
39 using Binder = base::Callback<void(mojo::ScopedMessagePipeHandle)>; | 41 using Binder = base::Callback<void(mojo::ScopedMessagePipeHandle)>; |
40 explicit TestApi(Connector* connector) : connector_(connector) {} | 42 explicit TestApi(Connector* connector) : connector_(connector) {} |
| 43 ~TestApi() { connector_->ResetStartServiceCallback(); } |
41 | 44 |
42 // Allows caller to specify a callback to bind requests for |interface_name| | 45 // Allows caller to specify a callback to bind requests for |interface_name| |
43 // from |service_name| locally, rather than passing the request through the | 46 // from |service_name| locally, rather than passing the request through the |
44 // Service Manager. | 47 // Service Manager. |
45 void OverrideBinderForTesting(const std::string& service_name, | 48 void OverrideBinderForTesting(const std::string& service_name, |
46 const std::string& interface_name, | 49 const std::string& interface_name, |
47 const Binder& binder) { | 50 const Binder& binder) { |
48 connector_->OverrideBinderForTesting(service_name, interface_name, | 51 connector_->OverrideBinderForTesting(service_name, interface_name, |
49 binder); | 52 binder); |
50 } | 53 } |
51 void ClearBinderOverrides() { connector_->ClearBinderOverrides(); } | 54 void ClearBinderOverrides() { connector_->ClearBinderOverrides(); } |
52 | 55 |
| 56 // Register a callback to be run with the result of an attempt to start a |
| 57 // service. This will be run in response to calls to StartService() or |
| 58 // BindInterface(). |
| 59 void SetStartServiceCallback(const StartServiceCallback& callback) { |
| 60 connector_->SetStartServiceCallback(callback); |
| 61 } |
| 62 |
53 private: | 63 private: |
54 Connector* connector_; | 64 Connector* connector_; |
55 }; | 65 }; |
56 | 66 |
57 virtual ~Connector() {} | 67 virtual ~Connector() {} |
58 | 68 |
59 // Creates a new Connector instance and fills in |*request| with a request | 69 // Creates a new Connector instance and fills in |*request| with a request |
60 // for the other end the Connector's interface. | 70 // for the other end the Connector's interface. |
61 static std::unique_ptr<Connector> Create(mojom::ConnectorRequest* request); | 71 static std::unique_ptr<Connector> Create(mojom::ConnectorRequest* request); |
62 | 72 |
| 73 // Creates an instance of a service for |identity|. |
| 74 virtual void StartService(const Identity& identity) = 0; |
| 75 |
| 76 // Creates an instance of the service |name| inheriting the caller's identity. |
| 77 virtual void StartService(const std::string& name) = 0; |
| 78 |
63 // Creates an instance of a service for |identity| in a process started by the | 79 // Creates an instance of a service for |identity| in a process started by the |
64 // client (or someone else). Must be called before Connect() may be called to | 80 // client (or someone else). Must be called before BindInterface() may be |
65 // |identity|. | 81 // called to |identity|. |
66 virtual void StartService( | 82 virtual void StartService( |
67 const Identity& identity, | 83 const Identity& identity, |
68 mojom::ServicePtr service, | 84 mojom::ServicePtr service, |
69 mojom::PIDReceiverRequest pid_receiver_request) = 0; | 85 mojom::PIDReceiverRequest pid_receiver_request) = 0; |
70 | 86 |
71 // Requests a new connection to a service. Returns a pointer to the | |
72 // connection if the connection is permitted by that service, nullptr | |
73 // otherwise. Once this method is called, this object is bound to the thread | |
74 // on which the call took place. To pass to another thread, call Clone() and | |
75 // pass the result. | |
76 virtual std::unique_ptr<Connection> Connect(const std::string& name) = 0; | |
77 virtual std::unique_ptr<Connection> Connect(const Identity& target) = 0; | |
78 | |
79 // Connect to |target| & request to bind |Interface|. | 87 // Connect to |target| & request to bind |Interface|. |
80 template <typename Interface> | 88 template <typename Interface> |
81 void BindInterface(const Identity& target, | 89 void BindInterface(const Identity& target, |
82 mojo::InterfacePtr<Interface>* ptr) { | 90 mojo::InterfacePtr<Interface>* ptr) { |
83 mojo::MessagePipe pipe; | 91 mojo::MessagePipe pipe; |
84 ptr->Bind(mojo::InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); | 92 ptr->Bind(mojo::InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); |
85 BindInterface(target, Interface::Name_, std::move(pipe.handle1)); | 93 BindInterface(target, Interface::Name_, std::move(pipe.handle1)); |
86 } | 94 } |
87 template <typename Interface> | 95 template <typename Interface> |
88 void BindInterface(const std::string& name, | 96 void BindInterface(const std::string& name, |
89 mojo::InterfacePtr<Interface>* ptr) { | 97 mojo::InterfacePtr<Interface>* ptr) { |
90 return BindInterface(Identity(name, mojom::kInheritUserID), ptr); | 98 return BindInterface(Identity(name, mojom::kInheritUserID), ptr); |
91 } | 99 } |
92 template <typename Interface> | 100 template <typename Interface> |
93 void BindInterface(const std::string& name, | 101 void BindInterface(const std::string& name, |
94 mojo::InterfaceRequest<Interface> request) { | 102 mojo::InterfaceRequest<Interface> request) { |
95 return BindInterface(Identity(name, mojom::kInheritUserID), | 103 return BindInterface(Identity(name, mojom::kInheritUserID), |
96 Interface::Name_, request.PassMessagePipe()); | 104 Interface::Name_, request.PassMessagePipe()); |
97 } | 105 } |
98 virtual void BindInterface(const Identity& target, | 106 virtual void BindInterface(const Identity& target, |
99 const std::string& interface_name, | 107 const std::string& interface_name, |
100 mojo::ScopedMessagePipeHandle interface_pipe) = 0; | 108 mojo::ScopedMessagePipeHandle interface_pipe) = 0; |
101 | 109 |
102 // Creates a new instance of this class which may be passed to another thread. | 110 // Creates a new instance of this class which may be passed to another thread. |
103 // The returned object may be passed multiple times until Connect() is called, | 111 // The returned object may be passed multiple times until StartService() or |
104 // at which point this method must be called again to pass again. | 112 // BindInterface() is called, at which point this method must be called again |
| 113 // to pass again. |
105 virtual std::unique_ptr<Connector> Clone() = 0; | 114 virtual std::unique_ptr<Connector> Clone() = 0; |
106 | 115 |
107 // Binds a Connector request to the other end of this Connector. | 116 // Binds a Connector request to the other end of this Connector. |
108 virtual void BindConnectorRequest(mojom::ConnectorRequest request) = 0; | 117 virtual void BindConnectorRequest(mojom::ConnectorRequest request) = 0; |
109 | 118 |
110 virtual base::WeakPtr<Connector> GetWeakPtr() = 0; | 119 virtual base::WeakPtr<Connector> GetWeakPtr() = 0; |
111 | 120 |
112 protected: | 121 protected: |
113 virtual void OverrideBinderForTesting(const std::string& service_name, | 122 virtual void OverrideBinderForTesting(const std::string& service_name, |
114 const std::string& interface_name, | 123 const std::string& interface_name, |
115 const TestApi::Binder& binder) = 0; | 124 const TestApi::Binder& binder) = 0; |
116 virtual void ClearBinderOverrides() = 0; | 125 virtual void ClearBinderOverrides() = 0; |
| 126 virtual void SetStartServiceCallback( |
| 127 const StartServiceCallback& callback) = 0; |
| 128 virtual void ResetStartServiceCallback() = 0; |
117 }; | 129 }; |
118 | 130 |
119 } // namespace service_manager | 131 } // namespace service_manager |
120 | 132 |
121 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_ | 133 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_ |
OLD | NEW |