application_connection.h revision 116680a4aac90f2aa7413d9095a592090648e557
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
12namespace mojo {
13
14// An instance of this class is passed to
15// ApplicationDelegate's ConfigureIncomingConnection() method each time a
16// connection is made to this app, and to ApplicationDelegate's
17// ConfigureOutgoingConnection() method when the app connects to
18// another.
19//
20// To use define a class that implements your specific service api, e.g. FooImpl
21// to implement a service named Foo.
22// That class must subclass an InterfaceImpl specialization.
23//
24// If there is context that is to be shared amongst all instances, define a
25// constructor with that class as its only argument, otherwise define an empty
26// constructor.
27//
28// class FooImpl : public InterfaceImpl<Foo> {
29//  public:
30//   explicit FooImpl(ApplicationConnnection* connection) {}
31// };
32//
33// or
34//
35// class BarImpl : public InterfaceImpl<Bar> {
36//  public:
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);
49class ApplicationConnection {
50 public:
51  virtual ~ApplicationConnection();
52
53  // Impl’s constructor will receive two arguments:
54  // Impl::Impl(Application::Context* app_context,
55  //            ServiceContext* svc_context)
56  template <typename Impl, typename ServiceContext>
57  void AddService(ServiceContext* context) {
58    AddServiceConnector(
59        new internal::ServiceConnector<Impl, ServiceContext>(Impl::Name_,
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  }
70
71  // Connect to the service implementing |Interface|.
72  template <typename Interface>
73  void ConnectToService(InterfacePtr<Interface>* ptr) {
74    MessagePipe pipe;
75    ptr->Bind(pipe.handle0.Pass());
76    GetServiceProvider()->ConnectToService(Interface::Name_,
77                                           pipe.handle1.Pass());
78  }
79
80  // The url identifying the application on the other end of this connection.
81  virtual const std::string& GetRemoteApplicationURL() = 0;
82
83  // Establishes a new connection to an application.
84  // TODO(davemoore): Would it be better to expose the ApplicationImpl?
85  virtual ApplicationConnection* ConnectToApplication(
86      const std::string& url) = 0;
87
88  // Connect to application identified by |application_url| and connect to
89  // the service implementation of the interface identified by |Interface|.
90  template <typename Interface>
91  void ConnectToService(const std::string& application_url,
92                        InterfacePtr<Interface>* ptr) {
93    ConnectToApplication(application_url)->ConnectToService(ptr);
94  }
95
96  // Raw ServiceProvider interface to remote application.
97  virtual ServiceProvider* GetServiceProvider() = 0;
98
99private:
100 virtual void AddServiceConnector(
101      internal::ServiceConnectorBase* service_connector) = 0;
102};
103
104}  // namespace mojo
105
106#endif  // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
107