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 "ui/base/ui_base_types.h"
19 #include "xwalk/application/browser/event_observer.h"
20 #include "xwalk/application/common/application_data.h"
21 #include "xwalk/application/common/security_policy.h"
22 #include "xwalk/runtime/browser/runtime.h"
24 #if defined(USE_OZONE) && defined(OS_TIZEN)
25 #include "base/message_loop/message_pump_observer.h"
32 namespace application {
34 class ApplicationHost;
38 // The Application class is representing an active (running) application.
39 // Application instances are owned by ApplicationService.
40 // ApplicationService will delete an Application instance when it is
42 // There's one-to-one correspondence between Application and Render Process
43 // Host, obtained from its "runtimes" (pages).
46 #if defined(USE_OZONE) && defined(OS_TIZEN)
47 public base::MessagePumpObserver,
49 public Runtime::Observer {
51 virtual ~Application();
55 // Invoked when application is terminated - all its pages (runtimes)
57 virtual void OnApplicationTerminated(Application* app) {}
60 virtual ~Observer() {}
63 // Manifest keys that can be used as application entry points.
64 enum LaunchEntryPoint {
65 AppMainKey = 1 << 0, // app.main
66 LaunchLocalPathKey = 1 << 1, // app.launch.local_path
67 URLKey = 1 << 2, // url
68 Default = AppMainKey | LaunchLocalPathKey | URLKey
70 typedef unsigned LaunchEntryPoints;
74 entry_points(Default),
76 force_fullscreen(false) {}
78 LaunchEntryPoints entry_points;
80 // Used only when running as service. Specifies the PID of the launcher
84 bool force_fullscreen;
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.
94 enum TerminationMode {
96 Immediate // Ignore OnSuspend event handler.
98 void Terminate(TerminationMode = Normal);
100 #if defined(OS_TIZEN)
104 // Returns Runtime (application page) containing the application's
105 // 'main document'. The main document is the main entry point of
106 // the application to the system. This method will return 'NULL'
107 // if application has different entry point (local path manifest key).
108 // See http://anssiko.github.io/runtime/app-lifecycle.html#dfn-main-document
109 // The main document never has a visible window on its own.
110 Runtime* GetMainDocumentRuntime() const;
112 const std::set<Runtime*>& runtimes() const { return runtimes_; }
114 // Returns the unique application id which is used to distinguish the
115 // application amoung both running applications and installed ones
116 // (ApplicationData objects).
117 std::string id() const { return application_data_->ID(); }
118 int GetRenderProcessHostID() const;
120 const ApplicationData* data() const { return application_data_; }
121 ApplicationData* data() { return application_data_; }
123 // Tells whether the application use the specified extension.
124 bool UseExtension(const std::string& extension_name) const;
126 // The runtime permission mapping is registered by extension which
127 // implements some specific API, for example:
128 // "bluetooth" -> "bluetooth.read, bluetooth.write, bluetooth.management"
129 // Whenever there comes a API permission request, we can tell whether
130 // this API is registered, if yes, return the according permission name.
131 bool RegisterPermissions(const std::string& extension_name,
132 const std::string& perm_table);
133 std::string GetRegisteredPermissionName(const std::string& extension_name,
134 const std::string& api_name) const;
136 StoredPermission GetPermission(PermissionType type,
137 std::string& permission_name) const;
138 bool SetPermission(PermissionType type,
139 const std::string& permission_name,
140 StoredPermission perm);
141 bool CanRequestURL(const GURL& url) const;
144 bool HasMainDocument() const;
145 // Runtime::Observer implementation.
146 virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
147 virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
149 // We enforce ApplicationService ownership.
150 friend class ApplicationService;
151 Application(scoped_refptr<ApplicationData> data,
152 RuntimeContext* context,
154 bool Launch(const LaunchParams& launch_params);
156 // Try to extract the URL from different possible keys for entry points in the
157 // manifest, returns it and the entry point used.
158 GURL GetStartURL(const LaunchParams& params, LaunchEntryPoint* used);
159 ui::WindowShowState GetWindowShowState(const LaunchParams& params);
161 GURL GetURLFromAppMainKey();
162 GURL GetURLFromLocalPathKey();
163 GURL GetURLFromURLKey();
165 friend class FinishEventObserver;
166 void CloseMainDocument();
167 void NotifyTermination();
168 bool IsOnSuspendHandlerRegistered() const;
169 bool IsTerminating() const { return finish_observer_; }
171 void InitSecurityPolicy();
172 void AddSecurityPolicy(const GURL& url, bool subdomains);
174 #if defined(USE_OZONE) && defined(OS_TIZEN)
175 virtual base::EventStatus WillProcessEvent(
176 const base::NativeEvent& event) OVERRIDE;
177 virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE;
180 RuntimeContext* runtime_context_;
181 const scoped_refptr<ApplicationData> application_data_;
182 Runtime* main_runtime_;
183 std::set<Runtime*> runtimes_;
184 scoped_ptr<EventObserver> finish_observer_;
186 // The entry point used as part of Launch().
187 LaunchEntryPoint entry_point_used_;
188 TerminationMode termination_mode_used_;
189 base::WeakPtrFactory<Application> weak_factory_;
190 std::map<std::string, std::string> name_perm_map_;
191 // Application's session permissions.
192 StoredPermissionMap permission_map_;
193 // Security policy set.
194 ScopedVector<SecurityPolicy> security_policy_;
195 bool is_security_mode_;
197 DISALLOW_COPY_AND_ASSIGN(Application);
200 } // namespace application
203 #endif // XWALK_APPLICATION_BROWSER_APPLICATION_H_