1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Copyright 2014 The Chromium Authors. All rights reserved.
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Use of this source code is governed by a BSD-style license that can be
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// found in the LICENSE file.
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <string>
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include "mojo/public/cpp/application/lib/service_connector.h"
11471ae72f18e7b23a96b245dbd508386fe139449cpbos@webrtc.org#include "mojo/public/interfaces/application/service_provider.mojom.h"
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
13b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.orgnamespace mojo {
14b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
15b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// An instance of this class is passed to
16b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// ApplicationDelegate's ConfigureIncomingConnection() method each time a
17b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// connection is made to this app, and to ApplicationDelegate's
18b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// ConfigureOutgoingConnection() method when the app connects to
19b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// another.
20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org//
21b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// To use define a class that implements your specific service api, e.g. FooImpl
22b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// to implement a service named Foo.
23b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// That class must subclass an InterfaceImpl specialization.
24b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org//
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Then implement an InterfaceFactory<Foo> that binds instances of FooImpl to
26b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// InterfaceRequest<Foo>s and register that on the connection.
27b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org//
28b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// connection->AddService(&factory);
29b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org//
30b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Or if you have multiple factories implemented by the same type, explicitly
31b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// specify the interface to register the factory for:
32b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org//
33b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// connection->AddService<Foo>(&my_foo_and_bar_factory_);
34b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// connection->AddService<Bar>(&my_foo_and_bar_factory_);
35b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org//
36b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org// The InterfaceFactory must outlive the ApplicationConnection.
37b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass ApplicationConnection {
38b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org public:
39b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  virtual ~ApplicationConnection();
40b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
41b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  template <typename Interface>
42b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  void AddService(InterfaceFactory<Interface>* factory) {
43b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    AddServiceConnector(
44b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org        new internal::InterfaceFactoryConnector<Interface>(factory));
45b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  }
46b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
47b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  // Connect to the service implementing |Interface|.
48b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  template <typename Interface>
49b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  void ConnectToService(InterfacePtr<Interface>* ptr) {
50b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org    MessagePipe pipe;
51b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org    ptr->Bind(pipe.handle0.Pass());
52b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org    GetServiceProvider()->ConnectToService(Interface::Name_,
53b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org                                           pipe.handle1.Pass());
54b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  }
55b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org
56b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  // The url identifying the application on the other end of this connection.
57b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  virtual const std::string& GetRemoteApplicationURL() = 0;
58b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
59b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  // Establishes a new connection to an application.
60b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  // TODO(davemoore): Would it be better to expose the ApplicationImpl?
61b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  virtual ApplicationConnection* ConnectToApplication(
62b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      const std::string& url) = 0;
63b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
64b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  // Connect to application identified by |application_url| and connect to
65b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  // the service implementation of the interface identified by |Interface|.
66b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  template <typename Interface>
67b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  void ConnectToService(const std::string& application_url,
68b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org                        InterfacePtr<Interface>* ptr) {
69b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org    ConnectToApplication(application_url)->ConnectToService(ptr);
70b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  }
71b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org
72b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  // Raw ServiceProvider interface to remote application.
73b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  virtual ServiceProvider* GetServiceProvider() = 0;
74b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org
75b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org private:
76b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org  virtual void AddServiceConnector(
77b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org      internal::ServiceConnectorBase* service_connector) = 0;
78b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
79b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
80b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org}  // namespace mojo
81b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org
82b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org#endif  // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
83b3ada1540827c60a63058570a94a57dfd260ad11pbos@webrtc.org