Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: net/disk_cache/simple/simple_synchronous_entry.cc

Issue 2867693004: Snapshot of all changes to get jumbo in blink and content.
Patch Set: Rebased again Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/disk_cache/simple/simple_index_file.cc ('k') | net/dns/dns_query.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "net/disk_cache/simple/simple_synchronous_entry.h" 5 #include "net/disk_cache/simple/simple_synchronous_entry.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstring> 8 #include <cstring>
9 #include <functional> 9 #include <functional>
10 #include <limits> 10 #include <limits>
11 11
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/hash.h" 14 #include "base/hash.h"
15 #include "base/location.h" 15 #include "base/location.h"
16 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
17 #include "base/numerics/safe_conversions.h" 17 #include "base/numerics/safe_conversions.h"
18 #include "base/sha1.h" 18 #include "base/sha1.h"
19 #include "base/timer/elapsed_timer.h" 19 #include "base/timer/elapsed_timer.h"
20 #include "crypto/secure_hash.h" 20 #include "crypto/secure_hash.h"
21 #include "net/base/hash_value.h" 21 #include "net/base/hash_value.h"
22 #include "net/base/io_buffer.h" 22 #include "net/base/io_buffer.h"
23 #include "net/base/net_errors.h" 23 #include "net/base/net_errors.h"
24 #include "net/disk_cache/simple/simple_backend_version.h" 24 #include "net/disk_cache/simple/simple_backend_version.h"
25 #include "net/disk_cache/simple/simple_histogram_macros.h" 25 #include "net/disk_cache/simple/simple_histogram_macros.h"
26 #include "net/disk_cache/simple/simple_util.h" 26 #include "net/disk_cache/simple/simple_util.h"
27 #include "third_party/zlib/zlib.h" 27 #include "third_party/zlib/zlib.h"
28 28
29 using base::File;
30 using base::FilePath; 29 using base::FilePath;
31 using base::Time; 30 using base::Time;
32 31
33 namespace { 32 namespace {
34 33
35 // Used in histograms, please only add entries at the end. 34 // Used in histograms, please only add entries at the end.
36 enum OpenEntryResult { 35 enum OpenEntryResult {
37 OPEN_ENTRY_SUCCESS = 0, 36 OPEN_ENTRY_SUCCESS = 0,
38 OPEN_ENTRY_PLATFORM_FILE_ERROR = 1, 37 OPEN_ENTRY_PLATFORM_FILE_ERROR = 1,
39 OPEN_ENTRY_CANT_READ_HEADER = 2, 38 OPEN_ENTRY_CANT_READ_HEADER = 2,
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 static_cast<int>(KeySHA256Result::MAX)); 117 static_cast<int>(KeySHA256Result::MAX));
119 } 118 }
120 119
121 bool CanOmitEmptyFile(int file_index) { 120 bool CanOmitEmptyFile(int file_index) {
122 DCHECK_GE(file_index, 0); 121 DCHECK_GE(file_index, 0);
123 DCHECK_LT(file_index, disk_cache::kSimpleEntryFileCount); 122 DCHECK_LT(file_index, disk_cache::kSimpleEntryFileCount);
124 return file_index == disk_cache::simple_util::GetFileIndexFromStreamIndex(2); 123 return file_index == disk_cache::simple_util::GetFileIndexFromStreamIndex(2);
125 } 124 }
126 125
127 bool TruncatePath(const FilePath& filename_to_truncate) { 126 bool TruncatePath(const FilePath& filename_to_truncate) {
128 File file_to_truncate; 127 base::File file_to_truncate;
129 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | 128 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_W RITE |
130 File::FLAG_SHARE_DELETE; 129 base::File::FLAG_SHARE_DELETE;
131 file_to_truncate.Initialize(filename_to_truncate, flags); 130 file_to_truncate.Initialize(filename_to_truncate, flags);
132 if (!file_to_truncate.IsValid()) 131 if (!file_to_truncate.IsValid())
133 return false; 132 return false;
134 if (!file_to_truncate.SetLength(0)) 133 if (!file_to_truncate.SetLength(0))
135 return false; 134 return false;
136 return true; 135 return true;
137 } 136 }
138 137
139 void CalculateSHA256OfKey(const std::string& key, 138 void CalculateSHA256OfKey(const std::string& key,
140 net::SHA256HashValue* out_hash_value) { 139 net::SHA256HashValue* out_hash_value) {
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 if (empty_file_omitted_[file_index]) { 416 if (empty_file_omitted_[file_index]) {
418 // Don't create a new file if the entry has been doomed, to avoid it being 417 // Don't create a new file if the entry has been doomed, to avoid it being
419 // mixed up with a newly-created entry with the same key. 418 // mixed up with a newly-created entry with the same key.
420 if (doomed) { 419 if (doomed) {
421 DLOG(WARNING) << "Rejecting write to lazily omitted stream " 420 DLOG(WARNING) << "Rejecting write to lazily omitted stream "
422 << in_entry_op.index << " of doomed cache entry."; 421 << in_entry_op.index << " of doomed cache entry.";
423 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_STREAM_ENTRY_DOOMED); 422 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_STREAM_ENTRY_DOOMED);
424 *out_result = net::ERR_CACHE_WRITE_FAILURE; 423 *out_result = net::ERR_CACHE_WRITE_FAILURE;
425 return; 424 return;
426 } 425 }
427 File::Error error; 426 base::File::Error error;
428 if (!MaybeCreateFile(file_index, FILE_REQUIRED, &error)) { 427 if (!MaybeCreateFile(file_index, FILE_REQUIRED, &error)) {
429 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_CREATE_FAILURE); 428 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_CREATE_FAILURE);
430 Doom(); 429 Doom();
431 *out_result = net::ERR_CACHE_WRITE_FAILURE; 430 *out_result = net::ERR_CACHE_WRITE_FAILURE;
432 return; 431 return;
433 } 432 }
434 CreateEntryResult result; 433 CreateEntryResult result;
435 if (!InitializeCreatedFile(file_index, &result)) { 434 if (!InitializeCreatedFile(file_index, &result)) {
436 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_INITIALIZE_FAILURE); 435 RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_INITIALIZE_FAILURE);
437 Doom(); 436 Doom();
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 } 828 }
830 829
831 SimpleSynchronousEntry::~SimpleSynchronousEntry() { 830 SimpleSynchronousEntry::~SimpleSynchronousEntry() {
832 DCHECK(!(have_open_files_ && initialized_)); 831 DCHECK(!(have_open_files_ && initialized_));
833 if (have_open_files_) 832 if (have_open_files_)
834 CloseFiles(); 833 CloseFiles();
835 } 834 }
836 835
837 bool SimpleSynchronousEntry::MaybeOpenFile( 836 bool SimpleSynchronousEntry::MaybeOpenFile(
838 int file_index, 837 int file_index,
839 File::Error* out_error) { 838 base::File::Error* out_error) {
840 DCHECK(out_error); 839 DCHECK(out_error);
841 840
842 FilePath filename = GetFilenameFromFileIndex(file_index); 841 FilePath filename = GetFilenameFromFileIndex(file_index);
843 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | 842 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_W RITE |
844 File::FLAG_SHARE_DELETE; 843 base::File::FLAG_SHARE_DELETE;
845 files_[file_index].Initialize(filename, flags); 844 files_[file_index].Initialize(filename, flags);
846 *out_error = files_[file_index].error_details(); 845 *out_error = files_[file_index].error_details();
847 846
848 if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() && 847 if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() &&
849 *out_error == File::FILE_ERROR_NOT_FOUND) { 848 *out_error == base::File::FILE_ERROR_NOT_FOUND) {
850 empty_file_omitted_[file_index] = true; 849 empty_file_omitted_[file_index] = true;
851 return true; 850 return true;
852 } 851 }
853 852
854 return files_[file_index].IsValid(); 853 return files_[file_index].IsValid();
855 } 854 }
856 855
857 bool SimpleSynchronousEntry::MaybeCreateFile( 856 bool SimpleSynchronousEntry::MaybeCreateFile(
858 int file_index, 857 int file_index,
859 FileRequired file_required, 858 FileRequired file_required,
860 File::Error* out_error) { 859 base::File::Error* out_error) {
861 DCHECK(out_error); 860 DCHECK(out_error);
862 861
863 if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) { 862 if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) {
864 empty_file_omitted_[file_index] = true; 863 empty_file_omitted_[file_index] = true;
865 return true; 864 return true;
866 } 865 }
867 866
868 FilePath filename = GetFilenameFromFileIndex(file_index); 867 FilePath filename = GetFilenameFromFileIndex(file_index);
869 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE | 868 int flags = base::File::FLAG_CREATE | base::File::FLAG_READ | base::File::FLAG _WRITE |
870 File::FLAG_SHARE_DELETE; 869 base::File::FLAG_SHARE_DELETE;
871 files_[file_index].Initialize(filename, flags); 870 files_[file_index].Initialize(filename, flags);
872 871
873 // It's possible that the creation failed because someone deleted the 872 // It's possible that the creation failed because someone deleted the
874 // directory (e.g. because someone pressed "clear cache" on Android). 873 // directory (e.g. because someone pressed "clear cache" on Android).
875 // If so, we would keep failing for a while until periodic index snapshot 874 // If so, we would keep failing for a while until periodic index snapshot
876 // re-creates the cache dir, so try to recover from it quickly here. 875 // re-creates the cache dir, so try to recover from it quickly here.
877 if (!files_[file_index].IsValid() && 876 if (!files_[file_index].IsValid() &&
878 files_[file_index].error_details() == File::FILE_ERROR_NOT_FOUND && 877 files_[file_index].error_details() == base::File::FILE_ERROR_NOT_FOUND &&
879 !base::DirectoryExists(path_)) { 878 !base::DirectoryExists(path_)) {
880 if (base::CreateDirectory(path_)) 879 if (base::CreateDirectory(path_))
881 files_[file_index].Initialize(filename, flags); 880 files_[file_index].Initialize(filename, flags);
882 } 881 }
883 882
884 *out_error = files_[file_index].error_details(); 883 *out_error = files_[file_index].error_details();
885 empty_file_omitted_[file_index] = false; 884 empty_file_omitted_[file_index] = false;
886 885
887 return files_[file_index].IsValid(); 886 return files_[file_index].IsValid();
888 } 887 }
889 888
890 bool SimpleSynchronousEntry::OpenFiles(SimpleEntryStat* out_entry_stat) { 889 bool SimpleSynchronousEntry::OpenFiles(SimpleEntryStat* out_entry_stat) {
891 for (int i = 0; i < kSimpleEntryFileCount; ++i) { 890 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
892 File::Error error; 891 base::File::Error error;
893 if (!MaybeOpenFile(i, &error)) { 892 if (!MaybeOpenFile(i, &error)) {
894 // TODO(juliatuttle,gavinp): Remove one each of these triplets of 893 // TODO(juliatuttle,gavinp): Remove one each of these triplets of
895 // histograms. We can calculate the third as the sum or difference of the 894 // histograms. We can calculate the third as the sum or difference of the
896 // other two. 895 // other two.
897 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_PLATFORM_FILE_ERROR, 896 RecordSyncOpenResult(cache_type_, OPEN_ENTRY_PLATFORM_FILE_ERROR,
898 had_index_); 897 had_index_);
899 SIMPLE_CACHE_UMA(ENUMERATION, 898 SIMPLE_CACHE_UMA(ENUMERATION,
900 "SyncOpenPlatformFileError", cache_type_, 899 "SyncOpenPlatformFileError", cache_type_,
901 -error, -base::File::FILE_ERROR_MAX); 900 -error, -base::File::FILE_ERROR_MAX);
902 if (had_index_) { 901 if (had_index_) {
(...skipping 14 matching lines...) Expand all
917 916
918 have_open_files_ = true; 917 have_open_files_ = true;
919 918
920 base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch(); 919 base::TimeDelta entry_age = base::Time::Now() - base::Time::UnixEpoch();
921 for (int i = 0; i < kSimpleEntryFileCount; ++i) { 920 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
922 if (empty_file_omitted_[i]) { 921 if (empty_file_omitted_[i]) {
923 out_entry_stat->set_data_size(i + 1, 0); 922 out_entry_stat->set_data_size(i + 1, 0);
924 continue; 923 continue;
925 } 924 }
926 925
927 File::Info file_info; 926 base::File::Info file_info;
928 bool success = files_[i].GetInfo(&file_info); 927 bool success = files_[i].GetInfo(&file_info);
929 base::Time file_last_modified; 928 base::Time file_last_modified;
930 if (!success) { 929 if (!success) {
931 DLOG(WARNING) << "Could not get platform file info."; 930 DLOG(WARNING) << "Could not get platform file info.";
932 continue; 931 continue;
933 } 932 }
934 out_entry_stat->set_last_used(file_info.last_accessed); 933 out_entry_stat->set_last_used(file_info.last_accessed);
935 out_entry_stat->set_last_modified(file_info.last_modified); 934 out_entry_stat->set_last_modified(file_info.last_modified);
936 935
937 base::TimeDelta stream_age = 936 base::TimeDelta stream_age =
(...skipping 25 matching lines...) Expand all
963 "SyncOpenEntryAge", cache_type_, 962 "SyncOpenEntryAge", cache_type_,
964 entry_age.InHours(), 1, 1000, 50); 963 entry_age.InHours(), 1, 1000, 50);
965 964
966 files_created_ = false; 965 files_created_ = false;
967 966
968 return true; 967 return true;
969 } 968 }
970 969
971 bool SimpleSynchronousEntry::CreateFiles(SimpleEntryStat* out_entry_stat) { 970 bool SimpleSynchronousEntry::CreateFiles(SimpleEntryStat* out_entry_stat) {
972 for (int i = 0; i < kSimpleEntryFileCount; ++i) { 971 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
973 File::Error error; 972 base::File::Error error;
974 if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) { 973 if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) {
975 // TODO(juliatuttle,gavinp): Remove one each of these triplets of 974 // TODO(juliatuttle,gavinp): Remove one each of these triplets of
976 // histograms. We can calculate the third as the sum or difference of the 975 // histograms. We can calculate the third as the sum or difference of the
977 // other two. 976 // other two.
978 RecordSyncCreateResult(CREATE_ENTRY_PLATFORM_FILE_ERROR, had_index_); 977 RecordSyncCreateResult(CREATE_ENTRY_PLATFORM_FILE_ERROR, had_index_);
979 SIMPLE_CACHE_UMA(ENUMERATION, 978 SIMPLE_CACHE_UMA(ENUMERATION,
980 "SyncCreatePlatformFileError", cache_type_, 979 "SyncCreatePlatformFileError", cache_type_,
981 -error, -base::File::FILE_ERROR_MAX); 980 -error, -base::File::FILE_ERROR_MAX);
982 if (had_index_) { 981 if (had_index_) {
983 SIMPLE_CACHE_UMA(ENUMERATION, 982 SIMPLE_CACHE_UMA(ENUMERATION,
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
1302 1301
1303 int SimpleSynchronousEntry::GetEOFRecordData(int index, 1302 int SimpleSynchronousEntry::GetEOFRecordData(int index,
1304 const SimpleEntryStat& entry_stat, 1303 const SimpleEntryStat& entry_stat,
1305 bool* out_has_crc32, 1304 bool* out_has_crc32,
1306 bool* out_has_key_sha256, 1305 bool* out_has_key_sha256,
1307 uint32_t* out_crc32, 1306 uint32_t* out_crc32,
1308 int32_t* out_data_size) const { 1307 int32_t* out_data_size) const {
1309 SimpleFileEOF eof_record; 1308 SimpleFileEOF eof_record;
1310 int file_offset = entry_stat.GetEOFOffsetInFile(key_.size(), index); 1309 int file_offset = entry_stat.GetEOFOffsetInFile(key_.size(), index);
1311 int file_index = GetFileIndexFromStreamIndex(index); 1310 int file_index = GetFileIndexFromStreamIndex(index);
1312 File* file = const_cast<File*>(&files_[file_index]); 1311 base::File* file = const_cast<base::File*>(&files_[file_index]);
1313 if (file->Read(file_offset, reinterpret_cast<char*>(&eof_record), 1312 if (file->Read(file_offset, reinterpret_cast<char*>(&eof_record),
1314 sizeof(eof_record)) != 1313 sizeof(eof_record)) !=
1315 sizeof(eof_record)) { 1314 sizeof(eof_record)) {
1316 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); 1315 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE);
1317 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; 1316 return net::ERR_CACHE_CHECKSUM_READ_FAILURE;
1318 } 1317 }
1319 1318
1320 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { 1319 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) {
1321 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); 1320 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH);
1322 DVLOG(1) << "EOF record had bad magic number."; 1321 DVLOG(1) << "EOF record had bad magic number.";
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1402 return path_.AppendASCII( 1401 return path_.AppendASCII(
1403 GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index)); 1402 GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index));
1404 } 1403 }
1405 1404
1406 bool SimpleSynchronousEntry::OpenSparseFileIfExists( 1405 bool SimpleSynchronousEntry::OpenSparseFileIfExists(
1407 int32_t* out_sparse_data_size) { 1406 int32_t* out_sparse_data_size) {
1408 DCHECK(!sparse_file_open()); 1407 DCHECK(!sparse_file_open());
1409 1408
1410 FilePath filename = path_.AppendASCII( 1409 FilePath filename = path_.AppendASCII(
1411 GetSparseFilenameFromEntryHash(entry_hash_)); 1410 GetSparseFilenameFromEntryHash(entry_hash_));
1412 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | 1411 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_W RITE |
1413 File::FLAG_SHARE_DELETE; 1412 base::File::FLAG_SHARE_DELETE;
1414 sparse_file_.Initialize(filename, flags); 1413 sparse_file_.Initialize(filename, flags);
1415 if (sparse_file_.IsValid()) 1414 if (sparse_file_.IsValid())
1416 return ScanSparseFile(out_sparse_data_size); 1415 return ScanSparseFile(out_sparse_data_size);
1417 1416
1418 return sparse_file_.error_details() == File::FILE_ERROR_NOT_FOUND; 1417 return sparse_file_.error_details() == base::File::FILE_ERROR_NOT_FOUND;
1419 } 1418 }
1420 1419
1421 bool SimpleSynchronousEntry::CreateSparseFile() { 1420 bool SimpleSynchronousEntry::CreateSparseFile() {
1422 DCHECK(!sparse_file_open()); 1421 DCHECK(!sparse_file_open());
1423 1422
1424 FilePath filename = path_.AppendASCII( 1423 FilePath filename = path_.AppendASCII(
1425 GetSparseFilenameFromEntryHash(entry_hash_)); 1424 GetSparseFilenameFromEntryHash(entry_hash_));
1426 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE | 1425 int flags = base::File::FLAG_CREATE | base::File::FLAG_READ | base::File::FLAG _WRITE |
1427 File::FLAG_SHARE_DELETE; 1426 base::File::FLAG_SHARE_DELETE;
1428 sparse_file_.Initialize(filename, flags); 1427 sparse_file_.Initialize(filename, flags);
1429 if (!sparse_file_.IsValid()) 1428 if (!sparse_file_.IsValid())
1430 return false; 1429 return false;
1431 1430
1432 return InitializeSparseFile(); 1431 return InitializeSparseFile();
1433 } 1432 }
1434 1433
1435 void SimpleSynchronousEntry::CloseSparseFile() { 1434 void SimpleSynchronousEntry::CloseSparseFile() {
1436 DCHECK(sparse_file_open()); 1435 DCHECK(sparse_file_open());
1437 sparse_file_.Close(); 1436 sparse_file_.Close();
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
1651 range.offset = offset; 1650 range.offset = offset;
1652 range.length = len; 1651 range.length = len;
1653 range.data_crc32 = data_crc32; 1652 range.data_crc32 = data_crc32;
1654 range.file_offset = data_file_offset; 1653 range.file_offset = data_file_offset;
1655 sparse_ranges_.insert(std::make_pair(offset, range)); 1654 sparse_ranges_.insert(std::make_pair(offset, range));
1656 1655
1657 return true; 1656 return true;
1658 } 1657 }
1659 1658
1660 } // namespace disk_cache 1659 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index_file.cc ('k') | net/dns/dns_query.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698