| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/download/download_danger_prompt.h" | 5 #include "chrome/browser/download/download_danger_prompt.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "base/macros.h" | 7 #include "base/macros.h" |
| 9 #include "base/metrics/sparse_histogram.h" | 8 #include "base/metrics/sparse_histogram.h" |
| 10 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" | |
| 14 #include "chrome/browser/download/chrome_download_manager_delegate.h" | |
| 15 #include "chrome/browser/download/download_stats.h" | |
| 16 #include "chrome/browser/extensions/api/experience_sampling_private/experience_s
ampling.h" | |
| 17 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 11 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 18 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" | |
| 19 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" | |
| 20 #include "chrome/common/safe_browsing/csd.pb.h" | 12 #include "chrome/common/safe_browsing/csd.pb.h" |
| 21 #include "chrome/common/safe_browsing/download_protection_util.h" | 13 #include "chrome/common/safe_browsing/download_protection_util.h" |
| 22 #include "chrome/grit/chromium_strings.h" | |
| 23 #include "chrome/grit/generated_resources.h" | |
| 24 #include "content/public/browser/browser_context.h" | |
| 25 #include "content/public/browser/download_danger_type.h" | 14 #include "content/public/browser/download_danger_type.h" |
| 26 #include "content/public/browser/download_item.h" | 15 #include "content/public/browser/download_item.h" |
| 27 #include "grit/components_strings.h" | |
| 28 #include "ui/base/l10n/l10n_util.h" | |
| 29 | 16 |
| 30 using extensions::ExperienceSamplingEvent; | |
| 31 using safe_browsing::ClientDownloadResponse; | 17 using safe_browsing::ClientDownloadResponse; |
| 32 using safe_browsing::ClientSafeBrowsingReportRequest; | 18 using safe_browsing::ClientSafeBrowsingReportRequest; |
| 33 using safe_browsing::download_protection_util:: | 19 using safe_browsing::download_protection_util:: |
| 34 GetSBClientDownloadExtensionValueForUMA; | 20 GetSBClientDownloadExtensionValueForUMA; |
| 35 | 21 |
| 36 namespace { | 22 namespace { |
| 37 | 23 |
| 38 const char kDownloadDangerPromptPrefix[] = "Download.DownloadDangerPrompt"; | 24 const char kDownloadDangerPromptPrefix[] = "Download.DownloadDangerPrompt"; |
| 39 | 25 |
| 40 // TODO(wittman): Create a native web contents modal dialog implementation of | |
| 41 // this dialog for non-Views platforms, to support bold formatting of the | |
| 42 // message lead. | |
| 43 | |
| 44 // Implements DownloadDangerPrompt using a TabModalConfirmDialog. | |
| 45 class DownloadDangerPromptImpl : public DownloadDangerPrompt, | |
| 46 public content::DownloadItem::Observer, | |
| 47 public TabModalConfirmDialogDelegate { | |
| 48 public: | |
| 49 DownloadDangerPromptImpl(content::DownloadItem* item, | |
| 50 content::WebContents* web_contents, | |
| 51 bool show_context, | |
| 52 const OnDone& done); | |
| 53 ~DownloadDangerPromptImpl() override; | |
| 54 | |
| 55 // DownloadDangerPrompt: | |
| 56 void InvokeActionForTesting(Action action) override; | |
| 57 | |
| 58 private: | |
| 59 // content::DownloadItem::Observer: | |
| 60 void OnDownloadUpdated(content::DownloadItem* download) override; | |
| 61 | |
| 62 // TabModalConfirmDialogDelegate: | |
| 63 base::string16 GetTitle() override; | |
| 64 base::string16 GetDialogMessage() override; | |
| 65 base::string16 GetAcceptButtonTitle() override; | |
| 66 base::string16 GetCancelButtonTitle() override; | |
| 67 void OnAccepted() override; | |
| 68 void OnCanceled() override; | |
| 69 void OnClosed() override; | |
| 70 | |
| 71 void RunDone(Action action); | |
| 72 | |
| 73 content::DownloadItem* download_; | |
| 74 bool show_context_; | |
| 75 OnDone done_; | |
| 76 | |
| 77 std::unique_ptr<ExperienceSamplingEvent> sampling_event_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptImpl); | |
| 80 }; | |
| 81 | |
| 82 DownloadDangerPromptImpl::DownloadDangerPromptImpl( | |
| 83 content::DownloadItem* download, | |
| 84 content::WebContents* web_contents, | |
| 85 bool show_context, | |
| 86 const OnDone& done) | |
| 87 : TabModalConfirmDialogDelegate(web_contents), | |
| 88 download_(download), | |
| 89 show_context_(show_context), | |
| 90 done_(done) { | |
| 91 DCHECK(!done_.is_null()); | |
| 92 download_->AddObserver(this); | |
| 93 RecordOpenedDangerousConfirmDialog(download_->GetDangerType()); | |
| 94 | |
| 95 // ExperienceSampling: A malicious download warning is being shown to the | |
| 96 // user, so we start a new SamplingEvent and track it. | |
| 97 sampling_event_.reset(new ExperienceSamplingEvent( | |
| 98 ExperienceSamplingEvent::kDownloadDangerPrompt, download->GetURL(), | |
| 99 download->GetReferrerUrl(), download->GetBrowserContext())); | |
| 100 } | |
| 101 | |
| 102 DownloadDangerPromptImpl::~DownloadDangerPromptImpl() { | |
| 103 // |this| might be deleted without invoking any callbacks. E.g. pressing Esc | |
| 104 // on GTK or if the user navigates away from the page showing the prompt. | |
| 105 RunDone(DISMISS); | |
| 106 } | |
| 107 | |
| 108 void DownloadDangerPromptImpl::InvokeActionForTesting(Action action) { | |
| 109 switch (action) { | |
| 110 case ACCEPT: | |
| 111 Accept(); | |
| 112 break; | |
| 113 case CANCEL: | |
| 114 Cancel(); | |
| 115 break; | |
| 116 case DISMISS: | |
| 117 RunDone(DISMISS); | |
| 118 Cancel(); | |
| 119 break; | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 void DownloadDangerPromptImpl::OnDownloadUpdated( | |
| 124 content::DownloadItem* download) { | |
| 125 // If the download is nolonger dangerous (accepted externally) or the download | |
| 126 // is in a terminal state, then the download danger prompt is no longer | |
| 127 // necessary. | |
| 128 if (!download->IsDangerous() || download->IsDone()) { | |
| 129 RunDone(DISMISS); | |
| 130 Cancel(); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 base::string16 DownloadDangerPromptImpl::GetTitle() { | |
| 135 if (show_context_) | |
| 136 return l10n_util::GetStringUTF16(IDS_CONFIRM_KEEP_DANGEROUS_DOWNLOAD_TITLE); | |
| 137 switch (download_->GetDangerType()) { | |
| 138 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: | |
| 139 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: | |
| 140 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: | |
| 141 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: { | |
| 142 return l10n_util::GetStringUTF16( | |
| 143 IDS_RESTORE_KEEP_DANGEROUS_DOWNLOAD_TITLE); | |
| 144 } | |
| 145 default: { | |
| 146 return l10n_util::GetStringUTF16( | |
| 147 IDS_CONFIRM_KEEP_DANGEROUS_DOWNLOAD_TITLE); | |
| 148 } | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 base::string16 DownloadDangerPromptImpl::GetDialogMessage() { | |
| 153 if (show_context_) { | |
| 154 switch (download_->GetDangerType()) { | |
| 155 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: { | |
| 156 return l10n_util::GetStringFUTF16( | |
| 157 IDS_PROMPT_DANGEROUS_DOWNLOAD, | |
| 158 download_->GetFileNameToReportUser().LossyDisplayName()); | |
| 159 } | |
| 160 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: // Fall through | |
| 161 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: | |
| 162 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: { | |
| 163 return l10n_util::GetStringFUTF16( | |
| 164 IDS_PROMPT_MALICIOUS_DOWNLOAD_CONTENT, | |
| 165 download_->GetFileNameToReportUser().LossyDisplayName()); | |
| 166 } | |
| 167 case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: { | |
| 168 return l10n_util::GetStringFUTF16( | |
| 169 IDS_PROMPT_UNCOMMON_DOWNLOAD_CONTENT, | |
| 170 download_->GetFileNameToReportUser().LossyDisplayName()); | |
| 171 } | |
| 172 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: { | |
| 173 return l10n_util::GetStringFUTF16( | |
| 174 IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS, | |
| 175 download_->GetFileNameToReportUser().LossyDisplayName()); | |
| 176 } | |
| 177 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: | |
| 178 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: | |
| 179 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: | |
| 180 case content::DOWNLOAD_DANGER_TYPE_MAX: { | |
| 181 break; | |
| 182 } | |
| 183 } | |
| 184 } else { | |
| 185 switch (download_->GetDangerType()) { | |
| 186 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: | |
| 187 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: | |
| 188 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: { | |
| 189 return l10n_util::GetStringUTF16( | |
| 190 IDS_PROMPT_CONFIRM_KEEP_MALICIOUS_DOWNLOAD_LEAD) + | |
| 191 base::ASCIIToUTF16("\n\n") + | |
| 192 l10n_util::GetStringUTF16( | |
| 193 IDS_PROMPT_CONFIRM_KEEP_MALICIOUS_DOWNLOAD_BODY); | |
| 194 } | |
| 195 default: { | |
| 196 return l10n_util::GetStringUTF16( | |
| 197 IDS_PROMPT_CONFIRM_KEEP_DANGEROUS_DOWNLOAD); | |
| 198 } | |
| 199 } | |
| 200 } | |
| 201 NOTREACHED(); | |
| 202 return base::string16(); | |
| 203 } | |
| 204 | |
| 205 base::string16 DownloadDangerPromptImpl::GetAcceptButtonTitle() { | |
| 206 if (show_context_) | |
| 207 return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD); | |
| 208 switch (download_->GetDangerType()) { | |
| 209 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: | |
| 210 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: | |
| 211 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: | |
| 212 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: { | |
| 213 return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD_AGAIN_MALICIOUS); | |
| 214 } | |
| 215 default: | |
| 216 return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD_AGAIN); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 base::string16 DownloadDangerPromptImpl::GetCancelButtonTitle() { | |
| 221 if (show_context_) | |
| 222 return l10n_util::GetStringUTF16(IDS_CANCEL); | |
| 223 switch (download_->GetDangerType()) { | |
| 224 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: | |
| 225 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: | |
| 226 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: | |
| 227 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: { | |
| 228 return l10n_util::GetStringUTF16(IDS_CONFIRM_CANCEL_AGAIN_MALICIOUS); | |
| 229 } | |
| 230 default: | |
| 231 return l10n_util::GetStringUTF16(IDS_CANCEL); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void DownloadDangerPromptImpl::OnAccepted() { | |
| 236 // ExperienceSampling: User proceeded through the warning. | |
| 237 sampling_event_->CreateUserDecisionEvent(ExperienceSamplingEvent::kProceed); | |
| 238 RunDone(ACCEPT); | |
| 239 } | |
| 240 | |
| 241 void DownloadDangerPromptImpl::OnCanceled() { | |
| 242 // ExperienceSampling: User canceled the warning. | |
| 243 sampling_event_->CreateUserDecisionEvent(ExperienceSamplingEvent::kDeny); | |
| 244 RunDone(CANCEL); | |
| 245 } | |
| 246 | |
| 247 void DownloadDangerPromptImpl::OnClosed() { | |
| 248 // ExperienceSampling: User canceled the warning. | |
| 249 sampling_event_->CreateUserDecisionEvent(ExperienceSamplingEvent::kDeny); | |
| 250 RunDone(DISMISS); | |
| 251 } | |
| 252 | |
| 253 void DownloadDangerPromptImpl::RunDone(Action action) { | |
| 254 // Invoking the callback can cause the download item state to change or cause | |
| 255 // the constrained window to close, and |callback| refers to a member | |
| 256 // variable. | |
| 257 OnDone done = done_; | |
| 258 done_.Reset(); | |
| 259 if (download_ != NULL) { | |
| 260 const bool accept = action == DownloadDangerPrompt::ACCEPT; | |
| 261 RecordDownloadDangerPrompt(accept, *download_); | |
| 262 if (!download_->GetURL().is_empty() && | |
| 263 !download_->GetBrowserContext()->IsOffTheRecord()) { | |
| 264 SendSafeBrowsingDownloadRecoveryReport(accept, *download_); | |
| 265 } | |
| 266 download_->RemoveObserver(this); | |
| 267 download_ = NULL; | |
| 268 } | |
| 269 if (!done.is_null()) | |
| 270 done.Run(action); | |
| 271 } | |
| 272 | |
| 273 // Converts DownloadDangerType into their corresponding string. | 26 // Converts DownloadDangerType into their corresponding string. |
| 274 const char* GetDangerTypeString( | 27 const char* GetDangerTypeString( |
| 275 const content::DownloadDangerType& danger_type) { | 28 const content::DownloadDangerType& danger_type) { |
| 276 switch (danger_type) { | 29 switch (danger_type) { |
| 277 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: | 30 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: |
| 278 return "DangerousFile"; | 31 return "DangerousFile"; |
| 279 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: | 32 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: |
| 280 return "DangerousURL"; | 33 return "DangerousURL"; |
| 281 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: | 34 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: |
| 282 return "DangerousContent"; | 35 return "DangerousContent"; |
| 283 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: | 36 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: |
| 284 return "DangerousHost"; | 37 return "DangerousHost"; |
| 285 case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: | 38 case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: |
| 286 return "UncommonContent"; | 39 return "UncommonContent"; |
| 287 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: | 40 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: |
| 288 return "PotentiallyUnwanted"; | 41 return "PotentiallyUnwanted"; |
| 289 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: | 42 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: |
| 290 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: | 43 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: |
| 291 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: | 44 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: |
| 292 case content::DOWNLOAD_DANGER_TYPE_MAX: | 45 case content::DOWNLOAD_DANGER_TYPE_MAX: |
| 293 break; | 46 break; |
| 294 } | 47 } |
| 295 NOTREACHED(); | 48 NOTREACHED(); |
| 296 return nullptr; | 49 return nullptr; |
| 297 } | 50 } |
| 298 | 51 |
| 299 } // namespace | 52 } // namespace |
| 300 | 53 |
| 301 #if !defined(USE_AURA) | |
| 302 // static | |
| 303 DownloadDangerPrompt* DownloadDangerPrompt::Create( | |
| 304 content::DownloadItem* item, | |
| 305 content::WebContents* web_contents, | |
| 306 bool show_context, | |
| 307 const OnDone& done) { | |
| 308 DownloadDangerPromptImpl* prompt = | |
| 309 new DownloadDangerPromptImpl(item, web_contents, show_context, done); | |
| 310 // |prompt| will be deleted when the dialog is done. | |
| 311 TabModalConfirmDialog::Create(prompt, web_contents); | |
| 312 return prompt; | |
| 313 } | |
| 314 #endif | |
| 315 | |
| 316 void DownloadDangerPrompt::SendSafeBrowsingDownloadRecoveryReport( | 54 void DownloadDangerPrompt::SendSafeBrowsingDownloadRecoveryReport( |
| 317 bool did_proceed, | 55 bool did_proceed, |
| 318 const content::DownloadItem& download) { | 56 const content::DownloadItem& download) { |
| 319 safe_browsing::SafeBrowsingService* sb_service = | 57 safe_browsing::SafeBrowsingService* sb_service = |
| 320 g_browser_process->safe_browsing_service(); | 58 g_browser_process->safe_browsing_service(); |
| 321 ClientSafeBrowsingReportRequest report; | 59 ClientSafeBrowsingReportRequest report; |
| 322 report.set_type(ClientSafeBrowsingReportRequest::DANGEROUS_DOWNLOAD_RECOVERY); | 60 report.set_type(ClientSafeBrowsingReportRequest::DANGEROUS_DOWNLOAD_RECOVERY); |
| 323 switch (download.GetDangerType()) { | 61 switch (download.GetDangerType()) { |
| 324 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: | 62 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: |
| 325 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: | 63 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 base::StringPrintf("%s.%s.Shown", kDownloadDangerPromptPrefix, | 96 base::StringPrintf("%s.%s.Shown", kDownloadDangerPromptPrefix, |
| 359 GetDangerTypeString(danger_type)), | 97 GetDangerTypeString(danger_type)), |
| 360 dangerous_file_type); | 98 dangerous_file_type); |
| 361 if (did_proceed) { | 99 if (did_proceed) { |
| 362 UMA_HISTOGRAM_SPARSE_SLOWLY( | 100 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 363 base::StringPrintf("%s.%s.Proceed", kDownloadDangerPromptPrefix, | 101 base::StringPrintf("%s.%s.Proceed", kDownloadDangerPromptPrefix, |
| 364 GetDangerTypeString(danger_type)), | 102 GetDangerTypeString(danger_type)), |
| 365 dangerous_file_type); | 103 dangerous_file_type); |
| 366 } | 104 } |
| 367 } | 105 } |
| OLD | NEW |