Upstream version 10.38.217.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / linux / running_application_object.cc
index efba653..87877dc 100644 (file)
@@ -1,4 +1,5 @@
 // Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -53,19 +54,31 @@ RunningApplicationObject::RunningApplicationObject(
 
   properties()->Set(
       kRunningApplicationDBusInterface, "AppID",
-      scoped_ptr<base::Value>(base::Value::CreateStringValue(app_id)));
+      scoped_ptr<base::Value>(new base::StringValue(app_id)));
 
+  // FIXME: RemoveAllCookies and SetUserAgentString
+  // are exported for web_setting extension usage.
+  // This is a temporary solution - when another
+  // IPC on extension process side is implemented,
+  // these methods have to be removed.
   dbus_object()->ExportMethod(
-      kRunningApplicationDBusInterface, "Terminate",
-      base::Bind(&RunningApplicationObject::OnTerminate,
-                 base::Unretained(this), Application::Normal),
+      kRunningApplicationDBusInterface, "RemoveAllCookies",
+      base::Bind(&RunningApplicationObject::OnRemoveAllCookies,
+                 base::Unretained(this)),
       base::Bind(&RunningApplicationObject::OnExported,
                  base::Unretained(this)));
 
   dbus_object()->ExportMethod(
-      kRunningApplicationDBusInterface, "ForceTerminate",
+      kRunningApplicationDBusInterface, "SetUserAgentString",
+      base::Bind(&RunningApplicationObject::OnSetUserAgentString,
+                 base::Unretained(this)),
+      base::Bind(&RunningApplicationObject::OnExported,
+                 base::Unretained(this)));
+
+  dbus_object()->ExportMethod(
+      kRunningApplicationDBusInterface, "Terminate",
       base::Bind(&RunningApplicationObject::OnTerminate,
-                 base::Unretained(this), Application::Immediate),
+                 base::Unretained(this)),
       base::Bind(&RunningApplicationObject::OnExported,
                  base::Unretained(this)));
 
@@ -83,6 +96,20 @@ RunningApplicationObject::RunningApplicationObject(
                  base::Unretained(this)),
       base::Bind(&RunningApplicationObject::OnExported,
                  base::Unretained(this)));
+
+  dbus_object()->ExportMethod(
+      kRunningApplicationDBusInterface, "Suspend",
+      base::Bind(&RunningApplicationObject::OnSuspend,
+                 base::Unretained(this)),
+      base::Bind(&RunningApplicationObject::OnExported,
+                 base::Unretained(this)));
+
+  dbus_object()->ExportMethod(
+      kRunningApplicationDBusInterface, "Resume",
+      base::Bind(&RunningApplicationObject::OnResume,
+                 base::Unretained(this)),
+      base::Bind(&RunningApplicationObject::OnExported,
+                 base::Unretained(this)));
 #endif
 }
 
@@ -90,14 +117,8 @@ RunningApplicationObject::~RunningApplicationObject() {
   UnlistenForOwnerChange();
 }
 
