OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/search/suggestions/blacklist_store.h" | |
6 | |
7 #include <set> | |
8 #include <string> | |
9 | |
10 #include "base/base64.h" | |
11 #include "base/prefs/pref_service.h" | |
12 #include "chrome/common/pref_names.h" | |
13 #include "components/pref_registry/pref_registry_syncable.h" | |
14 | |
15 namespace suggestions { | |
16 | |
17 namespace { | |
18 | |
19 void PopulateBlacklistSet(const SuggestionsBlacklist& blacklist_proto, | |
20 std::set<std::string>* blacklist_set) { | |
21 blacklist_set->clear(); | |
22 for (int i = 0; i < blacklist_proto.urls_size(); ++i) { | |
23 blacklist_set->insert(blacklist_proto.urls(i)); | |
24 } | |
25 } | |
26 | |
27 void PopulateBlacklistProto(const std::set<std::string>& blacklist_set, | |
28 SuggestionsBlacklist* blacklist_proto) { | |
29 blacklist_proto->Clear(); | |
30 for (std::set<std::string>::const_iterator it = blacklist_set.begin(); | |
31 it != blacklist_set.end(); ++it) { | |
32 blacklist_proto->add_urls(*it); | |
33 } | |
34 } | |
35 | |
36 } // namespace | |
37 | |
38 BlacklistStore::BlacklistStore(PrefService* profile_prefs) | |
39 : pref_service_(profile_prefs) { | |
40 DCHECK(pref_service_); | |
41 } | |
42 | |
43 BlacklistStore::~BlacklistStore() {} | |
44 | |
45 bool BlacklistStore::BlacklistUrl(const GURL& url) { | |
46 if (!url.is_valid()) return false; | |
47 | |
48 SuggestionsBlacklist blacklist_proto; | |
49 LoadBlacklist(&blacklist_proto); | |
50 | |
51 std::set<std::string> blacklist_set; | |
52 PopulateBlacklistSet(blacklist_proto, &blacklist_set); | |
53 | |
54 if (!blacklist_set.insert(url.spec()).second) { | |
55 // |url| was already in the blacklist. | |
56 return true; | |
57 } | |
58 | |
59 PopulateBlacklistProto(blacklist_set, &blacklist_proto); | |
60 return StoreBlacklist(blacklist_proto); | |
61 } | |
62 | |
63 bool BlacklistStore::GetSomeBlacklistedUrl(GURL* url) { | |
Mathieu
2014/06/17 14:51:14
Ah so this gets the first entry from the blacklist
manzagop (departed)
2014/06/17 15:42:23
Done.
| |
64 SuggestionsBlacklist blacklist; | |
65 LoadBlacklist(&blacklist); | |
66 if (!blacklist.urls_size()) return false; | |
67 GURL blacklisted(blacklist.urls(0)); | |
68 url->Swap(&blacklisted); | |
69 return true; | |
70 } | |
71 | |
72 bool BlacklistStore::RemoveUrl(const GURL& url) { | |
73 if (!url.is_valid()) return false; | |
74 const std::string removal_candidate = url.spec(); | |
75 | |
76 SuggestionsBlacklist blacklist; | |
77 LoadBlacklist(&blacklist); | |
78 | |
79 SuggestionsBlacklist updated_blacklist; | |
80 for (int i = 0; i < blacklist.urls_size(); ++i) { | |
81 if (blacklist.urls(i) != removal_candidate) | |
82 updated_blacklist.add_urls(blacklist.urls(i)); | |
83 } | |
84 | |
85 return StoreBlacklist(updated_blacklist); | |
86 } | |
87 | |
88 void BlacklistStore::FilterSuggestions(SuggestionsProfile* profile) { | |
89 if (!profile->suggestions_size()) | |
90 return; // Empty profile, nothing to filter. | |
91 | |
92 SuggestionsBlacklist blacklist_proto; | |
93 if (!LoadBlacklist(&blacklist_proto)) { | |
94 // There was an error loading the blacklist. The blacklist was cleared and | |
95 // there's nothing to be done about it. | |
96 return; | |
97 } | |
98 if (!blacklist_proto.urls_size()) | |
99 return; // Empty blacklist, nothing to filter. | |
100 | |
101 std::set<std::string> blacklist_set; | |
102 PopulateBlacklistSet(blacklist_proto, &blacklist_set); | |
103 | |
104 // Populate the filtered suggestions. | |
105 SuggestionsProfile filtered_profile; | |
106 for (int i = 0; i < profile->suggestions_size(); ++i) { | |
107 if (blacklist_set.find(profile->suggestions(i).url()) == | |
108 blacklist_set.end()) { | |
109 // This suggestion is not blacklisted. | |
110 ChromeSuggestion* suggestion = filtered_profile.add_suggestions(); | |
111 // Note: swapping! | |
112 suggestion->Swap(profile->mutable_suggestions(i)); | |
113 } | |
114 } | |
115 | |
116 // Swap |profile| and |filtered_profile|. | |
117 profile->Swap(&filtered_profile); | |
118 } | |
119 | |
120 // static | |
121 void BlacklistStore::RegisterProfilePrefs( | |
122 user_prefs::PrefRegistrySyncable* registry) { | |
123 registry->RegisterStringPref( | |
124 prefs::kSuggestionsBlacklist, std::string(), | |
125 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
126 } | |
127 | |
128 bool BlacklistStore::LoadBlacklist(SuggestionsBlacklist* blacklist) { | |
129 DCHECK(blacklist); | |
130 | |
131 const std::string base64_blacklist_data = | |
132 pref_service_->GetString(prefs::kSuggestionsBlacklist); | |
133 if (base64_blacklist_data.empty()) { | |
134 blacklist->Clear(); | |
135 return false; | |
136 } | |
137 | |
138 // If the decode process fails, assume the pref value is corrupt and clear it. | |
139 std::string blacklist_data; | |
140 if (!base::Base64Decode(base64_blacklist_data, &blacklist_data) || | |
141 !blacklist->ParseFromString(blacklist_data)) { | |
142 VLOG(1) << "Suggestions blacklist data in profile pref is corrupt, " | |
143 << " clearing it."; | |
144 blacklist->Clear(); | |
145 ClearBlacklist(); | |
146 return false; | |
147 } | |
148 | |
149 return true; | |
150 } | |
151 | |
152 bool BlacklistStore::StoreBlacklist(const SuggestionsBlacklist& blacklist) { | |
153 std::string blacklist_data; | |
154 if (!blacklist.SerializeToString(&blacklist_data)) return false; | |
155 | |
156 std::string base64_blacklist_data; | |
157 base::Base64Encode(blacklist_data, &base64_blacklist_data); | |
158 | |
159 pref_service_->SetString(prefs::kSuggestionsBlacklist, base64_blacklist_data); | |
160 return true; | |
161 } | |
162 | |
163 void BlacklistStore::ClearBlacklist() { | |
164 pref_service_->ClearPref(prefs::kSuggestionsBlacklist); | |
165 } | |
166 | |
167 } // namespace suggestions | |
OLD | NEW |