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 "components/web_modal/web_contents_modal_dialog_manager.h"
6
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"
14
15 using content::WebContents;
16
17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(web_modal::WebContentsModalDialogManager);
18
19 namespace web_modal {
20
~WebContentsModalDialogManager()21 WebContentsModalDialogManager::~WebContentsModalDialogManager() {
22 DCHECK(child_dialogs_.empty());
23 }
24
SetDelegate(WebContentsModalDialogManagerDelegate * d)25 void WebContentsModalDialogManager::SetDelegate(
26 WebContentsModalDialogManagerDelegate* d) {
27 delegate_ = d;
28 // Delegate can be NULL on Views/Win32 during tab drag.
29 native_manager_->HostChanged(d ? d->GetWebContentsModalDialogHost() : NULL);
30 }
31
ShowDialog(NativeWebContentsModalDialog dialog)32 void WebContentsModalDialogManager::ShowDialog(
33 NativeWebContentsModalDialog dialog) {
34 child_dialogs_.push_back(DialogState(dialog));
35
36 native_manager_->ManageDialog(dialog);
37
38 if (child_dialogs_.size() == 1) {
39 if (delegate_ && delegate_->IsWebContentsVisible(web_contents()))
40 native_manager_->ShowDialog(dialog);
41 BlockWebContentsInteraction(true);
42 }
43 }
44
IsDialogActive() const45 bool WebContentsModalDialogManager::IsDialogActive() const {
46 return !child_dialogs_.empty();
47 }
48
FocusTopmostDialog()49 void WebContentsModalDialogManager::FocusTopmostDialog() {
50 DCHECK(!child_dialogs_.empty());
51 native_manager_->FocusDialog(child_dialogs_.front().dialog);
52 }
53
SetCloseOnInterstitialPage(NativeWebContentsModalDialog dialog,bool close)54 void WebContentsModalDialogManager::SetCloseOnInterstitialPage(
55 NativeWebContentsModalDialog dialog,
56 bool close) {
57 WebContentsModalDialogList::iterator loc = FindDialogState(dialog);
58 DCHECK(loc != child_dialogs_.end());
59 loc->close_on_interstitial_webui = close;
60 }
61
GetWebContents() const62 content::WebContents* WebContentsModalDialogManager::GetWebContents() const {
63 return web_contents();
64 }
65
WillClose(NativeWebContentsModalDialog dialog)66 void WebContentsModalDialogManager::WillClose(
67 NativeWebContentsModalDialog dialog) {
68 WebContentsModalDialogList::iterator i = FindDialogState(dialog);
69
70 // The Views tab contents modal dialog calls WillClose twice. Ignore the
71 // second invocation.
72 if (i == child_dialogs_.end())
73 return;
74
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);
80
81 BlockWebContentsInteraction(!child_dialogs_.empty());
82 }
83
WebContentsModalDialogManager(content::WebContents * web_contents)84 WebContentsModalDialogManager::WebContentsModalDialogManager(
85 content::WebContents* web_contents)
86 : content::WebContentsObserver(web_contents),
87 delegate_(NULL),
88 native_manager_(CreateNativeManager(this)),
89 closing_all_dialogs_(false) {
90 DCHECK(native_manager_);
91 }
92
DialogState(NativeWebContentsModalDialog dialog)93 WebContentsModalDialogManager::DialogState::DialogState(
94 NativeWebContentsModalDialog dialog)
95 : dialog(dialog),
96 #if defined(OS_WIN) || defined(USE_AURA)
97 close_on_interstitial_webui(true)
98 #else
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)
102 #endif
103 {
104 }
105
106 WebContentsModalDialogManager::WebContentsModalDialogList::iterator
FindDialogState(NativeWebContentsModalDialog dialog)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)
112 break;
113 }
114
115 return i;
116 }
117
BlockWebContentsInteraction(bool blocked)118 void WebContentsModalDialogManager::BlockWebContentsInteraction(bool blocked) {
119 WebContents* contents = web_contents();
120 if (!contents) {
121 // The WebContents has already disconnected.
122 return;
123 }
124
125 // RenderViewHost may be NULL during shutdown.
126 content::RenderViewHost* host = contents->GetRenderViewHost();
127 if (host)
128 host->SetIgnoreInputEvents(blocked);
129 if (delegate_)
130 delegate_->SetWebContentsBlocked(contents, blocked);
131 }
132
CloseAllDialogs()133 void WebContentsModalDialogManager::CloseAllDialogs() {
134 closing_all_dialogs_ = true;
135
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);
139
140 closing_all_dialogs_ = false;
141 }
142
DidNavigateMainFrame(const content::LoadCommittedDetails & details,const content::FrameNavigateParams & params)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))
150 CloseAllDialogs();
151 }
152
DidGetIgnoredUIEvent()153 void WebContentsModalDialogManager::DidGetIgnoredUIEvent() {
154 if (!child_dialogs_.empty())
155 native_manager_->FocusDialog(child_dialogs_.front().dialog);
156 }
157
WasShown()158 void WebContentsModalDialogManager::WasShown() {
159 if (!child_dialogs_.empty())
160 native_manager_->ShowDialog(child_dialogs_.front().dialog);
161 }
162
WasHidden()163 void WebContentsModalDialogManager::WasHidden() {
164 if (!child_dialogs_.empty())
165 native_manager_->HideDialog(child_dialogs_.front().dialog);
166 }
167
WebContentsDestroyed(WebContents * tab)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.
173 CloseAllDialogs();
174 }
175
DidAttachInterstitialPage()176 void WebContentsModalDialogManager::DidAttachInterstitialPage() {
177 // Copy the dialogs so we can close and remove them while iterating over the
178 // list.
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);
184 }
185 }
186
187 } // namespace web_modal
188