365b63e9957add6d767b155136791e890ed95f6f
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application.h
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #ifndef XWALK_APPLICATION_BROWSER_APPLICATION_H_
7 #define XWALK_APPLICATION_BROWSER_APPLICATION_H_
8
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
13
14 #include "base/callback.h"
15 #include "base/compiler_specific.h"
16 #include "base/files/file_path.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_vector.h"
19 #include "base/observer_list.h"
20 #include "content/public/browser/render_process_host_observer.h"
21 #include "ui/base/ui_base_types.h"
22 #include "xwalk/application/common/application_data.h"
23 #include "xwalk/application/common/security_policy.h"
24 #include "xwalk/runtime/browser/runtime.h"
25
26
27 namespace content {
28 class RenderProcessHost;
29 }
30
31 namespace xwalk {
32
33 class RuntimeContext;
34
35 namespace application {
36
37 class ApplicationHost;
38 class Manifest;
39 class SecurityPolicy;
40
41 // The Application class is representing an active (running) application.
42 // Application instances are owned by ApplicationService.
43 // ApplicationService will delete an Application instance when it is
44 // terminated.
45 // There's one-to-one correspondence between Application and Render Process
46 // Host, obtained from its "runtimes" (pages).
47 class Application : public Runtime::Observer,
48                     public content::RenderProcessHostObserver {
49  public:
50   virtual ~Application();
51
52   class Observer {
53    public:
54     // Invoked when application is terminated - all its pages (runtimes)
55     // are closed.
56     virtual void OnApplicationTerminated(Application* app) {}
57
58    protected:
59     virtual ~Observer() {}
60   };
61
62   struct LaunchParams {
63     // Used only when running as service. Specifies the PID of the launcher
64     // process.
65     int32 launcher_pid;
66
67     bool force_fullscreen;
68     bool remote_debugging;
69   };
70
71   // Closes all the application's runtimes (application pages).
72   // NOTE: Application is terminated asynchronously.
73   // Please use ApplicationService::Observer::WillDestroyApplication()
74   // interface to be notified about actual app termination.
75   //
76   // NOTE: ApplicationService deletes an Application instance
77   // immediately after its termination.
78   void Terminate();
79
80   const std::set<Runtime*>& runtimes() const { return runtimes_; }
81
82   // Returns the unique application id which is used to distinguish the
83   // application amoung both running applications and installed ones
84   // (ApplicationData objects).
85   std::string id() const { return data_->ID(); }
86   int GetRenderProcessHostID() const;
87   content::RenderProcessHost* render_process_host() {
88     return render_process_host_; }
89
90   const ApplicationData* data() const { return data_.get(); }
91   ApplicationData* data() { return data_.get(); }
92
93   // Tells whether the application use the specified extension.
94   bool UseExtension(const std::string& extension_name) const;
95
96   // The runtime permission mapping is registered by extension which
97   // implements some specific API, for example:
98   // "bluetooth" -> "bluetooth.read, bluetooth.write, bluetooth.management"
99   // Whenever there comes a API permission request, we can tell whether
100   // this API is registered, if yes, return the according permission name.
101   bool RegisterPermissions(const std::string& extension_name,
102                            const std::string& perm_table);
103   std::string GetRegisteredPermissionName(const std::string& extension_name,
104                                           const std::string& api_name) const;
105
106   StoredPermission GetPermission(PermissionType type,
107                                  const std::string& permission_name) const;
108   bool SetPermission(PermissionType type,
109                      const std::string& permission_name,
110                      StoredPermission perm);
111   bool CanRequestURL(const GURL& url) const;
112
113   void set_observer(Observer* observer) { observer_ = observer; }
114
115  protected:
116   Application(scoped_refptr<ApplicationData> data, RuntimeContext* context);
117   virtual bool Launch(const LaunchParams& launch_params);
118   virtual void InitSecurityPolicy();
119
120   // Get the path of splash screen image. Return empty path by default.
121   // Sub class can override it to return a specific path.
122   virtual base::FilePath GetSplashScreenPath();
123
124   std::set<Runtime*> runtimes_;
125   RuntimeContext* runtime_context_;
126   scoped_refptr<ApplicationData> const data_;
127   // The application's render process host.
128   content::RenderProcessHost* render_process_host_;
129   content::WebContents* web_contents_;
130   bool security_mode_enabled_;
131
132   base::WeakPtr<Application> GetWeakPtr() {
133     return weak_factory_.GetWeakPtr();
134   }
135
136  private:
137   // We enforce ApplicationService ownership.
138   friend class ApplicationService;
139   static scoped_ptr<Application> Create(scoped_refptr<ApplicationData> data,
140       RuntimeContext* context);
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   template <Manifest::Type> GURL GetStartURL();
156
157   template <Manifest::Type>
158   ui::WindowShowState GetWindowShowState(const LaunchParams& params);
159
160   GURL GetAbsoluteURLFromKey(const std::string& key);
161
162   void NotifyTermination();
163
164   Observer* observer_;
165
166   std::map<std::string, std::string> name_perm_map_;
167   // Application's session permissions.
168   StoredPermissionMap permission_map_;
169   // Security policy.
170   scoped_ptr<SecurityPolicy> security_policy_;
171   // Remote debugging enabled or not for this Application
172   bool remote_debugging_enabled_;
173   // WeakPtrFactory should be always declared the last.
174   base::WeakPtrFactory<Application> weak_factory_;
175   DISALLOW_COPY_AND_ASSIGN(Application);
176 };
177
178 }  // namespace application
179 }  // namespace xwalk
180
181 #endif  // XWALK_APPLICATION_BROWSER_APPLICATION_H_