| 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_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" |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/supports_user_data.h" | 13 #include "base/supports_user_data.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 15 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 16 #include "chrome/browser/download/download_crx_util.h" | 16 #include "chrome/browser/download/download_crx_util.h" |
| 17 #include "chrome/browser/download/download_history.h" | 17 #include "chrome/browser/download/download_history.h" |
| 18 #include "chrome/browser/download/download_service.h" | 18 #include "chrome/browser/download/download_service.h" |
| 19 #include "chrome/browser/download/download_service_factory.h" | 19 #include "chrome/browser/download/download_service_factory.h" |
| 20 #include "chrome/browser/download/download_stats.h" | 20 #include "chrome/browser/download/download_stats.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/safe_browsing/download_feedback_service.h" | 22 #include "chrome/browser/safe_browsing/download_feedback_service.h" |
| 23 #include "chrome/common/safe_browsing/download_file_types.pb.h" |
| 23 #include "chrome/grit/chromium_strings.h" | 24 #include "chrome/grit/chromium_strings.h" |
| 24 #include "chrome/grit/generated_resources.h" | 25 #include "chrome/grit/generated_resources.h" |
| 25 #include "content/public/browser/download_danger_type.h" | 26 #include "content/public/browser/download_danger_type.h" |
| 26 #include "content/public/browser/download_interrupt_reasons.h" | 27 #include "content/public/browser/download_interrupt_reasons.h" |
| 27 #include "content/public/browser/download_item.h" | 28 #include "content/public/browser/download_item.h" |
| 28 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 29 #include "ui/base/l10n/time_format.h" | 30 #include "ui/base/l10n/time_format.h" |
| 30 #include "ui/base/text/bytes_formatting.h" | 31 #include "ui/base/text/bytes_formatting.h" |
| 31 #include "ui/gfx/text_elider.h" | 32 #include "ui/gfx/text_elider.h" |
| 32 | 33 |
| 33 using base::TimeDelta; | 34 using base::TimeDelta; |
| 34 using content::DownloadItem; | 35 using content::DownloadItem; |
| 36 using safe_browsing::DownloadFileType; |
| 35 | 37 |
| 36 namespace { | 38 namespace { |
| 37 | 39 |
| 38 // Per DownloadItem data used by DownloadItemModel. The model doesn't keep any | 40 // Per DownloadItem data used by DownloadItemModel. The model doesn't keep any |
| 39 // state since there could be multiple models associated with a single | 41 // state since there could be multiple models associated with a single |
| 40 // DownloadItem, and the lifetime of the model is shorter than the DownloadItem. | 42 // DownloadItem, and the lifetime of the model is shorter than the DownloadItem. |
| 41 class DownloadItemModelData : public base::SupportsUserData::Data { | 43 class DownloadItemModelData : public base::SupportsUserData::Data { |
| 42 public: | 44 public: |
| 43 // Get the DownloadItemModelData object for |download|. Returns NULL if | 45 // Get the DownloadItemModelData object for |download|. Returns NULL if |
| 44 // there's no model data. | 46 // there's no model data. |
| 45 static const DownloadItemModelData* Get(const DownloadItem* download); | 47 static const DownloadItemModelData* Get(const DownloadItem* download); |
| 46 | 48 |
| 47 // Get the DownloadItemModelData object for |download|. Creates a model data | 49 // Get the DownloadItemModelData object for |download|. Creates a model data |
| 48 // object if not found. Always returns a non-NULL pointer, unless OOM. | 50 // object if not found. Always returns a non-NULL pointer, unless OOM. |
| 49 static DownloadItemModelData* GetOrCreate(DownloadItem* download); | 51 static DownloadItemModelData* GetOrCreate(DownloadItem* download); |
| 50 | 52 |
| 51 // Whether the download should be displayed in the download shelf. True by | 53 // Whether the download should be displayed in the download shelf. True by |
| 52 // default. | 54 // default. |
| 53 bool should_show_in_shelf_; | 55 bool should_show_in_shelf_; |
| 54 | 56 |
| 55 // Whether the UI has been notified about this download. | 57 // Whether the UI has been notified about this download. |
| 56 bool was_ui_notified_; | 58 bool was_ui_notified_; |
| 57 | 59 |
| 58 // Whether the download should be opened in the browser vs. the system handler | 60 // Whether the download should be opened in the browser vs. the system handler |
| 59 // for the file type. | 61 // for the file type. |
| 60 bool should_prefer_opening_in_browser_; | 62 bool should_prefer_opening_in_browser_; |
| 61 | 63 |
| 62 // Danger level of the file determined based on the file type and whether | 64 // Danger level of the file determined based on the file type and whether |
| 63 // there was a user action associated with the download. | 65 // there was a user action associated with the download. |
| 64 download_util::DownloadDangerLevel danger_level_; | 66 DownloadFileType::DangerLevel danger_level_; |
| 65 | 67 |
| 66 // Whether the download is currently being revived. | 68 // Whether the download is currently being revived. |
| 67 bool is_being_revived_; | 69 bool is_being_revived_; |
| 68 | 70 |
| 69 private: | 71 private: |
| 70 DownloadItemModelData(); | 72 DownloadItemModelData(); |
| 71 ~DownloadItemModelData() override {} | 73 ~DownloadItemModelData() override {} |
| 72 | 74 |
| 73 static const char kKey[]; | 75 static const char kKey[]; |
| 74 }; | 76 }; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 91 data = new DownloadItemModelData(); | 93 data = new DownloadItemModelData(); |
| 92 download->SetUserData(kKey, data); | 94 download->SetUserData(kKey, data); |
| 93 } | 95 } |
| 94 return data; | 96 return data; |
| 95 } | 97 } |
| 96 | 98 |
| 97 DownloadItemModelData::DownloadItemModelData() | 99 DownloadItemModelData::DownloadItemModelData() |
| 98 : should_show_in_shelf_(true), | 100 : should_show_in_shelf_(true), |
| 99 was_ui_notified_(false), | 101 was_ui_notified_(false), |
| 100 should_prefer_opening_in_browser_(false), | 102 should_prefer_opening_in_browser_(false), |
| 101 danger_level_(download_util::NOT_DANGEROUS), | 103 danger_level_(DownloadFileType::NOT_DANGEROUS), |
| 102 is_being_revived_(false) {} | 104 is_being_revived_(false) {} |
| 103 | 105 |
| 104 base::string16 InterruptReasonStatusMessage( | 106 base::string16 InterruptReasonStatusMessage( |
| 105 content::DownloadInterruptReason reason) { | 107 content::DownloadInterruptReason reason) { |
| 106 int string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS; | 108 int string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS; |
| 107 | 109 |
| 108 switch (reason) { | 110 switch (reason) { |
| 109 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: | 111 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: |
| 110 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; | 112 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; |
| 111 break; | 113 break; |
| (...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 bool DownloadItemModel::ShouldPreferOpeningInBrowser() const { | 612 bool DownloadItemModel::ShouldPreferOpeningInBrowser() const { |
| 611 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); | 613 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
| 612 return data && data->should_prefer_opening_in_browser_; | 614 return data && data->should_prefer_opening_in_browser_; |
| 613 } | 615 } |
| 614 | 616 |
| 615 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) { | 617 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) { |
| 616 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 618 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
| 617 data->should_prefer_opening_in_browser_ = preference; | 619 data->should_prefer_opening_in_browser_ = preference; |
| 618 } | 620 } |
| 619 | 621 |
| 620 download_util::DownloadDangerLevel DownloadItemModel::GetDangerLevel() const { | 622 DownloadFileType::DangerLevel DownloadItemModel::GetDangerLevel() const { |
| 621 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); | 623 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
| 622 return data ? data->danger_level_ : download_util::NOT_DANGEROUS; | 624 return data ? data->danger_level_ : DownloadFileType::NOT_DANGEROUS; |
| 623 } | 625 } |
| 624 | 626 |
| 625 void DownloadItemModel::SetDangerLevel( | 627 void DownloadItemModel::SetDangerLevel( |
| 626 download_util::DownloadDangerLevel danger_level) { | 628 DownloadFileType::DangerLevel danger_level) { |
| 627 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 629 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
| 628 data->danger_level_ = danger_level; | 630 data->danger_level_ = danger_level; |
| 629 } | 631 } |
| 630 | 632 |
| 631 bool DownloadItemModel::IsBeingRevived() const { | 633 bool DownloadItemModel::IsBeingRevived() const { |
| 632 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); | 634 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
| 633 return data && data->is_being_revived_; | 635 return data && data->is_being_revived_; |
| 634 } | 636 } |
| 635 | 637 |
| 636 void DownloadItemModel::SetIsBeingRevived(bool is_being_revived) { | 638 void DownloadItemModel::SetIsBeingRevived(bool is_being_revived) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 if (!download_service) | 723 if (!download_service) |
| 722 return; | 724 return; |
| 723 | 725 |
| 724 ChromeDownloadManagerDelegate* delegate = | 726 ChromeDownloadManagerDelegate* delegate = |
| 725 download_service->GetDownloadManagerDelegate(); | 727 download_service->GetDownloadManagerDelegate(); |
| 726 if (!delegate) | 728 if (!delegate) |
| 727 return; | 729 return; |
| 728 delegate->OpenDownloadUsingPlatformHandler(download_); | 730 delegate->OpenDownloadUsingPlatformHandler(download_); |
| 729 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); | 731 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); |
| 730 } | 732 } |
| OLD | NEW |