| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/entry_impl.h" | 5 #include "net/disk_cache/entry_impl.h" |
| 6 | 6 |
| 7 #include "base/histogram.h" | 7 #include "base/histogram.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 namespace disk_cache { | 77 namespace disk_cache { |
| 78 | 78 |
| 79 EntryImpl::EntryImpl(BackendImpl* backend, Addr address) | 79 EntryImpl::EntryImpl(BackendImpl* backend, Addr address) |
| 80 : entry_(NULL, Addr(0)), node_(NULL, Addr(0)) { | 80 : entry_(NULL, Addr(0)), node_(NULL, Addr(0)) { |
| 81 entry_.LazyInit(backend->File(address), address); | 81 entry_.LazyInit(backend->File(address), address); |
| 82 doomed_ = false; | 82 doomed_ = false; |
| 83 backend_ = backend; | 83 backend_ = backend; |
| 84 for (int i = 0; i < kNumStreams; i++) { | 84 for (int i = 0; i < kNumStreams; i++) { |
| 85 unreported_size_[i] = 0; | 85 unreported_size_[i] = 0; |
| 86 } | 86 } |
| 87 key_file_ = NULL; |
| 87 } | 88 } |
| 88 | 89 |
| 89 // When an entry is deleted from the cache, we clean up all the data associated | 90 // When an entry is deleted from the cache, we clean up all the data associated |
| 90 // with it for two reasons: to simplify the reuse of the block (we know that any | 91 // with it for two reasons: to simplify the reuse of the block (we know that any |
| 91 // unused block is filled with zeros), and to simplify the handling of write / | 92 // unused block is filled with zeros), and to simplify the handling of write / |
| 92 // read partial information from an entry (don't have to worry about returning | 93 // read partial information from an entry (don't have to worry about returning |
| 93 // data related to a previous cache entry because the range was not fully | 94 // data related to a previous cache entry because the range was not fully |
| 94 // written before). | 95 // written before). |
| 95 EntryImpl::~EntryImpl() { | 96 EntryImpl::~EntryImpl() { |
| 96 // Save the sparse info to disk before deleting this entry. | 97 // Save the sparse info to disk before deleting this entry. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 SetPointerForInvalidEntry(backend_->GetCurrentEntryId()); | 139 SetPointerForInvalidEntry(backend_->GetCurrentEntryId()); |
| 139 backend_->InternalDoomEntry(this); | 140 backend_->InternalDoomEntry(this); |
| 140 } | 141 } |
| 141 | 142 |
| 142 void EntryImpl::Close() { | 143 void EntryImpl::Close() { |
| 143 Release(); | 144 Release(); |
| 144 } | 145 } |
| 145 | 146 |
| 146 std::string EntryImpl::GetKey() const { | 147 std::string EntryImpl::GetKey() const { |
| 147 CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_); | 148 CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_); |
| 148 if (entry->Data()->key_len > kMaxInternalKeyLength) { | 149 if (entry->Data()->key_len <= kMaxInternalKeyLength) |
| 149 Addr address(entry->Data()->long_key); | 150 return std::string(entry->Data()->key); |
| 150 DCHECK(address.is_initialized()); | 151 |
| 152 Addr address(entry->Data()->long_key); |
| 153 DCHECK(address.is_initialized()); |
| 154 size_t offset = 0; |
| 155 if (address.is_block_file()) |
| 156 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; |
| 157 |
| 158 if (!key_file_) { |
| 159 // We keep a copy of the file needed to access the key so that we can |
| 160 // always return this object's key, even if the backend is disabled. |
| 151 COMPILE_ASSERT(kNumStreams == kKeyFileIndex, invalid_key_index); | 161 COMPILE_ASSERT(kNumStreams == kKeyFileIndex, invalid_key_index); |
| 152 File* file = const_cast<EntryImpl*>(this)->GetBackingFile(address, | 162 key_file_ = const_cast<EntryImpl*>(this)->GetBackingFile(address, |
| 153 kKeyFileIndex); | 163 kKeyFileIndex); |
| 164 } |
| 154 | 165 |
| 155 size_t offset = 0; | 166 std::string key; |
| 156 if (address.is_block_file()) | 167 if (!key_file_ || |
| 157 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; | 168 !key_file_->Read(WriteInto(&key, entry->Data()->key_len + 1), |
| 158 | 169 entry->Data()->key_len + 1, offset)) |
| 159 std::string key; | 170 key.clear(); |
| 160 if (!file || !file->Read(WriteInto(&key, entry->Data()->key_len + 1), | 171 return key; |
| 161 entry->Data()->key_len + 1, offset)) | |
| 162 key.clear(); | |
| 163 return key; | |
| 164 } else { | |
| 165 return std::string(entry->Data()->key); | |
| 166 } | |
| 167 } | 172 } |
| 168 | 173 |
| 169 Time EntryImpl::GetLastUsed() const { | 174 Time EntryImpl::GetLastUsed() const { |
| 170 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); | 175 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); |
| 171 return Time::FromInternalValue(node->Data()->last_used); | 176 return Time::FromInternalValue(node->Data()->last_used); |
| 172 } | 177 } |
| 173 | 178 |
| 174 Time EntryImpl::GetLastModified() const { | 179 Time EntryImpl::GetLastModified() const { |
| 175 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); | 180 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); |
| 176 return Time::FromInternalValue(node->Data()->last_modified); | 181 return Time::FromInternalValue(node->Data()->last_modified); |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 | 407 |
| 403 entry_store->hash = hash; | 408 entry_store->hash = hash; |
| 404 entry_store->creation_time = Time::Now().ToInternalValue(); | 409 entry_store->creation_time = Time::Now().ToInternalValue(); |
| 405 entry_store->key_len = static_cast<int32>(key.size()); | 410 entry_store->key_len = static_cast<int32>(key.size()); |
| 406 if (entry_store->key_len > kMaxInternalKeyLength) { | 411 if (entry_store->key_len > kMaxInternalKeyLength) { |
| 407 Addr address(0); | 412 Addr address(0); |
| 408 if (!CreateBlock(entry_store->key_len + 1, &address)) | 413 if (!CreateBlock(entry_store->key_len + 1, &address)) |
| 409 return false; | 414 return false; |
| 410 | 415 |
| 411 entry_store->long_key = address.value(); | 416 entry_store->long_key = address.value(); |
| 412 File* file = GetBackingFile(address, kKeyFileIndex); | 417 key_file_ = GetBackingFile(address, kKeyFileIndex); |
| 413 | 418 |
| 414 size_t offset = 0; | 419 size_t offset = 0; |
| 415 if (address.is_block_file()) | 420 if (address.is_block_file()) |
| 416 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; | 421 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; |
| 417 | 422 |
| 418 if (!file || !file->Write(key.data(), key.size(), offset)) { | 423 if (!key_file_ || !key_file_->Write(key.data(), key.size(), offset)) { |
| 419 DeleteData(address, kKeyFileIndex); | 424 DeleteData(address, kKeyFileIndex); |
| 420 return false; | 425 return false; |
| 421 } | 426 } |
| 422 | 427 |
| 423 if (address.is_separate_file()) | 428 if (address.is_separate_file()) |
| 424 file->SetLength(key.size() + 1); | 429 key_file_->SetLength(key.size() + 1); |
| 425 } else { | 430 } else { |
| 426 memcpy(entry_store->key, key.data(), key.size()); | 431 memcpy(entry_store->key, key.data(), key.size()); |
| 427 entry_store->key[key.size()] = '\0'; | 432 entry_store->key[key.size()] = '\0'; |
| 428 } | 433 } |
| 429 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); | 434 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); |
| 430 node->dirty = backend_->GetCurrentEntryId(); | 435 node->dirty = backend_->GetCurrentEntryId(); |
| 431 Log("Create Entry "); | 436 Log("Create Entry "); |
| 432 return true; | 437 return true; |
| 433 } | 438 } |
| 434 | 439 |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), | 907 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), |
| 903 entry_.address().value(), node_.address().value()); | 908 entry_.address().value(), node_.address().value()); |
| 904 | 909 |
| 905 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], | 910 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], |
| 906 entry_.Data()->data_addr[1], entry_.Data()->long_key); | 911 entry_.Data()->data_addr[1], entry_.Data()->long_key); |
| 907 | 912 |
| 908 Trace(" doomed: %d 0x%p 0x%x", doomed_, pointer, dirty); | 913 Trace(" doomed: %d 0x%p 0x%x", doomed_, pointer, dirty); |
| 909 } | 914 } |
| 910 | 915 |
| 911 } // namespace disk_cache | 916 } // namespace disk_cache |
| OLD | NEW |