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 | 11 |
12 namespace mojo { | 12 namespace mojo { |
13 | 13 |
14 // An instance of this class is passed to | 14 // An instance of this class is passed to |
15 // ApplicationDelegate's ConfigureIncomingConnection() method each time a | 15 // ApplicationDelegate's ConfigureIncomingConnection() method each time a |
16 // connection is made to this app, and to ApplicationDelegate's | 16 // connection is made to this app, and to ApplicationDelegate's |
17 // ConfigureOutgoingConnection() method when the app connects to | 17 // ConfigureOutgoingConnection() method when the app connects to |
18 // another. | 18 // another. |
19 // | 19 // |
20 // 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. FooImpl |
21 // to implement a service named Foo. | 21 // to implement a service named Foo. |
22 // That class must subclass an InterfaceImpl specialization. | 22 // That class must subclass an InterfaceImpl specialization. |
23 // | 23 // |
24 // If there is context that is to be shared amongst all instances, define a | 24 // Then implement an InterfaceFactory<Foo> that binds instances of FooImpl to |
25 // constructor with that class as its only argument, otherwise define an empty | 25 // InterfaceRequest<Foo>s and register that on the connection. |
26 // constructor. | |
27 // | 26 // |
28 // class FooImpl : public InterfaceImpl<Foo> { | 27 // connection->AddService(&factory); |
29 // public: | |
30 // explicit FooImpl(ApplicationConnnection* connection) {} | |
31 // }; | |
32 // | 28 // |
33 // or | 29 // Or if you have multiple factories implemented by the same type, explicitly |
| 30 // specify the interface to register the factory for: |
34 // | 31 // |
35 // class BarImpl : public InterfaceImpl<Bar> { | 32 // connection->AddService<Foo>(&my_foo_and_bar_factory_); |
36 // public: | 33 // connection->AddService<Bar>(&my_foo_and_bar_factory_); |
37 // // contexts will remain valid for the lifetime of BarImpl. | |
38 // BarImpl(ApplicationConnnection* connection, BarContext* service_context) | |
39 // : connection_(connection), servicecontext_(context) {} | |
40 // | 34 // |
41 // Create an ApplicationDelegate instance and pass it to the constructor | 35 // The InterfaceFactory must outlive the ApplicationConnection. |
42 // of an ApplicationImpl. The delegate will be called when new connections are | |
43 // made to other applications. | |
44 // | |
45 // connection->AddService<FooImpl>(); | |
46 // | |
47 // BarContext context; | |
48 // connection->AddService<BarImpl>(&context); | |
49 class ApplicationConnection { | 36 class ApplicationConnection { |
50 public: | 37 public: |
51 virtual ~ApplicationConnection(); | 38 virtual ~ApplicationConnection(); |
52 | 39 |
53 // Impl’s constructor will receive two arguments: | 40 template <typename Interface> |
54 // Impl::Impl(Application::Context* app_context, | 41 void AddService(InterfaceFactory<Interface>* factory) { |
55 // ServiceContext* svc_context) | |
56 template <typename Impl, typename ServiceContext> | |
57 void AddService(ServiceContext* context) { | |
58 AddServiceConnector( | 42 AddServiceConnector( |
59 new internal::ServiceConnector<Impl, ServiceContext>(Impl::Name_, | 43 new internal::InterfaceFactoryConnector<Interface>(factory)); |
60 context)); | |
61 } | |
62 | |
63 // Impl’s constructor will receive one argument: | |
64 // Impl::Impl(Application::Context* app_context) | |
65 template <typename Impl> | |
66 void AddService() { | |
67 AddServiceConnector( | |
68 new internal::ServiceConnector<Impl, void>(Impl::Name_, NULL)); | |
69 } | 44 } |
70 | 45 |
71 // Connect to the service implementing |Interface|. | 46 // Connect to the service implementing |Interface|. |
72 template <typename Interface> | 47 template <typename Interface> |
73 void ConnectToService(InterfacePtr<Interface>* ptr) { | 48 void ConnectToService(InterfacePtr<Interface>* ptr) { |
74 MessagePipe pipe; | 49 MessagePipe pipe; |
75 ptr->Bind(pipe.handle0.Pass()); | 50 ptr->Bind(pipe.handle0.Pass()); |
76 GetServiceProvider()->ConnectToService(Interface::Name_, | 51 GetServiceProvider()->ConnectToService(Interface::Name_, |
77 pipe.handle1.Pass()); | 52 pipe.handle1.Pass()); |
78 } | 53 } |
(...skipping 10 matching lines...) Expand all Loading... |
89 // the service implementation of the interface identified by |Interface|. | 64 // the service implementation of the interface identified by |Interface|. |
90 template <typename Interface> | 65 template <typename Interface> |
91 void ConnectToService(const std::string& application_url, | 66 void ConnectToService(const std::string& application_url, |
92 InterfacePtr<Interface>* ptr) { | 67 InterfacePtr<Interface>* ptr) { |
93 ConnectToApplication(application_url)->ConnectToService(ptr); | 68 ConnectToApplication(application_url)->ConnectToService(ptr); |
94 } | 69 } |
95 | 70 |
96 // Raw ServiceProvider interface to remote application. | 71 // Raw ServiceProvider interface to remote application. |
97 virtual ServiceProvider* GetServiceProvider() = 0; | 72 virtual ServiceProvider* GetServiceProvider() = 0; |
98 | 73 |
99 private: | 74 private: |
100 virtual void AddServiceConnector( | 75 virtual void AddServiceConnector( |
101 internal::ServiceConnectorBase* service_connector) = 0; | 76 internal::ServiceConnectorBase* service_connector) = 0; |
102 }; | 77 }; |
103 | 78 |
104 } // namespace mojo | 79 } // namespace mojo |
105 | 80 |
106 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ | 81 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ |
OLD | NEW |