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

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

Issue 149737: Blacklist API change for allowing multiple rule matches... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/privacy_blacklist/blacklist.h" 5 #include "chrome/browser/privacy_blacklist/blacklist.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/file_path.h" 10 #include "base/file_path.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/string_util.h"
12 #include "chrome/browser/privacy_blacklist/blacklist_store.h" 13 #include "chrome/browser/privacy_blacklist/blacklist_store.h"
14 #include "chrome/common/url_constants.h"
13 #include "net/http/http_util.h" 15 #include "net/http/http_util.h"
14 16
15 #define STRINGIZE(s) #s 17 #define STRINGIZE(s) #s
16 18
17 namespace { 19 namespace {
18 20
19 bool matches(const std::string& pattern, const std::string& url) {
20 return url.find(pattern) != std::string::npos;
21 }
22
23 const char* const cookie_headers[2] = { "cookie", "set-cookie" }; 21 const char* const cookie_headers[2] = { "cookie", "set-cookie" };
24 22
25 } // namespace 23 } // namespace
26 24
27 // Value is not important, here just that the object has an address. 25 // Value is not important, here just that the object has an address.
28 const void* const Blacklist::kRequestDataKey = 0; 26 const void* const Blacklist::kRequestDataKey = 0;
29 27
30 unsigned int Blacklist::String2Attribute(const std::string& s) { 28 unsigned int Blacklist::String2Attribute(const std::string& s) {
31 if (s == STRINGIZE(kBlockAll)) 29 if (s == STRINGIZE(kBlockAll))
32 return kBlockAll; 30 return kBlockAll;
33 else if (s == STRINGIZE(kDontSendCookies)) 31 else if (s == STRINGIZE(kDontSendCookies))
34 return kDontSendCookies; 32 return kDontSendCookies;
35 else if (s == STRINGIZE(kDontStoreCookies)) 33 else if (s == STRINGIZE(kDontStoreCookies))
36 return kDontStoreCookies; 34 return kDontStoreCookies;
37 else if (s == STRINGIZE(kDontPersistCookies)) 35 else if (s == STRINGIZE(kDontPersistCookies))
38 return kDontPersistCookies; 36 return kDontPersistCookies;
39 else if (s == STRINGIZE(kDontSendReferrer)) 37 else if (s == STRINGIZE(kDontSendReferrer))
40 return kDontSendReferrer; 38 return kDontSendReferrer;
41 else if (s == STRINGIZE(kDontSendUserAgent)) 39 else if (s == STRINGIZE(kDontSendUserAgent))
42 return kDontSendUserAgent; 40 return kDontSendUserAgent;
43 else if (s == STRINGIZE(kBlockByType)) 41 else if (s == STRINGIZE(kBlockByType))
44 return kBlockByType; 42 return kBlockByType;
45 else if (s == STRINGIZE(kBlockUnsecure)) 43 else if (s == STRINGIZE(kBlockUnsecure))
46 return kBlockUnsecure; 44 return kBlockUnsecure;
47 return 0; 45 return 0;
48 } 46 }
49 47
48 bool Blacklist::Matches(const std::string& pattern, const std::string& url) {
49 if (pattern.size() > url.size())
50 return false;
51
52 std::string::size_type p = 0;
53 std::string::size_type u = 0;
54
55 while (pattern[p] != '\0' && url[u] != '\0') {
56 if (pattern[p] == '@') {
57 while (pattern[++p] == '@'); // Consecutive @ are redundant.
58
59 if (pattern[p] == '\0')
60 return true; // Nothing to match after the @.
61
62 // Look for another wildcard to determine pattern-chunk.
63 std::string::size_type tp = pattern.find_first_of("@", p);
64
65 // If it must match until the end, compare the last characters.
66 if (tp == std::string::npos) {
67 std::string::size_type ur = url.size() - u;
68 std::string::size_type pr = pattern.size() - p;
69 return (pr <= ur) &&
70 (url.compare(url.size() - pr, pr, pattern.c_str() + p) == 0);
71 }
72
73 // Else find the pattern chunk which is pattern[p:tp]
74 std::string::size_type tu = url.find(pattern.c_str() + p, u, tp - p);
75 if (tu == std::string::npos)
76 return false; // Pattern chunk not found.
77
78 // Since tp is strictly greater than p, both u and p always increase.
79 u = tu + tp - p;
80 p = tp;
81 continue;
82 }
83
84 // Match non-wildcard character.
85 if (pattern[p++] != url[u++])
86 return false;
87 }
88 return pattern[p] == '\0';
89 }
90
50 bool Blacklist::Entry::MatchType(const std::string& type) const { 91 bool Blacklist::Entry::MatchType(const std::string& type) const {
51 return std::find(types_.begin(), types_.end(), type) != types_.end(); 92 return std::find(types_.begin(), types_.end(), type) != types_.end();
52 } 93 }
53 94
54 bool Blacklist::Entry::IsBlocked(const GURL& url) const { 95 bool Blacklist::Entry::IsBlocked(const GURL& url) const {
55 return (attributes_ & kBlockAll) || 96 return (attributes_ & kBlockAll) ||
56 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure()); 97 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure());
57 } 98 }
58 99
59 Blacklist::Entry::Entry(const std::string& pattern, const Provider* provider) 100 Blacklist::Entry::Entry(const std::string& pattern, const Provider* provider)
(...skipping 13 matching lines...) Expand all
73 std::copy(entry.types_.begin(), entry.types_.end(), 114 std::copy(entry.types_.begin(), entry.types_.end(),
74 std::back_inserter(types_)); 115 std::back_inserter(types_));
75 } 116 }
76 117
77 void Blacklist::Entry::SwapTypes(std::vector<std::string>* types) { 118 void Blacklist::Entry::SwapTypes(std::vector<std::string>* types) {
78 if (types && types->size()) { 119 if (types && types->size()) {
79 types->swap(types_); 120 types->swap(types_);
80 } 121 }
81 } 122 }
82 123
124 bool Blacklist::Match::MatchType(const std::string& type) const {
125 for (std::vector<const Entry*>::const_iterator i = entries_.begin();
126 i != entries_.end(); ++i) {
127 if ((*i)->MatchType(type))
128 return true;
129 }
130 return false;
131 }
132
133 bool Blacklist::Match::IsBlocked(const GURL& url) const {
134 return (attributes_ & kBlockAll) ||
135 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure());
136 }
137
138 Blacklist::Match::Match() : attributes_(0) {}
139
140 void Blacklist::Match::AddEntry(const Entry* entry) {
141 attributes_ |= entry->attributes();
142 entries_.push_back(entry);
143 }
144
83 Blacklist::Blacklist(const FilePath& file) { 145 Blacklist::Blacklist(const FilePath& file) {
84 // No blacklist, nothing to load. 146 // No blacklist, nothing to load.
85 if (file.value().empty()) 147 if (file.value().empty())
86 return; 148 return;
87 149
88 BlacklistStoreInput input(file_util::OpenFile(file, "rb")); 150 BlacklistStoreInput input(file_util::OpenFile(file, "rb"));
89 151
90 // Read the providers 152 // Read the providers
91 std::size_t n = input.ReadNumProviders(); 153 std::size_t n = input.ReadNumProviders();
92 providers_.reserve(n); 154 providers_.reserve(n);
(...skipping 22 matching lines...) Expand all
115 for (std::vector<Entry*>::iterator i = blacklist_.begin(); 177 for (std::vector<Entry*>::iterator i = blacklist_.begin();
116 i != blacklist_.end(); ++i) 178 i != blacklist_.end(); ++i)
117 delete *i; 179 delete *i;
118 for (std::vector<Provider*>::iterator i = providers_.begin(); 180 for (std::vector<Provider*>::iterator i = providers_.begin();
119 i != providers_.end(); ++i) 181 i != providers_.end(); ++i)
120 delete *i; 182 delete *i;
121 } 183 }
122 184
123 // Returns a pointer to the Blacklist-owned entry which matches the given 185 // Returns a pointer to the Blacklist-owned entry which matches the given
124 // URL. If no matching Entry is found, returns null. 186 // URL. If no matching Entry is found, returns null.
125 const Blacklist::Entry* Blacklist::findMatch(const GURL& url) const { 187 Blacklist::Match* Blacklist::findMatch(const GURL& url) const {
188 // Never match something which is not http, https or ftp.
189 // TODO(idanan): Investigate if this would be an inclusion test instead of an
190 // exclusion test and if there are other schemes to test for.
191 if (!url.SchemeIs(chrome::kHttpScheme) &&
192 !url.SchemeIs(chrome::kHttpsScheme) &&
193 !url.SchemeIs(chrome::kFtpScheme))
194 return 0;
195 Match* match = NULL;
126 for (std::vector<Entry*>::const_iterator i = blacklist_.begin(); 196 for (std::vector<Entry*>::const_iterator i = blacklist_.begin();
127 i != blacklist_.end(); ++i) 197 i != blacklist_.end(); ++i) {
128 if (matches((*i)->pattern(), url.spec())) 198 if (Matches((*i)->pattern(), url.host()+url.path())) {
129 return *i; 199 if (!match)
130 return 0; 200 match = new Match;
201 match->AddEntry(*i);
202 }
203 }
204 return match;
131 } 205 }
132 206
133 std::string Blacklist::StripCookies(const std::string& header) { 207 std::string Blacklist::StripCookies(const std::string& header) {
134 return net::HttpUtil::StripHeaders(header, cookie_headers, 2); 208 return net::HttpUtil::StripHeaders(header, cookie_headers, 2);
135 } 209 }
136 210
137 std::string Blacklist::StripCookieExpiry(const std::string& cookie) { 211 std::string Blacklist::StripCookieExpiry(const std::string& cookie) {
138 std::string::size_type start = cookie.find("; expires="); 212 std::string::size_type delim = cookie.find(';');
213 std::string::size_type start = cookie.find("expires=", delim + 1);
139 if (start != std::string::npos) { 214 if (start != std::string::npos) {
140 std::string::size_type finish = cookie.find(";", start+1); 215 std::string::size_type i = start;
141 std::string session_cookie(cookie, 0, start); 216 // Make sure only whitespace precedes the expiry until a delimiter.
142 if (finish != std::string::npos) 217 while (cookie[--i] != ';')
143 session_cookie.append(cookie.substr(finish)); 218 if (!IsAsciiWhitespace(cookie[i]))
219 return cookie;
220
221 std::string session_cookie(cookie, 0, i);
222 std::string::size_type end = cookie.find(';', start + 1);
223 if (end != std::string::npos)
224 session_cookie.append(cookie.substr(end));
144 return session_cookie; 225 return session_cookie;
145 } 226 }
146 return cookie; 227 return cookie;
147 } 228 }
OLDNEW
« no previous file with comments | « chrome/browser/privacy_blacklist/blacklist.h ('k') | chrome/browser/privacy_blacklist/blacklist_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698