Upstream version 5.34.97.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / runtime.cc
index 7eb78f5..8c0dd12 100644 (file)
@@ -28,6 +28,7 @@
 #include "grit/xwalk_resources.h"
 #include "ui/base/resource/resource_bundle.h"
 #include "ui/gfx/image/image_skia.h"
+#include "ui/gfx/native_widget_types.h"
 
 using content::FaviconURL;
 using content::WebContents;
@@ -40,26 +41,13 @@ namespace {
 const int kDefaultWidth = 840;
 const int kDefaultHeight = 600;
 
-static Runtime::Observer* g_observer_for_testing;
-
 }  // namespace
 
 // static
-Runtime* Runtime::Create(
-        RuntimeContext* runtime_context, const GURL& url, Observer* observer) {
-  WebContents::CreateParams params(runtime_context, NULL);
-  params.routing_id = MSG_ROUTING_NONE;
-  WebContents* web_contents = WebContents::Create(params);
-
-  Runtime* runtime = new Runtime(web_contents, observer);
-  runtime->LoadURL(url);
-  return runtime;
-}
-
-// static
 Runtime* Runtime::CreateWithDefaultWindow(
     RuntimeContext* runtime_context, const GURL& url, Observer* observer) {
-  Runtime* runtime = Runtime::Create(runtime_context, url, observer);
+  Runtime* runtime = Runtime::Create(runtime_context, observer);
+  runtime->LoadURL(url);
   runtime->AttachDefaultWindow();
   return runtime;
 }
@@ -71,20 +59,14 @@ Runtime* Runtime::Create(
   params.routing_id = MSG_ROUTING_NONE;
   WebContents* web_contents = WebContents::Create(params);
 
-  return new Runtime(web_contents, observer);
-}
+  Runtime* runtime = new Runtime(web_contents, observer);
+#if defined(OS_TIZEN)
+  runtime->InitRootWindow();
+#endif
 
-// static
-void Runtime::SetGlobalObserverForTesting(Observer* observer) {
-  g_observer_for_testing = observer;
+  return runtime;
 }
 
-#define FOR_EACH_RUNTIME_OBSERVER(method) \
-  if (observer_)                          \
-    observer_->method;                    \
-  if (g_observer_for_testing)             \
-    g_observer_for_testing->method
-
 Runtime::Runtime(content::WebContents* web_contents, Observer* observer)
     : WebContentsObserver(web_contents),
       web_contents_(web_contents),
@@ -98,8 +80,11 @@ Runtime::Runtime(content::WebContents* web_contents, Observer* observer)
        xwalk::NOTIFICATION_RUNTIME_OPENED,
        content::Source<Runtime>(this),
        content::NotificationService::NoDetails());
-
-  FOR_EACH_RUNTIME_OBSERVER(OnRuntimeAdded(this));
+#if defined(OS_TIZEN)
+  root_window_ = NULL;
+#endif
+  if (observer_)
+    observer_->OnRuntimeAdded(this);
 }
 
 Runtime::~Runtime() {
@@ -107,19 +92,12 @@ Runtime::~Runtime() {
           xwalk::NOTIFICATION_RUNTIME_CLOSED,
           content::Source<Runtime>(this),
           content::NotificationService::NoDetails());
-  FOR_EACH_RUNTIME_OBSERVER(OnRuntimeRemoved(this));
+  if (observer_)
+    observer_->OnRuntimeRemoved(this);
 }
 
 void Runtime::AttachDefaultWindow() {
   NativeAppWindow::CreateParams params;
-  params.delegate = this;
-  params.web_contents = web_contents_.get();
-  params.bounds = gfx::Rect(0, 0, kDefaultWidth, kDefaultHeight);
-  CommandLine* cmd_line = CommandLine::ForCurrentProcess();
-  if (cmd_line->HasSwitch(switches::kFullscreen)) {
-    params.state = ui::SHOW_STATE_FULLSCREEN;
-    fullscreen_options_ |= FULLSCREEN_FOR_LAUNCH;
-  }
   AttachWindow(params);
 }
 
