| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_CONNECTOR_H_ | |
| 6 #define SERVICES_SHELL_PUBLIC_CPP_CONNECTOR_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "services/shell/public/cpp/connection.h" | |
| 11 #include "services/shell/public/cpp/identity.h" | |
| 12 #include "services/shell/public/interfaces/connector.mojom.h" | |
| 13 #include "services/shell/public/interfaces/service.mojom.h" | |
| 14 #include "services/shell/public/interfaces/service_manager.mojom.h" | |
| 15 | |
| 16 namespace shell { | |
| 17 | |
| 18 // An interface that encapsulates the Mojo Shell's broker interface by which | |
| 19 // connections between applications are established. Once Connect() is called, | |
| 20 // this class is bound to the thread the call was made on and it cannot be | |
| 21 // passed to another thread without calling Clone(). | |
| 22 // An instance of this class is created internally by ServiceContext for use | |
| 23 // on the thread ServiceContext is instantiated on, and this interface is | |
| 24 // wrapped by the Shell interface. | |
| 25 // To use this interface on other threads, call Shell::CloneConnector() and | |
| 26 // pass the result to another thread. To pass to subsequent threads, call | |
| 27 // Clone() on instances of this object. | |
| 28 // While instances of this object are owned by the caller, the underlying | |
| 29 // connection with the shell is bound to the lifetime of the instance that | |
| 30 // created it, i.e. when the application is terminated the Connector pipe is | |
| 31 // closed. | |
| 32 class Connector { | |
| 33 public: | |
| 34 virtual ~Connector() {} | |
| 35 | |
| 36 class ConnectParams { | |
| 37 public: | |
| 38 explicit ConnectParams(const Identity& target); | |
| 39 explicit ConnectParams(const std::string& name); | |
| 40 ~ConnectParams(); | |
| 41 | |
| 42 const Identity& target() { return target_; } | |
| 43 void set_target(const Identity& target) { target_ = target; } | |
| 44 void set_client_process_connection( | |
| 45 mojom::ServicePtr service, | |
| 46 mojom::PIDReceiverRequest pid_receiver_request) { | |
| 47 service_ = std::move(service); | |
| 48 pid_receiver_request_ = std::move(pid_receiver_request); | |
| 49 } | |
| 50 void TakeClientProcessConnection( | |
| 51 mojom::ServicePtr* service, | |
| 52 mojom::PIDReceiverRequest* pid_receiver_request) { | |
| 53 *service = std::move(service_); | |
| 54 *pid_receiver_request = std::move(pid_receiver_request_); | |
| 55 } | |
| 56 InterfaceProvider* remote_interfaces() { return remote_interfaces_; } | |
| 57 void set_remote_interfaces(InterfaceProvider* remote_interfaces) { | |
| 58 remote_interfaces_ = remote_interfaces; | |
| 59 } | |
| 60 | |
| 61 private: | |
| 62 Identity target_; | |
| 63 mojom::ServicePtr service_; | |
| 64 mojom::PIDReceiverRequest pid_receiver_request_; | |
| 65 InterfaceProvider* remote_interfaces_ = nullptr; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(ConnectParams); | |
| 68 }; | |
| 69 | |
| 70 // Creates a new Connector instance and fills in |*request| with a request | |
| 71 // for the other end the Connector's interface. | |
| 72 static std::unique_ptr<Connector> Create(mojom::ConnectorRequest* request); | |
| 73 | |
| 74 // Requests a new connection to an application. Returns a pointer to the | |
| 75 // connection if the connection is permitted by this application's delegate, | |
| 76 // or nullptr otherwise. Caller takes ownership. | |
| 77 // Once this method is called, this object is bound to the thread on which the | |
| 78 // call took place. To pass to another thread, call Clone() and pass the | |
| 79 // result. | |
| 80 virtual std::unique_ptr<Connection> Connect(const std::string& name) = 0; | |
| 81 virtual std::unique_ptr<Connection> Connect(ConnectParams* params) = 0; | |
| 82 | |
| 83 // Connect to application identified by |request->name| and connect to the | |
| 84 // service implementation of the interface identified by |Interface|. | |
| 85 template <typename Interface> | |
| 86 void ConnectToInterface(ConnectParams* params, | |
| 87 mojo::InterfacePtr<Interface>* ptr) { | |
| 88 std::unique_ptr<Connection> connection = Connect(params); | |
| 89 if (connection) | |
| 90 connection->GetInterface(ptr); | |
| 91 } | |
| 92 template <typename Interface> | |
| 93 void ConnectToInterface(const Identity& target, | |
| 94 mojo::InterfacePtr<Interface>* ptr) { | |
| 95 ConnectParams params(target); | |
| 96 return ConnectToInterface(¶ms, ptr); | |
| 97 } | |
| 98 template <typename Interface> | |
| 99 void ConnectToInterface(const std::string& name, | |
| 100 mojo::InterfacePtr<Interface>* ptr) { | |
| 101 ConnectParams params(name); | |
| 102 return ConnectToInterface(¶ms, ptr); | |
| 103 } | |
| 104 | |
| 105 // Creates a new instance of this class which may be passed to another thread. | |
| 106 // The returned object may be passed multiple times until Connect() is called, | |
| 107 // at which point this method must be called again to pass again. | |
| 108 virtual std::unique_ptr<Connector> Clone() = 0; | |
| 109 }; | |
| 110 | |
| 111 } // namespace shell | |
| 112 | |
| 113 #endif // SERVICES_SHELL_PUBLIC_CPP_CONNECTOR_H_ | |
| OLD | NEW |