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
3 // found in the LICENSE file.
5 #ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
10 #include "mojo/public/cpp/application/lib/service_connector.h"
11 #include "mojo/public/interfaces/application/service_provider.mojom.h"
15 // An instance of this class is passed to
16 // ApplicationDelegate's ConfigureIncomingConnection() method each time a
17 // connection is made to this app, and to ApplicationDelegate's
18 // ConfigureOutgoingConnection() method when the app connects to
21 // To use define a class that implements your specific service api, e.g. FooImpl
22 // to implement a service named Foo.
23 // That class must subclass an InterfaceImpl specialization.
25 // Then implement an InterfaceFactory<Foo> that binds instances of FooImpl to
26 // InterfaceRequest<Foo>s and register that on the connection.
28 // connection->AddService(&factory);
30 // Or if you have multiple factories implemented by the same type, explicitly
31 // specify the interface to register the factory for:
33 // connection->AddService<Foo>(&my_foo_and_bar_factory_);
34 // connection->AddService<Bar>(&my_foo_and_bar_factory_);
36 // The InterfaceFactory must outlive the ApplicationConnection.
37 class ApplicationConnection {
39 virtual ~ApplicationConnection();
41 template <typename Interface>
42 void AddService(InterfaceFactory<Interface>* factory) {
44 new internal::InterfaceFactoryConnector<Interface>(factory));
47 // Connect to the service implementing |Interface|.
48 template <typename Interface>
49 void ConnectToService(InterfacePtr<Interface>* ptr) {
51 ptr->Bind(pipe.handle0.Pass());
52 GetServiceProvider()->ConnectToService(Interface::Name_,
56 // The url identifying the application on the other end of this connection.
57 virtual const std::string& GetRemoteApplicationURL() = 0;
59 // Establishes a new connection to an application.
60 // TODO(davemoore): Would it be better to expose the ApplicationImpl?
61 virtual ApplicationConnection* ConnectToApplication(
62 const std::string& url) = 0;
64 // Connect to application identified by |application_url| and connect to
65 // the service implementation of the interface identified by |Interface|.
66 template <typename Interface>
67 void ConnectToService(const std::string& application_url,
68 InterfacePtr<Interface>* ptr) {
69 ConnectToApplication(application_url)->ConnectToService(ptr);
72 // Raw ServiceProvider interface to remote application.
73 virtual ServiceProvider* GetServiceProvider() = 0;
76 virtual void AddServiceConnector(
77 internal::ServiceConnectorBase* service_connector) = 0;
82 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_