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_APPLICATION_PUBLIC_CPP_APPLICATION_CONNECTION_H_ | |
6 #define MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_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/application/public/cpp/lib/interface_factory_connector.h" | |
15 #include "mojo/application/public/interfaces/service_provider.mojom.h" | |
16 | |
17 namespace mojo { | |
18 | |
19 class ServiceConnector; | |
20 | |
21 // Represents a connection to another application. An instance of this class is | |
22 // passed to ApplicationDelegate's ConfigureIncomingConnection() method each | |
23 // time a connection is made to this app, and to ApplicationDelegate's | |
24 // ConfigureOutgoingConnection() method when the app connects to another. | |
25 // | |
26 // To use, define a class that implements your specific service API (e.g., | |
27 // FooImpl to implement a service named Foo). Then implement an | |
28 // InterfaceFactory<Foo> that binds instances of FooImpl to | |
29 // InterfaceRequest<Foo>s and register that on the connection like this: | |
30 // | |
31 // connection->AddService(&factory); | |
32 // | |
33 // Or, if you have multiple factories implemented by the same type, explicitly | |
34 // specify the interface to register the factory for: | |
35 // | |
36 // connection->AddService<Foo>(&my_foo_and_bar_factory_); | |
37 // connection->AddService<Bar>(&my_foo_and_bar_factory_); | |
38 // | |
39 // The InterfaceFactory must outlive the ApplicationConnection. | |
40 // | |
41 // Additionally you specify a ServiceConnector. If a ServiceConnector has | |
42 // been set and an InterfaceFactory has not been registered for the interface | |
43 // request, than the interface request is sent to the ServiceConnector. | |
44 // | |
45 // Just as with InterfaceFactory, ServiceConnector must outlive | |
46 // ApplicationConnection. | |
47 // | |
48 // An ApplicationConnection's lifetime is managed by an ApplicationImpl. To | |
49 // close a connection, call CloseConnection which will destroy this object. | |
50 class ApplicationConnection { | |
51 public: | |
52 virtual ~ApplicationConnection() {} | |
53 | |
54 class TestApi { | |
55 public: | |
56 explicit TestApi(ApplicationConnection* connection) | |
57 : connection_(connection) { | |
58 } | |
59 base::WeakPtr<ApplicationConnection> GetWeakPtr() { | |
60 return connection_->GetWeakPtr(); | |
61 } | |
62 | |
63 private: | |
64 ApplicationConnection* connection_; | |
65 }; | |
66 | |
67 // See class description for details. | |
68 virtual void SetServiceConnector(ServiceConnector* connector) = 0; | |
69 | |
70 // Makes Interface available as a service to the remote application. | |
71 // |factory| will create implementations of Interface on demand. | |
72 // Returns true if the service was exposed, false if capability filtering | |
73 // from the shell prevented the service from being exposed. | |
74 template <typename Interface> | |
75 bool AddService(InterfaceFactory<Interface>* factory) { | |
76 return SetServiceConnectorForName( | |
77 new internal::InterfaceFactoryConnector<Interface>(factory), | |
78 Interface::Name_); | |
79 } | |
80 | |
81 // Binds |ptr| to an implemention of Interface in the remote application. | |
82 // |ptr| can immediately be used to start sending requests to the remote | |
83 // service. | |
84 template <typename Interface> | |
85 void ConnectToService(InterfacePtr<Interface>* ptr) { | |
86 if (ServiceProvider* sp = GetServiceProvider()) { | |
87 MessagePipe pipe; | |
88 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); | |
89 sp->ConnectToService(Interface::Name_, std::move(pipe.handle1)); | |
90 } | |
91 } | |
92 | |
93 // Returns the URL that was used by the source application to establish a | |
94 // connection to the destination application. | |
95 // | |
96 // When ApplicationConnection is representing an incoming connection this can | |
97 // be different than the URL the application was initially loaded from, if the | |
98 // application handles multiple URLs. Note that this is the URL after all | |
99 // URL rewriting and HTTP redirects have been performed. | |
100 // | |
101 // When ApplicationConnection is representing and outgoing connection, this | |
102 // will be the same as the value returned by GetRemoveApplicationURL(). | |
103 virtual const std::string& GetConnectionURL() = 0; | |
104 | |
105 // Returns the URL identifying the remote application on this connection. | |
106 virtual const std::string& GetRemoteApplicationURL() = 0; | |
107 | |
108 // Returns the raw proxy to the remote application's ServiceProvider | |
109 // interface. Most applications will just use ConnectToService() instead. | |
110 // Caller does not take ownership. | |
111 virtual ServiceProvider* GetServiceProvider() = 0; | |
112 | |
113 // Returns the local application's ServiceProvider interface. The return | |
114 // value is owned by this connection. | |
115 virtual ServiceProvider* GetLocalServiceProvider() = 0; | |
116 | |
117 // Register a handler to receive an error notification on the pipe to the | |
118 // remote application's service provider. | |
119 virtual void SetRemoteServiceProviderConnectionErrorHandler( | |
120 const Closure& handler) = 0; | |
121 | |
122 // Returns the id of the deepest content handler used in connecting to | |
123 // the application. If the content handler id has not yet been determined | |
124 // this returns false, use AddContentHandlerIDCallback() to schedule a | |
125 // callback when the content handler is has been obtained. A value of 0 | |
126 // indicates no content handler was used in connecting to the application. | |
127 virtual bool GetContentHandlerID(uint32_t* content_handler_id) = 0; | |
128 | |
129 // See description in GetTargetID(). If the id of the content handler has | |
130 // been obtained |callback| is run immediately. | |
131 virtual void AddContentHandlerIDCallback(const Closure& callback) = 0; | |
132 | |
133 protected: | |
134 // Returns true if the connector was set, false if it was not set (e.g. by | |
135 // some filtering policy preventing this interface from being exposed). | |
136 virtual bool SetServiceConnectorForName(ServiceConnector* service_connector, | |
137 const std::string& name) = 0; | |
138 | |
139 virtual base::WeakPtr<ApplicationConnection> GetWeakPtr() = 0; | |
140 }; | |
141 | |
142 } // namespace mojo | |
143 | |
144 #endif // MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_CONNECTION_H_ | |
OLD | NEW |