- update source.
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application.h
1 // Copyright (c) 2013 Intel Corporation. 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 XWALK_APPLICATION_BROWSER_APPLICATION_H_
6 #define XWALK_APPLICATION_BROWSER_APPLICATION_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/observer_list.h"
17 #include "xwalk/application/browser/event_observer.h"
18 #include "xwalk/application/common/application_data.h"
19 #include "xwalk/runtime/browser/runtime.h"
20
21 namespace xwalk {
22
23 class RuntimeContext;
24
25 namespace application {
26
27 class ApplicationHost;
28 class Manifest;
29
30 // The Application class is representing an active (running) application.
31 // Application instances are owned by ApplicationService.
32 // ApplicationService will delete an Application instance when it is
33 // terminated.
34 // There's one-to-one correspondence between Application and Render Process
35 // Host, obtained from its "runtimes" (pages).
36 class Application : public Runtime::Observer {
37  public:
38   virtual ~Application();
39
40   class Observer {
41    public:
42     // Invoked when application is terminated - all its pages (runtimes)
43     // are closed.
44     virtual void OnApplicationTerminated(Application* app) {}
45
46    protected:
47     virtual ~Observer() {}
48   };
49
50   // Manifest keys that can be used as application entry points.
51   enum LaunchEntryPoint {
52     AppMainKey = 1 << 0,  // app.main
53     LaunchLocalPathKey = 1 << 1,  // app.launch.local_path
54     // NOTE: The following key is only used for "dummy" hosted apps,
55     // which can be using any arbitrary URL, incl. remote ones.
56     // For now this should be disabled for all other cases as this will
57     // require special care with permissions etc.
58     URLKey = 1 << 2,  // url
59     Default = AppMainKey | LaunchLocalPathKey
60   };
61   typedef unsigned LaunchEntryPoints;
62
63   struct LaunchParams {
64     LaunchParams() :
65         entry_points(Default) {}
66
67     LaunchEntryPoints entry_points;
68   };
69
70   // Closes all the application's runtimes (application pages).
71   // NOTE: ApplicationService deletes an Application instance
72   // immediately after its termination.
73   void Terminate();
74
75   // Returns Runtime (application page) containing the application's
76   // 'main document'. The main document is the main entry point of
77   // the application to the system. This method will return 'NULL'
78   // if application has different entry point (local path manifest key).
79   // See http://anssiko.github.io/runtime/app-lifecycle.html#dfn-main-document
80   // The main document never has a visible window on its own.
81   Runtime* GetMainDocumentRuntime() const;
82
83   const std::set<Runtime*>& runtimes() const { return runtimes_; }
84
85   // Returns the unique application id which is used to distinguish the
86   // application amoung both running applications and installed ones
87   // (ApplicationData objects).
88   std::string id() const { return application_data_->ID(); }
89   bool HasMainDocument() const { return application_data_->HasMainDocument(); }
90   int GetRenderProcessHostID() const;
91
92   const ApplicationData* data() const { return application_data_; }
93   ApplicationData* data() { return application_data_; }
94
95  private:
96   // Runtime::Observer implementation.
97   virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
98   virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
99
100   // We enforce ApplicationService ownership.
101   friend class ApplicationService;
102   Application(scoped_refptr<ApplicationData> data,
103               RuntimeContext* context,
104               Observer* observer);
105   bool Launch(const LaunchParams& launch_params);
106
107   template<LaunchEntryPoint>
108   bool TryLaunchAt();
109
110   friend class FinishEventObserver;
111   void CloseMainDocument();
112   bool IsOnSuspendHandlerRegistered() const;
113
114   RuntimeContext* runtime_context_;
115   const scoped_refptr<ApplicationData> application_data_;
116   Runtime* main_runtime_;
117   std::set<Runtime*> runtimes_;
118   scoped_ptr<EventObserver> finish_observer_;
119   Observer* observer_;
120
121   DISALLOW_COPY_AND_ASSIGN(Application);
122 };
123
124 }  // namespace application
125 }  // namespace xwalk
126
127 #endif  // XWALK_APPLICATION_BROWSER_APPLICATION_H_