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 89 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_util::INVALID); | 110 DCHECK_NE(list_id, safe_browsing::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_util::GenerateHostsToCheck(url, &hosts); | 131 safe_browsing::GenerateHostsToCheck(url, &hosts); |
132 } | 132 } |
133 | 133 |
134 std::vector<std::string> paths; | 134 std::vector<std::string> paths; |
135 safe_browsing_util::GeneratePathsToCheck(url, &paths); | 135 safe_browsing::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(SBFullHashForString(hosts[i] + path)); | 140 full_hashes->push_back(safe_browsing::SBFullHashForString( |
| 141 hosts[i] + path)); |
141 | 142 |
142 // 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 |
143 // 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 |
144 // that ends in '/' we also add the path without the slash. | 145 // that ends in '/' we also add the path without the slash. |
145 if (include_whitelist_hashes && | 146 if (include_whitelist_hashes && |
146 path.size() > 1 && | 147 path.size() > 1 && |
147 path[path.size() - 1] == '/') { | 148 path[path.size() - 1] == '/') { |
148 full_hashes->push_back( | 149 full_hashes->push_back( |
149 SBFullHashForString(hosts[i] + path.substr(0, path.size() - 1))); | 150 safe_browsing::SBFullHashForString( |
| 151 hosts[i] + path.substr(0, path.size() - 1))); |
150 } | 152 } |
151 } | 153 } |
152 } | 154 } |
153 } | 155 } |
154 | 156 |
155 // Helper function to compare addprefixes in |store| with |prefixes|. | 157 // Helper function to compare addprefixes in |store| with |prefixes|. |
156 // The |list_bit| indicates which list (url or hash) to compare. | 158 // The |list_bit| indicates which list (url or hash) to compare. |
157 // | 159 // |
158 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain | 160 // Returns true if there is a match, |*prefix_hits| (if non-NULL) will contain |
159 // the actual matching prefixes. | 161 // the actual matching prefixes. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 // Always decode 2 ranges, even if only the first one is expected. | 225 // Always decode 2 ranges, even if only the first one is expected. |
224 // The loop below will only load as many into |lists| as |listnames| | 226 // The loop below will only load as many into |lists| as |listnames| |
225 // indicates. | 227 // indicates. |
226 std::vector<std::string> adds(2); | 228 std::vector<std::string> adds(2); |
227 std::vector<std::string> subs(2); | 229 std::vector<std::string> subs(2); |
228 GetChunkRanges(add_chunks, &adds); | 230 GetChunkRanges(add_chunks, &adds); |
229 GetChunkRanges(sub_chunks, &subs); | 231 GetChunkRanges(sub_chunks, &subs); |
230 | 232 |
231 for (size_t i = 0; i < listnames.size(); ++i) { | 233 for (size_t i = 0; i < listnames.size(); ++i) { |
232 const std::string& listname = listnames[i]; | 234 const std::string& listname = listnames[i]; |
233 DCHECK_EQ(safe_browsing_util::GetListId(listname) % 2, | 235 DCHECK_EQ(safe_browsing::GetListId(listname) % 2, |
234 static_cast<int>(i % 2)); | 236 static_cast<int>(i % 2)); |
235 DCHECK_NE(safe_browsing_util::GetListId(listname), | 237 DCHECK_NE(safe_browsing::GetListId(listname), |
236 safe_browsing_util::INVALID); | 238 safe_browsing::INVALID); |
237 lists->push_back(SBListChunkRanges(listname)); | 239 lists->push_back(SBListChunkRanges(listname)); |
238 lists->back().adds.swap(adds[i]); | 240 lists->back().adds.swap(adds[i]); |
239 lists->back().subs.swap(subs[i]); | 241 lists->back().subs.swap(subs[i]); |
240 } | 242 } |
241 } | 243 } |
242 | 244 |
243 void UpdateChunkRangesForLists(SafeBrowsingStore* store, | 245 void UpdateChunkRangesForLists(SafeBrowsingStore* store, |
244 const std::string& listname0, | 246 const std::string& listname0, |
245 const std::string& listname1, | 247 const std::string& listname1, |
246 std::vector<SBListChunkRanges>* lists) { | 248 std::vector<SBListChunkRanges>* lists) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 // Remove expired entries. | 284 // Remove expired entries. |
283 SBCachedFullHashResult& cached_result = citer->second; | 285 SBCachedFullHashResult& cached_result = citer->second; |
284 if (cached_result.expire_after <= expire_base) { | 286 if (cached_result.expire_after <= expire_base) { |
285 cache->erase(citer); | 287 cache->erase(citer); |
286 return false; | 288 return false; |
287 } | 289 } |
288 | 290 |
289 // Find full-hash matches. | 291 // Find full-hash matches. |
290 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes; | 292 std::vector<SBFullHashResult>& cached_hashes = cached_result.full_hashes; |
291 for (size_t i = 0; i < cached_hashes.size(); ++i) { | 293 for (size_t i = 0; i < cached_hashes.size(); ++i) { |
292 if (SBFullHashEqual(full_hash, cached_hashes[i].hash)) | 294 if (safe_browsing::SBFullHashEqual(full_hash, cached_hashes[i].hash)) |
293 results->push_back(cached_hashes[i]); | 295 results->push_back(cached_hashes[i]); |
294 } | 296 } |
295 | 297 |
296 return true; | 298 return true; |
297 } | 299 } |
298 | 300 |
299 SafeBrowsingStoreFile* CreateStore( | 301 SafeBrowsingStoreFile* CreateStore( |
300 bool enable, | 302 bool enable, |
301 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 303 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
302 if (!enable) | 304 if (!enable) |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 UrlToFullHashes(urls[i], false, &full_hashes); | 440 UrlToFullHashes(urls[i], false, &full_hashes); |
439 | 441 |
440 for (size_t i = 0; i < full_hashes.size(); ++i) | 442 for (size_t i = 0; i < full_hashes.size(); ++i) |
441 prefixes->push_back(full_hashes[i].prefix); | 443 prefixes->push_back(full_hashes[i].prefix); |
442 } | 444 } |
443 | 445 |
444 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { | 446 SafeBrowsingStore* SafeBrowsingDatabaseNew::GetStore(const int list_id) { |
445 // Stores are not thread safe. | 447 // Stores are not thread safe. |
446 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 448 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
447 | 449 |
448 if (list_id == safe_browsing_util::PHISH || | 450 if (list_id == safe_browsing::PHISH || |
449 list_id == safe_browsing_util::MALWARE) { | 451 list_id == safe_browsing::MALWARE) { |
450 return browse_store_.get(); | 452 return browse_store_.get(); |
451 } else if (list_id == safe_browsing_util::BINURL) { | 453 } else if (list_id == safe_browsing::BINURL) { |
452 return download_store_.get(); | 454 return download_store_.get(); |
453 } else if (list_id == safe_browsing_util::CSDWHITELIST) { | 455 } else if (list_id == safe_browsing::CSDWHITELIST) { |
454 return csd_whitelist_store_.get(); | 456 return csd_whitelist_store_.get(); |
455 } else if (list_id == safe_browsing_util::DOWNLOADWHITELIST) { | 457 } else if (list_id == safe_browsing::DOWNLOADWHITELIST) { |
456 return download_whitelist_store_.get(); | 458 return download_whitelist_store_.get(); |
457 } else if (list_id == safe_browsing_util::INCLUSIONWHITELIST) { | 459 } else if (list_id == safe_browsing::INCLUSIONWHITELIST) { |
458 return inclusion_whitelist_store_.get(); | 460 return inclusion_whitelist_store_.get(); |
459 } else if (list_id == safe_browsing_util::EXTENSIONBLACKLIST) { | 461 } else if (list_id == safe_browsing::EXTENSIONBLACKLIST) { |
460 return extension_blacklist_store_.get(); | 462 return extension_blacklist_store_.get(); |
461 } else if (list_id == safe_browsing_util::IPBLACKLIST) { | 463 } else if (list_id == safe_browsing::IPBLACKLIST) { |
462 return ip_blacklist_store_.get(); | 464 return ip_blacklist_store_.get(); |
463 } else if (list_id == safe_browsing_util::UNWANTEDURL) { | 465 } else if (list_id == safe_browsing::UNWANTEDURL) { |
464 return unwanted_software_store_.get(); | 466 return unwanted_software_store_.get(); |
465 } | 467 } |
466 return NULL; | 468 return NULL; |
467 } | 469 } |
468 | 470 |
469 // static | 471 // static |
470 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) { | 472 void SafeBrowsingDatabase::RecordFailure(FailureType failure_type) { |
471 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type, | 473 UMA_HISTOGRAM_ENUMERATION("SB2.DatabaseFailure", failure_type, |
472 FAILURE_DATABASE_MAX); | 474 FAILURE_DATABASE_MAX); |
473 } | 475 } |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes( | 910 bool SafeBrowsingDatabaseNew::ContainsDownloadUrlPrefixes( |
909 const std::vector<SBPrefix>& prefixes, | 911 const std::vector<SBPrefix>& prefixes, |
910 std::vector<SBPrefix>* prefix_hits) { | 912 std::vector<SBPrefix>* prefix_hits) { |
911 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 913 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
912 | 914 |
913 // Ignore this check when download checking is not enabled. | 915 // Ignore this check when download checking is not enabled. |
914 if (!download_store_.get()) | 916 if (!download_store_.get()) |
915 return false; | 917 return false; |
916 | 918 |
917 return MatchAddPrefixes(download_store_.get(), | 919 return MatchAddPrefixes(download_store_.get(), |
918 safe_browsing_util::BINURL % 2, | 920 safe_browsing::BINURL % 2, |
919 prefixes, | 921 prefixes, |
920 prefix_hits); | 922 prefix_hits); |
921 } | 923 } |
922 | 924 |
923 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { | 925 bool SafeBrowsingDatabaseNew::ContainsCsdWhitelistedUrl(const GURL& url) { |
924 std::vector<SBFullHash> full_hashes; | 926 std::vector<SBFullHash> full_hashes; |
925 UrlToFullHashes(url, true, &full_hashes); | 927 UrlToFullHashes(url, true, &full_hashes); |
926 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); | 928 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); |
927 } | 929 } |
928 | 930 |
(...skipping 11 matching lines...) Expand all Loading... |
940 | 942 |
941 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( | 943 bool SafeBrowsingDatabaseNew::ContainsExtensionPrefixes( |
942 const std::vector<SBPrefix>& prefixes, | 944 const std::vector<SBPrefix>& prefixes, |
943 std::vector<SBPrefix>* prefix_hits) { | 945 std::vector<SBPrefix>* prefix_hits) { |
944 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 946 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
945 | 947 |
946 if (!extension_blacklist_store_) | 948 if (!extension_blacklist_store_) |
947 return false; | 949 return false; |
948 | 950 |
949 return MatchAddPrefixes(extension_blacklist_store_.get(), | 951 return MatchAddPrefixes(extension_blacklist_store_.get(), |
950 safe_browsing_util::EXTENSIONBLACKLIST % 2, | 952 safe_browsing::EXTENSIONBLACKLIST % 2, |
951 prefixes, | 953 prefixes, |
952 prefix_hits); | 954 prefix_hits); |
953 } | 955 } |
954 | 956 |
955 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { | 957 bool SafeBrowsingDatabaseNew::ContainsMalwareIP(const std::string& ip_address) { |
956 net::IPAddressNumber ip_number; | 958 net::IPAddressNumber ip_number; |
957 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) | 959 if (!net::ParseIPLiteralToNumber(ip_address, &ip_number)) |
958 return false; | 960 return false; |
959 if (ip_number.size() == net::kIPv4AddressSize) | 961 if (ip_number.size() == net::kIPv4AddressSize) |
960 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); | 962 ip_number = net::ConvertIPv4NumberToIPv6Number(ip_number); |
(...skipping 19 matching lines...) Expand all Loading... |
980 if (it->second.count(hash) > 0) { | 982 if (it->second.count(hash) > 0) { |
981 return true; | 983 return true; |
982 } | 984 } |
983 } | 985 } |
984 return false; | 986 return false; |
985 } | 987 } |
986 | 988 |
987 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString( | 989 bool SafeBrowsingDatabaseNew::ContainsDownloadWhitelistedString( |
988 const std::string& str) { | 990 const std::string& str) { |
989 std::vector<SBFullHash> hashes; | 991 std::vector<SBFullHash> hashes; |
990 hashes.push_back(SBFullHashForString(str)); | 992 hashes.push_back(safe_browsing::SBFullHashForString(str)); |
991 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes); | 993 return ContainsWhitelistedHashes(SBWhitelistId::DOWNLOAD, hashes); |
992 } | 994 } |
993 | 995 |
994 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes( | 996 bool SafeBrowsingDatabaseNew::ContainsWhitelistedHashes( |
995 SBWhitelistId whitelist_id, | 997 SBWhitelistId whitelist_id, |
996 const std::vector<SBFullHash>& hashes) { | 998 const std::vector<SBFullHash>& hashes) { |
997 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); | 999 scoped_ptr<ReadTransaction> txn = state_manager_.BeginReadTransaction(); |
998 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id); | 1000 const SBWhitelist* whitelist = txn->GetSBWhitelist(whitelist_id); |
999 if (whitelist->second) | 1001 if (whitelist->second) |
1000 return true; | 1002 return true; |
1001 for (std::vector<SBFullHash>::const_iterator it = hashes.begin(); | 1003 for (std::vector<SBFullHash>::const_iterator it = hashes.begin(); |
1002 it != hashes.end(); ++it) { | 1004 it != hashes.end(); ++it) { |
1003 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(), | 1005 if (std::binary_search(whitelist->first.begin(), whitelist->first.end(), |
1004 *it, SBFullHashLess)) { | 1006 *it, safe_browsing::SBFullHashLess)) { |
1005 return true; | 1007 return true; |
1006 } | 1008 } |
1007 } | 1009 } |
1008 return false; | 1010 return false; |
1009 } | 1011 } |
1010 | 1012 |
1011 // Helper to insert add-chunk entries. | 1013 // Helper to insert add-chunk entries. |
1012 void SafeBrowsingDatabaseNew::InsertAddChunk( | 1014 void SafeBrowsingDatabaseNew::InsertAddChunk( |
1013 SafeBrowsingStore* store, | 1015 SafeBrowsingStore* store, |
1014 const safe_browsing_util::ListType list_id, | 1016 const safe_browsing::ListType list_id, |
1015 const SBChunkData& chunk_data) { | 1017 const SBChunkData& chunk_data) { |
1016 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1018 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1017 DCHECK(store); | 1019 DCHECK(store); |
1018 | 1020 |
1019 // The server can give us a chunk that we already have because | 1021 // The server can give us a chunk that we already have because |
1020 // it's part of a range. Don't add it again. | 1022 // it's part of a range. Don't add it again. |
1021 const int chunk_id = chunk_data.ChunkNumber(); | 1023 const int chunk_id = chunk_data.ChunkNumber(); |
1022 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); | 1024 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); |
1023 if (store->CheckAddChunk(encoded_chunk_id)) | 1025 if (store->CheckAddChunk(encoded_chunk_id)) |
1024 return; | 1026 return; |
1025 | 1027 |
1026 store->SetAddChunk(encoded_chunk_id); | 1028 store->SetAddChunk(encoded_chunk_id); |
1027 if (chunk_data.IsPrefix()) { | 1029 if (chunk_data.IsPrefix()) { |
1028 const size_t c = chunk_data.PrefixCount(); | 1030 const size_t c = chunk_data.PrefixCount(); |
1029 for (size_t i = 0; i < c; ++i) { | 1031 for (size_t i = 0; i < c; ++i) { |
1030 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); | 1032 store->WriteAddPrefix(encoded_chunk_id, chunk_data.PrefixAt(i)); |
1031 } | 1033 } |
1032 } else { | 1034 } else { |
1033 const size_t c = chunk_data.FullHashCount(); | 1035 const size_t c = chunk_data.FullHashCount(); |
1034 for (size_t i = 0; i < c; ++i) { | 1036 for (size_t i = 0; i < c; ++i) { |
1035 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); | 1037 store->WriteAddHash(encoded_chunk_id, chunk_data.FullHashAt(i)); |
1036 } | 1038 } |
1037 } | 1039 } |
1038 } | 1040 } |
1039 | 1041 |
1040 // Helper to insert sub-chunk entries. | 1042 // Helper to insert sub-chunk entries. |
1041 void SafeBrowsingDatabaseNew::InsertSubChunk( | 1043 void SafeBrowsingDatabaseNew::InsertSubChunk( |
1042 SafeBrowsingStore* store, | 1044 SafeBrowsingStore* store, |
1043 const safe_browsing_util::ListType list_id, | 1045 const safe_browsing::ListType list_id, |
1044 const SBChunkData& chunk_data) { | 1046 const SBChunkData& chunk_data) { |
1045 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1047 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1046 DCHECK(store); | 1048 DCHECK(store); |
1047 | 1049 |
1048 // The server can give us a chunk that we already have because | 1050 // The server can give us a chunk that we already have because |
1049 // it's part of a range. Don't add it again. | 1051 // it's part of a range. Don't add it again. |
1050 const int chunk_id = chunk_data.ChunkNumber(); | 1052 const int chunk_id = chunk_data.ChunkNumber(); |
1051 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); | 1053 const int encoded_chunk_id = EncodeChunkId(chunk_id, list_id); |
1052 if (store->CheckSubChunk(encoded_chunk_id)) | 1054 if (store->CheckSubChunk(encoded_chunk_id)) |
1053 return; | 1055 return; |
(...skipping 22 matching lines...) Expand all Loading... |
1076 const std::string& list_name, | 1078 const std::string& list_name, |
1077 const std::vector<SBChunkData*>& chunks) { | 1079 const std::vector<SBChunkData*>& chunks) { |
1078 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1080 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1079 | 1081 |
1080 if (db_state_manager_.corruption_detected() || chunks.empty()) | 1082 if (db_state_manager_.corruption_detected() || chunks.empty()) |
1081 return; | 1083 return; |
1082 | 1084 |
1083 const base::TimeTicks before = base::TimeTicks::Now(); | 1085 const base::TimeTicks before = base::TimeTicks::Now(); |
1084 | 1086 |
1085 // TODO(shess): The caller should just pass list_id. | 1087 // TODO(shess): The caller should just pass list_id. |
1086 const safe_browsing_util::ListType list_id = | 1088 const safe_browsing::ListType list_id = |
1087 safe_browsing_util::GetListId(list_name); | 1089 safe_browsing::GetListId(list_name); |
1088 | 1090 |
1089 SafeBrowsingStore* store = GetStore(list_id); | 1091 SafeBrowsingStore* store = GetStore(list_id); |
1090 if (!store) return; | 1092 if (!store) return; |
1091 | 1093 |
1092 db_state_manager_.set_change_detected(); | 1094 db_state_manager_.set_change_detected(); |
1093 | 1095 |
1094 // TODO(shess): I believe that the list is always add or sub. Can this use | 1096 // TODO(shess): I believe that the list is always add or sub. Can this use |
1095 // that productively? | 1097 // that productively? |
1096 store->BeginChunk(); | 1098 store->BeginChunk(); |
1097 for (size_t i = 0; i < chunks.size(); ++i) { | 1099 for (size_t i = 0; i < chunks.size(); ++i) { |
(...skipping 11 matching lines...) Expand all Loading... |
1109 } | 1111 } |
1110 | 1112 |
1111 void SafeBrowsingDatabaseNew::DeleteChunks( | 1113 void SafeBrowsingDatabaseNew::DeleteChunks( |
1112 const std::vector<SBChunkDelete>& chunk_deletes) { | 1114 const std::vector<SBChunkDelete>& chunk_deletes) { |
1113 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1115 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1114 | 1116 |
1115 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) | 1117 if (db_state_manager_.corruption_detected() || chunk_deletes.empty()) |
1116 return; | 1118 return; |
1117 | 1119 |
1118 const std::string& list_name = chunk_deletes.front().list_name; | 1120 const std::string& list_name = chunk_deletes.front().list_name; |
1119 const safe_browsing_util::ListType list_id = | 1121 const safe_browsing::ListType list_id = |
1120 safe_browsing_util::GetListId(list_name); | 1122 safe_browsing::GetListId(list_name); |
1121 | 1123 |
1122 SafeBrowsingStore* store = GetStore(list_id); | 1124 SafeBrowsingStore* store = GetStore(list_id); |
1123 if (!store) return; | 1125 if (!store) return; |
1124 | 1126 |
1125 db_state_manager_.set_change_detected(); | 1127 db_state_manager_.set_change_detected(); |
1126 | 1128 |
1127 for (size_t i = 0; i < chunk_deletes.size(); ++i) { | 1129 for (size_t i = 0; i < chunk_deletes.size(); ++i) { |
1128 std::vector<int> chunk_numbers; | 1130 std::vector<int> chunk_numbers; |
1129 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers); | 1131 RangesToChunks(chunk_deletes[i].chunk_del, &chunk_numbers); |
1130 for (size_t j = 0; j < chunk_numbers.size(); ++j) { | 1132 for (size_t j = 0; j < chunk_numbers.size(); ++j) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1214 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN); | 1216 RecordFailure(FAILURE_UNWANTED_SOFTWARE_DATABASE_UPDATE_BEGIN); |
1215 HandleCorruptDatabase(); | 1217 HandleCorruptDatabase(); |
1216 return false; | 1218 return false; |
1217 } | 1219 } |
1218 | 1220 |
1219 // Cached fullhash results must be cleared on every database update (whether | 1221 // Cached fullhash results must be cleared on every database update (whether |
1220 // successful or not). | 1222 // successful or not). |
1221 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache(); | 1223 state_manager_.BeginWriteTransaction()->clear_prefix_gethash_cache(); |
1222 | 1224 |
1223 UpdateChunkRangesForLists(browse_store_.get(), | 1225 UpdateChunkRangesForLists(browse_store_.get(), |
1224 safe_browsing_util::kMalwareList, | 1226 safe_browsing::kMalwareList, |
1225 safe_browsing_util::kPhishingList, | 1227 safe_browsing::kPhishingList, |
1226 lists); | 1228 lists); |
1227 | 1229 |
1228 // NOTE(shess): |download_store_| used to contain kBinHashList, which has been | 1230 // NOTE(shess): |download_store_| used to contain kBinHashList, which has been |
1229 // deprecated. Code to delete the list from the store shows ~15k hits/day as | 1231 // deprecated. Code to delete the list from the store shows ~15k hits/day as |
1230 // of Feb 2014, so it has been removed. Everything _should_ be resilient to | 1232 // of Feb 2014, so it has been removed. Everything _should_ be resilient to |
1231 // extra data of that sort. | 1233 // extra data of that sort. |
1232 UpdateChunkRangesForList(download_store_.get(), | 1234 UpdateChunkRangesForList(download_store_.get(), |
1233 safe_browsing_util::kBinUrlList, lists); | 1235 safe_browsing::kBinUrlList, lists); |
1234 | 1236 |
1235 UpdateChunkRangesForList(csd_whitelist_store_.get(), | 1237 UpdateChunkRangesForList(csd_whitelist_store_.get(), |
1236 safe_browsing_util::kCsdWhiteList, lists); | 1238 safe_browsing::kCsdWhiteList, lists); |
1237 | 1239 |
1238 UpdateChunkRangesForList(download_whitelist_store_.get(), | 1240 UpdateChunkRangesForList(download_whitelist_store_.get(), |
1239 safe_browsing_util::kDownloadWhiteList, lists); | 1241 safe_browsing::kDownloadWhiteList, lists); |
1240 | 1242 |
1241 UpdateChunkRangesForList(inclusion_whitelist_store_.get(), | 1243 UpdateChunkRangesForList(inclusion_whitelist_store_.get(), |
1242 safe_browsing_util::kInclusionWhitelist, lists); | 1244 safe_browsing::kInclusionWhitelist, lists); |
1243 | 1245 |
1244 UpdateChunkRangesForList(extension_blacklist_store_.get(), | 1246 UpdateChunkRangesForList(extension_blacklist_store_.get(), |
1245 safe_browsing_util::kExtensionBlacklist, lists); | 1247 safe_browsing::kExtensionBlacklist, lists); |
1246 | 1248 |
1247 UpdateChunkRangesForList(ip_blacklist_store_.get(), | 1249 UpdateChunkRangesForList(ip_blacklist_store_.get(), |
1248 safe_browsing_util::kIPBlacklist, lists); | 1250 safe_browsing::kIPBlacklist, lists); |
1249 | 1251 |
1250 UpdateChunkRangesForList(unwanted_software_store_.get(), | 1252 UpdateChunkRangesForList(unwanted_software_store_.get(), |
1251 safe_browsing_util::kUnwantedUrlList, | 1253 safe_browsing::kUnwantedUrlList, |
1252 lists); | 1254 lists); |
1253 | 1255 |
1254 db_state_manager_.reset_corruption_detected(); | 1256 db_state_manager_.reset_corruption_detected(); |
1255 db_state_manager_.reset_change_detected(); | 1257 db_state_manager_.reset_change_detected(); |
1256 return true; | 1258 return true; |
1257 } | 1259 } |
1258 | 1260 |
1259 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { | 1261 void SafeBrowsingDatabaseNew::UpdateFinished(bool update_succeeded) { |
1260 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1262 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
1261 | 1263 |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1695 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); | 1697 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); |
1696 return; | 1698 return; |
1697 } | 1699 } |
1698 | 1700 |
1699 std::vector<SBFullHash> new_whitelist; | 1701 std::vector<SBFullHash> new_whitelist; |
1700 new_whitelist.reserve(full_hashes.size()); | 1702 new_whitelist.reserve(full_hashes.size()); |
1701 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); | 1703 for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin(); |
1702 it != full_hashes.end(); ++it) { | 1704 it != full_hashes.end(); ++it) { |
1703 new_whitelist.push_back(it->full_hash); | 1705 new_whitelist.push_back(it->full_hash); |
1704 } | 1706 } |
1705 std::sort(new_whitelist.begin(), new_whitelist.end(), SBFullHashLess); | 1707 std::sort(new_whitelist.begin(), new_whitelist.end(), |
| 1708 safe_browsing::SBFullHashLess); |
1706 | 1709 |
1707 SBFullHash kill_switch = SBFullHashForString(kWhitelistKillSwitchUrl); | 1710 SBFullHash kill_switch = safe_browsing::SBFullHashForString( |
| 1711 kWhitelistKillSwitchUrl); |
1708 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), | 1712 if (std::binary_search(new_whitelist.begin(), new_whitelist.end(), |
1709 kill_switch, SBFullHashLess)) { | 1713 kill_switch, safe_browsing::SBFullHashLess)) { |
1710 // The kill switch is whitelisted hence we whitelist all URLs. | 1714 // The kill switch is whitelisted hence we whitelist all URLs. |
1711 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); | 1715 state_manager_.BeginWriteTransaction()->WhitelistEverything(whitelist_id); |
1712 } else { | 1716 } else { |
1713 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, | 1717 state_manager_.BeginWriteTransaction()->SwapSBWhitelist(whitelist_id, |
1714 &new_whitelist); | 1718 &new_whitelist); |
1715 } | 1719 } |
1716 } | 1720 } |
1717 | 1721 |
1718 void SafeBrowsingDatabaseNew::LoadIpBlacklist( | 1722 void SafeBrowsingDatabaseNew::LoadIpBlacklist( |
1719 const std::vector<SBAddFullHash>& full_hashes) { | 1723 const std::vector<SBAddFullHash>& full_hashes) { |
(...skipping 29 matching lines...) Expand all Loading... |
1749 << " prefix_size:" << prefix_size | 1753 << " prefix_size:" << prefix_size |
1750 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(), | 1754 << " hashed_ip:" << base::HexEncode(hashed_ip_prefix.data(), |
1751 hashed_ip_prefix.size()); | 1755 hashed_ip_prefix.size()); |
1752 new_blacklist[mask].insert(hashed_ip_prefix); | 1756 new_blacklist[mask].insert(hashed_ip_prefix); |
1753 } | 1757 } |
1754 | 1758 |
1755 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist); | 1759 state_manager_.BeginWriteTransaction()->swap_ip_blacklist(&new_blacklist); |
1756 } | 1760 } |
1757 | 1761 |
1758 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { | 1762 bool SafeBrowsingDatabaseNew::IsMalwareIPMatchKillSwitchOn() { |
1759 SBFullHash malware_kill_switch = SBFullHashForString(kMalwareIPKillSwitchUrl); | 1763 SBFullHash malware_kill_switch = safe_browsing::SBFullHashForString( |
| 1764 kMalwareIPKillSwitchUrl); |
1760 std::vector<SBFullHash> full_hashes; | 1765 std::vector<SBFullHash> full_hashes; |
1761 full_hashes.push_back(malware_kill_switch); | 1766 full_hashes.push_back(malware_kill_switch); |
1762 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); | 1767 return ContainsWhitelistedHashes(SBWhitelistId::CSD, full_hashes); |
1763 } | 1768 } |
1764 | 1769 |
1765 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { | 1770 bool SafeBrowsingDatabaseNew::IsCsdWhitelistKillSwitchOn() { |
1766 return state_manager_.BeginReadTransaction() | 1771 return state_manager_.BeginReadTransaction() |
1767 ->GetSBWhitelist(SBWhitelistId::CSD) | 1772 ->GetSBWhitelist(SBWhitelistId::CSD) |
1768 ->second; | 1773 ->second; |
1769 } | 1774 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1822 else | 1827 else |
1823 NOTREACHED(); // Add support for new lists above. | 1828 NOTREACHED(); // Add support for new lists above. |
1824 | 1829 |
1825 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. | 1830 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. |
1826 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( | 1831 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( |
1827 histogram_name, 1, 1000000, 50, | 1832 histogram_name, 1, 1000000, 50, |
1828 base::HistogramBase::kUmaTargetedHistogramFlag); | 1833 base::HistogramBase::kUmaTargetedHistogramFlag); |
1829 | 1834 |
1830 histogram_pointer->Add(file_size_kilobytes); | 1835 histogram_pointer->Add(file_size_kilobytes); |
1831 } | 1836 } |
OLD | NEW |