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" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 // static | 166 // static |
167 void SimpleIndex::InsertInEntrySet( | 167 void SimpleIndex::InsertInEntrySet( |
168 const disk_cache::EntryMetadata& entry_metadata, | 168 const disk_cache::EntryMetadata& entry_metadata, |
169 EntrySet* entry_set) { | 169 EntrySet* entry_set) { |
170 DCHECK(entry_set); | 170 DCHECK(entry_set); |
171 entry_set->insert( | 171 entry_set->insert( |
172 std::make_pair(entry_metadata.GetHashKey(), entry_metadata)); | 172 std::make_pair(entry_metadata.GetHashKey(), entry_metadata)); |
173 } | 173 } |
174 | 174 |
175 void SimpleIndex::PostponeWritingToDisk() { | 175 void SimpleIndex::PostponeWritingToDisk() { |
176 if (!initialized_) | |
177 return; | |
176 const base::TimeDelta file_age = base::Time::Now() - last_write_to_disk_; | 178 const base::TimeDelta file_age = base::Time::Now() - last_write_to_disk_; |
177 if (file_age > base::TimeDelta::FromSeconds(kMaxWriteToDiskDelaySecs) && | 179 if (file_age > base::TimeDelta::FromSeconds(kMaxWriteToDiskDelaySecs) && |
178 write_to_disk_timer_.IsRunning()) { | 180 write_to_disk_timer_.IsRunning()) { |
179 // If the index file is too old and there is a timer programmed to run a | 181 // 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 | 182 // WriteToDisk soon, we don't postpone it, so we always WriteToDisk |
181 // approximately every kMaxWriteToDiskDelaySecs. | 183 // approximately every kMaxWriteToDiskDelaySecs. |
182 return; | 184 return; |
183 } | 185 } |
184 | 186 |
185 // If the timer is already active, Start() will just Reset it, postponing it. | 187 // If the timer is already active, Start() will just Reset it, postponing it. |
186 write_to_disk_timer_.Start( | 188 write_to_disk_timer_.Start( |
187 FROM_HERE, | 189 FROM_HERE, |
188 base::TimeDelta::FromSeconds(kWriteToDiskDelaySecs), | 190 base::TimeDelta::FromSeconds(kWriteToDiskDelaySecs), |
189 base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr())); | 191 base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr())); |
190 } | 192 } |
191 | 193 |
192 // static | 194 // static |
193 void SimpleIndex::LoadFromDisk( | 195 void SimpleIndex::LoadFromDisk( |
194 const base::FilePath& index_filename, | 196 const base::FilePath& index_filename, |
195 base::SingleThreadTaskRunner* io_thread, | 197 base::SingleThreadTaskRunner* io_thread, |
196 const IndexCompletionCallback& completion_callback) { | 198 const IndexCompletionCallback& completion_callback) { |
199 // TODO(felipeg): probably could load a stale index and use it for something. | |
197 scoped_ptr<EntrySet> index_file_entries = | 200 scoped_ptr<EntrySet> index_file_entries = |
198 SimpleIndexFile::LoadFromDisk(index_filename); | 201 SimpleIndexFile::LoadFromDisk(index_filename); |
199 | 202 |
200 bool force_index_flush = false; | 203 bool force_index_flush = false; |
gavinp
2013/04/19 12:23:13
This whole function is a lot easier to read. Thank
felipeg
2013/04/19 12:55:58
Done.
| |
201 if (!index_file_entries.get()) { | 204 if (!index_file_entries) { |
202 index_file_entries = SimpleIndex::RestoreFromDisk(index_filename); | 205 index_file_entries = SimpleIndex::RestoreFromDisk(index_filename); |
203 // When we restore from disk we write the merged index file to disk right | 206 // 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. | 207 // away, this might save us from having to restore again next time. |
205 force_index_flush = true; | 208 force_index_flush = true; |
206 } | 209 } |
207 | 210 |
208 io_thread->PostTask(FROM_HERE, | 211 io_thread->PostTask(FROM_HERE, |
209 base::Bind(completion_callback, | 212 base::Bind(completion_callback, |
210 base::Passed(&index_file_entries), | 213 base::Passed(&index_file_entries), |
211 force_index_flush)); | 214 force_index_flush)); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 cache_size_); | 327 cache_size_); |
325 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, | 328 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, |
326 entries_set_); | 329 entries_set_); |
327 cache_thread_->PostTask(FROM_HERE, base::Bind( | 330 cache_thread_->PostTask(FROM_HERE, base::Bind( |
328 &SimpleIndex::WriteToDiskInternal, | 331 &SimpleIndex::WriteToDiskInternal, |
329 index_filename_, | 332 index_filename_, |
330 base::Passed(&pickle))); | 333 base::Passed(&pickle))); |
331 } | 334 } |
332 | 335 |
333 } // namespace disk_cache | 336 } // namespace disk_cache |
OLD | NEW |