@@ -128,6 +106,9 @@ void Runtime::AttachWindow(const NativeAppWindow::CreateParams& params) {
   NOTIMPLEMENTED();
 #else
   CHECK(!window_);
+  NativeAppWindow::CreateParams effective_params(params);
+  ApplyWindowDefaultParams(&effective_params);
+
   // Set the app icon if it is passed from command line.
   CommandLine* command_line = CommandLine::ForCurrentProcess();
   if (command_line->HasSwitch(switches::kAppIcon)) {
@@ -144,10 +125,14 @@ void Runtime::AttachWindow(const NativeAppWindow::CreateParams& params) {
         content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED,
         content::Source<content::WebContents>(web_contents_.get()));
 
-  window_ = NativeAppWindow::Create(params);
+  window_ = NativeAppWindow::Create(effective_params);
   if (!app_icon_.IsEmpty())
     window_->UpdateIcon(app_icon_);
   window_->Show();
+#if defined(OS_TIZEN)
+  if (root_window_)
+    root_window_->Show();
+#endif
 #endif
 }
 
@@ -161,6 +146,12 @@ void Runtime::LoadURL(const GURL& url) {
 }
 
 void Runtime::Close() {
+  if (window_) {
+    window_->Close();
+    return;
+  }
+  // Runtime should not free itself on Close but be owned
+  // by Application.
   delete this;
 }
 
@@ -236,10 +227,13 @@ void Runtime::HandleKeyboardEvent(
 void Runtime::WebContentsCreated(
     content::WebContents* source_contents,
     int64 source_frame_id,
-    const string16& frame_name,
+    const base::string16& frame_name,
     const GURL& target_url,
     content::WebContents* new_contents) {
   Runtime* new_runtime = new Runtime(new_contents, observer_);
+#if defined(OS_TIZEN)
+  new_runtime->SetRootWindow(root_window_);
+#endif
   new_runtime->AttachDefaultWindow();
 }
 
@@ -261,7 +255,8 @@ void Runtime::DeactivateContents(content::WebContents* contents) {
 
 content::ColorChooser* Runtime::OpenColorChooser(
     content::WebContents* web_contents,
-    SkColor initial_color) {
+    SkColor initial_color,
+    const std::vector<content::ColorSuggestion>& suggestions) {
   return xwalk::ShowColorChooser(web_contents, initial_color);
 }
 
@@ -329,14 +324,16 @@ void Runtime::Observe(int type,
             details).ptr();
 
     if (title->first) {
-      string16 text = title->first->GetTitle();
+      base::string16 text = title->first->GetTitle();
       window_->UpdateTitle(text);
     }
   }
 }
 
 void Runtime::OnWindowDestroyed() {
-  Close();
+  // Runtime should not free itself on Close but be owned
+  // by Application.
+  delete this;
 }
 
 void Runtime::RequestMediaAccessPermission(
@@ -353,5 +350,59 @@ void Runtime::RenderProcessGone(base::TerminationStatus status) {
   XWalkRunner::GetInstance()->OnRenderProcessHostGone(rph);
 }
 
+void Runtime::ApplyWindowDefaultParams(NativeAppWindow::CreateParams* params) {
+  if (!params->delegate)
+    params->delegate = this;
+  if (!params->web_contents)
+    params->web_contents = web_contents_.get();
+  if (params->bounds.IsEmpty())
+    params->bounds = gfx::Rect(0, 0, kDefaultWidth, kDefaultHeight);
+#if defined(OS_TIZEN)
+  if (root_window_)
+    params->parent = root_window_->GetNativeWindow();
+#endif
+  ApplyFullScreenParam(params);
+}
+
+void Runtime::ApplyFullScreenParam(NativeAppWindow::CreateParams* params) {
+  DCHECK(params);
+  // TODO(cmarcelo): This is policy that probably should be moved to outside
+  // Runtime class.
+  CommandLine* cmd_line = CommandLine::ForCurrentProcess();
+  if (cmd_line->HasSwitch(switches::kFullscreen)) {
+    params->state = ui::SHOW_STATE_FULLSCREEN;
+    fullscreen_options_ |= FULLSCREEN_FOR_LAUNCH;
+  }
+}
+
+#if defined(OS_TIZEN)
+void Runtime::CloseRootWindow() {
+  if (root_window_) {
+    root_window_->Close();
+    root_window_ = NULL;
+  }
+}
+
+void Runtime::ApplyRootWindowParams(NativeAppWindow::CreateParams* params) {
+  if (!params->delegate)
+    params->delegate = this;
+  if (params->bounds.IsEmpty())
+    params->bounds = gfx::Rect(0, 0, kDefaultWidth, kDefaultHeight);
+  ApplyFullScreenParam(params);
+}
+
+void Runtime::InitRootWindow() {
+  if (root_window_)
+    return;
 
+  NativeAppWindow::CreateParams params;
+  ApplyRootWindowParams(&params);
+  root_window_ = NativeAppWindow::Create(params);
+}
+
+void Runtime::SetRootWindow(NativeAppWindow* window) {
+  root_window_= window;
+}
+
+#endif
 }  // namespace xwalk