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.
6 #ifndef XWALK_APPLICATION_BROWSER_APPLICATION_H_
7 #define XWALK_APPLICATION_BROWSER_APPLICATION_H_
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"
29 class RenderProcessHost;
36 namespace application {
38 class ApplicationHost;
40 class ApplicationSecurityPolicy;
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
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 {
51 virtual ~Application();
55 // Invoked when application is terminated - all its pages (runtimes)
57 virtual void OnApplicationTerminated(Application* app) {}
60 virtual ~Observer() {}
64 // Used only when running as service. Specifies the PID of the launcher
68 bool force_fullscreen;
69 bool remote_debugging;
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.
77 // NOTE: ApplicationService deletes an Application instance
78 // immediately after its termination.
81 const std::set<Runtime*>& runtimes() const { return runtimes_; }
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_; }
91 const ApplicationData* data() const { return data_.get(); }
92 ApplicationData* data() { return data_.get(); }
94 // Tells whether the application use the specified extension.
95 bool UseExtension(const std::string& extension_name) const;
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;
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;
114 void set_observer(Observer* observer) { observer_ = observer; }
116 RuntimeUIStrategy* ui_strategy() { return ui_strategy_.get(); }
119 Application(scoped_refptr<ApplicationData> data, RuntimeContext* context);
120 virtual bool Launch(const LaunchParams& launch_params);
121 virtual void InitSecurityPolicy();
123 // Runtime::Observer implementation.
124 virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
125 virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
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();
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_;
139 scoped_ptr<RuntimeUIStrategy> ui_strategy_;
140 xwalk::NativeAppWindow::CreateParams window_show_params_;
142 base::WeakPtr<Application> GetWeakPtr() {
143 return weak_factory_.GetWeakPtr();
147 // We enforce ApplicationService ownership.
148 friend class ApplicationService;
149 static scoped_ptr<Application> Create(scoped_refptr<ApplicationData> data,
150 RuntimeContext* context);
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;
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();
164 template <Manifest::Type>
165 ui::WindowShowState GetWindowShowState(const LaunchParams& params);
167 GURL GetAbsoluteURLFromKey(const std::string& key);
169 void NotifyTermination();
173 std::map<std::string, std::string> name_perm_map_;
174 // Application's session permissions.
175 StoredPermissionMap permission_map_;
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);
185 } // namespace application
188 #endif // XWALK_APPLICATION_BROWSER_APPLICATION_H_