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/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"
26 class RenderProcessHost;
33 namespace application {
35 class ApplicationHost;
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
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 {
48 virtual ~Application();
52 // Invoked when application is terminated - all its pages (runtimes)
54 virtual void OnApplicationTerminated(Application* app) {}
57 virtual ~Observer() {}
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
67 typedef unsigned LaunchEntryPoints;
71 entry_points(Default),
73 force_fullscreen(false) {}
75 LaunchEntryPoints entry_points;
77 // Used only when running as service. Specifies the PID of the launcher
81 bool force_fullscreen;
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.
89 // NOTE: ApplicationService deletes an Application instance
90 // immediately after its termination.
93 const std::set<Runtime*>& runtimes() const { return runtimes_; }
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;
101 const ApplicationData* data() const { return data_; }
102 ApplicationData* data() { return data_; }
104 // Tells whether the application use the specified extension.
105 bool UseExtension(const std::string& extension_name) const;
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;
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;
125 // We enforce ApplicationService ownership.
126 friend class ApplicationService;
127 Application(scoped_refptr<ApplicationData> data,
128 RuntimeContext* context,
130 virtual bool Launch(const LaunchParams& launch_params);
131 virtual void InitSecurityPolicy();
132 void AddSecurityPolicy(const GURL& url, bool subdomains);
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_;
141 // Runtime::Observer implementation.
142 virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
143 virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
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;
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);
159 GURL GetURLFromURLKey();
161 GURL GetURLFromRelativePathKey(const std::string& key);
163 void NotifyTermination();
165 RuntimeContext* runtime_context_;
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);
179 } // namespace application
182 #endif // XWALK_APPLICATION_BROWSER_APPLICATION_H_