Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / web_modal / web_contents_modal_dialog_manager.cc
index 53b4667..7ae8a2d 100644 (file)
@@ -9,7 +9,6 @@
 #include "content/public/browser/navigation_entry.h"
 #include "content/public/browser/render_view_host.h"
 #include "content/public/browser/web_contents.h"
-#include "content/public/browser/web_contents_view.h"
 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
 
 using content::WebContents;
@@ -25,20 +24,33 @@ WebContentsModalDialogManager::~WebContentsModalDialogManager() {
 void WebContentsModalDialogManager::SetDelegate(
     WebContentsModalDialogManagerDelegate* d) {
   delegate_ = d;
-  // Delegate can be NULL on Views/Win32 during tab drag.
-  native_manager_->HostChanged(d ? d->GetWebContentsModalDialogHost() : NULL);
+
+  for (WebContentsModalDialogList::iterator it = child_dialogs_.begin();
+       it != child_dialogs_.end(); it++) {
+    // Delegate can be NULL on Views/Win32 during tab drag.
+    (*it)->manager->HostChanged(d ? d->GetWebContentsModalDialogHost() : NULL);
+  }
 }
 
-void WebContentsModalDialogManager::ShowDialog(
+void WebContentsModalDialogManager::ShowModalDialog(
     NativeWebContentsModalDialog dialog) {
-  child_dialogs_.push_back(DialogState(dialog));
+  scoped_ptr<SingleWebContentsDialogManager> mgr(
+      CreateNativeWebModalManager(dialog, this));
+  ShowDialogWithManager(dialog, mgr.Pass());
+}
 
-  native_manager_->ManageDialog(dialog);
+// TODO(gbillock): Maybe "ShowBubbleWithManager"?
+void WebContentsModalDialogManager::ShowDialogWithManager(
+    NativeWebContentsModalDialog dialog,
+    scoped_ptr<SingleWebContentsDialogManager> manager) {
+  if (delegate_)
+    manager->HostChanged(delegate_->GetWebContentsModalDialogHost());
+  child_dialogs_.push_back(new DialogState(dialog, manager.Pass()));
 
   if (child_dialogs_.size() == 1) {
-    if (delegate_ && delegate_->IsWebContentsVisible(web_contents()))
-      native_manager_->ShowDialog(dialog);
     BlockWebContentsInteraction(true);
+    if (delegate_ && delegate_->IsWebContentsVisible(web_contents()))
+      child_dialogs_.back()->manager->Show();
   }
 }
 
@@ -48,7 +60,7 @@ bool WebContentsModalDialogManager::IsDialogActive() const {
 
 void WebContentsModalDialogManager::FocusTopmostDialog() {
   DCHECK(!child_dialogs_.empty());
-  native_manager_->FocusDialog(child_dialogs_.front().dialog);
+  child_dialogs_.front()->manager->Focus();
 }
 
 void WebContentsModalDialogManager::SetCloseOnInterstitialPage(
@@ -56,7 +68,7 @@ void WebContentsModalDialogManager::SetCloseOnInterstitialPage(
     bool close) {
   WebContentsModalDialogList::iterator loc = FindDialogState(dialog);
   DCHECK(loc != child_dialogs_.end());
-  loc->close_on_interstitial_webui = close;
+  (*loc)->close_on_interstitial_webui = close;
 }
 
 content::WebContents* WebContentsModalDialogManager::GetWebContents() const {
@@ -65,18 +77,20 @@ content::WebContents* WebContentsModalDialogManager::GetWebContents() const {
 
 void WebContentsModalDialogManager::WillClose(
     NativeWebContentsModalDialog dialog) {
-  WebContentsModalDialogList::iterator i = FindDialogState(dialog);
+  WebContentsModalDialogList::iterator dlg = FindDialogState(dialog);
 
   // The Views tab contents modal dialog calls WillClose twice.  Ignore the
   // second invocation.
-  if (i == child_dialogs_.end())
+  if (dlg == child_dialogs_.end())
     return;
 
-  bool removed_topmost_dialog = i == child_dialogs_.begin();
-  child_dialogs_.erase(i);
+  bool removed_topmost_dialog = dlg == child_dialogs_.begin();
+  scoped_ptr<DialogState> deleter(*dlg);
+  child_dialogs_.erase(dlg);
   if (!child_dialogs_.empty() && removed_topmost_dialog &&
-      !closing_all_dialogs_)
-    native_manager_->ShowDialog(child_dialogs_.front().dialog);
+      !closing_all_dialogs_) {
+    child_dialogs_.front()->manager->Show();
+  }
 
   BlockWebContentsInteraction(!child_dialogs_.empty());
 }
@@ -85,15 +99,15 @@ WebContentsModalDialogManager::WebContentsModalDialogManager(
     content::WebContents* web_contents)
     : content::WebContentsObserver(web_contents),
       delegate_(NULL),
-      native_manager_(CreateNativeManager(this)),
       closing_all_dialogs_(false) {
-  DCHECK(native_manager_);
 }
 
 WebContentsModalDialogManager::DialogState::DialogState(
-    NativeWebContentsModalDialog dialog)
+    NativeWebContentsModalDialog dialog,
+    scoped_ptr<SingleWebContentsDialogManager> mgr)
     : dialog(dialog),
-#if defined(OS_WIN) || defined(USE_AURA)
+      manager(mgr.release()),
+#if defined(USE_AURA)
       close_on_interstitial_webui(true)
 #else
       // TODO(wittman): Test that closing on interstitial webui works properly
@@ -103,18 +117,24 @@ WebContentsModalDialogManager::DialogState::DialogState(
                                          {
 }
 
+WebContentsModalDialogManager::DialogState::~DialogState() {}
+
 WebContentsModalDialogManager::WebContentsModalDialogList::iterator
     WebContentsModalDialogManager::FindDialogState(
         NativeWebContentsModalDialog dialog) {
   WebContentsModalDialogList::iterator i;
   for (i = child_dialogs_.begin(); i != child_dialogs_.end(); ++i) {
-    if (i->dialog == dialog)
+    if ((*i)->dialog == dialog)
       break;
   }
 
   return i;
 }
 
+// TODO(gbillock): Move this to Views impl within Show()? It would
+// call WebContents* contents = native_delegate_->GetWebContents(); and
+// then set the block state. Advantage: could restrict some of the
+// WCMDM delegate methods, then, and pass them behind the scenes.
 void WebContentsModalDialogManager::BlockWebContentsInteraction(bool blocked) {
   WebContents* contents = web_contents();
   if (!contents) {
@@ -134,8 +154,9 @@ void WebContentsModalDialogManager::CloseAllDialogs() {
   closing_all_dialogs_ = true;
 
   // Clear out any dialogs since we are leaving this page entirely.
-  while (!child_dialogs_.empty())
-    native_manager_->CloseDialog(child_dialogs_.front().dialog);
+  while (!child_dialogs_.empty()) {
+    child_dialogs_.front()->manager->Close();
+  }
 
   closing_all_dialogs_ = false;
 }
@@ -151,21 +172,22 @@ void WebContentsModalDialogManager::DidNavigateMainFrame(
 }
 
 void WebContentsModalDialogManager::DidGetIgnoredUIEvent() {
-  if (!child_dialogs_.empty())
-    native_manager_->FocusDialog(child_dialogs_.front().dialog);
+  if (!child_dialogs_.empty()) {
+    child_dialogs_.front()->manager->Focus();
+  }
 }
 
 void WebContentsModalDialogManager::WasShown() {
   if (!child_dialogs_.empty())
-    native_manager_->ShowDialog(child_dialogs_.front().dialog);
+    child_dialogs_.front()->manager->Show();
 }
 
 void WebContentsModalDialogManager::WasHidden() {
   if (!child_dialogs_.empty())
-    native_manager_->HideDialog(child_dialogs_.front().dialog);
+    child_dialogs_.front()->manager->Hide();
 }
 
-void WebContentsModalDialogManager::WebContentsDestroyed(WebContents* tab) {
+void WebContentsModalDialogManager::WebContentsDestroyed() {
   // First cleanly close all child dialogs.
   // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
   // some of these to close.  CloseAllDialogs is async, so it might get called
@@ -179,8 +201,8 @@ void WebContentsModalDialogManager::DidAttachInterstitialPage() {
   WebContentsModalDialogList dialogs(child_dialogs_);
   for (WebContentsModalDialogList::iterator it = dialogs.begin();
        it != dialogs.end(); ++it) {
-    if (it->close_on_interstitial_webui)
-      native_manager_->CloseDialog(it->dialog);
+    if ((*it)->close_on_interstitial_webui)
+      (*it)->manager->Close();
   }
 }