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_backend_impl.h" | 5 #include "net/disk_cache/simple/simple_backend_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 | 9 |
10 #if defined(OS_POSIX) | 10 #if defined(OS_POSIX) |
(...skipping 16 matching lines...) Expand all Loading... | |
27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
28 #include "net/disk_cache/backend_impl.h" | 28 #include "net/disk_cache/backend_impl.h" |
29 #include "net/disk_cache/simple/simple_entry_format.h" | 29 #include "net/disk_cache/simple/simple_entry_format.h" |
30 #include "net/disk_cache/simple/simple_entry_impl.h" | 30 #include "net/disk_cache/simple/simple_entry_impl.h" |
31 #include "net/disk_cache/simple/simple_histogram_macros.h" | 31 #include "net/disk_cache/simple/simple_histogram_macros.h" |
32 #include "net/disk_cache/simple/simple_index.h" | 32 #include "net/disk_cache/simple/simple_index.h" |
33 #include "net/disk_cache/simple/simple_index_file.h" | 33 #include "net/disk_cache/simple/simple_index_file.h" |
34 #include "net/disk_cache/simple/simple_synchronous_entry.h" | 34 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
35 #include "net/disk_cache/simple/simple_util.h" | 35 #include "net/disk_cache/simple/simple_util.h" |
36 | 36 |
37 using base::Callback; | |
37 using base::Closure; | 38 using base::Closure; |
38 using base::FilePath; | 39 using base::FilePath; |
39 using base::MessageLoopProxy; | 40 using base::MessageLoopProxy; |
40 using base::SequencedWorkerPool; | 41 using base::SequencedWorkerPool; |
41 using base::SingleThreadTaskRunner; | 42 using base::SingleThreadTaskRunner; |
42 using base::Time; | 43 using base::Time; |
43 using base::DirectoryExists; | 44 using base::DirectoryExists; |
44 using file_util::CreateDirectory; | 45 using file_util::CreateDirectory; |
45 | 46 |
46 namespace { | 47 namespace { |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 } | 194 } |
194 | 195 |
195 // A short bindable thunk that can call a completion callback. Intended to be | 196 // A short bindable thunk that can call a completion callback. Intended to be |
196 // used to post a task to run a callback after an operation completes. | 197 // used to post a task to run a callback after an operation completes. |
197 void CallCompletionCallback(const net::CompletionCallback& callback, | 198 void CallCompletionCallback(const net::CompletionCallback& callback, |
198 int error_code) { | 199 int error_code) { |
199 DCHECK(!callback.is_null()); | 200 DCHECK(!callback.is_null()); |
200 callback.Run(error_code); | 201 callback.Run(error_code); |
201 } | 202 } |
202 | 203 |
204 // A short bindable thunk that ensures a completion callback is always called | |
205 // after running an operation asynchronously. | |
206 void RunOperationAndCallback( | |
207 const Callback<int(const net::CompletionCallback&)>& operation, | |
208 const net::CompletionCallback& operation_callback) { | |
209 const int operation_result = operation.Run(operation_callback); | |
210 if (operation_result != net::ERR_IO_PENDING) | |
211 operation_callback.Run(operation_result); | |
212 } | |
213 | |
203 void RecordIndexLoad(net::CacheType cache_type, | 214 void RecordIndexLoad(net::CacheType cache_type, |
204 base::TimeTicks constructed_since, | 215 base::TimeTicks constructed_since, |
205 int result) { | 216 int result) { |
206 const base::TimeDelta creation_to_index = base::TimeTicks::Now() - | 217 const base::TimeDelta creation_to_index = base::TimeTicks::Now() - |
207 constructed_since; | 218 constructed_since; |
208 if (result == net::OK) { | 219 if (result == net::OK) { |
209 SIMPLE_CACHE_UMA(TIMES, "CreationToIndex", cache_type, creation_to_index); | 220 SIMPLE_CACHE_UMA(TIMES, "CreationToIndex", cache_type, creation_to_index); |
210 } else { | 221 } else { |
211 SIMPLE_CACHE_UMA(TIMES, | 222 SIMPLE_CACHE_UMA(TIMES, |
212 "CreationToIndexFail", cache_type, creation_to_index); | 223 "CreationToIndexFail", cache_type, creation_to_index); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 } | 278 } |
268 | 279 |
269 int SimpleBackendImpl::GetMaxFileSize() const { | 280 int SimpleBackendImpl::GetMaxFileSize() const { |
270 return index_->max_size() / kMaxFileRatio; | 281 return index_->max_size() / kMaxFileRatio; |
271 } | 282 } |
272 | 283 |
273 void SimpleBackendImpl::OnDeactivated(const SimpleEntryImpl* entry) { | 284 void SimpleBackendImpl::OnDeactivated(const SimpleEntryImpl* entry) { |
274 active_entries_.erase(entry->entry_hash()); | 285 active_entries_.erase(entry->entry_hash()); |
275 } | 286 } |
276 | 287 |
288 void SimpleBackendImpl::OnDoomStart(uint64 entry_hash) { | |
289 DCHECK_EQ(0u, entries_pending_doom_.count(entry_hash)); | |
290 entries_pending_doom_.insert( | |
291 std::make_pair(entry_hash, std::vector<Closure>())); | |
292 } | |
293 | |
294 void SimpleBackendImpl::OnDoomComplete(uint64 entry_hash) { | |
295 DCHECK_EQ(1u, entries_pending_doom_.count(entry_hash)); | |
296 base::hash_map<uint64, std::vector<Closure> >::iterator it = | |
297 entries_pending_doom_.find(entry_hash); | |
298 std::vector<Closure> to_run_closures; | |
299 to_run_closures.swap(it->second); | |
300 entries_pending_doom_.erase(it); | |
301 | |
302 std::for_each(to_run_closures.begin(), to_run_closures.end(), | |
303 std::mem_fun_ref(&Closure::Run)); | |
304 } | |
305 | |
277 net::CacheType SimpleBackendImpl::GetCacheType() const { | 306 net::CacheType SimpleBackendImpl::GetCacheType() const { |
278 return net::DISK_CACHE; | 307 return net::DISK_CACHE; |
279 } | 308 } |
280 | 309 |
281 int32 SimpleBackendImpl::GetEntryCount() const { | 310 int32 SimpleBackendImpl::GetEntryCount() const { |
282 // TODO(pasko): Use directory file count when index is not ready. | 311 // TODO(pasko): Use directory file count when index is not ready. |
283 return index_->GetEntryCount(); | 312 return index_->GetEntryCount(); |
284 } | 313 } |
285 | 314 |
286 int SimpleBackendImpl::OpenEntry(const std::string& key, | 315 int SimpleBackendImpl::OpenEntry(const std::string& key, |
287 Entry** entry, | 316 Entry** entry, |
288 const CompletionCallback& callback) { | 317 const CompletionCallback& callback) { |
289 scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveEntry(key); | 318 const uint64 entry_hash = simple_util::GetEntryHashKey(key); |
319 | |
320 base::hash_map<uint64, std::vector<Closure> >::iterator it = | |
Philippe
2013/09/11 15:41:31
I wonder if it would make sense to factor out this
Deprecated (see juliatuttle)
2013/09/11 19:03:21
I think it's a good idea.
gavinp
2013/09/11 19:20:53
I agree, but doing it is subtle enough that I didn
| |
321 entries_pending_doom_.find(entry_hash); | |
322 if (it != entries_pending_doom_.end()) { | |
323 Callback<int(const net::CompletionCallback&)> operation = | |
324 base::Bind(&SimpleBackendImpl::OpenEntry, | |
325 base::Unretained(this), key, entry); | |
326 it->second.push_back(base::Bind(&RunOperationAndCallback, | |
327 operation, callback)); | |
328 return net::ERR_IO_PENDING; | |
329 } | |
330 scoped_refptr<SimpleEntryImpl> simple_entry = | |
331 CreateOrFindActiveEntry(entry_hash, key); | |
290 CompletionCallback backend_callback = | 332 CompletionCallback backend_callback = |
291 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromKey, | 333 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromKey, |
292 AsWeakPtr(), | 334 AsWeakPtr(), |
293 key, | 335 key, |
294 entry, | 336 entry, |
295 simple_entry, | 337 simple_entry, |
296 callback); | 338 callback); |
297 return simple_entry->OpenEntry(entry, backend_callback); | 339 return simple_entry->OpenEntry(entry, backend_callback); |
298 } | 340 } |
299 | 341 |
300 int SimpleBackendImpl::CreateEntry(const std::string& key, | 342 int SimpleBackendImpl::CreateEntry(const std::string& key, |
301 Entry** entry, | 343 Entry** entry, |
302 const CompletionCallback& callback) { | 344 const CompletionCallback& callback) { |
303 DCHECK_LT(0u, key.size()); | 345 DCHECK_LT(0u, key.size()); |
304 scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveEntry(key); | 346 const uint64 entry_hash = simple_util::GetEntryHashKey(key); |
347 | |
348 base::hash_map<uint64, std::vector<Closure> >::iterator it = | |
349 entries_pending_doom_.find(entry_hash); | |
350 if (it != entries_pending_doom_.end()) { | |
351 Callback<int(const net::CompletionCallback&)> operation = | |
352 base::Bind(&SimpleBackendImpl::CreateEntry, | |
353 base::Unretained(this), key, entry); | |
354 it->second.push_back(base::Bind(&RunOperationAndCallback, | |
355 operation, callback)); | |
356 return net::ERR_IO_PENDING; | |
357 } | |
358 scoped_refptr<SimpleEntryImpl> simple_entry = | |
359 CreateOrFindActiveEntry(entry_hash, key); | |
305 return simple_entry->CreateEntry(entry, callback); | 360 return simple_entry->CreateEntry(entry, callback); |
306 } | 361 } |
307 | 362 |
308 int SimpleBackendImpl::DoomEntry(const std::string& key, | 363 int SimpleBackendImpl::DoomEntry(const std::string& key, |
309 const net::CompletionCallback& callback) { | 364 const net::CompletionCallback& callback) { |
310 scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveEntry(key); | 365 const uint64 entry_hash = simple_util::GetEntryHashKey(key); |
311 return simple_entry->DoomEntry(callback); | 366 |
367 base::hash_map<uint64, std::vector<Closure> >::iterator it = | |
368 entries_pending_doom_.find(entry_hash); | |
369 if (it != entries_pending_doom_.end()) { | |
370 Callback<int(const net::CompletionCallback&)> operation = | |
371 base::Bind(&SimpleBackendImpl::DoomEntry, base::Unretained(this), key); | |
372 it->second.push_back(base::Bind(&RunOperationAndCallback, | |
373 operation, callback)); | |
374 return net::ERR_IO_PENDING; | |
375 } | |
376 scoped_refptr<SimpleEntryImpl> simple_entry = | |
377 CreateOrFindActiveEntry(entry_hash, key); | |
378 return simple_entry->DoomEntry(callback); | |
312 } | 379 } |
313 | 380 |
314 int SimpleBackendImpl::DoomAllEntries(const CompletionCallback& callback) { | 381 int SimpleBackendImpl::DoomAllEntries(const CompletionCallback& callback) { |
315 return DoomEntriesBetween(Time(), Time(), callback); | 382 return DoomEntriesBetween(Time(), Time(), callback); |
316 } | 383 } |
317 | 384 |
318 void SimpleBackendImpl::IndexReadyForDoom(Time initial_time, | 385 void SimpleBackendImpl::IndexReadyForDoom(Time initial_time, |
319 Time end_time, | 386 Time end_time, |
320 const CompletionCallback& callback, | 387 const CompletionCallback& callback, |
321 int result) { | 388 int result) { |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
422 // TODO(pasko): Move PreferedCacheSize() to cache_util.h. Also fix the | 489 // TODO(pasko): Move PreferedCacheSize() to cache_util.h. Also fix the |
423 // spelling. | 490 // spelling. |
424 result.max_size = disk_cache::PreferedCacheSize(available); | 491 result.max_size = disk_cache::PreferedCacheSize(available); |
425 } | 492 } |
426 DCHECK(result.max_size); | 493 DCHECK(result.max_size); |
427 } | 494 } |
428 return result; | 495 return result; |
429 } | 496 } |
430 | 497 |
431 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry( | 498 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry( |
499 const uint64 entry_hash, | |
432 const std::string& key) { | 500 const std::string& key) { |
433 const uint64 entry_hash = simple_util::GetEntryHashKey(key); | 501 DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key)); |
434 | |
435 std::pair<EntryMap::iterator, bool> insert_result = | 502 std::pair<EntryMap::iterator, bool> insert_result = |
436 active_entries_.insert(std::make_pair(entry_hash, | 503 active_entries_.insert(std::make_pair(entry_hash, |
437 base::WeakPtr<SimpleEntryImpl>())); | 504 base::WeakPtr<SimpleEntryImpl>())); |
438 EntryMap::iterator& it = insert_result.first; | 505 EntryMap::iterator& it = insert_result.first; |
439 if (insert_result.second) | 506 if (insert_result.second) |
440 DCHECK(!it->second.get()); | 507 DCHECK(!it->second.get()); |
441 if (!it->second.get()) { | 508 if (!it->second.get()) { |
442 SimpleEntryImpl* entry = new SimpleEntryImpl( | 509 SimpleEntryImpl* entry = new SimpleEntryImpl( |
443 cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_); | 510 cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_); |
444 entry->SetKey(key); | 511 entry->SetKey(key); |
445 it->second = entry->AsWeakPtr(); | 512 it->second = entry->AsWeakPtr(); |
446 } | 513 } |
447 DCHECK(it->second.get()); | 514 DCHECK(it->second.get()); |
448 // It's possible, but unlikely, that we have an entry hash collision with a | 515 // It's possible, but unlikely, that we have an entry hash collision with a |
449 // currently active entry. | 516 // currently active entry. |
450 if (key != it->second->key()) { | 517 if (key != it->second->key()) { |
451 it->second->Doom(); | 518 it->second->Doom(); |
452 DCHECK_EQ(0U, active_entries_.count(entry_hash)); | 519 DCHECK_EQ(0U, active_entries_.count(entry_hash)); |
453 return CreateOrFindActiveEntry(key); | 520 return CreateOrFindActiveEntry(entry_hash, key); |
454 } | 521 } |
455 return make_scoped_refptr(it->second.get()); | 522 return make_scoped_refptr(it->second.get()); |
456 } | 523 } |
457 | 524 |
458 int SimpleBackendImpl::OpenEntryFromHash(uint64 hash, | 525 int SimpleBackendImpl::OpenEntryFromHash(uint64 hash, |
459 Entry** entry, | 526 Entry** entry, |
460 const CompletionCallback& callback) { | 527 const CompletionCallback& callback) { |
461 EntryMap::iterator has_active = active_entries_.find(hash); | 528 EntryMap::iterator has_active = active_entries_.find(hash); |
462 if (has_active != active_entries_.end()) | 529 if (has_active != active_entries_.end()) |
463 return OpenEntry(has_active->second->key(), entry, callback); | 530 return OpenEntry(has_active->second->key(), entry, callback); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
574 } | 641 } |
575 callback.Run(error_code); | 642 callback.Run(error_code); |
576 } | 643 } |
577 | 644 |
578 void SimpleBackendImpl::FlushWorkerPoolForTesting() { | 645 void SimpleBackendImpl::FlushWorkerPoolForTesting() { |
579 if (g_sequenced_worker_pool) | 646 if (g_sequenced_worker_pool) |
580 g_sequenced_worker_pool->FlushForTesting(); | 647 g_sequenced_worker_pool->FlushForTesting(); |
581 } | 648 } |
582 | 649 |
583 } // namespace disk_cache | 650 } // namespace disk_cache |
OLD | NEW |