Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / application / application_connection.h
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.
4
5 #ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
7
8 #include <string>
9
10 #include "mojo/public/cpp/application/lib/service_connector.h"
11 #include "mojo/public/interfaces/application/service_provider.mojom.h"
12
13 namespace mojo {
14
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
19 // another.
20 //
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.
24 //
25 // Then implement an InterfaceFactory<Foo> that binds instances of FooImpl to
26 // InterfaceRequest<Foo>s and register that on the connection.
27 //
28 // connection->AddService(&factory);
29 //
30 // Or if you have multiple factories implemented by the same type, explicitly
31 // specify the interface to register the factory for:
32 //
33 // connection->AddService<Foo>(&my_foo_and_bar_factory_);
34 // connection->AddService<Bar>(&my_foo_and_bar_factory_);
35 //
36 // The InterfaceFactory must outlive the ApplicationConnection.
37 class ApplicationConnection {
38  public:
39   virtual ~ApplicationConnection();
40
41   template <typename Interface>
42   void AddService(InterfaceFactory<Interface>* factory) {
43     AddServiceConnector(
44         new internal::InterfaceFactoryConnector<Interface>(factory));
45   }
46
47   // Connect to the service implementing |Interface|.
48   template <typename Interface>
49   void ConnectToService(InterfacePtr<Interface>* ptr) {
50     MessagePipe pipe;
51     ptr->Bind(pipe.handle0.Pass());
52     GetServiceProvider()->ConnectToService(Interface::Name_,
53                                            pipe.handle1.Pass());
54   }
55
56   // The url identifying the application on the other end of this connection.
57   virtual const std::string& GetRemoteApplicationURL() = 0;
58
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;
63
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);
70   }
71
72   // Raw ServiceProvider interface to remote application.
73   virtual ServiceProvider* GetServiceProvider() = 0;
74
75  private:
76   virtual void AddServiceConnector(
77       internal::ServiceConnectorBase* service_connector) = 0;
78 };
79
80 }  // namespace mojo
81
82 #endif  // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_