Upstream version 8.36.169.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/files/file_path.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_vector.h"
18 #include "base/observer_list.h"
19 #include "content/public/browser/render_process_host_observer.h"
20 #include "ui/base/ui_base_types.h"
21 #include "xwalk/application/common/application_data.h"
22 #include "xwalk/application/common/security_policy.h"
23 #include "xwalk/runtime/browser/runtime.h"
24
25
26 namespace content {
27   class RenderProcessHost;
28 }
29
30 namespace xwalk {
31
32 class RuntimeContext;
33
34 namespace application {
35
36 class ApplicationHost;
37 class Manifest;
38 class SecurityPolicy;
39
40 // The Application class is representing an active (running) application.
41 // Application instances are owned by ApplicationService.
42 // ApplicationService will delete an Application instance when it is
43 // terminated.
44 // There's one-to-one correspondence between Application and Render Process
45 // Host, obtained from its "runtimes" (pages).
46 class Application : public Runtime::Observer,
47                     public content::RenderProcessHostObserver {
48  public:
49   virtual ~Application();
50
51   class Observer {
52    public:
53     // Invoked when application is terminated - all its pages (runtimes)
54     // are closed.
55     virtual void OnApplicationTerminated(Application* app) {}
56
57    protected:
58     virtual ~Observer() {}
59   };
60
61   // Manifest keys that can be used as application entry points.
62   enum LaunchEntryPoint {
63     StartURLKey = 1 << 0,  // start_url
64     LaunchLocalPathKey = 1 << 1,  // app.launch.local_path
65     URLKey = 1 << 2,  // url
66     Default = StartURLKey | LaunchLocalPathKey
67   };
68   typedef unsigned LaunchEntryPoints;
69
70   struct LaunchParams {
71     LaunchParams() :
72         entry_points(Default),
73         launcher_pid(0),
74         force_fullscreen(false) {}
75
76     LaunchEntryPoints entry_points;
77
78     // Used only when running as service. Specifies the PID of the launcher
79     // process.
80     int32 launcher_pid;
81
82     bool force_fullscreen;
83   };
84
85   // Closes all the application's runtimes (application pages).
86   // NOTE: Application is terminated asynchronously.
87   // Please use ApplicationService::Observer::WillDestroyApplication()
88   // interface to be notified about actual app termination.
89   //
90   // NOTE: ApplicationService deletes an Application instance
91   // immediately after its termination.
92   void Terminate();
93
94   const std::set<Runtime*>& runtimes() const { return runtimes_; }
95
96   // Returns the unique application id which is used to distinguish the
97   // application amoung both running applications and installed ones
98   // (ApplicationData objects).
99   std::string id() const { return data_->ID(); }
100   int GetRenderProcessHostID() const;
101   content::RenderProcessHost* render_process_host() {
102     return render_process_host_; }
103
104   const ApplicationData* data() const { return data_; }
105   ApplicationData* data() { return data_; }
106
107   // Tells whether the application use the specified extension.
108   bool UseExtension(const std::string& extension_name) const;
109
110   // The runtime permission mapping is registered by extension which
111   // implements some specific API, for example:
112   // "bluetooth" -> "bluetooth.read, bluetooth.write, bluetooth.management"
113   // Whenever there comes a API permission request, we can tell whether
114   // this API is registered, if yes, return the according permission name.
115   bool RegisterPermissions(const std::string& extension_name,
116                            const std::string& perm_table);
117   std::string GetRegisteredPermissionName(const std::string& extension_name,
118                                           const std::string& api_name) const;
119
120   StoredPermission GetPermission(PermissionType type,
121                                  const std::string& permission_name) const;
122   bool SetPermission(PermissionType type,
123                      const std::string& permission_name,
124                      StoredPermission perm);
125   bool CanRequestURL(const GURL& url) const;
126
127  protected:
128   // We enforce ApplicationService ownership.
129   friend class ApplicationService;
130   Application(scoped_refptr<ApplicationData> data,
131               RuntimeContext* context,
132               Observer* observer);
133   virtual bool Launch(const LaunchParams& launch_params);
134   virtual void InitSecurityPolicy();
135
136   // Get the path of splash screen image. Return empty path by default.
137   // Sub class can override it to return a specific path.
138   virtual base::FilePath GetSplashScreenPath();
139
140   std::set<Runtime*> runtimes_;
141   scoped_refptr<ApplicationData> const data_;
142   // The application's render process host.
143   content::RenderProcessHost* render_process_host_;
144   bool security_mode_enabled_;
145
146   base::WeakPtr<Application> GetWeakPtr() {
147     return weak_factory_.GetWeakPtr();
148   }
149
150  private:
151   // Runtime::Observer implementation.
152   virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
153   virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
154
155   // content::RenderProcessHostObserver implementation.
156   virtual void RenderProcessExited(content::RenderProcessHost* host,
157                                    base::ProcessHandle handle,
158                                    base::TerminationStatus status,
159                                    int exit_code) OVERRIDE;
160   virtual void RenderProcessHostDestroyed(
161       content::RenderProcessHost* host) OVERRIDE;
162
163   // Try to extract the URL from different possible keys for entry points in the
164   // manifest, returns it and the entry point used.
165   GURL GetStartURL(const LaunchParams& params, LaunchEntryPoint* used);
166   ui::WindowShowState GetWindowShowStateWGT(const LaunchParams& params);
167   ui::WindowShowState GetWindowShowStateXPK(const LaunchParams& params);
168
169   GURL GetURLFromURLKey();
170
171   GURL GetURLFromRelativePathKey(const std::string& key);
172
173   void NotifyTermination();
174
175   RuntimeContext* runtime_context_;
176   Observer* observer_;
177   // The entry point used as part of Launch().
178   LaunchEntryPoint entry_point_used_;
179   std::map<std::string, std::string> name_perm_map_;
180   // Application's session permissions.
181   StoredPermissionMap permission_map_;
182   // Security policy.
183   scoped_ptr<SecurityPolicy> security_policy_;
184   // WeakPtrFactory should be always declared the last.
185   base::WeakPtrFactory<Application> weak_factory_;
186   DISALLOW_COPY_AND_ASSIGN(Application);
187 };
188
189 }  // namespace application
190 }  // namespace xwalk
191
192 #endif  // XWALK_APPLICATION_BROWSER_APPLICATION_H_