Upstream version 8.36.156.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application.h
index 6277d47..0434150 100644 (file)
@@ -8,16 +8,24 @@
 #include <map>
 #include <set>
 #include <string>
+#include <vector>
 
 #include "base/callback.h"
 #include "base/compiler_specific.h"
 #include "base/memory/ref_counted.h"
 #include "base/memory/scoped_vector.h"
 #include "base/observer_list.h"
-#include "xwalk/application/browser/event_observer.h"
+#include "content/public/browser/render_process_host_observer.h"
+#include "ui/base/ui_base_types.h"
 #include "xwalk/application/common/application_data.h"
+#include "xwalk/application/common/security_policy.h"
 #include "xwalk/runtime/browser/runtime.h"
 
+
+namespace content {
+  class RenderProcessHost;
+}
+
 namespace xwalk {
 
 class RuntimeContext;
@@ -26,6 +34,7 @@ namespace application {
 
 class ApplicationHost;
 class Manifest;
+class SecurityPolicy;
 
 // The Application class is representing an active (running) application.
 // Application instances are owned by ApplicationService.
@@ -33,7 +42,8 @@ class Manifest;
 // terminated.
 // There's one-to-one correspondence between Application and Render Process
 // Host, obtained from its "runtimes" (pages).
-class Application : public Runtime::Observer {
+class Application : public Runtime::Observer,
+                    public content::RenderProcessHostObserver {
  public:
   virtual ~Application();
 
@@ -49,66 +59,125 @@ class Application : public Runtime::Observer {
 
   // Manifest keys that can be used as application entry points.
   enum LaunchEntryPoint {
-    AppMainKey = 1 << 0,  // app.main
+    StartURLKey = 1 << 0,  // start_url
     LaunchLocalPathKey = 1 << 1,  // app.launch.local_path
-    LaunchWebURLKey = 1 << 2,  // app.launch.web_url
-    Default = AppMainKey | LaunchLocalPathKey
+    URLKey = 1 << 2,  // url
+    Default = StartURLKey | LaunchLocalPathKey
   };
   typedef unsigned LaunchEntryPoints;
 
-  LaunchEntryPoints entry_points() const { return entry_points_; }
+  struct LaunchParams {
+    LaunchParams() :
+        entry_points(Default),
+        launcher_pid(0),
+        force_fullscreen(false) {}
 
-  // Closes all the application's runtimes (application pages).
-  void Close();
+    LaunchEntryPoints entry_points;
+
+    // Used only when running as service. Specifies the PID of the launcher
+    // process.
+    int32 launcher_pid;
+
+    bool force_fullscreen;
+  };
 
-  // Returns Runtime (application page) containing the application's
-  // 'main document'. The main document is the main entry point of
-  // the application to the system. This method will return 'NULL'
-  // if application has different entry point (local path manifest key).
-  // See http://anssiko.github.io/runtime/app-lifecycle.html#dfn-main-document
-  // The main document never has a visible window on its own.
-  Runtime* GetMainDocumentRuntime() const;
+  // Closes all the application's runtimes (application pages).
+  // NOTE: Application is terminated asynchronously.
+  // Please use ApplicationService::Observer::WillDestroyApplication()
+  // interface to be notified about actual app termination.
+  //
+  // NOTE: ApplicationService deletes an Application instance
+  // immediately after its termination.
+  void Terminate();
 
   const std::set<Runtime*>& runtimes() const { return runtimes_; }
 
   // Returns the unique application id which is used to distinguish the
   // application amoung both running applications and installed ones
   // (ApplicationData objects).
-  std::string id() const { return application_data_->ID(); }
-  bool HasMainDocument() const { return application_data_->HasMainDocument(); }
+  std::string id() const { return data_->ID(); }
   int GetRenderProcessHostID() const;
+  content::RenderProcessHost* render_process_host() {
+    return render_process_host_; }
+
+  const ApplicationData* data() const { return data_; }
+  ApplicationData* data() { return data_; }
+
+  // Tells whether the application use the specified extension.
+  bool UseExtension(const std::string& extension_name) const;
+
+  // The runtime permission mapping is registered by extension which
+  // implements some specific API, for example:
+  // "bluetooth" -> "bluetooth.read, bluetooth.write, bluetooth.management"
+  // Whenever there comes a API permission request, we can tell whether
+  // this API is registered, if yes, return the according permission name.
+  bool RegisterPermissions(const std::string& extension_name,
+                           const std::string& perm_table);
+  std::string GetRegisteredPermissionName(const std::string& extension_name,
+                                          const std::string& api_name) const;
+
+  StoredPermission GetPermission(PermissionType type,
+                                 const std::string& permission_name) const;
+  bool SetPermission(PermissionType type,
+                     const std::string& permission_name,
+                     StoredPermission perm);
+  bool CanRequestURL(const GURL& url) const;
+
+ protected:
+  // We enforce ApplicationService ownership.
+  friend class ApplicationService;
+  Application(scoped_refptr<ApplicationData> data,
+              RuntimeContext* context,
+              Observer* observer);
+  virtual bool Launch(const LaunchParams& launch_params);
+  virtual void InitSecurityPolicy();
 
-  const ApplicationData* data() const { return application_data_; }
-  ApplicationData* data() { return application_data_; }
+  std::set<Runtime*> runtimes_;
+  scoped_refptr<ApplicationData> const data_;
+  // The application's render process host.
+  content::RenderProcessHost* render_process_host_;
+  bool security_mode_enabled_;
+
+  base::WeakPtr<Application> GetWeakPtr() {
+    return weak_factory_.GetWeakPtr();
+  }
 
  private:
   // Runtime::Observer implementation.
   virtual void OnRuntimeAdded(Runtime* runtime) OVERRIDE;
   virtual void OnRuntimeRemoved(Runtime* runtime) OVERRIDE;
 
-  // We enforce ApplicationService ownership.
-  friend class ApplicationService;
-  Application(scoped_refptr<ApplicationData> data,
-              RuntimeContext* context,
-              Observer* observer);
-  bool Launch();
+  // content::RenderProcessHostObserver implementation.
+  virtual void RenderProcessExited(content::RenderProcessHost* host,
+                                   base::ProcessHandle handle,
+                                   base::TerminationStatus status,
+                                   int exit_code) OVERRIDE;
+  virtual void RenderProcessHostDestroyed(
+      content::RenderProcessHost* host) OVERRIDE;
+
+  // Try to extract the URL from different possible keys for entry points in the
+  // manifest, returns it and the entry point used.
+  GURL GetStartURL(const LaunchParams& params, LaunchEntryPoint* used);
+  ui::WindowShowState GetWindowShowStateWGT(const LaunchParams& params);
+  ui::WindowShowState GetWindowShowStateXPK(const LaunchParams& params);
+
+  GURL GetURLFromURLKey();
 
-  template<LaunchEntryPoint>
-  bool TryLaunchAt();
-  void set_entry_points(LaunchEntryPoints entry_points);
+  GURL GetURLFromRelativePathKey(const std::string& key);
 
-  friend class FinishEventObserver;
-  void CloseMainDocument();
-  bool IsOnSuspendHandlerRegistered() const;
+  void NotifyTermination();
 
   RuntimeContext* runtime_context_;
-  const scoped_refptr<ApplicationData> application_data_;
-  Runtime* main_runtime_;
-  std::set<Runtime*> runtimes_;
-  scoped_ptr<EventObserver> finish_observer_;
   Observer* observer_;
-  LaunchEntryPoints entry_points_;
-
+  // The entry point used as part of Launch().
+  LaunchEntryPoint entry_point_used_;
+  std::map<std::string, std::string> name_perm_map_;
+  // Application's session permissions.
+  StoredPermissionMap permission_map_;
+  // Security policy.
+  scoped_ptr<SecurityPolicy> security_policy_;
+  // WeakPtrFactory should be always declared the last.
+  base::WeakPtrFactory<Application> weak_factory_;
   DISALLOW_COPY_AND_ASSIGN(Application);
 };