| OLD | NEW |
| 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 <limits> | 8 #include <limits> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 continue; | 99 continue; |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Match non-wildcard character. | 102 // Match non-wildcard character. |
| 103 if (pattern[p++] != url[u++]) | 103 if (pattern[p++] != url[u++]) |
| 104 return false; | 104 return false; |
| 105 } | 105 } |
| 106 return pattern[p] == '\0'; | 106 return pattern[p] == '\0'; |
| 107 } | 107 } |
| 108 | 108 |
| 109 bool Blacklist::Entry::MatchType(const std::string& type) const { | 109 bool Blacklist::Entry::MatchesType(const std::string& type) const { |
| 110 return std::find(types_.begin(), types_.end(), type) != types_.end(); | 110 return std::find(types_.begin(), types_.end(), type) != types_.end(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 bool Blacklist::Entry::IsBlocked(const GURL& url) const { | 113 bool Blacklist::Entry::IsBlocked(const GURL& url) const { |
| 114 return (attributes_ & kBlockAll) || | 114 return (attributes_ & kBlockAll) || |
| 115 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure()); | 115 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure()); |
| 116 } | 116 } |
| 117 | 117 |
| 118 Blacklist::Entry::Entry(const std::string& pattern, const Provider* provider) | 118 Blacklist::Entry::Entry(const std::string& pattern, const Provider* provider) |
| 119 : pattern_(pattern), attributes_(0), provider_(provider) {} | 119 : pattern_(pattern), attributes_(0), provider_(provider) {} |
| 120 | 120 |
| 121 void Blacklist::Entry::AddAttributes(unsigned int attributes) { | 121 void Blacklist::Entry::AddAttributes(unsigned int attributes) { |
| 122 attributes_ |= attributes; | 122 attributes_ |= attributes; |
| 123 } | 123 } |
| 124 | 124 |
| 125 void Blacklist::Entry::AddType(const std::string& type) { | 125 void Blacklist::Entry::AddType(const std::string& type) { |
| 126 types_.push_back(type); | 126 types_.push_back(type); |
| 127 } | 127 } |
| 128 | 128 |
| 129 void Blacklist::Entry::Merge(const Entry& entry) { | 129 void Blacklist::Entry::Merge(const Entry& entry) { |
| 130 attributes_ |= entry.attributes_; | 130 attributes_ |= entry.attributes_; |
| 131 | 131 |
| 132 std::copy(entry.types_.begin(), entry.types_.end(), | 132 std::copy(entry.types_.begin(), entry.types_.end(), |
| 133 std::back_inserter(types_)); | 133 std::back_inserter(types_)); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void Blacklist::Entry::SwapTypes(std::vector<std::string>* types) { | 136 void Blacklist::Entry::SwapTypes(std::vector<std::string>* types) { |
| 137 if (types && types->size()) { | 137 DCHECK(types); |
| 138 types->swap(types_); | 138 types->swap(types_); |
| 139 } | |
| 140 } | 139 } |
| 141 | 140 |
| 142 bool Blacklist::Match::MatchType(const std::string& type) const { | 141 bool Blacklist::Match::MatchType(const std::string& type) const { |
| 143 for (std::vector<const Entry*>::const_iterator i = entries_.begin(); | 142 for (std::vector<const Entry*>::const_iterator i = entries_.begin(); |
| 144 i != entries_.end(); ++i) { | 143 i != entries_.end(); ++i) { |
| 145 if ((*i)->MatchType(type)) | 144 if ((*i)->MatchesType(type)) |
| 146 return true; | 145 return true; |
| 147 } | 146 } |
| 148 return false; | 147 return false; |
| 149 } | 148 } |
| 150 | 149 |
| 151 bool Blacklist::Match::IsBlocked(const GURL& url) const { | 150 bool Blacklist::Match::IsBlocked(const GURL& url) const { |
| 152 return (attributes_ & kBlockAll) || | 151 return (attributes_ & kBlockAll) || |
| 153 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure()); | 152 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure()); |
| 154 } | 153 } |
| 155 | 154 |
| 156 Blacklist::Match::Match() : attributes_(0) {} | 155 Blacklist::Match::Match() : attributes_(0) {} |
| 157 | 156 |
| 158 void Blacklist::Match::AddEntry(const Entry* entry) { | 157 void Blacklist::Match::AddEntry(const Entry* entry) { |
| 159 attributes_ |= entry->attributes(); | 158 attributes_ |= entry->attributes(); |
| 160 entries_.push_back(entry); | 159 entries_.push_back(entry); |
| 161 } | 160 } |
| 162 | 161 |
| 163 Blacklist::Blacklist(const FilePath& file) : is_good_(false) { | 162 Blacklist::Blacklist() { |
| 164 // No blacklist, nothing to load. | |
| 165 if (file.value().empty()) | |
| 166 return; | |
| 167 | |
| 168 FILE* fp = file_util::OpenFile(file, "rb"); | |
| 169 if (fp == NULL) | |
| 170 return; | |
| 171 | |
| 172 BlacklistStoreInput input(fp); | |
| 173 | |
| 174 // Read the providers | |
| 175 std::size_t n = input.ReadNumProviders(); | |
| 176 if (n == std::numeric_limits<uint32>::max()) | |
| 177 return; | |
| 178 | |
| 179 providers_.reserve(n); | |
| 180 std::string name; | |
| 181 std::string url; | |
| 182 for (std::size_t i = 0; i < n; ++i) { | |
| 183 if (!input.ReadProvider(&name, &url)) | |
| 184 return; | |
| 185 providers_.push_back(new Provider(name.c_str(), url.c_str())); | |
| 186 } | |
| 187 | |
| 188 // Read the entries | |
| 189 n = input.ReadNumEntries(); | |
| 190 if (n == std::numeric_limits<uint32>::max()) | |
| 191 return; | |
| 192 | |
| 193 std::string pattern; | |
| 194 unsigned int attributes, provider; | |
| 195 std::vector<std::string> types; | |
| 196 for (unsigned int i = 0; i < n; ++i) { | |
| 197 if (!input.ReadEntry(&pattern, &attributes, &types, &provider)) | |
| 198 return; | |
| 199 | |
| 200 Entry* entry = new Entry(pattern, providers_[provider]); | |
| 201 entry->AddAttributes(attributes); | |
| 202 entry->SwapTypes(&types); | |
| 203 blacklist_.push_back(entry); | |
| 204 } | |
| 205 | |
| 206 is_good_ = true; | |
| 207 } | 163 } |
| 208 | 164 |
| 209 Blacklist::~Blacklist() { | 165 Blacklist::~Blacklist() { |
| 210 for (std::vector<Entry*>::iterator i = blacklist_.begin(); | 166 } |
| 211 i != blacklist_.end(); ++i) | 167 |
| 212 delete *i; | 168 void Blacklist::AddEntry(Entry* entry) { |
| 213 for (std::vector<Provider*>::iterator i = providers_.begin(); | 169 DCHECK(entry); |
| 214 i != providers_.end(); ++i) | 170 blacklist_.push_back(linked_ptr<Entry>(entry)); |
| 215 delete *i; | 171 } |
| 172 |
| 173 void Blacklist::AddProvider(Provider* provider) { |
| 174 DCHECK(provider); |
| 175 providers_.push_back(linked_ptr<Provider>(provider)); |
| 216 } | 176 } |
| 217 | 177 |
| 218 // Returns a pointer to the Blacklist-owned entry which matches the given | 178 // Returns a pointer to the Blacklist-owned entry which matches the given |
| 219 // URL. If no matching Entry is found, returns null. | 179 // URL. If no matching Entry is found, returns null. |
| 220 Blacklist::Match* Blacklist::findMatch(const GURL& url) const { | 180 Blacklist::Match* Blacklist::findMatch(const GURL& url) const { |
| 221 if (!is_good_) | |
| 222 return NULL; // Don't attempt to find matches if the data is corrupt. | |
| 223 | |
| 224 // Never match something which is not http, https or ftp. | 181 // Never match something which is not http, https or ftp. |
| 225 // TODO(idanan): Investigate if this would be an inclusion test instead of an | 182 // TODO(idanan): Investigate if this would be an inclusion test instead of an |
| 226 // exclusion test and if there are other schemes to test for. | 183 // exclusion test and if there are other schemes to test for. |
| 227 if (!url.SchemeIs(chrome::kHttpScheme) && | 184 if (!url.SchemeIs(chrome::kHttpScheme) && |
| 228 !url.SchemeIs(chrome::kHttpsScheme) && | 185 !url.SchemeIs(chrome::kHttpsScheme) && |
| 229 !url.SchemeIs(chrome::kFtpScheme)) | 186 !url.SchemeIs(chrome::kFtpScheme)) |
| 230 return 0; | 187 return 0; |
| 231 Match* match = NULL; | 188 Match* match = NULL; |
| 232 for (std::vector<Entry*>::const_iterator i = blacklist_.begin(); | 189 for (EntryList::const_iterator i = blacklist_.begin(); |
| 233 i != blacklist_.end(); ++i) { | 190 i != blacklist_.end(); ++i) { |
| 234 if (Matches((*i)->pattern(), url.host()+url.path())) { | 191 if (Matches((*i)->pattern(), url.host() + url.path())) { |
| 235 if (!match) | 192 if (!match) |
| 236 match = new Match; | 193 match = new Match; |
| 237 match->AddEntry(*i); | 194 match->AddEntry(i->get()); |
| 238 } | 195 } |
| 239 } | 196 } |
| 240 return match; | 197 return match; |
| 241 } | 198 } |
| 242 | 199 |
| 243 std::string Blacklist::StripCookies(const std::string& header) { | 200 std::string Blacklist::StripCookies(const std::string& header) { |
| 244 return net::HttpUtil::StripHeaders(header, cookie_headers, 2); | 201 return net::HttpUtil::StripHeaders(header, cookie_headers, 2); |
| 245 } | 202 } |
| 246 | 203 |
| 247 std::string Blacklist::StripCookieExpiry(const std::string& cookie) { | 204 std::string Blacklist::StripCookieExpiry(const std::string& cookie) { |
| 248 std::string::size_type delim = cookie.find(';'); | 205 std::string::size_type delim = cookie.find(';'); |
| 249 std::string::size_type start = cookie.find("expires=", delim + 1); | 206 std::string::size_type start = cookie.find("expires=", delim + 1); |
| 250 if (start != std::string::npos) { | 207 if (start != std::string::npos) { |
| 251 std::string::size_type i = start; | 208 std::string::size_type i = start; |
| 252 // Make sure only whitespace precedes the expiry until a delimiter. | 209 // Make sure only whitespace precedes the expiry until a delimiter. |
| 253 while (cookie[--i] != ';') | 210 while (cookie[--i] != ';') |
| 254 if (!IsAsciiWhitespace(cookie[i])) | 211 if (!IsAsciiWhitespace(cookie[i])) |
| 255 return cookie; | 212 return cookie; |
| 256 | 213 |
| 257 std::string session_cookie(cookie, 0, i); | 214 std::string session_cookie(cookie, 0, i); |
| 258 std::string::size_type end = cookie.find(';', start + 1); | 215 std::string::size_type end = cookie.find(';', start + 1); |
| 259 if (end != std::string::npos) | 216 if (end != std::string::npos) |
| 260 session_cookie.append(cookie.substr(end)); | 217 session_cookie.append(cookie.substr(end)); |
| 261 return session_cookie; | 218 return session_cookie; |
| 262 } | 219 } |
| 263 return cookie; | 220 return cookie; |
| 264 } | 221 } |
| OLD | NEW |