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/feature_list.h" | 8 #include "base/feature_list.h" |
8 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
9 #include "chrome/browser/engagement/site_engagement_service.h" | 10 #include "chrome/browser/engagement/site_engagement_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_list.h" |
| 14 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_views.h" |
| 15 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
11 #include "components/app_modal/javascript_dialog_manager.h" | 17 #include "components/app_modal/javascript_dialog_manager.h" |
| 18 #include "content/public/browser/web_contents_delegate.h" |
12 | 19 |
13 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); | 20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); |
14 | 21 |
15 namespace { | 22 namespace { |
16 | 23 |
17 const base::Feature kAutoDismissingDialogsFeature{ | 24 const base::Feature kAutoDismissingDialogsFeature{ |
18 "AutoDismissingDialogs", base::FEATURE_DISABLED_BY_DEFAULT}; | 25 "AutoDismissingDialogs", base::FEATURE_DISABLED_BY_DEFAULT}; |
19 | 26 |
20 bool IsEnabled() { | 27 bool IsEnabled() { |
21 return base::FeatureList::IsEnabled(kAutoDismissingDialogsFeature); | 28 return base::FeatureList::IsEnabled(kAutoDismissingDialogsFeature); |
22 } | 29 } |
23 | 30 |
24 content::JavaScriptDialogManager* AppModalDialogManager() { | 31 app_modal::JavaScriptDialogManager* AppModalDialogManager() { |
25 return app_modal::JavaScriptDialogManager::GetInstance(); | 32 return app_modal::JavaScriptDialogManager::GetInstance(); |
26 } | 33 } |
27 | 34 |
| 35 bool IsWebContentsForemost(content::WebContents* web_contents) { |
| 36 Browser* browser = BrowserList::GetInstance()->GetLastActive(); |
| 37 DCHECK(browser); |
| 38 return browser->tab_strip_model()->GetActiveWebContents() == web_contents; |
| 39 } |
| 40 |
28 } // namespace | 41 } // namespace |
29 | 42 |
30 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( | 43 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( |
31 content::WebContents* web_contents) | 44 content::WebContents* web_contents) |
32 : content::WebContentsObserver(web_contents) { | 45 : content::WebContentsObserver(web_contents) { |
33 } | 46 } |
34 | 47 |
35 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { | 48 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { |
36 } | 49 } |
37 | 50 |
38 void JavaScriptDialogTabHelper::WebContentsDestroyed() { | |
39 } | |
40 | |
41 void JavaScriptDialogTabHelper::RunJavaScriptDialog( | 51 void JavaScriptDialogTabHelper::RunJavaScriptDialog( |
42 content::WebContents* web_contents, | 52 content::WebContents* alerting_web_contents, |
43 const GURL& origin_url, | 53 const GURL& origin_url, |
44 content::JavaScriptMessageType message_type, | 54 content::JavaScriptMessageType message_type, |
45 const base::string16& message_text, | 55 const base::string16& message_text, |
46 const base::string16& default_prompt_text, | 56 const base::string16& default_prompt_text, |
47 const DialogClosedCallback& callback, | 57 const DialogClosedCallback& callback, |
48 bool* did_suppress_message) { | 58 bool* did_suppress_message) { |
49 SiteEngagementService* site_engagement_service = SiteEngagementService::Get( | 59 SiteEngagementService* site_engagement_service = SiteEngagementService::Get( |
50 Profile::FromBrowserContext(web_contents->GetBrowserContext())); | 60 Profile::FromBrowserContext(alerting_web_contents->GetBrowserContext())); |
51 double engagement_score = site_engagement_service->GetScore(origin_url); | 61 double engagement_score = site_engagement_service->GetScore(origin_url); |
52 switch (message_type) { | 62 switch (message_type) { |
53 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: | 63 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: |
54 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Alert", | 64 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Alert", |
55 engagement_score); | 65 engagement_score); |
56 break; | 66 break; |
57 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: | 67 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: |
58 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Confirm", | 68 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Confirm", |
59 engagement_score); | 69 engagement_score); |
60 break; | 70 break; |
(...skipping 11 matching lines...) Expand all Loading... |
72 message_length); | 82 message_length); |
73 } else if (engagement_score < 5) { | 83 } else if (engagement_score < 5) { |
74 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementOneToFive", | 84 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementOneToFive", |
75 message_length); | 85 message_length); |
76 } else { | 86 } else { |
77 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementHigher", | 87 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementHigher", |
78 message_length); | 88 message_length); |
79 } | 89 } |
80 | 90 |
81 if (IsEnabled()) { | 91 if (IsEnabled()) { |
82 NOTREACHED() << "auto-dismissing dialog code does not yet exist"; | 92 content::WebContents* parent_web_contents = |
| 93 WebContentsObserver::web_contents(); |
| 94 |
| 95 if (!IsWebContentsForemost(parent_web_contents) && |
| 96 message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) { |
| 97 // Don't allow "prompt" dialogs to steal the user's focus. TODO(avi): |
| 98 // Eventually, for subsequent phases of http://bit.ly/project-oldspice, |
| 99 // turn off focus stealing for other dialog types. |
| 100 *did_suppress_message = true; |
| 101 callback.Run(false, base::string16()); |
| 102 return; |
| 103 } |
| 104 |
| 105 if (dialog_) { |
| 106 // There's already a dialog up; clear it out. |
| 107 CloseDialog(false, false, base::string16()); |
| 108 } |
| 109 |
| 110 parent_web_contents->GetDelegate()->ActivateContents(parent_web_contents); |
| 111 |
| 112 base::string16 title = |
| 113 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); |
| 114 dialog_callback_ = callback; |
| 115 BrowserList::AddObserver(this); |
| 116 // TODO(avi): abstract out so that we have a Mac version too... |
| 117 dialog_ = JavaScriptDialogViews::Create( |
| 118 parent_web_contents, alerting_web_contents, title, message_type, |
| 119 message_text, default_prompt_text, callback); |
| 120 |
| 121 // Message suppression is something that we don't give the user a checkbox |
| 122 // for any more. It was useful back in the day when dialogs were app-modal |
| 123 // and clicking the checkbox was the only way to escape a loop that the page |
| 124 // was doing, but now the user can just close the page. |
| 125 *did_suppress_message = false; |
83 } else { | 126 } else { |
84 AppModalDialogManager()->RunJavaScriptDialog( | 127 AppModalDialogManager()->RunJavaScriptDialog( |
85 web_contents, origin_url, message_type, message_text, | 128 alerting_web_contents, origin_url, message_type, message_text, |
86 default_prompt_text, callback, did_suppress_message); | 129 default_prompt_text, callback, did_suppress_message); |
87 } | 130 } |
88 | 131 |
89 if (did_suppress_message) { | 132 if (did_suppress_message) { |
90 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed", | 133 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed", |
91 message_length); | 134 message_length); |
92 } | 135 } |
93 } | 136 } |
94 | 137 |
95 namespace { | 138 namespace { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 172 |
130 return AppModalDialogManager()->RunBeforeUnloadDialog( | 173 return AppModalDialogManager()->RunBeforeUnloadDialog( |
131 web_contents, is_reload, | 174 web_contents, is_reload, |
132 base::Bind(&SaveUnloadUmaStats, engagement_score, callback)); | 175 base::Bind(&SaveUnloadUmaStats, engagement_score, callback)); |
133 } | 176 } |
134 | 177 |
135 bool JavaScriptDialogTabHelper::HandleJavaScriptDialog( | 178 bool JavaScriptDialogTabHelper::HandleJavaScriptDialog( |
136 content::WebContents* web_contents, | 179 content::WebContents* web_contents, |
137 bool accept, | 180 bool accept, |
138 const base::string16* prompt_override) { | 181 const base::string16* prompt_override) { |
139 if (!IsEnabled()) { | 182 if (dialog_) { |
140 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, | 183 CloseDialog(false /*suppress_callback*/, accept, |
141 prompt_override); | 184 prompt_override ? *prompt_override : base::string16()); |
| 185 return true; |
142 } | 186 } |
143 | 187 |
144 NOTREACHED() << "auto-dismissing dialog code does not yet exist"; | 188 // Handle any app-modal dialogs being run by the app-modal dialog system. |
145 return false; | 189 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, |
| 190 prompt_override); |
146 } | 191 } |
147 | 192 |
148 void JavaScriptDialogTabHelper::CancelDialogs( | 193 void JavaScriptDialogTabHelper::CancelDialogs( |
149 content::WebContents* web_contents, | 194 content::WebContents* web_contents, |
150 bool suppress_callbacks, | 195 bool suppress_callbacks, |
151 bool reset_state) { | 196 bool reset_state) { |
152 // Cancel any app-modal dialogs that may be going. | 197 if (dialog_) |
153 if (!IsEnabled()) { | 198 CloseDialog(suppress_callbacks, false, base::string16()); |
154 return AppModalDialogManager()->CancelDialogs( | |
155 web_contents, suppress_callbacks, reset_state); | |
156 } | |
157 | 199 |
158 // More work here for the auto-dismissing dialogs. | 200 // Cancel any app-modal dialogs being run by the app-modal dialog system. |
| 201 return AppModalDialogManager()->CancelDialogs( |
| 202 web_contents, suppress_callbacks, reset_state); |
159 } | 203 } |
| 204 |
| 205 void JavaScriptDialogTabHelper::WasHidden() { |
| 206 if (dialog_) |
| 207 CloseDialog(false, false, base::string16()); |
| 208 } |
| 209 |
| 210 void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) { |
| 211 if (dialog_ && !IsWebContentsForemost(web_contents())) |
| 212 CloseDialog(false, false, base::string16()); |
| 213 } |
| 214 |
| 215 void JavaScriptDialogTabHelper::CloseDialog(bool suppress_callback, |
| 216 bool success, |
| 217 const base::string16& user_input) { |
| 218 DCHECK(dialog_); |
| 219 |
| 220 dialog_->CloseJavaScriptDialog(); |
| 221 if (!suppress_callback) |
| 222 dialog_callback_.Run(success, user_input); |
| 223 |
| 224 dialog_.reset(); |
| 225 dialog_callback_.Reset(); |
| 226 BrowserList::RemoveObserver(this); |
| 227 } |
OLD | NEW |