| 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" |
| 11 #include "chrome/browser/engagement/site_engagement_service.h" | 11 #include "chrome/browser/engagement/site_engagement_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" | 15 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/common/chrome_features.h" | 17 #include "chrome/common/chrome_features.h" |
| 18 #include "components/app_modal/javascript_dialog_manager.h" | 18 #include "components/app_modal/javascript_dialog_manager.h" |
| 19 #include "content/public/browser/navigation_handle.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
| 20 | 21 |
| 21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); | 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 bool IsEnabled() { | 26 bool IsEnabled() { |
| 26 return base::FeatureList::IsEnabled(features::kAutoDismissingDialogs); | 27 return base::FeatureList::IsEnabled(features::kAutoDismissingDialogs); |
| 27 } | 28 } |
| 28 | 29 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 59 enum class JavaScriptDialogTabHelper::DismissalCause { | 60 enum class JavaScriptDialogTabHelper::DismissalCause { |
| 60 // This is used for a UMA histogram. Please never alter existing values, only | 61 // This is used for a UMA histogram. Please never alter existing values, only |
| 61 // append new ones. | 62 // append new ones. |
| 62 TAB_HELPER_DESTROYED = 0, | 63 TAB_HELPER_DESTROYED = 0, |
| 63 SUBSEQUENT_DIALOG_SHOWN = 1, | 64 SUBSEQUENT_DIALOG_SHOWN = 1, |
| 64 HANDLE_DIALOG_CALLED = 2, | 65 HANDLE_DIALOG_CALLED = 2, |
| 65 CANCEL_DIALOGS_CALLED = 3, | 66 CANCEL_DIALOGS_CALLED = 3, |
| 66 TAB_HIDDEN = 4, | 67 TAB_HIDDEN = 4, |
| 67 BROWSER_SWITCHED = 5, | 68 BROWSER_SWITCHED = 5, |
| 68 DIALOG_BUTTON_CLICKED = 6, | 69 DIALOG_BUTTON_CLICKED = 6, |
| 70 TAB_NAVIGATED = 7, |
| 69 MAX, | 71 MAX, |
| 70 }; | 72 }; |
| 71 | 73 |
| 72 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( | 74 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( |
| 73 content::WebContents* web_contents) | 75 content::WebContents* web_contents) |
| 74 : content::WebContentsObserver(web_contents) { | 76 : content::WebContentsObserver(web_contents) { |
| 75 } | 77 } |
| 76 | 78 |
| 77 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { | 79 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { |
| 78 if (dialog_) { | 80 if (dialog_) { |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 | 239 |
| 238 // Handle any app-modal dialogs being run by the app-modal dialog system. | 240 // Handle any app-modal dialogs being run by the app-modal dialog system. |
| 239 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, | 241 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, |
| 240 prompt_override); | 242 prompt_override); |
| 241 } | 243 } |
| 242 | 244 |
| 243 void JavaScriptDialogTabHelper::CancelDialogs( | 245 void JavaScriptDialogTabHelper::CancelDialogs( |
| 244 content::WebContents* web_contents, | 246 content::WebContents* web_contents, |
| 245 bool suppress_callbacks, | 247 bool suppress_callbacks, |
| 246 bool reset_state) { | 248 bool reset_state) { |
| 247 if (dialog_) | 249 if (dialog_) { |
| 248 CloseDialog(suppress_callbacks, false, base::string16(), | 250 CloseDialog(suppress_callbacks, false, base::string16(), |
| 249 DismissalCause::CANCEL_DIALOGS_CALLED); | 251 DismissalCause::CANCEL_DIALOGS_CALLED); |
| 252 } |
| 250 | 253 |
| 251 // Cancel any app-modal dialogs being run by the app-modal dialog system. | 254 // Cancel any app-modal dialogs being run by the app-modal dialog system. |
| 252 return AppModalDialogManager()->CancelDialogs( | 255 return AppModalDialogManager()->CancelDialogs( |
| 253 web_contents, suppress_callbacks, reset_state); | 256 web_contents, suppress_callbacks, reset_state); |
| 254 } | 257 } |
| 255 | 258 |
| 256 void JavaScriptDialogTabHelper::WasHidden() { | 259 void JavaScriptDialogTabHelper::WasHidden() { |
| 257 if (dialog_) { | 260 if (dialog_) |
| 258 CloseDialog(false, false, base::string16(), DismissalCause::TAB_HIDDEN); | 261 CloseDialog(false, false, base::string16(), DismissalCause::TAB_HIDDEN); |
| 259 } | 262 } |
| 263 |
| 264 // This function handles the case where browser-side navigation (PlzNavigate) is |
| 265 // enabled. DidStartNavigationToPendingEntry, below, handles the case where |
| 266 // PlzNavigate is not enabled. TODO(avi): When the non-PlzNavigate code is |
| 267 // removed, remove DidStartNavigationToPendingEntry. |
| 268 void JavaScriptDialogTabHelper::DidStartNavigation( |
| 269 content::NavigationHandle* navigation_handle) { |
| 270 // Close the dialog if the user started a new navigation. This allows reloads |
| 271 // and history navigations to proceed. |
| 272 if (dialog_) |
| 273 CloseDialog(false, false, base::string16(), DismissalCause::TAB_NAVIGATED); |
| 274 } |
| 275 |
| 276 // This function handles the case where browser-side navigation (PlzNavigate) is |
| 277 // not enabled. DidStartNavigation, above, handles the case where PlzNavigate is |
| 278 // enabled. TODO(avi): When the non-PlzNavigate code is removed, remove |
| 279 // DidStartNavigationToPendingEntry. |
| 280 void JavaScriptDialogTabHelper::DidStartNavigationToPendingEntry( |
| 281 const GURL& url, |
| 282 content::ReloadType reload_type) { |
| 283 // Close the dialog if the user started a new navigation. This allows reloads |
| 284 // and history navigations to proceed. |
| 285 if (dialog_) |
| 286 CloseDialog(false, false, base::string16(), DismissalCause::TAB_NAVIGATED); |
| 260 } | 287 } |
| 261 | 288 |
| 262 void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) { | 289 void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) { |
| 263 if (dialog_ && !IsWebContentsForemost(web_contents())) { | 290 if (dialog_ && !IsWebContentsForemost(web_contents())) { |
| 264 CloseDialog(false, false, base::string16(), | 291 CloseDialog(false, false, base::string16(), |
| 265 DismissalCause::BROWSER_SWITCHED); | 292 DismissalCause::BROWSER_SWITCHED); |
| 266 } | 293 } |
| 267 } | 294 } |
| 268 | 295 |
| 269 void JavaScriptDialogTabHelper::LogDialogDismissalCause( | 296 void JavaScriptDialogTabHelper::LogDialogDismissalCause( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 dialog_callback_.Run(success, user_input); | 336 dialog_callback_.Run(success, user_input); |
| 310 | 337 |
| 311 ClearDialogInfo(); | 338 ClearDialogInfo(); |
| 312 } | 339 } |
| 313 | 340 |
| 314 void JavaScriptDialogTabHelper::ClearDialogInfo() { | 341 void JavaScriptDialogTabHelper::ClearDialogInfo() { |
| 315 dialog_.reset(); | 342 dialog_.reset(); |
| 316 dialog_callback_.Reset(); | 343 dialog_callback_.Reset(); |
| 317 BrowserList::RemoveObserver(this); | 344 BrowserList::RemoveObserver(this); |
| 318 } | 345 } |
| OLD | NEW |