Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_service.cc
index 41af7b4..a2ee3f0 100644 (file)
 #include "xwalk/application/browser/application_system.h"
 #include "xwalk/application/browser/installer/package.h"
 #include "xwalk/application/common/application_file_util.h"
-#include "xwalk/application/common/application_manifest_constants.h"
 #include "xwalk/application/common/event_names.h"
-#include "xwalk/application/common/id_util.h"
 #include "xwalk/application/common/permission_policy_manager.h"
 #include "xwalk/runtime/browser/runtime_context.h"
 #include "xwalk/runtime/browser/runtime.h"
 #include "xwalk/runtime/browser/xwalk_runner.h"
 
-#if defined(OS_TIZEN_MOBILE)
-#include "xwalk/application/browser/installer/tizen/package_installer.h"
+#if defined(OS_TIZEN)
 #include "xwalk/application/browser/installer/tizen/service_package_installer.h"
 #endif
 
-using xwalk::RuntimeContext;
+namespace xwalk {
+namespace application {
 
 namespace {
 
@@ -46,18 +44,18 @@ void CloseMessageLoop() {
 
 void WaitForEventAndClose(
     const std::string& app_id, const std::string& event_name,
-    xwalk::application::ApplicationEventManager* event_manager) {
-  class CloseOnEventArrived : public xwalk::application::EventObserver {
+    ApplicationEventManager* event_manager) {
+  class CloseOnEventArrived : public EventObserver {
    public:
     static CloseOnEventArrived* Create(const std::string& event_name,
-        xwalk::application::ApplicationEventManager* event_manager) {
+        ApplicationEventManager* event_manager) {
       return new CloseOnEventArrived(event_name, event_manager);
     }
 
     virtual void Observe(
         const std::string& app_id,
-        scoped_refptr<xwalk::application::Event> event) OVERRIDE {
-      DCHECK(xwalk::application::kOnJavaScriptEventAck == event->name());
+        scoped_refptr<Event> event) OVERRIDE {
+      DCHECK(kOnJavaScriptEventAck == event->name());
       std::string ack_event_name;
       event->args()->GetString(0, &ack_event_name);
       if (ack_event_name != event_name_)
@@ -69,8 +67,8 @@ void WaitForEventAndClose(
    private:
     CloseOnEventArrived(
         const std::string& event_name,
-        xwalk::application::ApplicationEventManager* event_manager)
-        : xwalk::application::EventObserver(event_manager),
+        ApplicationEventManager* event_manager)
+        : EventObserver(event_manager),
           event_name_(event_name) {}
 
     std::string event_name_;
@@ -80,18 +78,18 @@ void WaitForEventAndClose(
   CloseOnEventArrived* observer =
       CloseOnEventArrived::Create(event_name, event_manager);
   event_manager->AttachObserver(app_id,
-      xwalk::application::kOnJavaScriptEventAck, observer);
+      kOnJavaScriptEventAck, observer);
 }
 
 void WaitForFinishLoad(
-    scoped_refptr<xwalk::application::ApplicationData> application,
-    xwalk::application::ApplicationEventManager* event_manager,
+    scoped_refptr<ApplicationData> application,
+    ApplicationEventManager* event_manager,
     content::WebContents* contents) {
   class CloseAfterLoadObserver : public content::WebContentsObserver {
    public:
     CloseAfterLoadObserver(
-        scoped_refptr<xwalk::application::ApplicationData> application,
-        xwalk::application::ApplicationEventManager* event_manager,
+        scoped_refptr<ApplicationData> application,
+        ApplicationEventManager* event_manager,
         content::WebContents* contents)
         : content::WebContentsObserver(contents),
           application_(application),
@@ -105,13 +103,13 @@ void WaitForFinishLoad(
         const GURL& validate_url,
         bool is_main_frame,
         content::RenderViewHost* render_view_host) OVERRIDE {
-      if (!IsEventHandlerRegistered(xwalk::application::kOnInstalled)) {
+      if (!IsEventHandlerRegistered(kOnInstalled)) {
         CloseMessageLoop();
       } else {
         scoped_ptr<base::ListValue> event_args(new base::ListValue);
-        scoped_refptr<xwalk::application::Event> event =
-            xwalk::application::Event::CreateEvent(
-                xwalk::application::kOnInstalled, event_args.Pass());
+        scoped_refptr<Event> event =
+            Event::CreateEvent(
+                kOnInstalled, event_args.Pass());
         event_manager_->SendEvent(application_->ID(), event);
 
         WaitForEventAndClose(
@@ -126,8 +124,8 @@ void WaitForFinishLoad(
       return events.find(event_name) != events.end();
     }
 
-    scoped_refptr<xwalk::application::ApplicationData> application_;
-    xwalk::application::ApplicationEventManager* event_manager_;
+    scoped_refptr<ApplicationData> application_;
+    ApplicationEventManager* event_manager_;
   };
 
   // This object is self-destroyed when an event occurs.
@@ -135,13 +133,13 @@ void WaitForFinishLoad(
 }
 
 void SaveSystemEventsInfo(
-    xwalk::application::ApplicationService* application_service,
-    scoped_refptr<xwalk::application::ApplicationData> application_data,
-    xwalk::application::ApplicationEventManager* event_manager) {
+    ApplicationService* application_service,
+    scoped_refptr<ApplicationData> application_data,
+    ApplicationEventManager* event_manager) {
   // We need to run main document after installation in order to
   // register system events.
   if (application_data->HasMainDocument()) {
-    if (xwalk::application::Application* application =
+    if (Application* application =
         application_service->Launch(application_data->ID())) {
       WaitForFinishLoad(application->data(), event_manager,
                         application->GetMainDocumentRuntime()->web_contents());
@@ -149,43 +147,29 @@ void SaveSystemEventsInfo(
   }
 }
 
-#if defined(OS_TIZEN_MOBILE)
-bool InstallPackageOnTizen(xwalk::application::ApplicationService* service,
-                           xwalk::application::ApplicationStorage* storage,
-                           xwalk::application::ApplicationData* application,
+#if defined(OS_TIZEN)
+bool InstallPackageOnTizen(ApplicationData* application_data,
                            const base::FilePath& data_dir) {
-  if (xwalk::XWalkRunner::GetInstance()->is_running_as_service()) {
-    return InstallApplicationForTizen(application, data_dir);
-  }
-
-  scoped_ptr<xwalk::application::PackageInstaller> installer =
-      xwalk::application::PackageInstaller::Create(service, storage,
-                                                   application->ID(), data_dir);
-  if (!installer || !installer->Install()) {
-    LOG(ERROR) << "An error occurred during installation on Tizen.";
+  if (!XWalkRunner::GetInstance()->is_running_as_service()) {
+    LOG(ERROR) << "Installation on Tizen is only possible in"
+               << "service mode via 'xwalkctl' utility.";
     return false;
   }
-  return true;
+
+  return InstallApplicationForTizen(application_data, data_dir);
 }
 
-bool UninstallPackageOnTizen(xwalk::application::ApplicationService* service,
-                             xwalk::application::ApplicationStorage* storage,
-                             xwalk::application::ApplicationData* application,
+bool UninstallPackageOnTizen(ApplicationData* application_data,
                              const base::FilePath& data_dir) {
-  if (xwalk::XWalkRunner::GetInstance()->is_running_as_service()) {
-    return UninstallApplicationForTizen(application, data_dir);
-  }
-
-  scoped_ptr<xwalk::application::PackageInstaller> installer =
-      xwalk::application::PackageInstaller::Create(service, storage,
-                                                   application->ID(), data_dir);
-  if (!installer || !installer->Uninstall()) {
-    LOG(ERROR) << "An error occurred during uninstallation on Tizen.";
+  if (!XWalkRunner::GetInstance()->is_running_as_service()) {
+    LOG(ERROR) << "Uninstallation on Tizen is only possible in"
+               << "service mode using 'xwalkctl' utility.";
     return false;
   }
-  return true;
+
+  return UninstallApplicationForTizen(application_data, data_dir);
 }
-#endif  // OS_TIZEN_MOBILE
+#endif  // OS_TIZEN
 
 bool CopyDirectoryContents(const base::FilePath& from,
     const base::FilePath& to) {
@@ -205,9 +189,6 @@ bool CopyDirectoryContents(const base::FilePath& from,
 
 }  // namespace
 
-namespace xwalk {
-namespace application {
-
 const base::FilePath::CharType kApplicationsDir[] =
     FILE_PATH_LITERAL("applications");
 
@@ -293,9 +274,8 @@ bool ApplicationService::Install(const base::FilePath& path, std::string* id) {
     return false;
   }
 
-#if defined(OS_TIZEN_MOBILE)
-  if (!InstallPackageOnTizen(this, application_storage_,
-                             application_data.get(),
+#if defined(OS_TIZEN)
+  if (!InstallPackageOnTizen(application_data,
                              runtime_context_->GetPath())) {
     application_storage_->RemoveApplication(application_data->ID());
     return false;
@@ -402,9 +382,8 @@ bool ApplicationService::Update(const std::string& id,
     return false;
   }
 
-#if defined(OS_TIZEN_MOBILE)
-  if (!UninstallPackageOnTizen(this, application_storage_,
-                               old_application.get(),
+#if defined(OS_TIZEN)
+  if (!UninstallPackageOnTizen(old_application,
                                runtime_context_->GetPath())) {
     base::DeleteFile(app_dir, true);
     base::Move(tmp_dir, app_dir);
@@ -416,16 +395,14 @@ bool ApplicationService::Update(const std::string& id,
     LOG(ERROR) << "An Error occurred when updating the application.";
     base::DeleteFile(app_dir, true);
     base::Move(tmp_dir, app_dir);
-#if defined(OS_TIZEN_MOBILE)
-    InstallPackageOnTizen(this, application_storage_,
-                          old_application.get(),
+#if defined(OS_TIZEN)
+    InstallPackageOnTizen(old_application,
                           runtime_context_->GetPath());
 #endif
     return false;
   }
-#if defined(OS_TIZEN_MOBILE)
-  if (!InstallPackageOnTizen(this, application_storage_,
-                             new_application.get(),
+#if defined(OS_TIZEN)
+  if (!InstallPackageOnTizen(new_application,
                              runtime_context_->GetPath()))
     return false;
 #endif
@@ -455,8 +432,8 @@ bool ApplicationService::Uninstall(const std::string& id) {
     app->Terminate(Application::Immediate);
   }
 
-#if defined(OS_TIZEN_MOBILE)
-  if (!UninstallPackageOnTizen(this, application_storage_, application.get(),
+#if defined(OS_TIZEN)
+  if (!UninstallPackageOnTizen(application,
                                runtime_context_->GetPath()))
     result = false;
 #endif
@@ -491,6 +468,36 @@ bool ApplicationService::Uninstall(const std::string& id) {
 }
 
 Application* ApplicationService::Launch(
+    scoped_refptr<ApplicationData> application_data,
+    const Application::LaunchParams& launch_params) {
+  if (GetApplicationByID(application_data->ID()) != NULL) {
+    LOG(INFO) << "Application with id: " << application_data->ID()
+              << " is already running.";
+    // FIXME: we need to notify application that it had been attempted
+    // to invoke and let the application to define the further behavior.
+    return NULL;
+  }
+
+  event_manager_->AddEventRouterForApp(application_data);
+  Application* application(new Application(application_data,
+                                           runtime_context_,
+                                           this));
+  ScopedVector<Application>::iterator app_iter =
+      applications_.insert(applications_.end(), application);
+
+  if (!application->Launch(launch_params)) {
+    event_manager_->RemoveEventRouterForApp(application_data);
+    applications_.erase(app_iter);
+    return NULL;
+  }
+
+  FOR_EACH_OBSERVER(Observer, observers_,
+                    DidLaunchApplication(application));
+
+  return application;
+}
+
+Application* ApplicationService::Launch(
     const std::string& id, const Application::LaunchParams& params) {
   scoped_refptr<ApplicationData> application_data =
     application_storage_->GetApplicationData(id);
@@ -502,7 +509,8 @@ Application* ApplicationService::Launch(
   return Launch(application_data, params);
 }
 
-Application* ApplicationService::Launch(const base::FilePath& path) {
+Application* ApplicationService::Launch(
+    const base::FilePath& path, const Application::LaunchParams& params) {
   if (!base::DirectoryExists(path))
     return NULL;
 
@@ -516,36 +524,7 @@ Application* ApplicationService::Launch(const base::FilePath& path) {
     return NULL;
   }
 
-  return Launch(application_data, Application::LaunchParams());
-}
-
-Application* ApplicationService::Launch(const GURL& url) {
-  namespace keys = xwalk::application_manifest_keys;
-
-  const std::string& url_spec = url.spec();
-  DCHECK(!url_spec.empty());
-  const std::string& app_id = GenerateId(url_spec);
-  // FIXME: we need to handle hash collisions.
-  DCHECK(!application_storage_->GetApplicationData(app_id));
-
-  base::DictionaryValue manifest;
-  // FIXME: define permissions!
-  manifest.SetString(keys::kURLKey, url_spec);
-  manifest.SetString(keys::kNameKey, "XWalk Dummy App");
-  manifest.SetString(keys::kVersionKey, "0");
-  manifest.SetInteger(keys::kManifestVersionKey, 1);
-  std::string error;
-  scoped_refptr<ApplicationData> application_data = ApplicationData::Create(
-            base::FilePath(), Manifest::COMMAND_LINE, manifest, app_id, &error);
-  if (!application_data) {
-    LOG(ERROR) << "Error occurred while trying to launch application: "
-               << error;
-    return NULL;
-  }
-
-  Application::LaunchParams launch_params;
-  launch_params.entry_points = Application::URLKey;
-  return Launch(application_data, launch_params);
+  return Launch(application_data, params);
 }
 
 namespace {
@@ -610,36 +589,6 @@ void ApplicationService::OnApplicationTerminated(
   }
 }
 
-Application* ApplicationService::Launch(
-    scoped_refptr<ApplicationData> application_data,
-    const Application::LaunchParams& launch_params) {
-  if (GetApplicationByID(application_data->ID()) != NULL) {
-    LOG(INFO) << "Application with id: " << application_data->ID()
-              << " is already running.";
-    // FIXME: we need to notify application that it had been attempted
-    // to invoke and let the application to define the further behavior.
-    return NULL;
-  }
-
-  event_manager_->AddEventRouterForApp(application_data);
-  Application* application(new Application(application_data,
-                                           runtime_context_,
-                                           this));
-  ScopedVector<Application>::iterator app_iter =
-      applications_.insert(applications_.end(), application);
-
-  if (!application->Launch(launch_params)) {
-    event_manager_->RemoveEventRouterForApp(application_data);
-    applications_.erase(app_iter);
-    return NULL;
-  }
-
-  FOR_EACH_OBSERVER(Observer, observers_,
-                    DidLaunchApplication(application));
-
-  return application;
-}
-
 void ApplicationService::CheckAPIAccessControl(const std::string& app_id,
     const std::string& extension_name,
     const std::string& api_name, const PermissionCallback& callback) {