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.
5 #include "components/web_modal/web_contents_modal_dialog_manager.h"
7 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
8 #include "content/public/browser/navigation_details.h"
9 #include "content/public/browser/navigation_entry.h"
10 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/web_contents_view.h"
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
15 using content::WebContents;
17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(web_modal::WebContentsModalDialogManager);
21 WebContentsModalDialogManager::~WebContentsModalDialogManager() {
22 DCHECK(child_dialogs_.empty());
25 void WebContentsModalDialogManager::SetDelegate(
26 WebContentsModalDialogManagerDelegate* d) {
28 // Delegate can be NULL on Views/Win32 during tab drag.
29 native_manager_->HostChanged(d ? d->GetWebContentsModalDialogHost() : NULL);
32 void WebContentsModalDialogManager::ShowDialog(
33 NativeWebContentsModalDialog dialog) {
34 child_dialogs_.push_back(DialogState(dialog));
36 native_manager_->ManageDialog(dialog);
38 if (child_dialogs_.size() == 1) {
39 if (delegate_ && delegate_->IsWebContentsVisible(web_contents()))
40 native_manager_->ShowDialog(dialog);
41 BlockWebContentsInteraction(true);
45 bool WebContentsModalDialogManager::IsDialogActive() const {
46 return !child_dialogs_.empty();
49 void WebContentsModalDialogManager::FocusTopmostDialog() {
50 DCHECK(!child_dialogs_.empty());
51 native_manager_->FocusDialog(child_dialogs_.front().dialog);
54 void WebContentsModalDialogManager::SetCloseOnInterstitialPage(
55 NativeWebContentsModalDialog dialog,
57 WebContentsModalDialogList::iterator loc = FindDialogState(dialog);
58 DCHECK(loc != child_dialogs_.end());
59 loc->close_on_interstitial_webui = close;
62 content::WebContents* WebContentsModalDialogManager::GetWebContents() const {
63 return web_contents();
66 void WebContentsModalDialogManager::WillClose(
67 NativeWebContentsModalDialog dialog) {
68 WebContentsModalDialogList::iterator i = FindDialogState(dialog);
70 // The Views tab contents modal dialog calls WillClose twice. Ignore the
72 if (i == child_dialogs_.end())
75 bool removed_topmost_dialog = i == child_dialogs_.begin();
76 child_dialogs_.erase(i);
77 if (!child_dialogs_.empty() && removed_topmost_dialog &&
78 !closing_all_dialogs_)
79 native_manager_->ShowDialog(child_dialogs_.front().dialog);
81 BlockWebContentsInteraction(!child_dialogs_.empty());
84 WebContentsModalDialogManager::WebContentsModalDialogManager(
85 content::WebContents* web_contents)
86 : content::WebContentsObserver(web_contents),
88 native_manager_(CreateNativeManager(this)),
89 closing_all_dialogs_(false) {
90 DCHECK(native_manager_);
93 WebContentsModalDialogManager::DialogState::DialogState(
94 NativeWebContentsModalDialog dialog)
96 #if defined(OS_WIN) || defined(USE_AURA)
97 close_on_interstitial_webui(true)
99 // TODO(wittman): Test that closing on interstitial webui works properly
100 // on Mac and use the true default for all platforms.
101 close_on_interstitial_webui(false)
106 WebContentsModalDialogManager::WebContentsModalDialogList::iterator
107 WebContentsModalDialogManager::FindDialogState(
108 NativeWebContentsModalDialog dialog) {
109 WebContentsModalDialogList::iterator i;
110 for (i = child_dialogs_.begin(); i != child_dialogs_.end(); ++i) {
111 if (i->dialog == dialog)
118 void WebContentsModalDialogManager::BlockWebContentsInteraction(bool blocked) {
119 WebContents* contents = web_contents();
121 // The WebContents has already disconnected.
125 // RenderViewHost may be NULL during shutdown.
126 content::RenderViewHost* host = contents->GetRenderViewHost();
128 host->SetIgnoreInputEvents(blocked);
130 delegate_->SetWebContentsBlocked(contents, blocked);
133 void WebContentsModalDialogManager::CloseAllDialogs() {
134 closing_all_dialogs_ = true;
136 // Clear out any dialogs since we are leaving this page entirely.
137 while (!child_dialogs_.empty())
138 native_manager_->CloseDialog(child_dialogs_.front().dialog);
140 closing_all_dialogs_ = false;
143 void WebContentsModalDialogManager::DidNavigateMainFrame(
144 const content::LoadCommittedDetails& details,
145 const content::FrameNavigateParams& params) {
146 // Close constrained windows if necessary.
147 if (!net::registry_controlled_domains::SameDomainOrHost(
148 details.previous_url, details.entry->GetURL(),
149 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES))
153 void WebContentsModalDialogManager::DidGetIgnoredUIEvent() {
154 if (!child_dialogs_.empty())
155 native_manager_->FocusDialog(child_dialogs_.front().dialog);
158 void WebContentsModalDialogManager::WasShown() {
159 if (!child_dialogs_.empty())
160 native_manager_->ShowDialog(child_dialogs_.front().dialog);
163 void WebContentsModalDialogManager::WasHidden() {
164 if (!child_dialogs_.empty())
165 native_manager_->HideDialog(child_dialogs_.front().dialog);
168 void WebContentsModalDialogManager::WebContentsDestroyed(WebContents* tab) {
169 // First cleanly close all child dialogs.
170 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
171 // some of these to close. CloseAllDialogs is async, so it might get called
172 // twice before it runs.
176 void WebContentsModalDialogManager::DidAttachInterstitialPage() {
177 // Copy the dialogs so we can close and remove them while iterating over the
179 WebContentsModalDialogList dialogs(child_dialogs_);
180 for (WebContentsModalDialogList::iterator it = dialogs.begin();
181 it != dialogs.end(); ++it) {
182 if (it->close_on_interstitial_webui)
183 native_manager_->CloseDialog(it->dialog);
187 } // namespace web_modal