Chromium Code Reviews| 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/interface_factory.h" | |
| 10 #include "mojo/public/cpp/application/lib/service_connector.h" | 11 #include "mojo/public/cpp/application/lib/service_connector.h" |
| 11 | 12 |
| 12 namespace mojo { | 13 namespace mojo { |
| 13 | 14 |
| 14 // An instance of this class is passed to | 15 // An instance of this class is passed to |
| 15 // ApplicationDelegate's ConfigureIncomingConnection() method each time a | 16 // ApplicationDelegate's ConfigureIncomingConnection() method each time a |
| 16 // connection is made to this app, and to ApplicationDelegate's | 17 // connection is made to this app, and to ApplicationDelegate's |
| 17 // ConfigureOutgoingConnection() method when the app connects to | 18 // ConfigureOutgoingConnection() method when the app connects to |
| 18 // another. | 19 // another. |
| 19 // | 20 // |
| 20 // To use define a class that implements your specific service api, e.g. FooImpl | 21 // To use define a class that implements your specific service api, e.g. FooImpl |
| 21 // to implement a service named Foo. | 22 // to implement a service named Foo. |
| 22 // That class must subclass an InterfaceImpl specialization. | 23 // That class must subclass an InterfaceImpl specialization. |
| 23 // | 24 // |
| 24 // If there is context that is to be shared amongst all instances, define a | 25 // Then implement a InterfaceFactory<Foo> that binds instances of FooImpl to |
|
darin (slow to review)
2014/07/15 06:10:37
nit: "a" -> "an"
| |
| 25 // constructor with that class as its only argument, otherwise define an empty | 26 // InterfaceRequest<Foo>s and register that on the connection. |
| 26 // constructor. | |
| 27 // | 27 // |
| 28 // class FooImpl : public InterfaceImpl<Foo> { | 28 // connection->AddServiceFactory(&factory); |
| 29 // public: | |
| 30 // explicit FooImpl(ApplicationConnnection* connection) {} | |
| 31 // }; | |
| 32 // | 29 // |
| 33 // or | 30 // Or if you have multiple factories implemented by the same type, explicitly |
| 31 // specify the interface to register the factory for: | |
| 34 // | 32 // |
| 35 // class BarImpl : public InterfaceImpl<Bar> { | 33 // connection->AddServiceFactory<Foo>(&my_foo_and_bar_factory_); |
| 36 // public: | 34 // connection->AddServiceFactory<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 // | |
| 41 // Create an ApplicationDelegate instance and pass it to the constructor | |
| 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 { | 35 class ApplicationConnection { |
| 50 public: | 36 public: |
| 51 virtual ~ApplicationConnection(); | 37 virtual ~ApplicationConnection(); |
| 52 | 38 |
| 53 // Impl’s constructor will receive two arguments: | 39 template <typename Interface> |
| 54 // Impl::Impl(Application::Context* app_context, | 40 void AddServiceFactory(InterfaceFactory<Interface>* factory) { |
|
darin (slow to review)
2014/07/15 06:10:37
you probably want to say somewhere that the Interf
| |
| 55 // ServiceContext* svc_context) | |
| 56 template <typename Impl, typename ServiceContext> | |
| 57 void AddService(ServiceContext* context) { | |
| 58 AddServiceConnector( | 41 AddServiceConnector( |
| 59 new internal::ServiceConnector<Impl, ServiceContext>(Impl::Name_, | 42 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 } | 43 } |
| 70 | 44 |
| 71 // Connect to the service implementing |Interface|. | 45 // Connect to the service implementing |Interface|. |
| 72 template <typename Interface> | 46 template <typename Interface> |
| 73 void ConnectToService(InterfacePtr<Interface>* ptr) { | 47 void ConnectToService(InterfacePtr<Interface>* ptr) { |
| 74 MessagePipe pipe; | 48 MessagePipe pipe; |
| 75 ptr->Bind(pipe.handle0.Pass()); | 49 ptr->Bind(pipe.handle0.Pass()); |
| 76 GetServiceProvider()->ConnectToService(Interface::Name_, | 50 GetServiceProvider()->ConnectToService(Interface::Name_, |
| 77 pipe.handle1.Pass()); | 51 pipe.handle1.Pass()); |
| 78 } | 52 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 97 virtual ServiceProvider* GetServiceProvider() = 0; | 71 virtual ServiceProvider* GetServiceProvider() = 0; |
| 98 | 72 |
| 99 private: | 73 private: |
| 100 virtual void AddServiceConnector( | 74 virtual void AddServiceConnector( |
| 101 internal::ServiceConnectorBase* service_connector) = 0; | 75 internal::ServiceConnectorBase* service_connector) = 0; |
| 102 }; | 76 }; |
| 103 | 77 |
| 104 } // namespace mojo | 78 } // namespace mojo |
| 105 | 79 |
| 106 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ | 80 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_ |
| OLD | NEW |