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

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_database.cc

Issue 1420053005: Move code in components/safe_browsing_db and chrome/browser/s_b/ under the safe_browsing namespace. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@02_components_move
Patch Set: Merging the latest changes from trunk. Created 5 years, 1 month 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
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
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;
35 using safe_browsing::PrefixSetBuilder;
36 34
37 namespace { 35 namespace safe_browsing {
36
37 class PrefixSet;
38 class PrefixSetBuilder;
38 39
39 // Filename suffix for the bloom filter. 40 // Filename suffix for the bloom filter.
Nathan Parker 2015/11/05 22:00:53 Keep these in anon-namespace.
vakh (old account. dont use) 2015/11/07 01:22:56 Done.
40 const base::FilePath::CharType kBloomFilterFileSuffix[] = 41 const base::FilePath::CharType kBloomFilterFileSuffix[] =
41 FILE_PATH_LITERAL(" Filter 2"); 42 FILE_PATH_LITERAL(" Filter 2");
42 // Filename suffix for the prefix set. 43 // Filename suffix for the prefix set.
43 const base::FilePath::CharType kPrefixSetFileSuffix[] = 44 const base::FilePath::CharType kPrefixSetFileSuffix[] =
44 FILE_PATH_LITERAL(" Prefix Set"); 45 FILE_PATH_LITERAL(" Prefix Set");
45 // Filename suffix for download store. 46 // Filename suffix for download store.
46 const base::FilePath::CharType kDownloadDBFile[] = 47 const base::FilePath::CharType kDownloadDBFile[] =
47 FILE_PATH_LITERAL(" Download"); 48 FILE_PATH_LITERAL(" Download");
48 // Filename suffix for client-side phishing detection whitelist store. 49 // Filename suffix for client-side phishing detection whitelist store.
49 const base::FilePath::CharType kCsdWhitelistDBFile[] = 50 const base::FilePath::CharType kCsdWhitelistDBFile[] =
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 // TODO(lzheng): It was reasonable when database is saved in sqlite, but 101 // 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 102 // there should be better ways to save chunk_id and list_id after we use
102 // SafeBrowsingStoreFile. 103 // SafeBrowsingStoreFile.
103 int GetListIdBit(const int encoded_chunk_id) { 104 int GetListIdBit(const int encoded_chunk_id) {
104 return encoded_chunk_id & 1; 105 return encoded_chunk_id & 1;
105 } 106 }
106 int DecodeChunkId(int encoded_chunk_id) { 107 int DecodeChunkId(int encoded_chunk_id) {
107 return encoded_chunk_id >> 1; 108 return encoded_chunk_id >> 1;
108 } 109 }
109 int EncodeChunkId(const int chunk, const int list_id) { 110 int EncodeChunkId(const int chunk, const int list_id) {
110 DCHECK_NE(list_id, safe_browsing::INVALID); 111 DCHECK_NE(list_id, INVALID);
Nathan Parker 2015/11/05 22:00:53 (This is a terribly generically named enum val. W
111 return chunk << 1 | list_id % 2; 112 return chunk << 1 | list_id % 2;
112 } 113 }
113 114
114 // Generate the set of full hashes to check for |url|. If 115 // Generate the set of full hashes to check for |url|. If
115 // |include_whitelist_hashes| is true we will generate additional path-prefixes 116 // |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 117 // 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 118 // 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. 119 // other lists. We'll also always add a pattern for the empty path.
119 // TODO(shess): This function is almost the same as 120 // TODO(shess): This function is almost the same as
120 // |CompareFullHashes()| in safe_browsing_util.cc, except that code 121 // |CompareFullHashes()| in safe_browsing_util.cc, except that code
121 // does an early exit on match. Since match should be the infrequent 122 // does an early exit on match. Since match should be the infrequent
122 // case (phishing or malware found), consider combining this function 123 // case (phishing or malware found), consider combining this function
123 // with that one. 124 // with that one.
124 void UrlToFullHashes(const GURL& url, 125 void UrlToFullHashes(const GURL& url,
125 bool include_whitelist_hashes, 126 bool include_whitelist_hashes,
126 std::vector<SBFullHash>* full_hashes) { 127 std::vector<SBFullHash>* full_hashes) {
127 std::vector<std::string> hosts; 128 std::vector<std::string> hosts;
128 if (url.HostIsIPAddress()) { 129 if (url.HostIsIPAddress()) {
129 hosts.push_back(url.host()); 130 hosts.push_back(url.host());
130 } else { 131 } else {
131 safe_browsing::GenerateHostsToCheck(url, &hosts); 132 GenerateHostsToCheck(url, &hosts);
132 } 133 }
133 134
134 std::vector<std::string> paths; 135 std::vector<std::string> paths;
135 safe_browsing::GeneratePathsToCheck(url, &paths); 136 GeneratePathsToCheck(url, &paths);
136 137
137 for (size_t i = 0; i < hosts.size(); ++i) { 138 for (size_t i = 0; i < hosts.size(); ++i) {
138 for (size_t j = 0; j < paths.size(); ++j) { 139 for (size_t j = 0; j < paths.size(); ++j) {
139 const std::string& path = paths[j]; 140 const std::string& path = paths[j];
140 full_hashes->push_back(safe_browsing::SBFullHashForString( 141 full_hashes->push_back(SBFullHashForString(
141 hosts[i] + path)); 142 hosts[i] + path));
142 143
143 // We may have /foo as path-prefix in the whitelist which should 144 // 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 145 // also match with /foo/bar and /foo?bar. Hence, for every path
145 // that ends in '/' we also add the path without the slash. 146 // that ends in '/' we also add the path without the slash.
146 if (include_whitelist_hashes && 147 if (include_whitelist_hashes &&
147 path.size() > 1 && 148 path.size() > 1 &&
148 path[path.size() - 1] == '/') { 149 path[path.size() - 1] == '/') {
149 full_hashes->push_back( 150 full_hashes->push_back(
150 safe_browsing::SBFullHashForString( 151 SBFullHashForString(
151 hosts[i] + path.substr(0, path.size() - 1))); 152 hosts[i] + path.substr(0, path.size() - 1)));
152 } 153 }
153 } 154 }
154 } 155 }
155 } 156 }
156 157
157 // Helper function to compare addprefixes in |store| with |prefixes|. 158 // Helper function to compare addprefixes in |store| with |prefixes|.
158 // The |list_bit| indicates which list (url or hash) to compare. 159 // The |list_bit| indicates which list (url or hash) to compare.
159 // 160 //
160 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain 161 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 // Always decode 2 ranges, even if only the first one is expected. 226 // 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| 227 // The loop below will only load as many into |lists| as |listnames|
227 // indicates. 228 // indicates.
228 std::vector<std::string> adds(2); 229 std::vector<std::string> adds(2);
229 std::vector<std::string> subs(2); 230 std::vector<std::string> subs(2);
230 GetChunkRanges(add_chunks, &adds); 231 GetChunkRanges(add_chunks, &adds);
231 GetChunkRanges(sub_chunks, &subs); 232 GetChunkRanges(sub_chunks, &subs);
232 233
233 for (size_t i = 0; i < listnames.size(); ++i) { 234 for (size_t i = 0; i < listnames.size(); ++i) {
234 const std::string& listname = listnames[i]; 235 const std::string& listname = listnames[i];
235 DCHECK_EQ(safe_browsing::GetListId(listname) % 2, 236 DCHECK_EQ(GetListId(listname) % 2,
236 static_cast<int>(i % 2)); 237 static_cast<int>(i % 2));
237 DCHECK_NE(safe_browsing::GetListId(listname), 238 DCHECK_NE(GetListId(listname),
238 safe_browsing::INVALID); 239 INVALID);
239 lists->push_back(SBListChunkRanges(listname)); 240 lists->push_back(SBListChunkRanges(listname));
240 lists->back().adds.swap(adds[i]); 241 lists->back().adds.swap(adds[i]);
241 lists->back().subs.swap(subs[i]); 242 lists->back().subs.swap(subs[i]);
242 } 243 }
243 } 244 }
244 245
245 void UpdateChunkRangesForLists(SafeBrowsingStore* store, 246 void UpdateChunkRangesForLists(SafeBrowsingStore* store,
246 const std::string& listname0, 247 const std::string& listname0,
247 const std::string& listname1, 248 const std::string& listname1,
248 std::vector<SBListChunkRanges>* lists) { 249 std::vector<SBListChunkRanges>* lists) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 // Remove expired entries. 285 // Remove expired entries.
285 SBCachedFullHashResult& cached_result = citer->second; 286 SBCachedFullHashResult& cached_result = citer->second;
286 if (cached_result.expire_after <= expire_base) { 287 if (cached_result.expire_after <= expire_base) {
287 cache->erase(citer); 288 cache->erase(citer);
288 return false; 289 return false;
289 } 290 }
290 291
291 // Find full-hash matches. 292 // Find full-hash matches.
292 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes; 293 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes;
293 for (size_t i = 0; i < cached_hashes.size(); ++i) { 294 for (size_t i = 0; i < cached_hashes.size(); ++i) {
294 if (safe_browsing::SBFullHashEqual(full_hash, cached_hashes[i].hash)) 295 if (SBFullHashEqual(full_hash, cached_hashes[i].hash))
295 results->push_back(cached_hashes[i]); 296 results->push_back(cached_hashes[i]);
296 } 297 }
297 298
298 return true; 299 return true;
299 } 300 }
300 301
301 SafeBrowsingStoreFile* CreateStore( 302 SafeBrowsingStoreFile* CreateStore(
302 bool enable, 303 bool enable,
303 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { 304 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
304 if (!enable) 305 if (!enable)
305 return nullptr; 306 return nullptr;
306 return new SafeBrowsingStoreFile(task_runner); 307 return new SafeBrowsingStoreFile(task_runner);
307 } 308 }
308 309
309 } // namespace
310
311 // The default SafeBrowsingDatabaseFactory. 310 // The default SafeBrowsingDatabaseFactory.
312 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory { 311 class SafeBrowsingDatabaseFactoryImpl : public SafeBrowsingDatabaseFactory {
313 public: 312 public:
314 SafeBrowsingDatabase* CreateSafeBrowsingDatabase( 313 SafeBrowsingDatabase* CreateSafeBrowsingDatabase(
315 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 314 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
316 bool enable_download_protection, 315 bool enable_download_protection,
317 bool enable_client_side_whitelist, 316 bool enable_client_side_whitelist,
318 bool enable_download_whitelist, 317 bool enable_download_whitelist,
319 bool enable_extension_blacklist, 318 bool enable_extension_blacklist,
320 bool enable_ip_blacklist, 319 bool enable_ip_blacklist,
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 UrlToFullHashes(urls[i], false, &full_hashes); 439 UrlToFullHashes(urls[i], false, &full_hashes);
441 440
442 for (size_t i = 0; i < full_hashes.size(); ++i) 441 for (size_t i = 0; i < full_hashes.size(); ++i)
443 prefixes->push_back(full_hashes[i].prefix); 442 prefixes->push_back(full_hashes[i].prefix);
444 } 443 }
445 444
446 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { 445 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) {
447 // Stores are not thread safe. 446 // Stores are not thread safe.
448 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 447 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
449 448
450 if (list_id == safe_browsing::PHISH || 449 if (list_id == PHISH ||
451 list_id == safe_browsing::MALWARE) { 450 list_id == MALWARE) {
452 return browse_store_.get(); 451 return browse_store_.get();
453 } else if (list_id == safe_browsing::BINURL) { 452 } else if (list_id == BINURL) {
454 return download_store_.get(); 453 return download_store_.get();
455 } else if (list_id == safe_browsing::CSDWHITELIST) { 454 } else if (list_id == CSDWHITELIST) {
456 return csd_whitelist_store_.get(); 455 return csd_whitelist_store_.get();
457 } else if (list_id == safe_browsing::DOWNLOADWHITELIST) { 456 } else if (list_id == DOWNLOADWHITELIST) {
458 return download_whitelist_store_.get(); 457 return download_whitelist_store_.get();
459 } else if (list_id == safe_browsing::INCLUSIONWHITELIST) { 458 } else if (list_id == INCLUSIONWHITELIST) {
460 return inclusion_whitelist_store_.get(); 459 return inclusion_whitelist_store_.get();
461 } else if (list_id == safe_browsing::EXTENSIONBLACKLIST) { 460 } else if (list_id == EXTENSIONBLACKLIST) {
462 return extension_blacklist_store_.get(); 461 return extension_blacklist_store_.get();
463 } else if (list_id == safe_browsing::IPBLACKLIST) { 462 } else if (list_id == IPBLACKLIST) {
464 return ip_blacklist_store_.get(); 463 return ip_blacklist_store_.get();
465 } else if (list_id == safe_browsing::UNWANTEDURL) { 464 } else if (list_id == UNWANTEDURL) {
466 return unwanted_software_store_.get(); 465 return unwanted_software_store_.get();
467 } 466 }
468 return NULL; 467 return NULL;
469 } 468 }
470 469
471 // static 470 // static
472 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) { 471 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) {
473 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type, 472 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type,
474 FAILURE_DATABASE_MAX); 473 FAILURE_DATABASE_MAX);
475 } 474 }
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes( 909 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes(
911 const std::vector<SBPrefix>& prefixes, 910 const std::vector<SBPrefix>& prefixes,
912 std::vector<SBPrefix>* prefix_hits) { 911 std::vector<SBPrefix>* prefix_hits) {
913 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 912 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
914 913
915 // Ignore this check when download checking is not enabled. 914 // Ignore this check when download checking is not enabled.
916 if (!download_store_.get()) 915 if (!download_store_.get())
917 return false; 916 return false;
918 917
919 return MatchAddPrefixes(download_store_.get(), 918 return MatchAddPrefixes(download_store_.get(),
920 safe_browsing::BINURL % 2, 919 BINURL % 2,
921 prefixes, 920 prefixes,
922 prefix_hits); 921 prefix_hits);
923 } 922 }
924 923
925 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { 924 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) {
926 std::vector<SBFullHash> full_hashes; 925 std::vector<SBFullHash> full_hashes;
927 UrlToFullHashes(url, true, &full_hashes); 926 UrlToFullHashes(url, true, &full_hashes);
928 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); 927 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes);
929 } 928 }
930 929
(...skipping 11 matching lines...) Expand all
942 941
943 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( 942 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes(
944 const std::vector<SBPrefix>& prefixes, 943 const std::vector<SBPrefix>& prefixes,
945 std::vector<SBPrefix>* prefix_hits) { 944 std::vector<SBPrefix>* prefix_hits) {
946 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 945 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
947 946
948 if (!extension_blacklist_store_) 947 if (!extension_blacklist_store_)
949 return false; 948 return false;
950 949
951 return MatchAddPrefixes(extension_blacklist_store_.get(), 950 return MatchAddPrefixes(extension_blacklist_store_.get(),
952 safe_browsing::EXTENSIONBLACKLIST % 2, 951 EXTENSIONBLACKLIST % 2,
953 prefixes, 952 prefixes,
954 prefix_hits); 953 prefix_hits);
955 } 954 }
956 955
957 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { 956 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) {
958 net::IPAddressNumber ip_number; 957 net::IPAddressNumber ip_number;
959 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) 958 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number))
960 return false; 959 return false;
961 if (ip_number.size() == net::kIPv4AddressSize) 960 if (ip_number.size() == net::kIPv4AddressSize)
962 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); 961 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number);
(...skipping 19 matching lines...) Expand all
982 if (it->second.count(hash) > 0) { 981 if (it->second.count(hash) > 0) {
983 return true; 982 return true;
984 } 983 }
985 } 984 }
986 return false; 985 return false;
987 } 986 }
988 987
989 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString( 988 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString(
990 const std::string& str) { 989 const std::string& str) {
991 std::vector<SBFullHash> hashes; 990 std::vector<SBFullHash> hashes;
992 hashes.push_back(safe_browsing::SBFullHashForString(str)); 991 hashes.push_back(SBFullHashForString(str));
993 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes); 992 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes);
994 } 993 }
995 994
996 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes( 995 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes(
997 SBWhitelistId whitelist_id, 996 SBWhitelistId whitelist_id,
998 const std::vector<SBFullHash>& hashes) { 997 const std::vector<SBFullHash>& hashes) {
999 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); 998 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction();
1000 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id); 999 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id);
1001 if (whitelist->second) 1000 if (whitelist->second)
1002 return true; 1001 return true;
1003 for (std::vector<SBFullHash>::const_iterator it = hashes.begin(); 1002 for (std::vector<SBFullHash>::const_iterator it = hashes.begin();
1004 it != hashes.end(); ++it) { 1003 it != hashes.end(); ++it) {
1005 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(), 1004 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(),
1006 *it, safe_browsing::SBFullHashLess)) { 1005 *it, SBFullHashLess)) {
1007 return true; 1006 return true;
1008 } 1007 }
1009 } 1008 }
1010 return false; 1009 return false;
1011 } 1010 }
1012 1011
1013 // Helper to insert add-chunk entries. 1012 // Helper to insert add-chunk entries.
1014 void SafeBrowsingDatabaseNew::InsertAddChunk( 1013 void SafeBrowsingDatabaseNew::InsertAddChunk(
1015 SafeBrowsingStore* store, 1014 SafeBrowsingStore* store,
1016 const safe_browsing::ListType list_id, 1015 const ListType list_id,
1017 const SBChunkData& chunk_data) { 1016 const SBChunkData& chunk_data) {
1018 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 1017 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1019 DCHECK(store); 1018 DCHECK(store);
1020 1019
1021 // The server can give us a chunk that we already have because 1020 // The server can give us a chunk that we already have because
1022 // it's part of a range. Don't add it again. 1021 // it's part of a range. Don't add it again.
1023 const int chunk_id = chunk_data.ChunkNumber(); 1022 const int chunk_id = chunk_data.ChunkNumber();
1024 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); 1023 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id);
1025 if (store->CheckAddChunk(encoded_chunk_id)) 1024 if (store->CheckAddChunk(encoded_chunk_id))
1026 return; 1025 return;
1027 1026
1028 store->SetAddChunk(encoded_chunk_id); 1027 store->SetAddChunk(encoded_chunk_id);
1029 if (chunk_data.IsPrefix()) { 1028 if (chunk_data.IsPrefix()) {
1030 const size_t c = chunk_data.PrefixCount(); 1029 const size_t c = chunk_data.PrefixCount();
1031 for (size_t i = 0; i < c; ++i) { 1030 for (size_t i = 0; i < c; ++i) {
1032 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); 1031 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i));
1033 } 1032 }
1034 } else { 1033 } else {
1035 const size_t c = chunk_data.FullHashCount(); 1034 const size_t c = chunk_data.FullHashCount();
1036 for (size_t i = 0; i < c; ++i) { 1035 for (size_t i = 0; i < c; ++i) {
1037 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); 1036 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i));
1038 } 1037 }
1039 } 1038 }
1040 } 1039 }
1041 1040
1042 // Helper to insert sub-chunk entries. 1041 // Helper to insert sub-chunk entries.
1043 void SafeBrowsingDatabaseNew::InsertSubChunk( 1042 void SafeBrowsingDatabaseNew::InsertSubChunk(
1044 SafeBrowsingStore* store, 1043 SafeBrowsingStore* store,
1045 const safe_browsing::ListType list_id, 1044 const ListType list_id,
1046 const SBChunkData& chunk_data) { 1045 const SBChunkData& chunk_data) {
1047 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 1046 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1048 DCHECK(store); 1047 DCHECK(store);
1049 1048
1050 // The server can give us a chunk that we already have because 1049 // The server can give us a chunk that we already have because
1051 // it's part of a range. Don't add it again. 1050 // it's part of a range. Don't add it again.
1052 const int chunk_id = chunk_data.ChunkNumber(); 1051 const int chunk_id = chunk_data.ChunkNumber();
1053 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); 1052 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id);
1054 if (store->CheckSubChunk(encoded_chunk_id)) 1053 if (store->CheckSubChunk(encoded_chunk_id))
1055 return; 1054 return;
(...skipping 22 matching lines...) Expand all
1078 const std::string& list_name, 1077 const std::string& list_name,
1079 const std::vector<SBChunkData*>& chunks) { 1078 const std::vector<SBChunkData*>& chunks) {
1080 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 1079 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1081 1080
1082 if (db_state_manager_.corruption_detected() || chunks.empty()) 1081 if (db_state_manager_.corruption_detected() || chunks.empty())
1083 return; 1082 return;
1084 1083
1085 const base::TimeTicks before = base::TimeTicks::Now(); 1084 const base::TimeTicks before = base::TimeTicks::Now();
1086 1085
1087 // TODO(shess): The caller should just pass list_id. 1086 // TODO(shess): The caller should just pass list_id.
1088 const safe_browsing::ListType list_id = 1087 const ListType list_id =
1089 safe_browsing::GetListId(list_name); 1088 GetListId(list_name);
1090 1089
1091 SafeBrowsingStore* store = GetStore(list_id); 1090 SafeBrowsingStore* store = GetStore(list_id);
1092 if (!store) return; 1091 if (!store) return;
1093 1092
1094 db_state_manager_.set_change_detected(); 1093 db_state_manager_.set_change_detected();
1095 1094
1096 // TODO(shess): I believe that the list is always add or sub. Can this use 1095 // TODO(shess): I believe that the list is always add or sub. Can this use
1097 // that productively? 1096 // that productively?
1098 store->BeginChunk(); 1097 store->BeginChunk();
1099 for (size_t i = 0; i < chunks.size(); ++i) { 1098 for (size_t i = 0; i < chunks.size(); ++i) {
(...skipping 11 matching lines...) Expand all
1111 } 1110 }
1112 1111
1113 void SafeBrowsingDatabaseNew::DeleteChunks( 1112 void SafeBrowsingDatabaseNew::DeleteChunks(
1114 const std::vector<SBChunkDelete>& chunk_deletes) { 1113 const std::vector<SBChunkDelete>& chunk_deletes) {
1115 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 1114 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1116 1115
1117 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) 1116 if (db_state_manager_.corruption_detected() || chunk_deletes.empty())
1118 return; 1117 return;
1119 1118
1120 const std::string& list_name = chunk_deletes.front().list_name; 1119 const std::string& list_name = chunk_deletes.front().list_name;
1121 const safe_browsing::ListType list_id = 1120 const ListType list_id =
1122 safe_browsing::GetListId(list_name); 1121 GetListId(list_name);
1123 1122
1124 SafeBrowsingStore* store = GetStore(list_id); 1123 SafeBrowsingStore* store = GetStore(list_id);
1125 if (!store) return; 1124 if (!store) return;
1126 1125
1127 db_state_manager_.set_change_detected(); 1126 db_state_manager_.set_change_detected();
1128 1127
1129 for (size_t i = 0; i < chunk_deletes.size(); ++i) { 1128 for (size_t i = 0; i < chunk_deletes.size(); ++i) {
1130 std::vector<int> chunk_numbers; 1129 std::vector<int> chunk_numbers;
1131 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers); 1130 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers);
1132 for (size_t j = 0; j < chunk_numbers.size(); ++j) { 1131 for (size_t j = 0; j < chunk_numbers.size(); ++j) {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1216 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN); 1215 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN);
1217 HandleCorruptDatabase(); 1216 HandleCorruptDatabase();
1218 return false; 1217 return false;
1219 } 1218 }
1220 1219
1221 // Cached fullhash results must be cleared on every database update (whether 1220 // Cached fullhash results must be cleared on every database update (whether
1222 // successful or not). 1221 // successful or not).
1223 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache(); 1222 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache();
1224 1223
1225 UpdateChunkRangesForLists(browse_store_.get(), 1224 UpdateChunkRangesForLists(browse_store_.get(),
1226 safe_browsing::kMalwareList, 1225 kMalwareList,
1227 safe_browsing::kPhishingList, 1226 kPhishingList,
1228 lists); 1227 lists);
1229 1228
1230 // NOTE(shess): |download_store_| used to contain kBinHashList, which has been 1229 // 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 1230 // 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 1231 // of Feb 2014, so it has been removed. Everything _should_ be resilient to
1233 // extra data of that sort. 1232 // extra data of that sort.
1234 UpdateChunkRangesForList(download_store_.get(), 1233 UpdateChunkRangesForList(download_store_.get(),
1235 safe_browsing::kBinUrlList, lists); 1234 kBinUrlList, lists);
1236 1235
1237 UpdateChunkRangesForList(csd_whitelist_store_.get(), 1236 UpdateChunkRangesForList(csd_whitelist_store_.get(),
1238 safe_browsing::kCsdWhiteList, lists); 1237 kCsdWhiteList, lists);
1239 1238
1240 UpdateChunkRangesForList(download_whitelist_store_.get(), 1239 UpdateChunkRangesForList(download_whitelist_store_.get(),
1241 safe_browsing::kDownloadWhiteList, lists); 1240 kDownloadWhiteList, lists);
1242 1241
1243 UpdateChunkRangesForList(inclusion_whitelist_store_.get(), 1242 UpdateChunkRangesForList(inclusion_whitelist_store_.get(),
1244 safe_browsing::kInclusionWhitelist, lists); 1243 kInclusionWhitelist, lists);
1245 1244
1246 UpdateChunkRangesForList(extension_blacklist_store_.get(), 1245 UpdateChunkRangesForList(extension_blacklist_store_.get(),
1247 safe_browsing::kExtensionBlacklist, lists); 1246 kExtensionBlacklist, lists);
1248 1247
1249 UpdateChunkRangesForList(ip_blacklist_store_.get(), 1248 UpdateChunkRangesForList(ip_blacklist_store_.get(),
1250 safe_browsing::kIPBlacklist, lists); 1249 kIPBlacklist, lists);
1251 1250
1252 UpdateChunkRangesForList(unwanted_software_store_.get(), 1251 UpdateChunkRangesForList(unwanted_software_store_.get(),
1253 safe_browsing::kUnwantedUrlList, 1252 kUnwantedUrlList,
1254 lists); 1253 lists);
1255 1254
1256 db_state_manager_.reset_corruption_detected(); 1255 db_state_manager_.reset_corruption_detected();
1257 db_state_manager_.reset_change_detected(); 1256 db_state_manager_.reset_change_detected();
1258 return true; 1257 return true;
1259 } 1258 }
1260 1259
1261 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { 1260 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) {
1262 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 1261 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
1263 1262
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
1698 return; 1697 return;
1699 } 1698 }
1700 1699
1701 std::vector<SBFullHash> new_whitelist; 1700 std::vector<SBFullHash> new_whitelist;
1702 new_whitelist.reserve(full_hashes.size()); 1701 new_whitelist.reserve(full_hashes.size());
1703 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); 1702 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin();
1704 it != full_hashes.end(); ++it) { 1703 it != full_hashes.end(); ++it) {
1705 new_whitelist.push_back(it->full_hash); 1704 new_whitelist.push_back(it->full_hash);
1706 } 1705 }
1707 std::sort(new_whitelist.begin(), new_whitelist.end(), 1706 std::sort(new_whitelist.begin(), new_whitelist.end(),
1708 safe_browsing::SBFullHashLess); 1707 SBFullHashLess);
1709 1708
1710 SBFullHash kill_switch = safe_browsing::SBFullHashForString( 1709 SBFullHash kill_switch = SBFullHashForString(
1711 kWhitelistKillSwitchUrl); 1710 kWhitelistKillSwitchUrl);
1712 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), 1711 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(),
1713 kill_switch, safe_browsing::SBFullHashLess)) { 1712 kill_switch, SBFullHashLess)) {
1714 // The kill switch is whitelisted hence we whitelist all URLs. 1713 // The kill switch is whitelisted hence we whitelist all URLs.
1715 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); 1714 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id);
1716 } else { 1715 } else {
1717 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, 1716 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id,
1718 &new_whitelist); 1717 &new_whitelist);
1719 } 1718 }
1720 } 1719 }
1721 1720
1722 void SafeBrowsingDatabaseNew::LoadIpBlacklist( 1721 void SafeBrowsingDatabaseNew::LoadIpBlacklist(
1723 const std::vector<SBAddFullHash>& full_hashes) { 1722 const std::vector<SBAddFullHash>& full_hashes) {
(...skipping 29 matching lines...) Expand all
1753 << " prefix_size:" << prefix_size 1752 << " prefix_size:" << prefix_size
1754 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(), 1753 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(),
1755 hashed_ip_prefix.size()); 1754 hashed_ip_prefix.size());
1756 new_blacklist[mask].insert(hashed_ip_prefix); 1755 new_blacklist[mask].insert(hashed_ip_prefix);
1757 } 1756 }
1758 1757
1759 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist); 1758 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist);
1760 } 1759 }
1761 1760
1762 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { 1761 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() {
1763 SBFullHash malware_kill_switch = safe_browsing::SBFullHashForString( 1762 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl);
1764 kMalwareIPKillSwitchUrl);
1765 std::vector<SBFullHash> full_hashes; 1763 std::vector<SBFullHash> full_hashes;
1766 full_hashes.push_back(malware_kill_switch); 1764 full_hashes.push_back(malware_kill_switch);
1767 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); 1765 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes);
1768 } 1766 }
1769 1767
1770 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { 1768 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() {
1771 return state_manager_.BeginReadTransaction() 1769 return state_manager_.BeginReadTransaction()
1772 ->GetSBWhitelist(SBWhitelistId::CSD) 1770 ->GetSBWhitelist(SBWhitelistId::CSD)
1773 ->second; 1771 ->second;
1774 } 1772 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1827 else 1825 else
1828 NOTREACHED(); // Add support for new lists above. 1826 NOTREACHED(); // Add support for new lists above.
1829 1827
1830 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. 1828 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro.
1831 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( 1829 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet(
1832 histogram_name, 1, 1000000, 50, 1830 histogram_name, 1, 1000000, 50,
1833 base::HistogramBase::kUmaTargetedHistogramFlag); 1831 base::HistogramBase::kUmaTargetedHistogramFlag);
1834 1832
1835 histogram_pointer->Add(file_size_kilobytes); 1833 histogram_pointer->Add(file_size_kilobytes);
1836 } 1834 }
1835
1836 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698