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