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

Side by Side Diff: components/suggestions/blacklist_store.cc

Issue 766053010: SuggestionsService undo blacklist functionality. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Mathieu's comments. Created 6 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/suggestions/blacklist_store.h" 5 #include "components/suggestions/blacklist_store.h"
6 6
7 #include <algorithm>
7 #include <set> 8 #include <set>
8 #include <string> 9 #include <string>
9 10
10 #include "base/base64.h" 11 #include "base/base64.h"
11 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
12 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
13 #include "components/pref_registry/pref_registry_syncable.h" 14 #include "components/pref_registry/pref_registry_syncable.h"
14 #include "components/suggestions/suggestions_pref_names.h" 15 #include "components/suggestions/suggestions_pref_names.h"
15 16
17 using base::TimeDelta;
18 using base::TimeTicks;
19
16 namespace suggestions { 20 namespace suggestions {
17
18 namespace { 21 namespace {
19 22
20 void PopulateBlacklistSet(const SuggestionsBlacklist& blacklist_proto, 23 void PopulateBlacklistSet(const SuggestionsBlacklist& blacklist_proto,
21 std::set<std::string>* blacklist_set) { 24 std::set<std::string>* blacklist_set) {
22 blacklist_set->clear(); 25 blacklist_set->clear();
23 for (int i = 0; i < blacklist_proto.urls_size(); ++i) { 26 for (int i = 0; i < blacklist_proto.urls_size(); ++i) {
24 blacklist_set->insert(blacklist_proto.urls(i)); 27 blacklist_set->insert(blacklist_proto.urls(i));
25 } 28 }
26 } 29 }
27 30
28 void PopulateBlacklistProto(const std::set<std::string>& blacklist_set, 31 void PopulateBlacklistProto(const std::set<std::string>& blacklist_set,
29 SuggestionsBlacklist* blacklist_proto) { 32 SuggestionsBlacklist* blacklist_proto) {
30 blacklist_proto->Clear(); 33 blacklist_proto->Clear();
31 for (std::set<std::string>::const_iterator it = blacklist_set.begin(); 34 for (std::set<std::string>::const_iterator it = blacklist_set.begin();
32 it != blacklist_set.end(); ++it) { 35 it != blacklist_set.end(); ++it) {
33 blacklist_proto->add_urls(*it); 36 blacklist_proto->add_urls(*it);
34 } 37 }
35 } 38 }
36 39
37 } // namespace 40 } // namespace
38 41
39 BlacklistStore::BlacklistStore(PrefService* profile_prefs) 42 BlacklistStore::BlacklistStore(PrefService* profile_prefs,
40 : pref_service_(profile_prefs) { 43 const base::TimeDelta& upload_delay)
44 : pref_service_(profile_prefs), upload_delay_(upload_delay) {
41 DCHECK(pref_service_); 45 DCHECK(pref_service_);
42 46
43 // Log the blacklist's size. A single BlacklistStore is created for the 47 // Log the blacklist's size. A single BlacklistStore is created for the
44 // SuggestionsService; this will run once. 48 // SuggestionsService; this will run once.
45 SuggestionsBlacklist blacklist_proto; 49 SuggestionsBlacklist blacklist_proto;
46 LoadBlacklist(&blacklist_proto); 50 LoadBlacklist(&blacklist_proto);
47 UMA_HISTOGRAM_COUNTS_10000("Suggestions.LocalBlacklistSize", 51 UMA_HISTOGRAM_COUNTS_10000("Suggestions.LocalBlacklistSize",
48 blacklist_proto.urls_size()); 52 blacklist_proto.urls_size());
49 } 53 }
50 54
51 BlacklistStore::~BlacklistStore() {} 55 BlacklistStore::~BlacklistStore() {}
52 56
53 bool BlacklistStore::BlacklistUrl(const GURL& url) { 57 bool BlacklistStore::BlacklistUrl(const GURL& url) {
54 if (!url.is_valid()) return false; 58 if (!url.is_valid()) return false;
55 59
56 SuggestionsBlacklist blacklist_proto; 60 SuggestionsBlacklist blacklist_proto;
57 LoadBlacklist(&blacklist_proto); 61 LoadBlacklist(&blacklist_proto);
58
59 std::set<std::string> blacklist_set; 62 std::set<std::string> blacklist_set;
60 PopulateBlacklistSet(blacklist_proto, &blacklist_set); 63 PopulateBlacklistSet(blacklist_proto, &blacklist_set);
61 64
62 if (!blacklist_set.insert(url.spec()).second) { 65 bool success = false;
66 if (blacklist_set.insert(url.spec()).second) {
67 PopulateBlacklistProto(blacklist_set, &blacklist_proto);
68 success = StoreBlacklist(blacklist_proto);
69 } else {
63 // |url| was already in the blacklist. 70 // |url| was already in the blacklist.
71 success = true;
72 }
73
74 if (success) {
75 // Update the blacklist time.
76 blacklist_times_[url.spec()] = TimeTicks::Now();
77 }
78
79 return success;
80 }
81
82 bool BlacklistStore::GetTimeUntilReadyForUpload(TimeDelta* delta) {
83 SuggestionsBlacklist blacklist;
84 LoadBlacklist(&blacklist);
85 if (!blacklist.urls_size())
86 return false;
87
88 // Note: the size is non-negative.
89 if (blacklist_times_.size() < static_cast<size_t>(blacklist.urls_size())) {
90 // A url is not in the timestamp map: it's candidate for upload. This can
91 // happen after a restart. Another (undesired) case when this could happen
92 // is if more than one instance were created.
93 *delta = TimeDelta::FromSeconds(0);
64 return true; 94 return true;
65 } 95 }
66 96
67 PopulateBlacklistProto(blacklist_set, &blacklist_proto); 97 // Find the minimum blacklist time. Note: blacklist_times_ is NOT empty since
68 return StoreBlacklist(blacklist_proto); 98 // blacklist is non-empty and blacklist_times_ contains as many items.
99 CHECK(blacklist_times_.size());
Mathieu 2014/12/05 16:32:05 Let's remove this and DCHECK below instead.
manzagop (departed) 2014/12/05 20:09:19 Done.
100 base::TimeTicks min_time = blacklist_times_.begin()->second;
Mathieu 2014/12/05 16:32:05 initial value should be a big number
manzagop (departed) 2014/12/05 20:09:19 Done. Operating on TimeDelta instead of TimeTicks
101 for (const auto& kv : blacklist_times_) {
102 if (kv.second < min_time)
103 min_time = kv.second;
104 }
105 *delta = std::max(upload_delay_ - (TimeTicks::Now() - min_time),
106 TimeDelta::FromSeconds(0));
107
Mathieu 2014/12/05 16:32:05 DCHECK that it's not the big number
manzagop (departed) 2014/12/05 20:09:19 Done.
108 return true;
69 } 109 }
70 110
71 bool BlacklistStore::GetFirstUrlFromBlacklist(GURL* url) { 111 bool BlacklistStore::GetTimeUntilURLReadyForUpload(const GURL& url,
112 TimeDelta* delta) {
113 auto it = blacklist_times_.find(url.spec());
114 if (it != blacklist_times_.end()) {
115 // The url is in the timestamps map.
116 *delta = std::max(upload_delay_ - (TimeTicks::Now() - it->second),
117 TimeDelta::FromSeconds(0));
118 return true;
119 }
120
121 // The url still might be in the blacklist.
72 SuggestionsBlacklist blacklist; 122 SuggestionsBlacklist blacklist;
73 LoadBlacklist(&blacklist); 123 LoadBlacklist(&blacklist);
74 if (!blacklist.urls_size()) return false; 124 for (int i = 0; i < blacklist.urls_size(); ++i) {
75 GURL blacklisted(blacklist.urls(0)); 125 if (blacklist.urls(i) == url.spec()) {
76 url->Swap(&blacklisted); 126 *delta = TimeDelta::FromSeconds(0);
77 return true; 127 return true;
128 }
129 }
130
131 return false;
132 }
133
134 bool BlacklistStore::GetCandidateForUpload(GURL* url) {
135 SuggestionsBlacklist blacklist;
136 LoadBlacklist(&blacklist);
137
138 for (int i = 0; i < blacklist.urls_size(); ++i) {
139 bool is_candidate = true;
140 auto it = blacklist_times_.find(blacklist.urls(i));
141 if (it != blacklist_times_.end() &&
142 TimeTicks::Now() < it->second + upload_delay_) {
143 // URL was added too recently.
144 is_candidate = false;
145 }
146 if (is_candidate) {
147 GURL blacklisted(blacklist.urls(i));
148 url->Swap(&blacklisted);
149 return true;
150 }
151 }
152
153 return false;
78 } 154 }
79 155
80 bool BlacklistStore::RemoveUrl(const GURL& url) { 156 bool BlacklistStore::RemoveUrl(const GURL& url) {
81 if (!url.is_valid()) return false; 157 if (!url.is_valid()) return false;
82 const std::string removal_candidate = url.spec(); 158 const std::string removal_candidate = url.spec();
83 159
84 SuggestionsBlacklist blacklist; 160 SuggestionsBlacklist blacklist;
85 LoadBlacklist(&blacklist); 161 LoadBlacklist(&blacklist);
86 162
163 bool removed = false;
87 SuggestionsBlacklist updated_blacklist; 164 SuggestionsBlacklist updated_blacklist;
88 for (int i = 0; i < blacklist.urls_size(); ++i) { 165 for (int i = 0; i < blacklist.urls_size(); ++i) {
89 if (blacklist.urls(i) != removal_candidate) 166 if (blacklist.urls(i) == removal_candidate) {
167 removed = true;
168 } else {
90 updated_blacklist.add_urls(blacklist.urls(i)); 169 updated_blacklist.add_urls(blacklist.urls(i));
170 }
91 } 171 }
92 172
93 return StoreBlacklist(updated_blacklist); 173 if (removed && StoreBlacklist(updated_blacklist)) {
174 blacklist_times_.erase(url.spec());
175 return true;
176 }
177
178 return false;
94 } 179 }
95 180
96 void BlacklistStore::FilterSuggestions(SuggestionsProfile* profile) { 181 void BlacklistStore::FilterSuggestions(SuggestionsProfile* profile) {
97 if (!profile->suggestions_size()) 182 if (!profile->suggestions_size())
98 return; // Empty profile, nothing to filter. 183 return; // Empty profile, nothing to filter.
99 184
100 SuggestionsBlacklist blacklist_proto; 185 SuggestionsBlacklist blacklist_proto;
101 if (!LoadBlacklist(&blacklist_proto)) { 186 if (!LoadBlacklist(&blacklist_proto)) {
102 // There was an error loading the blacklist. The blacklist was cleared and 187 // There was an error loading the blacklist. The blacklist was cleared and
103 // there's nothing to be done about it. 188 // there's nothing to be done about it.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 251
167 pref_service_->SetString(prefs::kSuggestionsBlacklist, base64_blacklist_data); 252 pref_service_->SetString(prefs::kSuggestionsBlacklist, base64_blacklist_data);
168 return true; 253 return true;
169 } 254 }
170 255
171 void BlacklistStore::ClearBlacklist() { 256 void BlacklistStore::ClearBlacklist() {
172 pref_service_->ClearPref(prefs::kSuggestionsBlacklist); 257 pref_service_->ClearPref(prefs::kSuggestionsBlacklist);
173 } 258 }
174 259
175 } // namespace suggestions 260 } // namespace suggestions
OLDNEW
« no previous file with comments | « components/suggestions/blacklist_store.h ('k') | components/suggestions/blacklist_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698