Chromium Code Reviews| 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 15 matching lines...) Expand all Loading... | |
| 26 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 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_index.h" | 31 #include "net/disk_cache/simple/simple_index.h" |
| 32 #include "net/disk_cache/simple/simple_index_file.h" | 32 #include "net/disk_cache/simple/simple_index_file.h" |
| 33 #include "net/disk_cache/simple/simple_synchronous_entry.h" | 33 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
| 34 #include "net/disk_cache/simple/simple_util.h" | 34 #include "net/disk_cache/simple/simple_util.h" |
| 35 | 35 |
| 36 using base::Callback; | |
| 36 using base::Closure; | 37 using base::Closure; |
| 37 using base::FilePath; | 38 using base::FilePath; |
| 38 using base::MessageLoopProxy; | 39 using base::MessageLoopProxy; |
| 39 using base::SequencedWorkerPool; | 40 using base::SequencedWorkerPool; |
| 40 using base::SingleThreadTaskRunner; | 41 using base::SingleThreadTaskRunner; |
| 41 using base::Time; | 42 using base::Time; |
| 42 using base::DirectoryExists; | 43 using base::DirectoryExists; |
| 43 using file_util::CreateDirectory; | 44 using file_util::CreateDirectory; |
| 44 | 45 |
| 45 namespace { | 46 namespace { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 return true; | 190 return true; |
| 190 } | 191 } |
| 191 } | 192 } |
| 192 | 193 |
| 193 void CallCompletionCallback(const net::CompletionCallback& callback, | 194 void CallCompletionCallback(const net::CompletionCallback& callback, |
| 194 int error_code) { | 195 int error_code) { |
| 195 DCHECK(!callback.is_null()); | 196 DCHECK(!callback.is_null()); |
| 196 callback.Run(error_code); | 197 callback.Run(error_code); |
| 197 } | 198 } |
| 198 | 199 |
| 200 // See ChainOperationIntoClosure, below. | |
| 201 void ChainOperationIntoClosureImpl( | |
| 202 const base::WeakPtr<disk_cache::SimpleBackendImpl>& backend_weak_ptr, | |
| 203 const Callback<int(const net::CompletionCallback&)>& to_chain_operation, | |
| 204 const net::CompletionCallback& operation_callback, | |
| 205 const Closure& closure) { | |
| 206 if (backend_weak_ptr) { | |
| 207 const int operation_result = to_chain_operation.Run(operation_callback); | |
| 208 if (operation_result != net::ERR_IO_PENDING) | |
| 209 operation_callback.Run(operation_result); | |
| 210 } | |
| 211 if (!closure.is_null()) | |
| 212 closure.Run(); | |
| 213 } | |
| 214 | |
| 215 // Returns a Closure that will first run |to_chain_operation|, returning its | |
| 216 // result to |operation_callback|, and finally run |closure| if it is not null. | |
| 217 Closure ChainOperationIntoClosure( | |
| 218 const base::WeakPtr<disk_cache::SimpleBackendImpl>& backend_weak_ptr, | |
| 219 const Callback<int(const net::CompletionCallback&)>& to_chain_operation, | |
| 220 const net::CompletionCallback& operation_callback, | |
| 221 const Closure& closure) { | |
| 222 return base::Bind(&ChainOperationIntoClosureImpl, backend_weak_ptr, | |
| 223 to_chain_operation, operation_callback, closure); | |
| 224 } | |
| 225 | |
| 199 void RecordIndexLoad(base::TimeTicks constructed_since, int result) { | 226 void RecordIndexLoad(base::TimeTicks constructed_since, int result) { |
| 200 const base::TimeDelta creation_to_index = base::TimeTicks::Now() - | 227 const base::TimeDelta creation_to_index = base::TimeTicks::Now() - |
| 201 constructed_since; | 228 constructed_since; |
| 202 if (result == net::OK) | 229 if (result == net::OK) |
| 203 UMA_HISTOGRAM_TIMES("SimpleCache.CreationToIndex", creation_to_index); | 230 UMA_HISTOGRAM_TIMES("SimpleCache.CreationToIndex", creation_to_index); |
| 204 else | 231 else |
| 205 UMA_HISTOGRAM_TIMES("SimpleCache.CreationToIndexFail", creation_to_index); | 232 UMA_HISTOGRAM_TIMES("SimpleCache.CreationToIndexFail", creation_to_index); |
| 206 } | 233 } |
| 207 | 234 |
| 208 } // namespace | 235 } // namespace |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 } | 286 } |
| 260 | 287 |
| 261 int SimpleBackendImpl::GetMaxFileSize() const { | 288 int SimpleBackendImpl::GetMaxFileSize() const { |
| 262 return index_->max_size() / kMaxFileRatio; | 289 return index_->max_size() / kMaxFileRatio; |
| 263 } | 290 } |
| 264 | 291 |
| 265 void SimpleBackendImpl::OnDeactivated(const SimpleEntryImpl* entry) { | 292 void SimpleBackendImpl::OnDeactivated(const SimpleEntryImpl* entry) { |
| 266 active_entries_.erase(entry->entry_hash()); | 293 active_entries_.erase(entry->entry_hash()); |
| 267 } | 294 } |
| 268 | 295 |
| 296 void SimpleBackendImpl::OnDoomStart(const uint64 entry_hash) { | |
| 297 DCHECK_EQ(0u, entries_pending_doom_.count(entry_hash)); | |
| 298 entries_pending_doom_.insert( | |
| 299 base::hash_map<uint64, Closure>::value_type(entry_hash, Closure())); | |
|
Philippe
2013/08/29 10:16:16
The closure that you create here will be the last
Philippe
2013/08/29 11:46:14
Or maybe instead do an erase() on the iterator ret
| |
| 300 } | |
| 301 | |
| 302 void SimpleBackendImpl::OnDoomComplete(const uint64 entry_hash) { | |
| 303 DCHECK_EQ(1u, entries_pending_doom_.count(entry_hash)); | |
| 304 Closure to_run_closure = entries_pending_doom_.find(entry_hash)->second; | |
| 305 if (!to_run_closure.is_null()) | |
| 306 to_run_closure.Run(); | |
| 307 } | |
| 308 | |
| 269 net::CacheType SimpleBackendImpl::GetCacheType() const { | 309 net::CacheType SimpleBackendImpl::GetCacheType() const { |
| 270 return net::DISK_CACHE; | 310 return net::DISK_CACHE; |
| 271 } | 311 } |
| 272 | 312 |
| 273 int32 SimpleBackendImpl::GetEntryCount() const { | 313 int32 SimpleBackendImpl::GetEntryCount() const { |
| 274 // TODO(pasko): Use directory file count when index is not ready. | 314 // TODO(pasko): Use directory file count when index is not ready. |
| 275 return index_->GetEntryCount(); | 315 return index_->GetEntryCount(); |
| 276 } | 316 } |
| 277 | 317 |
| 278 int SimpleBackendImpl::OpenEntry(const std::string& key, | 318 int SimpleBackendImpl::OpenEntry(const std::string& key, |
| 279 Entry** entry, | 319 Entry** entry, |
| 280 const CompletionCallback& callback) { | 320 const CompletionCallback& callback) { |
| 281 scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveEntry(key); | 321 const uint64 entry_hash = simple_util::GetEntryHashKey(key); |
| 322 | |
| 323 base::hash_map<uint64, base::Closure>::iterator | |
| 324 it = entries_pending_doom_.find(entry_hash); | |
| 325 if (it != entries_pending_doom_.end()) { | |
| 326 Callback<int(const net::CompletionCallback&)> operation = | |
| 327 base::Bind(&SimpleBackendImpl::OpenEntry, | |
| 328 base::Unretained(this), key, entry); | |
| 329 it->second = ChainOperationIntoClosure(AsWeakPtr(), operation, | |
| 330 callback, it->second); | |
| 331 return net::ERR_IO_PENDING; | |
| 332 } | |
| 333 scoped_refptr<SimpleEntryImpl> | |
| 334 simple_entry = CreateOrFindActiveEntry(entry_hash, key); | |
| 282 CompletionCallback backend_callback = | 335 CompletionCallback backend_callback = |
| 283 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromKey, | 336 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromKey, |
| 284 AsWeakPtr(), | 337 AsWeakPtr(), |
| 285 key, | 338 key, |
| 286 entry, | 339 entry, |
| 287 simple_entry, | 340 simple_entry, |
| 288 callback); | 341 callback); |
| 289 return simple_entry->OpenEntry(entry, backend_callback); | 342 return simple_entry->OpenEntry(entry, backend_callback); |
| 290 } | 343 } |
| 291 | 344 |
| 292 int SimpleBackendImpl::CreateEntry(const std::string& key, | 345 int SimpleBackendImpl::CreateEntry(const std::string& key, |
| 293 Entry** entry, | 346 Entry** entry, |
| 294 const CompletionCallback& callback) { | 347 const CompletionCallback& callback) { |
| 295 DCHECK(key.size() > 0); | 348 DCHECK(key.size() > 0); |
| 296 scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveEntry(key); | 349 const uint64 entry_hash = simple_util::GetEntryHashKey(key); |
| 350 | |
| 351 base::hash_map<uint64, base::Closure>::iterator | |
| 352 it = entries_pending_doom_.find(entry_hash); | |
| 353 if (it != entries_pending_doom_.end()) { | |
| 354 Callback<int(const net::CompletionCallback&)> operation = | |
| 355 base::Bind(&SimpleBackendImpl::CreateEntry, base::Unretained(this), key, entry); | |
| 356 it->second = ChainOperationIntoClosure(AsWeakPtr(), operation, callback, it- >second); | |
| 357 return net::ERR_IO_PENDING; | |
| 358 } | |
| 359 scoped_refptr<SimpleEntryImpl> | |
| 360 simple_entry = CreateOrFindActiveEntry(entry_hash, key); | |
| 297 return simple_entry->CreateEntry(entry, callback); | 361 return simple_entry->CreateEntry(entry, callback); |
| 298 } | 362 } |
| 299 | 363 |
| 300 int SimpleBackendImpl::DoomEntry(const std::string& key, | 364 int SimpleBackendImpl::DoomEntry(const std::string& key, |
| 301 const net::CompletionCallback& callback) { | 365 const net::CompletionCallback& callback) { |
| 302 scoped_refptr<SimpleEntryImpl> simple_entry = CreateOrFindActiveEntry(key); | 366 const uint64 entry_hash = simple_util::GetEntryHashKey(key); |
| 367 | |
| 368 base::hash_map<uint64, base::Closure>::iterator | |
| 369 it = entries_pending_doom_.find(entry_hash); | |
| 370 if (it != entries_pending_doom_.end()) { | |
| 371 Callback<int(const net::CompletionCallback&)> operation = | |
| 372 base::Bind(&SimpleBackendImpl::DoomEntry, base::Unretained(this), key); | |
| 373 it->second = ChainOperationIntoClosure(AsWeakPtr(), operation, callback, it- >second); | |
| 374 return net::ERR_IO_PENDING; | |
| 375 } | |
| 376 scoped_refptr<SimpleEntryImpl> | |
| 377 simple_entry = CreateOrFindActiveEntry(entry_hash, key); | |
| 303 return simple_entry->DoomEntry(callback); | 378 return simple_entry->DoomEntry(callback); |
| 304 } | 379 } |
| 305 | 380 |
| 306 int SimpleBackendImpl::DoomAllEntries(const CompletionCallback& callback) { | 381 int SimpleBackendImpl::DoomAllEntries(const CompletionCallback& callback) { |
| 307 return DoomEntriesBetween(Time(), Time(), callback); | 382 return DoomEntriesBetween(Time(), Time(), callback); |
| 308 } | 383 } |
| 309 | 384 |
| 310 void SimpleBackendImpl::IndexReadyForDoom(Time initial_time, | 385 void SimpleBackendImpl::IndexReadyForDoom(Time initial_time, |
| 311 Time end_time, | 386 Time end_time, |
| 312 const CompletionCallback& callback, | 387 const CompletionCallback& callback, |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 // TODO(pasko): Move PreferedCacheSize() to cache_util.h. Also fix the | 489 // TODO(pasko): Move PreferedCacheSize() to cache_util.h. Also fix the |
| 415 // spelling. | 490 // spelling. |
| 416 result.max_size = disk_cache::PreferedCacheSize(available); | 491 result.max_size = disk_cache::PreferedCacheSize(available); |
| 417 } | 492 } |
| 418 DCHECK(result.max_size); | 493 DCHECK(result.max_size); |
| 419 } | 494 } |
| 420 return result; | 495 return result; |
| 421 } | 496 } |
| 422 | 497 |
| 423 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry( | 498 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry( |
| 499 const uint64 entry_hash, | |
| 424 const std::string& key) { | 500 const std::string& key) { |
| 425 const uint64 entry_hash = simple_util::GetEntryHashKey(key); | |
| 426 | 501 |
| 427 std::pair<EntryMap::iterator, bool> insert_result = | 502 std::pair<EntryMap::iterator, bool> insert_result = |
| 428 active_entries_.insert(std::make_pair(entry_hash, | 503 active_entries_.insert(std::make_pair(entry_hash, |
| 429 base::WeakPtr<SimpleEntryImpl>())); | 504 base::WeakPtr<SimpleEntryImpl>())); |
| 430 EntryMap::iterator& it = insert_result.first; | 505 EntryMap::iterator& it = insert_result.first; |
| 431 if (insert_result.second) | 506 if (insert_result.second) |
| 432 DCHECK(!it->second.get()); | 507 DCHECK(!it->second.get()); |
| 433 if (!it->second.get()) { | 508 if (!it->second.get()) { |
| 434 SimpleEntryImpl* entry = new SimpleEntryImpl( | 509 SimpleEntryImpl* entry = new SimpleEntryImpl( |
| 435 path_, entry_hash, entry_operations_mode_, this, net_log_); | 510 path_, entry_hash, entry_operations_mode_, this, net_log_); |
| 436 entry->SetKey(key); | 511 entry->SetKey(key); |
| 437 it->second = entry->AsWeakPtr(); | 512 it->second = entry->AsWeakPtr(); |
| 438 } | 513 } |
| 439 DCHECK(it->second.get()); | 514 DCHECK(it->second.get()); |
| 440 // 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 |
| 441 // currently active entry. | 516 // currently active entry. |
| 442 if (key != it->second->key()) { | 517 if (key != it->second->key()) { |
| 443 it->second->Doom(); | 518 it->second->Doom(); |
| 444 DCHECK_EQ(0U, active_entries_.count(entry_hash)); | 519 DCHECK_EQ(0U, active_entries_.count(entry_hash)); |
| 445 return CreateOrFindActiveEntry(key); | 520 return CreateOrFindActiveEntry(entry_hash, key); |
| 446 } | 521 } |
| 447 return make_scoped_refptr(it->second.get()); | 522 return make_scoped_refptr(it->second.get()); |
| 448 } | 523 } |
| 449 | 524 |
| 450 int SimpleBackendImpl::OpenEntryFromHash(uint64 hash, | 525 int SimpleBackendImpl::OpenEntryFromHash(uint64 hash, |
| 451 Entry** entry, | 526 Entry** entry, |
| 452 const CompletionCallback& callback) { | 527 const CompletionCallback& callback) { |
| 453 EntryMap::iterator has_active = active_entries_.find(hash); | 528 EntryMap::iterator has_active = active_entries_.find(hash); |
| 454 if (has_active != active_entries_.end()) | 529 if (has_active != active_entries_.end()) |
| 455 return OpenEntry(has_active->second->key(), entry, callback); | 530 return OpenEntry(has_active->second->key(), entry, callback); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 const CompletionCallback& callback, | 636 const CompletionCallback& callback, |
| 562 int error_code) { | 637 int error_code) { |
| 563 if (error_code == net::ERR_FAILED) { | 638 if (error_code == net::ERR_FAILED) { |
| 564 OpenNextEntry(iter, entry, callback); | 639 OpenNextEntry(iter, entry, callback); |
| 565 return; | 640 return; |
| 566 } | 641 } |
| 567 CallCompletionCallback(callback, error_code); | 642 CallCompletionCallback(callback, error_code); |
| 568 } | 643 } |
| 569 | 644 |
| 570 } // namespace disk_cache | 645 } // namespace disk_cache |
| OLD | NEW |