| 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/download_crx_util.h" | 16 #include "chrome/browser/download/download_crx_util.h" |
| 17 #include "chrome/browser/download/download_service.h" |
| 18 #include "chrome/browser/download/download_service_factory.h" |
| 16 #include "chrome/browser/safe_browsing/download_feedback_service.h" | 19 #include "chrome/browser/safe_browsing/download_feedback_service.h" |
| 17 #include "content/public/browser/download_danger_type.h" | 20 #include "content/public/browser/download_danger_type.h" |
| 18 #include "content/public/browser/download_interrupt_reasons.h" | 21 #include "content/public/browser/download_interrupt_reasons.h" |
| 19 #include "content/public/browser/download_item.h" | 22 #include "content/public/browser/download_item.h" |
| 20 #include "grit/chromium_strings.h" | 23 #include "grit/chromium_strings.h" |
| 21 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/base/l10n/time_format.h" | 26 #include "ui/base/l10n/time_format.h" |
| 24 #include "ui/base/text/bytes_formatting.h" | 27 #include "ui/base/text/bytes_formatting.h" |
| 25 #include "ui/gfx/text_elider.h" | 28 #include "ui/gfx/text_elider.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 45 bool should_show_in_shelf() const { return should_show_in_shelf_; } | 48 bool should_show_in_shelf() const { return should_show_in_shelf_; } |
| 46 void set_should_show_in_shelf(bool should_show_in_shelf) { | 49 void set_should_show_in_shelf(bool should_show_in_shelf) { |
| 47 should_show_in_shelf_ = should_show_in_shelf; | 50 should_show_in_shelf_ = should_show_in_shelf; |
| 48 } | 51 } |
| 49 | 52 |
| 50 bool should_notify_ui() const { return should_notify_ui_; } | 53 bool should_notify_ui() const { return should_notify_ui_; } |
| 51 void set_should_notify_ui(bool should_notify_ui) { | 54 void set_should_notify_ui(bool should_notify_ui) { |
| 52 should_notify_ui_ = should_notify_ui; | 55 should_notify_ui_ = should_notify_ui; |
| 53 } | 56 } |
| 54 | 57 |
| 58 bool should_prefer_opening_in_browser() const { |
| 59 return should_prefer_opening_in_browser_; |
| 60 } |
| 61 void set_should_prefer_opening_in_browser(bool preference) { |
| 62 should_prefer_opening_in_browser_ = preference; |
| 63 } |
| 64 |
| 55 private: | 65 private: |
| 56 DownloadItemModelData(); | 66 DownloadItemModelData(); |
| 57 virtual ~DownloadItemModelData() {} | 67 virtual ~DownloadItemModelData() {} |
| 58 | 68 |
| 59 static const char kKey[]; | 69 static const char kKey[]; |
| 60 | 70 |
| 61 // Whether the download should be displayed in the download shelf. True by | 71 // Whether the download should be displayed in the download shelf. True by |
| 62 // default. | 72 // default. |
| 63 bool should_show_in_shelf_; | 73 bool should_show_in_shelf_; |
| 64 | 74 |
| 65 // Whether the UI should be notified when the download is ready to be | 75 // Whether the UI should be notified when the download is ready to be |
| 66 // presented. | 76 // presented. |
| 67 bool should_notify_ui_; | 77 bool should_notify_ui_; |
| 78 |
| 79 // Whether the download should be opened in the browser vs. the system handler |
| 80 // for the file type. |
| 81 bool should_prefer_opening_in_browser_; |
| 68 }; | 82 }; |
| 69 | 83 |
| 70 // static | 84 // static |
| 71 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; | 85 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; |
| 72 | 86 |
| 73 // static | 87 // static |
| 74 const DownloadItemModelData* DownloadItemModelData::Get( | 88 const DownloadItemModelData* DownloadItemModelData::Get( |
| 75 const DownloadItem* download) { | 89 const DownloadItem* download) { |
| 76 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); | 90 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); |
| 77 } | 91 } |
| 78 | 92 |
| 79 // static | 93 // static |
| 80 DownloadItemModelData* DownloadItemModelData::GetOrCreate( | 94 DownloadItemModelData* DownloadItemModelData::GetOrCreate( |
| 81 DownloadItem* download) { | 95 DownloadItem* download) { |
| 82 DownloadItemModelData* data = | 96 DownloadItemModelData* data = |
| 83 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); | 97 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); |
| 84 if (data == NULL) { | 98 if (data == NULL) { |
| 85 data = new DownloadItemModelData(); | 99 data = new DownloadItemModelData(); |
| 86 download->SetUserData(kKey, data); | 100 download->SetUserData(kKey, data); |
| 87 } | 101 } |
| 88 return data; | 102 return data; |
| 89 } | 103 } |
| 90 | 104 |
| 91 DownloadItemModelData::DownloadItemModelData() | 105 DownloadItemModelData::DownloadItemModelData() |
| 92 : should_show_in_shelf_(true), | 106 : should_show_in_shelf_(true), |
| 93 should_notify_ui_(false) { | 107 should_notify_ui_(false), |
| 108 should_prefer_opening_in_browser_(false) { |
| 94 } | 109 } |
| 95 | 110 |
| 96 string16 InterruptReasonStatusMessage(int reason) { | 111 string16 InterruptReasonStatusMessage(int reason) { |
| 97 int string_id = 0; | 112 int string_id = 0; |
| 98 | 113 |
| 99 switch (reason) { | 114 switch (reason) { |
| 100 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: | 115 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: |
| 101 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; | 116 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; |
| 102 break; | 117 break; |
| 103 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: | 118 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 bool DownloadItemModel::ShouldNotifyUI() const { | 540 bool DownloadItemModel::ShouldNotifyUI() const { |
| 526 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); | 541 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
| 527 return data && data->should_notify_ui(); | 542 return data && data->should_notify_ui(); |
| 528 } | 543 } |
| 529 | 544 |
| 530 void DownloadItemModel::SetShouldNotifyUI(bool should_notify) { | 545 void DownloadItemModel::SetShouldNotifyUI(bool should_notify) { |
| 531 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 546 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
| 532 data->set_should_notify_ui(should_notify); | 547 data->set_should_notify_ui(should_notify); |
| 533 } | 548 } |
| 534 | 549 |
| 550 bool DownloadItemModel::ShouldPreferOpeningInBrowser() const { |
| 551 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
| 552 return data && data->should_prefer_opening_in_browser(); |
| 553 } |
| 554 |
| 555 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) { |
| 556 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
| 557 data->set_should_prefer_opening_in_browser(preference); |
| 558 } |
| 559 |
| 535 string16 DownloadItemModel::GetProgressSizesString() const { | 560 string16 DownloadItemModel::GetProgressSizesString() const { |
| 536 string16 size_ratio; | 561 string16 size_ratio; |
| 537 int64 size = GetCompletedBytes(); | 562 int64 size = GetCompletedBytes(); |
| 538 int64 total = GetTotalBytes(); | 563 int64 total = GetTotalBytes(); |
| 539 if (total > 0) { | 564 if (total > 0) { |
| 540 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); | 565 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); |
| 541 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); | 566 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); |
| 542 | 567 |
| 543 // In RTL locales, we render the text "size/total" in an RTL context. This | 568 // In RTL locales, we render the text "size/total" in an RTL context. This |
| 544 // is problematic since a string such as "123/456 MB" is displayed | 569 // 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... |
| 598 } | 623 } |
| 599 | 624 |
| 600 // In progress download with no known time left and non-zero completed bytes: | 625 // In progress download with no known time left and non-zero completed bytes: |
| 601 // "100/120 MB" or "100 MB" | 626 // "100/120 MB" or "100 MB" |
| 602 if (GetCompletedBytes() > 0) | 627 if (GetCompletedBytes() > 0) |
| 603 return size_ratio; | 628 return size_ratio; |
| 604 | 629 |
| 605 // Instead of displaying "0 B" we say "Starting..." | 630 // Instead of displaying "0 B" we say "Starting..." |
| 606 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING); | 631 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING); |
| 607 } | 632 } |
| 633 |
| 634 void DownloadItemModel::OpenUsingPlatformHandler() { |
| 635 DownloadService* download_service = |
| 636 DownloadServiceFactory::GetForBrowserContext( |
| 637 download_->GetBrowserContext()); |
| 638 if (!download_service) |
| 639 return; |
| 640 |
| 641 ChromeDownloadManagerDelegate* delegate = |
| 642 download_service->GetDownloadManagerDelegate(); |
| 643 if (!delegate) |
| 644 return; |
| 645 delegate->OpenDownloadUsingPlatformHandler(download_); |
| 646 } |
| OLD | NEW |