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