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_
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/observer_list.h"
17 #include "ui/base/ui_base_types.h"
18 #include "xwalk/application/browser/event_observer.h"
19 #include "xwalk/application/common/application_data.h"
20 #include "xwalk/runtime/browser/runtime.h"
26 namespace application {
28 class ApplicationHost;
31 // The Application class is representing an active (running) application.
32 // Application instances are owned by ApplicationService.
33 // ApplicationService will delete an Application instance when it is
35 // There's one-to-one correspondence between Application and Render Process
36 // Host, obtained from its "runtimes" (pages).
37 class Application : public Runtime::Observer {
39 virtual ~Application();
43 // Invoked when application is terminated - all its pages (runtimes)
45 virtual void OnApplicationTerminated(Application* app) {}
48 virtual ~Observer() {}
51 // Manifest keys that can be used as application entry points.
52 enum LaunchEntryPoint {
53 AppMainKey = 1 << 0, // app.main
54 LaunchLocalPathKey = 1 << 1, // app.launch.local_path
55 // NOTE: The following key is only used for "dummy" hosted apps,
56 // which can be using any arbitrary URL, incl. remote ones.
57 // For now this should be disabled for all other cases as this will
58 // require special care with permissions etc.
59 URLKey = 1 << 2, // url
60 Default = AppMainKey | LaunchLocalPathKey
62 typedef unsigned LaunchEntryPoints;
66 entry_points(Default),
68 window_state(ui::SHOW_STATE_DEFAULT) {}
70 LaunchEntryPoints entry_points;
72 // Used only when running as service. Specifies the PID of the launcher
76 // Sets the initial state for the application windows.
77 ui::WindowShowState window_state;
80 // Closes all the application's runtimes (application pages).
81 // NOTE: Application is terminated asynchronously.
82 // Please use ApplicationService::Observer::WillDestroyApplication()
83 // interface to be notified about actual app termination.
85 // NOTE: ApplicationService deletes an Application instance
86 // immediately after its termination.
87 enum TerminationMode {
89 Immediate // Ignore OnSuspend event handler.
91 void Terminate(TerminationMode = Normal);
93 // Returns Runtime (application page) containing the application's
94 // 'main document'. The main document is the main entry point of
95 // the application to the system. This method will return 'NULL'
96 // if application has different entry point (local path manifest key).
97 // See http://anssiko.github.io/runtime/app-lifecycle.html#dfn-main-document
98 // The main document never has a visible window on its own.
99 Runtime* GetMainDocumentRuntime() const;
101 const std::set<Runtime*>& runtimes() const { return runtimes_; }
103 // Returns the unique application id which is used to distinguish the
104 // application amoung both running applications and installed ones
105 // (ApplicationData objects).
106 std::string id() const { return application_data_->ID(); }
107 int GetRenderProcessHostID() const;
109 const ApplicationData* data() const { return application_data_; }
110 ApplicationData* data() { return application_data_; }
112 // Tells whether the application use the specified extension.
113 bool UseExtension(const std::string& extension_name) const;
115 // The runtime permission mapping is registered by extension which
116 // implements some specific API, for example:
117 // "bluetooth" -> "bluetooth.read, bluetooth.write, bluetooth.management"
118 // Whenever there comes a API permission request, we can tell whether
119 // this API is registered, if yes, return the according permission name.
120 bool RegisterPermissions(const std::string& extension_name,
121 const std::string& perm_table);
122 std::string GetRegisteredPermissionName(const std::string& extension_name,
123 const std::string& api_name) const;
125 StoredPermission GetPermission(PermissionType type,
126 std::string& permission_name) const;
127 bool SetPermission(PermissionType type,
128 const std::string& permission_name,
129 StoredPermission perm);
132 bool HasMainDocument() const;
133 // Runtime::Observer implementation.
134 virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
135 virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
137 // We enforce ApplicationService ownership.
138 friend class ApplicationService;
139 Application(scoped_refptr<ApplicationData> data,
140 RuntimeContext* context,
142 bool Launch(const LaunchParams& launch_params);
144 // Try to extract the URL from different possible keys for entry points in the
145 // manifest, returns it and the entry point used.
146 GURL GetURLForLaunch(const LaunchParams& params, LaunchEntryPoint* used);
148 GURL GetURLFromAppMainKey();
149 GURL GetURLFromLocalPathKey();
150 GURL GetURLFromURLKey();
152 friend class FinishEventObserver;
153 void CloseMainDocument();
154 void NotifyTermination();
155 bool IsOnSuspendHandlerRegistered() const;
156 bool IsTerminating() const { return finish_observer_; }
158 void InitSecurityPolicy();
160 RuntimeContext* runtime_context_;
161 const scoped_refptr<ApplicationData> application_data_;
162 Runtime* main_runtime_;
163 std::set<Runtime*> runtimes_;
164 scoped_ptr<EventObserver> finish_observer_;
166 // The entry point used as part of Launch().
167 LaunchEntryPoint entry_point_used_;
168 TerminationMode termination_mode_used_;
169 base::WeakPtrFactory<Application> weak_factory_;
170 std::map<std::string, std::string> name_perm_map_;
171 // Application's session permissions.
172 StoredPermissionMap permission_map_;
174 DISALLOW_COPY_AND_ASSIGN(Application);
177 } // namespace application
180 #endif // XWALK_APPLICATION_BROWSER_APPLICATION_H_