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_SHELL_PUBLIC_CPP_CONNECTION_H_ | 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_CONNECTION_H_ |
6 #define SERVICES_SHELL_PUBLIC_CPP_CONNECTION_H_ | 6 #define SERVICES_SHELL_PUBLIC_CPP_CONNECTION_H_ |
7 | 7 |
8 #include <stdint.h> | |
9 | |
10 #include <string> | |
11 #include <utility> | |
12 | |
13 #include "base/callback_forward.h" | |
14 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
15 #include "services/shell/public/cpp/connect.h" | |
16 #include "services/shell/public/cpp/identity.h" | 9 #include "services/shell/public/cpp/identity.h" |
17 #include "services/shell/public/cpp/interface_provider.h" | 10 #include "services/shell/public/cpp/interface_provider.h" |
18 #include "services/shell/public/cpp/interface_registry.h" | |
19 #include "services/shell/public/interfaces/connector.mojom.h" | |
20 #include "services/shell/public/interfaces/interface_provider.mojom.h" | |
21 | 11 |
22 namespace shell { | 12 namespace shell { |
23 | 13 |
24 class InterfaceBinder; | |
25 class InterfaceProvider; | 14 class InterfaceProvider; |
26 | 15 |
27 // Represents a connection to another application. An instance of this class is | 16 // Represents a connection to another application. An implementation of this |
28 // returned from Shell's ConnectToApplication(), and passed to Service's | 17 // interface is returned from Connector::Connect(). |
29 // OnConnect() each time an incoming connection is received. | |
30 // | |
31 // Call AddService<T>(factory) to expose an interface to the remote application, | |
32 // and GetInterface(&interface_ptr) to consume an interface exposed by the | |
33 // remote application. | |
34 // | |
35 // Internally, this class wraps an InterfaceRegistry that accepts interfaces | |
36 // that may be exposed to a remote application. See documentation in | |
37 // interface_registry.h for more information. | |
38 // | |
39 // A Connection returned via Shell::ConnectToApplication() is owned by the | |
40 // caller. A Connection received via OnConnect is owned by the ServiceContext. | |
41 // To close a connection, call CloseConnection which will destroy this object. | |
42 class Connection { | 18 class Connection { |
43 public: | 19 public: |
44 virtual ~Connection() {} | 20 virtual ~Connection() {} |
45 | 21 |
46 enum class State { | 22 enum class State { |
47 // The shell has not yet processed the connection. | 23 // The shell has not yet processed the connection. |
48 PENDING, | 24 PENDING, |
49 | 25 |
50 // The shell processed the connection and it was established. GetResult() | 26 // The shell processed the connection and it was established. GetResult() |
51 // returns mojom::ConnectionResult::SUCCESS. | 27 // returns mojom::ConnectionResult::SUCCESS. |
52 CONNECTED, | 28 CONNECTED, |
53 | 29 |
54 // The shell processed the connection and establishment was prevented by | 30 // The shell processed the connection and establishment was prevented by |
55 // an error, call GetResult(). | 31 // an error, call GetResult(). |
56 DISCONNECTED | 32 DISCONNECTED |
57 }; | 33 }; |
58 | 34 |
59 class TestApi { | 35 class TestApi { |
60 public: | 36 public: |
61 explicit TestApi(Connection* connection) : connection_(connection) {} | 37 explicit TestApi(Connection* connection) : connection_(connection) {} |
62 base::WeakPtr<Connection> GetWeakPtr() { | 38 base::WeakPtr<Connection> GetWeakPtr() { |
63 return connection_->GetWeakPtr(); | 39 return connection_->GetWeakPtr(); |
64 } | 40 } |
65 | 41 |
66 private: | 42 private: |
67 Connection* connection_; | 43 Connection* connection_; |
68 }; | 44 }; |
69 | 45 |
70 // Allow the remote application to request instances of Interface. | |
71 // |factory| will create implementations of Interface on demand. | |
72 // Returns true if the interface was exposed, false if capability filtering | |
73 // from the shell prevented the interface from being exposed. | |
74 template <typename Interface> | |
75 bool AddInterface(InterfaceFactory<Interface>* factory) { | |
76 return GetInterfaceRegistry()->AddInterface<Interface>(factory); | |
77 } | |
78 template <typename Interface> | |
79 bool AddInterface( | |
80 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& callback, | |
81 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | |
82 return GetInterfaceRegistry()->AddInterface<Interface>( | |
83 callback, task_runner); | |
84 } | |
85 | |
86 // Binds |ptr| to an implementation of Interface in the remote application. | 46 // Binds |ptr| to an implementation of Interface in the remote application. |
87 // |ptr| can immediately be used to start sending requests to the remote | 47 // |ptr| can immediately be used to start sending requests to the remote |
88 // interface. | 48 // interface. |
89 template <typename Interface> | 49 template <typename Interface> |
90 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { | 50 void GetInterface(mojo::InterfacePtr<Interface>* ptr) { |
91 GetRemoteInterfaces()->GetInterface(ptr); | 51 GetRemoteInterfaces()->GetInterface(ptr); |
92 } | 52 } |
93 template <typename Interface> | 53 template <typename Interface> |
94 void GetInterface(mojo::InterfaceRequest<Interface> request) { | 54 void GetInterface(mojo::InterfaceRequest<Interface> request) { |
95 GetRemoteInterfaces()->GetInterface(std::move(request)); | 55 GetRemoteInterfaces()->GetInterface(std::move(request)); |
96 } | 56 } |
97 | 57 |
98 // Returns true if the remote application has the specified capability class | |
99 // specified in its manifest. Only valid for inbound connections. Will return | |
100 // false for outbound connections. | |
101 virtual bool HasCapabilityClass(const std::string& class_name) const = 0; | |
102 | |
103 // Returns the remote identity. While the connection is in the pending state, | 58 // Returns the remote identity. While the connection is in the pending state, |
104 // the user_id() field will be the value passed via Connect(). After the | 59 // the user_id() field will be the value passed via Connect(). After the |
105 // connection is completed, it will change to the value assigned by the shell. | 60 // connection is completed, it will change to the value assigned by the shell. |
106 // Call AddConnectionCompletedClosure() to schedule a closure to be run when | 61 // Call AddConnectionCompletedClosure() to schedule a closure to be run when |
107 // the resolved user id is available. | 62 // the resolved user id is available. |
108 virtual const Identity& GetRemoteIdentity() const = 0; | 63 virtual const Identity& GetRemoteIdentity() const = 0; |
109 | 64 |
110 // Register a handler to receive an error notification on the pipe to the | 65 // Register a handler to receive an error notification on the pipe to the |
111 // remote application's InterfaceProvider. | 66 // remote application's InterfaceProvider. |
112 virtual void SetConnectionLostClosure(const base::Closure& handler) = 0; | 67 virtual void SetConnectionLostClosure(const base::Closure& handler) = 0; |
113 | 68 |
114 // Returns the result of the connection. This function should only be called | 69 // Returns the result of the connection. This function should only be called |
115 // when the connection state is not pending. Call | 70 // when the connection state is not pending. Call |
116 // AddConnectionCompletedClosure() to schedule a closure to be run when the | 71 // AddConnectionCompletedClosure() to schedule a closure to be run when the |
117 // connection is processed by the shell. | 72 // connection is processed by the shell. |
118 virtual mojom::ConnectResult GetResult() const = 0; | 73 virtual mojom::ConnectResult GetResult() const = 0; |
119 | 74 |
120 // Returns true if the connection has not yet been processed by the shell. | 75 // Returns true if the connection has not yet been processed by the shell. |
121 virtual bool IsPending() const = 0; | 76 virtual bool IsPending() const = 0; |
122 | 77 |
123 // Register a closure to be run when the connection has been completed by the | 78 // Register a closure to be run when the connection has been completed by the |
124 // shell and remote metadata is available. Useful only for connections created | 79 // shell and remote metadata is available. Useful only for connections created |
125 // via Connector::Connect(). Once the connection is complete, metadata is | 80 // via Connector::Connect(). Once the connection is complete, metadata is |
126 // available immediately. | 81 // available immediately. |
127 virtual void AddConnectionCompletedClosure(const base::Closure& callback) = 0; | 82 virtual void AddConnectionCompletedClosure(const base::Closure& callback) = 0; |
128 | 83 |
129 // Returns true if the Shell allows |interface_name| to be exposed to the | |
130 // remote application. | |
131 virtual bool AllowsInterface(const std::string& interface_name) const = 0; | |
132 | |
133 // Returns the InterfaceRegistry that implements the mojom::InterfaceProvider | |
134 // exposed to the remote application. | |
135 virtual InterfaceRegistry* GetInterfaceRegistry() = 0; | |
136 | |
137 // Returns an object encapsulating a remote InterfaceProvider. | 84 // Returns an object encapsulating a remote InterfaceProvider. |
138 virtual InterfaceProvider* GetRemoteInterfaces() = 0; | 85 virtual InterfaceProvider* GetRemoteInterfaces() = 0; |
139 | 86 |
140 protected: | 87 protected: |
141 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; | 88 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; |
142 }; | 89 }; |
143 | 90 |
144 } // namespace shell | 91 } // namespace shell |
145 | 92 |
146 #endif // SERVICES_SHELL_PUBLIC_CPP_CONNECTION_H_ | 93 #endif // SERVICES_SHELL_PUBLIC_CPP_CONNECTION_H_ |
OLD | NEW |