| 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_CONNECTION_H_ | 5 #ifndef MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ |
| 6 #define MOJO_SHELL_PUBLIC_CPP_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_binder.h" | 14 #include "mojo/shell/public/cpp/connect.h" |
| 15 #include "mojo/shell/public/cpp/interface_registry.h" |
| 15 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" | 16 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" |
| 16 | 17 |
| 17 namespace mojo { | 18 namespace mojo { |
| 18 | 19 |
| 19 class InterfaceBinder; | 20 class InterfaceBinder; |
| 20 | 21 |
| 21 // Represents a connection to another application. An instance of this class is | 22 // Represents a connection to another application. An instance of this class is |
| 22 // returned from Shell's ConnectToApplication(), and passed to ShellClient's | 23 // returned from Shell's ConnectToApplication(), and passed to ShellClient's |
| 23 // AcceptConnection() each time an incoming connection is received. | 24 // AcceptConnection() each time an incoming connection is received. |
| 24 // | 25 // |
| 25 // To use, define a class that implements your specific interface. Then | 26 // Call AddService<T>(factory) to expose an interface to the remote application, |
| 26 // implement an InterfaceFactory<Foo> that binds instances of FooImpl to | 27 // and GetInterface(&interface_ptr) to consume an interface exposed by the |
| 27 // InterfaceRequest<Foo>s and register that on the connection like this: | 28 // remote application. |
| 28 // | 29 // |
| 29 // connection->AddInterface(&factory); | 30 // Internally, this class wraps an InterfaceRegistry that accepts interfaces |
| 30 // | 31 // that may be exposed to a remote application. See documentation in |
| 31 // Or, if you have multiple factories implemented by the same type, explicitly | 32 // interface_registry.h for more information. |
| 32 // specify the interface to register the factory for: | |
| 33 // | |
| 34 // connection->AddInterface<Foo>(&my_foo_and_bar_factory_); | |
| 35 // connection->AddInterface<Bar>(&my_foo_and_bar_factory_); | |
| 36 // | |
| 37 // The InterfaceFactory must outlive the Connection. | |
| 38 // | |
| 39 // Additionally you may specify a default InterfaceBinder to handle requests for | |
| 40 // interfaces unhandled by any registered InterfaceFactory. Just as with | |
| 41 // InterfaceFactory, the default InterfaceBinder supplied must outlive | |
| 42 // Connection. | |
| 43 // | 33 // |
| 44 // A Connection returned via Shell::ConnectToApplication() is owned by the | 34 // A Connection returned via Shell::ConnectToApplication() is owned by the |
| 45 // caller. | 35 // caller. |
| 46 // An Connection received via AcceptConnection is owned by the ShellConnection. | 36 // An Connection received via AcceptConnection is owned by the ShellConnection. |
| 47 // To close a connection, call CloseConnection which will destroy this object. | 37 // To close a connection, call CloseConnection which will destroy this object. |
| 48 class Connection { | 38 class Connection { |
| 49 public: | 39 public: |
| 50 virtual ~Connection() {} | 40 virtual ~Connection() {} |
| 51 | 41 |
| 52 class TestApi { | 42 class TestApi { |
| 53 public: | 43 public: |
| 54 explicit TestApi(Connection* connection) : connection_(connection) {} | 44 explicit TestApi(Connection* connection) : connection_(connection) {} |
| 55 base::WeakPtr<Connection> GetWeakPtr() { | 45 base::WeakPtr<Connection> GetWeakPtr() { |
| 56 return connection_->GetWeakPtr(); | 46 return connection_->GetWeakPtr(); |
| 57 } | 47 } |
| 58 | 48 |
| 59 private: | 49 private: |
| 60 Connection* connection_; | 50 Connection* connection_; |
| 61 }; | 51 }; |
| 62 | 52 |
| 63 // See class description for details. | |
| 64 virtual void SetDefaultInterfaceBinder(InterfaceBinder* binder) = 0; | |
| 65 | |
| 66 // Allow the remote application to request instances of Interface. | 53 // Allow the remote application to request instances of Interface. |
| 67 // |factory| will create implementations of Interface on demand. | 54 // |factory| will create implementations of Interface on demand. |
| 68 // Returns true if the interface was exposed, false if capability filtering | 55 // Returns true if the interface was exposed, false if capability filtering |
| 69 // from the shell prevented the interface from being exposed. | 56 // from the shell prevented the interface from being exposed. |
| 70 template <typename Interface> | 57 template <typename Interface> |
| 71 bool AddInterface(InterfaceFactory<Interface>* factory) { | 58 bool AddInterface(InterfaceFactory<Interface>* factory) { |
| 72 return SetInterfaceBinderForName( | 59 return GetLocalRegistry()->AddInterface<Interface>(factory); |
| 73 new internal::InterfaceFactoryBinder<Interface>(factory), | |
| 74 Interface::Name_); | |
| 75 } | 60 } |
| 76 | 61 |
| 77 // Binds |ptr| to an implemention of Interface in the remote application. | 62 // Binds |ptr| to an implemention of Interface in the remote application. |
| 78 // |ptr| can immediately be used to start sending requests to the remote | 63 // |ptr| can immediately be used to start sending requests to the remote |
| 79 // interface. | 64 // interface. |
| 80 template <typename Interface> | 65 template <typename Interface> |
| 81 void GetInterface(InterfacePtr<Interface>* ptr) { | 66 void GetInterface(InterfacePtr<Interface>* ptr) { |
| 82 if (shell::mojom::InterfaceProvider* ip = GetRemoteInterfaces()) { | 67 mojo::GetInterface(GetRemoteInterfaces(), ptr); |
| 83 MessagePipe pipe; | |
| 84 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); | |
| 85 ip->GetInterface(Interface::Name_, std::move(pipe.handle1)); | |
| 86 } | |
| 87 } | 68 } |
| 88 | 69 |
| 89 // Returns the URL that was used by the source application to establish a | 70 // Returns the URL that was used by the source application to establish a |
| 90 // connection to the destination application. | 71 // connection to the destination application. |
| 91 // | 72 // |
| 92 // When Connection is representing an incoming connection this can be | 73 // When Connection is representing an incoming connection this can be |
| 93 // different than the URL the application was initially loaded from, if the | 74 // different than the URL the application was initially loaded from, if the |
| 94 // application handles multiple URLs. Note that this is the URL after all | 75 // application handles multiple URLs. Note that this is the URL after all |
| 95 // URL rewriting and HTTP redirects have been performed. | 76 // URL rewriting and HTTP redirects have been performed. |
| 96 // | 77 // |
| 97 // When Connection is representing and outgoing connection, this will be the | 78 // When Connection is representing and outgoing connection, this will be the |
| 98 // same as the value returned by GetRemoveApplicationURL(). | 79 // same as the value returned by GetRemoveApplicationURL(). |
| 99 virtual const std::string& GetConnectionURL() = 0; | 80 virtual const std::string& GetConnectionURL() = 0; |
| 100 | 81 |
| 101 // Returns the URL identifying the remote application on this connection. | 82 // Returns the URL identifying the remote application on this connection. |
| 102 virtual const std::string& GetRemoteApplicationURL() = 0; | 83 virtual const std::string& GetRemoteApplicationURL() = 0; |
| 103 | 84 |
| 104 // Returns the raw proxy to the remote application's InterfaceProvider | |
| 105 // interface. Most applications will just use GetInterface() instead. | |
| 106 // Caller does not take ownership. | |
| 107 virtual shell::mojom::InterfaceProvider* GetRemoteInterfaces() = 0; | |
| 108 | |
| 109 // Returns the local application's InterfaceProvider interface. The return | |
| 110 // value is owned by this connection. | |
| 111 virtual shell::mojom::InterfaceProvider* GetLocalInterfaces() = 0; | |
| 112 | |
| 113 // Register a handler to receive an error notification on the pipe to the | 85 // Register a handler to receive an error notification on the pipe to the |
| 114 // remote application's InterfaceProvider. | 86 // remote application's InterfaceProvider. |
| 115 virtual void SetRemoteInterfaceProviderConnectionErrorHandler( | 87 virtual void SetRemoteInterfaceProviderConnectionErrorHandler( |
| 116 const Closure& handler) = 0; | 88 const Closure& handler) = 0; |
| 117 | 89 |
| 118 // Returns the id of the remote application. For Connections created via | 90 // Returns the id of the remote application. For Connections created via |
| 119 // Shell::Connect(), this will not be determined until Connect()'s callback is | 91 // Shell::Connect(), this will not be determined until Connect()'s callback is |
| 120 // run, and this function will return false. Use AddRemoteIDCallback() to | 92 // run, and this function will return false. Use AddRemoteIDCallback() to |
| 121 // schedule a callback to be run when the remote application id is available. | 93 // schedule a callback to be run when the remote application id is available. |
| 122 // A value of Shell::kInvalidApplicationID indicates the connection has not | 94 // A value of Shell::kInvalidApplicationID indicates the connection has not |
| 123 // been established. | 95 // been established. |
| 124 virtual bool GetRemoteApplicationID(uint32_t* remote_id) const = 0; | 96 virtual bool GetRemoteApplicationID(uint32_t* remote_id) const = 0; |
| 125 | 97 |
| 126 // Returns the id of the deepest content handler used in connecting to | 98 // Returns the id of the deepest content handler used in connecting to |
| 127 // the application. See GetRemoteApplicationID() for details about the return | 99 // the application. See GetRemoteApplicationID() for details about the return |
| 128 // value. A |content_handler_id| value of Shell::kInvalidApplicationID | 100 // value. A |content_handler_id| value of Shell::kInvalidApplicationID |
| 129 // indicates no content handler was used in connecting to the application. | 101 // indicates no content handler was used in connecting to the application. |
| 130 virtual bool GetRemoteContentHandlerID( | 102 virtual bool GetRemoteContentHandlerID( |
| 131 uint32_t* content_handler_id) const = 0; | 103 uint32_t* content_handler_id) const = 0; |
| 132 | 104 |
| 133 // See description in GetRemoteApplicationID()/GetRemoteContentHandlerID(). If | 105 // See description in GetRemoteApplicationID()/GetRemoteContentHandlerID(). If |
| 134 // the ids are available, |callback| is run immediately. | 106 // the ids are available, |callback| is run immediately. |
| 135 virtual void AddRemoteIDCallback(const Closure& callback) = 0; | 107 virtual void AddRemoteIDCallback(const Closure& callback) = 0; |
| 136 | 108 |
| 109 // Returns true if the Shell allows |interface_name| to be exposed to the |
| 110 // remote application. |
| 111 virtual bool AllowsInterface(const std::string& interface_name) const = 0; |
| 112 |
| 113 // Returns the raw proxy to the remote application's InterfaceProvider |
| 114 // interface. Most applications will just use GetInterface() instead. |
| 115 // Caller does not take ownership. |
| 116 virtual shell::mojom::InterfaceProvider* GetRemoteInterfaces() = 0; |
| 117 |
| 137 protected: | 118 protected: |
| 138 // Returns true if the binder was set, false if it was not set (e.g. by | 119 virtual InterfaceRegistry* GetLocalRegistry() = 0; |
| 139 // some filtering policy preventing this interface from being exposed). | |
| 140 virtual bool SetInterfaceBinderForName(InterfaceBinder* binder, | |
| 141 const std::string& name) = 0; | |
| 142 | 120 |
| 143 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; | 121 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; |
| 144 }; | 122 }; |
| 145 | 123 |
| 146 } // namespace mojo | 124 } // namespace mojo |
| 147 | 125 |
| 148 #endif // MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ | 126 #endif // MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ |
| OLD | NEW |