application_connection.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <string>
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/public/cpp/application/lib/service_connector.h"
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "mojo/public/interfaces/application/service_provider.mojom.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace mojo {
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// An instance of this class is passed to
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// ApplicationDelegate's ConfigureIncomingConnection() method each time a
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// connection is made to this app, and to ApplicationDelegate's
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// ConfigureOutgoingConnection() method when the app connects to
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// another.
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// To use define a class that implements your specific service api, e.g. FooImpl
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// to implement a service named Foo.
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// That class must subclass an InterfaceImpl specialization.
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Then implement an InterfaceFactory<Foo> that binds instances of FooImpl to
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// InterfaceRequest<Foo>s and register that on the connection.
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// connection->AddService(&factory);
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Or if you have multiple factories implemented by the same type, explicitly
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// specify the interface to register the factory for:
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// connection->AddService<Foo>(&my_foo_and_bar_factory_);
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// connection->AddService<Bar>(&my_foo_and_bar_factory_);
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// The InterfaceFactory must outlive the ApplicationConnection.
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass ApplicationConnection {
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~ApplicationConnection();
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  template <typename Interface>
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void AddService(InterfaceFactory<Interface>* factory) {
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    AddServiceConnector(
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        new internal::InterfaceFactoryConnector<Interface>(factory));
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Connect to the service implementing |Interface|.
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  template <typename Interface>
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void ConnectToService(InterfacePtr<Interface>* ptr) {
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    MessagePipe pipe;
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ptr->Bind(pipe.handle0.Pass());
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    GetServiceProvider()->ConnectToService(Interface::Name_,
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                           pipe.handle1.Pass());
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // The url identifying the application on the other end of this connection.
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual const std::string& GetRemoteApplicationURL() = 0;
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Establishes a new connection to an application.
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // TODO(davemoore): Would it be better to expose the ApplicationImpl?
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ApplicationConnection* ConnectToApplication(
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const std::string& url) = 0;
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Connect to application identified by |application_url| and connect to
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // the service implementation of the interface identified by |Interface|.
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  template <typename Interface>
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void ConnectToService(const std::string& application_url,
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                        InterfacePtr<Interface>* ptr) {
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ConnectToApplication(application_url)->ConnectToService(ptr);
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Raw ServiceProvider interface to remote application.
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ServiceProvider* GetServiceProvider() = 0;
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) private:
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void AddServiceConnector(
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      internal::ServiceConnectorBase* service_connector) = 0;
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace mojo
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif  // MOJO_PUBLIC_APPLICATION_APPLICATION_CONNECTION_H_
83