| 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" | 10 #include "services/service_manager/public/cpp/connection.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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 |
| 33 // created it, i.e. when the application is terminated the Connector pipe is | 33 // created it, i.e. when the application is terminated the Connector pipe is |
| 34 // closed. | 34 // closed. |
| 35 class Connector { | 35 class Connector { |
| 36 public: | 36 public: |
| 37 virtual ~Connector() {} | 37 virtual ~Connector() {} |
| 38 | 38 |
| 39 class ConnectParams { | |
| 40 public: | |
| 41 explicit ConnectParams(const Identity& target); | |
| 42 explicit ConnectParams(const std::string& name); | |
| 43 ~ConnectParams(); | |
| 44 | |
| 45 const Identity& target() { return target_; } | |
| 46 void set_target(const Identity& target) { target_ = target; } | |
| 47 void set_client_process_connection( | |
| 48 mojom::ServicePtr service, | |
| 49 mojom::PIDReceiverRequest pid_receiver_request) { | |
| 50 service_ = std::move(service); | |
| 51 pid_receiver_request_ = std::move(pid_receiver_request); | |
| 52 } | |
| 53 void TakeClientProcessConnection( | |
| 54 mojom::ServicePtr* service, | |
| 55 mojom::PIDReceiverRequest* pid_receiver_request) { | |
| 56 *service = std::move(service_); | |
| 57 *pid_receiver_request = std::move(pid_receiver_request_); | |
| 58 } | |
| 59 InterfaceProvider* remote_interfaces() { return remote_interfaces_; } | |
| 60 void set_remote_interfaces(InterfaceProvider* remote_interfaces) { | |
| 61 remote_interfaces_ = remote_interfaces; | |
| 62 } | |
| 63 | |
| 64 private: | |
| 65 Identity target_; | |
| 66 mojom::ServicePtr service_; | |
| 67 mojom::PIDReceiverRequest pid_receiver_request_; | |
| 68 InterfaceProvider* remote_interfaces_ = nullptr; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(ConnectParams); | |
| 71 }; | |
| 72 | |
| 73 // Creates a new Connector instance and fills in |*request| with a request | 39 // Creates a new Connector instance and fills in |*request| with a request |
| 74 // for the other end the Connector's interface. | 40 // for the other end the Connector's interface. |
| 75 static std::unique_ptr<Connector> Create(mojom::ConnectorRequest* request); | 41 static std::unique_ptr<Connector> Create(mojom::ConnectorRequest* request); |
| 76 | 42 |
| 77 // Requests a new connection to an application. Returns a pointer to the | 43 // Registers an instance of a service in a process started by the client (or |
| 78 // connection if the connection is permitted by this application's delegate, | 44 // someone else). Must be called before Connect() may be called to |identity|. |
| 79 // or nullptr otherwise. Caller takes ownership. | 45 virtual void RegisterService( |
| 80 // Once this method is called, this object is bound to the thread on which the | 46 const Identity& identity, |
| 81 // call took place. To pass to another thread, call Clone() and pass the | 47 mojom::ServicePtr service, |
| 82 // result. | 48 mojom::PIDReceiverRequest pid_receiver_request) = 0; |
| 49 |
| 50 // Requests a new connection to a service. Returns a pointer to the |
| 51 // connection if the connection is permitted by that service, nullptr |
| 52 // otherwise. Once this method is called, this object is bound to the thread |
| 53 // on which the call took place. To pass to another thread, call Clone() and |
| 54 // pass the result. |
| 83 virtual std::unique_ptr<Connection> Connect(const std::string& name) = 0; | 55 virtual std::unique_ptr<Connection> Connect(const std::string& name) = 0; |
| 84 virtual std::unique_ptr<Connection> Connect(ConnectParams* params) = 0; | 56 virtual std::unique_ptr<Connection> Connect(const Identity& target) = 0; |
| 85 | 57 |
| 86 // Connect to application identified by |request->name| and connect to the | 58 // Connect to |target| & request to bind |Interface|. Does not retain a |
| 87 // service implementation of the interface identified by |Interface|. | 59 // connection to |target|. |
| 88 template <typename Interface> | 60 template <typename Interface> |
| 89 void ConnectToInterface(ConnectParams* params, | 61 void ConnectToInterface(const Identity& target, |
| 90 mojo::InterfacePtr<Interface>* ptr) { | 62 mojo::InterfacePtr<Interface>* ptr) { |
| 91 std::unique_ptr<Connection> connection = Connect(params); | 63 std::unique_ptr<Connection> connection = Connect(target); |
| 92 if (connection) | 64 if (connection) |
| 93 connection->GetInterface(ptr); | 65 connection->GetInterface(ptr); |
| 94 } | 66 } |
| 95 template <typename Interface> | 67 template <typename Interface> |
| 96 void ConnectToInterface(const Identity& target, | |
| 97 mojo::InterfacePtr<Interface>* ptr) { | |
| 98 ConnectParams params(target); | |
| 99 return ConnectToInterface(¶ms, ptr); | |
| 100 } | |
| 101 template <typename Interface> | |
| 102 void ConnectToInterface(const std::string& name, | 68 void ConnectToInterface(const std::string& name, |
| 103 mojo::InterfacePtr<Interface>* ptr) { | 69 mojo::InterfacePtr<Interface>* ptr) { |
| 104 ConnectParams params(name); | 70 return ConnectToInterface(Identity(name, mojom::kInheritUserID), ptr); |
| 105 return ConnectToInterface(¶ms, ptr); | |
| 106 } | 71 } |
| 107 | 72 |
| 108 // Creates a new instance of this class which may be passed to another thread. | 73 // Creates a new instance of this class which may be passed to another thread. |
| 109 // The returned object may be passed multiple times until Connect() is called, | 74 // The returned object may be passed multiple times until Connect() is called, |
| 110 // at which point this method must be called again to pass again. | 75 // at which point this method must be called again to pass again. |
| 111 virtual std::unique_ptr<Connector> Clone() = 0; | 76 virtual std::unique_ptr<Connector> Clone() = 0; |
| 112 | 77 |
| 113 // Binds a Connector request to the other end of this Connector. | 78 // Binds a Connector request to the other end of this Connector. |
| 114 virtual void BindRequest(mojom::ConnectorRequest request) = 0; | 79 virtual void BindRequest(mojom::ConnectorRequest request) = 0; |
| 115 }; | 80 }; |
| 116 | 81 |
| 117 } // namespace service_manager | 82 } // namespace service_manager |
| 118 | 83 |
| 119 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_ | 84 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_ |
| OLD | NEW |