| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 } | 83 } |
| 84 | 84 |
| 85 void JavaScriptDialogTabHelper::SetDialogShownCallbackForTesting( | 85 void JavaScriptDialogTabHelper::SetDialogShownCallbackForTesting( |
| 86 base::Closure callback) { | 86 base::Closure callback) { |
| 87 dialog_shown_ = callback; | 87 dialog_shown_ = callback; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void JavaScriptDialogTabHelper::RunJavaScriptDialog( | 90 void JavaScriptDialogTabHelper::RunJavaScriptDialog( |
| 91 content::WebContents* alerting_web_contents, | 91 content::WebContents* alerting_web_contents, |
| 92 const GURL& origin_url, | 92 const GURL& origin_url, |
| 93 content::JavaScriptMessageType message_type, | 93 content::JavaScriptDialogType dialog_type, |
| 94 const base::string16& message_text, | 94 const base::string16& message_text, |
| 95 const base::string16& default_prompt_text, | 95 const base::string16& default_prompt_text, |
| 96 const DialogClosedCallback& callback, | 96 const DialogClosedCallback& callback, |
| 97 bool* did_suppress_message) { | 97 bool* did_suppress_message) { |
| 98 SiteEngagementService* site_engagement_service = SiteEngagementService::Get( | 98 SiteEngagementService* site_engagement_service = SiteEngagementService::Get( |
| 99 Profile::FromBrowserContext(alerting_web_contents->GetBrowserContext())); | 99 Profile::FromBrowserContext(alerting_web_contents->GetBrowserContext())); |
| 100 double engagement_score = site_engagement_service->GetScore(origin_url); | 100 double engagement_score = site_engagement_service->GetScore(origin_url); |
| 101 int32_t message_length = static_cast<int32_t>(message_text.length()); | 101 int32_t message_length = static_cast<int32_t>(message_text.length()); |
| 102 if (engagement_score == 0) { | 102 if (engagement_score == 0) { |
| 103 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementNone", | 103 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementNone", |
| 104 message_length); | 104 message_length); |
| 105 } else if (engagement_score < 1) { | 105 } else if (engagement_score < 1) { |
| 106 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementLessThanOne", | 106 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementLessThanOne", |
| 107 message_length); | 107 message_length); |
| 108 } else if (engagement_score < 5) { | 108 } else if (engagement_score < 5) { |
| 109 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementOneToFive", | 109 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementOneToFive", |
| 110 message_length); | 110 message_length); |
| 111 } else { | 111 } else { |
| 112 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementHigher", | 112 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementHigher", |
| 113 message_length); | 113 message_length); |
| 114 } | 114 } |
| 115 | 115 |
| 116 content::WebContents* parent_web_contents = | 116 content::WebContents* parent_web_contents = |
| 117 WebContentsObserver::web_contents(); | 117 WebContentsObserver::web_contents(); |
| 118 bool foremost = IsWebContentsForemost(parent_web_contents); | 118 bool foremost = IsWebContentsForemost(parent_web_contents); |
| 119 switch (message_type) { | 119 switch (dialog_type) { |
| 120 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: | 120 case content::JAVASCRIPT_DIALOG_TYPE_ALERT: |
| 121 UMA_HISTOGRAM_BOOLEAN("JSDialogs.IsForemost.Alert", foremost); | 121 UMA_HISTOGRAM_BOOLEAN("JSDialogs.IsForemost.Alert", foremost); |
| 122 break; | 122 break; |
| 123 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: | 123 case content::JAVASCRIPT_DIALOG_TYPE_CONFIRM: |
| 124 UMA_HISTOGRAM_BOOLEAN("JSDialogs.IsForemost.Confirm", foremost); | 124 UMA_HISTOGRAM_BOOLEAN("JSDialogs.IsForemost.Confirm", foremost); |
| 125 break; | 125 break; |
| 126 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT: | 126 case content::JAVASCRIPT_DIALOG_TYPE_PROMPT: |
| 127 UMA_HISTOGRAM_BOOLEAN("JSDialogs.IsForemost.Prompt", foremost); | 127 UMA_HISTOGRAM_BOOLEAN("JSDialogs.IsForemost.Prompt", foremost); |
| 128 break; | 128 break; |
| 129 } | 129 } |
| 130 | 130 |
| 131 if (IsEnabled()) { | 131 if (IsEnabled()) { |
| 132 if (!IsWebContentsForemost(parent_web_contents) && | 132 if (!IsWebContentsForemost(parent_web_contents) && |
| 133 message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) { | 133 dialog_type == content::JAVASCRIPT_DIALOG_TYPE_PROMPT) { |
| 134 // Don't allow "prompt" dialogs to steal the user's focus. TODO(avi): | 134 // Don't allow "prompt" dialogs to steal the user's focus. TODO(avi): |
| 135 // Eventually, for subsequent phases of http://bit.ly/project-oldspice, | 135 // Eventually, for subsequent phases of http://bit.ly/project-oldspice, |
| 136 // turn off focus stealing for other dialog types. | 136 // turn off focus stealing for other dialog types. |
| 137 *did_suppress_message = true; | 137 *did_suppress_message = true; |
| 138 alerting_web_contents->GetMainFrame()->AddMessageToConsole( | 138 alerting_web_contents->GetMainFrame()->AddMessageToConsole( |
| 139 content::CONSOLE_MESSAGE_LEVEL_WARNING, | 139 content::CONSOLE_MESSAGE_LEVEL_WARNING, |
| 140 "A window.prompt() dialog generated by this page was suppressed " | 140 "A window.prompt() dialog generated by this page was suppressed " |
| 141 "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. " |
| 142 "Please make sure your dialogs are triggered by user interactions " | 142 "Please make sure your dialogs are triggered by user interactions " |
| 143 "to avoid this situation. " | 143 "to avoid this situation. " |
| 144 "https://www.chromestatus.com/feature/5637107137642496"); | 144 "https://www.chromestatus.com/feature/5637107137642496"); |
| 145 return; | 145 return; |
| 146 } | 146 } |
| 147 | 147 |
| 148 if (dialog_) { | 148 if (dialog_) { |
| 149 // There's already a dialog up; clear it out. | 149 // There's already a dialog up; clear it out. |
| 150 CloseDialog(false, base::string16(), | 150 CloseDialog(false, base::string16(), |
| 151 DismissalCause::SUBSEQUENT_DIALOG_SHOWN); | 151 DismissalCause::SUBSEQUENT_DIALOG_SHOWN); |
| 152 } | 152 } |
| 153 | 153 |
| 154 base::string16 title = | 154 base::string16 title = |
| 155 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); | 155 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); |
| 156 dialog_callback_ = callback; | 156 dialog_callback_ = callback; |
| 157 message_type_ = message_type; | 157 dialog_type_ = dialog_type; |
| 158 dialog_ = JavaScriptDialog::Create( | 158 dialog_ = JavaScriptDialog::Create( |
| 159 parent_web_contents, alerting_web_contents, title, message_type, | 159 parent_web_contents, alerting_web_contents, title, dialog_type, |
| 160 message_text, default_prompt_text, | 160 message_text, default_prompt_text, |
| 161 base::Bind(&JavaScriptDialogTabHelper::OnDialogClosed, | 161 base::Bind(&JavaScriptDialogTabHelper::OnDialogClosed, |
| 162 base::Unretained(this), callback)); | 162 base::Unretained(this), callback)); |
| 163 | 163 |
| 164 BrowserList::AddObserver(this); | 164 BrowserList::AddObserver(this); |
| 165 | 165 |
| 166 // Message suppression is something that we don't give the user a checkbox | 166 // Message suppression is something that we don't give the user a checkbox |
| 167 // for any more. It was useful back in the day when dialogs were app-modal | 167 // for any more. It was useful back in the day when dialogs were app-modal |
| 168 // and clicking the checkbox was the only way to escape a loop that the page | 168 // and clicking the checkbox was the only way to escape a loop that the page |
| 169 // was doing, but now the user can just close the page. | 169 // was doing, but now the user can just close the page. |
| 170 *did_suppress_message = false; | 170 *did_suppress_message = false; |
| 171 | 171 |
| 172 if (!dialog_shown_.is_null()) { | 172 if (!dialog_shown_.is_null()) { |
| 173 dialog_shown_.Run(); | 173 dialog_shown_.Run(); |
| 174 dialog_shown_.Reset(); | 174 dialog_shown_.Reset(); |
| 175 } | 175 } |
| 176 } else { | 176 } else { |
| 177 AppModalDialogManager()->RunJavaScriptDialog( | 177 AppModalDialogManager()->RunJavaScriptDialog( |
| 178 alerting_web_contents, origin_url, message_type, message_text, | 178 alerting_web_contents, origin_url, dialog_type, message_text, |
| 179 default_prompt_text, callback, did_suppress_message); | 179 default_prompt_text, callback, did_suppress_message); |
| 180 } | 180 } |
| 181 | 181 |
| 182 if (did_suppress_message) { | 182 if (did_suppress_message) { |
| 183 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed", | 183 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed", |
| 184 message_length); | 184 message_length); |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 | 187 |
| 188 namespace { | 188 namespace { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 } | 282 } |
| 283 | 283 |
| 284 void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) { | 284 void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) { |
| 285 if (dialog_ && !IsWebContentsForemost(web_contents())) { | 285 if (dialog_ && !IsWebContentsForemost(web_contents())) { |
| 286 CloseDialog(false, base::string16(), DismissalCause::BROWSER_SWITCHED); | 286 CloseDialog(false, base::string16(), DismissalCause::BROWSER_SWITCHED); |
| 287 } | 287 } |
| 288 } | 288 } |
| 289 | 289 |
| 290 void JavaScriptDialogTabHelper::LogDialogDismissalCause( | 290 void JavaScriptDialogTabHelper::LogDialogDismissalCause( |
| 291 JavaScriptDialogTabHelper::DismissalCause cause) { | 291 JavaScriptDialogTabHelper::DismissalCause cause) { |
| 292 switch (message_type_) { | 292 switch (dialog_type_) { |
| 293 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: | 293 case content::JAVASCRIPT_DIALOG_TYPE_ALERT: |
| 294 UMA_HISTOGRAM_ENUMERATION("JSDialogs.DismissalCause.Alert", | 294 UMA_HISTOGRAM_ENUMERATION("JSDialogs.DismissalCause.Alert", |
| 295 static_cast<int>(cause), | 295 static_cast<int>(cause), |
| 296 static_cast<int>(DismissalCause::MAX)); | 296 static_cast<int>(DismissalCause::MAX)); |
| 297 break; | 297 break; |
| 298 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: | 298 case content::JAVASCRIPT_DIALOG_TYPE_CONFIRM: |
| 299 UMA_HISTOGRAM_ENUMERATION("JSDialogs.DismissalCause.Confirm", | 299 UMA_HISTOGRAM_ENUMERATION("JSDialogs.DismissalCause.Confirm", |
| 300 static_cast<int>(cause), | 300 static_cast<int>(cause), |
| 301 static_cast<int>(DismissalCause::MAX)); | 301 static_cast<int>(DismissalCause::MAX)); |
| 302 break; | 302 break; |
| 303 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT: | 303 case content::JAVASCRIPT_DIALOG_TYPE_PROMPT: |
| 304 UMA_HISTOGRAM_ENUMERATION("JSDialogs.DismissalCause.Prompt", | 304 UMA_HISTOGRAM_ENUMERATION("JSDialogs.DismissalCause.Prompt", |
| 305 static_cast<int>(cause), | 305 static_cast<int>(cause), |
| 306 static_cast<int>(DismissalCause::MAX)); | 306 static_cast<int>(DismissalCause::MAX)); |
| 307 break; | 307 break; |
| 308 } | 308 } |
| 309 } | 309 } |
| 310 | 310 |
| 311 void JavaScriptDialogTabHelper::OnDialogClosed( | 311 void JavaScriptDialogTabHelper::OnDialogClosed( |
| 312 DialogClosedCallback callback, | 312 DialogClosedCallback callback, |
| 313 bool success, | 313 bool success, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 328 dialog_callback_.Run(success, user_input); | 328 dialog_callback_.Run(success, user_input); |
| 329 | 329 |
| 330 ClearDialogInfo(); | 330 ClearDialogInfo(); |
| 331 } | 331 } |
| 332 | 332 |
| 333 void JavaScriptDialogTabHelper::ClearDialogInfo() { | 333 void JavaScriptDialogTabHelper::ClearDialogInfo() { |
| 334 dialog_.reset(); | 334 dialog_.reset(); |
| 335 dialog_callback_.Reset(); | 335 dialog_callback_.Reset(); |
| 336 BrowserList::RemoveObserver(this); | 336 BrowserList::RemoveObserver(this); |
| 337 } | 337 } |
| OLD | NEW |