OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/safe_browsing/safe_browsing_database.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
25 #include "crypto/sha2.h" | 25 #include "crypto/sha2.h" |
26 #include "net/base/ip_address_number.h" | 26 #include "net/base/ip_address_number.h" |
27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
28 | 28 |
29 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
30 #include "base/mac/mac_util.h" | 30 #include "base/mac/mac_util.h" |
31 #endif | 31 #endif |
32 | 32 |
33 using content::BrowserThread; | 33 using content::BrowserThread; |
34 using safe_browsing::PrefixSet; | 34 |
35 using safe_browsing::PrefixSetBuilder; | 35 namespace safe_browsing { |
36 | 36 |
37 namespace { | 37 namespace { |
38 | 38 |
39 // Filename suffix for the bloom filter. | 39 // Filename suffix for the bloom filter. |
40 const base::FilePath::CharType kBloomFilterFileSuffix[] = | 40 const base::FilePath::CharType kBloomFilterFileSuffix[] = |
41 FILE_PATH_LITERAL(" Filter 2"); | 41 FILE_PATH_LITERAL(" Filter 2"); |
42 // Filename suffix for the prefix set. | 42 // Filename suffix for the prefix set. |
43 const base::FilePath::CharType kPrefixSetFileSuffix[] = | 43 const base::FilePath::CharType kPrefixSetFileSuffix[] = |
44 FILE_PATH_LITERAL(" Prefix Set"); | 44 FILE_PATH_LITERAL(" Prefix Set"); |
45 // Filename suffix for download store. | 45 // Filename suffix for download store. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // TODO(lzheng): It was reasonable when database is saved in sqlite, but | 100 // TODO(lzheng): It was reasonable when database is saved in sqlite, but |
101 // there should be better ways to save chunk_id and list_id after we use | 101 // there should be better ways to save chunk_id and list_id after we use |
102 // SafeBrowsingStoreFile. | 102 // SafeBrowsingStoreFile. |
103 int GetListIdBit(const int encoded_chunk_id) { | 103 int GetListIdBit(const int encoded_chunk_id) { |
104 return encoded_chunk_id & 1; | 104 return encoded_chunk_id & 1; |
105 } | 105 } |
106 int DecodeChunkId(int encoded_chunk_id) { | 106 int DecodeChunkId(int encoded_chunk_id) { |
107 return encoded_chunk_id >> 1; | 107 return encoded_chunk_id >> 1; |
108 } | 108 } |
109 int EncodeChunkId(const int chunk, const int list_id) { | 109 int EncodeChunkId(const int chunk, const int list_id) { |
110 DCHECK_NE(list_id, safe_browsing::INVALID); | 110 DCHECK_NE(list_id, INVALID); |
111 return chunk << 1 | list_id % 2; | 111 return chunk << 1 | list_id % 2; |
112 } | 112 } |
113 | 113 |
114 // Generate the set of full hashes to check for |url|. If | 114 // Generate the set of full hashes to check for |url|. If |
115 // |include_whitelist_hashes| is true we will generate additional path-prefixes | 115 // |include_whitelist_hashes| is true we will generate additional path-prefixes |
116 // to match against the csd whitelist. E.g., if the path-prefix /foo is on the | 116 // to match against the csd whitelist. E.g., if the path-prefix /foo is on the |
117 // whitelist it should also match /foo/bar which is not the case for all the | 117 // whitelist it should also match /foo/bar which is not the case for all the |
118 // other lists. We'll also always add a pattern for the empty path. | 118 // other lists. We'll also always add a pattern for the empty path. |
119 // TODO(shess): This function is almost the same as | 119 // TODO(shess): This function is almost the same as |
120 // |CompareFullHashes()| in safe_browsing_util.cc, except that code | 120 // |CompareFullHashes()| in safe_browsing_util.cc, except that code |
121 // does an early exit on match. Since match should be the infrequent | 121 // does an early exit on match. Since match should be the infrequent |
122 // case (phishing or malware found), consider combining this function | 122 // case (phishing or malware found), consider combining this function |
123 // with that one. | 123 // with that one. |
124 void UrlToFullHashes(const GURL& url, | 124 void UrlToFullHashes(const GURL& url, |
125 bool include_whitelist_hashes, | 125 bool include_whitelist_hashes, |
126 std::vector<SBFullHash>* full_hashes) { | 126 std::vector<SBFullHash>* full_hashes) { |
127 std::vector<std::string> hosts; | 127 std::vector<std::string> hosts; |
128 if (url.HostIsIPAddress()) { | 128 if (url.HostIsIPAddress()) { |
129 hosts.push_back(url.host()); | 129 hosts.push_back(url.host()); |
130 } else { | 130 } else { |
131 safe_browsing::GenerateHostsToCheck(url, &hosts); | 131 GenerateHostsToCheck(url, &hosts); |
132 } | 132 } |
133 | 133 |
134 std::vector<std::string> paths; | 134 std::vector<std::string> paths; |
135 safe_browsing::GeneratePathsToCheck(url, &paths); | 135 GeneratePathsToCheck(url, &paths); |
136 | 136 |
137 for (size_t i = 0; i < hosts.size(); ++i) { | 137 for (size_t i = 0; i < hosts.size(); ++i) { |
138 for (size_t j = 0; j < paths.size(); ++j) { | 138 for (size_t j = 0; j < paths.size(); ++j) { |
139 const std::string& path = paths[j]; | 139 const std::string& path = paths[j]; |
140 full_hashes->push_back(safe_browsing::SBFullHashForString( | 140 full_hashes->push_back( |
141 hosts[i] + path)); | 141 SBFullHashForString(hosts[i] + path)); |
142 | 142 |
143 // We may have /foo as path-prefix in the whitelist which should | 143 // We may have /foo as path-prefix in the whitelist which should |
144 // also match with /foo/bar and /foo?bar. Hence, for every path | 144 // also match with /foo/bar and /foo?bar. Hence, for every path |
145 // that ends in '/' we also add the path without the slash. | 145 // that ends in '/' we also add the path without the slash. |
146 if (include_whitelist_hashes && | 146 if (include_whitelist_hashes && path.size() > 1 && |
147 path.size() > 1 && | |
148 path[path.size() - 1] == '/') { | 147 path[path.size() - 1] == '/') { |
149 full_hashes->push_back( | 148 full_hashes->push_back(SBFullHashForString( |
150 safe_browsing::SBFullHashForString( | 149 hosts[i] + path.substr(0, path.size() - 1))); |
151 hosts[i] + path.substr(0, path.size() - 1))); | |
152 } | 150 } |
153 } | 151 } |
154 } | 152 } |
155 } | 153 } |
156 | 154 |
157 // Helper function to compare addprefixes in |store| with |prefixes|. | 155 // Helper function to compare addprefixes in |store| with |prefixes|. |
158 // The |list_bit| indicates which list (url or hash) to compare. | 156 // The |list_bit| indicates which list (url or hash) to compare. |
159 // | 157 // |
160 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain | 158 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain |
161 // the actual matching prefixes. | 159 // the actual matching prefixes. |
162 bool MatchAddPrefixes(SafeBrowsingStore* store, | 160 bool MatchAddPrefixes(SafeBrowsingStore* store, |
163 int list_bit, | 161 int list_bit, |
164 const std::vector<SBPrefix>& prefixes, | 162 const std::vector<SBPrefix>& prefixes, |
165 std::vector<SBPrefix>* prefix_hits) { | 163 std::vector<SBPrefix>* prefix_hits) { |
166 prefix_hits->clear(); | 164 prefix_hits->clear(); |
167 bool found_match = false; | 165 bool found_match = false; |
168 | 166 |
169 SBAddPrefixes add_prefixes; | 167 SBAddPrefixes add_prefixes; |
170 store->GetAddPrefixes(&add_prefixes); | 168 store->GetAddPrefixes(&add_prefixes); |
171 for (SBAddPrefixes::const_iterator iter = add_prefixes.begin(); | 169 for (SBAddPrefixes::const_iterator iter = add_prefixes.begin(); |
172 iter != add_prefixes.end(); ++iter) { | 170 iter != add_prefixes.end(); ++iter) { |
173 for (size_t j = 0; j < prefixes.size(); ++j) { | 171 for (size_t j = 0; j < prefixes.size(); ++j) { |
174 const SBPrefix& prefix = prefixes[j]; | 172 const SBPrefix& prefix = prefixes[j]; |
175 if (prefix == iter->prefix && | 173 if (prefix == iter->prefix && GetListIdBit(iter->chunk_id) == list_bit) { |
176 GetListIdBit(iter->chunk_id) == list_bit) { | |
177 prefix_hits->push_back(prefix); | 174 prefix_hits->push_back(prefix); |
178 found_match = true; | 175 found_match = true; |
179 } | 176 } |
180 } | 177 } |
181 } | 178 } |
182 return found_match; | 179 return found_match; |
183 } | 180 } |
184 | 181 |
185 // This function generates a chunk range string for |chunks|. It | 182 // This function generates a chunk range string for |chunks|. It |
186 // outputs one chunk range string per list and writes it to the | 183 // outputs one chunk range string per list and writes it to the |
187 // |list_ranges| vector. We expect |list_ranges| to already be of the | 184 // |list_ranges| vector. We expect |list_ranges| to already be of the |
188 // right size. E.g., if |chunks| contains chunks with two different | 185 // right size. E.g., if |chunks| contains chunks with two different |
189 // list ids then |list_ranges| must contain two elements. | 186 // list ids then |list_ranges| must contain two elements. |
190 void GetChunkRanges(const std::vector<int>& chunks, | 187 void GetChunkRanges(const std::vector<int>& chunks, |
191 std::vector<std::string>* list_ranges) { | 188 std::vector<std::string>* list_ranges) { |
192 // Since there are 2 possible list ids, there must be exactly two | 189 // Since there are 2 possible list ids, there must be exactly two |
193 // list ranges. Even if the chunk data should only contain one | 190 // list ranges. Even if the chunk data should only contain one |
194 // line, this code has to somehow handle corruption. | 191 // line, this code has to somehow handle corruption. |
195 DCHECK_EQ(2U, list_ranges->size()); | 192 DCHECK_EQ(2U, list_ranges->size()); |
196 | 193 |
197 std::vector<std::vector<int> > decoded_chunks(list_ranges->size()); | 194 std::vector<std::vector<int>> decoded_chunks(list_ranges->size()); |
198 for (std::vector<int>::const_iterator iter = chunks.begin(); | 195 for (std::vector<int>::const_iterator iter = chunks.begin(); |
199 iter != chunks.end(); ++iter) { | 196 iter != chunks.end(); ++iter) { |
200 int mod_list_id = GetListIdBit(*iter); | 197 int mod_list_id = GetListIdBit(*iter); |
201 DCHECK_GE(mod_list_id, 0); | 198 DCHECK_GE(mod_list_id, 0); |
202 DCHECK_LT(static_cast<size_t>(mod_list_id), decoded_chunks.size()); | 199 DCHECK_LT(static_cast<size_t>(mod_list_id), decoded_chunks.size()); |
203 decoded_chunks[mod_list_id].push_back(DecodeChunkId(*iter)); | 200 decoded_chunks[mod_list_id].push_back(DecodeChunkId(*iter)); |
204 } | 201 } |
205 for (size_t i = 0; i < decoded_chunks.size(); ++i) { | 202 for (size_t i = 0; i < decoded_chunks.size(); ++i) { |
206 ChunksToRangeString(decoded_chunks[i], &((*list_ranges)[i])); | 203 ChunksToRangeString(decoded_chunks[i], &((*list_ranges)[i])); |
207 } | 204 } |
(...skipping 17 matching lines...) Expand all Loading... |
225 // Always decode 2 ranges, even if only the first one is expected. | 222 // Always decode 2 ranges, even if only the first one is expected. |
226 // The loop below will only load as many into |lists| as |listnames| | 223 // The loop below will only load as many into |lists| as |listnames| |
227 // indicates. | 224 // indicates. |
228 std::vector<std::string> adds(2); | 225 std::vector<std::string> adds(2); |
229 std::vector<std::string> subs(2); | 226 std::vector<std::string> subs(2); |
230 GetChunkRanges(add_chunks, &adds); | 227 GetChunkRanges(add_chunks, &adds); |
231 GetChunkRanges(sub_chunks, &subs); | 228 GetChunkRanges(sub_chunks, &subs); |
232 | 229 |
233 for (size_t i = 0; i < listnames.size(); ++i) { | 230 for (size_t i = 0; i < listnames.size(); ++i) { |
234 const std::string& listname = listnames[i]; | 231 const std::string& listname = listnames[i]; |
235 DCHECK_EQ(safe_browsing::GetListId(listname) % 2, | 232 DCHECK_EQ(GetListId(listname) % 2, static_cast<int>(i % 2)); |
236 static_cast<int>(i % 2)); | 233 DCHECK_NE(GetListId(listname), INVALID); |
237 DCHECK_NE(safe_browsing::GetListId(listname), | |
238 safe_browsing::INVALID); | |
239 lists->push_back(SBListChunkRanges(listname)); | 234 lists->push_back(SBListChunkRanges(listname)); |
240 lists->back().adds.swap(adds[i]); | 235 lists->back().adds.swap(adds[i]); |
241 lists->back().subs.swap(subs[i]); | 236 lists->back().subs.swap(subs[i]); |
242 } | 237 } |
243 } | 238 } |
244 | 239 |
245 void UpdateChunkRangesForLists(SafeBrowsingStore* store, | 240 void UpdateChunkRangesForLists( |
246 const std::string& listname0, | 241 SafeBrowsingStore* store, |
247 const std::string& listname1, | 242 const std::string& listname0, |
248 std::vector<SBListChunkRanges>* lists) { | 243 const std::string& listname1, |
| 244 std::vector<SBListChunkRanges>* lists) { |
249 std::vector<std::string> listnames; | 245 std::vector<std::string> listnames; |
250 listnames.push_back(listname0); | 246 listnames.push_back(listname0); |
251 listnames.push_back(listname1); | 247 listnames.push_back(listname1); |
252 UpdateChunkRanges(store, listnames, lists); | 248 UpdateChunkRanges(store, listnames, lists); |
253 } | 249 } |
254 | 250 |
255 void UpdateChunkRangesForList(SafeBrowsingStore* store, | 251 void UpdateChunkRangesForList( |
256 const std::string& listname, | 252 SafeBrowsingStore* store, |
257 std::vector<SBListChunkRanges>* lists) { | 253 const std::string& listname, |
| 254 std::vector<SBListChunkRanges>* lists) { |
258 UpdateChunkRanges(store, std::vector<std::string>(1, listname), lists); | 255 UpdateChunkRanges(store, std::vector<std::string>(1, listname), lists); |
259 } | 256 } |
260 | 257 |
261 // This code always checks for non-zero file size. This helper makes | 258 // This code always checks for non-zero file size. This helper makes |
262 // that less verbose. | 259 // that less verbose. |
263 int64 GetFileSizeOrZero(const base::FilePath& file_path) { | 260 int64 GetFileSizeOrZero(const base::FilePath& file_path) { |
264 int64 size_64; | 261 int64 size_64; |
265 if (!base::GetFileSize(file_path, &size_64)) | 262 if (!base::GetFileSize(file_path, &size_64)) |
266 return 0; | 263 return 0; |
267 return size_64; | 264 return size_64; |
268 } | 265 } |
269 | 266 |
270 // Helper for PrefixSetContainsUrlHashes(). Returns true if an un-expired match | 267 // Helper for PrefixSetContainsUrlHashes(). Returns true if an un-expired match |
271 // for |full_hash| is found in |cache|, with any matches appended to |results| | 268 // for |full_hash| is found in |cache|, with any matches appended to |results| |
272 // (true can be returned with zero matches). |expire_base| is used to check the | 269 // (true can be returned with zero matches). |expire_base| is used to check the |
273 // cache lifetime of matches, expired matches will be discarded from |cache|. | 270 // cache lifetime of matches, expired matches will be discarded from |cache|. |
274 bool GetCachedFullHash(std::map<SBPrefix, SBCachedFullHashResult>* cache, | 271 bool GetCachedFullHash(std::map<SBPrefix, SBCachedFullHashResult>* cache, |
275 const SBFullHash& full_hash, | 272 const SBFullHash& full_hash, |
276 const base::Time& expire_base, | 273 const base::Time& expire_base, |
277 std::vector<SBFullHashResult>* results) { | 274 std::vector<SBFullHashResult>* results) { |
278 // First check if there is a valid cached result for this prefix. | 275 // First check if there is a valid cached result for this prefix. |
279 std::map<SBPrefix, SBCachedFullHashResult>::iterator | 276 std::map<SBPrefix, SBCachedFullHashResult>::iterator citer = |
280 citer = cache->find(full_hash.prefix); | 277 cache->find(full_hash.prefix); |
281 if (citer == cache->end()) | 278 if (citer == cache->end()) |
282 return false; | 279 return false; |
283 | 280 |
284 // Remove expired entries. | 281 // Remove expired entries. |
285 SBCachedFullHashResult& cached_result = citer->second; | 282 SBCachedFullHashResult& cached_result = citer->second; |
286 if (cached_result.expire_after <= expire_base) { | 283 if (cached_result.expire_after <= expire_base) { |
287 cache->erase(citer); | 284 cache->erase(citer); |
288 return false; | 285 return false; |
289 } | 286 } |
290 | 287 |
291 // Find full-hash matches. | 288 // Find full-hash matches. |
292 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes; | 289 std::vector<SBFullHashResult>& cached_hashes = |
| 290 cached_result.full_hashes; |
293 for (size_t i = 0; i < cached_hashes.size(); ++i) { | 291 for (size_t i = 0; i < cached_hashes.size(); ++i) { |
294 if (safe_browsing::SBFullHashEqual(full_hash, cached_hashes[i].hash)) | 292 if (SBFullHashEqual(full_hash, cached_hashes[i].hash)) |
295 results->push_back(cached_hashes[i]); | 293 results->push_back(cached_hashes[i]); |
296 } | 294 } |
297 | 295 |
298 return true; | 296 return true; |
299 } | 297 } |
300 | 298 |
301 SafeBrowsingStoreFile* CreateStore( | 299 SafeBrowsingStoreFile* CreateStore( |
302 bool enable, | 300 bool enable, |
303 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 301 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
304 if (!enable) | 302 if (!enable) |
(...skipping 18 matching lines...) Expand all Loading... |
323 db_task_runner, CreateStore(true, db_task_runner), // browse_store | 321 db_task_runner, CreateStore(true, db_task_runner), // browse_store |
324 CreateStore(enable_download_protection, db_task_runner), | 322 CreateStore(enable_download_protection, db_task_runner), |
325 CreateStore(enable_client_side_whitelist, db_task_runner), | 323 CreateStore(enable_client_side_whitelist, db_task_runner), |
326 CreateStore(enable_download_whitelist, db_task_runner), | 324 CreateStore(enable_download_whitelist, db_task_runner), |
327 CreateStore(true, db_task_runner), // inclusion_whitelist_store | 325 CreateStore(true, db_task_runner), // inclusion_whitelist_store |
328 CreateStore(enable_extension_blacklist, db_task_runner), | 326 CreateStore(enable_extension_blacklist, db_task_runner), |
329 CreateStore(enable_ip_blacklist, db_task_runner), | 327 CreateStore(enable_ip_blacklist, db_task_runner), |
330 CreateStore(enable_unwanted_software_list, db_task_runner)); | 328 CreateStore(enable_unwanted_software_list, db_task_runner)); |
331 } | 329 } |
332 | 330 |
333 SafeBrowsingDatabaseFactoryImpl() { } | 331 SafeBrowsingDatabaseFactoryImpl() {} |
334 | 332 |
335 private: | 333 private: |
336 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseFactoryImpl); | 334 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseFactoryImpl); |
337 }; | 335 }; |
338 | 336 |
339 // static | 337 // static |
340 SafeBrowsingDatabaseFactory* SafeBrowsingDatabase::factory_ = NULL; | 338 SafeBrowsingDatabaseFactory* SafeBrowsingDatabase::factory_ = NULL; |
341 | 339 |
342 // Factory method, should be called on the Safe Browsing sequenced task runner, | 340 // Factory method, should be called on the Safe Browsing sequenced task runner, |
343 // which is also passed to the function as |current_task_runner|. | 341 // which is also passed to the function as |current_task_runner|. |
(...skipping 11 matching lines...) Expand all Loading... |
355 DCHECK(current_task_runner->RunsTasksOnCurrentThread()); | 353 DCHECK(current_task_runner->RunsTasksOnCurrentThread()); |
356 if (!factory_) | 354 if (!factory_) |
357 factory_ = new SafeBrowsingDatabaseFactoryImpl(); | 355 factory_ = new SafeBrowsingDatabaseFactoryImpl(); |
358 return factory_->CreateSafeBrowsingDatabase( | 356 return factory_->CreateSafeBrowsingDatabase( |
359 current_task_runner, enable_download_protection, | 357 current_task_runner, enable_download_protection, |
360 enable_client_side_whitelist, enable_download_whitelist, | 358 enable_client_side_whitelist, enable_download_whitelist, |
361 enable_extension_blacklist, enable_ip_blacklist, | 359 enable_extension_blacklist, enable_ip_blacklist, |
362 enable_unwanted_software_list); | 360 enable_unwanted_software_list); |
363 } | 361 } |
364 | 362 |
365 SafeBrowsingDatabase::~SafeBrowsingDatabase() { | 363 SafeBrowsingDatabase::~SafeBrowsingDatabase() {} |
366 } | |
367 | 364 |
368 // static | 365 // static |
369 base::FilePath SafeBrowsingDatabase::BrowseDBFilename( | 366 base::FilePath SafeBrowsingDatabase::BrowseDBFilename( |
370 const base::FilePath& db_base_filename) { | 367 const base::FilePath& db_base_filename) { |
371 return base::FilePath(db_base_filename.value() + kBrowseDBFile); | 368 return base::FilePath(db_base_filename.value() + kBrowseDBFile); |
372 } | 369 } |
373 | 370 |
374 // static | 371 // static |
375 base::FilePath SafeBrowsingDatabase::DownloadDBFilename( | 372 base::FilePath SafeBrowsingDatabase::DownloadDBFilename( |
376 const base::FilePath& db_base_filename) { | 373 const base::FilePath& db_base_filename) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 UrlToFullHashes(urls[i], false, &full_hashes); | 437 UrlToFullHashes(urls[i], false, &full_hashes); |
441 | 438 |
442 for (size_t i = 0; i < full_hashes.size(); ++i) | 439 for (size_t i = 0; i < full_hashes.size(); ++i) |
443 prefixes->push_back(full_hashes[i].prefix); | 440 prefixes->push_back(full_hashes[i].prefix); |
444 } | 441 } |
445 | 442 |
446 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { | 443 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { |
447 // Stores are not thread safe. | 444 // Stores are not thread safe. |
448 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 445 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
449 | 446 |
450 if (list_id == safe_browsing::PHISH || | 447 if (list_id == PHISH || list_id == MALWARE) { |
451 list_id == safe_browsing::MALWARE) { | |
452 return browse_store_.get(); | 448 return browse_store_.get(); |
453 } else if (list_id == safe_browsing::BINURL) { | 449 } else if (list_id == BINURL) { |
454 return download_store_.get(); | 450 return download_store_.get(); |
455 } else if (list_id == safe_browsing::CSDWHITELIST) { | 451 } else if (list_id == CSDWHITELIST) { |
456 return csd_whitelist_store_.get(); | 452 return csd_whitelist_store_.get(); |
457 } else if (list_id == safe_browsing::DOWNLOADWHITELIST) { | 453 } else if (list_id == DOWNLOADWHITELIST) { |
458 return download_whitelist_store_.get(); | 454 return download_whitelist_store_.get(); |
459 } else if (list_id == safe_browsing::INCLUSIONWHITELIST) { | 455 } else if (list_id == INCLUSIONWHITELIST) { |
460 return inclusion_whitelist_store_.get(); | 456 return inclusion_whitelist_store_.get(); |
461 } else if (list_id == safe_browsing::EXTENSIONBLACKLIST) { | 457 } else if (list_id == EXTENSIONBLACKLIST) { |
462 return extension_blacklist_store_.get(); | 458 return extension_blacklist_store_.get(); |
463 } else if (list_id == safe_browsing::IPBLACKLIST) { | 459 } else if (list_id == IPBLACKLIST) { |
464 return ip_blacklist_store_.get(); | 460 return ip_blacklist_store_.get(); |
465 } else if (list_id == safe_browsing::UNWANTEDURL) { | 461 } else if (list_id == UNWANTEDURL) { |
466 return unwanted_software_store_.get(); | 462 return unwanted_software_store_.get(); |
467 } | 463 } |
468 return NULL; | 464 return NULL; |
469 } | 465 } |
470 | 466 |
471 // static | 467 // static |
472 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) { | 468 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) { |
473 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type, | 469 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type, |
474 FAILURE_DATABASE_MAX); | 470 FAILURE_DATABASE_MAX); |
475 } | 471 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 } | 596 } |
601 | 597 |
602 ThreadSafeStateManager* outer_; | 598 ThreadSafeStateManager* outer_; |
603 base::AutoLock transaction_lock_; | 599 base::AutoLock transaction_lock_; |
604 | 600 |
605 DISALLOW_COPY_AND_ASSIGN(WriteTransaction); | 601 DISALLOW_COPY_AND_ASSIGN(WriteTransaction); |
606 }; | 602 }; |
607 | 603 |
608 SafeBrowsingDatabaseNew::ThreadSafeStateManager::ThreadSafeStateManager( | 604 SafeBrowsingDatabaseNew::ThreadSafeStateManager::ThreadSafeStateManager( |
609 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner) | 605 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner) |
610 : db_task_runner_(db_task_runner) { | 606 : db_task_runner_(db_task_runner) {} |
611 } | |
612 | 607 |
613 SafeBrowsingDatabaseNew::ThreadSafeStateManager::~ThreadSafeStateManager() { | 608 SafeBrowsingDatabaseNew::ThreadSafeStateManager::~ThreadSafeStateManager() {} |
614 } | |
615 | 609 |
616 SafeBrowsingDatabaseNew::DatabaseStateManager::DatabaseStateManager( | 610 SafeBrowsingDatabaseNew::DatabaseStateManager::DatabaseStateManager( |
617 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner) | 611 const scoped_refptr<const base::SequencedTaskRunner>& db_task_runner) |
618 : db_task_runner_(db_task_runner), | 612 : db_task_runner_(db_task_runner), |
619 corruption_detected_(false), | 613 corruption_detected_(false), |
620 change_detected_(false) { | 614 change_detected_(false) {} |
621 } | |
622 | 615 |
623 SafeBrowsingDatabaseNew::DatabaseStateManager::~DatabaseStateManager() { | 616 SafeBrowsingDatabaseNew::DatabaseStateManager::~DatabaseStateManager() {} |
624 } | |
625 | 617 |
626 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> | 618 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> |
627 SafeBrowsingDatabaseNew::ThreadSafeStateManager::BeginReadTransaction() { | 619 SafeBrowsingDatabaseNew::ThreadSafeStateManager::BeginReadTransaction() { |
628 return make_scoped_ptr( | 620 return make_scoped_ptr( |
629 new ReadTransaction(this, ReadTransaction::AutoLockRequirement::LOCK)); | 621 new ReadTransaction(this, ReadTransaction::AutoLockRequirement::LOCK)); |
630 } | 622 } |
631 | 623 |
632 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> SafeBrowsingDatabaseNew:: | 624 scoped_ptr<SafeBrowsingDatabaseNew::ReadTransaction> SafeBrowsingDatabaseNew:: |
633 ThreadSafeStateManager::BeginReadTransactionNoLockOnMainTaskRunner() { | 625 ThreadSafeStateManager::BeginReadTransactionNoLockOnMainTaskRunner() { |
634 return make_scoped_ptr(new ReadTransaction( | 626 return make_scoped_ptr(new ReadTransaction( |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
909 | 901 |
910 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes( | 902 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes( |
911 const std::vector<SBPrefix>& prefixes, | 903 const std::vector<SBPrefix>& prefixes, |
912 std::vector<SBPrefix>* prefix_hits) { | 904 std::vector<SBPrefix>* prefix_hits) { |
913 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 905 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
914 | 906 |
915 // Ignore this check when download checking is not enabled. | 907 // Ignore this check when download checking is not enabled. |
916 if (!download_store_.get()) | 908 if (!download_store_.get()) |
917 return false; | 909 return false; |
918 | 910 |
919 return MatchAddPrefixes(download_store_.get(), | 911 return MatchAddPrefixes(download_store_.get(), BINURL % 2, prefixes, |
920 safe_browsing::BINURL % 2, | |
921 prefixes, | |
922 prefix_hits); | 912 prefix_hits); |
923 } | 913 } |
924 | 914 |
925 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { | 915 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { |
926 std::vector<SBFullHash> full_hashes; | 916 std::vector<SBFullHash> full_hashes; |
927 UrlToFullHashes(url, true, &full_hashes); | 917 UrlToFullHashes(url, true, &full_hashes); |
928 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); | 918 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); |
929 } | 919 } |
930 | 920 |
931 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedUrl(const GURL& url) { | 921 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedUrl(const GURL& url) { |
(...skipping 10 matching lines...) Expand all Loading... |
942 | 932 |
943 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( | 933 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( |
944 const std::vector<SBPrefix>& prefixes, | 934 const std::vector<SBPrefix>& prefixes, |
945 std::vector<SBPrefix>* prefix_hits) { | 935 std::vector<SBPrefix>* prefix_hits) { |
946 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 936 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
947 | 937 |
948 if (!extension_blacklist_store_) | 938 if (!extension_blacklist_store_) |
949 return false; | 939 return false; |
950 | 940 |
951 return MatchAddPrefixes(extension_blacklist_store_.get(), | 941 return MatchAddPrefixes(extension_blacklist_store_.get(), |
952 safe_browsing::EXTENSIONBLACKLIST % 2, | 942 EXTENSIONBLACKLIST % 2, prefixes, prefix_hits); |
953 prefixes, | |
954 prefix_hits); | |
955 } | 943 } |
956 | 944 |
957 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { | 945 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { |
958 net::IPAddressNumber ip_number; | 946 net::IPAddressNumber ip_number; |
959 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) | 947 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) |
960 return false; | 948 return false; |
961 if (ip_number.size() == net::kIPv4AddressSize) | 949 if (ip_number.size() == net::kIPv4AddressSize) |
962 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); | 950 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); |
963 if (ip_number.size() != net::kIPv6AddressSize) | 951 if (ip_number.size() != net::kIPv6AddressSize) |
964 return false; // better safe than sorry. | 952 return false; // better safe than sorry. |
(...skipping 17 matching lines...) Expand all Loading... |
982 if (it->second.count(hash) > 0) { | 970 if (it->second.count(hash) > 0) { |
983 return true; | 971 return true; |
984 } | 972 } |
985 } | 973 } |
986 return false; | 974 return false; |
987 } | 975 } |
988 | 976 |
989 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString( | 977 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString( |
990 const std::string& str) { | 978 const std::string& str) { |
991 std::vector<SBFullHash> hashes; | 979 std::vector<SBFullHash> hashes; |
992 hashes.push_back(safe_browsing::SBFullHashForString(str)); | 980 hashes.push_back(SBFullHashForString(str)); |
993 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes); | 981 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes); |
994 } | 982 } |
995 | 983 |
996 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes( | 984 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes( |
997 SBWhitelistId whitelist_id, | 985 SBWhitelistId whitelist_id, |
998 const std::vector<SBFullHash>& hashes) { | 986 const std::vector<SBFullHash>& hashes) { |
999 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); | 987 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); |
1000 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id); | 988 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id); |
1001 if (whitelist->second) | 989 if (whitelist->second) |
1002 return true; | 990 return true; |
1003 for (std::vector<SBFullHash>::const_iterator it = hashes.begin(); | 991 for (std::vector<SBFullHash>::const_iterator it = hashes.begin(); |
1004 it != hashes.end(); ++it) { | 992 it != hashes.end(); ++it) { |
1005 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(), | 993 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(), |
1006 *it, safe_browsing::SBFullHashLess)) { | 994 *it, SBFullHashLess)) { |
1007 return true; | 995 return true; |
1008 } | 996 } |
1009 } | 997 } |
1010 return false; | 998 return false; |
1011 } | 999 } |
1012 | 1000 |
1013 // Helper to insert add-chunk entries. | 1001 // Helper to insert add-chunk entries. |
1014 void SafeBrowsingDatabaseNew::InsertAddChunk( | 1002 void SafeBrowsingDatabaseNew::InsertAddChunk(SafeBrowsingStore* store, |
1015 SafeBrowsingStore* store, | 1003 const ListType list_id, |
1016 const safe_browsing::ListType list_id, | 1004 const SBChunkData& chunk_data) { |
1017 const SBChunkData& chunk_data) { | |
1018 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1005 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1019 DCHECK(store); | 1006 DCHECK(store); |
1020 | 1007 |
1021 // The server can give us a chunk that we already have because | 1008 // The server can give us a chunk that we already have because |
1022 // it's part of a range. Don't add it again. | 1009 // it's part of a range. Don't add it again. |
1023 const int chunk_id = chunk_data.ChunkNumber(); | 1010 const int chunk_id = chunk_data.ChunkNumber(); |
1024 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); | 1011 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); |
1025 if (store->CheckAddChunk(encoded_chunk_id)) | 1012 if (store->CheckAddChunk(encoded_chunk_id)) |
1026 return; | 1013 return; |
1027 | 1014 |
1028 store->SetAddChunk(encoded_chunk_id); | 1015 store->SetAddChunk(encoded_chunk_id); |
1029 if (chunk_data.IsPrefix()) { | 1016 if (chunk_data.IsPrefix()) { |
1030 const size_t c = chunk_data.PrefixCount(); | 1017 const size_t c = chunk_data.PrefixCount(); |
1031 for (size_t i = 0; i < c; ++i) { | 1018 for (size_t i = 0; i < c; ++i) { |
1032 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); | 1019 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); |
1033 } | 1020 } |
1034 } else { | 1021 } else { |
1035 const size_t c = chunk_data.FullHashCount(); | 1022 const size_t c = chunk_data.FullHashCount(); |
1036 for (size_t i = 0; i < c; ++i) { | 1023 for (size_t i = 0; i < c; ++i) { |
1037 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); | 1024 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); |
1038 } | 1025 } |
1039 } | 1026 } |
1040 } | 1027 } |
1041 | 1028 |
1042 // Helper to insert sub-chunk entries. | 1029 // Helper to insert sub-chunk entries. |
1043 void SafeBrowsingDatabaseNew::InsertSubChunk( | 1030 void SafeBrowsingDatabaseNew::InsertSubChunk(SafeBrowsingStore* store, |
1044 SafeBrowsingStore* store, | 1031 const ListType list_id, |
1045 const safe_browsing::ListType list_id, | 1032 const SBChunkData& chunk_data) { |
1046 const SBChunkData& chunk_data) { | |
1047 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1033 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1048 DCHECK(store); | 1034 DCHECK(store); |
1049 | 1035 |
1050 // The server can give us a chunk that we already have because | 1036 // The server can give us a chunk that we already have because |
1051 // it's part of a range. Don't add it again. | 1037 // it's part of a range. Don't add it again. |
1052 const int chunk_id = chunk_data.ChunkNumber(); | 1038 const int chunk_id = chunk_data.ChunkNumber(); |
1053 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); | 1039 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); |
1054 if (store->CheckSubChunk(encoded_chunk_id)) | 1040 if (store->CheckSubChunk(encoded_chunk_id)) |
1055 return; | 1041 return; |
1056 | 1042 |
(...skipping 21 matching lines...) Expand all Loading... |
1078 const std::string& list_name, | 1064 const std::string& list_name, |
1079 const std::vector<SBChunkData*>& chunks) { | 1065 const std::vector<SBChunkData*>& chunks) { |
1080 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1066 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1081 | 1067 |
1082 if (db_state_manager_.corruption_detected() || chunks.empty()) | 1068 if (db_state_manager_.corruption_detected() || chunks.empty()) |
1083 return; | 1069 return; |
1084 | 1070 |
1085 const base::TimeTicks before = base::TimeTicks::Now(); | 1071 const base::TimeTicks before = base::TimeTicks::Now(); |
1086 | 1072 |
1087 // TODO(shess): The caller should just pass list_id. | 1073 // TODO(shess): The caller should just pass list_id. |
1088 const safe_browsing::ListType list_id = | 1074 const ListType list_id = GetListId(list_name); |
1089 safe_browsing::GetListId(list_name); | |
1090 | 1075 |
1091 SafeBrowsingStore* store = GetStore(list_id); | 1076 SafeBrowsingStore* store = GetStore(list_id); |
1092 if (!store) return; | 1077 if (!store) |
| 1078 return; |
1093 | 1079 |
1094 db_state_manager_.set_change_detected(); | 1080 db_state_manager_.set_change_detected(); |
1095 | 1081 |
1096 // TODO(shess): I believe that the list is always add or sub. Can this use | 1082 // TODO(shess): I believe that the list is always add or sub. Can this use |
1097 // that productively? | 1083 // that productively? |
1098 store->BeginChunk(); | 1084 store->BeginChunk(); |
1099 for (size_t i = 0; i < chunks.size(); ++i) { | 1085 for (size_t i = 0; i < chunks.size(); ++i) { |
1100 if (chunks[i]->IsAdd()) { | 1086 if (chunks[i]->IsAdd()) { |
1101 InsertAddChunk(store, list_id, *chunks[i]); | 1087 InsertAddChunk(store, list_id, *chunks[i]); |
1102 } else if (chunks[i]->IsSub()) { | 1088 } else if (chunks[i]->IsSub()) { |
1103 InsertSubChunk(store, list_id, *chunks[i]); | 1089 InsertSubChunk(store, list_id, *chunks[i]); |
1104 } else { | 1090 } else { |
1105 NOTREACHED(); | 1091 NOTREACHED(); |
1106 } | 1092 } |
1107 } | 1093 } |
1108 store->FinishChunk(); | 1094 store->FinishChunk(); |
1109 | 1095 |
1110 UMA_HISTOGRAM_TIMES("SB2.ChunkInsert", base::TimeTicks::Now() - before); | 1096 UMA_HISTOGRAM_TIMES("SB2.ChunkInsert", base::TimeTicks::Now() - before); |
1111 } | 1097 } |
1112 | 1098 |
1113 void SafeBrowsingDatabaseNew::DeleteChunks( | 1099 void SafeBrowsingDatabaseNew::DeleteChunks( |
1114 const std::vector<SBChunkDelete>& chunk_deletes) { | 1100 const std::vector<SBChunkDelete>& chunk_deletes) { |
1115 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1101 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1116 | 1102 |
1117 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) | 1103 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) |
1118 return; | 1104 return; |
1119 | 1105 |
1120 const std::string& list_name = chunk_deletes.front().list_name; | 1106 const std::string& list_name = chunk_deletes.front().list_name; |
1121 const safe_browsing::ListType list_id = | 1107 const ListType list_id = GetListId(list_name); |
1122 safe_browsing::GetListId(list_name); | |
1123 | 1108 |
1124 SafeBrowsingStore* store = GetStore(list_id); | 1109 SafeBrowsingStore* store = GetStore(list_id); |
1125 if (!store) return; | 1110 if (!store) |
| 1111 return; |
1126 | 1112 |
1127 db_state_manager_.set_change_detected(); | 1113 db_state_manager_.set_change_detected(); |
1128 | 1114 |
1129 for (size_t i = 0; i < chunk_deletes.size(); ++i) { | 1115 for (size_t i = 0; i < chunk_deletes.size(); ++i) { |
1130 std::vector<int> chunk_numbers; | 1116 std::vector<int> chunk_numbers; |
1131 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers); | 1117 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers); |
1132 for (size_t j = 0; j < chunk_numbers.size(); ++j) { | 1118 for (size_t j = 0; j < chunk_numbers.size(); ++j) { |
1133 const int encoded_chunk_id = EncodeChunkId(chunk_numbers[j], list_id); | 1119 const int encoded_chunk_id = EncodeChunkId(chunk_numbers[j], list_id); |
1134 if (chunk_deletes[i].is_sub_del) | 1120 if (chunk_deletes[i].is_sub_del) |
1135 store->DeleteSubChunk(encoded_chunk_id); | 1121 store->DeleteSubChunk(encoded_chunk_id); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1215 if (unwanted_software_store_ && !unwanted_software_store_->BeginUpdate()) { | 1201 if (unwanted_software_store_ && !unwanted_software_store_->BeginUpdate()) { |
1216 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN); | 1202 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN); |
1217 HandleCorruptDatabase(); | 1203 HandleCorruptDatabase(); |
1218 return false; | 1204 return false; |
1219 } | 1205 } |
1220 | 1206 |
1221 // Cached fullhash results must be cleared on every database update (whether | 1207 // Cached fullhash results must be cleared on every database update (whether |
1222 // successful or not). | 1208 // successful or not). |
1223 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache(); | 1209 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache(); |
1224 | 1210 |
1225 UpdateChunkRangesForLists(browse_store_.get(), | 1211 UpdateChunkRangesForLists(browse_store_.get(), kMalwareList, kPhishingList, |
1226 safe_browsing::kMalwareList, | |
1227 safe_browsing::kPhishingList, | |
1228 lists); | 1212 lists); |
1229 | 1213 |
1230 // NOTE(shess): |download_store_| used to contain kBinHashList, which has been | 1214 // NOTE(shess): |download_store_| used to contain kBinHashList, which has been |
1231 // deprecated. Code to delete the list from the store shows ~15k hits/day as | 1215 // deprecated. Code to delete the list from the store shows ~15k hits/day as |
1232 // of Feb 2014, so it has been removed. Everything _should_ be resilient to | 1216 // of Feb 2014, so it has been removed. Everything _should_ be resilient to |
1233 // extra data of that sort. | 1217 // extra data of that sort. |
1234 UpdateChunkRangesForList(download_store_.get(), | 1218 UpdateChunkRangesForList(download_store_.get(), kBinUrlList, lists); |
1235 safe_browsing::kBinUrlList, lists); | |
1236 | 1219 |
1237 UpdateChunkRangesForList(csd_whitelist_store_.get(), | 1220 UpdateChunkRangesForList(csd_whitelist_store_.get(), kCsdWhiteList, lists); |
1238 safe_browsing::kCsdWhiteList, lists); | |
1239 | 1221 |
1240 UpdateChunkRangesForList(download_whitelist_store_.get(), | 1222 UpdateChunkRangesForList(download_whitelist_store_.get(), kDownloadWhiteList, |
1241 safe_browsing::kDownloadWhiteList, lists); | 1223 lists); |
1242 | 1224 |
1243 UpdateChunkRangesForList(inclusion_whitelist_store_.get(), | 1225 UpdateChunkRangesForList(inclusion_whitelist_store_.get(), |
1244 safe_browsing::kInclusionWhitelist, lists); | 1226 kInclusionWhitelist, lists); |
1245 | 1227 |
1246 UpdateChunkRangesForList(extension_blacklist_store_.get(), | 1228 UpdateChunkRangesForList(extension_blacklist_store_.get(), |
1247 safe_browsing::kExtensionBlacklist, lists); | 1229 kExtensionBlacklist, lists); |
1248 | 1230 |
1249 UpdateChunkRangesForList(ip_blacklist_store_.get(), | 1231 UpdateChunkRangesForList(ip_blacklist_store_.get(), kIPBlacklist, lists); |
1250 safe_browsing::kIPBlacklist, lists); | |
1251 | 1232 |
1252 UpdateChunkRangesForList(unwanted_software_store_.get(), | 1233 UpdateChunkRangesForList(unwanted_software_store_.get(), kUnwantedUrlList, |
1253 safe_browsing::kUnwantedUrlList, | |
1254 lists); | 1234 lists); |
1255 | 1235 |
1256 db_state_manager_.reset_corruption_detected(); | 1236 db_state_manager_.reset_corruption_detected(); |
1257 db_state_manager_.reset_change_detected(); | 1237 db_state_manager_.reset_change_detected(); |
1258 return true; | 1238 return true; |
1259 } | 1239 } |
1260 | 1240 |
1261 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { | 1241 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { |
1262 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1242 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1263 | 1243 |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1472 new_prefix_set.Pass()); | 1452 new_prefix_set.Pass()); |
1473 | 1453 |
1474 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", base::TimeTicks::Now() - before); | 1454 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", base::TimeTicks::Now() - before); |
1475 | 1455 |
1476 WritePrefixSet(db_filename, prefix_set_id, write_failure_type); | 1456 WritePrefixSet(db_filename, prefix_set_id, write_failure_type); |
1477 | 1457 |
1478 // Gather statistics. | 1458 // Gather statistics. |
1479 if (got_counters && metric->GetIOCounters(&io_after)) { | 1459 if (got_counters && metric->GetIOCounters(&io_after)) { |
1480 UMA_HISTOGRAM_COUNTS("SB2.BuildReadKilobytes", | 1460 UMA_HISTOGRAM_COUNTS("SB2.BuildReadKilobytes", |
1481 static_cast<int>(io_after.ReadTransferCount - | 1461 static_cast<int>(io_after.ReadTransferCount - |
1482 io_before.ReadTransferCount) / 1024); | 1462 io_before.ReadTransferCount) / |
| 1463 1024); |
1483 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteKilobytes", | 1464 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteKilobytes", |
1484 static_cast<int>(io_after.WriteTransferCount - | 1465 static_cast<int>(io_after.WriteTransferCount - |
1485 io_before.WriteTransferCount) / 1024); | 1466 io_before.WriteTransferCount) / |
| 1467 1024); |
1486 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", | 1468 UMA_HISTOGRAM_COUNTS("SB2.BuildReadOperations", |
1487 static_cast<int>(io_after.ReadOperationCount - | 1469 static_cast<int>(io_after.ReadOperationCount - |
1488 io_before.ReadOperationCount)); | 1470 io_before.ReadOperationCount)); |
1489 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", | 1471 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", |
1490 static_cast<int>(io_after.WriteOperationCount - | 1472 static_cast<int>(io_after.WriteOperationCount - |
1491 io_before.WriteOperationCount)); | 1473 io_before.WriteOperationCount)); |
1492 } | 1474 } |
1493 | 1475 |
1494 RecordFileSizeHistogram(db_filename); | 1476 RecordFileSizeHistogram(db_filename); |
1495 | 1477 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1595 // if something looks wrong. | 1577 // if something looks wrong. |
1596 | 1578 |
1597 const bool r1 = browse_store_->Delete(); | 1579 const bool r1 = browse_store_->Delete(); |
1598 if (!r1) | 1580 if (!r1) |
1599 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1581 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1600 | 1582 |
1601 const bool r2 = download_store_.get() ? download_store_->Delete() : true; | 1583 const bool r2 = download_store_.get() ? download_store_->Delete() : true; |
1602 if (!r2) | 1584 if (!r2) |
1603 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1585 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1604 | 1586 |
1605 const bool r3 = csd_whitelist_store_.get() ? | 1587 const bool r3 = |
1606 csd_whitelist_store_->Delete() : true; | 1588 csd_whitelist_store_.get() ? csd_whitelist_store_->Delete() : true; |
1607 if (!r3) | 1589 if (!r3) |
1608 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1590 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1609 | 1591 |
1610 const bool r4 = download_whitelist_store_.get() ? | 1592 const bool r4 = download_whitelist_store_.get() |
1611 download_whitelist_store_->Delete() : true; | 1593 ? download_whitelist_store_->Delete() |
| 1594 : true; |
1612 if (!r4) | 1595 if (!r4) |
1613 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1596 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1614 | 1597 |
1615 const bool r5 = inclusion_whitelist_store_.get() ? | 1598 const bool r5 = inclusion_whitelist_store_.get() |
1616 inclusion_whitelist_store_->Delete() : true; | 1599 ? inclusion_whitelist_store_->Delete() |
| 1600 : true; |
1617 if (!r5) | 1601 if (!r5) |
1618 RecordFailure(FAILURE_DATABASE_STORE_DELETE); | 1602 RecordFailure(FAILURE_DATABASE_STORE_DELETE); |
1619 | 1603 |
1620 const base::FilePath browse_filename = | 1604 const base::FilePath browse_filename = |
1621 BrowseDBFilename(db_state_manager_.filename_base()); | 1605 BrowseDBFilename(db_state_manager_.filename_base()); |
1622 const base::FilePath bloom_filter_filename = | 1606 const base::FilePath bloom_filter_filename = |
1623 BloomFilterForFilename(browse_filename); | 1607 BloomFilterForFilename(browse_filename); |
1624 const bool r6 = base::DeleteFile(bloom_filter_filename, false); | 1608 const bool r6 = base::DeleteFile(bloom_filter_filename, false); |
1625 if (!r6) | 1609 if (!r6) |
1626 RecordFailure(FAILURE_DATABASE_FILTER_DELETE); | 1610 RecordFailure(FAILURE_DATABASE_FILTER_DELETE); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1690 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); | 1674 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); |
1691 return; | 1675 return; |
1692 } | 1676 } |
1693 | 1677 |
1694 std::vector<SBFullHash> new_whitelist; | 1678 std::vector<SBFullHash> new_whitelist; |
1695 new_whitelist.reserve(full_hashes.size()); | 1679 new_whitelist.reserve(full_hashes.size()); |
1696 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); | 1680 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); |
1697 it != full_hashes.end(); ++it) { | 1681 it != full_hashes.end(); ++it) { |
1698 new_whitelist.push_back(it->full_hash); | 1682 new_whitelist.push_back(it->full_hash); |
1699 } | 1683 } |
1700 std::sort(new_whitelist.begin(), new_whitelist.end(), | 1684 std::sort(new_whitelist.begin(), new_whitelist.end(), SBFullHashLess); |
1701 safe_browsing::SBFullHashLess); | |
1702 | 1685 |
1703 SBFullHash kill_switch = safe_browsing::SBFullHashForString( | 1686 SBFullHash kill_switch = SBFullHashForString(kWhitelistKillSwitchUrl); |
1704 kWhitelistKillSwitchUrl); | |
1705 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), | 1687 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), |
1706 kill_switch, safe_browsing::SBFullHashLess)) { | 1688 kill_switch, SBFullHashLess)) { |
1707 // The kill switch is whitelisted hence we whitelist all URLs. | 1689 // The kill switch is whitelisted hence we whitelist all URLs. |
1708 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); | 1690 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); |
1709 } else { | 1691 } else { |
1710 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, | 1692 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, |
1711 &new_whitelist); | 1693 &new_whitelist); |
1712 } | 1694 } |
1713 } | 1695 } |
1714 | 1696 |
1715 void SafeBrowsingDatabaseNew::LoadIpBlacklist( | 1697 void SafeBrowsingDatabaseNew::LoadIpBlacklist( |
1716 const std::vector<SBAddFullHash>& full_hashes) { | 1698 const std::vector<SBAddFullHash>& full_hashes) { |
1717 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1699 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1718 | 1700 |
1719 IPBlacklist new_blacklist; | 1701 IPBlacklist new_blacklist; |
1720 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); | 1702 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); |
1721 it != full_hashes.end(); | 1703 it != full_hashes.end(); ++it) { |
1722 ++it) { | |
1723 const char* full_hash = it->full_hash.full_hash; | 1704 const char* full_hash = it->full_hash.full_hash; |
1724 DCHECK_EQ(crypto::kSHA256Length, arraysize(it->full_hash.full_hash)); | 1705 DCHECK_EQ(crypto::kSHA256Length, arraysize(it->full_hash.full_hash)); |
1725 // The format of the IP blacklist is: | 1706 // The format of the IP blacklist is: |
1726 // SHA-1(IPv6 prefix) + uint8(prefix size) + 11 unused bytes. | 1707 // SHA-1(IPv6 prefix) + uint8(prefix size) + 11 unused bytes. |
1727 std::string hashed_ip_prefix(full_hash, base::kSHA1Length); | 1708 std::string hashed_ip_prefix(full_hash, base::kSHA1Length); |
1728 size_t prefix_size = static_cast<uint8>(full_hash[base::kSHA1Length]); | 1709 size_t prefix_size = static_cast<uint8>(full_hash[base::kSHA1Length]); |
1729 if (prefix_size > kMaxIpPrefixSize || prefix_size < kMinIpPrefixSize) { | 1710 if (prefix_size > kMaxIpPrefixSize || prefix_size < kMinIpPrefixSize) { |
1730 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_INVALID); | 1711 RecordFailure(FAILURE_IP_BLACKLIST_UPDATE_INVALID); |
1731 new_blacklist.clear(); // Load empty blacklist. | 1712 new_blacklist.clear(); // Load empty blacklist. |
1732 break; | 1713 break; |
(...skipping 13 matching lines...) Expand all Loading... |
1746 << " prefix_size:" << prefix_size | 1727 << " prefix_size:" << prefix_size |
1747 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(), | 1728 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(), |
1748 hashed_ip_prefix.size()); | 1729 hashed_ip_prefix.size()); |
1749 new_blacklist[mask].insert(hashed_ip_prefix); | 1730 new_blacklist[mask].insert(hashed_ip_prefix); |
1750 } | 1731 } |
1751 | 1732 |
1752 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist); | 1733 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist); |
1753 } | 1734 } |
1754 | 1735 |
1755 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { | 1736 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { |
1756 SBFullHash malware_kill_switch = safe_browsing::SBFullHashForString( | 1737 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl); |
1757 kMalwareIPKillSwitchUrl); | |
1758 std::vector<SBFullHash> full_hashes; | 1738 std::vector<SBFullHash> full_hashes; |
1759 full_hashes.push_back(malware_kill_switch); | 1739 full_hashes.push_back(malware_kill_switch); |
1760 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); | 1740 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); |
1761 } | 1741 } |
1762 | 1742 |
1763 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { | 1743 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { |
1764 return state_manager_.BeginReadTransaction() | 1744 return state_manager_.BeginReadTransaction() |
1765 ->GetSBWhitelist(SBWhitelistId::CSD) | 1745 ->GetSBWhitelist(SBWhitelistId::CSD) |
1766 ->second; | 1746 ->second; |
1767 } | 1747 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1820 else | 1800 else |
1821 NOTREACHED(); // Add support for new lists above. | 1801 NOTREACHED(); // Add support for new lists above. |
1822 | 1802 |
1823 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. | 1803 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. |
1824 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( | 1804 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( |
1825 histogram_name, 1, 1000000, 50, | 1805 histogram_name, 1, 1000000, 50, |
1826 base::HistogramBase::kUmaTargetedHistogramFlag); | 1806 base::HistogramBase::kUmaTargetedHistogramFlag); |
1827 | 1807 |
1828 histogram_pointer->Add(file_size_kilobytes); | 1808 histogram_pointer->Add(file_size_kilobytes); |
1829 } | 1809 } |
| 1810 |
| 1811 } // namespace safe_browsing |
OLD | NEW |