Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/base64.h" | 5 #include "base/base64.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 void V4Store::Initialize() { | 189 void V4Store::Initialize() { |
| 190 // If a state already exists, don't re-initilize. | 190 // If a state already exists, don't re-initilize. |
| 191 DCHECK(state_.empty()); | 191 DCHECK(state_.empty()); |
| 192 | 192 |
| 193 StoreReadResult store_read_result = ReadFromDisk(); | 193 StoreReadResult store_read_result = ReadFromDisk(); |
| 194 RecordStoreReadResult(store_read_result); | 194 RecordStoreReadResult(store_read_result); |
| 195 } | 195 } |
| 196 | 196 |
| 197 V4Store::V4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 197 V4Store::V4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 198 const base::FilePath& store_path) | 198 const base::FilePath& store_path) |
| 199 : store_path_(store_path), task_runner_(task_runner) {} | 199 : file_size_(0), store_path_(store_path), task_runner_(task_runner) {} |
| 200 | 200 |
| 201 V4Store::~V4Store() { | 201 V4Store::~V4Store() { |
| 202 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 202 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 203 } | 203 } |
| 204 | 204 |
| 205 std::string V4Store::DebugString() const { | 205 std::string V4Store::DebugString() const { |
| 206 std::string state_base64; | 206 std::string state_base64; |
| 207 base::Base64Encode(state_, &state_base64); | 207 base::Base64Encode(state_, &state_base64); |
| 208 | 208 |
| 209 return base::StringPrintf("path: %" PRIsFP "; state: %s", | 209 return base::StringPrintf("path: %" PRIsFP "; state: %s", |
| (...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 693 std::unique_ptr<ListUpdateResponse> response(new ListUpdateResponse); | 693 std::unique_ptr<ListUpdateResponse> response(new ListUpdateResponse); |
| 694 response->Swap(file_format.mutable_list_update_response()); | 694 response->Swap(file_format.mutable_list_update_response()); |
| 695 ApplyUpdateResult apply_update_result = ProcessFullUpdate( | 695 ApplyUpdateResult apply_update_result = ProcessFullUpdate( |
| 696 kReadFromDisk, response, true /* delay_checksum check */); | 696 kReadFromDisk, response, true /* delay_checksum check */); |
| 697 RecordApplyUpdateResult(kReadFromDisk, apply_update_result, store_path_); | 697 RecordApplyUpdateResult(kReadFromDisk, apply_update_result, store_path_); |
| 698 if (apply_update_result != APPLY_UPDATE_SUCCESS) { | 698 if (apply_update_result != APPLY_UPDATE_SUCCESS) { |
| 699 hash_prefix_map_.clear(); | 699 hash_prefix_map_.clear(); |
| 700 return HASH_PREFIX_MAP_GENERATION_FAILURE; | 700 return HASH_PREFIX_MAP_GENERATION_FAILURE; |
| 701 } | 701 } |
| 702 RecordApplyUpdateTime(kReadFromDisk, TimeTicks::Now() - before, store_path_); | 702 RecordApplyUpdateTime(kReadFromDisk, TimeTicks::Now() - before, store_path_); |
| 703 file_size_ = V4ProtocolManagerUtil::GetFileSizeOrZero(store_path_); | |
|
Scott Hess - ex-Googler
2016/10/21 23:36:45
Could you get this from contents.size()?
vakh (use Gerrit instead)
2016/10/24 19:39:32
Done.
| |
| 703 | 704 |
| 704 return READ_SUCCESS; | 705 return READ_SUCCESS; |
| 705 } | 706 } |
| 706 | 707 |
| 707 StoreWriteResult V4Store::WriteToDisk(const Checksum& checksum) const { | 708 StoreWriteResult V4Store::WriteToDisk(const Checksum& checksum) { |
| 708 V4StoreFileFormat file_format; | 709 V4StoreFileFormat file_format; |
| 709 ListUpdateResponse* lur = file_format.mutable_list_update_response(); | 710 ListUpdateResponse* lur = file_format.mutable_list_update_response(); |
| 710 *(lur->mutable_checksum()) = checksum; | 711 *(lur->mutable_checksum()) = checksum; |
| 711 lur->set_new_client_state(state_); | 712 lur->set_new_client_state(state_); |
| 712 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); | 713 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); |
| 713 for (auto map_iter : hash_prefix_map_) { | 714 for (auto map_iter : hash_prefix_map_) { |
| 714 ThreatEntrySet* additions = lur->add_additions(); | 715 ThreatEntrySet* additions = lur->add_additions(); |
| 715 // TODO(vakh): Write RICE encoded hash prefixes on disk. Not doing so | 716 // TODO(vakh): Write RICE encoded hash prefixes on disk. Not doing so |
| 716 // currently since it takes a long time to decode them on startup, which | 717 // currently since it takes a long time to decode them on startup, which |
| 717 // blocks resource load. See: http://crbug.com/654819 | 718 // blocks resource load. See: http://crbug.com/654819 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 728 std::string file_format_string; | 729 std::string file_format_string; |
| 729 file_format.SerializeToString(&file_format_string); | 730 file_format.SerializeToString(&file_format_string); |
| 730 size_t written = base::WriteFile(new_filename, file_format_string.data(), | 731 size_t written = base::WriteFile(new_filename, file_format_string.data(), |
| 731 file_format_string.size()); | 732 file_format_string.size()); |
| 732 DCHECK_EQ(file_format_string.size(), written); | 733 DCHECK_EQ(file_format_string.size(), written); |
| 733 | 734 |
| 734 if (!base::Move(new_filename, store_path_)) { | 735 if (!base::Move(new_filename, store_path_)) { |
| 735 return UNABLE_TO_RENAME_FAILURE; | 736 return UNABLE_TO_RENAME_FAILURE; |
| 736 } | 737 } |
| 737 | 738 |
| 739 file_size_ = V4ProtocolManagerUtil::GetFileSizeOrZero(store_path_); | |
|
Scott Hess - ex-Googler
2016/10/21 23:36:45
Could this ever be a different value from |written
vakh (use Gerrit instead)
2016/10/24 19:39:32
Done.
| |
| 740 | |
| 738 return WRITE_SUCCESS; | 741 return WRITE_SUCCESS; |
| 739 } | 742 } |
| 740 | 743 |
| 741 HashPrefix V4Store::GetMatchingHashPrefix(const FullHash& full_hash) { | 744 HashPrefix V4Store::GetMatchingHashPrefix(const FullHash& full_hash) { |
| 742 // It should never be the case that more than one hash prefixes match a given | 745 // It should never be the case that more than one hash prefixes match a given |
| 743 // full hash. However, if that happens, this method returns any one of them. | 746 // full hash. However, if that happens, this method returns any one of them. |
| 744 // It does not guarantee which one of those will be returned. | 747 // It does not guarantee which one of those will be returned. |
| 745 DCHECK_EQ(32u, full_hash.size()); | 748 DCHECK_EQ(32u, full_hash.size()); |
| 746 for (const auto& pair : hash_prefix_map_) { | 749 for (const auto& pair : hash_prefix_map_) { |
| 747 const PrefixSize& prefix_size = pair.first; | 750 const PrefixSize& prefix_size = pair.first; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 825 DVLOG(1) << "Failure: Checksum mismatch: calculated: " << checksum_b64 | 828 DVLOG(1) << "Failure: Checksum mismatch: calculated: " << checksum_b64 |
| 826 << "; expected: " << expected_checksum_b64 | 829 << "; expected: " << expected_checksum_b64 |
| 827 << "; store: " << *this; | 830 << "; store: " << *this; |
| 828 #endif | 831 #endif |
| 829 return false; | 832 return false; |
| 830 } | 833 } |
| 831 } | 834 } |
| 832 return true; | 835 return true; |
| 833 } | 836 } |
| 834 | 837 |
| 838 int V4Store::RecordAndReturnFileSize(const std::string& base_metric) { | |
| 839 std::string suffix = GetUmaSuffixForStore(store_path_); | |
| 840 // Histogram properties as in UMA_HISTOGRAM_COUNTS macro. | |
| 841 base::HistogramBase* histogram = base::Histogram::FactoryGet( | |
| 842 base_metric + suffix, 1, 1000000, 50, | |
| 843 base::HistogramBase::kUmaTargetedHistogramFlag); | |
| 844 if (histogram) { | |
| 845 const int file_size_kilobytes = static_cast<int>(file_size_ / 1024); | |
| 846 histogram->Add(file_size_kilobytes); | |
| 847 } | |
| 848 return file_size_; | |
| 849 } | |
| 850 | |
| 835 } // namespace safe_browsing | 851 } // namespace safe_browsing |
| OLD | NEW |