| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h" | 5 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/feature_list.h" | 9 #include "base/feature_list.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 MAX, | 71 MAX, |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( | 74 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( |
| 75 content::WebContents* web_contents) | 75 content::WebContents* web_contents) |
| 76 : content::WebContentsObserver(web_contents) { | 76 : content::WebContentsObserver(web_contents) { |
| 77 } | 77 } |
| 78 | 78 |
| 79 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { | 79 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { |
| 80 if (dialog_) { | 80 if (dialog_) { |
| 81 CloseDialog(true /*suppress_callback*/, false, base::string16(), | 81 CloseDialog(false, base::string16(), DismissalCause::TAB_HELPER_DESTROYED); |
| 82 DismissalCause::TAB_HELPER_DESTROYED); | |
| 83 } | 82 } |
| 84 } | 83 } |
| 85 | 84 |
| 86 void JavaScriptDialogTabHelper::SetDialogShownCallbackForTesting( | 85 void JavaScriptDialogTabHelper::SetDialogShownCallbackForTesting( |
| 87 base::Closure callback) { | 86 base::Closure callback) { |
| 88 dialog_shown_ = callback; | 87 dialog_shown_ = callback; |
| 89 } | 88 } |
| 90 | 89 |
| 91 void JavaScriptDialogTabHelper::RunJavaScriptDialog( | 90 void JavaScriptDialogTabHelper::RunJavaScriptDialog( |
| 92 content::WebContents* alerting_web_contents, | 91 content::WebContents* alerting_web_contents, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 "A window.prompt() dialog generated by this page was suppressed " | 140 "A window.prompt() dialog generated by this page was suppressed " |
| 142 "because this page is not the active tab of the front window. " | 141 "because this page is not the active tab of the front window. " |
| 143 "Please make sure your dialogs are triggered by user interactions " | 142 "Please make sure your dialogs are triggered by user interactions " |
| 144 "to avoid this situation. " | 143 "to avoid this situation. " |
| 145 "https://www.chromestatus.com/feature/5637107137642496"); | 144 "https://www.chromestatus.com/feature/5637107137642496"); |
| 146 return; | 145 return; |
| 147 } | 146 } |
| 148 | 147 |
| 149 if (dialog_) { | 148 if (dialog_) { |
| 150 // There's already a dialog up; clear it out. | 149 // There's already a dialog up; clear it out. |
| 151 CloseDialog(false, false, base::string16(), | 150 CloseDialog(false, base::string16(), |
| 152 DismissalCause::SUBSEQUENT_DIALOG_SHOWN); | 151 DismissalCause::SUBSEQUENT_DIALOG_SHOWN); |
| 153 } | 152 } |
| 154 | 153 |
| 155 base::string16 title = | 154 base::string16 title = |
| 156 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); | 155 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); |
| 157 dialog_callback_ = callback; | 156 dialog_callback_ = callback; |
| 158 message_type_ = message_type; | 157 message_type_ = message_type; |
| 159 dialog_ = JavaScriptDialog::Create( | 158 dialog_ = JavaScriptDialog::Create( |
| 160 parent_web_contents, alerting_web_contents, title, message_type, | 159 parent_web_contents, alerting_web_contents, title, message_type, |
| 161 message_text, default_prompt_text, | 160 message_text, default_prompt_text, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 return AppModalDialogManager()->RunBeforeUnloadDialog( | 223 return AppModalDialogManager()->RunBeforeUnloadDialog( |
| 225 web_contents, is_reload, | 224 web_contents, is_reload, |
| 226 base::Bind(&SaveUnloadUmaStats, engagement_score, callback)); | 225 base::Bind(&SaveUnloadUmaStats, engagement_score, callback)); |
| 227 } | 226 } |
| 228 | 227 |
| 229 bool JavaScriptDialogTabHelper::HandleJavaScriptDialog( | 228 bool JavaScriptDialogTabHelper::HandleJavaScriptDialog( |
| 230 content::WebContents* web_contents, | 229 content::WebContents* web_contents, |
| 231 bool accept, | 230 bool accept, |
| 232 const base::string16* prompt_override) { | 231 const base::string16* prompt_override) { |
| 233 if (dialog_) { | 232 if (dialog_) { |
| 234 CloseDialog(false /*suppress_callback*/, accept, | 233 CloseDialog(accept, prompt_override ? *prompt_override : base::string16(), |
| 235 prompt_override ? *prompt_override : base::string16(), | |
| 236 DismissalCause::HANDLE_DIALOG_CALLED); | 234 DismissalCause::HANDLE_DIALOG_CALLED); |
| 237 return true; | 235 return true; |
| 238 } | 236 } |
| 239 | 237 |
| 240 // Handle any app-modal dialogs being run by the app-modal dialog system. | 238 // Handle any app-modal dialogs being run by the app-modal dialog system. |
| 241 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, | 239 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, |
| 242 prompt_override); | 240 prompt_override); |
| 243 } | 241 } |
| 244 | 242 |
| 245 void JavaScriptDialogTabHelper::CancelDialogs( | 243 void JavaScriptDialogTabHelper::CancelDialogs( |
| 246 content::WebContents* web_contents, | 244 content::WebContents* web_contents, |
| 247 bool suppress_callbacks, | 245 bool suppress_callbacks, |
| 248 bool reset_state) { | 246 bool reset_state) { |
| 249 if (dialog_) { | 247 if (dialog_) { |
| 250 CloseDialog(suppress_callbacks, false, base::string16(), | 248 CloseDialog(false, base::string16(), DismissalCause::CANCEL_DIALOGS_CALLED); |
| 251 DismissalCause::CANCEL_DIALOGS_CALLED); | |
| 252 } | 249 } |
| 253 | 250 |
| 254 // Cancel any app-modal dialogs being run by the app-modal dialog system. | 251 // Cancel any app-modal dialogs being run by the app-modal dialog system. |
| 255 return AppModalDialogManager()->CancelDialogs( | 252 return AppModalDialogManager()->CancelDialogs( |
| 256 web_contents, suppress_callbacks, reset_state); | 253 web_contents, suppress_callbacks, reset_state); |
| 257 } | 254 } |
| 258 | 255 |
| 259 void JavaScriptDialogTabHelper::WasHidden() { | 256 void JavaScriptDialogTabHelper::WasHidden() { |
| 260 if (dialog_) | 257 if (dialog_) |
| 261 CloseDialog(false, false, base::string16(), DismissalCause::TAB_HIDDEN); | 258 CloseDialog(false, base::string16(), DismissalCause::TAB_HIDDEN); |
| 262 } | 259 } |
| 263 | 260 |
| 264 // This function handles the case where browser-side navigation (PlzNavigate) is | 261 // This function handles the case where browser-side navigation (PlzNavigate) is |
| 265 // enabled. DidStartNavigationToPendingEntry, below, handles the case where | 262 // enabled. DidStartNavigationToPendingEntry, below, handles the case where |
| 266 // PlzNavigate is not enabled. TODO(avi): When the non-PlzNavigate code is | 263 // PlzNavigate is not enabled. TODO(avi): When the non-PlzNavigate code is |
| 267 // removed, remove DidStartNavigationToPendingEntry. | 264 // removed, remove DidStartNavigationToPendingEntry. |
| 268 void JavaScriptDialogTabHelper::DidStartNavigation( | 265 void JavaScriptDialogTabHelper::DidStartNavigation( |
| 269 content::NavigationHandle* navigation_handle) { | 266 content::NavigationHandle* navigation_handle) { |
| 270 // Close the dialog if the user started a new navigation. This allows reloads | 267 // Close the dialog if the user started a new navigation. This allows reloads |
| 271 // and history navigations to proceed. | 268 // and history navigations to proceed. |
| 272 if (dialog_) | 269 if (dialog_) |
| 273 CloseDialog(false, false, base::string16(), DismissalCause::TAB_NAVIGATED); | 270 CloseDialog(false, base::string16(), DismissalCause::TAB_NAVIGATED); |
| 274 } | 271 } |
| 275 | 272 |
| 276 // This function handles the case where browser-side navigation (PlzNavigate) is | 273 // This function handles the case where browser-side navigation (PlzNavigate) is |
| 277 // not enabled. DidStartNavigation, above, handles the case where PlzNavigate is | 274 // not enabled. DidStartNavigation, above, handles the case where PlzNavigate is |
| 278 // enabled. TODO(avi): When the non-PlzNavigate code is removed, remove | 275 // enabled. TODO(avi): When the non-PlzNavigate code is removed, remove |
| 279 // DidStartNavigationToPendingEntry. | 276 // DidStartNavigationToPendingEntry. |
| 280 void JavaScriptDialogTabHelper::DidStartNavigationToPendingEntry( | 277 void JavaScriptDialogTabHelper::DidStartNavigationToPendingEntry( |
| 281 const GURL& url, | 278 const GURL& url, |
| 282 content::ReloadType reload_type) { | 279 content::ReloadType reload_type) { |
| 283 // Close the dialog if the user started a new navigation. This allows reloads | 280 // Close the dialog if the user started a new navigation. This allows reloads |
| 284 // and history navigations to proceed. | 281 // and history navigations to proceed. |
| 285 if (dialog_) | 282 if (dialog_) |
| 286 CloseDialog(false, false, base::string16(), DismissalCause::TAB_NAVIGATED); | 283 CloseDialog(false, base::string16(), DismissalCause::TAB_NAVIGATED); |
| 287 } | 284 } |
| 288 | 285 |
| 289 void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) { | 286 void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) { |
| 290 if (dialog_ && !IsWebContentsForemost(web_contents())) { | 287 if (dialog_ && !IsWebContentsForemost(web_contents())) { |
| 291 CloseDialog(false, false, base::string16(), | 288 CloseDialog(false, base::string16(), DismissalCause::BROWSER_SWITCHED); |
| 292 DismissalCause::BROWSER_SWITCHED); | |
| 293 } | 289 } |
| 294 } | 290 } |
| 295 | 291 |
| 296 void JavaScriptDialogTabHelper::LogDialogDismissalCause( | 292 void JavaScriptDialogTabHelper::LogDialogDismissalCause( |
| 297 JavaScriptDialogTabHelper::DismissalCause cause) { | 293 JavaScriptDialogTabHelper::DismissalCause cause) { |
| 298 switch (message_type_) { | 294 switch (message_type_) { |
| 299 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: | 295 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: |
| 300 UMA_HISTOGRAM_ENUMERATION("JSDialogs.DismissalCause.Alert", | 296 UMA_HISTOGRAM_ENUMERATION("JSDialogs.DismissalCause.Alert", |
| 301 static_cast<int>(cause), | 297 static_cast<int>(cause), |
| 302 static_cast<int>(DismissalCause::MAX)); | 298 static_cast<int>(DismissalCause::MAX)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 317 void JavaScriptDialogTabHelper::OnDialogClosed( | 313 void JavaScriptDialogTabHelper::OnDialogClosed( |
| 318 DialogClosedCallback callback, | 314 DialogClosedCallback callback, |
| 319 bool success, | 315 bool success, |
| 320 const base::string16& user_input) { | 316 const base::string16& user_input) { |
| 321 LogDialogDismissalCause(DismissalCause::DIALOG_BUTTON_CLICKED); | 317 LogDialogDismissalCause(DismissalCause::DIALOG_BUTTON_CLICKED); |
| 322 callback.Run(success, user_input); | 318 callback.Run(success, user_input); |
| 323 | 319 |
| 324 ClearDialogInfo(); | 320 ClearDialogInfo(); |
| 325 } | 321 } |
| 326 | 322 |
| 327 void JavaScriptDialogTabHelper::CloseDialog(bool suppress_callback, | 323 void JavaScriptDialogTabHelper::CloseDialog(bool success, |
| 328 bool success, | |
| 329 const base::string16& user_input, | 324 const base::string16& user_input, |
| 330 DismissalCause cause) { | 325 DismissalCause cause) { |
| 331 DCHECK(dialog_); | 326 DCHECK(dialog_); |
| 332 LogDialogDismissalCause(cause); | 327 LogDialogDismissalCause(cause); |
| 333 | 328 |
| 334 dialog_->CloseDialogWithoutCallback(); | 329 dialog_->CloseDialogWithoutCallback(); |
| 335 if (!suppress_callback) | 330 dialog_callback_.Run(success, user_input); |
| 336 dialog_callback_.Run(success, user_input); | |
| 337 | 331 |
| 338 ClearDialogInfo(); | 332 ClearDialogInfo(); |
| 339 } | 333 } |
| 340 | 334 |
| 341 void JavaScriptDialogTabHelper::ClearDialogInfo() { | 335 void JavaScriptDialogTabHelper::ClearDialogInfo() { |
| 342 dialog_.reset(); | 336 dialog_.reset(); |
| 343 dialog_callback_.Reset(); | 337 dialog_callback_.Reset(); |
| 344 BrowserList::RemoveObserver(this); | 338 BrowserList::RemoveObserver(this); |
| 345 } | 339 } |
| OLD | NEW |