| 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 #include <functional> | 9 #include <functional> |
| 10 | 10 |
| 11 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
| 12 #include <sys/resource.h> | 12 #include <sys/resource.h> |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
| 18 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
| 19 #include "base/location.h" | 19 #include "base/location.h" |
| 20 #include "base/macros.h" |
| 20 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
| 21 #include "base/metrics/histogram_macros.h" | 22 #include "base/metrics/histogram_macros.h" |
| 22 #include "base/metrics/sparse_histogram.h" | 23 #include "base/metrics/sparse_histogram.h" |
| 23 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
| 24 #include "base/sys_info.h" | 25 #include "base/sys_info.h" |
| 25 #include "base/task_runner_util.h" | 26 #include "base/task_runner_util.h" |
| 26 #include "base/thread_task_runner_handle.h" | 27 #include "base/thread_task_runner_handle.h" |
| 27 #include "base/threading/sequenced_worker_pool.h" | 28 #include "base/threading/sequenced_worker_pool.h" |
| 28 #include "base/time/time.h" | 29 #include "base/time/time.h" |
| 29 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 : public SimpleEntryImpl::ActiveEntryProxy { | 203 : public SimpleEntryImpl::ActiveEntryProxy { |
| 203 public: | 204 public: |
| 204 ~ActiveEntryProxy() override { | 205 ~ActiveEntryProxy() override { |
| 205 if (backend_) { | 206 if (backend_) { |
| 206 DCHECK_EQ(1U, backend_->active_entries_.count(entry_hash_)); | 207 DCHECK_EQ(1U, backend_->active_entries_.count(entry_hash_)); |
| 207 backend_->active_entries_.erase(entry_hash_); | 208 backend_->active_entries_.erase(entry_hash_); |
| 208 } | 209 } |
| 209 } | 210 } |
| 210 | 211 |
| 211 static scoped_ptr<SimpleEntryImpl::ActiveEntryProxy> Create( | 212 static scoped_ptr<SimpleEntryImpl::ActiveEntryProxy> Create( |
| 212 int64 entry_hash, | 213 int64_t entry_hash, |
| 213 SimpleBackendImpl* backend) { | 214 SimpleBackendImpl* backend) { |
| 214 scoped_ptr<SimpleEntryImpl::ActiveEntryProxy> | 215 scoped_ptr<SimpleEntryImpl::ActiveEntryProxy> |
| 215 proxy(new ActiveEntryProxy(entry_hash, backend)); | 216 proxy(new ActiveEntryProxy(entry_hash, backend)); |
| 216 return proxy.Pass(); | 217 return proxy.Pass(); |
| 217 } | 218 } |
| 218 | 219 |
| 219 private: | 220 private: |
| 220 ActiveEntryProxy(uint64 entry_hash, | 221 ActiveEntryProxy(uint64_t entry_hash, SimpleBackendImpl* backend) |
| 221 SimpleBackendImpl* backend) | 222 : entry_hash_(entry_hash), backend_(backend->AsWeakPtr()) {} |
| 222 : entry_hash_(entry_hash), | |
| 223 backend_(backend->AsWeakPtr()) {} | |
| 224 | 223 |
| 225 uint64 entry_hash_; | 224 uint64_t entry_hash_; |
| 226 base::WeakPtr<SimpleBackendImpl> backend_; | 225 base::WeakPtr<SimpleBackendImpl> backend_; |
| 227 }; | 226 }; |
| 228 | 227 |
| 229 SimpleBackendImpl::SimpleBackendImpl( | 228 SimpleBackendImpl::SimpleBackendImpl( |
| 230 const FilePath& path, | 229 const FilePath& path, |
| 231 int max_bytes, | 230 int max_bytes, |
| 232 net::CacheType cache_type, | 231 net::CacheType cache_type, |
| 233 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, | 232 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, |
| 234 net::NetLog* net_log) | 233 net::NetLog* net_log) |
| 235 : path_(path), | 234 : path_(path), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 return false; | 274 return false; |
| 276 orig_max_size_ = max_bytes; | 275 orig_max_size_ = max_bytes; |
| 277 index_->SetMaxSize(max_bytes); | 276 index_->SetMaxSize(max_bytes); |
| 278 return true; | 277 return true; |
| 279 } | 278 } |
| 280 | 279 |
| 281 int SimpleBackendImpl::GetMaxFileSize() const { | 280 int SimpleBackendImpl::GetMaxFileSize() const { |
| 282 return static_cast<int>(index_->max_size() / kMaxFileRatio); | 281 return static_cast<int>(index_->max_size() / kMaxFileRatio); |
| 283 } | 282 } |
| 284 | 283 |
| 285 void SimpleBackendImpl::OnDoomStart(uint64 entry_hash) { | 284 void SimpleBackendImpl::OnDoomStart(uint64_t entry_hash) { |
| 286 DCHECK_EQ(0u, entries_pending_doom_.count(entry_hash)); | 285 DCHECK_EQ(0u, entries_pending_doom_.count(entry_hash)); |
| 287 entries_pending_doom_.insert( | 286 entries_pending_doom_.insert( |
| 288 std::make_pair(entry_hash, std::vector<Closure>())); | 287 std::make_pair(entry_hash, std::vector<Closure>())); |
| 289 } | 288 } |
| 290 | 289 |
| 291 void SimpleBackendImpl::OnDoomComplete(uint64 entry_hash) { | 290 void SimpleBackendImpl::OnDoomComplete(uint64_t entry_hash) { |
| 292 DCHECK_EQ(1u, entries_pending_doom_.count(entry_hash)); | 291 DCHECK_EQ(1u, entries_pending_doom_.count(entry_hash)); |
| 293 base::hash_map<uint64, std::vector<Closure> >::iterator it = | 292 base::hash_map<uint64_t, std::vector<Closure>>::iterator it = |
| 294 entries_pending_doom_.find(entry_hash); | 293 entries_pending_doom_.find(entry_hash); |
| 295 std::vector<Closure> to_run_closures; | 294 std::vector<Closure> to_run_closures; |
| 296 to_run_closures.swap(it->second); | 295 to_run_closures.swap(it->second); |
| 297 entries_pending_doom_.erase(it); | 296 entries_pending_doom_.erase(it); |
| 298 | 297 |
| 299 std::for_each(to_run_closures.begin(), to_run_closures.end(), | 298 std::for_each(to_run_closures.begin(), to_run_closures.end(), |
| 300 std::mem_fun_ref(&Closure::Run)); | 299 std::mem_fun_ref(&Closure::Run)); |
| 301 } | 300 } |
| 302 | 301 |
| 303 void SimpleBackendImpl::DoomEntries(std::vector<uint64>* entry_hashes, | 302 void SimpleBackendImpl::DoomEntries(std::vector<uint64_t>* entry_hashes, |
| 304 const net::CompletionCallback& callback) { | 303 const net::CompletionCallback& callback) { |
| 305 scoped_ptr<std::vector<uint64> > | 304 scoped_ptr<std::vector<uint64_t>> mass_doom_entry_hashes( |
| 306 mass_doom_entry_hashes(new std::vector<uint64>()); | 305 new std::vector<uint64_t>()); |
| 307 mass_doom_entry_hashes->swap(*entry_hashes); | 306 mass_doom_entry_hashes->swap(*entry_hashes); |
| 308 | 307 |
| 309 std::vector<uint64> to_doom_individually_hashes; | 308 std::vector<uint64_t> to_doom_individually_hashes; |
| 310 | 309 |
| 311 // For each of the entry hashes, there are two cases: | 310 // For each of the entry hashes, there are two cases: |
| 312 // 1. The entry is either open or pending doom, and so it should be doomed | 311 // 1. The entry is either open or pending doom, and so it should be doomed |
| 313 // individually to avoid flakes. | 312 // individually to avoid flakes. |
| 314 // 2. The entry is not in use at all, so we can call | 313 // 2. The entry is not in use at all, so we can call |
| 315 // SimpleSynchronousEntry::DoomEntrySet and delete the files en masse. | 314 // SimpleSynchronousEntry::DoomEntrySet and delete the files en masse. |
| 316 for (int i = mass_doom_entry_hashes->size() - 1; i >= 0; --i) { | 315 for (int i = mass_doom_entry_hashes->size() - 1; i >= 0; --i) { |
| 317 const uint64 entry_hash = (*mass_doom_entry_hashes)[i]; | 316 const uint64_t entry_hash = (*mass_doom_entry_hashes)[i]; |
| 318 DCHECK(active_entries_.count(entry_hash) == 0 || | 317 DCHECK(active_entries_.count(entry_hash) == 0 || |
| 319 entries_pending_doom_.count(entry_hash) == 0); | 318 entries_pending_doom_.count(entry_hash) == 0); |
| 320 if (!active_entries_.count(entry_hash) && | 319 if (!active_entries_.count(entry_hash) && |
| 321 !entries_pending_doom_.count(entry_hash)) { | 320 !entries_pending_doom_.count(entry_hash)) { |
| 322 continue; | 321 continue; |
| 323 } | 322 } |
| 324 | 323 |
| 325 to_doom_individually_hashes.push_back(entry_hash); | 324 to_doom_individually_hashes.push_back(entry_hash); |
| 326 | 325 |
| 327 (*mass_doom_entry_hashes)[i] = mass_doom_entry_hashes->back(); | 326 (*mass_doom_entry_hashes)[i] = mass_doom_entry_hashes->back(); |
| 328 mass_doom_entry_hashes->resize(mass_doom_entry_hashes->size() - 1); | 327 mass_doom_entry_hashes->resize(mass_doom_entry_hashes->size() - 1); |
| 329 } | 328 } |
| 330 | 329 |
| 331 net::CompletionCallback barrier_callback = | 330 net::CompletionCallback barrier_callback = |
| 332 MakeBarrierCompletionCallback(to_doom_individually_hashes.size() + 1, | 331 MakeBarrierCompletionCallback(to_doom_individually_hashes.size() + 1, |
| 333 callback); | 332 callback); |
| 334 for (std::vector<uint64>::const_iterator | 333 for (std::vector<uint64_t>::const_iterator |
| 335 it = to_doom_individually_hashes.begin(), | 334 it = to_doom_individually_hashes.begin(), |
| 336 end = to_doom_individually_hashes.end(); it != end; ++it) { | 335 end = to_doom_individually_hashes.end(); |
| 336 it != end; ++it) { |
| 337 const int doom_result = DoomEntryFromHash(*it, barrier_callback); | 337 const int doom_result = DoomEntryFromHash(*it, barrier_callback); |
| 338 DCHECK_EQ(net::ERR_IO_PENDING, doom_result); | 338 DCHECK_EQ(net::ERR_IO_PENDING, doom_result); |
| 339 index_->Remove(*it); | 339 index_->Remove(*it); |
| 340 } | 340 } |
| 341 | 341 |
| 342 for (std::vector<uint64>::const_iterator it = mass_doom_entry_hashes->begin(), | 342 for (std::vector<uint64_t>::const_iterator |
| 343 end = mass_doom_entry_hashes->end(); | 343 it = mass_doom_entry_hashes->begin(), |
| 344 end = mass_doom_entry_hashes->end(); |
| 344 it != end; ++it) { | 345 it != end; ++it) { |
| 345 index_->Remove(*it); | 346 index_->Remove(*it); |
| 346 OnDoomStart(*it); | 347 OnDoomStart(*it); |
| 347 } | 348 } |
| 348 | 349 |
| 349 // Taking this pointer here avoids undefined behaviour from calling | 350 // Taking this pointer here avoids undefined behaviour from calling |
| 350 // base::Passed before mass_doom_entry_hashes.get(). | 351 // base::Passed before mass_doom_entry_hashes.get(). |
| 351 std::vector<uint64>* mass_doom_entry_hashes_ptr = | 352 std::vector<uint64_t>* mass_doom_entry_hashes_ptr = |
| 352 mass_doom_entry_hashes.get(); | 353 mass_doom_entry_hashes.get(); |
| 353 PostTaskAndReplyWithResult(worker_pool_.get(), | 354 PostTaskAndReplyWithResult(worker_pool_.get(), |
| 354 FROM_HERE, | 355 FROM_HERE, |
| 355 base::Bind(&SimpleSynchronousEntry::DoomEntrySet, | 356 base::Bind(&SimpleSynchronousEntry::DoomEntrySet, |
| 356 mass_doom_entry_hashes_ptr, | 357 mass_doom_entry_hashes_ptr, |
| 357 path_), | 358 path_), |
| 358 base::Bind(&SimpleBackendImpl::DoomEntriesComplete, | 359 base::Bind(&SimpleBackendImpl::DoomEntriesComplete, |
| 359 AsWeakPtr(), | 360 AsWeakPtr(), |
| 360 base::Passed(&mass_doom_entry_hashes), | 361 base::Passed(&mass_doom_entry_hashes), |
| 361 barrier_callback)); | 362 barrier_callback)); |
| 362 } | 363 } |
| 363 | 364 |
| 364 net::CacheType SimpleBackendImpl::GetCacheType() const { | 365 net::CacheType SimpleBackendImpl::GetCacheType() const { |
| 365 return net::DISK_CACHE; | 366 return net::DISK_CACHE; |
| 366 } | 367 } |
| 367 | 368 |
| 368 int32 SimpleBackendImpl::GetEntryCount() const { | 369 int32_t SimpleBackendImpl::GetEntryCount() const { |
| 369 // TODO(pasko): Use directory file count when index is not ready. | 370 // TODO(pasko): Use directory file count when index is not ready. |
| 370 return index_->GetEntryCount(); | 371 return index_->GetEntryCount(); |
| 371 } | 372 } |
| 372 | 373 |
| 373 int SimpleBackendImpl::OpenEntry(const std::string& key, | 374 int SimpleBackendImpl::OpenEntry(const std::string& key, |
| 374 Entry** entry, | 375 Entry** entry, |
| 375 const CompletionCallback& callback) { | 376 const CompletionCallback& callback) { |
| 376 const uint64 entry_hash = simple_util::GetEntryHashKey(key); | 377 const uint64_t entry_hash = simple_util::GetEntryHashKey(key); |
| 377 | 378 |
| 378 // TODO(gavinp): Factor out this (not quite completely) repetitive code | 379 // TODO(gavinp): Factor out this (not quite completely) repetitive code |
| 379 // block from OpenEntry/CreateEntry/DoomEntry. | 380 // block from OpenEntry/CreateEntry/DoomEntry. |
| 380 base::hash_map<uint64, std::vector<Closure> >::iterator it = | 381 base::hash_map<uint64_t, std::vector<Closure>>::iterator it = |
| 381 entries_pending_doom_.find(entry_hash); | 382 entries_pending_doom_.find(entry_hash); |
| 382 if (it != entries_pending_doom_.end()) { | 383 if (it != entries_pending_doom_.end()) { |
| 383 Callback<int(const net::CompletionCallback&)> operation = | 384 Callback<int(const net::CompletionCallback&)> operation = |
| 384 base::Bind(&SimpleBackendImpl::OpenEntry, | 385 base::Bind(&SimpleBackendImpl::OpenEntry, |
| 385 base::Unretained(this), key, entry); | 386 base::Unretained(this), key, entry); |
| 386 it->second.push_back(base::Bind(&RunOperationAndCallback, | 387 it->second.push_back(base::Bind(&RunOperationAndCallback, |
| 387 operation, callback)); | 388 operation, callback)); |
| 388 return net::ERR_IO_PENDING; | 389 return net::ERR_IO_PENDING; |
| 389 } | 390 } |
| 390 scoped_refptr<SimpleEntryImpl> simple_entry = | 391 scoped_refptr<SimpleEntryImpl> simple_entry = |
| 391 CreateOrFindActiveEntry(entry_hash, key); | 392 CreateOrFindActiveEntry(entry_hash, key); |
| 392 CompletionCallback backend_callback = | 393 CompletionCallback backend_callback = |
| 393 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromKey, | 394 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromKey, |
| 394 AsWeakPtr(), | 395 AsWeakPtr(), |
| 395 key, | 396 key, |
| 396 entry, | 397 entry, |
| 397 simple_entry, | 398 simple_entry, |
| 398 callback); | 399 callback); |
| 399 return simple_entry->OpenEntry(entry, backend_callback); | 400 return simple_entry->OpenEntry(entry, backend_callback); |
| 400 } | 401 } |
| 401 | 402 |
| 402 int SimpleBackendImpl::CreateEntry(const std::string& key, | 403 int SimpleBackendImpl::CreateEntry(const std::string& key, |
| 403 Entry** entry, | 404 Entry** entry, |
| 404 const CompletionCallback& callback) { | 405 const CompletionCallback& callback) { |
| 405 DCHECK_LT(0u, key.size()); | 406 DCHECK_LT(0u, key.size()); |
| 406 const uint64 entry_hash = simple_util::GetEntryHashKey(key); | 407 const uint64_t entry_hash = simple_util::GetEntryHashKey(key); |
| 407 | 408 |
| 408 base::hash_map<uint64, std::vector<Closure> >::iterator it = | 409 base::hash_map<uint64_t, std::vector<Closure>>::iterator it = |
| 409 entries_pending_doom_.find(entry_hash); | 410 entries_pending_doom_.find(entry_hash); |
| 410 if (it != entries_pending_doom_.end()) { | 411 if (it != entries_pending_doom_.end()) { |
| 411 Callback<int(const net::CompletionCallback&)> operation = | 412 Callback<int(const net::CompletionCallback&)> operation = |
| 412 base::Bind(&SimpleBackendImpl::CreateEntry, | 413 base::Bind(&SimpleBackendImpl::CreateEntry, |
| 413 base::Unretained(this), key, entry); | 414 base::Unretained(this), key, entry); |
| 414 it->second.push_back(base::Bind(&RunOperationAndCallback, | 415 it->second.push_back(base::Bind(&RunOperationAndCallback, |
| 415 operation, callback)); | 416 operation, callback)); |
| 416 return net::ERR_IO_PENDING; | 417 return net::ERR_IO_PENDING; |
| 417 } | 418 } |
| 418 scoped_refptr<SimpleEntryImpl> simple_entry = | 419 scoped_refptr<SimpleEntryImpl> simple_entry = |
| 419 CreateOrFindActiveEntry(entry_hash, key); | 420 CreateOrFindActiveEntry(entry_hash, key); |
| 420 return simple_entry->CreateEntry(entry, callback); | 421 return simple_entry->CreateEntry(entry, callback); |
| 421 } | 422 } |
| 422 | 423 |
| 423 int SimpleBackendImpl::DoomEntry(const std::string& key, | 424 int SimpleBackendImpl::DoomEntry(const std::string& key, |
| 424 const net::CompletionCallback& callback) { | 425 const net::CompletionCallback& callback) { |
| 425 const uint64 entry_hash = simple_util::GetEntryHashKey(key); | 426 const uint64_t entry_hash = simple_util::GetEntryHashKey(key); |
| 426 | 427 |
| 427 base::hash_map<uint64, std::vector<Closure> >::iterator it = | 428 base::hash_map<uint64_t, std::vector<Closure>>::iterator it = |
| 428 entries_pending_doom_.find(entry_hash); | 429 entries_pending_doom_.find(entry_hash); |
| 429 if (it != entries_pending_doom_.end()) { | 430 if (it != entries_pending_doom_.end()) { |
| 430 Callback<int(const net::CompletionCallback&)> operation = | 431 Callback<int(const net::CompletionCallback&)> operation = |
| 431 base::Bind(&SimpleBackendImpl::DoomEntry, base::Unretained(this), key); | 432 base::Bind(&SimpleBackendImpl::DoomEntry, base::Unretained(this), key); |
| 432 it->second.push_back(base::Bind(&RunOperationAndCallback, | 433 it->second.push_back(base::Bind(&RunOperationAndCallback, |
| 433 operation, callback)); | 434 operation, callback)); |
| 434 return net::ERR_IO_PENDING; | 435 return net::ERR_IO_PENDING; |
| 435 } | 436 } |
| 436 scoped_refptr<SimpleEntryImpl> simple_entry = | 437 scoped_refptr<SimpleEntryImpl> simple_entry = |
| 437 CreateOrFindActiveEntry(entry_hash, key); | 438 CreateOrFindActiveEntry(entry_hash, key); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 return; | 488 return; |
| 488 } | 489 } |
| 489 if (index_initialization_error_code != net::OK) { | 490 if (index_initialization_error_code != net::OK) { |
| 490 callback.Run(index_initialization_error_code); | 491 callback.Run(index_initialization_error_code); |
| 491 return; | 492 return; |
| 492 } | 493 } |
| 493 if (!hashes_to_enumerate_) | 494 if (!hashes_to_enumerate_) |
| 494 hashes_to_enumerate_ = backend_->index()->GetAllHashes().Pass(); | 495 hashes_to_enumerate_ = backend_->index()->GetAllHashes().Pass(); |
| 495 | 496 |
| 496 while (!hashes_to_enumerate_->empty()) { | 497 while (!hashes_to_enumerate_->empty()) { |
| 497 uint64 entry_hash = hashes_to_enumerate_->back(); | 498 uint64_t entry_hash = hashes_to_enumerate_->back(); |
| 498 hashes_to_enumerate_->pop_back(); | 499 hashes_to_enumerate_->pop_back(); |
| 499 if (backend_->index()->Has(entry_hash)) { | 500 if (backend_->index()->Has(entry_hash)) { |
| 500 *next_entry = NULL; | 501 *next_entry = NULL; |
| 501 CompletionCallback continue_iteration = base::Bind( | 502 CompletionCallback continue_iteration = base::Bind( |
| 502 &SimpleIterator::CheckIterationReturnValue, | 503 &SimpleIterator::CheckIterationReturnValue, |
| 503 weak_factory_.GetWeakPtr(), | 504 weak_factory_.GetWeakPtr(), |
| 504 next_entry, | 505 next_entry, |
| 505 callback); | 506 callback); |
| 506 int error_code_open = backend_->OpenEntryFromHash(entry_hash, | 507 int error_code_open = backend_->OpenEntryFromHash(entry_hash, |
| 507 next_entry, | 508 next_entry, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 522 int error_code) { | 523 int error_code) { |
| 523 if (error_code == net::ERR_FAILED) { | 524 if (error_code == net::ERR_FAILED) { |
| 524 OpenNextEntry(entry, callback); | 525 OpenNextEntry(entry, callback); |
| 525 return; | 526 return; |
| 526 } | 527 } |
| 527 callback.Run(error_code); | 528 callback.Run(error_code); |
| 528 } | 529 } |
| 529 | 530 |
| 530 private: | 531 private: |
| 531 base::WeakPtr<SimpleBackendImpl> backend_; | 532 base::WeakPtr<SimpleBackendImpl> backend_; |
| 532 scoped_ptr<std::vector<uint64> > hashes_to_enumerate_; | 533 scoped_ptr<std::vector<uint64_t>> hashes_to_enumerate_; |
| 533 base::WeakPtrFactory<SimpleIterator> weak_factory_; | 534 base::WeakPtrFactory<SimpleIterator> weak_factory_; |
| 534 }; | 535 }; |
| 535 | 536 |
| 536 scoped_ptr<Backend::Iterator> SimpleBackendImpl::CreateIterator() { | 537 scoped_ptr<Backend::Iterator> SimpleBackendImpl::CreateIterator() { |
| 537 return scoped_ptr<Iterator>(new SimpleIterator(AsWeakPtr())); | 538 return scoped_ptr<Iterator>(new SimpleIterator(AsWeakPtr())); |
| 538 } | 539 } |
| 539 | 540 |
| 540 void SimpleBackendImpl::GetStats(base::StringPairs* stats) { | 541 void SimpleBackendImpl::GetStats(base::StringPairs* stats) { |
| 541 std::pair<std::string, std::string> item; | 542 std::pair<std::string, std::string> item; |
| 542 item.first = "Cache type"; | 543 item.first = "Cache type"; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 558 } | 559 } |
| 559 | 560 |
| 560 void SimpleBackendImpl::IndexReadyForDoom(Time initial_time, | 561 void SimpleBackendImpl::IndexReadyForDoom(Time initial_time, |
| 561 Time end_time, | 562 Time end_time, |
| 562 const CompletionCallback& callback, | 563 const CompletionCallback& callback, |
| 563 int result) { | 564 int result) { |
| 564 if (result != net::OK) { | 565 if (result != net::OK) { |
| 565 callback.Run(result); | 566 callback.Run(result); |
| 566 return; | 567 return; |
| 567 } | 568 } |
| 568 scoped_ptr<std::vector<uint64>> removed_key_hashes( | 569 scoped_ptr<std::vector<uint64_t>> removed_key_hashes( |
| 569 index_->GetEntriesBetween(initial_time, end_time).release()); | 570 index_->GetEntriesBetween(initial_time, end_time).release()); |
| 570 DoomEntries(removed_key_hashes.get(), callback); | 571 DoomEntries(removed_key_hashes.get(), callback); |
| 571 } | 572 } |
| 572 | 573 |
| 573 void SimpleBackendImpl::IndexReadyForSizeCalculation( | 574 void SimpleBackendImpl::IndexReadyForSizeCalculation( |
| 574 const CompletionCallback& callback, | 575 const CompletionCallback& callback, |
| 575 int result) { | 576 int result) { |
| 576 if (result == net::OK) | 577 if (result == net::OK) |
| 577 result = static_cast<int>(index_->GetCacheSize()); | 578 result = static_cast<int>(index_->GetCacheSize()); |
| 578 callback.Run(result); | 579 callback.Run(result); |
| 579 } | 580 } |
| 580 | 581 |
| 581 SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk( | 582 SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk( |
| 582 const base::FilePath& path, | 583 const base::FilePath& path, |
| 583 uint64 suggested_max_size) { | 584 uint64_t suggested_max_size) { |
| 584 DiskStatResult result; | 585 DiskStatResult result; |
| 585 result.max_size = suggested_max_size; | 586 result.max_size = suggested_max_size; |
| 586 result.net_error = net::OK; | 587 result.net_error = net::OK; |
| 587 if (!FileStructureConsistent(path)) { | 588 if (!FileStructureConsistent(path)) { |
| 588 LOG(ERROR) << "Simple Cache Backend: wrong file structure on disk: " | 589 LOG(ERROR) << "Simple Cache Backend: wrong file structure on disk: " |
| 589 << path.LossyDisplayName(); | 590 << path.LossyDisplayName(); |
| 590 result.net_error = net::ERR_FAILED; | 591 result.net_error = net::ERR_FAILED; |
| 591 } else { | 592 } else { |
| 592 bool mtime_result = | 593 bool mtime_result = |
| 593 disk_cache::simple_util::GetMTime(path, &result.cache_dir_mtime); | 594 disk_cache::simple_util::GetMTime(path, &result.cache_dir_mtime); |
| 594 DCHECK(mtime_result); | 595 DCHECK(mtime_result); |
| 595 if (!result.max_size) { | 596 if (!result.max_size) { |
| 596 int64 available = base::SysInfo::AmountOfFreeDiskSpace(path); | 597 int64_t available = base::SysInfo::AmountOfFreeDiskSpace(path); |
| 597 result.max_size = disk_cache::PreferredCacheSize(available); | 598 result.max_size = disk_cache::PreferredCacheSize(available); |
| 598 } | 599 } |
| 599 DCHECK(result.max_size); | 600 DCHECK(result.max_size); |
| 600 } | 601 } |
| 601 return result; | 602 return result; |
| 602 } | 603 } |
| 603 | 604 |
| 604 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry( | 605 scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry( |
| 605 const uint64 entry_hash, | 606 const uint64_t entry_hash, |
| 606 const std::string& key) { | 607 const std::string& key) { |
| 607 DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key)); | 608 DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key)); |
| 608 std::pair<EntryMap::iterator, bool> insert_result = | 609 std::pair<EntryMap::iterator, bool> insert_result = |
| 609 active_entries_.insert(EntryMap::value_type(entry_hash, NULL)); | 610 active_entries_.insert(EntryMap::value_type(entry_hash, NULL)); |
| 610 EntryMap::iterator& it = insert_result.first; | 611 EntryMap::iterator& it = insert_result.first; |
| 611 const bool did_insert = insert_result.second; | 612 const bool did_insert = insert_result.second; |
| 612 if (did_insert) { | 613 if (did_insert) { |
| 613 SimpleEntryImpl* entry = it->second = | 614 SimpleEntryImpl* entry = it->second = |
| 614 new SimpleEntryImpl(cache_type_, path_, entry_hash, | 615 new SimpleEntryImpl(cache_type_, path_, entry_hash, |
| 615 entry_operations_mode_,this, net_log_); | 616 entry_operations_mode_,this, net_log_); |
| 616 entry->SetKey(key); | 617 entry->SetKey(key); |
| 617 entry->SetActiveEntryProxy(ActiveEntryProxy::Create(entry_hash, this)); | 618 entry->SetActiveEntryProxy(ActiveEntryProxy::Create(entry_hash, this)); |
| 618 } | 619 } |
| 619 DCHECK(it->second); | 620 DCHECK(it->second); |
| 620 // It's possible, but unlikely, that we have an entry hash collision with a | 621 // It's possible, but unlikely, that we have an entry hash collision with a |
| 621 // currently active entry. | 622 // currently active entry. |
| 622 if (key != it->second->key()) { | 623 if (key != it->second->key()) { |
| 623 it->second->Doom(); | 624 it->second->Doom(); |
| 624 DCHECK_EQ(0U, active_entries_.count(entry_hash)); | 625 DCHECK_EQ(0U, active_entries_.count(entry_hash)); |
| 625 return CreateOrFindActiveEntry(entry_hash, key); | 626 return CreateOrFindActiveEntry(entry_hash, key); |
| 626 } | 627 } |
| 627 return make_scoped_refptr(it->second); | 628 return make_scoped_refptr(it->second); |
| 628 } | 629 } |
| 629 | 630 |
| 630 int SimpleBackendImpl::OpenEntryFromHash(uint64 entry_hash, | 631 int SimpleBackendImpl::OpenEntryFromHash(uint64_t entry_hash, |
| 631 Entry** entry, | 632 Entry** entry, |
| 632 const CompletionCallback& callback) { | 633 const CompletionCallback& callback) { |
| 633 base::hash_map<uint64, std::vector<Closure> >::iterator it = | 634 base::hash_map<uint64_t, std::vector<Closure>>::iterator it = |
| 634 entries_pending_doom_.find(entry_hash); | 635 entries_pending_doom_.find(entry_hash); |
| 635 if (it != entries_pending_doom_.end()) { | 636 if (it != entries_pending_doom_.end()) { |
| 636 Callback<int(const net::CompletionCallback&)> operation = | 637 Callback<int(const net::CompletionCallback&)> operation = |
| 637 base::Bind(&SimpleBackendImpl::OpenEntryFromHash, | 638 base::Bind(&SimpleBackendImpl::OpenEntryFromHash, |
| 638 base::Unretained(this), entry_hash, entry); | 639 base::Unretained(this), entry_hash, entry); |
| 639 it->second.push_back(base::Bind(&RunOperationAndCallback, | 640 it->second.push_back(base::Bind(&RunOperationAndCallback, |
| 640 operation, callback)); | 641 operation, callback)); |
| 641 return net::ERR_IO_PENDING; | 642 return net::ERR_IO_PENDING; |
| 642 } | 643 } |
| 643 | 644 |
| 644 EntryMap::iterator has_active = active_entries_.find(entry_hash); | 645 EntryMap::iterator has_active = active_entries_.find(entry_hash); |
| 645 if (has_active != active_entries_.end()) { | 646 if (has_active != active_entries_.end()) { |
| 646 return OpenEntry(has_active->second->key(), entry, callback); | 647 return OpenEntry(has_active->second->key(), entry, callback); |
| 647 } | 648 } |
| 648 | 649 |
| 649 scoped_refptr<SimpleEntryImpl> simple_entry = new SimpleEntryImpl( | 650 scoped_refptr<SimpleEntryImpl> simple_entry = new SimpleEntryImpl( |
| 650 cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_); | 651 cache_type_, path_, entry_hash, entry_operations_mode_, this, net_log_); |
| 651 CompletionCallback backend_callback = | 652 CompletionCallback backend_callback = |
| 652 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromHash, | 653 base::Bind(&SimpleBackendImpl::OnEntryOpenedFromHash, |
| 653 AsWeakPtr(), entry_hash, entry, simple_entry, callback); | 654 AsWeakPtr(), entry_hash, entry, simple_entry, callback); |
| 654 return simple_entry->OpenEntry(entry, backend_callback); | 655 return simple_entry->OpenEntry(entry, backend_callback); |
| 655 } | 656 } |
| 656 | 657 |
| 657 int SimpleBackendImpl::DoomEntryFromHash(uint64 entry_hash, | 658 int SimpleBackendImpl::DoomEntryFromHash(uint64_t entry_hash, |
| 658 const CompletionCallback& callback) { | 659 const CompletionCallback& callback) { |
| 659 Entry** entry = new Entry*(); | 660 Entry** entry = new Entry*(); |
| 660 scoped_ptr<Entry*> scoped_entry(entry); | 661 scoped_ptr<Entry*> scoped_entry(entry); |
| 661 | 662 |
| 662 base::hash_map<uint64, std::vector<Closure> >::iterator pending_it = | 663 base::hash_map<uint64_t, std::vector<Closure>>::iterator pending_it = |
| 663 entries_pending_doom_.find(entry_hash); | 664 entries_pending_doom_.find(entry_hash); |
| 664 if (pending_it != entries_pending_doom_.end()) { | 665 if (pending_it != entries_pending_doom_.end()) { |
| 665 Callback<int(const net::CompletionCallback&)> operation = | 666 Callback<int(const net::CompletionCallback&)> operation = |
| 666 base::Bind(&SimpleBackendImpl::DoomEntryFromHash, | 667 base::Bind(&SimpleBackendImpl::DoomEntryFromHash, |
| 667 base::Unretained(this), entry_hash); | 668 base::Unretained(this), entry_hash); |
| 668 pending_it->second.push_back(base::Bind(&RunOperationAndCallback, | 669 pending_it->second.push_back(base::Bind(&RunOperationAndCallback, |
| 669 operation, callback)); | 670 operation, callback)); |
| 670 return net::ERR_IO_PENDING; | 671 return net::ERR_IO_PENDING; |
| 671 } | 672 } |
| 672 | 673 |
| 673 EntryMap::iterator active_it = active_entries_.find(entry_hash); | 674 EntryMap::iterator active_it = active_entries_.find(entry_hash); |
| 674 if (active_it != active_entries_.end()) | 675 if (active_it != active_entries_.end()) |
| 675 return active_it->second->DoomEntry(callback); | 676 return active_it->second->DoomEntry(callback); |
| 676 | 677 |
| 677 // There's no pending dooms, nor any open entry. We can make a trivial | 678 // There's no pending dooms, nor any open entry. We can make a trivial |
| 678 // call to DoomEntries() to delete this entry. | 679 // call to DoomEntries() to delete this entry. |
| 679 std::vector<uint64> entry_hash_vector; | 680 std::vector<uint64_t> entry_hash_vector; |
| 680 entry_hash_vector.push_back(entry_hash); | 681 entry_hash_vector.push_back(entry_hash); |
| 681 DoomEntries(&entry_hash_vector, callback); | 682 DoomEntries(&entry_hash_vector, callback); |
| 682 return net::ERR_IO_PENDING; | 683 return net::ERR_IO_PENDING; |
| 683 } | 684 } |
| 684 | 685 |
| 685 void SimpleBackendImpl::OnEntryOpenedFromHash( | 686 void SimpleBackendImpl::OnEntryOpenedFromHash( |
| 686 uint64 hash, | 687 uint64_t hash, |
| 687 Entry** entry, | 688 Entry** entry, |
| 688 const scoped_refptr<SimpleEntryImpl>& simple_entry, | 689 const scoped_refptr<SimpleEntryImpl>& simple_entry, |
| 689 const CompletionCallback& callback, | 690 const CompletionCallback& callback, |
| 690 int error_code) { | 691 int error_code) { |
| 691 if (error_code != net::OK) { | 692 if (error_code != net::OK) { |
| 692 callback.Run(error_code); | 693 callback.Run(error_code); |
| 693 return; | 694 return; |
| 694 } | 695 } |
| 695 DCHECK(*entry); | 696 DCHECK(*entry); |
| 696 std::pair<EntryMap::iterator, bool> insert_result = | 697 std::pair<EntryMap::iterator, bool> insert_result = |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 final_code = net::ERR_FAILED; | 730 final_code = net::ERR_FAILED; |
| 730 } else { | 731 } else { |
| 731 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); | 732 DCHECK_EQ(simple_entry->entry_hash(), simple_util::GetEntryHashKey(key)); |
| 732 } | 733 } |
| 733 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); | 734 SIMPLE_CACHE_UMA(BOOLEAN, "KeyMatchedOnOpen", cache_type_, key_matches); |
| 734 } | 735 } |
| 735 callback.Run(final_code); | 736 callback.Run(final_code); |
| 736 } | 737 } |
| 737 | 738 |
| 738 void SimpleBackendImpl::DoomEntriesComplete( | 739 void SimpleBackendImpl::DoomEntriesComplete( |
| 739 scoped_ptr<std::vector<uint64> > entry_hashes, | 740 scoped_ptr<std::vector<uint64_t>> entry_hashes, |
| 740 const net::CompletionCallback& callback, | 741 const net::CompletionCallback& callback, |
| 741 int result) { | 742 int result) { |
| 742 for (const uint64& entry_hash : *entry_hashes) | 743 for (const uint64_t& entry_hash : *entry_hashes) |
| 743 OnDoomComplete(entry_hash); | 744 OnDoomComplete(entry_hash); |
| 744 callback.Run(result); | 745 callback.Run(result); |
| 745 } | 746 } |
| 746 | 747 |
| 747 // static | 748 // static |
| 748 void SimpleBackendImpl::FlushWorkerPoolForTesting() { | 749 void SimpleBackendImpl::FlushWorkerPoolForTesting() { |
| 749 // We only need to do this if we there is an active task runner. | 750 // We only need to do this if we there is an active task runner. |
| 750 if (base::ThreadTaskRunnerHandle::IsSet()) | 751 if (base::ThreadTaskRunnerHandle::IsSet()) |
| 751 g_sequenced_worker_pool.Get().FlushForTesting(); | 752 g_sequenced_worker_pool.Get().FlushForTesting(); |
| 752 } | 753 } |
| 753 | 754 |
| 754 } // namespace disk_cache | 755 } // namespace disk_cache |
| OLD | NEW |