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