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_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ | 5 #ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ |
6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ | 6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "mojo/public/cpp/application/lib/service_connector.h" | 10 #include "mojo/public/cpp/application/lib/service_connector.h" |
11 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 11 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 | 14 |
15 // An instance of this class is passed to | 15 // Represents a connection to another application. An instance of this class is |
16 // ApplicationDelegate's ConfigureIncomingConnection() method each time a | 16 // passed to ApplicationDelegate's ConfigureIncomingConnection() method each |
17 // connection is made to this app, and to ApplicationDelegate's | 17 // time a connection is made to this app, and to ApplicationDelegate's |
18 // ConfigureOutgoingConnection() method when the app connects to | 18 // ConfigureOutgoingConnection() method when the app connects to another. |
19 // another. | |
20 // | 19 // |
21 // To use define a class that implements your specific service api, e.g. FooImpl | 20 // To use, define a class that implements your specific service API (e.g., |
22 // to implement a service named Foo. | 21 // FooImpl to implement a service named Foo). Then implement an |
23 // That class must subclass an InterfaceImpl specialization. | 22 // InterfaceFactory<Foo> that binds instances of FooImpl to |
| 23 // InterfaceRequest<Foo>s and register that on the connection like this: |
24 // | 24 // |
25 // Then implement an InterfaceFactory<Foo> that binds instances of FooImpl to | 25 // connection->AddService(&factory); |
26 // InterfaceRequest<Foo>s and register that on the connection. | |
27 // | 26 // |
28 // connection->AddService(&factory); | 27 // Or, if you have multiple factories implemented by the same type, explicitly |
29 // | |
30 // Or if you have multiple factories implemented by the same type, explicitly | |
31 // specify the interface to register the factory for: | 28 // specify the interface to register the factory for: |
32 // | 29 // |
33 // connection->AddService<Foo>(&my_foo_and_bar_factory_); | 30 // connection->AddService<Foo>(&my_foo_and_bar_factory_); |
34 // connection->AddService<Bar>(&my_foo_and_bar_factory_); | 31 // connection->AddService<Bar>(&my_foo_and_bar_factory_); |
35 // | 32 // |
36 // The InterfaceFactory must outlive the ApplicationConnection. | 33 // The InterfaceFactory must outlive the ApplicationConnection. |
37 class ApplicationConnection { | 34 class ApplicationConnection { |
38 public: | 35 public: |
39 virtual ~ApplicationConnection(); | 36 virtual ~ApplicationConnection(); |
40 | 37 |
| 38 // Makes Interface available as a service to the remote application. |
| 39 // |factory| will create implementations of Interface on demand. |
41 template <typename Interface> | 40 template <typename Interface> |
42 void AddService(InterfaceFactory<Interface>* factory) { | 41 void AddService(InterfaceFactory<Interface>* factory) { |
43 AddServiceConnector( | 42 AddServiceConnector( |
44 new internal::InterfaceFactoryConnector<Interface>(factory)); | 43 new internal::InterfaceFactoryConnector<Interface>(factory)); |
45 } | 44 } |
46 | 45 |
47 // Connect to the service implementing |Interface|. | 46 // Binds |ptr| to an implemention of Interface in the remote application. |
| 47 // |ptr| can immediately be used to start sending requests to the remote |
| 48 // service. |
48 template <typename Interface> | 49 template <typename Interface> |
49 void ConnectToService(InterfacePtr<Interface>* ptr) { | 50 void ConnectToService(InterfacePtr<Interface>* ptr) { |
50 if (ServiceProvider* sp = GetServiceProvider()) { | 51 if (ServiceProvider* sp = GetServiceProvider()) { |
51 MessagePipe pipe; | 52 MessagePipe pipe; |
52 ptr->Bind(pipe.handle0.Pass()); | 53 ptr->Bind(pipe.handle0.Pass()); |
53 sp->ConnectToService(Interface::Name_, pipe.handle1.Pass()); | 54 sp->ConnectToService(Interface::Name_, pipe.handle1.Pass()); |
54 } | 55 } |
55 } | 56 } |
56 | 57 |
57 // The url identifying the application on the other end of this connection. | 58 // Returns the URL identifying the remote application on this connection. |
58 virtual const std::string& GetRemoteApplicationURL() = 0; | 59 virtual const std::string& GetRemoteApplicationURL() = 0; |
59 | 60 |
60 // Raw ServiceProvider interface to remote application. | 61 // Returns the raw proxy to the remote application's ServiceProvider |
| 62 // interface. Most applications will just use ConnectToService() instead. |
| 63 // Caller does not take ownership. |
61 virtual ServiceProvider* GetServiceProvider() = 0; | 64 virtual ServiceProvider* GetServiceProvider() = 0; |
62 | 65 |
63 private: | 66 private: |
64 virtual void AddServiceConnector( | 67 virtual void AddServiceConnector( |
65 internal::ServiceConnectorBase* service_connector) = 0; | 68 internal::ServiceConnectorBase* service_connector) = 0; |
66 }; | 69 }; |
67 | 70 |
68 } // namespace mojo | 71 } // namespace mojo |
69 | 72 |
70 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ | 73 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ |
OLD | NEW |