blob: 4b4cb9321c110214b56ca8a59f29d7c645794ec6 [file] [log] [blame]
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +09001// 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"
darin@chromium.orgf5e51512014-07-31 17:58:42 +090013#include "mojo/public/interfaces/application/application.mojom.h"
14#include "mojo/public/interfaces/application/shell.mojom.h"
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090015
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090016namespace mojo {
17
18class ApplicationDelegate;
19
20// Utility class for communicating with the Shell, and providing Services
21// to clients.
22//
23// To use define a class that implements your specific server api, e.g. FooImpl
24// to implement a service named Foo.
25// That class must subclass an InterfaceImpl specialization.
26//
27// If there is context that is to be shared amongst all instances, define a
28// constructor with that class as its only argument, otherwise define an empty
29// constructor.
30//
31// class FooImpl : public InterfaceImpl<Foo> {
32// public:
33// FooImpl(ApplicationContext* app_context) {}
34// };
35//
36// or
37//
38// class BarImpl : public InterfaceImpl<Bar> {
39// public:
40// // contexts will remain valid for the lifetime of BarImpl.
41// BarImpl(ApplicationContext* app_context, BarContext* service_context)
42// : app_context_(app_context), servicecontext_(context) {}
43//
tim@chromium.org82f179c2014-08-07 08:30:48 +090044// Create an ApplicationImpl instance that collects any service implementations.
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090045//
46// ApplicationImpl app(service_provider_handle);
47// app.AddService<FooImpl>();
48//
49// BarContext context;
50// app.AddService<BarImpl>(&context);
51//
52//
53class ApplicationImpl : public InterfaceImpl<Application> {
54 public:
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090055 ApplicationImpl(ApplicationDelegate* delegate,
56 ScopedMessagePipeHandle shell_handle);
57 ApplicationImpl(ApplicationDelegate* delegate,
58 MojoHandle shell_handle);
59 virtual ~ApplicationImpl();
60
aaf7e3f682014-09-05 14:17:28 +090061 Shell* shell() const { return shell_.get(); }
62
davemoore4838f762014-09-20 08:55:33 +090063 // Returns any initial configuration arguments, passed by the Shell.
64 const Array<String>& args() { return args_; }
65
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090066 // Establishes a new connection to an application. Caller does not own.
67 ApplicationConnection* ConnectToApplication(const String& application_url);
68
darin@chromium.orgb1ebb4c2014-07-29 17:12:35 +090069 // Connect to application identified by |application_url| and connect to the
70 // service implementation of the interface identified by |Interface|.
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090071 template <typename Interface>
72 void ConnectToService(const std::string& application_url,
73 InterfacePtr<Interface>* ptr) {
74 ConnectToApplication(application_url)->ConnectToService(ptr);
75 }
76
77 private:
davemoore9e945d72014-09-03 06:23:11 +090078 class ShellPtrWatcher;
tim@chromium.org82f179c2014-08-07 08:30:48 +090079
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090080 void BindShell(ScopedMessagePipeHandle shell_handle);
tim@chromium.org82f179c2014-08-07 08:30:48 +090081 void ClearConnections();
davemoore8048f092014-08-26 03:33:56 +090082 void OnShellError() {
83 ClearConnections();
84 Terminate();
85 };
tim@chromium.org82f179c2014-08-07 08:30:48 +090086
87 // Quits the main run loop for this application.
tim@chromium.org1e608da2014-08-21 19:46:21 +090088 static void Terminate();
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090089
90 // Application implementation.
Viet-Trung Luu7b624492014-09-30 05:50:24 +090091 virtual void Initialize(Array<String> args) override;
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090092 virtual void AcceptConnection(const String& requestor_url,
Viet-Trung Luu7b624492014-09-30 05:50:24 +090093 ServiceProviderPtr provider) override;
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090094
95 typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList;
davemoore4838f762014-09-20 08:55:33 +090096
97 bool initialized_;
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +090098 ServiceRegistryList incoming_service_registries_;
99 ServiceRegistryList outgoing_service_registries_;
100 ApplicationDelegate* delegate_;
101 ShellPtr shell_;
davemoore9e945d72014-09-03 06:23:11 +0900102 ShellPtrWatcher* shell_watch_;
davemoore4838f762014-09-20 08:55:33 +0900103 Array<String> args_;
davemoore@chromium.org15f4ce72014-06-26 06:57:02 +0900104
105 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl);
106};
107
108} // namespace mojo
109
110#endif // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_