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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 | 212 |
| 213 ApplyUpdateResult V4Store::ProcessPartialUpdateAndWriteToDisk( | 213 ApplyUpdateResult V4Store::ProcessPartialUpdateAndWriteToDisk( |
| 214 const std::string& metric, | 214 const std::string& metric, |
| 215 const HashPrefixMap& hash_prefix_map_old, | 215 const HashPrefixMap& hash_prefix_map_old, |
| 216 std::unique_ptr<ListUpdateResponse> response) { | 216 std::unique_ptr<ListUpdateResponse> response) { |
| 217 DCHECK(response->has_response_type()); | 217 DCHECK(response->has_response_type()); |
| 218 DCHECK_EQ(ListUpdateResponse::PARTIAL_UPDATE, response->response_type()); | 218 DCHECK_EQ(ListUpdateResponse::PARTIAL_UPDATE, response->response_type()); |
| 219 | 219 |
| 220 ApplyUpdateResult result = | 220 ApplyUpdateResult result = |
| 221 ProcessUpdate(metric, hash_prefix_map_old, response); | 221 ProcessUpdate(metric, hash_prefix_map_old, response); |
| 222 if (result == APPLY_UPDATE_SUCCESS) { | |
| 223 Checksum checksum = response->checksum(); | |
| 224 response.reset(); | |
| 225 RecordStoreWriteResult(WriteToDisk(checksum)); | |
| 226 } | |
| 222 return result; | 227 return result; |
| 223 } | 228 } |
| 224 | 229 |
| 225 ApplyUpdateResult V4Store::ProcessFullUpdateAndWriteToDisk( | 230 ApplyUpdateResult V4Store::ProcessFullUpdateAndWriteToDisk( |
| 226 const std::string& metric, | 231 const std::string& metric, |
| 227 std::unique_ptr<ListUpdateResponse> response) { | 232 std::unique_ptr<ListUpdateResponse> response) { |
| 228 ApplyUpdateResult result = ProcessFullUpdate(metric, response); | 233 ApplyUpdateResult result = ProcessFullUpdate(metric, response); |
| 229 if (result == APPLY_UPDATE_SUCCESS) { | 234 if (result == APPLY_UPDATE_SUCCESS) { |
| 230 RecordStoreWriteResult(WriteToDisk(std::move(response))); | 235 Checksum checksum = response->checksum(); |
| 236 response.reset(); | |
| 237 RecordStoreWriteResult(WriteToDisk(checksum)); | |
| 231 } | 238 } |
| 232 return result; | 239 return result; |
| 233 } | 240 } |
| 234 | 241 |
| 235 ApplyUpdateResult V4Store::ProcessFullUpdate( | 242 ApplyUpdateResult V4Store::ProcessFullUpdate( |
| 236 const std::string& metric, | 243 const std::string& metric, |
| 237 const std::unique_ptr<ListUpdateResponse>& response) { | 244 const std::unique_ptr<ListUpdateResponse>& response) { |
| 238 DCHECK(response->has_response_type()); | 245 DCHECK(response->has_response_type()); |
| 239 DCHECK_EQ(ListUpdateResponse::FULL_UPDATE, response->response_type()); | 246 DCHECK_EQ(ListUpdateResponse::FULL_UPDATE, response->response_type()); |
| 240 // TODO(vakh): For a full update, we don't need to process the update in | 247 // TODO(vakh): For a full update, we don't need to process the update in |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 635 } | 642 } |
| 636 | 643 |
| 637 if (contents.empty()) { | 644 if (contents.empty()) { |
| 638 return FILE_EMPTY_FAILURE; | 645 return FILE_EMPTY_FAILURE; |
| 639 } | 646 } |
| 640 | 647 |
| 641 V4StoreFileFormat file_format; | 648 V4StoreFileFormat file_format; |
| 642 if (!file_format.ParseFromString(contents)) { | 649 if (!file_format.ParseFromString(contents)) { |
| 643 return PROTO_PARSING_FAILURE; | 650 return PROTO_PARSING_FAILURE; |
| 644 } | 651 } |
| 652 contents.clear(); | |
|
Nathan Parker
2016/10/14 00:10:05
I think this doesn't actually free memory. You pro
Scott Hess - ex-Googler
2016/10/14 00:16:18
The canonical way to release string memory is some
| |
| 645 | 653 |
| 646 if (file_format.magic_number() != kFileMagic) { | 654 if (file_format.magic_number() != kFileMagic) { |
| 647 return UNEXPECTED_MAGIC_NUMBER_FAILURE; | 655 return UNEXPECTED_MAGIC_NUMBER_FAILURE; |
| 648 } | 656 } |
| 649 | 657 |
| 650 UMA_HISTOGRAM_SPARSE_SLOWLY("SafeBrowsing.V4StoreVersionRead", | 658 UMA_HISTOGRAM_SPARSE_SLOWLY("SafeBrowsing.V4StoreVersionRead", |
| 651 file_format.version_number()); | 659 file_format.version_number()); |
| 652 if (file_format.version_number() != kFileVersion) { | 660 if (file_format.version_number() != kFileVersion) { |
| 653 return FILE_VERSION_INCOMPATIBLE_FAILURE; | 661 return FILE_VERSION_INCOMPATIBLE_FAILURE; |
| 654 } | 662 } |
| 655 | 663 |
| 656 if (!file_format.has_list_update_response()) { | 664 if (!file_format.has_list_update_response()) { |
| 657 return HASH_PREFIX_INFO_MISSING_FAILURE; | 665 return HASH_PREFIX_INFO_MISSING_FAILURE; |
| 658 } | 666 } |
| 659 | 667 |
| 660 std::unique_ptr<ListUpdateResponse> response(new ListUpdateResponse); | 668 std::unique_ptr<ListUpdateResponse> response(new ListUpdateResponse); |
| 661 response->Swap(file_format.mutable_list_update_response()); | 669 response->Swap(file_format.mutable_list_update_response()); |
| 662 ApplyUpdateResult apply_update_result = | 670 ApplyUpdateResult apply_update_result = |
| 663 ProcessFullUpdate(kReadFromDisk, response); | 671 ProcessFullUpdate(kReadFromDisk, response); |
| 664 RecordApplyUpdateResult(kReadFromDisk, apply_update_result, store_path_); | 672 RecordApplyUpdateResult(kReadFromDisk, apply_update_result, store_path_); |
| 665 if (apply_update_result != APPLY_UPDATE_SUCCESS) { | 673 if (apply_update_result != APPLY_UPDATE_SUCCESS) { |
| 666 hash_prefix_map_.clear(); | 674 hash_prefix_map_.clear(); |
| 667 return HASH_PREFIX_MAP_GENERATION_FAILURE; | 675 return HASH_PREFIX_MAP_GENERATION_FAILURE; |
| 668 } | 676 } |
| 669 RecordApplyUpdateTime(kReadFromDisk, TimeTicks::Now() - before, store_path_); | 677 RecordApplyUpdateTime(kReadFromDisk, TimeTicks::Now() - before, store_path_); |
| 670 | 678 |
| 671 return READ_SUCCESS; | 679 return READ_SUCCESS; |
| 672 } | 680 } |
| 673 | 681 |
| 674 StoreWriteResult V4Store::WriteToDisk( | 682 StoreWriteResult V4Store::WriteToDisk(const Checksum& checksum) const { |
| 675 std::unique_ptr<ListUpdateResponse> response) const { | 683 V4StoreFileFormat file_format; |
| 676 // Do not write partial updates to the disk. | 684 ListUpdateResponse* lur = file_format.mutable_list_update_response(); |
| 677 // After merging the updates, the ListUpdateResponse passed to this method | 685 *(lur->mutable_checksum()) = checksum; |
| 678 // should be a FULL_UPDATE. | 686 lur->set_new_client_state(state_); |
| 679 if (!response->has_response_type() || | 687 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); |
| 680 response->response_type() != ListUpdateResponse::FULL_UPDATE) { | 688 for (auto map_iter : hash_prefix_map_) { |
| 681 DVLOG(1) << "Failure: response->has_response_type(): " | 689 ThreatEntrySet* additions = lur->add_additions(); |
| 682 << response->has_response_type() | 690 // TODO(vakh): Write RICE encoded hash prefixes on disk. Not doing so |
| 683 << " : response->response_type(): " << response->response_type(); | 691 // currently since it takes a long time to decode them on startup, which |
| 684 return INVALID_RESPONSE_TYPE_FAILURE; | 692 // blocks resource load. See: http://crbug.com/654819 |
| 693 additions->set_compression_type(RAW); | |
| 694 additions->mutable_raw_hashes()->set_prefix_size(map_iter.first); | |
| 695 additions->mutable_raw_hashes()->set_raw_hashes(map_iter.second); | |
| 685 } | 696 } |
| 686 | 697 |
| 687 // Attempt writing to a temporary file first and at the end, swap the files. | 698 // Attempt writing to a temporary file first and at the end, swap the files. |
| 688 const base::FilePath new_filename = TemporaryFileForFilename(store_path_); | 699 const base::FilePath new_filename = TemporaryFileForFilename(store_path_); |
| 689 | 700 |
| 690 V4StoreFileFormat file_format; | |
| 691 file_format.set_magic_number(kFileMagic); | 701 file_format.set_magic_number(kFileMagic); |
| 692 file_format.set_version_number(kFileVersion); | 702 file_format.set_version_number(kFileVersion); |
| 693 ListUpdateResponse* response_to_write = | |
| 694 file_format.mutable_list_update_response(); | |
| 695 response_to_write->Swap(response.get()); | |
| 696 std::string file_format_string; | 703 std::string file_format_string; |
| 697 file_format.SerializeToString(&file_format_string); | 704 file_format.SerializeToString(&file_format_string); |
| 698 size_t written = base::WriteFile(new_filename, file_format_string.data(), | 705 size_t written = base::WriteFile(new_filename, file_format_string.data(), |
| 699 file_format_string.size()); | 706 file_format_string.size()); |
| 700 DCHECK_EQ(file_format_string.size(), written); | 707 DCHECK_EQ(file_format_string.size(), written); |
| 701 | 708 |
| 702 if (!base::Move(new_filename, store_path_)) { | 709 if (!base::Move(new_filename, store_path_)) { |
| 703 return UNABLE_TO_RENAME_FAILURE; | 710 return UNABLE_TO_RENAME_FAILURE; |
| 704 } | 711 } |
| 705 | 712 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 794 << "; expected: " << expected_checksum_b64 | 801 << "; expected: " << expected_checksum_b64 |
| 795 << "; store: " << *this; | 802 << "; store: " << *this; |
| 796 #endif | 803 #endif |
| 797 return false; | 804 return false; |
| 798 } | 805 } |
| 799 } | 806 } |
| 800 return true; | 807 return true; |
| 801 } | 808 } |
| 802 | 809 |
| 803 } // namespace safe_browsing | 810 } // namespace safe_browsing |
| OLD | NEW |