OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_CONNECTION_H_ | 5 #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTION_H_ |
6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTION_H_ | 6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTION_H_ |
7 | 7 |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "services/service_manager/public/cpp/identity.h" | 9 #include "services/service_manager/public/cpp/identity.h" |
10 #include "services/service_manager/public/cpp/interface_provider.h" | 10 #include "services/service_manager/public/cpp/interface_provider.h" |
11 #include "services/service_manager/public/interfaces/connector.mojom.h" | 11 #include "services/service_manager/public/interfaces/connector.mojom.h" |
12 | 12 |
13 namespace service_manager { | 13 namespace service_manager { |
14 | 14 |
15 class InterfaceProvider; | 15 class InterfaceProvider; |
16 | 16 |
17 // Represents a connection to another application. An implementation of this | 17 // Represents a connection to another application. An implementation of this |
18 // interface is returned from Connector::Connect(). | 18 // interface is returned from Connector::Connect(). |
19 class Connection { | 19 class Connection { |
20 public: | 20 public: |
21 virtual ~Connection() {} | 21 virtual ~Connection() {} |
22 | 22 |
23 enum class State { | 23 enum class State { |
24 // The shell has not yet processed the connection. | 24 // The service manager has not yet processed the connection. |
25 PENDING, | 25 PENDING, |
26 | 26 |
27 // The shell processed the connection and it was established. GetResult() | 27 // The service manager processed the connection and it was established. |
28 // returns mojom::ConnectionResult::SUCCESS. | 28 // GetResult() returns mojom::ConnectionResult::SUCCESS. |
29 CONNECTED, | 29 CONNECTED, |
30 | 30 |
31 // The shell processed the connection and establishment was prevented by | 31 // The service manager processed the connection and establishment was |
32 // an error, call GetResult(). | 32 // prevented by an error, call GetResult(). |
33 DISCONNECTED | 33 DISCONNECTED |
34 }; | 34 }; |
35 | 35 |
36 class TestApi { | 36 class TestApi { |
37 public: | 37 public: |
38 explicit TestApi(Connection* connection) : connection_(connection) {} | 38 explicit TestApi(Connection* connection) : connection_(connection) {} |
39 base::WeakPtr<Connection> GetWeakPtr() { | 39 base::WeakPtr<Connection> GetWeakPtr() { |
40 return connection_->GetWeakPtr(); | 40 return connection_->GetWeakPtr(); |
41 } | 41 } |
42 | 42 |
43 private: | 43 private: |
44 Connection* connection_; | 44 Connection* connection_; |
45 }; | 45 }; |
46 | 46 |
47 // Binds |ptr| to an implementation of Interface in the remote application. | 47 // Binds |ptr| to an implementation of Interface in the remote application. |
48 // |ptr| can immediately be used to start sending requests to the remote | 48 // |ptr| can immediately be used to start sending requests to the remote |
49 // interface. | 49 // interface. |
50 template <typename Interface> | 50 template <typename Interface> |
51 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { | 51 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { |
52 GetRemoteInterfaces()->GetInterface(ptr); | 52 GetRemoteInterfaces()->GetInterface(ptr); |
53 } | 53 } |
54 template <typename Interface> | 54 template <typename Interface> |
55 void GetInterface(mojo::InterfaceRequest<Interface> request) { | 55 void GetInterface(mojo::InterfaceRequest<Interface> request) { |
56 GetRemoteInterfaces()->GetInterface(std::move(request)); | 56 GetRemoteInterfaces()->GetInterface(std::move(request)); |
57 } | 57 } |
58 | 58 |
59 // Returns the remote identity. While the connection is in the pending state, | 59 // Returns the remote identity. While the connection is in the pending state, |
60 // the user_id() field will be the value passed via Connect(). After the | 60 // the user_id() field will be the value passed via Connect(). After the |
61 // connection is completed, it will change to the value assigned by the shell. | 61 // connection is completed, it will change to the value assigned by the |
62 // Call AddConnectionCompletedClosure() to schedule a closure to be run when | 62 // service manager. Call AddConnectionCompletedClosure() to schedule a closure |
63 // the resolved user id is available. | 63 // to be run when the resolved user id is available. |
64 virtual const Identity& GetRemoteIdentity() const = 0; | 64 virtual const Identity& GetRemoteIdentity() const = 0; |
65 | 65 |
66 // Register a handler to receive an error notification on the pipe to the | 66 // Register a handler to receive an error notification on the pipe to the |
67 // remote application's InterfaceProvider. | 67 // remote application's InterfaceProvider. |
68 virtual void SetConnectionLostClosure(const base::Closure& handler) = 0; | 68 virtual void SetConnectionLostClosure(const base::Closure& handler) = 0; |
69 | 69 |
70 // Returns the result of the connection. This function should only be called | 70 // Returns the result of the connection. This function should only be called |
71 // when the connection state is not pending. Call | 71 // when the connection state is not pending. Call |
72 // AddConnectionCompletedClosure() to schedule a closure to be run when the | 72 // AddConnectionCompletedClosure() to schedule a closure to be run when the |
73 // connection is processed by the shell. | 73 // connection is processed by the service manager. |
74 virtual mojom::ConnectResult GetResult() const = 0; | 74 virtual mojom::ConnectResult GetResult() const = 0; |
75 | 75 |
76 // Returns true if the connection has not yet been processed by the shell. | 76 // Returns true if the connection has not yet been processed by the service |
| 77 // manager. |
77 virtual bool IsPending() const = 0; | 78 virtual bool IsPending() const = 0; |
78 | 79 |
79 // Register a closure to be run when the connection has been completed by the | 80 // Register a closure to be run when the connection has been completed by the |
80 // shell and remote metadata is available. Useful only for connections created | 81 // service manager and remote metadata is available. Useful only for |
| 82 // connections created |
81 // via Connector::Connect(). Once the connection is complete, metadata is | 83 // via Connector::Connect(). Once the connection is complete, metadata is |
82 // available immediately. | 84 // available immediately. |
83 virtual void AddConnectionCompletedClosure(const base::Closure& callback) = 0; | 85 virtual void AddConnectionCompletedClosure(const base::Closure& callback) = 0; |
84 | 86 |
85 // Returns an object encapsulating a remote InterfaceProvider. | 87 // Returns an object encapsulating a remote InterfaceProvider. |
86 virtual InterfaceProvider* GetRemoteInterfaces() = 0; | 88 virtual InterfaceProvider* GetRemoteInterfaces() = 0; |
87 | 89 |
88 protected: | 90 protected: |
89 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; | 91 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; |
90 }; | 92 }; |
91 | 93 |
92 } // namespace service_manager | 94 } // namespace service_manager |
93 | 95 |
94 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTION_H_ | 96 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTION_H_ |
OLD | NEW |