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_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 20 matching lines...) Expand all Loading... | |
| 31 #include "ui/gfx/text_elider.h" | 31 #include "ui/gfx/text_elider.h" |
| 32 | 32 |
| 33 using base::TimeDelta; | 33 using base::TimeDelta; |
| 34 using content::DownloadItem; | 34 using content::DownloadItem; |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 // Per DownloadItem data used by DownloadItemModel. The model doesn't keep any | 38 // Per DownloadItem data used by DownloadItemModel. The model doesn't keep any |
| 39 // state since there could be multiple models associated with a single | 39 // state since there could be multiple models associated with a single |
| 40 // DownloadItem, and the lifetime of the model is shorter than the DownloadItem. | 40 // DownloadItem, and the lifetime of the model is shorter than the DownloadItem. |
| 41 class DownloadItemModelData : public base::SupportsUserData::Data { | 41 class DownloadItemModelData : public base::SupportsUserData::Data, |
| 42 public DownloadItem::Observer { | |
|
asanka
2014/06/12 21:08:47
Let's try to avoid having the DownloadItemModelDat
| |
| 42 public: | 43 public: |
| 43 // Get the DownloadItemModelData object for |download|. Returns NULL if | 44 // Get the DownloadItemModelData object for |download|. Returns NULL if |
| 44 // there's no model data. | 45 // there's no model data. |
| 45 static const DownloadItemModelData* Get(const DownloadItem* download); | 46 static const DownloadItemModelData* Get(const DownloadItem* download); |
| 46 | 47 |
| 47 // Get the DownloadItemModelData object for |download|. Creates a model data | 48 // Get the DownloadItemModelData object for |download|. Creates a model data |
| 48 // object if not found. Always returns a non-NULL pointer, unless OOM. | 49 // object if not found. Always returns a non-NULL pointer, unless OOM. |
| 49 static DownloadItemModelData* GetOrCreate(DownloadItem* download); | 50 static DownloadItemModelData* GetOrCreate(DownloadItem* download); |
| 50 | 51 |
| 51 bool should_show_in_shelf() const { return should_show_in_shelf_; } | 52 bool should_show_in_shelf() const { return should_show_in_shelf_; } |
| 52 void set_should_show_in_shelf(bool should_show_in_shelf) { | 53 void set_should_show_in_shelf(bool should_show_in_shelf) { |
| 53 should_show_in_shelf_ = should_show_in_shelf; | 54 should_show_in_shelf_ = should_show_in_shelf; |
| 54 } | 55 } |
| 55 | 56 |
| 56 bool was_ui_notified() const { return was_ui_notified_; } | 57 bool was_ui_notified() const { return was_ui_notified_; } |
| 57 void set_was_ui_notified(bool was_ui_notified) { | 58 void set_was_ui_notified(bool was_ui_notified) { |
| 58 was_ui_notified_ = was_ui_notified; | 59 was_ui_notified_ = was_ui_notified; |
| 59 } | 60 } |
| 60 | 61 |
| 61 bool should_prefer_opening_in_browser() const { | 62 bool should_prefer_opening_in_browser() const { |
| 62 return should_prefer_opening_in_browser_; | 63 return should_prefer_opening_in_browser_; |
| 63 } | 64 } |
| 64 void set_should_prefer_opening_in_browser(bool preference) { | 65 void set_should_prefer_opening_in_browser(bool preference) { |
| 65 should_prefer_opening_in_browser_ = preference; | 66 should_prefer_opening_in_browser_ = preference; |
| 66 } | 67 } |
| 67 | 68 |
| 69 bool user_acted() const { | |
| 70 return user_acted_; | |
| 71 } | |
| 72 void set_user_acted(bool acted) { | |
| 73 user_acted_ = acted; | |
| 74 } | |
| 75 | |
| 68 private: | 76 private: |
| 69 DownloadItemModelData(); | 77 virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE; |
| 70 virtual ~DownloadItemModelData() {} | 78 virtual void OnDownloadShown(DownloadItem* download) OVERRIDE; |
| 79 virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; | |
| 80 | |
| 81 private: | |
| 82 explicit DownloadItemModelData(DownloadItem* download); | |
| 83 virtual ~DownloadItemModelData(); | |
| 71 | 84 |
| 72 static const char kKey[]; | 85 static const char kKey[]; |
| 73 | 86 |
| 87 // Download item that holds this data. | |
| 88 DownloadItem* download_; | |
| 89 | |
| 74 // Whether the download should be displayed in the download shelf. True by | 90 // Whether the download should be displayed in the download shelf. True by |
| 75 // default. | 91 // default. |
| 76 bool should_show_in_shelf_; | 92 bool should_show_in_shelf_; |
| 77 | 93 |
| 78 // Whether the UI has been notified about this download. | 94 // Whether the UI has been notified about this download. |
| 79 bool was_ui_notified_; | 95 bool was_ui_notified_; |
| 80 | 96 |
| 81 // Whether the download should be opened in the browser vs. the system handler | 97 // Whether the download should be opened in the browser vs. the system handler |
| 82 // for the file type. | 98 // for the file type. |
| 83 bool should_prefer_opening_in_browser_; | 99 bool should_prefer_opening_in_browser_; |
| 100 | |
| 101 // Whether user acted on this item, so that download shelf can autohide. | |
| 102 bool user_acted_; | |
| 84 }; | 103 }; |
| 85 | 104 |
| 86 // static | 105 // static |
| 87 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; | 106 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; |
| 88 | 107 |
| 89 // static | 108 // static |
| 90 const DownloadItemModelData* DownloadItemModelData::Get( | 109 const DownloadItemModelData* DownloadItemModelData::Get( |
| 91 const DownloadItem* download) { | 110 const DownloadItem* download) { |
| 92 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); | 111 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); |
| 93 } | 112 } |
| 94 | 113 |
| 95 // static | 114 // static |
| 96 DownloadItemModelData* DownloadItemModelData::GetOrCreate( | 115 DownloadItemModelData* DownloadItemModelData::GetOrCreate( |
| 97 DownloadItem* download) { | 116 DownloadItem* download) { |
| 98 DownloadItemModelData* data = | 117 DownloadItemModelData* data = |
| 99 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); | 118 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); |
| 100 if (data == NULL) { | 119 if (data == NULL) { |
| 101 data = new DownloadItemModelData(); | 120 data = new DownloadItemModelData(download); |
| 102 download->SetUserData(kKey, data); | 121 download->SetUserData(kKey, data); |
| 103 } | 122 } |
| 104 return data; | 123 return data; |
| 105 } | 124 } |
| 106 | 125 |
| 107 DownloadItemModelData::DownloadItemModelData() | 126 DownloadItemModelData::DownloadItemModelData(DownloadItem* download) |
| 108 : should_show_in_shelf_(true), | 127 : download_(download), |
| 128 should_show_in_shelf_(true), | |
| 109 was_ui_notified_(false), | 129 was_ui_notified_(false), |
| 110 should_prefer_opening_in_browser_(false) { | 130 should_prefer_opening_in_browser_(false), |
| 131 user_acted_(false) { | |
| 132 DCHECK(download); | |
| 133 | |
| 134 // As |download| might be loaded from history, update our own state. | |
| 135 user_acted_ = download->GetOpened(); | |
| 136 download_->AddObserver(this); | |
| 137 } | |
| 138 | |
| 139 DownloadItemModelData::~DownloadItemModelData() { | |
| 140 if (download_) | |
| 141 download_->RemoveObserver(this); | |
| 142 } | |
| 143 | |
| 144 void DownloadItemModelData::OnDownloadOpened(DownloadItem* download) { | |
| 145 DCHECK_EQ(download_, download); | |
| 146 user_acted_ = true; | |
| 147 } | |
| 148 | |
| 149 void DownloadItemModelData::OnDownloadShown(DownloadItem* download) { | |
| 150 DCHECK_EQ(download_, download); | |
| 151 | |
| 152 // The user can show download in shell even when it is not yet completed, | |
| 153 // as this flag is used to 'hide' the download item from user's sight, it is | |
| 154 // fair to do so only for completed downloads. | |
| 155 if (download->GetState() == DownloadItem::COMPLETE) { | |
| 156 user_acted_ = true; | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 void DownloadItemModelData::OnDownloadDestroyed(DownloadItem* download) { | |
| 161 DCHECK_EQ(download_, download); | |
| 162 | |
| 163 // This is called from DownloadItem's destructor - so that | |
| 164 // base::SupportsUserData would destroy this model. | |
| 165 download_->RemoveObserver(this); | |
| 166 download_ = NULL; | |
| 111 } | 167 } |
| 112 | 168 |
| 113 base::string16 InterruptReasonStatusMessage(int reason) { | 169 base::string16 InterruptReasonStatusMessage(int reason) { |
| 114 int string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS; | 170 int string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS; |
| 115 | 171 |
| 116 switch (static_cast<content::DownloadInterruptReason>(reason)) { | 172 switch (static_cast<content::DownloadInterruptReason>(reason)) { |
| 117 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: | 173 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: |
| 118 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; | 174 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; |
| 119 break; | 175 break; |
| 120 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: | 176 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: |
| (...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 683 if (!download_service) | 739 if (!download_service) |
| 684 return; | 740 return; |
| 685 | 741 |
| 686 ChromeDownloadManagerDelegate* delegate = | 742 ChromeDownloadManagerDelegate* delegate = |
| 687 download_service->GetDownloadManagerDelegate(); | 743 download_service->GetDownloadManagerDelegate(); |
| 688 if (!delegate) | 744 if (!delegate) |
| 689 return; | 745 return; |
| 690 delegate->OpenDownloadUsingPlatformHandler(download_); | 746 delegate->OpenDownloadUsingPlatformHandler(download_); |
| 691 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); | 747 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); |
| 692 } | 748 } |
| 749 | |
| 750 void DownloadItemModel::SetUserActed(bool acted) { | |
| 751 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | |
| 752 data->set_user_acted(acted); | |
| 753 } | |
| 754 | |
| 755 bool DownloadItemModel::GetUserActed() const { | |
| 756 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | |
| 757 return data->user_acted(); | |
| 758 } | |
| OLD | NEW |