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