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.
5 #ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_
6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_
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/application/application.mojom.h"
14 #include "mojo/public/interfaces/application/shell.mojom.h"
20 #define APPLICATION_EXPORT __declspec(dllexport)
23 #define APPLICATION_EXPORT __attribute__((visibility("default")))
26 // DSOs can either implement MojoMain directly or include
27 // mojo_main_{standalone|chromium}.cc in their project and implement
28 // ApplicationImpl::Create();
29 // TODO(davemoore): Establish this as part of our SDK for third party mojo
30 // application writers.
31 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain(
32 MojoHandle service_provider_handle);
36 class ApplicationDelegate;
38 // Utility class for communicating with the Shell, and providing Services
41 // To use define a class that implements your specific server api, e.g. FooImpl
42 // to implement a service named Foo.
43 // That class must subclass an InterfaceImpl specialization.
45 // If there is context that is to be shared amongst all instances, define a
46 // constructor with that class as its only argument, otherwise define an empty
49 // class FooImpl : public InterfaceImpl<Foo> {
51 // FooImpl(ApplicationContext* app_context) {}
56 // class BarImpl : public InterfaceImpl<Bar> {
58 // // contexts will remain valid for the lifetime of BarImpl.
59 // BarImpl(ApplicationContext* app_context, BarContext* service_context)
60 // : app_context_(app_context), servicecontext_(context) {}
62 // Create an ApplicationImpl instance that collects any service implementations.
64 // ApplicationImpl app(service_provider_handle);
65 // app.AddService<FooImpl>();
67 // BarContext context;
68 // app.AddService<BarImpl>(&context);
71 class ApplicationImpl : public InterfaceImpl<Application> {
73 explicit ApplicationImpl(ApplicationDelegate* delegate);
74 ApplicationImpl(ApplicationDelegate* delegate,
75 ScopedMessagePipeHandle shell_handle);
76 ApplicationImpl(ApplicationDelegate* delegate,
77 MojoHandle shell_handle);
78 virtual ~ApplicationImpl();
80 // Establishes a new connection to an application. Caller does not own.
81 ApplicationConnection* ConnectToApplication(const String& application_url);
83 // Connect to application identified by |application_url| and connect to the
84 // service implementation of the interface identified by |Interface|.
85 template <typename Interface>
86 void ConnectToService(const std::string& application_url,
87 InterfacePtr<Interface>* ptr) {
88 ConnectToApplication(application_url)->ConnectToService(ptr);
92 class ShellPtrWatcher : public ErrorHandler {
94 explicit ShellPtrWatcher(ApplicationImpl* impl);
95 virtual ~ShellPtrWatcher();
96 virtual void OnConnectionError() MOJO_OVERRIDE;
98 ApplicationImpl* impl_;
99 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher);
102 friend MojoResult (::MojoMain)(MojoHandle);
104 void BindShell(ScopedMessagePipeHandle shell_handle);
105 void BindShell(MojoHandle shell_handle);
106 void ClearConnections();
107 void OnShellError() { ClearConnections(); Terminate(); };
109 // Quits the main run loop for this application.
112 // Application implementation.
113 virtual void AcceptConnection(const String& requestor_url,
114 ServiceProviderPtr provider) MOJO_OVERRIDE;
116 typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList;
117 ServiceRegistryList incoming_service_registries_;
118 ServiceRegistryList outgoing_service_registries_;
119 ApplicationDelegate* delegate_;
121 ShellPtrWatcher shell_watch_;
123 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl);
128 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_