Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc

Issue 2640773002: [NTP::PhysicalWeb] Filter our pages with same group_id. (Closed)
Patch Set: treib@ comments. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 19 matching lines...) Expand all
30 using base::DictionaryValue; 30 using base::DictionaryValue;
31 using base::ListValue; 31 using base::ListValue;
32 using base::Value; 32 using base::Value;
33 33
34 namespace ntp_snippets { 34 namespace ntp_snippets {
35 35
36 namespace { 36 namespace {
37 37
38 const size_t kMaxSuggestionsCount = 10; 38 const size_t kMaxSuggestionsCount = 10;
39 39
40 std::string GetGroupId(const DictionaryValue& page_dictionary) {
41 std::string group_id;
42 if (!page_dictionary.GetString(physical_web::kGroupIdKey, &group_id)) {
43 LOG(DFATAL) << physical_web::kGroupIdKey << " field is missing.";
44 }
45 return group_id;
46 }
47
40 std::string GetPageId(const DictionaryValue& page_dictionary) { 48 std::string GetPageId(const DictionaryValue& page_dictionary) {
41 std::string raw_resolved_url; 49 std::string raw_resolved_url;
42 if (!page_dictionary.GetString(physical_web::kResolvedUrlKey, 50 if (!page_dictionary.GetString(physical_web::kResolvedUrlKey,
43 &raw_resolved_url)) { 51 &raw_resolved_url)) {
44 LOG(DFATAL) << physical_web::kResolvedUrlKey << " field is missing."; 52 LOG(DFATAL) << physical_web::kResolvedUrlKey << " field is missing.";
45 } 53 }
46 return raw_resolved_url; 54 return raw_resolved_url;
47 } 55 }
48 56
57 bool CompareByDistance(const DictionaryValue* left,
58 const DictionaryValue* right) {
59 double left_distance, right_distance;
60 bool success =
61 left->GetDouble(physical_web::kDistanceEstimateKey, &left_distance);
62 success =
63 right->GetDouble(physical_web::kDistanceEstimateKey, &right_distance) &&
64 success;
65 if (!success) {
66 LOG(DFATAL) << "Distance field is missing.";
67 }
68
69 // When there is no estimate, the value is <= 0, so we implicilty treat it as
Marc Treib 2017/01/18 11:54:48 s/implicilty/implicitly/
vitaliii 2017/01/18 14:02:27 Done.
70 // infinity.
71 bool is_left_estimated = left_distance > 0;
72 bool is_right_estimated = right_distance > 0;
73
74 if (is_left_estimated != is_right_estimated)
75 return is_left_estimated;
76 return left_distance < right_distance;
77 }
78
79 void FilterOutByGroupId(
80 std::vector<const DictionaryValue*>* page_dictionaries) {
81 // |std::unique| only removes duplicates that immediately follow each other.
82 // Thus, first, we have to sort by group_id and distance and only then remove
83 // duplicates.
84 std::sort(page_dictionaries->begin(), page_dictionaries->end(),
85 [](const DictionaryValue* left, const DictionaryValue* right) {
86 std::string left_group_id = GetGroupId(*left),
87 right_group_id = GetGroupId(*right);
Marc Treib 2017/01/18 11:54:48 nitty nit: I'd just make two declarations
vitaliii 2017/01/18 14:02:27 Done.
88
89 if (left_group_id != right_group_id) {
90 return left_group_id < right_group_id;
91 }
92
93 // We want closest pages first, so in case of same group_id we
94 // sort by distance.
95 return CompareByDistance(left, right);
96 });
97
98 // Each empty group_id must be treated as unique, so we do not apply
99 // std::unique to them at all.
100 auto nonempty_group_id_begin = std::find_if(
101 page_dictionaries->begin(), page_dictionaries->end(),
102 [](const DictionaryValue* page) { return !GetGroupId(*page).empty(); });
103
104 auto new_end = std::unique(
105 nonempty_group_id_begin, page_dictionaries->end(),
106 [](const DictionaryValue* left, const DictionaryValue* right) {
107 return GetGroupId(*left) == GetGroupId(*right);
108 });
109
110 page_dictionaries->erase(new_end, page_dictionaries->end());
111 }
112
49 } // namespace 113 } // namespace
50 114
51 PhysicalWebPageSuggestionsProvider::PhysicalWebPageSuggestionsProvider( 115 PhysicalWebPageSuggestionsProvider::PhysicalWebPageSuggestionsProvider(
52 ContentSuggestionsProvider::Observer* observer, 116 ContentSuggestionsProvider::Observer* observer,
53 physical_web::PhysicalWebDataSource* physical_web_data_source, 117 physical_web::PhysicalWebDataSource* physical_web_data_source,
54 PrefService* pref_service) 118 PrefService* pref_service)
55 : ContentSuggestionsProvider(observer), 119 : ContentSuggestionsProvider(observer),
56 category_status_(CategoryStatus::AVAILABLE), 120 category_status_(CategoryStatus::AVAILABLE),
57 provided_category_( 121 provided_category_(
58 Category::FromKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES)), 122 Category::FromKnownCategory(KnownCategories::PHYSICAL_WEB_PAGES)),
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 279
216 if (old_dismissed_ids.count(page_id)) { 280 if (old_dismissed_ids.count(page_id)) {
217 new_dismissed_ids.insert(page_id); 281 new_dismissed_ids.insert(page_id);
218 } 282 }
219 } 283 }
220 284
221 if (old_dismissed_ids.size() != new_dismissed_ids.size()) { 285 if (old_dismissed_ids.size() != new_dismissed_ids.size()) {
222 StoreDismissedIDsToPrefs(new_dismissed_ids); 286 StoreDismissedIDsToPrefs(new_dismissed_ids);
223 } 287 }
224 288
289 FilterOutByGroupId(&page_dictionaries);
290
225 std::sort(page_dictionaries.begin(), page_dictionaries.end(), 291 std::sort(page_dictionaries.begin(), page_dictionaries.end(),
226 [](const DictionaryValue* left, const DictionaryValue* right) { 292 CompareByDistance);
227 double left_distance, right_distance;
228 bool success = left->GetDouble(physical_web::kDistanceEstimateKey,
229 &left_distance);
230 success = right->GetDouble(physical_web::kDistanceEstimateKey,
231 &right_distance) &&
232 success;
233 if (!success) {
234 LOG(DFATAL) << "Distance field is missing.";
235 }
236 return left_distance < right_distance;
237 });
238 293
239 std::vector<ContentSuggestion> suggestions; 294 std::vector<ContentSuggestion> suggestions;
240 for (const DictionaryValue* page_dictionary : page_dictionaries) { 295 for (const DictionaryValue* page_dictionary : page_dictionaries) {
241 if (static_cast<int>(suggestions.size()) == max_quantity) { 296 if (static_cast<int>(suggestions.size()) == max_quantity) {
242 break; 297 break;
243 } 298 }
244 suggestions.push_back(ConvertPhysicalWebPage(*page_dictionary)); 299 suggestions.push_back(ConvertPhysicalWebPage(*page_dictionary));
245 } 300 }
246 301
247 return suggestions; 302 return suggestions;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 } 362 }
308 363
309 void PhysicalWebPageSuggestionsProvider::StoreDismissedIDsToPrefs( 364 void PhysicalWebPageSuggestionsProvider::StoreDismissedIDsToPrefs(
310 const std::set<std::string>& dismissed_ids) { 365 const std::set<std::string>& dismissed_ids) {
311 prefs::StoreDismissedIDsToPrefs(pref_service_, 366 prefs::StoreDismissedIDsToPrefs(pref_service_,
312 prefs::kDismissedPhysicalWebPageSuggestions, 367 prefs::kDismissedPhysicalWebPageSuggestions,
313 dismissed_ids); 368 dismissed_ids);
314 } 369 }
315 370
316 } // namespace ntp_snippets 371 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « no previous file | components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698