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

Side by Side Diff: chrome/browser/privacy_blacklist/blacklist.cc

Issue 2862041: Remove abonded privacy blacklist implementation. (Closed)
Patch Set: fix unit tests Created 10 years, 5 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
OLDNEW
(Empty)
1 // Copyright (c) 2009 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/privacy_blacklist/blacklist.h"
6
7 #include <algorithm>
8 #include <limits>
9 #include <string>
10
11 #include "base/file_path.h"
12 #include "base/file_util.h"
13 #include "base/string_util.h"
14 #include "chrome/browser/pref_service.h"
15 #include "chrome/browser/profile.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/url_constants.h"
18 #include "net/http/http_util.h"
19
20 namespace {
21
22 const char* const cookie_headers[2] = { "cookie", "set-cookie" };
23
24 } // namespace
25
26 const unsigned int Blacklist::kBlockAll = 1;
27 const unsigned int Blacklist::kBlockCookies = 1 << 1;
28 const unsigned int Blacklist::kDontSendReferrer = 1 << 2;
29 const unsigned int Blacklist::kDontSendUserAgent = 1 << 3;
30 const unsigned int Blacklist::kBlockUnsecure = 1 << 4;
31 const unsigned int Blacklist::kBlockRequest = kBlockAll | kBlockUnsecure;
32 const unsigned int Blacklist::kModifySentHeaders =
33 kBlockCookies | kDontSendUserAgent | kDontSendReferrer;
34 const unsigned int Blacklist::kModifyReceivedHeaders = kBlockCookies;
35
36 // static
37 bool Blacklist::Matches(const std::string& pattern, const std::string& url) {
38 if (pattern.size() > url.size())
39 return false;
40
41 std::string::size_type p = 0;
42 std::string::size_type u = 0;
43
44 while (pattern[p] != '\0' && url[u] != '\0') {
45 if (pattern[p] == '@') {
46 while (pattern[++p] == '@') {} // Consecutive @ are redundant.
47
48 if (pattern[p] == '\0')
49 return true; // Nothing to match after the @.
50
51 // Look for another wildcard to determine pattern-chunk.
52 std::string::size_type tp = pattern.find_first_of("@", p);
53
54 // If it must match until the end, compare the last characters.
55 if (tp == std::string::npos) {
56 std::string::size_type ur = url.size() - u;
57 std::string::size_type pr = pattern.size() - p;
58 return (pr <= ur) &&
59 (url.compare(url.size() - pr, pr, pattern.c_str() + p) == 0);
60 }
61
62 // Else find the pattern chunk which is pattern[p:tp]
63 std::string::size_type tu = url.find(pattern.c_str() + p, u, tp - p);
64 if (tu == std::string::npos)
65 return false; // Pattern chunk not found.
66
67 // Since tp is strictly greater than p, both u and p always increase.
68 u = tu + tp - p;
69 p = tp;
70 continue;
71 }
72
73 // Match non-wildcard character.
74 if (pattern[p++] != url[u++])
75 return false;
76 }
77 return pattern[p] == '\0';
78 }
79
80 bool Blacklist::Entry::IsBlocked(const GURL& url) const {
81 return (attributes_ & kBlockAll) ||
82 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure());
83 }
84
85 Blacklist::Entry::Entry(const std::string& pattern, const Provider* provider,
86 bool is_exception)
87 : attributes_(0),
88 is_exception_(is_exception),
89 pattern_(pattern),
90 provider_(provider) {}
91
92 void Blacklist::Entry::AddAttributes(unsigned int attributes) {
93 attributes_ |= attributes;
94 }
95
96 bool Blacklist::Match::IsBlocked(const GURL& url) const {
97 return (attributes() & kBlockAll) ||
98 ((attributes() & kBlockUnsecure) && !url.SchemeIsSecure());
99 }
100
101 Blacklist::Match::Match() : matching_attributes_(0), exception_attributes_(0) {}
102
103 void Blacklist::Match::AddEntry(const Entry* entry) {
104 if (entry->is_exception()) {
105 exception_attributes_ |= entry->attributes();
106 exception_entries_.push_back(entry);
107 } else {
108 matching_attributes_ |= entry->attributes();
109 matching_entries_.push_back(entry);
110 }
111 }
112
113 Blacklist::Blacklist(PrefService* prefs) : prefs_(prefs) {
114 LoadPreferences();
115 }
116
117 Blacklist::~Blacklist() {
118 }
119
120 void Blacklist::AddEntry(Entry* entry) {
121 DCHECK(entry);
122 blacklist_.push_back(linked_ptr<Entry>(entry));
123 }
124
125 void Blacklist::AddProvider(Provider* provider) {
126 DCHECK(provider);
127 providers_.push_back(linked_ptr<Provider>(provider));
128 }
129
130 // Returns a pointer to the Blacklist-owned entry which matches the given
131 // URL. If no matching Entry is found, returns null.
132 Blacklist::Match* Blacklist::FindMatch(const GURL& url) const {
133 // Never match something which is not http, https or ftp.
134 // TODO(idanan): Investigate if this would be an inclusion test instead of an
135 // exclusion test and if there are other schemes to test for.
136 if (!url.SchemeIs(chrome::kHttpScheme) &&
137 !url.SchemeIs(chrome::kHttpsScheme) &&
138 !url.SchemeIs(chrome::kFtpScheme))
139 return 0;
140 std::string url_spec = GetURLAsLookupString(url);
141 Match* match = NULL;
142 for (EntryList::const_iterator i = blacklist_.begin();
143 i != blacklist_.end(); ++i) {
144 if (Matches((*i)->pattern(), url_spec)) {
145 if (!match)
146 match = new Match;
147 match->AddEntry(i->get());
148 }
149 }
150 return match;
151 }
152
153 bool Blacklist::LoadEntryPreference(const ListValue& pref,
154 const Provider* provider) {
155 EntryList entries;
156 for (ListValue::const_iterator i = pref.begin(); i != pref.end(); ++i) {
157 if (!(*i)->IsType(Value::TYPE_DICTIONARY))
158 return false;
159 const DictionaryValue* entry_pref = static_cast<DictionaryValue*>(*i);
160 std::string pattern;
161 int attributes;
162 bool is_exception;
163 if (!(entry_pref->GetString(L"pattern", &pattern) &&
164 entry_pref->GetInteger(L"attributes", &attributes) &&
165 entry_pref->GetBoolean(L"exception", &is_exception)))
166 return false;
167 Entry* entry = new Entry(pattern, provider, is_exception);
168 entry->AddAttributes(static_cast<unsigned int>(attributes));
169 entries.push_back(linked_ptr<Entry>(entry));
170 }
171 blacklist_.insert(blacklist_.end(), entries.begin(), entries.end());
172 return true;
173 }
174
175 bool Blacklist::LoadProviderPreference(const DictionaryValue& pref,
176 const std::wstring& path) {
177 std::string name, url;
178 ListValue* entries;
179 if (!(pref.GetString(L"name", &name) && pref.GetString(L"url", &url) &&
180 pref.GetList(L"entries", &entries)))
181 return false;
182 linked_ptr<Provider> provider(new Provider(name, url, path));
183 if (LoadEntryPreference(*entries, provider.get()))
184 providers_.push_back(provider);
185 return true;
186 return false;
187 }
188
189 bool Blacklist::LoadPreferences() {
190 DCHECK(prefs_);
191 const DictionaryValue* blacklist_rules = prefs_->GetDictionary(
192 prefs::kPrivacyFilterRules);
193 if (!blacklist_rules)
194 return false;
195 bool result = true;
196 for (DictionaryValue::key_iterator key = blacklist_rules->begin_keys();
197 key != blacklist_rules->end_keys(); ++key) {
198 DictionaryValue* provider;
199 if (!(blacklist_rules->GetDictionaryWithoutPathExpansion(*key, &provider) &&
200 LoadProviderPreference(*provider, *key)))
201 result = false;
202 }
203 return result;
204 }
205
206 // static
207 std::string Blacklist::GetURLAsLookupString(const GURL& url) {
208 std::string url_spec = url.host() + url.path();
209 if (!url.query().empty())
210 url_spec = url_spec + "?" + url.query();
211
212 return url_spec;
213 }
214
215 // static
216 void Blacklist::RegisterUserPrefs(PrefService* user_prefs) {
217 user_prefs->RegisterDictionaryPref(prefs::kPrivacyFilterRules);
218 }
219
220 // static
221 std::string Blacklist::StripCookies(const std::string& header) {
222 return net::HttpUtil::StripHeaders(header, cookie_headers, 2);
223 }
OLDNEW
« no previous file with comments | « chrome/browser/privacy_blacklist/blacklist.h ('k') | chrome/browser/privacy_blacklist/blacklist_interceptor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698