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/common/application_data.h"
23 #include "xwalk/application/common/security_policy.h"
24 #include "xwalk/runtime/browser/runtime.h"
28 class RenderProcessHost;
35 namespace application {
37 class ApplicationHost;
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
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 {
50 virtual ~Application();
54 // Invoked when application is terminated - all its pages (runtimes)
56 virtual void OnApplicationTerminated(Application* app) {}
59 virtual ~Observer() {}
63 // Used only when running as service. Specifies the PID of the launcher
67 bool force_fullscreen;
68 bool remote_debugging;
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.
76 // NOTE: ApplicationService deletes an Application instance
77 // immediately after its termination.
80 const std::set<Runtime*>& runtimes() const { return runtimes_; }
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_; }
90 const ApplicationData* data() const { return data_.get(); }
91 ApplicationData* data() { return data_.get(); }
93 // Tells whether the application use the specified extension.
94 bool UseExtension(const std::string& extension_name) const;
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;
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;
113 void set_observer(Observer* observer) { observer_ = observer; }
116 Application(scoped_refptr<ApplicationData> data, RuntimeContext* context);
117 virtual bool Launch(const LaunchParams& launch_params);
118 virtual void InitSecurityPolicy();
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();
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_;
132 base::WeakPtr<Application> GetWeakPtr() {
133 return weak_factory_.GetWeakPtr();
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;
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 template <Manifest::Type> GURL GetStartURL();
157 template <Manifest::Type>
158 ui::WindowShowState GetWindowShowState(const LaunchParams& params);
160 GURL GetAbsoluteURLFromKey(const std::string& key);
162 void NotifyTermination();
166 std::map<std::string, std::string> name_perm_map_;
167 // Application's session permissions.
168 StoredPermissionMap permission_map_;
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);
178 } // namespace application
181 #endif // XWALK_APPLICATION_BROWSER_APPLICATION_H_