| 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_service.h" | 18 #include "chrome/browser/download/download_service.h" |
| 18 #include "chrome/browser/download/download_service_factory.h" | 19 #include "chrome/browser/download/download_service_factory.h" |
| 19 #include "chrome/browser/download/download_stats.h" | 20 #include "chrome/browser/download/download_stats.h" |
| 21 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/safe_browsing/download_feedback_service.h" | 22 #include "chrome/browser/safe_browsing/download_feedback_service.h" |
| 21 #include "content/public/browser/download_danger_type.h" | 23 #include "content/public/browser/download_danger_type.h" |
| 22 #include "content/public/browser/download_interrupt_reasons.h" | 24 #include "content/public/browser/download_interrupt_reasons.h" |
| 23 #include "content/public/browser/download_item.h" | 25 #include "content/public/browser/download_item.h" |
| 24 #include "grit/chromium_strings.h" | 26 #include "grit/chromium_strings.h" |
| 25 #include "grit/generated_resources.h" | 27 #include "grit/generated_resources.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
| 27 #include "ui/base/l10n/time_format.h" | 29 #include "ui/base/l10n/time_format.h" |
| 28 #include "ui/base/text/bytes_formatting.h" | 30 #include "ui/base/text/bytes_formatting.h" |
| 29 #include "ui/gfx/text_elider.h" | 31 #include "ui/gfx/text_elider.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 44 | 46 |
| 45 // Get the DownloadItemModelData object for |download|. Creates a model data | 47 // Get the DownloadItemModelData object for |download|. Creates a model data |
| 46 // object if not found. Always returns a non-NULL pointer, unless OOM. | 48 // object if not found. Always returns a non-NULL pointer, unless OOM. |
| 47 static DownloadItemModelData* GetOrCreate(DownloadItem* download); | 49 static DownloadItemModelData* GetOrCreate(DownloadItem* download); |
| 48 | 50 |
| 49 bool should_show_in_shelf() const { return should_show_in_shelf_; } | 51 bool should_show_in_shelf() const { return should_show_in_shelf_; } |
| 50 void set_should_show_in_shelf(bool should_show_in_shelf) { | 52 void set_should_show_in_shelf(bool should_show_in_shelf) { |
| 51 should_show_in_shelf_ = should_show_in_shelf; | 53 should_show_in_shelf_ = should_show_in_shelf; |
| 52 } | 54 } |
| 53 | 55 |
| 54 bool should_notify_ui() const { return should_notify_ui_; } | 56 bool was_ui_notified() const { return was_ui_notified_; } |
| 55 void set_should_notify_ui(bool should_notify_ui) { | 57 void set_was_ui_notified(bool was_ui_notified) { |
| 56 should_notify_ui_ = should_notify_ui; | 58 was_ui_notified_ = was_ui_notified; |
| 57 } | 59 } |
| 58 | 60 |
| 59 bool should_prefer_opening_in_browser() const { | 61 bool should_prefer_opening_in_browser() const { |
| 60 return should_prefer_opening_in_browser_; | 62 return should_prefer_opening_in_browser_; |
| 61 } | 63 } |
| 62 void set_should_prefer_opening_in_browser(bool preference) { | 64 void set_should_prefer_opening_in_browser(bool preference) { |
| 63 should_prefer_opening_in_browser_ = preference; | 65 should_prefer_opening_in_browser_ = preference; |
| 64 } | 66 } |
| 65 | 67 |
| 66 private: | 68 private: |
| 67 DownloadItemModelData(); | 69 DownloadItemModelData(); |
| 68 virtual ~DownloadItemModelData() {} | 70 virtual ~DownloadItemModelData() {} |
| 69 | 71 |
| 70 static const char kKey[]; | 72 static const char kKey[]; |
| 71 | 73 |
| 72 // Whether the download should be displayed in the download shelf. True by | 74 // Whether the download should be displayed in the download shelf. True by |
| 73 // default. | 75 // default. |
| 74 bool should_show_in_shelf_; | 76 bool should_show_in_shelf_; |
| 75 | 77 |
| 76 // Whether the UI should be notified when the download is ready to be | 78 // Whether the UI has been notified about this download. |
| 77 // presented. | 79 bool was_ui_notified_; |
| 78 bool should_notify_ui_; | |
| 79 | 80 |
| 80 // Whether the download should be opened in the browser vs. the system handler | 81 // Whether the download should be opened in the browser vs. the system handler |
| 81 // for the file type. | 82 // for the file type. |
| 82 bool should_prefer_opening_in_browser_; | 83 bool should_prefer_opening_in_browser_; |
| 83 }; | 84 }; |
| 84 | 85 |
| 85 // static | 86 // static |
| 86 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; | 87 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; |
| 87 | 88 |
| 88 // static | 89 // static |
| 89 const DownloadItemModelData* DownloadItemModelData::Get( | 90 const DownloadItemModelData* DownloadItemModelData::Get( |
| 90 const DownloadItem* download) { | 91 const DownloadItem* download) { |
| 91 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); | 92 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); |
| 92 } | 93 } |
| 93 | 94 |
| 94 // static | 95 // static |
| 95 DownloadItemModelData* DownloadItemModelData::GetOrCreate( | 96 DownloadItemModelData* DownloadItemModelData::GetOrCreate( |
| 96 DownloadItem* download) { | 97 DownloadItem* download) { |
| 97 DownloadItemModelData* data = | 98 DownloadItemModelData* data = |
| 98 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); | 99 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); |
| 99 if (data == NULL) { | 100 if (data == NULL) { |
| 100 data = new DownloadItemModelData(); | 101 data = new DownloadItemModelData(); |
| 101 download->SetUserData(kKey, data); | 102 download->SetUserData(kKey, data); |
| 102 } | 103 } |
| 103 return data; | 104 return data; |
| 104 } | 105 } |
| 105 | 106 |
| 106 DownloadItemModelData::DownloadItemModelData() | 107 DownloadItemModelData::DownloadItemModelData() |
| 107 : should_show_in_shelf_(true), | 108 : should_show_in_shelf_(true), |
| 108 should_notify_ui_(false), | 109 was_ui_notified_(false), |
| 109 should_prefer_opening_in_browser_(false) { | 110 should_prefer_opening_in_browser_(false) { |
| 110 } | 111 } |
| 111 | 112 |
| 112 base::string16 InterruptReasonStatusMessage(int reason) { | 113 base::string16 InterruptReasonStatusMessage(int reason) { |
| 113 int string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS; | 114 int string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS; |
| 114 | 115 |
| 115 switch (static_cast<content::DownloadInterruptReason>(reason)) { | 116 switch (static_cast<content::DownloadInterruptReason>(reason)) { |
| 116 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: | 117 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: |
| 117 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; | 118 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; |
| 118 break; | 119 break; |
| (...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); | 550 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
| 550 return !data || data->should_show_in_shelf(); | 551 return !data || data->should_show_in_shelf(); |
| 551 } | 552 } |
| 552 | 553 |
| 553 void DownloadItemModel::SetShouldShowInShelf(bool should_show) { | 554 void DownloadItemModel::SetShouldShowInShelf(bool should_show) { |
| 554 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 555 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
| 555 data->set_should_show_in_shelf(should_show); | 556 data->set_should_show_in_shelf(should_show); |
| 556 } | 557 } |
| 557 | 558 |
| 558 bool DownloadItemModel::ShouldNotifyUI() const { | 559 bool DownloadItemModel::ShouldNotifyUI() const { |
| 559 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); | 560 Profile* profile = |
| 560 return data && data->should_notify_ui(); | 561 Profile::FromBrowserContext(download_->GetBrowserContext()); |
| 562 DownloadService* download_service = |
| 563 DownloadServiceFactory::GetForBrowserContext(profile); |
| 564 DownloadHistory* download_history = |
| 565 (download_service ? download_service->GetDownloadHistory() : NULL); |
| 566 |
| 567 // The browser is only interested in new downloads. Ones that were restored |
| 568 // from history are not displayed on the shelf. The downloads page |
| 569 // independently listens for new downloads when it is active. Note that the UI |
| 570 // will be notified of downloads even if they are not meant to be displayed on |
| 571 // the shelf (i.e. ShouldShowInShelf() returns false). This is because: |
| 572 // * The shelf isn't the only UI. E.g. on Android, the UI is the system |
| 573 // DownloadManager. |
| 574 // * There are other UI activities that need to be performed. E.g. if the |
| 575 // download was initiated from a new tab, then that tab should be closed. |
| 576 // |
| 577 // TODO(asanka): If an interrupted download is restored from history and is |
| 578 // resumed, then ideally the UI should be notified. |
| 579 return !download_history || |
| 580 !download_history->WasRestoredFromHistory(download_); |
| 561 } | 581 } |
| 562 | 582 |
| 563 void DownloadItemModel::SetShouldNotifyUI(bool should_notify) { | 583 bool DownloadItemModel::WasUINotified() const { |
| 584 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
| 585 return data && data->was_ui_notified(); |
| 586 } |
| 587 |
| 588 void DownloadItemModel::SetWasUINotified(bool was_ui_notified) { |
| 564 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 589 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
| 565 data->set_should_notify_ui(should_notify); | 590 data->set_was_ui_notified(was_ui_notified); |
| 566 } | 591 } |
| 567 | 592 |
| 568 bool DownloadItemModel::ShouldPreferOpeningInBrowser() const { | 593 bool DownloadItemModel::ShouldPreferOpeningInBrowser() const { |
| 569 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); | 594 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
| 570 return data && data->should_prefer_opening_in_browser(); | 595 return data && data->should_prefer_opening_in_browser(); |
| 571 } | 596 } |
| 572 | 597 |
| 573 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) { | 598 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) { |
| 574 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 599 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
| 575 data->set_should_prefer_opening_in_browser(preference); | 600 data->set_should_prefer_opening_in_browser(preference); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 if (!download_service) | 683 if (!download_service) |
| 659 return; | 684 return; |
| 660 | 685 |
| 661 ChromeDownloadManagerDelegate* delegate = | 686 ChromeDownloadManagerDelegate* delegate = |
| 662 download_service->GetDownloadManagerDelegate(); | 687 download_service->GetDownloadManagerDelegate(); |
| 663 if (!delegate) | 688 if (!delegate) |
| 664 return; | 689 return; |
| 665 delegate->OpenDownloadUsingPlatformHandler(download_); | 690 delegate->OpenDownloadUsingPlatformHandler(download_); |
| 666 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); | 691 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); |
| 667 } | 692 } |
| OLD | NEW |