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.
5 #ifndef XWALK_APPLICATION_BROWSER_APPLICATION_H_
6 #define XWALK_APPLICATION_BROWSER_APPLICATION_H_
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"
27 class RenderProcessHost;
34 namespace application {
36 class ApplicationHost;
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
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 {
49 virtual ~Application();
53 // Invoked when application is terminated - all its pages (runtimes)
55 virtual void OnApplicationTerminated(Application* app) {}
58 virtual ~Observer() {}
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
68 typedef unsigned LaunchEntryPoints;
72 entry_points(Default),
74 force_fullscreen(false),
75 remote_debugging(false) {}
77 LaunchEntryPoints entry_points;
79 // Used only when running as service. Specifies the PID of the launcher
83 bool force_fullscreen;
84 bool remote_debugging;
87 // Closes all the application's runtimes (application pages).
88 // NOTE: Application is terminated asynchronously.
89 // Please use ApplicationService::Observer::WillDestroyApplication()
90 // interface to be notified about actual app termination.
92 // NOTE: ApplicationService deletes an Application instance
93 // immediately after its termination.
96 const std::set<Runtime*>& runtimes() const { return runtimes_; }
98 // Returns the unique application id which is used to distinguish the
99 // application amoung both running applications and installed ones
100 // (ApplicationData objects).
101 std::string id() const { return data_->ID(); }
102 int GetRenderProcessHostID() const;
103 content::RenderProcessHost* render_process_host() {
104 return render_process_host_; }
106 const ApplicationData* data() const { return data_; }
107 ApplicationData* data() { return data_; }
109 // Tells whether the application use the specified extension.
110 bool UseExtension(const std::string& extension_name) const;
112 // The runtime permission mapping is registered by extension which
113 // implements some specific API, for example:
114 // "bluetooth" -> "bluetooth.read, bluetooth.write, bluetooth.management"
115 // Whenever there comes a API permission request, we can tell whether
116 // this API is registered, if yes, return the according permission name.
117 bool RegisterPermissions(const std::string& extension_name,
118 const std::string& perm_table);
119 std::string GetRegisteredPermissionName(const std::string& extension_name,
120 const std::string& api_name) const;
122 StoredPermission GetPermission(PermissionType type,
123 const std::string& permission_name) const;
124 bool SetPermission(PermissionType type,
125 const std::string& permission_name,
126 StoredPermission perm);
127 bool CanRequestURL(const GURL& url) const;
130 // We enforce ApplicationService ownership.
131 friend class ApplicationService;
132 Application(scoped_refptr<ApplicationData> data,
133 RuntimeContext* context,
135 virtual bool Launch(const LaunchParams& launch_params);
136 virtual void InitSecurityPolicy();
138 // Get the path of splash screen image. Return empty path by default.
139 // Sub class can override it to return a specific path.
140 virtual base::FilePath GetSplashScreenPath();
142 std::set<Runtime*> runtimes_;
143 scoped_refptr<ApplicationData> const data_;
144 // The application's render process host.
145 content::RenderProcessHost* render_process_host_;
146 content::WebContents* web_contents_;
147 bool security_mode_enabled_;
149 base::WeakPtr<Application> GetWeakPtr() {
150 return weak_factory_.GetWeakPtr();
154 // Runtime::Observer implementation.
155 virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
156 virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
158 // content::RenderProcessHostObserver implementation.
159 virtual void RenderProcessExited(content::RenderProcessHost* host,
160 base::ProcessHandle handle,
161 base::TerminationStatus status,
162 int exit_code) OVERRIDE;
163 virtual void RenderProcessHostDestroyed(
164 content::RenderProcessHost* host) OVERRIDE;
166 // Try to extract the URL from different possible keys for entry points in the
167 // manifest, returns it and the entry point used.
168 GURL GetStartURL(const LaunchParams& params, LaunchEntryPoint* used);
169 ui::WindowShowState GetWindowShowStateWGT(const LaunchParams& params);
170 ui::WindowShowState GetWindowShowStateXPK(const LaunchParams& params);
172 GURL GetAbsoluteURLFromKey(const std::string& key);
174 void NotifyTermination();
176 RuntimeContext* runtime_context_;
178 // The entry point used as part of Launch().
179 LaunchEntryPoint entry_point_used_;
180 std::map<std::string, std::string> name_perm_map_;
181 // Application's session permissions.
182 StoredPermissionMap permission_map_;
184 scoped_ptr<SecurityPolicy> security_policy_;
185 // Remote debugging enabled or not for this Application
186 bool remote_debugging_enabled_;
187 // WeakPtrFactory should be always declared the last.
188 base::WeakPtrFactory<Application> weak_factory_;
189 DISALLOW_COPY_AND_ASSIGN(Application);
192 } // namespace application
195 #endif // XWALK_APPLICATION_BROWSER_APPLICATION_H_