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/lib/interface_factory_binder.h" |
15 #include "mojo/shell/public/interfaces/service_provider.mojom.h" | 15 #include "mojo/shell/public/interfaces/interface_provider.mojom.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 | 18 |
19 class InterfaceBinder; | 19 class InterfaceBinder; |
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 ShellClient'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 interface. Then |
26 // FooImpl to implement a service named Foo). Then implement an | 26 // implement an 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: | 27 // InterfaceRequest<Foo>s and register that on the connection like this: |
29 // | 28 // |
30 // connection->AddService(&factory); | 29 // connection->AddInterface(&factory); |
31 // | 30 // |
32 // Or, if you have multiple factories implemented by the same type, explicitly | 31 // Or, if you have multiple factories implemented by the same type, explicitly |
33 // specify the interface to register the factory for: | 32 // specify the interface to register the factory for: |
34 // | 33 // |
35 // connection->AddInterface<Foo>(&my_foo_and_bar_factory_); | 34 // connection->AddInterface<Foo>(&my_foo_and_bar_factory_); |
36 // connection->AddInterface<Bar>(&my_foo_and_bar_factory_); | 35 // connection->AddInterface<Bar>(&my_foo_and_bar_factory_); |
37 // | 36 // |
38 // The InterfaceFactory must outlive the Connection. | 37 // The InterfaceFactory must outlive the Connection. |
39 // | 38 // |
40 // Additionally you specify a InterfaceBinder. If a InterfaceBinder has | 39 // Additionally you specify a InterfaceBinder. If a InterfaceBinder has |
(...skipping 15 matching lines...) Expand all Loading... |
56 return connection_->GetWeakPtr(); | 55 return connection_->GetWeakPtr(); |
57 } | 56 } |
58 | 57 |
59 private: | 58 private: |
60 Connection* connection_; | 59 Connection* connection_; |
61 }; | 60 }; |
62 | 61 |
63 // See class description for details. | 62 // See class description for details. |
64 virtual void SetDefaultInterfaceBinder(InterfaceBinder* binder) = 0; | 63 virtual void SetDefaultInterfaceBinder(InterfaceBinder* binder) = 0; |
65 | 64 |
66 // Makes Interface available as a service to the remote application. | 65 // Allow the remote application to request instances of Interface. |
67 // |factory| will create implementations of Interface on demand. | 66 // |factory| will create implementations of Interface on demand. |
68 // Returns true if the service was exposed, false if capability filtering | 67 // Returns true if the interface was exposed, false if capability filtering |
69 // from the shell prevented the service from being exposed. | 68 // from the shell prevented the interface from being exposed. |
70 template <typename Interface> | 69 template <typename Interface> |
71 bool AddInterface(InterfaceFactory<Interface>* factory) { | 70 bool AddInterface(InterfaceFactory<Interface>* factory) { |
72 return SetInterfaceBinderForName( | 71 return SetInterfaceBinderForName( |
73 new internal::InterfaceFactoryBinder<Interface>(factory), | 72 new internal::InterfaceFactoryBinder<Interface>(factory), |
74 Interface::Name_); | 73 Interface::Name_); |
75 } | 74 } |
76 | 75 |
77 // Binds |ptr| to an implemention of Interface in the remote application. | 76 // Binds |ptr| to an implemention of Interface in the remote application. |
78 // |ptr| can immediately be used to start sending requests to the remote | 77 // |ptr| can immediately be used to start sending requests to the remote |
79 // service. | 78 // interface. |
80 template <typename Interface> | 79 template <typename Interface> |
81 void GetInterface(InterfacePtr<Interface>* ptr) { | 80 void GetInterface(InterfacePtr<Interface>* ptr) { |
82 if (ServiceProvider* sp = GetRemoteInterfaces()) { | 81 if (InterfaceProvider* ip = GetRemoteInterfaces()) { |
83 MessagePipe pipe; | 82 MessagePipe pipe; |
84 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); | 83 ptr->Bind(InterfacePtrInfo<Interface>(std::move(pipe.handle0), 0u)); |
85 sp->ConnectToService(Interface::Name_, std::move(pipe.handle1)); | 84 ip->GetInterface(Interface::Name_, std::move(pipe.handle1)); |
86 } | 85 } |
87 } | 86 } |
88 | 87 |
89 // Returns the URL that was used by the source application to establish a | 88 // Returns the URL that was used by the source application to establish a |
90 // connection to the destination application. | 89 // connection to the destination application. |
91 // | 90 // |
92 // When Connection is representing an incoming connection this can be | 91 // When Connection is representing an incoming connection this can be |
93 // different than the URL the application was initially loaded from, if the | 92 // 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 | 93 // application handles multiple URLs. Note that this is the URL after all |
95 // URL rewriting and HTTP redirects have been performed. | 94 // URL rewriting and HTTP redirects have been performed. |
96 // | 95 // |
97 // When Connection is representing and outgoing connection, this will be the | 96 // When Connection is representing and outgoing connection, this will be the |
98 // same as the value returned by GetRemoveApplicationURL(). | 97 // same as the value returned by GetRemoveApplicationURL(). |
99 virtual const std::string& GetConnectionURL() = 0; | 98 virtual const std::string& GetConnectionURL() = 0; |
100 | 99 |
101 // Returns the URL identifying the remote application on this connection. | 100 // Returns the URL identifying the remote application on this connection. |
102 virtual const std::string& GetRemoteApplicationURL() = 0; | 101 virtual const std::string& GetRemoteApplicationURL() = 0; |
103 | 102 |
104 // Returns the raw proxy to the remote application's ServiceProvider | 103 // Returns the raw proxy to the remote application's InterfaceProvider |
105 // interface. Most applications will just use ConnectToService() instead. | 104 // interface. Most applications will just use GetInterface() instead. |
106 // Caller does not take ownership. | 105 // Caller does not take ownership. |
107 virtual ServiceProvider* GetRemoteInterfaces() = 0; | 106 virtual InterfaceProvider* GetRemoteInterfaces() = 0; |
108 | 107 |
109 // Returns the local application's ServiceProvider interface. The return | 108 // Returns the local application's InterfaceProvider interface. The return |
110 // value is owned by this connection. | 109 // value is owned by this connection. |
111 virtual ServiceProvider* GetLocalInterfaces() = 0; | 110 virtual InterfaceProvider* GetLocalInterfaces() = 0; |
112 | 111 |
113 // Register a handler to receive an error notification on the pipe to the | 112 // Register a handler to receive an error notification on the pipe to the |
114 // remote application's service provider. | 113 // remote application's InterfaceProvider. |
115 virtual void SetRemoteServiceProviderConnectionErrorHandler( | 114 virtual void SetRemoteInterfaceProviderConnectionErrorHandler( |
116 const Closure& handler) = 0; | 115 const Closure& handler) = 0; |
117 | 116 |
118 // Returns the id of the remote application. For Connections created via | 117 // Returns the id of the remote application. For Connections created via |
119 // Shell::Connect(), this will not be determined until Connect()'s callback is | 118 // Shell::Connect(), this will not be determined until Connect()'s callback is |
120 // run, and this function will return false. Use AddRemoteIDCallback() to | 119 // run, and this function will return false. Use AddRemoteIDCallback() to |
121 // schedule a callback to be run when the remote application id is available. | 120 // schedule a callback to be run when the remote application id is available. |
122 // A value of Shell::kInvalidApplicationID indicates the connection has not | 121 // A value of Shell::kInvalidApplicationID indicates the connection has not |
123 // been established. | 122 // been established. |
124 virtual bool GetRemoteApplicationID(uint32_t* remote_id) const = 0; | 123 virtual bool GetRemoteApplicationID(uint32_t* remote_id) const = 0; |
125 | 124 |
(...skipping 13 matching lines...) Expand all Loading... |
139 // some filtering policy preventing this interface from being exposed). | 138 // some filtering policy preventing this interface from being exposed). |
140 virtual bool SetInterfaceBinderForName(InterfaceBinder* binder, | 139 virtual bool SetInterfaceBinderForName(InterfaceBinder* binder, |
141 const std::string& name) = 0; | 140 const std::string& name) = 0; |
142 | 141 |
143 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; | 142 virtual base::WeakPtr<Connection> GetWeakPtr() = 0; |
144 }; | 143 }; |
145 | 144 |
146 } // namespace mojo | 145 } // namespace mojo |
147 | 146 |
148 #endif // MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ | 147 #endif // MOJO_SHELL_PUBLIC_CPP_CONNECTION_H_ |
OLD | NEW |