| OLD | NEW |
| 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_index.h" | 5 #include "net/disk_cache/simple/simple_index.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/file_util.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 14 #include "base/pickle.h" | 15 #include "base/pickle.h" |
| 15 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
| 16 #include "base/threading/worker_pool.h" | 17 #include "base/threading/worker_pool.h" |
| 17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 18 #include "net/disk_cache/simple/simple_entry_format.h" | 19 #include "net/disk_cache/simple/simple_entry_format.h" |
| 19 #include "net/disk_cache/simple/simple_index_file.h" | 20 #include "net/disk_cache/simple/simple_index_file.h" |
| 20 #include "net/disk_cache/simple/simple_util.h" | 21 #include "net/disk_cache/simple/simple_util.h" |
| 21 | 22 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // static | 167 // static |
| 167 void SimpleIndex::InsertInEntrySet( | 168 void SimpleIndex::InsertInEntrySet( |
| 168 const disk_cache::EntryMetadata& entry_metadata, | 169 const disk_cache::EntryMetadata& entry_metadata, |
| 169 EntrySet* entry_set) { | 170 EntrySet* entry_set) { |
| 170 DCHECK(entry_set); | 171 DCHECK(entry_set); |
| 171 entry_set->insert( | 172 entry_set->insert( |
| 172 std::make_pair(entry_metadata.GetHashKey(), entry_metadata)); | 173 std::make_pair(entry_metadata.GetHashKey(), entry_metadata)); |
| 173 } | 174 } |
| 174 | 175 |
| 175 void SimpleIndex::PostponeWritingToDisk() { | 176 void SimpleIndex::PostponeWritingToDisk() { |
| 177 if (!initialized_) |
| 178 return; |
| 176 const base::TimeDelta file_age = base::Time::Now() - last_write_to_disk_; | 179 const base::TimeDelta file_age = base::Time::Now() - last_write_to_disk_; |
| 177 if (file_age > base::TimeDelta::FromSeconds(kMaxWriteToDiskDelaySecs) && | 180 if (file_age > base::TimeDelta::FromSeconds(kMaxWriteToDiskDelaySecs) && |
| 178 write_to_disk_timer_.IsRunning()) { | 181 write_to_disk_timer_.IsRunning()) { |
| 179 // If the index file is too old and there is a timer programmed to run a | 182 // If the index file is too old and there is a timer programmed to run a |
| 180 // WriteToDisk soon, we don't postpone it, so we always WriteToDisk | 183 // WriteToDisk soon, we don't postpone it, so we always WriteToDisk |
| 181 // approximately every kMaxWriteToDiskDelaySecs. | 184 // approximately every kMaxWriteToDiskDelaySecs. |
| 182 return; | 185 return; |
| 183 } | 186 } |
| 184 | 187 |
| 185 // If the timer is already active, Start() will just Reset it, postponing it. | 188 // If the timer is already active, Start() will just Reset it, postponing it. |
| 186 write_to_disk_timer_.Start( | 189 write_to_disk_timer_.Start( |
| 187 FROM_HERE, | 190 FROM_HERE, |
| 188 base::TimeDelta::FromSeconds(kWriteToDiskDelaySecs), | 191 base::TimeDelta::FromSeconds(kWriteToDiskDelaySecs), |
| 189 base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr())); | 192 base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr())); |
| 190 } | 193 } |
| 191 | 194 |
| 192 // static | 195 // static |
| 193 void SimpleIndex::LoadFromDisk( | 196 void SimpleIndex::LoadFromDisk( |
| 194 const base::FilePath& index_filename, | 197 const base::FilePath& index_filename, |
| 195 base::SingleThreadTaskRunner* io_thread, | 198 base::SingleThreadTaskRunner* io_thread, |
| 196 const IndexCompletionCallback& completion_callback) { | 199 const IndexCompletionCallback& completion_callback) { |
| 197 scoped_ptr<EntrySet> index_file_entries = | 200 bool restore_from_disk = SimpleIndexFile::IndexFileIsStale(index_filename); |
| 198 SimpleIndexFile::LoadFromDisk(index_filename); | |
| 199 | 201 |
| 200 bool force_index_flush = false; | 202 scoped_ptr<EntrySet> index_file_entries; |
| 203 if (!restore_from_disk) { |
| 204 index_file_entries = SimpleIndexFile::LoadFromDisk(index_filename); |
| 205 } |
| 206 |
| 201 if (!index_file_entries.get()) { | 207 if (!index_file_entries.get()) { |
| 202 index_file_entries = SimpleIndex::RestoreFromDisk(index_filename); | 208 index_file_entries = SimpleIndex::RestoreFromDisk(index_filename); |
| 203 // When we restore from disk we write the merged index file to disk right | 209 // When we restore from disk we write the merged index file to disk right |
| 204 // away, this might save us from having to restore again next time. | 210 // away, this might save us from having to restore again next time. |
| 205 force_index_flush = true; | 211 restore_from_disk = true; |
| 206 } | 212 } |
| 207 | 213 |
| 208 io_thread->PostTask(FROM_HERE, | 214 io_thread->PostTask(FROM_HERE, |
| 209 base::Bind(completion_callback, | 215 base::Bind(completion_callback, |
| 210 base::Passed(&index_file_entries), | 216 base::Passed(&index_file_entries), |
| 211 force_index_flush)); | 217 restore_from_disk)); |
| 212 } | 218 } |
| 213 | 219 |
| 214 // static | 220 // static |
| 215 scoped_ptr<SimpleIndex::EntrySet> SimpleIndex::RestoreFromDisk( | 221 scoped_ptr<SimpleIndex::EntrySet> SimpleIndex::RestoreFromDisk( |
| 216 const base::FilePath& index_filename) { | 222 const base::FilePath& index_filename) { |
| 217 using file_util::FileEnumerator; | 223 using file_util::FileEnumerator; |
| 218 LOG(INFO) << "Simple Cache Index is being restored from disk."; | 224 LOG(INFO) << "Simple Cache Index is being restored from disk."; |
| 219 | 225 |
| 220 file_util::Delete(index_filename, /* recursive = */ false); | 226 file_util::Delete(index_filename, /* recursive = */ false); |
| 221 scoped_ptr<EntrySet> index_file_entries(new EntrySet()); | 227 scoped_ptr<EntrySet> index_file_entries(new EntrySet()); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 cache_size_); | 330 cache_size_); |
| 325 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, | 331 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, |
| 326 entries_set_); | 332 entries_set_); |
| 327 cache_thread_->PostTask(FROM_HERE, base::Bind( | 333 cache_thread_->PostTask(FROM_HERE, base::Bind( |
| 328 &SimpleIndex::WriteToDiskInternal, | 334 &SimpleIndex::WriteToDiskInternal, |
| 329 index_filename_, | 335 index_filename_, |
| 330 base::Passed(&pickle))); | 336 base::Passed(&pickle))); |
| 331 } | 337 } |
| 332 | 338 |
| 333 } // namespace disk_cache | 339 } // namespace disk_cache |
| OLD | NEW |