| 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 "net/disk_cache/blockfile/entry_impl.h" | 5 #include "net/disk_cache/blockfile/entry_impl.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/hash.h" | 9 #include "base/hash.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/disk_cache/blockfile/backend_impl.h" | 15 #include "net/disk_cache/blockfile/backend_impl.h" |
| 16 #include "net/disk_cache/blockfile/bitmap.h" | 16 #include "net/disk_cache/blockfile/bitmap.h" |
| 17 #include "net/disk_cache/blockfile/disk_format.h" | 17 #include "net/disk_cache/blockfile/disk_format.h" |
| 18 #include "net/disk_cache/blockfile/histogram_macros.h" | 18 #include "net/disk_cache/blockfile/histogram_macros.h" |
| 19 #include "net/disk_cache/blockfile/sparse_control.h" | 19 #include "net/disk_cache/blockfile/sparse_control.h" |
| 20 #include "net/disk_cache/cache_util.h" | 20 #include "net/disk_cache/cache_util.h" |
| 21 #include "net/disk_cache/net_log_parameters.h" | 21 #include "net/disk_cache/net_log_parameters.h" |
| 22 #include "net/log/net_log_event_type.h" |
| 23 #include "net/log/net_log_source_type.h" |
| 22 | 24 |
| 23 // Provide a BackendImpl object to macros from histogram_macros.h. | 25 // Provide a BackendImpl object to macros from histogram_macros.h. |
| 24 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_ | 26 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_ |
| 25 | 27 |
| 26 using base::Time; | 28 using base::Time; |
| 27 using base::TimeDelta; | 29 using base::TimeDelta; |
| 28 using base::TimeTicks; | 30 using base::TimeTicks; |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 // Index for the file used to store the key, if any (files_[kKeyFileIndex]). | 34 // Index for the file used to store the key, if any (files_[kKeyFileIndex]). |
| 33 const int kKeyFileIndex = 3; | 35 const int kKeyFileIndex = 3; |
| 34 | 36 |
| 35 // This class implements FileIOCallback to buffer the callback from a file IO | 37 // This class implements FileIOCallback to buffer the callback from a file IO |
| 36 // operation from the actual net class. | 38 // operation from the actual net class. |
| 37 class SyncCallback: public disk_cache::FileIOCallback { | 39 class SyncCallback: public disk_cache::FileIOCallback { |
| 38 public: | 40 public: |
| 39 // |end_event_type| is the event type to log on completion. Logs nothing on | 41 // |end_event_type| is the event type to log on completion. Logs nothing on |
| 40 // discard, or when the NetLog is not set to log all events. | 42 // discard, or when the NetLog is not set to log all events. |
| 41 SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer, | 43 SyncCallback(disk_cache::EntryImpl* entry, |
| 44 net::IOBuffer* buffer, |
| 42 const net::CompletionCallback& callback, | 45 const net::CompletionCallback& callback, |
| 43 net::NetLog::EventType end_event_type) | 46 net::NetLogEventType end_event_type) |
| 44 : entry_(entry), callback_(callback), buf_(buffer), | 47 : entry_(entry), |
| 45 start_(TimeTicks::Now()), end_event_type_(end_event_type) { | 48 callback_(callback), |
| 49 buf_(buffer), |
| 50 start_(TimeTicks::Now()), |
| 51 end_event_type_(end_event_type) { |
| 46 entry->AddRef(); | 52 entry->AddRef(); |
| 47 entry->IncrementIoCount(); | 53 entry->IncrementIoCount(); |
| 48 } | 54 } |
| 49 ~SyncCallback() override {} | 55 ~SyncCallback() override {} |
| 50 | 56 |
| 51 void OnFileIOComplete(int bytes_copied) override; | 57 void OnFileIOComplete(int bytes_copied) override; |
| 52 void Discard(); | 58 void Discard(); |
| 53 | 59 |
| 54 private: | 60 private: |
| 55 disk_cache::EntryImpl* entry_; | 61 disk_cache::EntryImpl* entry_; |
| 56 net::CompletionCallback callback_; | 62 net::CompletionCallback callback_; |
| 57 scoped_refptr<net::IOBuffer> buf_; | 63 scoped_refptr<net::IOBuffer> buf_; |
| 58 TimeTicks start_; | 64 TimeTicks start_; |
| 59 const net::NetLog::EventType end_event_type_; | 65 const net::NetLogEventType end_event_type_; |
| 60 | 66 |
| 61 DISALLOW_COPY_AND_ASSIGN(SyncCallback); | 67 DISALLOW_COPY_AND_ASSIGN(SyncCallback); |
| 62 }; | 68 }; |
| 63 | 69 |
| 64 void SyncCallback::OnFileIOComplete(int bytes_copied) { | 70 void SyncCallback::OnFileIOComplete(int bytes_copied) { |
| 65 entry_->DecrementIoCount(); | 71 entry_->DecrementIoCount(); |
| 66 if (!callback_.is_null()) { | 72 if (!callback_.is_null()) { |
| 67 if (entry_->net_log().IsCapturing()) { | 73 if (entry_->net_log().IsCapturing()) { |
| 68 entry_->net_log().EndEvent( | 74 entry_->net_log().EndEvent( |
| 69 end_event_type_, | 75 end_event_type_, |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 return; | 318 return; |
| 313 | 319 |
| 314 SetPointerForInvalidEntry(backend_->GetCurrentEntryId()); | 320 SetPointerForInvalidEntry(backend_->GetCurrentEntryId()); |
| 315 backend_->InternalDoomEntry(this); | 321 backend_->InternalDoomEntry(this); |
| 316 } | 322 } |
| 317 | 323 |
| 318 int EntryImpl::ReadDataImpl(int index, int offset, IOBuffer* buf, int buf_len, | 324 int EntryImpl::ReadDataImpl(int index, int offset, IOBuffer* buf, int buf_len, |
| 319 const CompletionCallback& callback) { | 325 const CompletionCallback& callback) { |
| 320 if (net_log_.IsCapturing()) { | 326 if (net_log_.IsCapturing()) { |
| 321 net_log_.BeginEvent( | 327 net_log_.BeginEvent( |
| 322 net::NetLog::TYPE_ENTRY_READ_DATA, | 328 net::NetLogEventType::ENTRY_READ_DATA, |
| 323 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false)); | 329 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false)); |
| 324 } | 330 } |
| 325 | 331 |
| 326 int result = InternalReadData(index, offset, buf, buf_len, callback); | 332 int result = InternalReadData(index, offset, buf, buf_len, callback); |
| 327 | 333 |
| 328 if (result != net::ERR_IO_PENDING && net_log_.IsCapturing()) { | 334 if (result != net::ERR_IO_PENDING && net_log_.IsCapturing()) { |
| 329 net_log_.EndEvent( | 335 net_log_.EndEvent(net::NetLogEventType::ENTRY_READ_DATA, |
| 330 net::NetLog::TYPE_ENTRY_READ_DATA, | 336 CreateNetLogReadWriteCompleteCallback(result)); |
| 331 CreateNetLogReadWriteCompleteCallback(result)); | |
| 332 } | 337 } |
| 333 return result; | 338 return result; |
| 334 } | 339 } |
| 335 | 340 |
| 336 int EntryImpl::WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len, | 341 int EntryImpl::WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len, |
| 337 const CompletionCallback& callback, | 342 const CompletionCallback& callback, |
| 338 bool truncate) { | 343 bool truncate) { |
| 339 if (net_log_.IsCapturing()) { | 344 if (net_log_.IsCapturing()) { |
| 340 net_log_.BeginEvent( | 345 net_log_.BeginEvent( |
| 341 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 346 net::NetLogEventType::ENTRY_WRITE_DATA, |
| 342 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); | 347 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); |
| 343 } | 348 } |
| 344 | 349 |
| 345 int result = InternalWriteData(index, offset, buf, buf_len, callback, | 350 int result = InternalWriteData(index, offset, buf, buf_len, callback, |
| 346 truncate); | 351 truncate); |
| 347 | 352 |
| 348 if (result != net::ERR_IO_PENDING && net_log_.IsCapturing()) { | 353 if (result != net::ERR_IO_PENDING && net_log_.IsCapturing()) { |
| 349 net_log_.EndEvent( | 354 net_log_.EndEvent(net::NetLogEventType::ENTRY_WRITE_DATA, |
| 350 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 355 CreateNetLogReadWriteCompleteCallback(result)); |
| 351 CreateNetLogReadWriteCompleteCallback(result)); | |
| 352 } | 356 } |
| 353 return result; | 357 return result; |
| 354 } | 358 } |
| 355 | 359 |
| 356 int EntryImpl::ReadSparseDataImpl(int64_t offset, | 360 int EntryImpl::ReadSparseDataImpl(int64_t offset, |
| 357 IOBuffer* buf, | 361 IOBuffer* buf, |
| 358 int buf_len, | 362 int buf_len, |
| 359 const CompletionCallback& callback) { | 363 const CompletionCallback& callback) { |
| 360 DCHECK(node_.Data()->dirty || read_only_); | 364 DCHECK(node_.Data()->dirty || read_only_); |
| 361 int result = InitSparseData(); | 365 int result = InitSparseData(); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 | 463 |
| 460 bool EntryImpl::IsSameEntry(const std::string& key, uint32_t hash) { | 464 bool EntryImpl::IsSameEntry(const std::string& key, uint32_t hash) { |
| 461 if (entry_.Data()->hash != hash || | 465 if (entry_.Data()->hash != hash || |
| 462 static_cast<size_t>(entry_.Data()->key_len) != key.size()) | 466 static_cast<size_t>(entry_.Data()->key_len) != key.size()) |
| 463 return false; | 467 return false; |
| 464 | 468 |
| 465 return (key.compare(GetKey()) == 0); | 469 return (key.compare(GetKey()) == 0); |
| 466 } | 470 } |
| 467 | 471 |
| 468 void EntryImpl::InternalDoom() { | 472 void EntryImpl::InternalDoom() { |
| 469 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM); | 473 net_log_.AddEvent(net::NetLogEventType::ENTRY_DOOM); |
| 470 DCHECK(node_.HasData()); | 474 DCHECK(node_.HasData()); |
| 471 if (!node_.Data()->dirty) { | 475 if (!node_.Data()->dirty) { |
| 472 node_.Data()->dirty = backend_->GetCurrentEntryId(); | 476 node_.Data()->dirty = backend_->GetCurrentEntryId(); |
| 473 node_.Store(); | 477 node_.Store(); |
| 474 } | 478 } |
| 475 doomed_ = true; | 479 doomed_ = true; |
| 476 } | 480 } |
| 477 | 481 |
| 478 void EntryImpl::DeleteEntryData(bool everything) { | 482 void EntryImpl::DeleteEntryData(bool everything) { |
| 479 DCHECK(doomed_ || !everything); | 483 DCHECK(doomed_ || !everything); |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 case kWriteAsync1: | 728 case kWriteAsync1: |
| 725 CACHE_UMA(AGE_MS, "AsyncWriteDispatchTime", 0, start); | 729 CACHE_UMA(AGE_MS, "AsyncWriteDispatchTime", 0, start); |
| 726 break; | 730 break; |
| 727 default: | 731 default: |
| 728 NOTREACHED(); | 732 NOTREACHED(); |
| 729 } | 733 } |
| 730 } | 734 } |
| 731 | 735 |
| 732 void EntryImpl::BeginLogging(net::NetLog* net_log, bool created) { | 736 void EntryImpl::BeginLogging(net::NetLog* net_log, bool created) { |
| 733 DCHECK(!net_log_.net_log()); | 737 DCHECK(!net_log_.net_log()); |
| 734 net_log_ = net::BoundNetLog::Make( | 738 net_log_ = |
| 735 net_log, net::NetLog::SOURCE_DISK_CACHE_ENTRY); | 739 net::BoundNetLog::Make(net_log, net::NetLogSourceType::DISK_CACHE_ENTRY); |
| 736 net_log_.BeginEvent( | 740 net_log_.BeginEvent(net::NetLogEventType::DISK_CACHE_ENTRY_IMPL, |
| 737 net::NetLog::TYPE_DISK_CACHE_ENTRY_IMPL, | 741 CreateNetLogEntryCreationCallback(this, created)); |
| 738 CreateNetLogEntryCreationCallback(this, created)); | |
| 739 } | 742 } |
| 740 | 743 |
| 741 const net::BoundNetLog& EntryImpl::net_log() const { | 744 const net::BoundNetLog& EntryImpl::net_log() const { |
| 742 return net_log_; | 745 return net_log_; |
| 743 } | 746 } |
| 744 | 747 |
| 745 // static | 748 // static |
| 746 int EntryImpl::NumBlocksForEntry(int key_size) { | 749 int EntryImpl::NumBlocksForEntry(int key_size) { |
| 747 // The longest key that can be stored using one block. | 750 // The longest key that can be stored using one block. |
| 748 int key1_len = | 751 int key1_len = |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 944 | 947 |
| 945 // Remove this entry from the list of open entries. | 948 // Remove this entry from the list of open entries. |
| 946 backend_->OnEntryDestroyBegin(entry_.address()); | 949 backend_->OnEntryDestroyBegin(entry_.address()); |
| 947 | 950 |
| 948 if (doomed_) { | 951 if (doomed_) { |
| 949 DeleteEntryData(true); | 952 DeleteEntryData(true); |
| 950 } else { | 953 } else { |
| 951 #if defined(NET_BUILD_STRESS_CACHE) | 954 #if defined(NET_BUILD_STRESS_CACHE) |
| 952 SanityCheck(); | 955 SanityCheck(); |
| 953 #endif | 956 #endif |
| 954 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_CLOSE); | 957 net_log_.AddEvent(net::NetLogEventType::ENTRY_CLOSE); |
| 955 bool ret = true; | 958 bool ret = true; |
| 956 for (int index = 0; index < kNumStreams; index++) { | 959 for (int index = 0; index < kNumStreams; index++) { |
| 957 if (user_buffers_[index].get()) { | 960 if (user_buffers_[index].get()) { |
| 958 ret = Flush(index, 0); | 961 ret = Flush(index, 0); |
| 959 if (!ret) | 962 if (!ret) |
| 960 LOG(ERROR) << "Failed to save user data"; | 963 LOG(ERROR) << "Failed to save user data"; |
| 961 } | 964 } |
| 962 if (unreported_size_[index]) { | 965 if (unreported_size_[index]) { |
| 963 backend_->ModifyStorageSize( | 966 backend_->ModifyStorageSize( |
| 964 entry_.Data()->data_size[index] - unreported_size_[index], | 967 entry_.Data()->data_size[index] - unreported_size_[index], |
| 965 entry_.Data()->data_size[index]); | 968 entry_.Data()->data_size[index]); |
| 966 } | 969 } |
| 967 } | 970 } |
| 968 | 971 |
| 969 if (!ret) { | 972 if (!ret) { |
| 970 // There was a failure writing the actual data. Mark the entry as dirty. | 973 // There was a failure writing the actual data. Mark the entry as dirty. |
| 971 int current_id = backend_->GetCurrentEntryId(); | 974 int current_id = backend_->GetCurrentEntryId(); |
| 972 node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1; | 975 node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1; |
| 973 node_.Store(); | 976 node_.Store(); |
| 974 } else if (node_.HasData() && !dirty_ && node_.Data()->dirty) { | 977 } else if (node_.HasData() && !dirty_ && node_.Data()->dirty) { |
| 975 node_.Data()->dirty = 0; | 978 node_.Data()->dirty = 0; |
| 976 node_.Store(); | 979 node_.Store(); |
| 977 } | 980 } |
| 978 } | 981 } |
| 979 | 982 |
| 980 Trace("~EntryImpl out 0x%p", reinterpret_cast<void*>(this)); | 983 Trace("~EntryImpl out 0x%p", reinterpret_cast<void*>(this)); |
| 981 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_ENTRY_IMPL); | 984 net_log_.EndEvent(net::NetLogEventType::DISK_CACHE_ENTRY_IMPL); |
| 982 backend_->OnEntryDestroyEnd(); | 985 backend_->OnEntryDestroyEnd(); |
| 983 } | 986 } |
| 984 | 987 |
| 985 // ------------------------------------------------------------------------ | 988 // ------------------------------------------------------------------------ |
| 986 | 989 |
| 987 int EntryImpl::InternalReadData(int index, int offset, | 990 int EntryImpl::InternalReadData(int index, int offset, |
| 988 IOBuffer* buf, int buf_len, | 991 IOBuffer* buf, int buf_len, |
| 989 const CompletionCallback& callback) { | 992 const CompletionCallback& callback) { |
| 990 DCHECK(node_.Data()->dirty || read_only_); | 993 DCHECK(node_.Data()->dirty || read_only_); |
| 991 DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len; | 994 DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1039 size_t file_offset = offset; | 1042 size_t file_offset = offset; |
| 1040 if (address.is_block_file()) { | 1043 if (address.is_block_file()) { |
| 1041 DCHECK_LE(offset + buf_len, kMaxBlockSize); | 1044 DCHECK_LE(offset + buf_len, kMaxBlockSize); |
| 1042 file_offset += address.start_block() * address.BlockSize() + | 1045 file_offset += address.start_block() * address.BlockSize() + |
| 1043 kBlockHeaderSize; | 1046 kBlockHeaderSize; |
| 1044 } | 1047 } |
| 1045 | 1048 |
| 1046 SyncCallback* io_callback = NULL; | 1049 SyncCallback* io_callback = NULL; |
| 1047 if (!callback.is_null()) { | 1050 if (!callback.is_null()) { |
| 1048 io_callback = new SyncCallback(this, buf, callback, | 1051 io_callback = new SyncCallback(this, buf, callback, |
| 1049 net::NetLog::TYPE_ENTRY_READ_DATA); | 1052 net::NetLogEventType::ENTRY_READ_DATA); |
| 1050 } | 1053 } |
| 1051 | 1054 |
| 1052 TimeTicks start_async = TimeTicks::Now(); | 1055 TimeTicks start_async = TimeTicks::Now(); |
| 1053 | 1056 |
| 1054 bool completed; | 1057 bool completed; |
| 1055 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) { | 1058 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) { |
| 1056 if (io_callback) | 1059 if (io_callback) |
| 1057 io_callback->Discard(); | 1060 io_callback->Discard(); |
| 1058 DoomImpl(); | 1061 DoomImpl(); |
| 1059 return net::ERR_CACHE_READ_FAILURE; | 1062 return net::ERR_CACHE_READ_FAILURE; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1143 if (!file->SetLength(offset + buf_len)) | 1146 if (!file->SetLength(offset + buf_len)) |
| 1144 return net::ERR_FAILED; | 1147 return net::ERR_FAILED; |
| 1145 } | 1148 } |
| 1146 | 1149 |
| 1147 if (!buf_len) | 1150 if (!buf_len) |
| 1148 return 0; | 1151 return 0; |
| 1149 | 1152 |
| 1150 SyncCallback* io_callback = NULL; | 1153 SyncCallback* io_callback = NULL; |
| 1151 if (!callback.is_null()) { | 1154 if (!callback.is_null()) { |
| 1152 io_callback = new SyncCallback(this, buf, callback, | 1155 io_callback = new SyncCallback(this, buf, callback, |
| 1153 net::NetLog::TYPE_ENTRY_WRITE_DATA); | 1156 net::NetLogEventType::ENTRY_WRITE_DATA); |
| 1154 } | 1157 } |
| 1155 | 1158 |
| 1156 TimeTicks start_async = TimeTicks::Now(); | 1159 TimeTicks start_async = TimeTicks::Now(); |
| 1157 | 1160 |
| 1158 bool completed; | 1161 bool completed; |
| 1159 if (!file->Write(buf->data(), buf_len, file_offset, io_callback, | 1162 if (!file->Write(buf->data(), buf_len, file_offset, io_callback, |
| 1160 &completed)) { | 1163 &completed)) { |
| 1161 if (io_callback) | 1164 if (io_callback) |
| 1162 io_callback->Discard(); | 1165 io_callback->Discard(); |
| 1163 return net::ERR_CACHE_WRITE_FAILURE; | 1166 return net::ERR_CACHE_WRITE_FAILURE; |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1558 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), | 1561 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), |
| 1559 entry_.address().value(), node_.address().value()); | 1562 entry_.address().value(), node_.address().value()); |
| 1560 | 1563 |
| 1561 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], | 1564 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], |
| 1562 entry_.Data()->data_addr[1], entry_.Data()->long_key); | 1565 entry_.Data()->data_addr[1], entry_.Data()->long_key); |
| 1563 | 1566 |
| 1564 Trace(" doomed: %d 0x%x", doomed_, dirty); | 1567 Trace(" doomed: %d 0x%x", doomed_, dirty); |
| 1565 } | 1568 } |
| 1566 | 1569 |
| 1567 } // namespace disk_cache | 1570 } // namespace disk_cache |
| OLD | NEW |