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

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

Issue 11640007: Make the UI an observer of downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use DownloadItemModel. Address Nits. Created 8 years 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 | Annotate | Revision Log
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/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
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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 bool DownloadItemModel::ShouldShowInShelf() const { 386 bool DownloadItemModel::ShouldShowInShelf() const {
377 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); 387 const DownloadItemModelData* data = DownloadItemModelData::Get(download_);
378 return !data || data->should_show_in_shelf(); 388 return !data || data->should_show_in_shelf();
379 } 389 }
380 390
381 void DownloadItemModel::SetShouldShowInShelf(bool should_show) { 391 void DownloadItemModel::SetShouldShowInShelf(bool should_show) {
382 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); 392 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_);
383 data->set_should_show_in_shelf(should_show); 393 data->set_should_show_in_shelf(should_show);
384 } 394 }
385 395
396 bool DownloadItemModel::ShouldNotifyUI() const {
397 const DownloadItemModelData* data = DownloadItemModelData::Get(download_);
398 return data && data->should_notify_ui();
399 }
400
401 void DownloadItemModel::SetShouldNotifyUI(bool should_notify) {
402 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_);
403 data->set_should_notify_ui(should_notify);
404 }
405
386 string16 DownloadItemModel::GetProgressSizesString() const { 406 string16 DownloadItemModel::GetProgressSizesString() const {
387 string16 size_ratio; 407 string16 size_ratio;
388 int64 size = GetCompletedBytes(); 408 int64 size = GetCompletedBytes();
389 int64 total = GetTotalBytes(); 409 int64 total = GetTotalBytes();
390 if (total > 0) { 410 if (total > 0) {
391 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); 411 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total);
392 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); 412 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false);
393 413
394 // In RTL locales, we render the text "size/total" in an RTL context. This 414 // In RTL locales, we render the text "size/total" in an RTL context. This
395 // is problematic since a string such as "123/456 MB" is displayed 415 // is problematic since a string such as "123/456 MB" is displayed
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 } 469 }
450 470
451 // In progress download with no known time left and non-zero completed bytes: 471 // In progress download with no known time left and non-zero completed bytes:
452 // "100/120 MB" or "100 MB" 472 // "100/120 MB" or "100 MB"
453 if (GetCompletedBytes() > 0) 473 if (GetCompletedBytes() > 0)
454 return size_ratio; 474 return size_ratio;
455 475
456 // Instead of displaying "0 B" we say "Starting..." 476 // Instead of displaying "0 B" we say "Starting..."
457 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING); 477 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING);
458 } 478 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698