| 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 "components/ntp_snippets/physical_web_pages/physical_web_page_suggestio
ns_provider.h" | 5 #include "components/ntp_snippets/physical_web_pages/physical_web_page_suggestio
ns_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 // When there is no estimate, the value is <= 0, so we implicitly treat it as | 43 // When there is no estimate, the value is <= 0, so we implicitly treat it as |
| 44 // infinity. | 44 // infinity. |
| 45 bool is_left_estimated = left.distance_estimate > 0; | 45 bool is_left_estimated = left.distance_estimate > 0; |
| 46 bool is_right_estimated = right.distance_estimate > 0; | 46 bool is_right_estimated = right.distance_estimate > 0; |
| 47 | 47 |
| 48 if (is_left_estimated != is_right_estimated) | 48 if (is_left_estimated != is_right_estimated) |
| 49 return is_left_estimated; | 49 return is_left_estimated; |
| 50 return left.distance_estimate < right.distance_estimate; | 50 return left.distance_estimate < right.distance_estimate; |
| 51 } | 51 } |
| 52 | 52 |
| 53 void FilterOutByGroupId( | 53 void FilterOutByGroupId(physical_web::MetadataList& page_metadata_list) { |
| 54 physical_web::MetadataList& page_metadata_list) { | |
| 55 // |std::unique| only removes duplicates that immediately follow each other. | 54 // |std::unique| only removes duplicates that immediately follow each other. |
| 56 // Thus, first, we have to sort by group_id and distance and only then remove | 55 // Thus, first, we have to sort by group_id and distance and only then remove |
| 57 // duplicates. | 56 // duplicates. |
| 58 std::sort(page_metadata_list.begin(), page_metadata_list.end(), | 57 std::sort(page_metadata_list.begin(), page_metadata_list.end(), |
| 59 [](const physical_web::Metadata& left, | 58 [](const physical_web::Metadata& left, |
| 60 const physical_web::Metadata& right) { | 59 const physical_web::Metadata& right) { |
| 61 if (left.group_id != right.group_id) { | 60 if (left.group_id != right.group_id) { |
| 62 return left.group_id < right.group_id; | 61 return left.group_id < right.group_id; |
| 63 } | 62 } |
| 64 | 63 |
| 65 // We want closest pages first, so in case of same group_id we | 64 // We want closest pages first, so in case of same group_id we |
| 66 // sort by distance. | 65 // sort by distance. |
| 67 return CompareByDistance(left, right); | 66 return CompareByDistance(left, right); |
| 68 }); | 67 }); |
| 69 | 68 |
| 70 // Each empty group_id must be treated as unique, so we do not apply | 69 // Each empty group_id must be treated as unique, so we do not apply |
| 71 // std::unique to them at all. | 70 // std::unique to them at all. |
| 72 auto nonempty_group_id_begin = std::find_if( | 71 auto nonempty_group_id_begin = |
| 73 page_metadata_list.begin(), page_metadata_list.end(), | 72 std::find_if(page_metadata_list.begin(), page_metadata_list.end(), |
| 74 [](const physical_web::Metadata& page) { | 73 [](const physical_web::Metadata& page) { |
| 75 return !page.group_id.empty(); | 74 return !page.group_id.empty(); |
| 76 }); | 75 }); |
| 77 | 76 |
| 78 auto new_end = std::unique( | 77 auto new_end = std::unique(nonempty_group_id_begin, page_metadata_list.end(), |
| 79 nonempty_group_id_begin, page_metadata_list.end(), | 78 [](const physical_web::Metadata& left, |
| 80 [](const physical_web::Metadata& left, | 79 const physical_web::Metadata& right) { |
| 81 const physical_web::Metadata& right) { | 80 return left.group_id == right.group_id; |
| 82 return left.group_id == right.group_id; | 81 }); |
| 83 }); | |
| 84 | 82 |
| 85 page_metadata_list.erase(new_end, page_metadata_list.end()); | 83 page_metadata_list.erase(new_end, page_metadata_list.end()); |
| 86 } | 84 } |
| 87 | 85 |
| 88 } // namespace | 86 } // namespace |
| 89 | 87 |
| 90 PhysicalWebPageSuggestionsProvider::PhysicalWebPageSuggestionsProvider( | 88 PhysicalWebPageSuggestionsProvider::PhysicalWebPageSuggestionsProvider( |
| 91 ContentSuggestionsProvider::Observer* observer, | 89 ContentSuggestionsProvider::Observer* observer, |
| 92 physical_web::PhysicalWebDataSource* physical_web_data_source, | 90 physical_web::PhysicalWebDataSource* physical_web_data_source, |
| 93 PrefService* pref_service) | 91 PrefService* pref_service) |
| 94 : ContentSuggestionsProvider(observer), | 92 : ContentSuggestionsProvider(observer), |
| 95 category_status_(CategoryStatus::AVAILABLE), | 93 category_status_(CategoryStatus::AVAILABLE), |
| 96 provided_category_( | 94 provided_category_( |
| 97 Category::FromKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES)), | 95 Category::FromKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES)), |
| 98 physical_web_data_source_(physical_web_data_source), | 96 physical_web_data_source_(physical_web_data_source), |
| 99 pref_service_(pref_service) { | 97 pref_service_(pref_service) { |
| 100 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); | 98 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); |
| 101 physical_web_data_source_->RegisterListener(this, | 99 physical_web_data_source_->RegisterListener( |
| 102 physical_web::BACKGROUND_INTERMITTENT); | 100 this, physical_web::BACKGROUND_INTERMITTENT); |
| 103 // TODO(vitaliii): Rewrite initial fetch once crbug.com/667754 is resolved. | 101 // TODO(vitaliii): Rewrite initial fetch once crbug.com/667754 is resolved. |
| 104 FetchPhysicalWebPages(); | 102 FetchPhysicalWebPages(); |
| 105 } | 103 } |
| 106 | 104 |
| 107 PhysicalWebPageSuggestionsProvider::~PhysicalWebPageSuggestionsProvider() { | 105 PhysicalWebPageSuggestionsProvider::~PhysicalWebPageSuggestionsProvider() { |
| 108 physical_web_data_source_->UnregisterListener(this); | 106 physical_web_data_source_->UnregisterListener(this); |
| 109 } | 107 } |
| 110 | 108 |
| 111 CategoryStatus PhysicalWebPageSuggestionsProvider::GetCategoryStatus( | 109 CategoryStatus PhysicalWebPageSuggestionsProvider::GetCategoryStatus( |
| 112 Category category) { | 110 Category category) { |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 } | 357 } |
| 360 | 358 |
| 361 void PhysicalWebPageSuggestionsProvider::StoreDismissedIDsToPrefs( | 359 void PhysicalWebPageSuggestionsProvider::StoreDismissedIDsToPrefs( |
| 362 const std::set<std::string>& dismissed_ids) { | 360 const std::set<std::string>& dismissed_ids) { |
| 363 prefs::StoreDismissedIDsToPrefs(pref_service_, | 361 prefs::StoreDismissedIDsToPrefs(pref_service_, |
| 364 prefs::kDismissedPhysicalWebPageSuggestions, | 362 prefs::kDismissedPhysicalWebPageSuggestions, |
| 365 dismissed_ids); | 363 dismissed_ids); |
| 366 } | 364 } |
| 367 | 365 |
| 368 } // namespace ntp_snippets | 366 } // namespace ntp_snippets |
| OLD | NEW |