Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application.h
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.
4
5 #ifndef XWALK_APPLICATION_BROWSER_APPLICATION_H_
6 #define XWALK_APPLICATION_BROWSER_APPLICATION_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
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"
21
22 namespace xwalk {
23
24 class RuntimeContext;
25
26 namespace application {
27
28 class ApplicationHost;
29 class Manifest;
30
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
34 // terminated.
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 {
38  public:
39   virtual ~Application();
40
41   class Observer {
42    public:
43     // Invoked when application is terminated - all its pages (runtimes)
44     // are closed.
45     virtual void OnApplicationTerminated(Application* app) {}
46
47    protected:
48     virtual ~Observer() {}
49   };
50
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
61   };
62   typedef unsigned LaunchEntryPoints;
63
64   struct LaunchParams {
65     LaunchParams() :
66         entry_points(Default),
67         launcher_pid(0),
68         window_state(ui::SHOW_STATE_DEFAULT) {}
69
70     LaunchEntryPoints entry_points;
71
72     // Used only when running as service. Specifies the PID of the launcher
73     // process.
74     int32 launcher_pid;
75
76     // Sets the initial state for the application windows.
77     ui::WindowShowState window_state;
78   };
79
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.
84   //
85   // NOTE: ApplicationService deletes an Application instance
86   // immediately after its termination.
87   enum TerminationMode {
88     Normal,
89     Immediate  // Ignore OnSuspend event handler.
90   };
91   void Terminate(TerminationMode = Normal);
92
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;
100
101   const std::set<Runtime*>& runtimes() const { return runtimes_; }
102
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;
108
109   const ApplicationData* data() const { return application_data_; }
110   ApplicationData* data() { return application_data_; }
111
112   // Tells whether the application use the specified extension.
113   bool UseExtension(const std::string& extension_name) const;
114
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;
124
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);
130
131  private:
132   bool HasMainDocument() const;
133   // Runtime::Observer implementation.
134   virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
135   virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
136
137   // We enforce ApplicationService ownership.
138   friend class ApplicationService;
139   Application(scoped_refptr<ApplicationData> data,
140               RuntimeContext* context,
141               Observer* observer);
142   bool Launch(const LaunchParams& launch_params);
143
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);
147
148   GURL GetURLFromAppMainKey();
149   GURL GetURLFromLocalPathKey();
150   GURL GetURLFromURLKey();
151
152   friend class FinishEventObserver;
153   void CloseMainDocument();
154   void NotifyTermination();
155   bool IsOnSuspendHandlerRegistered() const;
156   bool IsTerminating() const { return finish_observer_; }
157
158   void InitSecurityPolicy();
159
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_;
165   Observer* 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_;
173
174   DISALLOW_COPY_AND_ASSIGN(Application);
175 };
176
177 }  // namespace application
178 }  // namespace xwalk
179
180 #endif  // XWALK_APPLICATION_BROWSER_APPLICATION_H_