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" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 using offline_pages::OfflinePageModelQueryBuilder; | 41 using offline_pages::OfflinePageModelQueryBuilder; |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 const int kDefaultMaxSuggestionsCount = 5; | 45 const int kDefaultMaxSuggestionsCount = 5; |
| 46 const char kAssetDownloadsPrefix = 'D'; | 46 const char kAssetDownloadsPrefix = 'D'; |
| 47 const char kOfflinePageDownloadsPrefix = 'O'; | 47 const char kOfflinePageDownloadsPrefix = 'O'; |
| 48 | 48 |
| 49 const char* kMaxSuggestionsCountParamName = "downloads_max_count"; | 49 const char* kMaxSuggestionsCountParamName = "downloads_max_count"; |
| 50 | 50 |
| 51 bool CompareOfflinePagesForSorting(const OfflinePageItem& left, | |
|
vitaliii
2016/12/08 17:40:32
Could you have this consistent (naming, struct) wi
vitaliii
2016/12/08 18:17:20
Actually, I understood that then the struct is ugl
dewittj
2016/12/08 19:09:02
Done.
dewittj
2016/12/08 19:09:02
Done.
| |
| 52 const OfflinePageItem& right) { | |
| 53 return left.creation_time > right.creation_time; | |
| 54 } | |
| 55 | |
| 51 int GetMaxSuggestionsCount() { | 56 int GetMaxSuggestionsCount() { |
| 52 bool assets_enabled = | 57 bool assets_enabled = |
| 53 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature); | 58 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature); |
| 54 return ntp_snippets::GetParamAsInt( | 59 return ntp_snippets::GetParamAsInt( |
| 55 assets_enabled ? features::kAssetDownloadSuggestionsFeature | 60 assets_enabled ? features::kAssetDownloadSuggestionsFeature |
| 56 : features::kOfflinePageDownloadSuggestionsFeature, | 61 : features::kOfflinePageDownloadSuggestionsFeature, |
| 57 kMaxSuggestionsCountParamName, kDefaultMaxSuggestionsCount); | 62 kMaxSuggestionsCountParamName, kDefaultMaxSuggestionsCount); |
| 58 } | 63 } |
| 59 | 64 |
| 60 std::string GetOfflinePagePerCategoryID(int64_t raw_offline_page_id) { | 65 std::string GetOfflinePagePerCategoryID(int64_t raw_offline_page_id) { |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 | 304 |
| 300 callback.Run(std::move(suggestions)); | 305 callback.Run(std::move(suggestions)); |
| 301 } | 306 } |
| 302 | 307 |
| 303 void DownloadSuggestionsProvider::OfflinePageModelLoaded( | 308 void DownloadSuggestionsProvider::OfflinePageModelLoaded( |
| 304 offline_pages::OfflinePageModel* model) { | 309 offline_pages::OfflinePageModel* model) { |
| 305 DCHECK_EQ(offline_page_model_, model); | 310 DCHECK_EQ(offline_page_model_, model); |
| 306 AsynchronouslyFetchOfflinePagesDownloads(/*notify=*/true); | 311 AsynchronouslyFetchOfflinePagesDownloads(/*notify=*/true); |
| 307 } | 312 } |
| 308 | 313 |
| 309 void DownloadSuggestionsProvider::OfflinePageAdded( | 314 void DownloadSuggestionsProvider::OfflinePageAdded( |
|
vitaliii
2016/12/08 17:40:32
Do I understand right that OfflinePage can be eith
dewittj
2016/12/08 19:09:02
Currently pages can be accessed (and thus altered)
vitaliii
2016/12/09 10:02:37
Acknowledged.
| |
| 310 offline_pages::OfflinePageModel* model, | 315 offline_pages::OfflinePageModel* model, |
| 311 const offline_pages::OfflinePageItem& added_page) { | 316 const offline_pages::OfflinePageItem& added_page) { |
| 312 // TODO(dewittj, vitaliii): Don't refetch everything when this is called. | |
| 313 DCHECK_EQ(offline_page_model_, model); | 317 DCHECK_EQ(offline_page_model_, model); |
| 314 AsynchronouslyFetchOfflinePagesDownloads(/*notify=*/true); | 318 if (!IsClientIdForOfflinePageDownload(model->GetPolicyController(), |
| 319 added_page.client_id)) { | |
| 320 return; | |
| 321 } | |
| 322 | |
| 323 cached_offline_page_downloads_.push_back(added_page); | |
|
vitaliii
2016/12/08 17:40:32
You also need to check that this offline page is n
vitaliii
2016/12/08 18:17:20
Ok, technically you do not need to check this unle
dewittj
2016/12/08 19:09:02
I didn't add that check because I assumed that new
dewittj
2016/12/08 19:09:02
Done.
| |
| 324 | |
| 325 if (static_cast<int>(cached_offline_page_downloads_.size()) > | |
| 326 GetMaxSuggestionsCount()) { | |
| 327 auto oldest_page_iterator = std::max_element( | |
| 328 cached_offline_page_downloads_.begin(), | |
| 329 cached_offline_page_downloads_.end(), | |
| 330 &CompareOfflinePagesForSorting); | |
| 331 cached_offline_page_downloads_.erase(oldest_page_iterator); | |
|
vitaliii
2016/12/08 17:40:32
Could you instead do
(*oldest_page_iterator) = ad
dewittj
2016/12/08 19:09:02
Done.
| |
| 332 } | |
| 333 | |
| 334 SubmitContentSuggestions(); | |
| 315 } | 335 } |
| 316 | 336 |
| 317 void DownloadSuggestionsProvider::OfflinePageDeleted( | 337 void DownloadSuggestionsProvider::OfflinePageDeleted( |
| 318 int64_t offline_id, | 338 int64_t offline_id, |
| 319 const offline_pages::ClientId& client_id) { | 339 const offline_pages::ClientId& client_id) { |
| 320 DCHECK(offline_page_model_); | 340 DCHECK(offline_page_model_); |
| 321 if (IsClientIdForOfflinePageDownload( | 341 if (IsClientIdForOfflinePageDownload( |
| 322 offline_page_model_->GetPolicyController(), client_id)) { | 342 offline_page_model_->GetPolicyController(), client_id)) { |
| 323 InvalidateSuggestion(GetOfflinePagePerCategoryID(offline_id)); | 343 InvalidateSuggestion(GetOfflinePagePerCategoryID(offline_id)); |
| 324 } | 344 } |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 651 const int max_suggestions_count = GetMaxSuggestionsCount(); | 671 const int max_suggestions_count = GetMaxSuggestionsCount(); |
| 652 if (static_cast<int>(items.size()) > max_suggestions_count) { | 672 if (static_cast<int>(items.size()) > max_suggestions_count) { |
| 653 // Partially sorts |items| such that: | 673 // Partially sorts |items| such that: |
| 654 // 1) The element at the index |max_suggestions_count| is changed to the | 674 // 1) The element at the index |max_suggestions_count| is changed to the |
| 655 // element which would occur on this position if |items| was sorted; | 675 // element which would occur on this position if |items| was sorted; |
| 656 // 2) All of the elements before index |max_suggestions_count| are less than | 676 // 2) All of the elements before index |max_suggestions_count| are less than |
| 657 // or equal to the elements after it. | 677 // or equal to the elements after it. |
| 658 std::nth_element( | 678 std::nth_element( |
| 659 items.begin(), items.begin() + max_suggestions_count, items.end(), | 679 items.begin(), items.begin() + max_suggestions_count, items.end(), |
| 660 [](const OfflinePageItem* left, const OfflinePageItem* right) { | 680 [](const OfflinePageItem* left, const OfflinePageItem* right) { |
| 661 return left->creation_time > right->creation_time; | 681 return CompareOfflinePagesForSorting(*left, *right); |
| 662 }); | 682 }); |
| 663 items.resize(max_suggestions_count); | 683 items.resize(max_suggestions_count); |
| 664 } | 684 } |
| 665 | 685 |
| 666 cached_offline_page_downloads_.clear(); | 686 cached_offline_page_downloads_.clear(); |
| 667 for (const OfflinePageItem* item : items) { | 687 for (const OfflinePageItem* item : items) { |
| 668 cached_offline_page_downloads_.push_back(*item); | 688 cached_offline_page_downloads_.push_back(*item); |
| 669 } | 689 } |
| 670 | 690 |
| 671 if (old_dismissed_ids.size() != retained_dismissed_ids.size()) { | 691 if (old_dismissed_ids.size() != retained_dismissed_ids.size()) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 747 void DownloadSuggestionsProvider::UnregisterDownloadItemObservers() { | 767 void DownloadSuggestionsProvider::UnregisterDownloadItemObservers() { |
| 748 DCHECK_NE(download_manager_, nullptr); | 768 DCHECK_NE(download_manager_, nullptr); |
| 749 | 769 |
| 750 std::vector<DownloadItem*> all_downloads; | 770 std::vector<DownloadItem*> all_downloads; |
| 751 download_manager_->GetAllDownloads(&all_downloads); | 771 download_manager_->GetAllDownloads(&all_downloads); |
| 752 | 772 |
| 753 for (DownloadItem* item : all_downloads) { | 773 for (DownloadItem* item : all_downloads) { |
| 754 item->RemoveObserver(this); | 774 item->RemoveObserver(this); |
| 755 } | 775 } |
| 756 } | 776 } |
| OLD | NEW |