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