Chromium Code Reviews| 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" |
| 11 #include "base/feature_list.h" | 11 #include "base/feature_list.h" |
| 12 #include "base/guid.h" | 12 #include "base/guid.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "base/values.h" | |
| 20 #include "chrome/common/chrome_features.h" | 21 #include "chrome/common/chrome_features.h" |
| 21 #include "chrome/grit/generated_resources.h" | 22 #include "chrome/grit/generated_resources.h" |
| 22 #include "components/ntp_snippets/features.h" | 23 #include "components/ntp_snippets/features.h" |
| 23 #include "components/ntp_snippets/pref_names.h" | 24 #include "components/ntp_snippets/pref_names.h" |
| 24 #include "components/ntp_snippets/pref_util.h" | 25 #include "components/ntp_snippets/pref_util.h" |
| 25 #include "components/offline_pages/core/offline_page_model_query.h" | 26 #include "components/offline_pages/core/offline_page_model_query.h" |
| 26 #include "components/prefs/pref_registry_simple.h" | 27 #include "components/prefs/pref_registry_simple.h" |
| 27 #include "components/prefs/pref_service.h" | 28 #include "components/prefs/pref_service.h" |
| 28 #include "components/variations/variations_associated_data.h" | 29 #include "components/variations/variations_associated_data.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 42 using offline_pages::OfflinePageModelQueryBuilder; | 43 using offline_pages::OfflinePageModelQueryBuilder; |
| 43 | 44 |
| 44 namespace { | 45 namespace { |
| 45 | 46 |
| 46 const int kDefaultMaxSuggestionsCount = 5; | 47 const int kDefaultMaxSuggestionsCount = 5; |
| 47 const char kAssetDownloadsPrefix = 'D'; | 48 const char kAssetDownloadsPrefix = 'D'; |
| 48 const char kOfflinePageDownloadsPrefix = 'O'; | 49 const char kOfflinePageDownloadsPrefix = 'O'; |
| 49 | 50 |
| 50 const char* kMaxSuggestionsCountParamName = "downloads_max_count"; | 51 const char* kMaxSuggestionsCountParamName = "downloads_max_count"; |
| 51 | 52 |
| 53 // Maximal number of dismissed asset download IDs stored at any time. | |
| 54 const int kMaxDismissedIdNum = 100; | |
|
Marc Treib
2016/12/09 16:37:26
nit: s/Num/Count/ (IMO that's much more common)
vitaliii
2016/12/09 17:30:36
Done.
| |
| 55 | |
| 52 int GetMaxSuggestionsCount() { | 56 int GetMaxSuggestionsCount() { |
| 53 bool assets_enabled = | 57 bool assets_enabled = |
| 54 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature); | 58 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature); |
| 55 return variations::GetVariationParamByFeatureAsInt( | 59 return variations::GetVariationParamByFeatureAsInt( |
| 56 assets_enabled ? features::kAssetDownloadSuggestionsFeature | 60 assets_enabled ? features::kAssetDownloadSuggestionsFeature |
| 57 : features::kOfflinePageDownloadSuggestionsFeature, | 61 : features::kOfflinePageDownloadSuggestionsFeature, |
| 58 kMaxSuggestionsCountParamName, kDefaultMaxSuggestionsCount); | 62 kMaxSuggestionsCountParamName, kDefaultMaxSuggestionsCount); |
| 59 } | 63 } |
| 60 | 64 |
| 61 std::string GetOfflinePagePerCategoryID(int64_t raw_offline_page_id) { | 65 std::string GetOfflinePagePerCategoryID(int64_t raw_offline_page_id) { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 /*has_more_action=*/false, | 179 /*has_more_action=*/false, |
| 176 /*has_reload_action=*/false, | 180 /*has_reload_action=*/false, |
| 177 /*has_view_all_action=*/download_manager_ui_enabled_, | 181 /*has_view_all_action=*/download_manager_ui_enabled_, |
| 178 /*show_if_empty=*/false, | 182 /*show_if_empty=*/false, |
| 179 l10n_util::GetStringUTF16(IDS_NTP_DOWNLOADS_SUGGESTIONS_SECTION_EMPTY)); | 183 l10n_util::GetStringUTF16(IDS_NTP_DOWNLOADS_SUGGESTIONS_SECTION_EMPTY)); |
| 180 } | 184 } |
| 181 | 185 |
| 182 void DownloadSuggestionsProvider::DismissSuggestion( | 186 void DownloadSuggestionsProvider::DismissSuggestion( |
| 183 const ContentSuggestion::ID& suggestion_id) { | 187 const ContentSuggestion::ID& suggestion_id) { |
| 184 DCHECK_EQ(provided_category_, suggestion_id.category()); | 188 DCHECK_EQ(provided_category_, suggestion_id.category()); |
| 185 std::set<std::string> dismissed_ids = | |
| 186 ReadDismissedIDsFromPrefs(CorrespondsToOfflinePage(suggestion_id)); | |
| 187 dismissed_ids.insert(suggestion_id.id_within_category()); | |
| 188 StoreDismissedIDsToPrefs(CorrespondsToOfflinePage(suggestion_id), | |
| 189 dismissed_ids); | |
| 190 | 189 |
| 190 AddToDismissedStorageIfNeeded(suggestion_id); | |
| 191 RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded(suggestion_id); | 191 RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded(suggestion_id); |
| 192 } | 192 } |
| 193 | 193 |
| 194 void DownloadSuggestionsProvider::FetchSuggestionImage( | 194 void DownloadSuggestionsProvider::FetchSuggestionImage( |
| 195 const ContentSuggestion::ID& suggestion_id, | 195 const ContentSuggestion::ID& suggestion_id, |
| 196 const ntp_snippets::ImageFetchedCallback& callback) { | 196 const ntp_snippets::ImageFetchedCallback& callback) { |
| 197 // TODO(vitaliii): Fetch proper thumbnail from OfflinePageModel once it is | 197 // TODO(vitaliii): Fetch proper thumbnail from OfflinePageModel once it is |
| 198 // available there. | 198 // available there. |
| 199 // TODO(vitaliii): Provide site's favicon for assets downloads or file type. | 199 // TODO(vitaliii): Provide site's favicon for assets downloads or file type. |
| 200 // See crbug.com/631447. | 200 // See crbug.com/631447. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 weak_ptr_factory_.GetWeakPtr(), callback)); | 251 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 252 } else { | 252 } else { |
| 253 GetPagesMatchingQueryCallbackForGetDismissedSuggestions( | 253 GetPagesMatchingQueryCallbackForGetDismissedSuggestions( |
| 254 callback, std::vector<OfflinePageItem>()); | 254 callback, std::vector<OfflinePageItem>()); |
| 255 } | 255 } |
| 256 } | 256 } |
| 257 | 257 |
| 258 void DownloadSuggestionsProvider::ClearDismissedSuggestionsForDebugging( | 258 void DownloadSuggestionsProvider::ClearDismissedSuggestionsForDebugging( |
| 259 Category category) { | 259 Category category) { |
| 260 DCHECK_EQ(provided_category_, category); | 260 DCHECK_EQ(provided_category_, category); |
| 261 StoreAssetDismissedIDsToPrefs(std::set<std::string>()); | 261 StoreAssetDismissedIDsToPrefs(std::list<std::string>()); |
| 262 StoreOfflinePageDismissedIDsToPrefs(std::set<std::string>()); | 262 StoreOfflinePageDismissedIDsToPrefs(std::set<std::string>()); |
| 263 AsynchronouslyFetchAllDownloadsAndSubmitSuggestions(); | 263 AsynchronouslyFetchAllDownloadsAndSubmitSuggestions(); |
| 264 } | 264 } |
| 265 | 265 |
| 266 // static | 266 // static |
| 267 void DownloadSuggestionsProvider::RegisterProfilePrefs( | 267 void DownloadSuggestionsProvider::RegisterProfilePrefs( |
| 268 PrefRegistrySimple* registry) { | 268 PrefRegistrySimple* registry) { |
| 269 registry->RegisterListPref(kDismissedAssetDownloadSuggestions); | 269 registry->RegisterListPref(kDismissedAssetDownloadSuggestions); |
| 270 registry->RegisterListPref(kDismissedOfflinePageDownloadSuggestions); | 270 registry->RegisterListPref(kDismissedOfflinePageDownloadSuggestions); |
| 271 } | 271 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 282 for (const OfflinePageItem& item : offline_pages) { | 282 for (const OfflinePageItem& item : offline_pages) { |
| 283 if (dismissed_ids.count(GetOfflinePagePerCategoryID(item.offline_id))) { | 283 if (dismissed_ids.count(GetOfflinePagePerCategoryID(item.offline_id))) { |
| 284 suggestions.push_back(ConvertOfflinePage(item)); | 284 suggestions.push_back(ConvertOfflinePage(item)); |
| 285 } | 285 } |
| 286 } | 286 } |
| 287 | 287 |
| 288 if (download_manager_) { | 288 if (download_manager_) { |
| 289 std::vector<DownloadItem*> all_downloads; | 289 std::vector<DownloadItem*> all_downloads; |
| 290 download_manager_->GetAllDownloads(&all_downloads); | 290 download_manager_->GetAllDownloads(&all_downloads); |
| 291 | 291 |
| 292 dismissed_ids = ReadAssetDismissedIDsFromPrefs(); | 292 std::list<std::string> dismissed_ids = ReadAssetDismissedIDsFromPrefs(); |
| 293 | 293 |
| 294 for (const DownloadItem* item : all_downloads) { | 294 for (const DownloadItem* item : all_downloads) { |
| 295 if (dismissed_ids.count(GetAssetDownloadPerCategoryID(item->GetId()))) { | 295 std::string id = GetAssetDownloadPerCategoryID(item->GetId()); |
| 296 if (std::find(dismissed_ids.begin(), dismissed_ids.end(), id) != | |
|
Marc Treib
2016/12/09 16:37:26
optional: base::ContainsValue (from stl_util.h) is
vitaliii
2016/12/09 17:30:36
Done.
Thanks, it is definitely more readable. I wa
| |
| 297 dismissed_ids.end()) { | |
|
Marc Treib
2016/12/09 16:37:26
Hm, so this is now O(n*m) instead of O((n+m)*log(n
vitaliii
2016/12/09 17:30:36
This is fine, because this method is used only in
| |
| 296 suggestions.push_back(ConvertDownloadItem(*item)); | 298 suggestions.push_back(ConvertDownloadItem(*item)); |
| 297 } | 299 } |
| 298 } | 300 } |
| 299 } | 301 } |
| 300 | 302 |
| 301 callback.Run(std::move(suggestions)); | 303 callback.Run(std::move(suggestions)); |
| 302 } | 304 } |
| 303 | 305 |
| 304 void DownloadSuggestionsProvider::OfflinePageModelLoaded( | 306 void DownloadSuggestionsProvider::OfflinePageModelLoaded( |
| 305 offline_pages::OfflinePageModel* model) { | 307 offline_pages::OfflinePageModel* model) { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 417 } | 419 } |
| 418 | 420 |
| 419 void DownloadSuggestionsProvider::FetchAssetsDownloads() { | 421 void DownloadSuggestionsProvider::FetchAssetsDownloads() { |
| 420 if (!download_manager_) { | 422 if (!download_manager_) { |
| 421 // The manager has gone down or was explicitly turned off. | 423 // The manager has gone down or was explicitly turned off. |
| 422 return; | 424 return; |
| 423 } | 425 } |
| 424 | 426 |
| 425 std::vector<DownloadItem*> all_downloads; | 427 std::vector<DownloadItem*> all_downloads; |
| 426 download_manager_->GetAllDownloads(&all_downloads); | 428 download_manager_->GetAllDownloads(&all_downloads); |
| 427 std::set<std::string> old_dismissed_ids = ReadAssetDismissedIDsFromPrefs(); | 429 std::list<std::string> old_dismissed_ids = ReadAssetDismissedIDsFromPrefs(); |
| 428 std::set<std::string> retained_dismissed_ids; | |
| 429 cached_asset_downloads_.clear(); | 430 cached_asset_downloads_.clear(); |
| 430 for (const DownloadItem* item : all_downloads) { | 431 for (const DownloadItem* item : all_downloads) { |
| 431 std::string within_category_id = | 432 std::string within_category_id = |
| 432 GetAssetDownloadPerCategoryID(item->GetId()); | 433 GetAssetDownloadPerCategoryID(item->GetId()); |
| 433 if (!old_dismissed_ids.count(within_category_id)) { | 434 if (std::find(old_dismissed_ids.begin(), old_dismissed_ids.end(), |
| 435 within_category_id) == old_dismissed_ids.end()) { | |
| 434 if (IsDownloadCompleted(*item)) { | 436 if (IsDownloadCompleted(*item)) { |
| 435 cached_asset_downloads_.push_back(item); | 437 cached_asset_downloads_.push_back(item); |
| 436 } | 438 } |
| 437 } else { | |
| 438 retained_dismissed_ids.insert(within_category_id); | |
| 439 } | 439 } |
| 440 } | 440 } |
| 441 | 441 |
| 442 if (old_dismissed_ids.size() != retained_dismissed_ids.size()) { | 442 // We do not prune dismissed IDs, because it is not possible to ensure that |
| 443 StoreAssetDismissedIDsToPrefs(retained_dismissed_ids); | 443 // the list of downloads is complete (i.e. DownloadManager has finished |
| 444 } | 444 // reading them). |
| 445 | 445 // TODO(vitaliii): Prune dismissed IDs once the |OnLoaded| notification is |
| 446 // provided. See crbug.com/672758. | |
| 446 const int max_suggestions_count = GetMaxSuggestionsCount(); | 447 const int max_suggestions_count = GetMaxSuggestionsCount(); |
| 447 if (static_cast<int>(cached_asset_downloads_.size()) > | 448 if (static_cast<int>(cached_asset_downloads_.size()) > |
| 448 max_suggestions_count) { | 449 max_suggestions_count) { |
| 449 // Partially sorts |downloads| such that: | 450 // Partially sorts |downloads| such that: |
| 450 // 1) The element at the index |max_suggestions_count| is changed to the | 451 // 1) The element at the index |max_suggestions_count| is changed to the |
| 451 // element which would occur on this position if |downloads| was sorted; | 452 // element which would occur on this position if |downloads| was sorted; |
| 452 // 2) All of the elements before index |max_suggestions_count| are less than | 453 // 2) All of the elements before index |max_suggestions_count| are less than |
| 453 // or equal to the elements after it. | 454 // or equal to the elements after it. |
| 454 std::nth_element(cached_asset_downloads_.begin(), | 455 std::nth_element(cached_asset_downloads_.begin(), |
| 455 cached_asset_downloads_.begin() + max_suggestions_count, | 456 cached_asset_downloads_.begin() + max_suggestions_count, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 537 bool DownloadSuggestionsProvider::CacheAssetDownloadIfNeeded( | 538 bool DownloadSuggestionsProvider::CacheAssetDownloadIfNeeded( |
| 538 const content::DownloadItem* item) { | 539 const content::DownloadItem* item) { |
| 539 if (!IsDownloadCompleted(*item)) { | 540 if (!IsDownloadCompleted(*item)) { |
| 540 return false; | 541 return false; |
| 541 } | 542 } |
| 542 | 543 |
| 543 if (base::ContainsValue(cached_asset_downloads_, item)) { | 544 if (base::ContainsValue(cached_asset_downloads_, item)) { |
| 544 return false; | 545 return false; |
| 545 } | 546 } |
| 546 | 547 |
| 547 std::set<std::string> dismissed_ids = ReadAssetDismissedIDsFromPrefs(); | 548 std::list<std::string> dismissed_ids = ReadAssetDismissedIDsFromPrefs(); |
| 548 if (dismissed_ids.count(GetAssetDownloadPerCategoryID(item->GetId()))) { | 549 std::string id = GetAssetDownloadPerCategoryID(item->GetId()); |
| 550 if (std::find(dismissed_ids.begin(), dismissed_ids.end(), id) != | |
| 551 dismissed_ids.end()) { | |
| 549 return false; | 552 return false; |
| 550 } | 553 } |
| 551 | 554 |
| 552 DCHECK_LE(static_cast<int>(cached_asset_downloads_.size()), | 555 DCHECK_LE(static_cast<int>(cached_asset_downloads_.size()), |
| 553 GetMaxSuggestionsCount()); | 556 GetMaxSuggestionsCount()); |
| 554 if (static_cast<int>(cached_asset_downloads_.size()) == | 557 if (static_cast<int>(cached_asset_downloads_.size()) == |
| 555 GetMaxSuggestionsCount()) { | 558 GetMaxSuggestionsCount()) { |
| 556 auto oldest = std::max_element(cached_asset_downloads_.begin(), | 559 auto oldest = std::max_element(cached_asset_downloads_.begin(), |
| 557 cached_asset_downloads_.end(), | 560 cached_asset_downloads_.end(), |
| 558 OrderDownloadsMostRecentlyDownloadedFirst()); | 561 OrderDownloadsMostRecentlyDownloadedFirst()); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 676 if (notify) { | 679 if (notify) { |
| 677 SubmitContentSuggestions(); | 680 SubmitContentSuggestions(); |
| 678 } | 681 } |
| 679 } | 682 } |
| 680 | 683 |
| 681 void DownloadSuggestionsProvider::InvalidateSuggestion( | 684 void DownloadSuggestionsProvider::InvalidateSuggestion( |
| 682 const std::string& id_within_category) { | 685 const std::string& id_within_category) { |
| 683 ContentSuggestion::ID suggestion_id(provided_category_, id_within_category); | 686 ContentSuggestion::ID suggestion_id(provided_category_, id_within_category); |
| 684 observer()->OnSuggestionInvalidated(this, suggestion_id); | 687 observer()->OnSuggestionInvalidated(this, suggestion_id); |
| 685 | 688 |
| 686 std::set<std::string> dismissed_ids = | 689 RemoveFromDismissedStorageIfNeeded(suggestion_id); |
| 687 ReadDismissedIDsFromPrefs(CorrespondsToOfflinePage(suggestion_id)); | |
| 688 auto it = dismissed_ids.find(id_within_category); | |
| 689 if (it != dismissed_ids.end()) { | |
| 690 dismissed_ids.erase(it); | |
| 691 StoreDismissedIDsToPrefs(CorrespondsToOfflinePage(suggestion_id), | |
| 692 dismissed_ids); | |
| 693 } | |
| 694 | |
| 695 RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded(suggestion_id); | 690 RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded(suggestion_id); |
| 696 } | 691 } |
| 697 | 692 |
| 698 std::set<std::string> | 693 // TODO(vitaliii): Do not use std::list once ensure pruning at the right time. |
| 694 // See crbug.com/672758. | |
| 695 std::list<std::string> | |
| 699 DownloadSuggestionsProvider::ReadAssetDismissedIDsFromPrefs() const { | 696 DownloadSuggestionsProvider::ReadAssetDismissedIDsFromPrefs() const { |
| 700 return ntp_snippets::prefs::ReadDismissedIDsFromPrefs( | 697 std::list<std::string> dismissed_ids; |
| 701 *pref_service_, kDismissedAssetDownloadSuggestions); | 698 const base::ListValue* list = |
| 699 pref_service_->GetList(kDismissedAssetDownloadSuggestions); | |
| 700 for (const std::unique_ptr<base::Value>& value : *list) { | |
| 701 std::string dismissed_id; | |
| 702 bool success = value->GetAsString(&dismissed_id); | |
| 703 DCHECK(success) << "Failed to parse dismissed id from prefs param " | |
| 704 << kDismissedAssetDownloadSuggestions << " into string."; | |
| 705 dismissed_ids.push_back(dismissed_id); | |
| 706 } | |
| 707 return dismissed_ids; | |
| 702 } | 708 } |
| 703 | 709 |
| 704 void DownloadSuggestionsProvider::StoreAssetDismissedIDsToPrefs( | 710 void DownloadSuggestionsProvider::StoreAssetDismissedIDsToPrefs( |
| 705 const std::set<std::string>& dismissed_ids) { | 711 const std::list<std::string>& dismissed_ids) { |
| 706 DCHECK(std::all_of( | 712 DCHECK(std::all_of( |
| 707 dismissed_ids.begin(), dismissed_ids.end(), | 713 dismissed_ids.begin(), dismissed_ids.end(), |
| 708 [](const std::string& id) { return id[0] == kAssetDownloadsPrefix; })); | 714 [](const std::string& id) { return id[0] == kAssetDownloadsPrefix; })); |
| 709 ntp_snippets::prefs::StoreDismissedIDsToPrefs( | 715 |
| 710 pref_service_, kDismissedAssetDownloadSuggestions, dismissed_ids); | 716 base::ListValue list; |
| 717 for (const std::string& dismissed_id : dismissed_ids) { | |
| 718 list.AppendString(dismissed_id); | |
| 719 } | |
| 720 pref_service_->Set(kDismissedAssetDownloadSuggestions, list); | |
| 711 } | 721 } |
| 712 | 722 |
| 713 std::set<std::string> | 723 std::set<std::string> |
| 714 DownloadSuggestionsProvider::ReadOfflinePageDismissedIDsFromPrefs() const { | 724 DownloadSuggestionsProvider::ReadOfflinePageDismissedIDsFromPrefs() const { |
| 715 return ntp_snippets::prefs::ReadDismissedIDsFromPrefs( | 725 return ntp_snippets::prefs::ReadDismissedIDsFromPrefs( |
| 716 *pref_service_, kDismissedOfflinePageDownloadSuggestions); | 726 *pref_service_, kDismissedOfflinePageDownloadSuggestions); |
| 717 } | 727 } |
| 718 | 728 |
| 719 void DownloadSuggestionsProvider::StoreOfflinePageDismissedIDsToPrefs( | 729 void DownloadSuggestionsProvider::StoreOfflinePageDismissedIDsToPrefs( |
| 720 const std::set<std::string>& dismissed_ids) { | 730 const std::set<std::string>& dismissed_ids) { |
| 721 DCHECK(std::all_of(dismissed_ids.begin(), dismissed_ids.end(), | 731 DCHECK(std::all_of(dismissed_ids.begin(), dismissed_ids.end(), |
| 722 [](const std::string& id) { | 732 [](const std::string& id) { |
| 723 return id[0] == kOfflinePageDownloadsPrefix; | 733 return id[0] == kOfflinePageDownloadsPrefix; |
| 724 })); | 734 })); |
| 725 ntp_snippets::prefs::StoreDismissedIDsToPrefs( | 735 ntp_snippets::prefs::StoreDismissedIDsToPrefs( |
| 726 pref_service_, kDismissedOfflinePageDownloadSuggestions, dismissed_ids); | 736 pref_service_, kDismissedOfflinePageDownloadSuggestions, dismissed_ids); |
| 727 } | 737 } |
| 728 | 738 |
| 729 std::set<std::string> DownloadSuggestionsProvider::ReadDismissedIDsFromPrefs( | 739 void DownloadSuggestionsProvider::AddToDismissedStorageIfNeeded( |
| 730 bool for_offline_page_downloads) const { | 740 const ContentSuggestion::ID& suggestion_id) { |
| 731 if (for_offline_page_downloads) { | 741 if (CorrespondsToOfflinePage(suggestion_id)) { |
| 732 return ReadOfflinePageDismissedIDsFromPrefs(); | 742 std::set<std::string> dismissed_ids = |
| 733 } | 743 ReadOfflinePageDismissedIDsFromPrefs(); |
| 734 return ReadAssetDismissedIDsFromPrefs(); | 744 dismissed_ids.insert(suggestion_id.id_within_category()); |
| 735 } | |
| 736 | |
| 737 // TODO(vitaliii): Store one set instead of two. See crbug.com/656024. | |
| 738 void DownloadSuggestionsProvider::StoreDismissedIDsToPrefs( | |
| 739 bool for_offline_page_downloads, | |
| 740 const std::set<std::string>& dismissed_ids) { | |
| 741 if (for_offline_page_downloads) { | |
| 742 StoreOfflinePageDismissedIDsToPrefs(dismissed_ids); | 745 StoreOfflinePageDismissedIDsToPrefs(dismissed_ids); |
| 743 } else { | 746 } else { |
| 744 StoreAssetDismissedIDsToPrefs(dismissed_ids); | 747 std::list<std::string> dismissed_ids = ReadAssetDismissedIDsFromPrefs(); |
| 748 // The suggestion may be double dismissed from previously opened NTPs. | |
| 749 if (std::find(dismissed_ids.begin(), dismissed_ids.end(), | |
| 750 suggestion_id.id_within_category()) == dismissed_ids.end()) { | |
| 751 dismissed_ids.push_back(suggestion_id.id_within_category()); | |
| 752 // TODO(vitaliii): Remove this workaround once the dismissed ids are | |
| 753 // pruned. See crbug.com/672758. | |
| 754 while (dismissed_ids.size() > kMaxDismissedIdNum) { | |
| 755 dismissed_ids.pop_front(); | |
| 756 } | |
| 757 StoreAssetDismissedIDsToPrefs(dismissed_ids); | |
| 758 } | |
| 745 } | 759 } |
| 746 } | 760 } |
| 747 | 761 |
| 762 void DownloadSuggestionsProvider::RemoveFromDismissedStorageIfNeeded( | |
| 763 const ContentSuggestion::ID& suggestion_id) { | |
| 764 if (CorrespondsToOfflinePage(suggestion_id)) { | |
| 765 std::set<std::string> dismissed_ids = | |
| 766 ReadOfflinePageDismissedIDsFromPrefs(); | |
| 767 if (dismissed_ids.count(suggestion_id.id_within_category())) { | |
| 768 dismissed_ids.erase(suggestion_id.id_within_category()); | |
| 769 StoreOfflinePageDismissedIDsToPrefs(dismissed_ids); | |
| 770 } | |
| 771 } else { | |
| 772 std::list<std::string> dismissed_ids = ReadAssetDismissedIDsFromPrefs(); | |
| 773 auto it = std::find(dismissed_ids.begin(), dismissed_ids.end(), | |
| 774 suggestion_id.id_within_category()); | |
| 775 if (it != dismissed_ids.end()) { | |
| 776 dismissed_ids.erase(it); | |
| 777 StoreAssetDismissedIDsToPrefs(dismissed_ids); | |
| 778 } | |
| 779 } | |
| 780 } | |
| 781 | |
| 748 void DownloadSuggestionsProvider::UnregisterDownloadItemObservers() { | 782 void DownloadSuggestionsProvider::UnregisterDownloadItemObservers() { |
| 749 DCHECK_NE(download_manager_, nullptr); | 783 DCHECK_NE(download_manager_, nullptr); |
| 750 | 784 |
| 751 std::vector<DownloadItem*> all_downloads; | 785 std::vector<DownloadItem*> all_downloads; |
| 752 download_manager_->GetAllDownloads(&all_downloads); | 786 download_manager_->GetAllDownloads(&all_downloads); |
| 753 | 787 |
| 754 for (DownloadItem* item : all_downloads) { | 788 for (DownloadItem* item : all_downloads) { |
| 755 item->RemoveObserver(this); | 789 item->RemoveObserver(this); |
| 756 } | 790 } |
| 757 } | 791 } |
| OLD | NEW |