Upstream version 9.38.207.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   struct LaunchParams {
62     // Used only when running as service. Specifies the PID of the launcher
63     // process.
64     int32 launcher_pid;
65
66     bool force_fullscreen;
67     bool remote_debugging;
68   };
69
70   // Closes all the application's runtimes (application pages).
71   // NOTE: Application is terminated asynchronously.
72   // Please use ApplicationService::Observer::WillDestroyApplication()
73   // interface to be notified about actual app termination.
74   //
75   // NOTE: ApplicationService deletes an Application instance
76   // immediately after its termination.
77   void Terminate();
78
79   const std::set<Runtime*>& runtimes() const { return runtimes_; }
80
81   // Returns the unique application id which is used to distinguish the
82   // application amoung both running applications and installed ones
83   // (ApplicationData objects).
84   std::string id() const { return data_->ID(); }
85   int GetRenderProcessHostID() const;
86   content::RenderProcessHost* render_process_host() {
87     return render_process_host_; }
88
89   const ApplicationData* data() const { return data_; }
90   ApplicationData* data() { return data_; }
91
92   // Tells whether the application use the specified extension.
93   bool UseExtension(const std::string& extension_name) const;
94
95   // The runtime permission mapping is registered by extension which
96   // implements some specific API, for example:
97   // "bluetooth" -> "bluetooth.read, bluetooth.write, bluetooth.management"
98   // Whenever there comes a API permission request, we can tell whether
99   // this API is registered, if yes, return the according permission name.
100   bool RegisterPermissions(const std::string& extension_name,
101                            const std::string& perm_table);
102   std::string GetRegisteredPermissionName(const std::string& extension_name,
103                                           const std::string& api_name) const;
104
105   StoredPermission GetPermission(PermissionType type,
106                                  const std::string& permission_name) const;
107   bool SetPermission(PermissionType type,
108                      const std::string& permission_name,
109                      StoredPermission perm);
110   bool CanRequestURL(const GURL& url) const;
111
112   void set_observer(Observer* observer) { observer_ = observer; }
113
114  protected:
115   Application(scoped_refptr<ApplicationData> data, RuntimeContext* context);
116   virtual bool Launch(const LaunchParams& launch_params);
117   virtual void InitSecurityPolicy();
118
119   // Get the path of splash screen image. Return empty path by default.
120   // Sub class can override it to return a specific path.
121   virtual base::FilePath GetSplashScreenPath();
122
123   std::set<Runtime*> runtimes_;
124   scoped_refptr<ApplicationData> const data_;
125   // The application's render process host.
126   content::RenderProcessHost* render_process_host_;
127   content::WebContents* web_contents_;
128   bool security_mode_enabled_;
129
130   base::WeakPtr<Application> GetWeakPtr() {
131     return weak_factory_.GetWeakPtr();
132   }
133
134  private:
135   // We enforce ApplicationService ownership.
136   friend class ApplicationService;
137   static scoped_ptr<Application> Create(scoped_refptr<ApplicationData> data,
138       RuntimeContext* context);
139   // Runtime::Observer implementation.
140   virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
141   virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
142
143   // content::RenderProcessHostObserver implementation.
144   virtual void RenderProcessExited(content::RenderProcessHost* host,
145                                    base::ProcessHandle handle,
146                                    base::TerminationStatus status,
147                                    int exit_code) OVERRIDE;
148   virtual void RenderProcessHostDestroyed(
149       content::RenderProcessHost* host) OVERRIDE;
150
151   // Try to extract the URL from different possible keys for entry points in the
152   // manifest, returns it and the entry point used.
153   template <Manifest::Type> GURL GetStartURL();
154
155   template <Manifest::Type>
156   ui::WindowShowState GetWindowShowState(const LaunchParams& params);
157
158   GURL GetAbsoluteURLFromKey(const std::string& key);
159
160   void NotifyTermination();
161
162   RuntimeContext* runtime_context_;
163   Observer* observer_;
164
165   std::map<std::string, std::string> name_perm_map_;
166   // Application's session permissions.
167   StoredPermissionMap permission_map_;
168   // Security policy.
169   scoped_ptr<SecurityPolicy> security_policy_;
170   // Remote debugging enabled or not for this Application
171   bool remote_debugging_enabled_;
172   // WeakPtrFactory should be always declared the last.
173   base::WeakPtrFactory<Application> weak_factory_;
174   DISALLOW_COPY_AND_ASSIGN(Application);
175 };
176
177 }  // namespace application
178 }  // namespace xwalk
179
180 #endif  // XWALK_APPLICATION_BROWSER_APPLICATION_H_