2a1a5969bfad2995f781bf5a980237182b4e4032
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / application / application_impl.h
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/application/application.mojom.h"
14 #include "mojo/public/interfaces/application/shell.mojom.h"
15
16 #if defined(WIN32)
17 #if !defined(CDECL)
18 #define CDECL __cdecl
19 #endif
20 #define APPLICATION_EXPORT __declspec(dllexport)
21 #else
22 #define CDECL
23 #define APPLICATION_EXPORT __attribute__((visibility("default")))
24 #endif
25
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);
33
34 namespace mojo {
35
36 class ApplicationDelegate;
37
38 // Utility class for communicating with the Shell, and providing Services
39 // to clients.
40 //
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.
44 //
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
47 // constructor.
48 //
49 // class FooImpl : public InterfaceImpl<Foo> {
50 //  public:
51 //   FooImpl(ApplicationContext* app_context) {}
52 // };
53 //
54 // or
55 //
56 // class BarImpl : public InterfaceImpl<Bar> {
57 //  public:
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) {}
61 //
62 // Create an ApplicationImpl instance that collects any service implementations.
63 //
64 // ApplicationImpl app(service_provider_handle);
65 // app.AddService<FooImpl>();
66 //
67 // BarContext context;
68 // app.AddService<BarImpl>(&context);
69 //
70 //
71 class ApplicationImpl : public InterfaceImpl<Application> {
72  public:
73   explicit ApplicationImpl(ApplicationDelegate* delegate);
74   ApplicationImpl(ApplicationDelegate* delegate,
75                   ScopedMessagePipeHandle shell_handle);
76   ApplicationImpl(ApplicationDelegate* delegate,
77                   MojoHandle shell_handle);
78   virtual ~ApplicationImpl();
79
80   // Establishes a new connection to an application. Caller does not own.
81   ApplicationConnection* ConnectToApplication(const String& application_url);
82
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);
89   }
90
91  private:
92   class ShellPtrWatcher : public ErrorHandler {
93    public:
94     explicit ShellPtrWatcher(ApplicationImpl* impl);
95     virtual ~ShellPtrWatcher();
96     virtual void OnConnectionError() MOJO_OVERRIDE;
97    private:
98     ApplicationImpl* impl_;
99     MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher);
100   };
101
102   friend MojoResult (::MojoMain)(MojoHandle);
103
104   void BindShell(ScopedMessagePipeHandle shell_handle);
105   void BindShell(MojoHandle shell_handle);
106   void ClearConnections();
107   void OnShellError() { ClearConnections(); Terminate(); };
108
109   // Quits the main run loop for this application.
110   void Terminate();
111
112   // Application implementation.
113   virtual void AcceptConnection(const String& requestor_url,
114                                 ServiceProviderPtr provider) MOJO_OVERRIDE;
115
116   typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList;
117   ServiceRegistryList incoming_service_registries_;
118   ServiceRegistryList outgoing_service_registries_;
119   ApplicationDelegate* delegate_;
120   ShellPtr shell_;
121   ShellPtrWatcher shell_watch_;
122
123   MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl);
124 };
125
126 }  // namespace mojo
127
128 #endif  // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_