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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 SetPointerForInvalidEntry(backend_->GetCurrentEntryId()); | 133 SetPointerForInvalidEntry(backend_->GetCurrentEntryId()); |
133 backend_->InternalDoomEntry(this); | 134 backend_->InternalDoomEntry(this); |
134 } | 135 } |
135 | 136 |
136 void EntryImpl::Close() { | 137 void EntryImpl::Close() { |
137 Release(); | 138 Release(); |
138 } | 139 } |
139 | 140 |
140 std::string EntryImpl::GetKey() const { | 141 std::string EntryImpl::GetKey() const { |
141 CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_); | 142 CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_); |
142 if (entry->Data()->key_len > kMaxInternalKeyLength) { | 143 if (entry->Data()->key_len <= kMaxInternalKeyLength) |
143 Addr address(entry->Data()->long_key); | 144 return std::string(entry->Data()->key); |
144 DCHECK(address.is_initialized()); | 145 |
| 146 Addr address(entry->Data()->long_key); |
| 147 DCHECK(address.is_initialized()); |
| 148 size_t offset = 0; |
| 149 if (address.is_block_file()) |
| 150 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; |
| 151 |
| 152 if (!key_file_) { |
| 153 // We keep a copy of the file needed to access the key so that we can |
| 154 // always return this object's key, even if the backend is disabled. |
145 COMPILE_ASSERT(kNumStreams == kKeyFileIndex, invalid_key_index); | 155 COMPILE_ASSERT(kNumStreams == kKeyFileIndex, invalid_key_index); |
146 File* file = const_cast<EntryImpl*>(this)->GetBackingFile(address, | 156 key_file_ = const_cast<EntryImpl*>(this)->GetBackingFile(address, |
147 kKeyFileIndex); | 157 kKeyFileIndex); |
| 158 } |
148 | 159 |
149 size_t offset = 0; | 160 std::string key; |
150 if (address.is_block_file()) | 161 if (!key_file_ || |
151 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; | 162 !key_file_->Read(WriteInto(&key, entry->Data()->key_len + 1), |
152 | 163 entry->Data()->key_len + 1, offset)) |
153 std::string key; | 164 key.clear(); |
154 if (!file || !file->Read(WriteInto(&key, entry->Data()->key_len + 1), | 165 return key; |
155 entry->Data()->key_len + 1, offset)) | |
156 key.clear(); | |
157 return key; | |
158 } else { | |
159 return std::string(entry->Data()->key); | |
160 } | |
161 } | 166 } |
162 | 167 |
163 Time EntryImpl::GetLastUsed() const { | 168 Time EntryImpl::GetLastUsed() const { |
164 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); | 169 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); |
165 return Time::FromInternalValue(node->Data()->last_used); | 170 return Time::FromInternalValue(node->Data()->last_used); |
166 } | 171 } |
167 | 172 |
168 Time EntryImpl::GetLastModified() const { | 173 Time EntryImpl::GetLastModified() const { |
169 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); | 174 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); |
170 return Time::FromInternalValue(node->Data()->last_modified); | 175 return Time::FromInternalValue(node->Data()->last_modified); |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 | 400 |
396 entry_store->hash = hash; | 401 entry_store->hash = hash; |
397 entry_store->creation_time = Time::Now().ToInternalValue(); | 402 entry_store->creation_time = Time::Now().ToInternalValue(); |
398 entry_store->key_len = static_cast<int32>(key.size()); | 403 entry_store->key_len = static_cast<int32>(key.size()); |
399 if (entry_store->key_len > kMaxInternalKeyLength) { | 404 if (entry_store->key_len > kMaxInternalKeyLength) { |
400 Addr address(0); | 405 Addr address(0); |
401 if (!CreateBlock(entry_store->key_len + 1, &address)) | 406 if (!CreateBlock(entry_store->key_len + 1, &address)) |
402 return false; | 407 return false; |
403 | 408 |
404 entry_store->long_key = address.value(); | 409 entry_store->long_key = address.value(); |
405 File* file = GetBackingFile(address, kKeyFileIndex); | 410 key_file_ = GetBackingFile(address, kKeyFileIndex); |
406 | 411 |
407 size_t offset = 0; | 412 size_t offset = 0; |
408 if (address.is_block_file()) | 413 if (address.is_block_file()) |
409 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; | 414 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; |
410 | 415 |
411 if (!file || !file->Write(key.data(), key.size(), offset)) { | 416 if (!key_file_ || !key_file_->Write(key.data(), key.size(), offset)) { |
412 DeleteData(address, kKeyFileIndex); | 417 DeleteData(address, kKeyFileIndex); |
413 return false; | 418 return false; |
414 } | 419 } |
415 | 420 |
416 if (address.is_separate_file()) | 421 if (address.is_separate_file()) |
417 file->SetLength(key.size() + 1); | 422 key_file_->SetLength(key.size() + 1); |
418 } else { | 423 } else { |
419 memcpy(entry_store->key, key.data(), key.size()); | 424 memcpy(entry_store->key, key.data(), key.size()); |
420 entry_store->key[key.size()] = '\0'; | 425 entry_store->key[key.size()] = '\0'; |
421 } | 426 } |
422 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); | 427 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); |
423 node->dirty = backend_->GetCurrentEntryId(); | 428 node->dirty = backend_->GetCurrentEntryId(); |
424 Log("Create Entry "); | 429 Log("Create Entry "); |
425 return true; | 430 return true; |
426 } | 431 } |
427 | 432 |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
889 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), | 894 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), |
890 entry_.address().value(), node_.address().value()); | 895 entry_.address().value(), node_.address().value()); |
891 | 896 |
892 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], | 897 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], |
893 entry_.Data()->data_addr[1], entry_.Data()->long_key); | 898 entry_.Data()->data_addr[1], entry_.Data()->long_key); |
894 | 899 |
895 Trace(" doomed: %d 0x%x", doomed_, dirty); | 900 Trace(" doomed: %d 0x%x", doomed_, dirty); |
896 } | 901 } |
897 | 902 |
898 } // namespace disk_cache | 903 } // namespace disk_cache |
OLD | NEW |