Upstream version 9.37.190.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / linux / running_applications_manager.cc
index 5861fd4..c8dd611 100644 (file)
@@ -10,6 +10,7 @@
 #include "dbus/message.h"
 
 #include "xwalk/application/browser/linux/running_application_object.h"
+#include "xwalk/application/common/application_data.h"
 
 namespace {
 
@@ -33,8 +34,21 @@ const dbus::ObjectPath kRunningManagerDBusPath("/running1");
 namespace xwalk {
 namespace application {
 
+// The [tizen_app_id] contains a dot, making it an invalid object path.
+// For this reason we replace it with an underscore '_'.
+std::string GetAppObjectPathFromAppId(const std::string& app_id) {
+#if defined(OS_TIZEN)
+  std::string ret(app_id);
+  std::replace(ret.begin(), ret.end(), '.', '_');
+  return ret;
+#else
+  return app_id;
+#endif
+}
+
 dbus::ObjectPath GetRunningPathForAppID(const std::string& app_id) {
-  return dbus::ObjectPath(kRunningManagerDBusPath.value() + "/" + app_id);
+  return dbus::ObjectPath(kRunningManagerDBusPath.value() + "/" +
+                          GetAppObjectPathFromAppId(app_id));
 }
 
 RunningApplicationsManager::RunningApplicationsManager(
@@ -50,6 +64,13 @@ RunningApplicationsManager::RunningApplicationsManager(
                  weak_factory_.GetWeakPtr()),
       base::Bind(&RunningApplicationsManager::OnExported,
                  weak_factory_.GetWeakPtr()));
+
+  adaptor_.manager_object()->ExportMethod(
+      kRunningManagerDBusInterface, "TerminateIfRunning",
+      base::Bind(&RunningApplicationsManager::OnTerminateIfRunning,
+                 weak_factory_.GetWeakPtr()),
+      base::Bind(&RunningApplicationsManager::OnExported,
+                 weak_factory_.GetWeakPtr()));
 }
 
 RunningApplicationsManager::~RunningApplicationsManager() {}
@@ -79,12 +100,12 @@ void RunningApplicationsManager::OnLaunch(
     dbus::ExportedObject::ResponseSender response_sender) {
 
   dbus::MessageReader reader(method_call);
-  std::string app_id;
+  std::string app_id_or_url;
   // We might want to pass key-value pairs if have more parameters in future.
   unsigned int launcher_pid;
   bool fullscreen;
 
-  if (!reader.PopString(&app_id) ||
+  if (!reader.PopString(&app_id_or_url) ||
       !reader.PopUint32(&launcher_pid) ||
       !reader.PopBool(&fullscreen)) {
     scoped_ptr<dbus::Response> response =
@@ -96,23 +117,41 @@ void RunningApplicationsManager::OnLaunch(
 
   Application::LaunchParams params;
   params.launcher_pid = launcher_pid;
-  if (fullscreen)
-    params.window_state = ui::SHOW_STATE_FULLSCREEN;
+  params.force_fullscreen = fullscreen;
+
+  Application* application;
+  if (GURL(app_id_or_url).spec().empty()) {
+    application = application_service_->Launch(app_id_or_url, params);
+  } else {
+    params.entry_points = Application::StartURLKey;
+    std::string error;
+    scoped_refptr<ApplicationData> application_data =
+        ApplicationData::Create(GURL(app_id_or_url), &error);
+    if (!application_data) {
+      scoped_ptr<dbus::Response> response = CreateError(method_call, error);
+      response_sender.Run(response.Pass());
+      return;
+    }
+
+    application = application_service_->Launch(application_data, params);
+  }
 
-  Application* application = application_service_->Launch(app_id, params);
   if (!application) {
     scoped_ptr<dbus::Response> response =
         CreateError(method_call,
-                    "Error launching application with id " + app_id);
+                    "Error launching application with id or url"
+                    + app_id_or_url);
     response_sender.Run(response.Pass());
     return;
   }
-  CHECK(app_id == application->id());
+
   // FIXME(cmarcelo): ApplicationService will tell us when new applications
   // appear (with DidLaunchApplication()) and we create new managed objects
   // in D-Bus based on that.
-  dbus::ObjectPath path = AddObject(app_id, method_call->GetSender(),
-                                    application);
+  dbus::ObjectPath path =
+      AddObject(GetAppObjectPathFromAppId(application->id()),
+                method_call->GetSender(),
+                application);
 
   scoped_ptr<dbus::Response> response =
       dbus::Response::FromMethodCall(method_call);
@@ -121,6 +160,31 @@ void RunningApplicationsManager::OnLaunch(
   response_sender.Run(response.Pass());
 }
 
+void RunningApplicationsManager::OnTerminateIfRunning(
+    dbus::MethodCall* method_call,
+    dbus::ExportedObject::ResponseSender response_sender) {
+
+  dbus::MessageReader reader(method_call);
+  std::string app_id;
+
+  if (!reader.PopString(&app_id)) {
+    scoped_ptr<dbus::Response> response =
+        CreateError(method_call,
+                    "Error parsing message. Missing argument.");
+    response_sender.Run(response.Pass());
+    return;
+  }
+
+  if (Application* app = application_service_->GetApplicationByID(app_id)) {
+    CHECK(app_id == app->id());
+    app->Terminate();
+  }
+
+  scoped_ptr<dbus::Response> response =
+      dbus::Response::FromMethodCall(method_call);
+  response_sender.Run(response.Pass());
+}
+
 void RunningApplicationsManager::OnExported(
     const std::string& interface_name,
     const std::string& method_name,