-void RunningApplicationObject::TerminateApplication(
-    Application::TerminationMode mode) {
-  // The application might be still running after 'Terminate' call
-  // (if 'mode == Normal' and it contains 'OnSuspend' handlers),
-  // so we do not call 'UnlistenForOwnerChange' here - it will
-  // be called anyway right before the actual 'Application' instance
-  // deletion.
-  application_->Terminate(mode);
+void RunningApplicationObject::TerminateApplication() {
+  application_->Terminate();
 }
 
 void RunningApplicationObject::OnExported(const std::string& interface_name,
@@ -110,8 +131,61 @@ void RunningApplicationObject::OnExported(const std::string& interface_name,
   }
 }
 
+void RunningApplicationObject::SetUserAgentStringOnIOThread(
+    const std::string& user_agent_string) {
+  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
+  ToApplicationTizen(application_)->SetUserAgentString(user_agent_string);
+}
+
+void RunningApplicationObject::OnRemoveAllCookies(dbus::MethodCall* method_call,
+    dbus::ExportedObject::ResponseSender response_sender) {
+  if (method_call->GetSender() != launcher_name_) {
+    scoped_ptr<dbus::ErrorResponse> error_response =
+        dbus::ErrorResponse::FromMethodCall(method_call,
+                                            kRunningApplicationDBusError,
+                                            "Not permitted");
+    response_sender.Run(error_response.PassAs<dbus::Response>());
+    return;
+  }
+
+  ToApplicationTizen(application_)->RemoveAllCookies();
+
+  scoped_ptr<dbus::Response> response =
+      dbus::Response::FromMethodCall(method_call);
+  response_sender.Run(response.Pass());
+}
+
+void RunningApplicationObject::OnSetUserAgentString(
+    dbus::MethodCall* method_call,
+    dbus::ExportedObject::ResponseSender response_sender) {
+  if (method_call->GetSender() != launcher_name_) {
+    scoped_ptr<dbus::ErrorResponse> error_response =
+        dbus::ErrorResponse::FromMethodCall(method_call,
+                                            kRunningApplicationDBusError,
+                                            "Not permitted");
+    response_sender.Run(error_response.PassAs<dbus::Response>());
+    return;
+  }
+  dbus::MessageReader reader(method_call);
+  std::string new_user_agent;
+  if (reader.PopString(&new_user_agent)) {
+    content::BrowserThread::PostTask(
+        content::BrowserThread::IO, FROM_HERE,
+        base::Bind(&RunningApplicationObject::SetUserAgentStringOnIOThread,
+                   base::Unretained(this), new_user_agent));
+    scoped_ptr<dbus::Response> response =
+        dbus::Response::FromMethodCall(method_call);
+    response_sender.Run(response.Pass());
+  } else {
+    scoped_ptr<dbus::ErrorResponse> error_response =
+        dbus::ErrorResponse::FromMethodCall(method_call,
+                                            kRunningApplicationDBusError,
+                                            "Wrong user agent string");
+    response_sender.Run(error_response.PassAs<dbus::Response>());
+  }
+}
+
 void RunningApplicationObject::OnTerminate(
-    Application::TerminationMode mode,
     dbus::MethodCall* method_call,
     dbus::ExportedObject::ResponseSender response_sender) {
   // We only allow the caller of Launch() to call Terminate().
@@ -124,7 +198,7 @@ void RunningApplicationObject::OnTerminate(
     return;
   }
 
-  TerminateApplication(mode);
+  TerminateApplication();
 
   scoped_ptr<dbus::Response> response =
       dbus::Response::FromMethodCall(method_call);
@@ -134,11 +208,9 @@ void RunningApplicationObject::OnTerminate(
 void RunningApplicationObject::OnGetExtensionProcessChannel(
     dbus::MethodCall* method_call,
     dbus::ExportedObject::ResponseSender response_sender) {
-  content::BrowserThread::PostTaskAndReplyWithResult(
+  content::BrowserThread::PostTask(
       content::BrowserThread::FILE,
       FROM_HERE,
-      base::Bind(&RunningApplicationObject::CreateClientFileDescriptor,
-                 base::Unretained(this)),
       base::Bind(&RunningApplicationObject::SendChannel,
                  base::Unretained(this),
                  method_call,
@@ -164,6 +236,45 @@ void RunningApplicationObject::OnHide(
       dbus::Response::FromMethodCall(method_call);
   response_sender.Run(response.Pass());
 }
+
+void RunningApplicationObject::OnSuspend(
+    dbus::MethodCall* method_call,
+    dbus::ExportedObject::ResponseSender response_sender) {
+  if (method_call->GetSender() != launcher_name_) {
+    scoped_ptr<dbus::ErrorResponse> error_response =
+        dbus::ErrorResponse::FromMethodCall(method_call,
+                                            kRunningApplicationDBusError,
+                                            "Not permitted");
+    response_sender.Run(error_response.PassAs<dbus::Response>());
+    return;
+  }
+
+  ToApplicationTizen(application_)->Suspend();
+
+  scoped_ptr<dbus::Response> response =
+      dbus::Response::FromMethodCall(method_call);
+  response_sender.Run(response.Pass());
+}
+
+void RunningApplicationObject::OnResume(
+    dbus::MethodCall* method_call,
+    dbus::ExportedObject::ResponseSender response_sender) {
+  if (method_call->GetSender() != launcher_name_) {
+    scoped_ptr<dbus::ErrorResponse> error_response =
+        dbus::ErrorResponse::FromMethodCall(method_call,
+                                            kRunningApplicationDBusError,
+                                            "Not permitted");
+    response_sender.Run(error_response.PassAs<dbus::Response>());
+    return;
+  }
+
+  ToApplicationTizen(application_)->Resume();
+  ToApplicationTizen(application_)->Show();
+
+  scoped_ptr<dbus::Response> response =
+      dbus::Response::FromMethodCall(method_call);
+  response_sender.Run(response.Pass());
+}
 #endif
 
 void RunningApplicationObject::ListenForOwnerChange() {
@@ -190,28 +301,29 @@ void RunningApplicationObject::OnNameOwnerChanged(
 }
 
 void RunningApplicationObject::OnLauncherDisappeared() {
-  // Do not care about 'OnSuspend' handlers if the launcher is already killed.
-  TerminateApplication(Application::Immediate);
-}
-
-scoped_ptr<dbus::FileDescriptor>
-RunningApplicationObject::CreateClientFileDescriptor() {
-  scoped_ptr<dbus::FileDescriptor> client_fd(
-      new dbus::FileDescriptor(ep_bp_channel_.socket.fd));
-  client_fd->CheckValidity();
-  return client_fd.Pass();
+  TerminateApplication();
 }
 
 void RunningApplicationObject::SendChannel(
     dbus::MethodCall* method_call,
-    dbus::ExportedObject::ResponseSender response_sender,
-    scoped_ptr<dbus::FileDescriptor> client_fd) {
+    dbus::ExportedObject::ResponseSender response_sender) {
   scoped_ptr<dbus::Response> response =
       dbus::Response::FromMethodCall(method_call);
-  dbus::MessageWriter writer(response.get());
 
+  int fd = ep_bp_channel_.socket.fd;
+  if (fd == -1) {  // EP was not yet created, return empty response.
+    response_sender.Run(response.Pass());
+    return;
+  }
+
+  dbus::MessageWriter writer(response.get());
   writer.AppendString(ep_bp_channel_.name);
+
+  scoped_ptr<dbus::FileDescriptor> client_fd(new dbus::FileDescriptor(fd));
+  client_fd->CheckValidity();
+  CHECK(client_fd->is_valid());
   writer.AppendFileDescriptor(*client_fd);
+
   response_sender.Run(response.Pass());
 }