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/string16.h" | 9 #include "base/string16.h" |
10 #include "base/supports_user_data.h" | 10 #include "base/supports_user_data.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 | 38 |
39 // Get the DownloadItemModelData object for |download|. Creates a model data | 39 // Get the DownloadItemModelData object for |download|. Creates a model data |
40 // object if not found. Always returns a non-NULL pointer, unless OOM. | 40 // object if not found. Always returns a non-NULL pointer, unless OOM. |
41 static DownloadItemModelData* GetOrCreate(DownloadItem* download); | 41 static DownloadItemModelData* GetOrCreate(DownloadItem* download); |
42 | 42 |
43 bool should_show_in_shelf() const { return should_show_in_shelf_; } | 43 bool should_show_in_shelf() const { return should_show_in_shelf_; } |
44 void set_should_show_in_shelf(bool should_show_in_shelf) { | 44 void set_should_show_in_shelf(bool should_show_in_shelf) { |
45 should_show_in_shelf_ = should_show_in_shelf; | 45 should_show_in_shelf_ = should_show_in_shelf; |
46 } | 46 } |
47 | 47 |
| 48 bool should_notify_ui() const { return should_notify_ui_; } |
| 49 void set_should_notify_ui(bool should_notify_ui) { |
| 50 should_notify_ui_ = should_notify_ui; |
| 51 } |
| 52 |
48 private: | 53 private: |
49 DownloadItemModelData(); | 54 DownloadItemModelData(); |
50 virtual ~DownloadItemModelData() {} | 55 virtual ~DownloadItemModelData() {} |
51 | 56 |
52 static const char kKey[]; | 57 static const char kKey[]; |
53 | 58 |
54 // Whether the download should be displayed in the download shelf. True by | 59 // Whether the download should be displayed in the download shelf. True by |
55 // default. | 60 // default. |
56 bool should_show_in_shelf_; | 61 bool should_show_in_shelf_; |
| 62 |
| 63 // Whether the UI should be notified when the download is ready to be |
| 64 // presented. |
| 65 bool should_notify_ui_; |
57 }; | 66 }; |
58 | 67 |
59 // static | 68 // static |
60 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; | 69 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; |
61 | 70 |
62 // static | 71 // static |
63 const DownloadItemModelData* DownloadItemModelData::Get( | 72 const DownloadItemModelData* DownloadItemModelData::Get( |
64 const DownloadItem* download) { | 73 const DownloadItem* download) { |
65 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); | 74 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); |
66 } | 75 } |
67 | 76 |
68 // static | 77 // static |
69 DownloadItemModelData* DownloadItemModelData::GetOrCreate( | 78 DownloadItemModelData* DownloadItemModelData::GetOrCreate( |
70 DownloadItem* download) { | 79 DownloadItem* download) { |
71 DownloadItemModelData* data = | 80 DownloadItemModelData* data = |
72 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); | 81 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); |
73 if (data == NULL) { | 82 if (data == NULL) { |
74 data = new DownloadItemModelData(); | 83 data = new DownloadItemModelData(); |
75 download->SetUserData(kKey, data); | 84 download->SetUserData(kKey, data); |
76 } | 85 } |
77 return data; | 86 return data; |
78 } | 87 } |
79 | 88 |
80 DownloadItemModelData::DownloadItemModelData() | 89 DownloadItemModelData::DownloadItemModelData() |
81 : should_show_in_shelf_(true) { | 90 : should_show_in_shelf_(true), |
| 91 should_notify_ui_(false) { |
82 } | 92 } |
83 | 93 |
84 string16 InterruptReasonStatusMessage(int reason) { | 94 string16 InterruptReasonStatusMessage(int reason) { |
85 int string_id = 0; | 95 int string_id = 0; |
86 | 96 |
87 switch (reason) { | 97 switch (reason) { |
88 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: | 98 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: |
89 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; | 99 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; |
90 break; | 100 break; |
91 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: | 101 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 bool DownloadItemModel::ShouldShowInShelf() const { | 425 bool DownloadItemModel::ShouldShowInShelf() const { |
416 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); | 426 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
417 return !data || data->should_show_in_shelf(); | 427 return !data || data->should_show_in_shelf(); |
418 } | 428 } |
419 | 429 |
420 void DownloadItemModel::SetShouldShowInShelf(bool should_show) { | 430 void DownloadItemModel::SetShouldShowInShelf(bool should_show) { |
421 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 431 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
422 data->set_should_show_in_shelf(should_show); | 432 data->set_should_show_in_shelf(should_show); |
423 } | 433 } |
424 | 434 |
| 435 bool DownloadItemModel::ShouldNotifyUI() const { |
| 436 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
| 437 return data && data->should_notify_ui(); |
| 438 } |
| 439 |
| 440 void DownloadItemModel::SetShouldNotifyUI(bool should_notify) { |
| 441 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
| 442 data->set_should_notify_ui(should_notify); |
| 443 } |
| 444 |
425 string16 DownloadItemModel::GetProgressSizesString() const { | 445 string16 DownloadItemModel::GetProgressSizesString() const { |
426 string16 size_ratio; | 446 string16 size_ratio; |
427 int64 size = GetCompletedBytes(); | 447 int64 size = GetCompletedBytes(); |
428 int64 total = GetTotalBytes(); | 448 int64 total = GetTotalBytes(); |
429 if (total > 0) { | 449 if (total > 0) { |
430 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); | 450 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); |
431 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); | 451 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); |
432 | 452 |
433 // In RTL locales, we render the text "size/total" in an RTL context. This | 453 // In RTL locales, we render the text "size/total" in an RTL context. This |
434 // is problematic since a string such as "123/456 MB" is displayed | 454 // is problematic since a string such as "123/456 MB" is displayed |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 } | 508 } |
489 | 509 |
490 // In progress download with no known time left and non-zero completed bytes: | 510 // In progress download with no known time left and non-zero completed bytes: |
491 // "100/120 MB" or "100 MB" | 511 // "100/120 MB" or "100 MB" |
492 if (GetCompletedBytes() > 0) | 512 if (GetCompletedBytes() > 0) |
493 return size_ratio; | 513 return size_ratio; |
494 | 514 |
495 // Instead of displaying "0 B" we say "Starting..." | 515 // Instead of displaying "0 B" we say "Starting..." |
496 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING); | 516 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING); |
497 } | 517 } |
OLD | NEW |