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

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: GetTimeUntilReadyForUpload should not return negative values 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::IsEmpty() {
83 SuggestionsBlacklist blacklist;
84 LoadBlacklist(&blacklist);
85 return blacklist.urls_size() == 0;
86 }
87
88 bool BlacklistStore::GetTimeUntilReadyForUpload(TimeDelta* delta) {
89 SuggestionsBlacklist blacklist;
90 LoadBlacklist(&blacklist);
91 if (blacklist.urls_size() == 0)
Mathieu 2014/12/04 18:53:32 !blacklist.urls_size() ?
manzagop (departed) 2014/12/05 15:13:21 Done.
92 return false;
93
94 // Note: the size is non-negative.
95 if (blacklist_times_.size() < static_cast<size_t>(blacklist.urls_size())) {
96 // A url is not in the timestamp map: it's candidate for upload.
Mathieu 2014/12/04 18:53:32 Indicate that this may happen if the application w
manzagop (departed) 2014/12/05 15:13:21 Done. Good point.
97 *delta = TimeDelta::FromSeconds(0);
64 return true; 98 return true;
65 } 99 }
66 100
67 PopulateBlacklistProto(blacklist_set, &blacklist_proto); 101 // Find the minimum blacklist time. Note: blacklist_times_ is NOT empty since
Mathieu 2014/12/04 18:53:32 CHECK(blacklist_times_ is not empty) ?
manzagop (departed) 2014/12/05 15:13:22 Done.
68 return StoreBlacklist(blacklist_proto); 102 // blacklist is non-empty and blacklist_times_ contains as many items.
103 base::TimeTicks min_time = blacklist_times_.begin()->second;
104 for (const auto& kv : blacklist_times_) {
105 if (kv.second < min_time)
106 min_time = kv.second;
107 }
108 *delta = std::max(upload_delay_ - (TimeTicks::Now() - min_time),
109 TimeDelta::FromSeconds(0));
110
111 return true;
69 } 112 }
70 113
71 bool BlacklistStore::GetFirstUrlFromBlacklist(GURL* url) { 114 bool BlacklistStore::GetTimeUntilReadyForUpload(const GURL& url,
115 TimeDelta* delta) {
116 auto it = blacklist_times_.find(url.spec());
117 if (it != blacklist_times_.end()) {
118 // The url is in the timestamps map.
119 *delta = std::max(upload_delay_ - (TimeTicks::Now() - it->second),
120 TimeDelta::FromSeconds(0));
121 return true;
122 }
123
124 // The url still might be in the blacklist.
72 SuggestionsBlacklist blacklist; 125 SuggestionsBlacklist blacklist;
73 LoadBlacklist(&blacklist); 126 LoadBlacklist(&blacklist);
74 if (!blacklist.urls_size()) return false; 127 for (int i = 0; i < blacklist.urls_size(); ++i) {
75 GURL blacklisted(blacklist.urls(0)); 128 if (blacklist.urls(i) == url.spec()) {
76 url->Swap(&blacklisted); 129 *delta = TimeDelta::FromSeconds(0);
77 return true; 130 return true;
131 }
132 }
133
134 return false;
135 }
136
137 bool BlacklistStore::GetCandidateForUpload(GURL* url) {
138 SuggestionsBlacklist blacklist;
139 LoadBlacklist(&blacklist);
140
141 for (int i = 0; i < blacklist.urls_size(); ++i) {
142 bool is_candidate = true;
143 auto it = blacklist_times_.find(blacklist.urls(i));
144 if (it != blacklist_times_.end() &&
145 TimeTicks::Now() < it->second + upload_delay_) {
146 // URL was added too recently.
147 is_candidate = false;
148 }
149 if (is_candidate) {
150 GURL blacklisted(blacklist.urls(i));
151 url->Swap(&blacklisted);
152 return true;
153 }
154 }
155
156 return false;
78 } 157 }
79 158
80 bool BlacklistStore::RemoveUrl(const GURL& url) { 159 bool BlacklistStore::RemoveUrl(const GURL& url) {
81 if (!url.is_valid()) return false; 160 if (!url.is_valid()) return false;
82 const std::string removal_candidate = url.spec(); 161 const std::string removal_candidate = url.spec();
83 162
84 SuggestionsBlacklist blacklist; 163 SuggestionsBlacklist blacklist;
85 LoadBlacklist(&blacklist); 164 LoadBlacklist(&blacklist);
86 165
166 bool removed = false;
87 SuggestionsBlacklist updated_blacklist; 167 SuggestionsBlacklist updated_blacklist;
88 for (int i = 0; i < blacklist.urls_size(); ++i) { 168 for (int i = 0; i < blacklist.urls_size(); ++i) {
89 if (blacklist.urls(i) != removal_candidate) 169 if (blacklist.urls(i) == removal_candidate) {
170 removed = true;
171 } else {
90 updated_blacklist.add_urls(blacklist.urls(i)); 172 updated_blacklist.add_urls(blacklist.urls(i));
173 }
91 } 174 }
92 175
93 return StoreBlacklist(updated_blacklist); 176 if (removed && StoreBlacklist(updated_blacklist)) {
177 blacklist_times_.erase(url.spec());
178 return true;
179 }
180
181 return false;
94 } 182 }
95 183
96 void BlacklistStore::FilterSuggestions(SuggestionsProfile* profile) { 184 void BlacklistStore::FilterSuggestions(SuggestionsProfile* profile) {
97 if (!profile->suggestions_size()) 185 if (!profile->suggestions_size())
98 return; // Empty profile, nothing to filter. 186 return; // Empty profile, nothing to filter.
99 187
100 SuggestionsBlacklist blacklist_proto; 188 SuggestionsBlacklist blacklist_proto;
101 if (!LoadBlacklist(&blacklist_proto)) { 189 if (!LoadBlacklist(&blacklist_proto)) {
102 // There was an error loading the blacklist. The blacklist was cleared and 190 // There was an error loading the blacklist. The blacklist was cleared and
103 // there's nothing to be done about it. 191 // there's nothing to be done about it.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 254
167 pref_service_->SetString(prefs::kSuggestionsBlacklist, base64_blacklist_data); 255 pref_service_->SetString(prefs::kSuggestionsBlacklist, base64_blacklist_data);
168 return true; 256 return true;
169 } 257 }
170 258
171 void BlacklistStore::ClearBlacklist() { 259 void BlacklistStore::ClearBlacklist() {
172 pref_service_->ClearPref(prefs::kSuggestionsBlacklist); 260 pref_service_->ClearPref(prefs::kSuggestionsBlacklist);
173 } 261 }
174 262
175 } // namespace suggestions 263 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698