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/feature_list.h" | 9 #include "base/feature_list.h" |
9 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
10 #include "chrome/browser/engagement/site_engagement_service.h" | 11 #include "chrome/browser/engagement/site_engagement_service.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
14 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" | 15 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" |
15 #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" |
16 #include "components/app_modal/javascript_dialog_manager.h" | 18 #include "components/app_modal/javascript_dialog_manager.h" |
17 #include "content/public/browser/web_contents_delegate.h" | |
18 | 19 |
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); | 20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 const base::Feature kAutoDismissingDialogsFeature{ | |
24 "AutoDismissingDialogs", base::FEATURE_DISABLED_BY_DEFAULT}; | |
25 | |
26 bool IsEnabled() { | 24 bool IsEnabled() { |
27 return base::FeatureList::IsEnabled(kAutoDismissingDialogsFeature); | 25 return base::FeatureList::IsEnabled(features::kAutoDismissingDialogs); |
28 } | 26 } |
29 | 27 |
30 app_modal::JavaScriptDialogManager* AppModalDialogManager() { | 28 app_modal::JavaScriptDialogManager* AppModalDialogManager() { |
31 return app_modal::JavaScriptDialogManager::GetInstance(); | 29 return app_modal::JavaScriptDialogManager::GetInstance(); |
32 } | 30 } |
33 | 31 |
34 bool IsWebContentsForemost(content::WebContents* web_contents) { | 32 bool IsWebContentsForemost(content::WebContents* web_contents) { |
35 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | 33 Browser* browser = BrowserList::GetInstance()->GetLastActive(); |
36 DCHECK(browser); | 34 DCHECK(browser); |
37 return browser->tab_strip_model()->GetActiveWebContents() == web_contents; | 35 return browser->tab_strip_model()->GetActiveWebContents() == web_contents; |
38 } | 36 } |
39 | 37 |
40 } // namespace | 38 } // namespace |
41 | 39 |
42 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( | 40 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( |
43 content::WebContents* web_contents) | 41 content::WebContents* web_contents) |
44 : content::WebContentsObserver(web_contents) { | 42 : content::WebContentsObserver(web_contents) { |
45 } | 43 } |
46 | 44 |
47 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { | 45 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { |
48 } | 46 } |
49 | 47 |
| 48 void JavaScriptDialogTabHelper::SetDialogShownCallbackForTesting( |
| 49 base::Closure callback) { |
| 50 dialog_shown_ = callback; |
| 51 } |
| 52 |
50 void JavaScriptDialogTabHelper::RunJavaScriptDialog( | 53 void JavaScriptDialogTabHelper::RunJavaScriptDialog( |
51 content::WebContents* alerting_web_contents, | 54 content::WebContents* alerting_web_contents, |
52 const GURL& origin_url, | 55 const GURL& origin_url, |
53 content::JavaScriptMessageType message_type, | 56 content::JavaScriptMessageType message_type, |
54 const base::string16& message_text, | 57 const base::string16& message_text, |
55 const base::string16& default_prompt_text, | 58 const base::string16& default_prompt_text, |
56 const DialogClosedCallback& callback, | 59 const DialogClosedCallback& callback, |
57 bool* did_suppress_message) { | 60 bool* did_suppress_message) { |
58 SiteEngagementService* site_engagement_service = SiteEngagementService::Get( | 61 SiteEngagementService* site_engagement_service = SiteEngagementService::Get( |
59 Profile::FromBrowserContext(alerting_web_contents->GetBrowserContext())); | 62 Profile::FromBrowserContext(alerting_web_contents->GetBrowserContext())); |
(...skipping 24 matching lines...) Expand all Loading... |
84 // turn off focus stealing for other dialog types. | 87 // turn off focus stealing for other dialog types. |
85 *did_suppress_message = true; | 88 *did_suppress_message = true; |
86 return; | 89 return; |
87 } | 90 } |
88 | 91 |
89 if (dialog_) { | 92 if (dialog_) { |
90 // There's already a dialog up; clear it out. | 93 // There's already a dialog up; clear it out. |
91 CloseDialog(false, false, base::string16()); | 94 CloseDialog(false, false, base::string16()); |
92 } | 95 } |
93 | 96 |
94 parent_web_contents->GetDelegate()->ActivateContents(parent_web_contents); | |
95 | |
96 base::string16 title = | 97 base::string16 title = |
97 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); | 98 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); |
98 dialog_callback_ = callback; | 99 dialog_callback_ = callback; |
99 BrowserList::AddObserver(this); | |
100 dialog_ = JavaScriptDialog::Create( | 100 dialog_ = JavaScriptDialog::Create( |
101 parent_web_contents, alerting_web_contents, title, message_type, | 101 parent_web_contents, alerting_web_contents, title, message_type, |
102 message_text, default_prompt_text, callback); | 102 message_text, default_prompt_text, callback); |
103 | 103 |
| 104 BrowserList::AddObserver(this); |
| 105 |
104 // Message suppression is something that we don't give the user a checkbox | 106 // Message suppression is something that we don't give the user a checkbox |
105 // for any more. It was useful back in the day when dialogs were app-modal | 107 // for any more. It was useful back in the day when dialogs were app-modal |
106 // and clicking the checkbox was the only way to escape a loop that the page | 108 // and clicking the checkbox was the only way to escape a loop that the page |
107 // was doing, but now the user can just close the page. | 109 // was doing, but now the user can just close the page. |
108 *did_suppress_message = false; | 110 *did_suppress_message = false; |
| 111 |
| 112 if (!dialog_shown_.is_null()) { |
| 113 dialog_shown_.Run(); |
| 114 dialog_shown_.Reset(); |
| 115 } |
109 } else { | 116 } else { |
110 AppModalDialogManager()->RunJavaScriptDialog( | 117 AppModalDialogManager()->RunJavaScriptDialog( |
111 alerting_web_contents, origin_url, message_type, message_text, | 118 alerting_web_contents, origin_url, message_type, message_text, |
112 default_prompt_text, callback, did_suppress_message); | 119 default_prompt_text, callback, did_suppress_message); |
113 } | 120 } |
114 | 121 |
115 if (did_suppress_message) { | 122 if (did_suppress_message) { |
116 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed", | 123 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed", |
117 message_length); | 124 message_length); |
118 } | 125 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 DCHECK(dialog_); | 208 DCHECK(dialog_); |
202 | 209 |
203 dialog_->CloseDialogWithoutCallback(); | 210 dialog_->CloseDialogWithoutCallback(); |
204 if (!suppress_callback) | 211 if (!suppress_callback) |
205 dialog_callback_.Run(success, user_input); | 212 dialog_callback_.Run(success, user_input); |
206 | 213 |
207 dialog_.reset(); | 214 dialog_.reset(); |
208 dialog_callback_.Reset(); | 215 dialog_callback_.Reset(); |
209 BrowserList::RemoveObserver(this); | 216 BrowserList::RemoveObserver(this); |
210 } | 217 } |
OLD | NEW |