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