Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: chrome/browser/download/download_item_model.cc

Issue 565053002: [Downloads] Gracefully handle SafeBrowsing check failures. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_item_model.h" 5 #include "chrome/browser/download/download_item_model.h"
6 6
7 #include "base/i18n/number_formatting.h" 7 #include "base/i18n/number_formatting.h"
8 #include "base/i18n/rtl.h" 8 #include "base/i18n/rtl.h"
9 #include "base/metrics/field_trial.h" 9 #include "base/metrics/field_trial.h"
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
(...skipping 30 matching lines...) Expand all
41 class DownloadItemModelData : public base::SupportsUserData::Data { 41 class DownloadItemModelData : public base::SupportsUserData::Data {
42 public: 42 public:
43 // Get the DownloadItemModelData object for |download|. Returns NULL if 43 // Get the DownloadItemModelData object for |download|. Returns NULL if
44 // there's no model data. 44 // there's no model data.
45 static const DownloadItemModelData* Get(const DownloadItem* download); 45 static const DownloadItemModelData* Get(const DownloadItem* download);
46 46
47 // Get the DownloadItemModelData object for |download|. Creates a model data 47 // Get the DownloadItemModelData object for |download|. Creates a model data
48 // object if not found. Always returns a non-NULL pointer, unless OOM. 48 // object if not found. Always returns a non-NULL pointer, unless OOM.
49 static DownloadItemModelData* GetOrCreate(DownloadItem* download); 49 static DownloadItemModelData* GetOrCreate(DownloadItem* download);
50 50
51 bool should_show_in_shelf() const { return should_show_in_shelf_; }
52 void set_should_show_in_shelf(bool should_show_in_shelf) {
53 should_show_in_shelf_ = should_show_in_shelf;
54 }
55
56 bool was_ui_notified() const { return was_ui_notified_; }
57 void set_was_ui_notified(bool was_ui_notified) {
58 was_ui_notified_ = was_ui_notified;
59 }
60
61 bool should_prefer_opening_in_browser() const {
62 return should_prefer_opening_in_browser_;
63 }
64 void set_should_prefer_opening_in_browser(bool preference) {
65 should_prefer_opening_in_browser_ = preference;
66 }
67
68 private:
69 DownloadItemModelData();
70 virtual ~DownloadItemModelData() {}
71
72 static const char kKey[];
73
74 // Whether the download should be displayed in the download shelf. True by 51 // Whether the download should be displayed in the download shelf. True by
75 // default. 52 // default.
76 bool should_show_in_shelf_; 53 bool should_show_in_shelf_;
77 54
78 // Whether the UI has been notified about this download. 55 // Whether the UI has been notified about this download.
79 bool was_ui_notified_; 56 bool was_ui_notified_;
80 57
81 // Whether the download should be opened in the browser vs. the system handler 58 // Whether the download should be opened in the browser vs. the system handler
82 // for the file type. 59 // for the file type.
83 bool should_prefer_opening_in_browser_; 60 bool should_prefer_opening_in_browser_;
61
62 // Whether the download should be considered dangerous if SafeBrowsing doesn't
63 // come up with a verdict.
64 bool is_dangerous_file_based_on_type_;
65
66 private:
67 DownloadItemModelData();
68 virtual ~DownloadItemModelData() {}
69
70 static const char kKey[];
84 }; 71 };
85 72
86 // static 73 // static
87 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; 74 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key";
88 75
89 // static 76 // static
90 const DownloadItemModelData* DownloadItemModelData::Get( 77 const DownloadItemModelData* DownloadItemModelData::Get(
91 const DownloadItem* download) { 78 const DownloadItem* download) {
92 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); 79 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey));
93 } 80 }
94 81
95 // static 82 // static
96 DownloadItemModelData* DownloadItemModelData::GetOrCreate( 83 DownloadItemModelData* DownloadItemModelData::GetOrCreate(
97 DownloadItem* download) { 84 DownloadItem* download) {
98 DownloadItemModelData* data = 85 DownloadItemModelData* data =
99 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); 86 static_cast<DownloadItemModelData*>(download->GetUserData(kKey));
100 if (data == NULL) { 87 if (data == NULL) {
101 data = new DownloadItemModelData(); 88 data = new DownloadItemModelData();
102 download->SetUserData(kKey, data); 89 download->SetUserData(kKey, data);
103 } 90 }
104 return data; 91 return data;
105 } 92 }
106 93
107 DownloadItemModelData::DownloadItemModelData() 94 DownloadItemModelData::DownloadItemModelData()
108 : should_show_in_shelf_(true), 95 : should_show_in_shelf_(true),
109 was_ui_notified_(false), 96 was_ui_notified_(false),
110 should_prefer_opening_in_browser_(false) { 97 should_prefer_opening_in_browser_(false),
98 is_dangerous_file_based_on_type_(false) {
111 } 99 }
112 100
113 base::string16 InterruptReasonStatusMessage(int reason) { 101 base::string16 InterruptReasonStatusMessage(int reason) {
114 int string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS; 102 int string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS;
115 103
116 switch (static_cast<content::DownloadInterruptReason>(reason)) { 104 switch (static_cast<content::DownloadInterruptReason>(reason)) {
117 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: 105 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED:
118 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; 106 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED;
119 break; 107 break;
120 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: 108 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE:
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 return false; 541 return false;
554 } 542 }
555 543
556 bool DownloadItemModel::ShouldShowDownloadStartedAnimation() const { 544 bool DownloadItemModel::ShouldShowDownloadStartedAnimation() const {
557 return !download_->IsSavePackageDownload() && 545 return !download_->IsSavePackageDownload() &&
558 !download_crx_util::IsExtensionDownload(*download_); 546 !download_crx_util::IsExtensionDownload(*download_);
559 } 547 }
560 548
561 bool DownloadItemModel::ShouldShowInShelf() const { 549 bool DownloadItemModel::ShouldShowInShelf() const {
562 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); 550 const DownloadItemModelData* data = DownloadItemModelData::Get(download_);
563 return !data || data->should_show_in_shelf(); 551 return !data || data->should_show_in_shelf_;
564 } 552 }
565 553
566 void DownloadItemModel::SetShouldShowInShelf(bool should_show) { 554 void DownloadItemModel::SetShouldShowInShelf(bool should_show) {
567 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); 555 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_);
568 data->set_should_show_in_shelf(should_show); 556 data->should_show_in_shelf_ = should_show;
569 } 557 }
570 558
571 bool DownloadItemModel::ShouldNotifyUI() const { 559 bool DownloadItemModel::ShouldNotifyUI() const {
572 Profile* profile = 560 Profile* profile =
573 Profile::FromBrowserContext(download_->GetBrowserContext()); 561 Profile::FromBrowserContext(download_->GetBrowserContext());
574 DownloadService* download_service = 562 DownloadService* download_service =
575 DownloadServiceFactory::GetForBrowserContext(profile); 563 DownloadServiceFactory::GetForBrowserContext(profile);
576 DownloadHistory* download_history = 564 DownloadHistory* download_history =
577 (download_service ? download_service->GetDownloadHistory() : NULL); 565 (download_service ? download_service->GetDownloadHistory() : NULL);
578 566
579 // The browser is only interested in new downloads. Ones that were restored 567 // The browser is only interested in new downloads. Ones that were restored
580 // from history are not displayed on the shelf. The downloads page 568 // from history are not displayed on the shelf. The downloads page
581 // independently listens for new downloads when it is active. Note that the UI 569 // independently listens for new downloads when it is active. Note that the UI
582 // will be notified of downloads even if they are not meant to be displayed on 570 // will be notified of downloads even if they are not meant to be displayed on
583 // the shelf (i.e. ShouldShowInShelf() returns false). This is because: 571 // the shelf (i.e. ShouldShowInShelf() returns false). This is because:
584 // * The shelf isn't the only UI. E.g. on Android, the UI is the system 572 // * The shelf isn't the only UI. E.g. on Android, the UI is the system
585 // DownloadManager. 573 // DownloadManager.
586 // * There are other UI activities that need to be performed. E.g. if the 574 // * There are other UI activities that need to be performed. E.g. if the
587 // download was initiated from a new tab, then that tab should be closed. 575 // download was initiated from a new tab, then that tab should be closed.
588 // 576 //
589 // TODO(asanka): If an interrupted download is restored from history and is 577 // TODO(asanka): If an interrupted download is restored from history and is
590 // resumed, then ideally the UI should be notified. 578 // resumed, then ideally the UI should be notified.
591 return !download_history || 579 return !download_history ||
592 !download_history->WasRestoredFromHistory(download_); 580 !download_history->WasRestoredFromHistory(download_);
593 } 581 }
594 582
595 bool DownloadItemModel::WasUINotified() const { 583 bool DownloadItemModel::WasUINotified() const {
596 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); 584 const DownloadItemModelData* data = DownloadItemModelData::Get(download_);
597 return data && data->was_ui_notified(); 585 return data && data->was_ui_notified_;
598 } 586 }
599 587
600 void DownloadItemModel::SetWasUINotified(bool was_ui_notified) { 588 void DownloadItemModel::SetWasUINotified(bool was_ui_notified) {
601 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); 589 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_);
602 data->set_was_ui_notified(was_ui_notified); 590 data->was_ui_notified_ = was_ui_notified;
603 } 591 }
604 592
605 bool DownloadItemModel::ShouldPreferOpeningInBrowser() const { 593 bool DownloadItemModel::ShouldPreferOpeningInBrowser() const {
606 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); 594 const DownloadItemModelData* data = DownloadItemModelData::Get(download_);
607 return data && data->should_prefer_opening_in_browser(); 595 return data && data->should_prefer_opening_in_browser_;
608 } 596 }
609 597
610 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) { 598 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) {
611 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); 599 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_);
612 data->set_should_prefer_opening_in_browser(preference); 600 data->should_prefer_opening_in_browser_ = preference;
601 }
602
603 bool DownloadItemModel::IsDangerousFileBasedOnType() const {
604 const DownloadItemModelData* data = DownloadItemModelData::Get(download_);
605 return data && data->is_dangerous_file_based_on_type_;
606 }
607
608 void DownloadItemModel::SetIsDangerousFileBasedOnType(bool dangerous) {
609 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_);
610 data->is_dangerous_file_based_on_type_ = dangerous;
613 } 611 }
614 612
615 base::string16 DownloadItemModel::GetProgressSizesString() const { 613 base::string16 DownloadItemModel::GetProgressSizesString() const {
616 base::string16 size_ratio; 614 base::string16 size_ratio;
617 int64 size = GetCompletedBytes(); 615 int64 size = GetCompletedBytes();
618 int64 total = GetTotalBytes(); 616 int64 total = GetTotalBytes();
619 if (total > 0) { 617 if (total > 0) {
620 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); 618 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total);
621 base::string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, fa lse); 619 base::string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, fa lse);
622 620
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 if (!download_service) 693 if (!download_service)
696 return; 694 return;
697 695
698 ChromeDownloadManagerDelegate* delegate = 696 ChromeDownloadManagerDelegate* delegate =
699 download_service->GetDownloadManagerDelegate(); 697 download_service->GetDownloadManagerDelegate();
700 if (!delegate) 698 if (!delegate)
701 return; 699 return;
702 delegate->OpenDownloadUsingPlatformHandler(download_); 700 delegate->OpenDownloadUsingPlatformHandler(download_);
703 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); 701 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM);
704 } 702 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_item_model.h ('k') | chrome/browser/download/download_target_determiner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698