Upstream version 6.35.131.0
[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 #include <vector>
12
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/observer_list.h"
18 #include "ui/base/ui_base_types.h"
19 #include "xwalk/application/browser/event_observer.h"
20 #include "xwalk/application/common/application_data.h"
21 #include "xwalk/application/common/security_policy.h"
22 #include "xwalk/runtime/browser/runtime.h"
23
24 #if defined(USE_OZONE) && defined(OS_TIZEN)
25 #include "base/message_loop/message_pump_observer.h"
26 #endif
27
28 namespace xwalk {
29
30 class RuntimeContext;
31
32 namespace application {
33
34 class ApplicationHost;
35 class Manifest;
36 class SecurityPolicy;
37
38 // The Application class is representing an active (running) application.
39 // Application instances are owned by ApplicationService.
40 // ApplicationService will delete an Application instance when it is
41 // terminated.
42 // There's one-to-one correspondence between Application and Render Process
43 // Host, obtained from its "runtimes" (pages).
44 class Application
45   :
46 #if defined(USE_OZONE) && defined(OS_TIZEN)
47   public base::MessagePumpObserver,
48 #endif
49   public Runtime::Observer {
50  public:
51   virtual ~Application();
52
53   class Observer {
54    public:
55     // Invoked when application is terminated - all its pages (runtimes)
56     // are closed.
57     virtual void OnApplicationTerminated(Application* app) {}
58
59    protected:
60     virtual ~Observer() {}
61   };
62
63   // Manifest keys that can be used as application entry points.
64   enum LaunchEntryPoint {
65     AppMainKey = 1 << 0,  // app.main
66     LaunchLocalPathKey = 1 << 1,  // app.launch.local_path
67     URLKey = 1 << 2,  // url
68     Default = AppMainKey | LaunchLocalPathKey | URLKey
69   };
70   typedef unsigned LaunchEntryPoints;
71
72   struct LaunchParams {
73     LaunchParams() :
74         entry_points(Default),
75         launcher_pid(0),
76         force_fullscreen(false) {}
77
78     LaunchEntryPoints entry_points;
79
80     // Used only when running as service. Specifies the PID of the launcher
81     // process.
82     int32 launcher_pid;
83
84     bool force_fullscreen;
85   };
86
87   // Closes all the application's runtimes (application pages).
88   // NOTE: Application is terminated asynchronously.
89   // Please use ApplicationService::Observer::WillDestroyApplication()
90   // interface to be notified about actual app termination.
91   //
92   // NOTE: ApplicationService deletes an Application instance
93   // immediately after its termination.
94   enum TerminationMode {
95     Normal,
96     Immediate  // Ignore OnSuspend event handler.
97   };
98   void Terminate(TerminationMode = Normal);
99
100 #if defined(OS_TIZEN)
101   void Hide();
102 #endif
103
104   // Returns Runtime (application page) containing the application's
105   // 'main document'. The main document is the main entry point of
106   // the application to the system. This method will return 'NULL'
107   // if application has different entry point (local path manifest key).
108   // See http://anssiko.github.io/runtime/app-lifecycle.html#dfn-main-document
109   // The main document never has a visible window on its own.
110   Runtime* GetMainDocumentRuntime() const;
111
112   const std::set<Runtime*>& runtimes() const { return runtimes_; }
113
114   // Returns the unique application id which is used to distinguish the
115   // application amoung both running applications and installed ones
116   // (ApplicationData objects).
117   std::string id() const { return application_data_->ID(); }
118   int GetRenderProcessHostID() const;
119
120   const ApplicationData* data() const { return application_data_; }
121   ApplicationData* data() { return application_data_; }
122
123   // Tells whether the application use the specified extension.
124   bool UseExtension(const std::string& extension_name) const;
125
126   // The runtime permission mapping is registered by extension which
127   // implements some specific API, for example:
128   // "bluetooth" -> "bluetooth.read, bluetooth.write, bluetooth.management"
129   // Whenever there comes a API permission request, we can tell whether
130   // this API is registered, if yes, return the according permission name.
131   bool RegisterPermissions(const std::string& extension_name,
132                            const std::string& perm_table);
133   std::string GetRegisteredPermissionName(const std::string& extension_name,
134                                           const std::string& api_name) const;
135
136   StoredPermission GetPermission(PermissionType type,
137                                  std::string& permission_name) const;
138   bool SetPermission(PermissionType type,
139                      const std::string& permission_name,
140                      StoredPermission perm);
141   bool CanRequestURL(const GURL& url) const;
142
143  private:
144   bool HasMainDocument() const;
145   // Runtime::Observer implementation.
146   virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
147   virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
148
149   // We enforce ApplicationService ownership.
150   friend class ApplicationService;
151   Application(scoped_refptr<ApplicationData> data,
152               RuntimeContext* context,
153               Observer* observer);
154   bool Launch(const LaunchParams& launch_params);
155
156   // Try to extract the URL from different possible keys for entry points in the
157   // manifest, returns it and the entry point used.
158   GURL GetStartURL(const LaunchParams& params, LaunchEntryPoint* used);
159   ui::WindowShowState GetWindowShowState(const LaunchParams& params);
160
161   GURL GetURLFromAppMainKey();
162   GURL GetURLFromLocalPathKey();
163   GURL GetURLFromURLKey();
164
165   friend class FinishEventObserver;
166   void CloseMainDocument();
167   void NotifyTermination();
168   bool IsOnSuspendHandlerRegistered() const;
169   bool IsTerminating() const { return finish_observer_; }
170
171   void InitSecurityPolicy();
172   void AddSecurityPolicy(const GURL& url, bool subdomains);
173
174 #if defined(USE_OZONE) && defined(OS_TIZEN)
175   virtual base::EventStatus WillProcessEvent(
176       const base::NativeEvent& event) OVERRIDE;
177   virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE;
178 #endif
179
180   RuntimeContext* runtime_context_;
181   const scoped_refptr<ApplicationData> application_data_;
182   Runtime* main_runtime_;
183   std::set<Runtime*> runtimes_;
184   scoped_ptr<EventObserver> finish_observer_;
185   Observer* observer_;
186   // The entry point used as part of Launch().
187   LaunchEntryPoint entry_point_used_;
188   TerminationMode termination_mode_used_;
189   base::WeakPtrFactory<Application> weak_factory_;
190   std::map<std::string, std::string> name_perm_map_;
191   // Application's session permissions.
192   StoredPermissionMap permission_map_;
193   // Security policy set.
194   ScopedVector<SecurityPolicy> security_policy_;
195   bool is_security_mode_;
196
197   DISALLOW_COPY_AND_ASSIGN(Application);
198 };
199
200 }  // namespace application
201 }  // namespace xwalk
202
203 #endif  // XWALK_APPLICATION_BROWSER_APPLICATION_H_