| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ntp_snippets/download_suggestions_provider.h" | 5 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 } | 514 } |
| 515 | 515 |
| 516 std::vector<DownloadItem*> all_downloads; | 516 std::vector<DownloadItem*> all_downloads; |
| 517 download_manager_->GetAllDownloads(&all_downloads); | 517 download_manager_->GetAllDownloads(&all_downloads); |
| 518 std::set<std::string> old_dismissed_ids = ReadAssetDismissedIDsFromPrefs(); | 518 std::set<std::string> old_dismissed_ids = ReadAssetDismissedIDsFromPrefs(); |
| 519 std::set<std::string> retained_dismissed_ids; | 519 std::set<std::string> retained_dismissed_ids; |
| 520 cached_asset_downloads_.clear(); | 520 cached_asset_downloads_.clear(); |
| 521 for (DownloadItem* item : all_downloads) { | 521 for (DownloadItem* item : all_downloads) { |
| 522 std::string within_category_id = | 522 std::string within_category_id = |
| 523 GetAssetDownloadPerCategoryID(item->GetId()); | 523 GetAssetDownloadPerCategoryID(item->GetId()); |
| 524 // TODO(vitaliii): Provide proper last access time here once it is collected |
| 525 // for asset downloads. |
| 524 if (old_dismissed_ids.count(within_category_id)) { | 526 if (old_dismissed_ids.count(within_category_id)) { |
| 525 retained_dismissed_ids.insert(within_category_id); | 527 retained_dismissed_ids.insert(within_category_id); |
| 526 } else if (IsAssetDownloadCompleted(*item) && | 528 } else if (IsAssetDownloadCompleted(*item) && |
| 527 !IsDownloadOutdated(GetAssetDownloadPublishedTime(*item))) { | 529 !IsDownloadOutdated(GetAssetDownloadPublishedTime(*item), |
| 530 base::Time())) { |
| 528 cached_asset_downloads_.push_back(item); | 531 cached_asset_downloads_.push_back(item); |
| 529 // We may already observe this item and, therefore, we remove the | 532 // We may already observe this item and, therefore, we remove the |
| 530 // observer first. | 533 // observer first. |
| 531 item->RemoveObserver(this); | 534 item->RemoveObserver(this); |
| 532 item->AddObserver(this); | 535 item->AddObserver(this); |
| 533 } | 536 } |
| 534 } | 537 } |
| 535 | 538 |
| 536 if (old_dismissed_ids.size() != retained_dismissed_ids.size()) { | 539 if (old_dismissed_ids.size() != retained_dismissed_ids.size()) { |
| 537 StoreAssetDismissedIDsToPrefs(retained_dismissed_ids); | 540 StoreAssetDismissedIDsToPrefs(retained_dismissed_ids); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 base::UTF8ToUTF16(download_item.GetURL().host())); | 625 base::UTF8ToUTF16(download_item.GetURL().host())); |
| 623 auto extra = base::MakeUnique<ntp_snippets::DownloadSuggestionExtra>(); | 626 auto extra = base::MakeUnique<ntp_snippets::DownloadSuggestionExtra>(); |
| 624 extra->target_file_path = download_item.GetTargetFilePath(); | 627 extra->target_file_path = download_item.GetTargetFilePath(); |
| 625 extra->mime_type = download_item.GetMimeType(); | 628 extra->mime_type = download_item.GetMimeType(); |
| 626 extra->is_download_asset = true; | 629 extra->is_download_asset = true; |
| 627 suggestion.set_download_suggestion_extra(std::move(extra)); | 630 suggestion.set_download_suggestion_extra(std::move(extra)); |
| 628 return suggestion; | 631 return suggestion; |
| 629 } | 632 } |
| 630 | 633 |
| 631 bool DownloadSuggestionsProvider::IsDownloadOutdated( | 634 bool DownloadSuggestionsProvider::IsDownloadOutdated( |
| 632 const base::Time& published_time) { | 635 const base::Time& published_time, |
| 633 // TODO(vitaliii): Also consider last access time here once it is collected | 636 const base::Time& last_visited_time) { |
| 634 // for asset downloads. | 637 DCHECK(last_visited_time == base::Time() || |
| 635 return published_time < | 638 last_visited_time >= published_time); |
| 639 const base::Time& last_interaction_time = |
| 640 (last_visited_time == base::Time() ? published_time : last_visited_time); |
| 641 return last_interaction_time < |
| 636 clock_->Now() - base::TimeDelta::FromHours(GetMaxDownloadAgeHours()); | 642 clock_->Now() - base::TimeDelta::FromHours(GetMaxDownloadAgeHours()); |
| 637 } | 643 } |
| 638 | 644 |
| 639 bool DownloadSuggestionsProvider::CacheAssetDownloadIfNeeded( | 645 bool DownloadSuggestionsProvider::CacheAssetDownloadIfNeeded( |
| 640 const content::DownloadItem* item) { | 646 const content::DownloadItem* item) { |
| 641 if (!IsAssetDownloadCompleted(*item)) { | 647 if (!IsAssetDownloadCompleted(*item)) { |
| 642 return false; | 648 return false; |
| 643 } | 649 } |
| 644 | 650 |
| 645 if (base::ContainsValue(cached_asset_downloads_, item)) { | 651 if (base::ContainsValue(cached_asset_downloads_, item)) { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 ReadOfflinePageDismissedIDsFromPrefs(); | 746 ReadOfflinePageDismissedIDsFromPrefs(); |
| 741 std::set<std::string> retained_dismissed_ids; | 747 std::set<std::string> retained_dismissed_ids; |
| 742 std::vector<const OfflinePageItem*> items; | 748 std::vector<const OfflinePageItem*> items; |
| 743 // Filtering out dismissed items and pruning dismissed IDs. | 749 // Filtering out dismissed items and pruning dismissed IDs. |
| 744 for (const OfflinePageItem& item : all_download_offline_pages) { | 750 for (const OfflinePageItem& item : all_download_offline_pages) { |
| 745 std::string id_within_category = | 751 std::string id_within_category = |
| 746 GetOfflinePagePerCategoryID(item.offline_id); | 752 GetOfflinePagePerCategoryID(item.offline_id); |
| 747 if (old_dismissed_ids.count(id_within_category)) { | 753 if (old_dismissed_ids.count(id_within_category)) { |
| 748 retained_dismissed_ids.insert(id_within_category); | 754 retained_dismissed_ids.insert(id_within_category); |
| 749 } else { | 755 } else { |
| 750 if (!IsDownloadOutdated(GetOfflinePagePublishedTime(item))) { | 756 if (!IsDownloadOutdated(GetOfflinePagePublishedTime(item), |
| 757 item.last_access_time)) { |
| 751 items.push_back(&item); | 758 items.push_back(&item); |
| 752 } | 759 } |
| 753 } | 760 } |
| 754 } | 761 } |
| 755 | 762 |
| 756 const int max_suggestions_count = GetMaxSuggestionsCount(); | 763 const int max_suggestions_count = GetMaxSuggestionsCount(); |
| 757 if (static_cast<int>(items.size()) > max_suggestions_count) { | 764 if (static_cast<int>(items.size()) > max_suggestions_count) { |
| 758 // Partially sorts |items| such that: | 765 // Partially sorts |items| such that: |
| 759 // 1) The element at the index |max_suggestions_count| is changed to the | 766 // 1) The element at the index |max_suggestions_count| is changed to the |
| 760 // element which would occur on this position if |items| was sorted; | 767 // element which would occur on this position if |items| was sorted; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 void DownloadSuggestionsProvider::UnregisterDownloadItemObservers() { | 858 void DownloadSuggestionsProvider::UnregisterDownloadItemObservers() { |
| 852 DCHECK_NE(download_manager_, nullptr); | 859 DCHECK_NE(download_manager_, nullptr); |
| 853 | 860 |
| 854 std::vector<DownloadItem*> all_downloads; | 861 std::vector<DownloadItem*> all_downloads; |
| 855 download_manager_->GetAllDownloads(&all_downloads); | 862 download_manager_->GetAllDownloads(&all_downloads); |
| 856 | 863 |
| 857 for (DownloadItem* item : all_downloads) { | 864 for (DownloadItem* item : all_downloads) { |
| 858 item->RemoveObserver(this); | 865 item->RemoveObserver(this); |
| 859 } | 866 } |
| 860 } | 867 } |
| OLD | NEW |