Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_tizen.cc
index c77f894..9297355 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.
 
 
 #include "content/public/browser/web_contents.h"
 #include "content/public/browser/render_process_host.h"
+#include "content/browser/screen_orientation/screen_orientation_dispatcher_host.h"
+#include "content/browser/screen_orientation/screen_orientation_provider.h"
 
+#include "xwalk/runtime/browser/runtime_context.h"
+#include "xwalk/runtime/browser/runtime_url_request_context_getter.h"
 #include "xwalk/runtime/browser/ui/native_app_window.h"
+#include "xwalk/runtime/browser/ui/native_app_window_tizen.h"
 #include "xwalk/runtime/common/xwalk_common_messages.h"
 
 #if defined(USE_OZONE)
-#include "base/message_loop/message_pump_ozone.h"
 #include "content/public/browser/render_view_host.h"
 #include "ui/events/event.h"
 #include "ui/events/event_constants.h"
 #include "ui/events/keycodes/keyboard_codes_posix.h"
-#include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
+#include "ui/events/platform/platform_event_source.h"
 #endif
 
 #include "xwalk/application/common/application_manifest_constants.h"
-#include "xwalk/application/common/manifest_handlers/navigation_handler.h"
+#include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
+#include "xwalk/application/common/manifest_handlers/tizen_splash_screen_handler.h"
 
 namespace xwalk {
 
@@ -32,87 +38,157 @@ namespace widget_keys = application_widget_keys;
 
 namespace application {
 
-namespace {
-const char kAsterisk[] = "*";
-}  // namespace
+blink::WebScreenOrientationLockType GetDefaultOrientation(
+    const base::WeakPtr<Application>& app) {
+  TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
+    app->data()->GetManifestData(widget_keys::kTizenSettingKey));
+  if (!info)
+    return blink::WebScreenOrientationLockDefault;
+  switch (info->screen_orientation()) {
+    case TizenSettingInfo::PORTRAIT:
+      return blink::WebScreenOrientationLockPortrait;
+    case TizenSettingInfo::LANDSCAPE:
+      return blink::WebScreenOrientationLockLandscape;
+    case TizenSettingInfo::AUTO:
+      return blink::WebScreenOrientationLockAny;
+    default:
+      NOTREACHED();
+      return blink::WebScreenOrientationLockDefault;
+  }
+}
 
+class ScreenOrientationProviderTizen :
+    public content::ScreenOrientationProvider {
+ public:
+  explicit ScreenOrientationProviderTizen(const base::WeakPtr<Application>& app)
+      : app_(app),
+        request_id_(0) {
+  }
+
+  virtual void LockOrientation(
+      int request_id,
+      blink::WebScreenOrientationLockType lock) OVERRIDE {
+    if (!app_)
+      return;
+    request_id_ = request_id;
+    const std::set<Runtime*>& runtimes = app_->runtimes();
+    DCHECK(!runtimes.empty());
+    // FIXME: Probably need better alignment with
+    // https://w3c.github.io/screen-orientation/#screen-orientation-lock-lifetime
+    std::set<Runtime*>::iterator it = runtimes.begin();
+    for (; it != runtimes.end(); ++it) {
+      NativeAppWindow* window = (*it)->window();
+      if (window && window->IsActive()) {
+        ToNativeAppWindowTizen(window)->LockOrientation(lock);
+        break;
+      }
+    }
+  }
+
+  virtual void UnlockOrientation() OVERRIDE {
+    LockOrientation(request_id_, GetDefaultOrientation(app_));
+  }
+
+  virtual void OnOrientationChange() OVERRIDE {}
+
+ private:
+  base::WeakPtr<Application> app_;
+  int request_id_;
+};
 
 ApplicationTizen::ApplicationTizen(
     scoped_refptr<ApplicationData> data,
-    RuntimeContext* runtime_context,
-    Application::Observer* observer)
-    : Application(data, runtime_context, observer) {
+    RuntimeContext* runtime_context)
+    : Application(data, runtime_context),
+      is_suspended_(false) {
 #if defined(USE_OZONE)
-  base::MessagePumpOzone::Current()->AddObserver(this);
+  ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
 #endif
+  cookie_manager_ = scoped_ptr<CookieManager>(
+      new CookieManager(id(), runtime_context_));
 }
 
 ApplicationTizen::~ApplicationTizen() {
 #if defined(USE_OZONE)
-  base::MessagePumpOzone::Current()->RemoveObserver(this);
+  ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
 #endif
 }
 
 void ApplicationTizen::Hide() {
-  DCHECK(runtimes_.size());
+  DCHECK(!runtimes_.empty());
   std::set<Runtime*>::iterator it = runtimes_.begin();
   for (; it != runtimes_.end(); ++it) {
     if ((*it)->window())
-      (*it)->window()->Hide();
+      (*it)->window()->Minimize();
   }
 }
 
-void ApplicationTizen::InitSecurityPolicy() {
-  // On Tizen, CSP mode has higher priority, and WARP will be disabled
-  // if the application is under CSP mode.
-  if (!data_->HasCSPDefined()) {
-    Application::InitSecurityPolicy();
-    return;
+void ApplicationTizen::Show() {
+  DCHECK(!runtimes_.empty());
+  for (Runtime* runtime : runtimes_) {
+    if (auto window = runtime->window())
+      window->Restore();
+  }
+}
+
+bool ApplicationTizen::Launch(const LaunchParams& launch_params) {
+  if (Application::Launch(launch_params)) {
+    DCHECK(web_contents_);
+    content::ScreenOrientationProvider *provider =
+        new ScreenOrientationProviderTizen(GetWeakPtr());
+    web_contents_->GetScreenOrientationDispatcherHost()->SetProvider(provider);
+
+    provider->LockOrientation(0, GetDefaultOrientation(GetWeakPtr()));
+    return true;
   }
+  return false;
+}
 
-  if (data_->GetPackageType() != Manifest::TYPE_WGT)
+base::FilePath ApplicationTizen::GetSplashScreenPath() {
+  if (TizenSplashScreenInfo* ss_info = static_cast<TizenSplashScreenInfo*>(
+      data()->GetManifestData(widget_keys::kTizenSplashScreenKey))) {
+    return data()->path().Append(FILE_PATH_LITERAL(ss_info->src()));
+  }
+  return base::FilePath();
+}
+
+void ApplicationTizen::Suspend() {
+  if (is_suspended_)
     return;
 
-  // Always enable security mode when under CSP mode.
-  security_mode_enabled_ = true;
-  NavigationInfo* info = static_cast<NavigationInfo*>(
-      data_->GetManifestData(widget_keys::kAllowNavigationKey));
-  if (info) {
-    const std::vector<std::string>& allowed_list = info->GetAllowedDomains();
-    for (std::vector<std::string>::const_iterator it = allowed_list.begin();
-         it != allowed_list.end(); ++it) {
-      // If the policy is "*", it represents that any external link is allowed
-      // to navigate to.
-      if ((*it) == kAsterisk) {
-        security_mode_enabled_ = false;
-        return;
-      }
+  DCHECK(render_process_host_);
+  render_process_host_->Send(new ViewMsg_SuspendJSEngine(true));
 
-      // If the policy start with "*.", like this: *.domain,
-      // means that can access to all subdomains for 'domain',
-      // otherwise, the host of request url should exactly the same
-      // as policy.
-      bool subdomains = ((*it).find("*.") == 0);
-      std::string host = subdomains ? (*it).substr(2) : (*it);
-      AddSecurityPolicy(GURL("http://" + host), subdomains);
-      AddSecurityPolicy(GURL("https://" + host), subdomains);
-    }
+  DCHECK(!runtimes_.empty());
+  std::set<Runtime*>::iterator it = runtimes_.begin();
+  for (; it != runtimes_.end(); ++it) {
+    if ((*it)->web_contents())
+      (*it)->web_contents()->WasHidden();
   }
+  is_suspended_ = true;
+}
+
+void ApplicationTizen::Resume() {
+  if (!is_suspended_)
+    return;
+
   DCHECK(render_process_host_);
-  render_process_host_->Send(
-      new ViewMsg_EnableSecurityMode(
-          ApplicationData::GetBaseURLFromApplicationId(id()),
-          SecurityPolicy::CSP));
+  render_process_host_->Send(new ViewMsg_SuspendJSEngine(false));
+
+  DCHECK(!runtimes_.empty());
+  std::set<Runtime*>::iterator it = runtimes_.begin();
+  for (; it != runtimes_.end(); ++it) {
+    if ((*it)->web_contents())
+      (*it)->web_contents()->WasShown();
+  }
+  is_suspended_ = false;
 }
 
 #if defined(USE_OZONE)
-base::EventStatus ApplicationTizen::WillProcessEvent(
-    const base::NativeEvent& event) {
-  return base::EVENT_CONTINUE;
-}
+void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
 
 void ApplicationTizen::DidProcessEvent(
-    const base::NativeEvent& event) {
+    const ui::PlatformEvent& event) {
   ui::Event* ui_event = static_cast<ui::Event*>(event);
   if (!ui_event->IsKeyEvent() || ui_event->type() != ui::ET_KEY_PRESSED)
     return;
@@ -141,5 +217,14 @@ void ApplicationTizen::DidProcessEvent(
 }
 #endif
 
+void ApplicationTizen::RemoveAllCookies() {
+  cookie_manager_->RemoveAllCookies();
+}
+
+void ApplicationTizen::SetUserAgentString(
+    const std::string& user_agent_string) {
+  cookie_manager_->SetUserAgentString(render_process_host_, user_agent_string);
+}
+
 }  // namespace application
 }  // namespace xwalk