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_store_file.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
6 | 6 |
7 #include "base/md5.h" | 7 #include "base/md5.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 // static | 172 // static |
173 void SafeBrowsingStoreFile::RecordFormatEvent(FormatEventType event_type) { | 173 void SafeBrowsingStoreFile::RecordFormatEvent(FormatEventType event_type) { |
174 UMA_HISTOGRAM_ENUMERATION("SB2.FormatEvent", event_type, FORMAT_EVENT_MAX); | 174 UMA_HISTOGRAM_ENUMERATION("SB2.FormatEvent", event_type, FORMAT_EVENT_MAX); |
175 } | 175 } |
176 | 176 |
177 // static | 177 // static |
178 void SafeBrowsingStoreFile::CheckForOriginalAndDelete( | 178 void SafeBrowsingStoreFile::CheckForOriginalAndDelete( |
179 const base::FilePath& current_filename) { | 179 const base::FilePath& current_filename) { |
180 const base::FilePath original_filename( | 180 const base::FilePath original_filename( |
181 current_filename.DirName().AppendASCII("Safe Browsing")); | 181 current_filename.DirName().AppendASCII("Safe Browsing")); |
182 if (file_util::PathExists(original_filename)) { | 182 if (base::PathExists(original_filename)) { |
183 int64 size = 0; | 183 int64 size = 0; |
184 if (file_util::GetFileSize(original_filename, &size)) { | 184 if (file_util::GetFileSize(original_filename, &size)) { |
185 UMA_HISTOGRAM_COUNTS("SB2.OldDatabaseKilobytes", | 185 UMA_HISTOGRAM_COUNTS("SB2.OldDatabaseKilobytes", |
186 static_cast<int>(size / 1024)); | 186 static_cast<int>(size / 1024)); |
187 } | 187 } |
188 | 188 |
189 if (base::Delete(original_filename, false)) { | 189 if (base::Delete(original_filename, false)) { |
190 RecordFormatEvent(FORMAT_EVENT_DELETED_ORIGINAL); | 190 RecordFormatEvent(FORMAT_EVENT_DELETED_ORIGINAL); |
191 } else { | 191 } else { |
192 RecordFormatEvent(FORMAT_EVENT_DELETED_ORIGINAL_FAILED); | 192 RecordFormatEvent(FORMAT_EVENT_DELETED_ORIGINAL_FAILED); |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 const base::FilePath new_filename = TemporaryFileForFilename(filename_); | 399 const base::FilePath new_filename = TemporaryFileForFilename(filename_); |
400 file_util::ScopedFILE new_file(file_util::OpenFile(new_filename, "wb+")); | 400 file_util::ScopedFILE new_file(file_util::OpenFile(new_filename, "wb+")); |
401 if (new_file.get() == NULL) | 401 if (new_file.get() == NULL) |
402 return false; | 402 return false; |
403 | 403 |
404 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb")); | 404 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb")); |
405 empty_ = (file.get() == NULL); | 405 empty_ = (file.get() == NULL); |
406 if (empty_) { | 406 if (empty_) { |
407 // If the file exists but cannot be opened, try to delete it (not | 407 // If the file exists but cannot be opened, try to delete it (not |
408 // deleting directly, the bloom filter needs to be deleted, too). | 408 // deleting directly, the bloom filter needs to be deleted, too). |
409 if (file_util::PathExists(filename_)) | 409 if (base::PathExists(filename_)) |
410 return OnCorruptDatabase(); | 410 return OnCorruptDatabase(); |
411 | 411 |
412 new_file_.swap(new_file); | 412 new_file_.swap(new_file); |
413 return true; | 413 return true; |
414 } | 414 } |
415 | 415 |
416 FileHeader header; | 416 FileHeader header; |
417 if (!ReadItem(&header, file.get(), NULL)) | 417 if (!ReadItem(&header, file.get(), NULL)) |
418 return OnCorruptDatabase(); | 418 return OnCorruptDatabase(); |
419 | 419 |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 if (!WriteItem(digest, new_file_.get(), NULL)) | 648 if (!WriteItem(digest, new_file_.get(), NULL)) |
649 return false; | 649 return false; |
650 | 650 |
651 // Trim any excess left over from the temporary chunk data. | 651 // Trim any excess left over from the temporary chunk data. |
652 if (!file_util::TruncateFile(new_file_.get())) | 652 if (!file_util::TruncateFile(new_file_.get())) |
653 return false; | 653 return false; |
654 | 654 |
655 // Close the file handle and swizzle the file into place. | 655 // Close the file handle and swizzle the file into place. |
656 new_file_.reset(); | 656 new_file_.reset(); |
657 if (!base::Delete(filename_, false) && | 657 if (!base::Delete(filename_, false) && |
658 file_util::PathExists(filename_)) | 658 base::PathExists(filename_)) |
659 return false; | 659 return false; |
660 | 660 |
661 const base::FilePath new_filename = TemporaryFileForFilename(filename_); | 661 const base::FilePath new_filename = TemporaryFileForFilename(filename_); |
662 if (!base::Move(new_filename, filename_)) | 662 if (!base::Move(new_filename, filename_)) |
663 return false; | 663 return false; |
664 | 664 |
665 // Record counts before swapping to caller. | 665 // Record counts before swapping to caller. |
666 UMA_HISTOGRAM_COUNTS("SB2.AddPrefixes", add_prefixes.size()); | 666 UMA_HISTOGRAM_COUNTS("SB2.AddPrefixes", add_prefixes.size()); |
667 UMA_HISTOGRAM_COUNTS("SB2.SubPrefixes", sub_prefixes.size()); | 667 UMA_HISTOGRAM_COUNTS("SB2.SubPrefixes", sub_prefixes.size()); |
668 | 668 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 add_del_cache_.insert(chunk_id); | 729 add_del_cache_.insert(chunk_id); |
730 } | 730 } |
731 | 731 |
732 void SafeBrowsingStoreFile::DeleteSubChunk(int32 chunk_id) { | 732 void SafeBrowsingStoreFile::DeleteSubChunk(int32 chunk_id) { |
733 sub_del_cache_.insert(chunk_id); | 733 sub_del_cache_.insert(chunk_id); |
734 } | 734 } |
735 | 735 |
736 // static | 736 // static |
737 bool SafeBrowsingStoreFile::DeleteStore(const base::FilePath& basename) { | 737 bool SafeBrowsingStoreFile::DeleteStore(const base::FilePath& basename) { |
738 if (!base::Delete(basename, false) && | 738 if (!base::Delete(basename, false) && |
739 file_util::PathExists(basename)) { | 739 base::PathExists(basename)) { |
740 NOTREACHED(); | 740 NOTREACHED(); |
741 return false; | 741 return false; |
742 } | 742 } |
743 | 743 |
744 const base::FilePath new_filename = TemporaryFileForFilename(basename); | 744 const base::FilePath new_filename = TemporaryFileForFilename(basename); |
745 if (!base::Delete(new_filename, false) && | 745 if (!base::Delete(new_filename, false) && |
746 file_util::PathExists(new_filename)) { | 746 base::PathExists(new_filename)) { |
747 NOTREACHED(); | 747 NOTREACHED(); |
748 return false; | 748 return false; |
749 } | 749 } |
750 | 750 |
751 // With SQLite support gone, one way to get to this code is if the | 751 // With SQLite support gone, one way to get to this code is if the |
752 // existing file is a SQLite file. Make sure the journal file is | 752 // existing file is a SQLite file. Make sure the journal file is |
753 // also removed. | 753 // also removed. |
754 const base::FilePath journal_filename( | 754 const base::FilePath journal_filename( |
755 basename.value() + FILE_PATH_LITERAL("-journal")); | 755 basename.value() + FILE_PATH_LITERAL("-journal")); |
756 if (file_util::PathExists(journal_filename)) | 756 if (base::PathExists(journal_filename)) |
757 base::Delete(journal_filename, false); | 757 base::Delete(journal_filename, false); |
758 | 758 |
759 return true; | 759 return true; |
760 } | 760 } |
OLD | NEW |