| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <iterator> | 11 #include <iterator> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 19 #include "base/metrics/histogram_macros.h" | 19 #include "base/metrics/histogram_macros.h" |
| 20 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
| 21 #include "base/process/process_metrics.h" | 21 #include "base/process/process_metrics.h" |
| 22 #include "base/sha1.h" | 22 #include "base/sha1.h" |
| 23 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 26 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 27 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 28 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" | 28 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
| 29 #include "components/safe_browsing_db/prefix_set.h" | 29 #include "components/safe_browsing_db/prefix_set.h" |
| 30 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 30 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 31 #include "crypto/sha2.h" | 32 #include "crypto/sha2.h" |
| 32 #include "net/base/ip_address.h" | 33 #include "net/base/ip_address.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| 35 #if defined(OS_MACOSX) | 36 #if defined(OS_MACOSX) |
| 36 #include "base/mac/mac_util.h" | 37 #include "base/mac/mac_util.h" |
| 37 #endif | 38 #endif |
| 38 | 39 |
| 39 using content::BrowserThread; | 40 using content::BrowserThread; |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 UpdateChunkRanges(store, listnames, lists); | 220 UpdateChunkRanges(store, listnames, lists); |
| 220 } | 221 } |
| 221 | 222 |
| 222 void UpdateChunkRangesForList( | 223 void UpdateChunkRangesForList( |
| 223 SafeBrowsingStore* store, | 224 SafeBrowsingStore* store, |
| 224 const std::string& listname, | 225 const std::string& listname, |
| 225 std::vector<SBListChunkRanges>* lists) { | 226 std::vector<SBListChunkRanges>* lists) { |
| 226 UpdateChunkRanges(store, std::vector<std::string>(1, listname), lists); | 227 UpdateChunkRanges(store, std::vector<std::string>(1, listname), lists); |
| 227 } | 228 } |
| 228 | 229 |
| 229 // This code always checks for non-zero file size. This helper makes | |
| 230 // that less verbose. | |
| 231 int64_t GetFileSizeOrZero(const base::FilePath& file_path) { | |
| 232 int64_t size_64; | |
| 233 if (!base::GetFileSize(file_path, &size_64)) | |
| 234 return 0; | |
| 235 return size_64; | |
| 236 } | |
| 237 | |
| 238 // Helper for PrefixSetContainsUrlHashes(). Returns true if an un-expired match | 230 // Helper for PrefixSetContainsUrlHashes(). Returns true if an un-expired match |
| 239 // for |full_hash| is found in |cache|, with any matches appended to |results| | 231 // for |full_hash| is found in |cache|, with any matches appended to |results| |
| 240 // (true can be returned with zero matches). |expire_base| is used to check the | 232 // (true can be returned with zero matches). |expire_base| is used to check the |
| 241 // cache lifetime of matches, expired matches will be discarded from |cache|. | 233 // cache lifetime of matches, expired matches will be discarded from |cache|. |
| 242 bool GetCachedFullHash(std::map<SBPrefix, SBCachedFullHashResult>* cache, | 234 bool GetCachedFullHash(std::map<SBPrefix, SBCachedFullHashResult>* cache, |
| 243 const SBFullHash& full_hash, | 235 const SBFullHash& full_hash, |
| 244 const base::Time& expire_base, | 236 const base::Time& expire_base, |
| 245 std::vector<SBFullHashResult>* results) { | 237 std::vector<SBFullHashResult>* results) { |
| 246 // First check if there is a valid cached result for this prefix. | 238 // First check if there is a valid cached result for this prefix. |
| 247 std::map<SBPrefix, SBCachedFullHashResult>::iterator citer = | 239 std::map<SBPrefix, SBCachedFullHashResult>::iterator citer = |
| (...skipping 1346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1594 // real error-handling. | 1586 // real error-handling. |
| 1595 void SafeBrowsingDatabaseNew::LoadPrefixSet(const base::FilePath& db_filename, | 1587 void SafeBrowsingDatabaseNew::LoadPrefixSet(const base::FilePath& db_filename, |
| 1596 WriteTransaction* txn, | 1588 WriteTransaction* txn, |
| 1597 PrefixSetId prefix_set_id, | 1589 PrefixSetId prefix_set_id, |
| 1598 FailureType read_failure_type) { | 1590 FailureType read_failure_type) { |
| 1599 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); | 1591 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
| 1600 DCHECK(txn); | 1592 DCHECK(txn); |
| 1601 DCHECK(!db_state_manager_.filename_base().empty()); | 1593 DCHECK(!db_state_manager_.filename_base().empty()); |
| 1602 | 1594 |
| 1603 // Only use the prefix set if database is present and non-empty. | 1595 // Only use the prefix set if database is present and non-empty. |
| 1604 if (!GetFileSizeOrZero(db_filename)) | 1596 if (!V4ProtocolManagerUtil::GetFileSizeOrZero(db_filename)) |
| 1605 return; | 1597 return; |
| 1606 | 1598 |
| 1607 // Cleanup any stale bloom filter (no longer used). | 1599 // Cleanup any stale bloom filter (no longer used). |
| 1608 // TODO(shess): Track existence to drive removal of this code? | 1600 // TODO(shess): Track existence to drive removal of this code? |
| 1609 const base::FilePath bloom_filter_filename = | 1601 const base::FilePath bloom_filter_filename = |
| 1610 BloomFilterForFilename(db_filename); | 1602 BloomFilterForFilename(db_filename); |
| 1611 base::DeleteFile(bloom_filter_filename, false); | 1603 base::DeleteFile(bloom_filter_filename, false); |
| 1612 | 1604 |
| 1613 const base::TimeTicks before = base::TimeTicks::Now(); | 1605 const base::TimeTicks before = base::TimeTicks::Now(); |
| 1614 std::unique_ptr<const PrefixSet> new_prefix_set = | 1606 std::unique_ptr<const PrefixSet> new_prefix_set = |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1815 ->second; | 1807 ->second; |
| 1816 } | 1808 } |
| 1817 | 1809 |
| 1818 SafeBrowsingDatabaseNew::PrefixGetHashCache* | 1810 SafeBrowsingDatabaseNew::PrefixGetHashCache* |
| 1819 SafeBrowsingDatabaseNew::GetUnsynchronizedPrefixGetHashCacheForTesting() { | 1811 SafeBrowsingDatabaseNew::GetUnsynchronizedPrefixGetHashCacheForTesting() { |
| 1820 return state_manager_.BeginReadTransaction()->prefix_gethash_cache(); | 1812 return state_manager_.BeginReadTransaction()->prefix_gethash_cache(); |
| 1821 } | 1813 } |
| 1822 | 1814 |
| 1823 void SafeBrowsingDatabaseNew::RecordFileSizeHistogram( | 1815 void SafeBrowsingDatabaseNew::RecordFileSizeHistogram( |
| 1824 const base::FilePath& file_path) { | 1816 const base::FilePath& file_path) { |
| 1825 const int64_t file_size = GetFileSizeOrZero(file_path); | 1817 const int64_t file_size = V4ProtocolManagerUtil::GetFileSizeOrZero(file_path); |
| 1826 const int file_size_kilobytes = static_cast<int>(file_size / 1024); | 1818 const int file_size_kilobytes = static_cast<int>(file_size / 1024); |
| 1827 | 1819 |
| 1828 base::FilePath::StringType filename = file_path.BaseName().value(); | 1820 base::FilePath::StringType filename = file_path.BaseName().value(); |
| 1829 | 1821 |
| 1830 // Default to logging DB sizes unless |file_path| points at PrefixSet storage. | 1822 // Default to logging DB sizes unless |file_path| points at PrefixSet storage. |
| 1831 std::string histogram_name("SB2.DatabaseSizeKilobytes"); | 1823 std::string histogram_name("SB2.DatabaseSizeKilobytes"); |
| 1832 if (base::EndsWith(filename, kPrefixSetFileSuffix, | 1824 if (base::EndsWith(filename, kPrefixSetFileSuffix, |
| 1833 base::CompareCase::SENSITIVE)) { | 1825 base::CompareCase::SENSITIVE)) { |
| 1834 histogram_name = "SB2.PrefixSetSizeKilobytes"; | 1826 histogram_name = "SB2.PrefixSetSizeKilobytes"; |
| 1835 // Clear the PrefixSet suffix to have the histogram suffix selector below | 1827 // Clear the PrefixSet suffix to have the histogram suffix selector below |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1877 | 1869 |
| 1878 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. | 1870 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. |
| 1879 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( | 1871 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( |
| 1880 histogram_name, 1, 1000000, 50, | 1872 histogram_name, 1, 1000000, 50, |
| 1881 base::HistogramBase::kUmaTargetedHistogramFlag); | 1873 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 1882 | 1874 |
| 1883 histogram_pointer->Add(file_size_kilobytes); | 1875 histogram_pointer->Add(file_size_kilobytes); |
| 1884 } | 1876 } |
| 1885 | 1877 |
| 1886 } // namespace safe_browsing | 1878 } // namespace safe_browsing |
| OLD | NEW |