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