application_impl.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_IMPL_H_
6#define MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_
7#include <vector>
8
9#include "mojo/public/cpp/application/application_connection.h"
10#include "mojo/public/cpp/application/lib/service_connector.h"
11#include "mojo/public/cpp/application/lib/service_registry.h"
12#include "mojo/public/cpp/system/core.h"
13#include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
14
15#if defined(WIN32)
16#if !defined(CDECL)
17#define CDECL __cdecl
18#endif
19#define APPLICATION_EXPORT __declspec(dllexport)
20#else
21#define CDECL
22#define APPLICATION_EXPORT __attribute__((visibility("default")))
23#endif
24
25// DSOs can either implement MojoMain directly or include
26// mojo_main_{standalone|chromium}.cc in their project and implement
27// ApplicationImpl::Create();
28// TODO(davemoore): Establish this as part of our SDK for third party mojo
29// application writers.
30extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain(
31    MojoHandle service_provider_handle);
32
33namespace mojo {
34
35class ApplicationDelegate;
36
37// Utility class for communicating with the Shell, and providing Services
38// to clients.
39//
40// To use define a class that implements your specific server api, e.g. FooImpl
41// to implement a service named Foo.
42// That class must subclass an InterfaceImpl specialization.
43//
44// If there is context that is to be shared amongst all instances, define a
45// constructor with that class as its only argument, otherwise define an empty
46// constructor.
47//
48// class FooImpl : public InterfaceImpl<Foo> {
49//  public:
50//   FooImpl(ApplicationContext* app_context) {}
51// };
52//
53// or
54//
55// class BarImpl : public InterfaceImpl<Bar> {
56//  public:
57//   // contexts will remain valid for the lifetime of BarImpl.
58//   BarImpl(ApplicationContext* app_context, BarContext* service_context)
59//          : app_context_(app_context), servicecontext_(context) {}
60//
61// Create an ApplicationDele instance that collects any service implementations.
62//
63// ApplicationImpl app(service_provider_handle);
64// app.AddService<FooImpl>();
65//
66// BarContext context;
67// app.AddService<BarImpl>(&context);
68//
69//
70class ApplicationImpl : public InterfaceImpl<Application> {
71 public:
72  explicit ApplicationImpl(ApplicationDelegate* delegate);
73  ApplicationImpl(ApplicationDelegate* delegate,
74                  ScopedMessagePipeHandle shell_handle);
75  ApplicationImpl(ApplicationDelegate* delegate,
76                  MojoHandle shell_handle);
77  virtual ~ApplicationImpl();
78
79  // Establishes a new connection to an application. Caller does not own.
80  ApplicationConnection* ConnectToApplication(const String& application_url);
81
82  // Connect to application identified by |application_url| and connect to
83  // an the service implementation of the interface identified by |Interface|.
84  template <typename Interface>
85  void ConnectToService(const std::string& application_url,
86                        InterfacePtr<Interface>* ptr) {
87    ConnectToApplication(application_url)->ConnectToService(ptr);
88  }
89
90 private:
91  friend MojoResult (::MojoMain)(MojoHandle);
92
93  void BindShell(ScopedMessagePipeHandle shell_handle);
94  void BindShell(MojoHandle shell_handle);
95
96  // Application implementation.
97  virtual void AcceptConnection(const String& requestor_url,
98                                ServiceProviderPtr provider) MOJO_OVERRIDE;
99
100  typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList;
101  ServiceRegistryList incoming_service_registries_;
102  ServiceRegistryList outgoing_service_registries_;
103  ApplicationDelegate* delegate_;
104  ShellPtr shell_;
105
106  MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl);
107};
108
109}  // namespace mojo
110
111#endif  // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_
112