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 <string> | 9 #include <string> |
9 | 10 |
10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
13 #include "chrome/browser/privacy_blacklist/blacklist_store.h" | 14 #include "chrome/browser/privacy_blacklist/blacklist_store.h" |
14 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
15 #include "net/http/http_util.h" | 16 #include "net/http/http_util.h" |
16 | 17 |
17 #define STRINGIZE(s) #s | 18 #define STRINGIZE(s) #s |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure()); | 153 ((attributes_ & kBlockUnsecure) && !url.SchemeIsSecure()); |
153 } | 154 } |
154 | 155 |
155 Blacklist::Match::Match() : attributes_(0) {} | 156 Blacklist::Match::Match() : attributes_(0) {} |
156 | 157 |
157 void Blacklist::Match::AddEntry(const Entry* entry) { | 158 void Blacklist::Match::AddEntry(const Entry* entry) { |
158 attributes_ |= entry->attributes(); | 159 attributes_ |= entry->attributes(); |
159 entries_.push_back(entry); | 160 entries_.push_back(entry); |
160 } | 161 } |
161 | 162 |
162 Blacklist::Blacklist(const FilePath& file) { | 163 Blacklist::Blacklist(const FilePath& file) : is_good_(false) { |
163 // No blacklist, nothing to load. | 164 // No blacklist, nothing to load. |
164 if (file.value().empty()) | 165 if (file.value().empty()) |
165 return; | 166 return; |
166 | 167 |
167 BlacklistStoreInput input(file_util::OpenFile(file, "rb")); | 168 FILE* fp = file_util::OpenFile(file, "rb"); |
| 169 if (fp == NULL) |
| 170 return; |
| 171 |
| 172 BlacklistStoreInput input(fp); |
168 | 173 |
169 // Read the providers | 174 // Read the providers |
170 std::size_t n = input.ReadNumProviders(); | 175 std::size_t n = input.ReadNumProviders(); |
| 176 if (n == std::numeric_limits<uint32>::max()) |
| 177 return; |
| 178 |
171 providers_.reserve(n); | 179 providers_.reserve(n); |
172 std::string name; | 180 std::string name; |
173 std::string url; | 181 std::string url; |
174 for (std::size_t i = 0; i < n; ++i) { | 182 for (std::size_t i = 0; i < n; ++i) { |
175 input.ReadProvider(&name, &url); | 183 if (!input.ReadProvider(&name, &url)) |
| 184 return; |
176 providers_.push_back(new Provider(name.c_str(), url.c_str())); | 185 providers_.push_back(new Provider(name.c_str(), url.c_str())); |
177 } | 186 } |
178 | 187 |
179 // Read the entries | 188 // Read the entries |
180 n = input.ReadNumEntries(); | 189 n = input.ReadNumEntries(); |
| 190 if (n == std::numeric_limits<uint32>::max()) |
| 191 return; |
| 192 |
181 std::string pattern; | 193 std::string pattern; |
182 unsigned int attributes, provider; | 194 unsigned int attributes, provider; |
183 std::vector<std::string> types; | 195 std::vector<std::string> types; |
184 for (unsigned int i = 0; i < n; ++i) { | 196 for (unsigned int i = 0; i < n; ++i) { |
185 input.ReadEntry(&pattern, &attributes, &types, &provider); | 197 if (!input.ReadEntry(&pattern, &attributes, &types, &provider)) |
| 198 return; |
| 199 |
186 Entry* entry = new Entry(pattern, providers_[provider]); | 200 Entry* entry = new Entry(pattern, providers_[provider]); |
187 entry->AddAttributes(attributes); | 201 entry->AddAttributes(attributes); |
188 entry->SwapTypes(&types); | 202 entry->SwapTypes(&types); |
189 blacklist_.push_back(entry); | 203 blacklist_.push_back(entry); |
190 } | 204 } |
| 205 |
| 206 is_good_ = true; |
191 } | 207 } |
192 | 208 |
193 Blacklist::~Blacklist() { | 209 Blacklist::~Blacklist() { |
194 for (std::vector<Entry*>::iterator i = blacklist_.begin(); | 210 for (std::vector<Entry*>::iterator i = blacklist_.begin(); |
195 i != blacklist_.end(); ++i) | 211 i != blacklist_.end(); ++i) |
196 delete *i; | 212 delete *i; |
197 for (std::vector<Provider*>::iterator i = providers_.begin(); | 213 for (std::vector<Provider*>::iterator i = providers_.begin(); |
198 i != providers_.end(); ++i) | 214 i != providers_.end(); ++i) |
199 delete *i; | 215 delete *i; |
200 } | 216 } |
201 | 217 |
202 // Returns a pointer to the Blacklist-owned entry which matches the given | 218 // Returns a pointer to the Blacklist-owned entry which matches the given |
203 // URL. If no matching Entry is found, returns null. | 219 // URL. If no matching Entry is found, returns null. |
204 Blacklist::Match* Blacklist::findMatch(const GURL& url) const { | 220 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 |
205 // Never match something which is not http, https or ftp. | 224 // Never match something which is not http, https or ftp. |
206 // TODO(idanan): Investigate if this would be an inclusion test instead of an | 225 // TODO(idanan): Investigate if this would be an inclusion test instead of an |
207 // exclusion test and if there are other schemes to test for. | 226 // exclusion test and if there are other schemes to test for. |
208 if (!url.SchemeIs(chrome::kHttpScheme) && | 227 if (!url.SchemeIs(chrome::kHttpScheme) && |
209 !url.SchemeIs(chrome::kHttpsScheme) && | 228 !url.SchemeIs(chrome::kHttpsScheme) && |
210 !url.SchemeIs(chrome::kFtpScheme)) | 229 !url.SchemeIs(chrome::kFtpScheme)) |
211 return 0; | 230 return 0; |
212 Match* match = NULL; | 231 Match* match = NULL; |
213 for (std::vector<Entry*>::const_iterator i = blacklist_.begin(); | 232 for (std::vector<Entry*>::const_iterator i = blacklist_.begin(); |
214 i != blacklist_.end(); ++i) { | 233 i != blacklist_.end(); ++i) { |
(...skipping 21 matching lines...) Expand all Loading... |
236 return cookie; | 255 return cookie; |
237 | 256 |
238 std::string session_cookie(cookie, 0, i); | 257 std::string session_cookie(cookie, 0, i); |
239 std::string::size_type end = cookie.find(';', start + 1); | 258 std::string::size_type end = cookie.find(';', start + 1); |
240 if (end != std::string::npos) | 259 if (end != std::string::npos) |
241 session_cookie.append(cookie.substr(end)); | 260 session_cookie.append(cookie.substr(end)); |
242 return session_cookie; | 261 return session_cookie; |
243 } | 262 } |
244 return cookie; | 263 return cookie; |
245 } | 264 } |
OLD | NEW |