OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/web_contents_modal_dialog_manager.h" |
| 6 |
| 7 #include "chrome/browser/ui/web_contents_modal_dialog.h" |
| 8 #include "chrome/browser/ui/web_contents_modal_dialog_manager_delegate.h" |
| 9 #include "chrome/common/render_messages.h" |
| 10 #include "content/public/browser/navigation_details.h" |
| 11 #include "content/public/browser/navigation_entry.h" |
| 12 #include "content/public/browser/render_view_host.h" |
| 13 #include "content/public/browser/render_widget_host_view.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 16 |
| 17 using content::WebContents; |
| 18 |
| 19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsModalDialogManager) |
| 20 |
| 21 WebContentsModalDialogManager::WebContentsModalDialogManager( |
| 22 content::WebContents* web_contents) |
| 23 : content::WebContentsObserver(web_contents), |
| 24 delegate_(NULL) { |
| 25 } |
| 26 |
| 27 WebContentsModalDialogManager::~WebContentsModalDialogManager() { |
| 28 DCHECK(child_dialogs_.empty()); |
| 29 } |
| 30 |
| 31 void WebContentsModalDialogManager::AddDialog( |
| 32 WebContentsModalDialog* dialog) { |
| 33 child_dialogs_.push_back(dialog); |
| 34 |
| 35 if (child_dialogs_.size() == 1 && dialog->CanShowWebContentsModalDialog()) { |
| 36 dialog->ShowWebContentsModalDialog(); |
| 37 BlockWebContentsInteraction(true); |
| 38 } |
| 39 } |
| 40 |
| 41 void WebContentsModalDialogManager::CloseAllDialogs() { |
| 42 // Clear out any dialogs since we are leaving this page entirely. To ensure |
| 43 // that we iterate over every element in child_dialogs_ we need to use a copy |
| 44 // of child_dialogs_. Otherwise if dialog->CloseWebContentsModalDialog() |
| 45 // modifies child_dialogs_ we could end up skipping some elements. |
| 46 WebContentsModalDialogList child_dialogs_copy(child_dialogs_); |
| 47 for (WebContentsModalDialogList::iterator it = child_dialogs_copy.begin(); |
| 48 it != child_dialogs_copy.end(); ++it) { |
| 49 WebContentsModalDialog* dialog = *it; |
| 50 if (dialog) { |
| 51 dialog->CloseWebContentsModalDialog(); |
| 52 BlockWebContentsInteraction(false); |
| 53 } |
| 54 } |
| 55 } |
| 56 |
| 57 void WebContentsModalDialogManager::WillClose(WebContentsModalDialog* dialog) { |
| 58 WebContentsModalDialogList::iterator i( |
| 59 std::find(child_dialogs_.begin(), child_dialogs_.end(), dialog)); |
| 60 bool removed_topmost_dialog = i == child_dialogs_.begin(); |
| 61 if (i != child_dialogs_.end()) |
| 62 child_dialogs_.erase(i); |
| 63 if (child_dialogs_.empty()) { |
| 64 BlockWebContentsInteraction(false); |
| 65 } else { |
| 66 if (removed_topmost_dialog) |
| 67 child_dialogs_[0]->ShowWebContentsModalDialog(); |
| 68 BlockWebContentsInteraction(true); |
| 69 } |
| 70 } |
| 71 |
| 72 void WebContentsModalDialogManager::BlockWebContentsInteraction(bool blocked) { |
| 73 WebContents* contents = web_contents(); |
| 74 if (!contents) { |
| 75 // The WebContents has already disconnected. |
| 76 return; |
| 77 } |
| 78 |
| 79 // RenderViewHost may be NULL during shutdown. |
| 80 content::RenderViewHost* host = contents->GetRenderViewHost(); |
| 81 if (host) { |
| 82 host->SetIgnoreInputEvents(blocked); |
| 83 host->Send(new ChromeViewMsg_SetVisuallyDeemphasized( |
| 84 host->GetRoutingID(), blocked)); |
| 85 } |
| 86 if (delegate_) |
| 87 delegate_->SetWebContentsBlocked(contents, blocked); |
| 88 } |
| 89 |
| 90 void WebContentsModalDialogManager::DidNavigateMainFrame( |
| 91 const content::LoadCommittedDetails& details, |
| 92 const content::FrameNavigateParams& params) { |
| 93 // Close dialogs if necessary. |
| 94 if (!net::RegistryControlledDomainService::SameDomainOrHost( |
| 95 details.previous_url, details.entry->GetURL())) |
| 96 CloseAllDialogs(); |
| 97 } |
| 98 |
| 99 void WebContentsModalDialogManager::DidGetIgnoredUIEvent() { |
| 100 if (dialog_count()) { |
| 101 WebContentsModalDialog* dialog = *dialog_begin(); |
| 102 dialog->FocusWebContentsModalDialog(); |
| 103 } |
| 104 } |
| 105 |
| 106 void WebContentsModalDialogManager::WebContentsDestroyed(WebContents* tab) { |
| 107 // First cleanly close all child dialogs. |
| 108 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked |
| 109 // some of these to close. CloseAllDialogs is async, so it might get called |
| 110 // twice before it runs. |
| 111 CloseAllDialogs(); |
| 112 } |
OLD | NEW |