Chromium Code Reviews| 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" | 7 #include "base/bind.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 11 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 11 #include "chrome/browser/download/download_stats.h" | 12 #include "chrome/browser/download/download_stats.h" |
| 12 #include "chrome/browser/extensions/api/experience_sampling_private/experience_s ampling.h" | 13 #include "chrome/browser/extensions/api/experience_sampling_private/experience_s ampling.h" |
| 14 #include "chrome/browser/safe_browsing/ping_manager.h" | |
| 15 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
| 13 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" | 16 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" |
| 14 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" | 17 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" |
| 18 #include "chrome/common/safe_browsing/csd.pb.h" | |
| 15 #include "chrome/grit/chromium_strings.h" | 19 #include "chrome/grit/chromium_strings.h" |
| 16 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
| 21 #include "content/public/browser/browser_thread.h" | |
| 17 #include "content/public/browser/download_danger_type.h" | 22 #include "content/public/browser/download_danger_type.h" |
| 18 #include "content/public/browser/download_item.h" | 23 #include "content/public/browser/download_item.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 20 | 25 |
| 21 using extensions::ExperienceSamplingEvent; | 26 using extensions::ExperienceSamplingEvent; |
| 27 using safe_browsing::ClientSafeBrowsingReportRequest; | |
| 22 | 28 |
| 23 namespace { | 29 namespace { |
| 24 | 30 |
| 25 // TODO(wittman): Create a native web contents modal dialog implementation of | 31 // TODO(wittman): Create a native web contents modal dialog implementation of |
| 26 // this dialog for non-Views platforms, to support bold formatting of the | 32 // this dialog for non-Views platforms, to support bold formatting of the |
| 27 // message lead. | 33 // message lead. |
| 28 | 34 |
| 29 // Implements DownloadDangerPrompt using a TabModalConfirmDialog. | 35 // Implements DownloadDangerPrompt using a TabModalConfirmDialog. |
| 30 class DownloadDangerPromptImpl : public DownloadDangerPrompt, | 36 class DownloadDangerPromptImpl : public DownloadDangerPrompt, |
| 31 public content::DownloadItem::Observer, | 37 public content::DownloadItem::Observer, |
| 32 public TabModalConfirmDialogDelegate { | 38 public TabModalConfirmDialogDelegate { |
| 33 public: | 39 public: |
| 34 DownloadDangerPromptImpl(content::DownloadItem* item, | 40 DownloadDangerPromptImpl(content::DownloadItem* item, |
| 35 content::WebContents* web_contents, | 41 content::WebContents* web_contents, |
| 36 bool show_context, | 42 bool show_context, |
| 37 const OnDone& done); | 43 const OnDone& done); |
| 38 ~DownloadDangerPromptImpl() override; | 44 ~DownloadDangerPromptImpl() override; |
| 39 | 45 |
| 40 // DownloadDangerPrompt: | 46 // DownloadDangerPrompt: |
| 41 void InvokeActionForTesting(Action action) override; | 47 std::string InvokeActionForTesting(Action action, const GURL& url) override; |
| 42 | 48 |
| 43 private: | 49 private: |
| 44 // content::DownloadItem::Observer: | 50 // content::DownloadItem::Observer: |
| 45 void OnDownloadUpdated(content::DownloadItem* download) override; | 51 void OnDownloadUpdated(content::DownloadItem* download) override; |
| 46 | 52 |
| 47 // TabModalConfirmDialogDelegate: | 53 // TabModalConfirmDialogDelegate: |
| 48 base::string16 GetTitle() override; | 54 base::string16 GetTitle() override; |
| 49 base::string16 GetDialogMessage() override; | 55 base::string16 GetDialogMessage() override; |
| 50 base::string16 GetAcceptButtonTitle() override; | 56 base::string16 GetAcceptButtonTitle() override; |
| 51 base::string16 GetCancelButtonTitle() override; | 57 base::string16 GetCancelButtonTitle() override; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 download->GetReferrerUrl(), | 91 download->GetReferrerUrl(), |
| 86 download->GetBrowserContext())); | 92 download->GetBrowserContext())); |
| 87 } | 93 } |
| 88 | 94 |
| 89 DownloadDangerPromptImpl::~DownloadDangerPromptImpl() { | 95 DownloadDangerPromptImpl::~DownloadDangerPromptImpl() { |
| 90 // |this| might be deleted without invoking any callbacks. E.g. pressing Esc | 96 // |this| might be deleted without invoking any callbacks. E.g. pressing Esc |
| 91 // on GTK or if the user navigates away from the page showing the prompt. | 97 // on GTK or if the user navigates away from the page showing the prompt. |
| 92 RunDone(DISMISS); | 98 RunDone(DISMISS); |
| 93 } | 99 } |
| 94 | 100 |
| 95 void DownloadDangerPromptImpl::InvokeActionForTesting(Action action) { | 101 std::string DownloadDangerPromptImpl::InvokeActionForTesting(Action action, |
| 102 const GURL& url) { | |
| 96 switch (action) { | 103 switch (action) { |
| 97 case ACCEPT: Accept(); break; | 104 case ACCEPT: Accept(); break; |
| 98 case CANCEL: Cancel(); break; | 105 case CANCEL: Cancel(); break; |
| 99 case DISMISS: | 106 case DISMISS: |
| 100 RunDone(DISMISS); | 107 RunDone(DISMISS); |
| 101 Cancel(); | 108 Cancel(); |
| 102 break; | 109 break; |
| 103 } | 110 } |
| 111 return CreateSafeBrowsingDownloadRecoveryReport(action == ACCEPT, url); | |
| 104 } | 112 } |
| 105 | 113 |
| 106 void DownloadDangerPromptImpl::OnDownloadUpdated( | 114 void DownloadDangerPromptImpl::OnDownloadUpdated( |
| 107 content::DownloadItem* download) { | 115 content::DownloadItem* download) { |
| 108 // If the download is nolonger dangerous (accepted externally) or the download | 116 // If the download is nolonger dangerous (accepted externally) or the download |
| 109 // is in a terminal state, then the download danger prompt is no longer | 117 // is in a terminal state, then the download danger prompt is no longer |
| 110 // necessary. | 118 // necessary. |
| 111 if (!download->IsDangerous() || download->IsDone()) { | 119 if (!download->IsDangerous() || download->IsDone()) { |
| 112 RunDone(DISMISS); | 120 RunDone(DISMISS); |
| 113 Cancel(); | 121 Cancel(); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 RunDone(DISMISS); | 241 RunDone(DISMISS); |
| 234 } | 242 } |
| 235 | 243 |
| 236 void DownloadDangerPromptImpl::RunDone(Action action) { | 244 void DownloadDangerPromptImpl::RunDone(Action action) { |
| 237 // Invoking the callback can cause the download item state to change or cause | 245 // Invoking the callback can cause the download item state to change or cause |
| 238 // the constrained window to close, and |callback| refers to a member | 246 // the constrained window to close, and |callback| refers to a member |
| 239 // variable. | 247 // variable. |
| 240 OnDone done = done_; | 248 OnDone done = done_; |
| 241 done_.Reset(); | 249 done_.Reset(); |
| 242 if (download_ != NULL) { | 250 if (download_ != NULL) { |
| 251 if (!download_->GetURL().is_empty()) { | |
| 252 std::string report = CreateSafeBrowsingDownloadRecoveryReport( | |
| 253 action == DownloadDangerPrompt::ACCEPT, download_->GetURL()); | |
| 254 if (!report.empty()) | |
| 255 SendSerializedReport(report); | |
| 256 } | |
| 243 download_->RemoveObserver(this); | 257 download_->RemoveObserver(this); |
| 244 download_ = NULL; | 258 download_ = NULL; |
| 245 } | 259 } |
| 246 if (!done.is_null()) | 260 if (!done.is_null()) |
| 247 done.Run(action); | 261 done.Run(action); |
| 248 } | 262 } |
| 249 | 263 |
| 250 } // namespace | 264 } // namespace |
| 251 | 265 |
| 252 #if !defined(USE_AURA) | 266 #if !defined(USE_AURA) |
| 253 // static | 267 // static |
| 254 DownloadDangerPrompt* DownloadDangerPrompt::Create( | 268 DownloadDangerPrompt* DownloadDangerPrompt::Create( |
| 255 content::DownloadItem* item, | 269 content::DownloadItem* item, |
| 256 content::WebContents* web_contents, | 270 content::WebContents* web_contents, |
| 257 bool show_context, | 271 bool show_context, |
| 258 const OnDone& done) { | 272 const OnDone& done) { |
| 259 DownloadDangerPromptImpl* prompt = new DownloadDangerPromptImpl( | 273 DownloadDangerPromptImpl* prompt = new DownloadDangerPromptImpl( |
| 260 item, web_contents, show_context, done); | 274 item, web_contents, show_context, done); |
| 261 // |prompt| will be deleted when the dialog is done. | 275 // |prompt| will be deleted when the dialog is done. |
| 262 TabModalConfirmDialog::Create(prompt, web_contents); | 276 TabModalConfirmDialog::Create(prompt, web_contents); |
| 263 return prompt; | 277 return prompt; |
| 264 } | 278 } |
| 265 #endif | 279 #endif |
| 280 | |
| 281 std::string DownloadDangerPrompt::CreateSafeBrowsingDownloadRecoveryReport( | |
| 282 bool did_proceed, | |
| 283 const GURL& url) { | |
| 284 ClientSafeBrowsingReportRequest report; | |
| 285 report.set_type(ClientSafeBrowsingReportRequest::MALICIOUS_DOWNLOAD_RECOVERY); | |
| 286 report.set_url(url.spec()); | |
| 287 report.set_did_proceed(did_proceed); | |
| 288 | |
| 289 std::string serialized_report; | |
| 290 if (!report.SerializeToString(&serialized_report)) { | |
| 291 DLOG(ERROR) << "Unable to serialize the threat report."; | |
| 292 return ""; | |
|
msw
2015/11/17 01:17:37
nit: return std::string()
Jialiu Lin
2015/11/17 02:05:50
Done.
| |
| 293 } | |
| 294 | |
| 295 return serialized_report; | |
| 296 } | |
| 297 | |
| 298 void DownloadDangerPrompt::SendSerializedReport( | |
| 299 const std::string& serialized_report) { | |
| 300 safe_browsing::SafeBrowsingService* sb_service = | |
| 301 g_browser_process->safe_browsing_service(); | |
| 302 if (sb_service) { | |
|
msw
2015/11/17 01:17:37
nit: curlies not needed
Jialiu Lin
2015/11/17 02:05:50
Done.
| |
| 303 sb_service->SendDownloadRecoveryReport(serialized_report); | |
| 304 } | |
| 305 } | |
| OLD | NEW |