| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/service_worker/service_worker_cache_storage.h" | 5 #include "content/browser/service_worker/service_worker_cache_storage.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/memory_mapped_file.h" | 11 #include "base/files/memory_mapped_file.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/sha1.h" | 13 #include "base/sha1.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "content/browser/service_worker/service_worker_cache.h" | 17 #include "content/browser/service_worker/service_worker_cache.h" |
| 18 #include "content/browser/service_worker/service_worker_cache.pb.h" | 18 #include "content/browser/service_worker/service_worker_cache.pb.h" |
| 19 #include "content/browser/service_worker/service_worker_cache_scheduler.h" |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 20 #include "net/base/directory_lister.h" | 21 #include "net/base/directory_lister.h" |
| 21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 22 #include "storage/browser/blob/blob_storage_context.h" | 23 #include "storage/browser/blob/blob_storage_context.h" |
| 23 #include "storage/browser/quota/quota_manager_proxy.h" | 24 #include "storage/browser/quota/quota_manager_proxy.h" |
| 24 | 25 |
| 25 namespace content { | 26 namespace content { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 | 371 |
| 371 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( | 372 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( |
| 372 const base::FilePath& path, | 373 const base::FilePath& path, |
| 373 bool memory_only, | 374 bool memory_only, |
| 374 base::SequencedTaskRunner* cache_task_runner, | 375 base::SequencedTaskRunner* cache_task_runner, |
| 375 net::URLRequestContext* request_context, | 376 net::URLRequestContext* request_context, |
| 376 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 377 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 377 base::WeakPtr<storage::BlobStorageContext> blob_context, | 378 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 378 const GURL& origin) | 379 const GURL& origin) |
| 379 : initialized_(false), | 380 : initialized_(false), |
| 381 initializing_(false), |
| 382 scheduler_(new ServiceWorkerCacheScheduler()), |
| 380 origin_path_(path), | 383 origin_path_(path), |
| 381 cache_task_runner_(cache_task_runner), | 384 cache_task_runner_(cache_task_runner), |
| 382 memory_only_(memory_only), | 385 memory_only_(memory_only), |
| 383 weak_factory_(this) { | 386 weak_factory_(this) { |
| 384 if (memory_only) | 387 if (memory_only) |
| 385 cache_loader_.reset(new MemoryLoader(cache_task_runner_.get(), | 388 cache_loader_.reset(new MemoryLoader(cache_task_runner_.get(), |
| 386 request_context, | 389 request_context, |
| 387 quota_manager_proxy, | 390 quota_manager_proxy, |
| 388 blob_context, | 391 blob_context, |
| 389 origin)); | 392 origin)); |
| 390 else | 393 else |
| 391 cache_loader_.reset(new SimpleCacheLoader(origin_path_, | 394 cache_loader_.reset(new SimpleCacheLoader(origin_path_, |
| 392 cache_task_runner_.get(), | 395 cache_task_runner_.get(), |
| 393 request_context, | 396 request_context, |
| 394 quota_manager_proxy, | 397 quota_manager_proxy, |
| 395 blob_context, | 398 blob_context, |
| 396 origin)); | 399 origin)); |
| 397 } | 400 } |
| 398 | 401 |
| 399 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { | 402 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { |
| 400 } | 403 } |
| 401 | 404 |
| 402 void ServiceWorkerCacheStorage::OpenCache( | 405 void ServiceWorkerCacheStorage::OpenCache( |
| 403 const std::string& cache_name, | 406 const std::string& cache_name, |
| 404 const CacheAndErrorCallback& callback) { | 407 const CacheAndErrorCallback& callback) { |
| 405 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 408 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 406 | 409 |
| 407 if (!initialized_) { | 410 if (!initialized_) |
| 408 LazyInit(base::Bind(&ServiceWorkerCacheStorage::OpenCache, | 411 LazyInit(); |
| 409 weak_factory_.GetWeakPtr(), | |
| 410 cache_name, | |
| 411 callback)); | |
| 412 return; | |
| 413 } | |
| 414 | 412 |
| 415 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); | 413 CacheAndErrorCallback pending_callback = |
| 416 if (cache.get()) { | 414 base::Bind(&ServiceWorkerCacheStorage::PendingCacheAndErrorCallback, |
| 417 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); | 415 weak_factory_.GetWeakPtr(), callback); |
| 418 return; | 416 scheduler_->ScheduleOperation( |
| 419 } | 417 base::Bind(&ServiceWorkerCacheStorage::OpenCacheImpl, |
| 420 | 418 weak_factory_.GetWeakPtr(), cache_name, pending_callback)); |
| 421 cache_loader_->CreateCache( | |
| 422 cache_name, | |
| 423 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidCreateCache, | |
| 424 weak_factory_.GetWeakPtr(), | |
| 425 cache_name, | |
| 426 callback)); | |
| 427 } | 419 } |
| 428 | 420 |
| 429 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, | 421 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, |
| 430 const BoolAndErrorCallback& callback) { | 422 const BoolAndErrorCallback& callback) { |
| 431 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 423 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 432 | 424 |
| 433 if (!initialized_) { | 425 if (!initialized_) |
| 434 LazyInit(base::Bind(&ServiceWorkerCacheStorage::HasCache, | 426 LazyInit(); |
| 435 weak_factory_.GetWeakPtr(), | |
| 436 cache_name, | |
| 437 callback)); | |
| 438 return; | |
| 439 } | |
| 440 | 427 |
| 441 bool has_cache = cache_map_.find(cache_name) != cache_map_.end(); | 428 BoolAndErrorCallback pending_callback = |
| 442 | 429 base::Bind(&ServiceWorkerCacheStorage::PendingBoolAndErrorCallback, |
| 443 callback.Run(has_cache, CACHE_STORAGE_ERROR_NO_ERROR); | 430 weak_factory_.GetWeakPtr(), callback); |
| 431 scheduler_->ScheduleOperation( |
| 432 base::Bind(&ServiceWorkerCacheStorage::HasCacheImpl, |
| 433 weak_factory_.GetWeakPtr(), cache_name, pending_callback)); |
| 444 } | 434 } |
| 445 | 435 |
| 446 void ServiceWorkerCacheStorage::DeleteCache( | 436 void ServiceWorkerCacheStorage::DeleteCache( |
| 447 const std::string& cache_name, | 437 const std::string& cache_name, |
| 448 const BoolAndErrorCallback& callback) { | 438 const BoolAndErrorCallback& callback) { |
| 449 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 439 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 450 | 440 |
| 451 if (!initialized_) { | 441 if (!initialized_) |
| 452 LazyInit(base::Bind(&ServiceWorkerCacheStorage::DeleteCache, | 442 LazyInit(); |
| 453 weak_factory_.GetWeakPtr(), | |
| 454 cache_name, | |
| 455 callback)); | |
| 456 return; | |
| 457 } | |
| 458 | 443 |
| 459 CacheMap::iterator it = cache_map_.find(cache_name); | 444 BoolAndErrorCallback pending_callback = |
| 460 if (it == cache_map_.end()) { | 445 base::Bind(&ServiceWorkerCacheStorage::PendingBoolAndErrorCallback, |
| 461 callback.Run(false, CACHE_STORAGE_ERROR_NOT_FOUND); | 446 weak_factory_.GetWeakPtr(), callback); |
| 462 return; | 447 scheduler_->ScheduleOperation( |
| 463 } | 448 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheImpl, |
| 464 | 449 weak_factory_.GetWeakPtr(), cache_name, pending_callback)); |
| 465 base::WeakPtr<ServiceWorkerCache> cache = it->second; | |
| 466 cache_map_.erase(it); | |
| 467 | |
| 468 // Delete the name from ordered_cache_names_. | |
| 469 StringVector::iterator iter = std::find( | |
| 470 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name); | |
| 471 DCHECK(iter != ordered_cache_names_.end()); | |
| 472 ordered_cache_names_.erase(iter); | |
| 473 | |
| 474 base::Closure closure = | |
| 475 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidClose, | |
| 476 weak_factory_.GetWeakPtr(), cache_name, callback, | |
| 477 ordered_cache_names_, make_scoped_refptr(cache.get())); | |
| 478 | |
| 479 if (cache) { | |
| 480 cache->Close(closure); | |
| 481 return; | |
| 482 } | |
| 483 | |
| 484 closure.Run(); | |
| 485 } | 450 } |
| 486 | 451 |
| 487 void ServiceWorkerCacheStorage::EnumerateCaches( | 452 void ServiceWorkerCacheStorage::EnumerateCaches( |
| 488 const StringsAndErrorCallback& callback) { | 453 const StringsAndErrorCallback& callback) { |
| 489 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 454 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 490 | 455 |
| 491 if (!initialized_) { | 456 if (!initialized_) |
| 492 LazyInit(base::Bind(&ServiceWorkerCacheStorage::EnumerateCaches, | 457 LazyInit(); |
| 493 weak_factory_.GetWeakPtr(), | |
| 494 callback)); | |
| 495 return; | |
| 496 } | |
| 497 | 458 |
| 498 callback.Run(ordered_cache_names_, CACHE_STORAGE_ERROR_NO_ERROR); | 459 StringsAndErrorCallback pending_callback = |
| 460 base::Bind(&ServiceWorkerCacheStorage::PendingStringsAndErrorCallback, |
| 461 weak_factory_.GetWeakPtr(), callback); |
| 462 scheduler_->ScheduleOperation( |
| 463 base::Bind(&ServiceWorkerCacheStorage::EnumerateCachesImpl, |
| 464 weak_factory_.GetWeakPtr(), pending_callback)); |
| 499 } | 465 } |
| 500 | 466 |
| 501 void ServiceWorkerCacheStorage::MatchCache( | 467 void ServiceWorkerCacheStorage::MatchCache( |
| 502 const std::string& cache_name, | 468 const std::string& cache_name, |
| 503 scoped_ptr<ServiceWorkerFetchRequest> request, | 469 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 504 const ServiceWorkerCache::ResponseCallback& callback) { | 470 const ServiceWorkerCache::ResponseCallback& callback) { |
| 505 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 471 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 506 | 472 |
| 507 if (!initialized_) { | 473 if (!initialized_) |
| 508 LazyInit(base::Bind(&ServiceWorkerCacheStorage::MatchCache, | 474 LazyInit(); |
| 509 weak_factory_.GetWeakPtr(), cache_name, | |
| 510 base::Passed(request.Pass()), callback)); | |
| 511 return; | |
| 512 } | |
| 513 | 475 |
| 514 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); | 476 ServiceWorkerCache::ResponseCallback pending_callback = |
| 515 | 477 base::Bind(&ServiceWorkerCacheStorage::PendingResponseCallback, |
| 516 if (!cache.get()) { | 478 weak_factory_.GetWeakPtr(), callback); |
| 517 callback.Run(ServiceWorkerCache::ErrorTypeNotFound, | 479 scheduler_->ScheduleOperation(base::Bind( |
| 518 scoped_ptr<ServiceWorkerResponse>(), | 480 &ServiceWorkerCacheStorage::MatchCacheImpl, weak_factory_.GetWeakPtr(), |
| 519 scoped_ptr<storage::BlobDataHandle>()); | 481 cache_name, base::Passed(request.Pass()), pending_callback)); |
| 520 return; | |
| 521 } | |
| 522 | |
| 523 // Pass the cache along to the callback to keep the cache open until match is | |
| 524 // done. | |
| 525 cache->Match(request.Pass(), | |
| 526 base::Bind(&ServiceWorkerCacheStorage::MatchCacheDidMatch, | |
| 527 weak_factory_.GetWeakPtr(), cache, callback)); | |
| 528 } | 482 } |
| 529 | 483 |
| 530 void ServiceWorkerCacheStorage::MatchAllCaches( | 484 void ServiceWorkerCacheStorage::MatchAllCaches( |
| 531 scoped_ptr<ServiceWorkerFetchRequest> request, | 485 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 532 const ServiceWorkerCache::ResponseCallback& callback) { | 486 const ServiceWorkerCache::ResponseCallback& callback) { |
| 533 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 487 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 534 | 488 |
| 535 if (!initialized_) { | 489 if (!initialized_) |
| 536 LazyInit(base::Bind(&ServiceWorkerCacheStorage::MatchAllCaches, | 490 LazyInit(); |
| 537 weak_factory_.GetWeakPtr(), | |
| 538 base::Passed(request.Pass()), callback)); | |
| 539 return; | |
| 540 } | |
| 541 | 491 |
| 542 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback_copy( | 492 ServiceWorkerCache::ResponseCallback pending_callback = |
| 543 new ServiceWorkerCache::ResponseCallback(callback)); | 493 base::Bind(&ServiceWorkerCacheStorage::PendingResponseCallback, |
| 544 | 494 weak_factory_.GetWeakPtr(), callback); |
| 545 ServiceWorkerCache::ResponseCallback* callback_ptr = callback_copy.get(); | 495 scheduler_->ScheduleOperation( |
| 546 base::Closure barrier_closure = base::BarrierClosure( | 496 base::Bind(&ServiceWorkerCacheStorage::MatchAllCachesImpl, |
| 547 ordered_cache_names_.size(), | 497 weak_factory_.GetWeakPtr(), base::Passed(request.Pass()), |
| 548 base::Bind(&ServiceWorkerCacheStorage::MatchAllCachesDidMatchAll, | 498 pending_callback)); |
| 549 weak_factory_.GetWeakPtr(), | |
| 550 base::Passed(callback_copy.Pass()))); | |
| 551 | |
| 552 for (const std::string& cache_name : ordered_cache_names_) { | |
| 553 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); | |
| 554 DCHECK(cache.get()); | |
| 555 | |
| 556 cache->Match(make_scoped_ptr(new ServiceWorkerFetchRequest(*request)), | |
| 557 base::Bind(&ServiceWorkerCacheStorage::MatchAllCachesDidMatch, | |
| 558 weak_factory_.GetWeakPtr(), cache, barrier_closure, | |
| 559 callback_ptr)); | |
| 560 } | |
| 561 } | 499 } |
| 562 | 500 |
| 563 void ServiceWorkerCacheStorage::CloseAllCaches(const base::Closure& callback) { | 501 void ServiceWorkerCacheStorage::CloseAllCaches(const base::Closure& callback) { |
| 564 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 502 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 565 | 503 |
| 566 if (!initialized_) { | 504 if (!initialized_) { |
| 567 callback.Run(); | 505 callback.Run(); |
| 568 return; | 506 return; |
| 569 } | 507 } |
| 570 | 508 |
| 571 int live_cache_count = 0; | 509 base::Closure pending_callback = |
| 572 for (const auto& key_value : cache_map_) { | 510 base::Bind(&ServiceWorkerCacheStorage::PendingClosure, |
| 573 if (key_value.second) | 511 weak_factory_.GetWeakPtr(), callback); |
| 574 live_cache_count += 1; | 512 scheduler_->ScheduleOperation( |
| 575 } | 513 base::Bind(&ServiceWorkerCacheStorage::CloseAllCachesImpl, |
| 576 | 514 weak_factory_.GetWeakPtr(), pending_callback)); |
| 577 if (live_cache_count == 0) { | |
| 578 callback.Run(); | |
| 579 return; | |
| 580 } | |
| 581 | |
| 582 // The closure might modify this object so delay calling it until after | |
| 583 // iterating through cache_map_ by adding one to the barrier. | |
| 584 base::Closure barrier_closure = | |
| 585 base::BarrierClosure(live_cache_count + 1, base::Bind(callback)); | |
| 586 | |
| 587 for (auto& key_value : cache_map_) { | |
| 588 if (key_value.second) { | |
| 589 key_value.second->Close(base::Bind( | |
| 590 CloseAllCachesDidCloseCache, | |
| 591 make_scoped_refptr(key_value.second.get()), barrier_closure)); | |
| 592 } | |
| 593 } | |
| 594 | |
| 595 barrier_closure.Run(); | |
| 596 } | 515 } |
| 597 | 516 |
| 598 int64 ServiceWorkerCacheStorage::MemoryBackedSize() const { | 517 int64 ServiceWorkerCacheStorage::MemoryBackedSize() const { |
| 599 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 518 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 600 | 519 |
| 601 if (!initialized_ || !memory_only_) | 520 if (!initialized_ || !memory_only_) |
| 602 return 0; | 521 return 0; |
| 603 | 522 |
| 604 int64 sum = 0; | 523 int64 sum = 0; |
| 605 for (auto& key_value : cache_map_) { | 524 for (auto& key_value : cache_map_) { |
| 606 if (key_value.second) | 525 if (key_value.second) |
| 607 sum += key_value.second->MemoryBackedSize(); | 526 sum += key_value.second->MemoryBackedSize(); |
| 608 } | 527 } |
| 609 return sum; | 528 return sum; |
| 610 } | 529 } |
| 611 | 530 |
| 531 void ServiceWorkerCacheStorage::StartAsyncOperationForTesting() { |
| 532 scheduler_->ScheduleOperation(base::Bind(&base::DoNothing)); |
| 533 } |
| 534 |
| 535 void ServiceWorkerCacheStorage::CompleteAsyncOperationForTesting() { |
| 536 scheduler_->CompleteOperationAndRunNext(); |
| 537 } |
| 538 |
| 612 // Init is run lazily so that it is called on the proper MessageLoop. | 539 // Init is run lazily so that it is called on the proper MessageLoop. |
| 613 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { | 540 void ServiceWorkerCacheStorage::LazyInit() { |
| 614 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 541 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 615 DCHECK(!initialized_); | 542 DCHECK(!initialized_); |
| 616 | 543 |
| 617 init_callbacks_.push_back(callback); | 544 if (initializing_) |
| 545 return; |
| 618 | 546 |
| 619 // If this isn't the first call to LazyInit then return as the initialization | 547 DCHECK(scheduler_->Empty()); |
| 620 // has already started. | 548 |
| 621 if (init_callbacks_.size() > 1u) | 549 initializing_ = true; |
| 622 return; | 550 scheduler_->ScheduleOperation(base::Bind( |
| 551 &ServiceWorkerCacheStorage::LazyInitImpl, weak_factory_.GetWeakPtr())); |
| 552 } |
| 553 |
| 554 void ServiceWorkerCacheStorage::LazyInitImpl() { |
| 555 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 556 DCHECK(!initialized_); |
| 557 DCHECK(initializing_); |
| 623 | 558 |
| 624 // 1. Get the list of cache names (async call) | 559 // 1. Get the list of cache names (async call) |
| 625 // 2. For each cache name, load the cache (async call) | 560 // 2. For each cache name, load the cache (async call) |
| 626 // 3. Once each load is complete, update the map variables. | 561 // 3. Once each load is complete, update the map variables. |
| 627 // 4. Call the list of waiting callbacks. | 562 // 4. Call the list of waiting callbacks. |
| 628 | 563 |
| 629 scoped_ptr<std::vector<std::string> > indexed_cache_names( | 564 scoped_ptr<std::vector<std::string> > indexed_cache_names( |
| 630 new std::vector<std::string>()); | 565 new std::vector<std::string>()); |
| 631 | 566 |
| 632 cache_loader_->LoadIndex( | 567 cache_loader_->LoadIndex( |
| 633 indexed_cache_names.Pass(), | 568 indexed_cache_names.Pass(), |
| 634 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex, | 569 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex, |
| 635 weak_factory_.GetWeakPtr(), | 570 weak_factory_.GetWeakPtr())); |
| 636 callback)); | |
| 637 } | 571 } |
| 638 | 572 |
| 639 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( | 573 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( |
| 640 const base::Closure& callback, | |
| 641 scoped_ptr<std::vector<std::string> > indexed_cache_names) { | 574 scoped_ptr<std::vector<std::string> > indexed_cache_names) { |
| 642 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 575 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 643 | 576 |
| 644 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) { | 577 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) { |
| 645 cache_map_.insert(std::make_pair(indexed_cache_names->at(i), | 578 cache_map_.insert(std::make_pair(indexed_cache_names->at(i), |
| 646 base::WeakPtr<ServiceWorkerCache>())); | 579 base::WeakPtr<ServiceWorkerCache>())); |
| 647 ordered_cache_names_.push_back(indexed_cache_names->at(i)); | 580 ordered_cache_names_.push_back(indexed_cache_names->at(i)); |
| 648 } | 581 } |
| 649 | 582 |
| 583 initializing_ = false; |
| 650 initialized_ = true; | 584 initialized_ = true; |
| 651 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); | 585 |
| 652 it != init_callbacks_.end(); | 586 scheduler_->CompleteOperationAndRunNext(); |
| 653 ++it) { | 587 } |
| 654 it->Run(); | 588 |
| 589 void ServiceWorkerCacheStorage::OpenCacheImpl( |
| 590 const std::string& cache_name, |
| 591 const CacheAndErrorCallback& callback) { |
| 592 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); |
| 593 if (cache.get()) { |
| 594 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); |
| 595 return; |
| 655 } | 596 } |
| 656 init_callbacks_.clear(); | 597 |
| 598 cache_loader_->CreateCache( |
| 599 cache_name, |
| 600 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidCreateCache, |
| 601 weak_factory_.GetWeakPtr(), cache_name, callback)); |
| 657 } | 602 } |
| 658 | 603 |
| 659 void ServiceWorkerCacheStorage::CreateCacheDidCreateCache( | 604 void ServiceWorkerCacheStorage::CreateCacheDidCreateCache( |
| 660 const std::string& cache_name, | 605 const std::string& cache_name, |
| 661 const CacheAndErrorCallback& callback, | 606 const CacheAndErrorCallback& callback, |
| 662 const scoped_refptr<ServiceWorkerCache>& cache) { | 607 const scoped_refptr<ServiceWorkerCache>& cache) { |
| 663 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 608 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 664 | 609 |
| 665 if (!cache.get()) { | 610 if (!cache.get()) { |
| 666 callback.Run(scoped_refptr<ServiceWorkerCache>(), | 611 callback.Run(scoped_refptr<ServiceWorkerCache>(), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 682 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( | 627 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( |
| 683 const CacheAndErrorCallback& callback, | 628 const CacheAndErrorCallback& callback, |
| 684 const scoped_refptr<ServiceWorkerCache>& cache, | 629 const scoped_refptr<ServiceWorkerCache>& cache, |
| 685 bool success) { | 630 bool success) { |
| 686 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 631 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 687 DCHECK(cache.get()); | 632 DCHECK(cache.get()); |
| 688 | 633 |
| 689 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); | 634 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); |
| 690 } | 635 } |
| 691 | 636 |
| 637 void ServiceWorkerCacheStorage::HasCacheImpl( |
| 638 const std::string& cache_name, |
| 639 const BoolAndErrorCallback& callback) { |
| 640 bool has_cache = cache_map_.find(cache_name) != cache_map_.end(); |
| 641 |
| 642 callback.Run(has_cache, CACHE_STORAGE_ERROR_NO_ERROR); |
| 643 } |
| 644 |
| 645 void ServiceWorkerCacheStorage::DeleteCacheImpl( |
| 646 const std::string& cache_name, |
| 647 const BoolAndErrorCallback& callback) { |
| 648 CacheMap::iterator it = cache_map_.find(cache_name); |
| 649 if (it == cache_map_.end()) { |
| 650 callback.Run(false, CACHE_STORAGE_ERROR_NOT_FOUND); |
| 651 return; |
| 652 } |
| 653 |
| 654 base::WeakPtr<ServiceWorkerCache> cache = it->second; |
| 655 cache_map_.erase(it); |
| 656 |
| 657 // Delete the name from ordered_cache_names_. |
| 658 StringVector::iterator iter = std::find( |
| 659 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name); |
| 660 DCHECK(iter != ordered_cache_names_.end()); |
| 661 ordered_cache_names_.erase(iter); |
| 662 |
| 663 base::Closure closure = |
| 664 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidClose, |
| 665 weak_factory_.GetWeakPtr(), cache_name, callback, |
| 666 ordered_cache_names_, make_scoped_refptr(cache.get())); |
| 667 |
| 668 if (cache) { |
| 669 cache->Close(closure); |
| 670 return; |
| 671 } |
| 672 |
| 673 closure.Run(); |
| 674 } |
| 675 |
| 692 void ServiceWorkerCacheStorage::DeleteCacheDidClose( | 676 void ServiceWorkerCacheStorage::DeleteCacheDidClose( |
| 693 const std::string& cache_name, | 677 const std::string& cache_name, |
| 694 const BoolAndErrorCallback& callback, | 678 const BoolAndErrorCallback& callback, |
| 695 const StringVector& ordered_cache_names, | 679 const StringVector& ordered_cache_names, |
| 696 const scoped_refptr<ServiceWorkerCache>& cache /* might be null */) { | 680 const scoped_refptr<ServiceWorkerCache>& cache /* might be null */) { |
| 697 cache_loader_->WriteIndex( | 681 cache_loader_->WriteIndex( |
| 698 ordered_cache_names, | 682 ordered_cache_names, |
| 699 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex, | 683 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex, |
| 700 weak_factory_.GetWeakPtr(), cache_name, callback)); | 684 weak_factory_.GetWeakPtr(), cache_name, callback)); |
| 701 } | 685 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 714 } | 698 } |
| 715 | 699 |
| 716 void ServiceWorkerCacheStorage::DeleteCacheDidCleanUp( | 700 void ServiceWorkerCacheStorage::DeleteCacheDidCleanUp( |
| 717 const BoolAndErrorCallback& callback, | 701 const BoolAndErrorCallback& callback, |
| 718 bool success) { | 702 bool success) { |
| 719 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 703 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 720 | 704 |
| 721 callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR); | 705 callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR); |
| 722 } | 706 } |
| 723 | 707 |
| 708 void ServiceWorkerCacheStorage::EnumerateCachesImpl( |
| 709 const StringsAndErrorCallback& callback) { |
| 710 callback.Run(ordered_cache_names_, CACHE_STORAGE_ERROR_NO_ERROR); |
| 711 } |
| 712 |
| 713 void ServiceWorkerCacheStorage::MatchCacheImpl( |
| 714 const std::string& cache_name, |
| 715 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 716 const ServiceWorkerCache::ResponseCallback& callback) { |
| 717 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); |
| 718 |
| 719 if (!cache.get()) { |
| 720 callback.Run(ServiceWorkerCache::ErrorTypeNotFound, |
| 721 scoped_ptr<ServiceWorkerResponse>(), |
| 722 scoped_ptr<storage::BlobDataHandle>()); |
| 723 return; |
| 724 } |
| 725 |
| 726 // Pass the cache along to the callback to keep the cache open until match is |
| 727 // done. |
| 728 cache->Match(request.Pass(), |
| 729 base::Bind(&ServiceWorkerCacheStorage::MatchCacheDidMatch, |
| 730 weak_factory_.GetWeakPtr(), cache, callback)); |
| 731 } |
| 732 |
| 724 void ServiceWorkerCacheStorage::MatchCacheDidMatch( | 733 void ServiceWorkerCacheStorage::MatchCacheDidMatch( |
| 725 const scoped_refptr<ServiceWorkerCache>& cache, | 734 const scoped_refptr<ServiceWorkerCache>& cache, |
| 726 const ServiceWorkerCache::ResponseCallback& callback, | 735 const ServiceWorkerCache::ResponseCallback& callback, |
| 727 ServiceWorkerCache::ErrorType error, | 736 ServiceWorkerCache::ErrorType error, |
| 728 scoped_ptr<ServiceWorkerResponse> response, | 737 scoped_ptr<ServiceWorkerResponse> response, |
| 729 scoped_ptr<storage::BlobDataHandle> handle) { | 738 scoped_ptr<storage::BlobDataHandle> handle) { |
| 730 callback.Run(error, response.Pass(), handle.Pass()); | 739 callback.Run(error, response.Pass(), handle.Pass()); |
| 731 } | 740 } |
| 732 | 741 |
| 742 void ServiceWorkerCacheStorage::MatchAllCachesImpl( |
| 743 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 744 const ServiceWorkerCache::ResponseCallback& callback) { |
| 745 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback_copy( |
| 746 new ServiceWorkerCache::ResponseCallback(callback)); |
| 747 |
| 748 ServiceWorkerCache::ResponseCallback* callback_ptr = callback_copy.get(); |
| 749 base::Closure barrier_closure = base::BarrierClosure( |
| 750 ordered_cache_names_.size(), |
| 751 base::Bind(&ServiceWorkerCacheStorage::MatchAllCachesDidMatchAll, |
| 752 weak_factory_.GetWeakPtr(), |
| 753 base::Passed(callback_copy.Pass()))); |
| 754 |
| 755 for (const std::string& cache_name : ordered_cache_names_) { |
| 756 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); |
| 757 DCHECK(cache.get()); |
| 758 |
| 759 cache->Match(make_scoped_ptr(new ServiceWorkerFetchRequest(*request)), |
| 760 base::Bind(&ServiceWorkerCacheStorage::MatchAllCachesDidMatch, |
| 761 weak_factory_.GetWeakPtr(), cache, barrier_closure, |
| 762 callback_ptr)); |
| 763 } |
| 764 } |
| 765 |
| 733 void ServiceWorkerCacheStorage::MatchAllCachesDidMatch( | 766 void ServiceWorkerCacheStorage::MatchAllCachesDidMatch( |
| 734 scoped_refptr<ServiceWorkerCache> cache, | 767 scoped_refptr<ServiceWorkerCache> cache, |
| 735 const base::Closure& barrier_closure, | 768 const base::Closure& barrier_closure, |
| 736 ServiceWorkerCache::ResponseCallback* callback, | 769 ServiceWorkerCache::ResponseCallback* callback, |
| 737 ServiceWorkerCache::ErrorType error, | 770 ServiceWorkerCache::ErrorType error, |
| 738 scoped_ptr<ServiceWorkerResponse> response, | 771 scoped_ptr<ServiceWorkerResponse> response, |
| 739 scoped_ptr<storage::BlobDataHandle> handle) { | 772 scoped_ptr<storage::BlobDataHandle> handle) { |
| 740 if (callback->is_null() || error == ServiceWorkerCache::ErrorTypeNotFound) { | 773 if (callback->is_null() || error == ServiceWorkerCache::ErrorTypeNotFound) { |
| 741 barrier_closure.Run(); | 774 barrier_closure.Run(); |
| 742 return; | 775 return; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 770 if (!cache) { | 803 if (!cache) { |
| 771 scoped_refptr<ServiceWorkerCache> new_cache = | 804 scoped_refptr<ServiceWorkerCache> new_cache = |
| 772 cache_loader_->CreateServiceWorkerCache(cache_name); | 805 cache_loader_->CreateServiceWorkerCache(cache_name); |
| 773 map_iter->second = new_cache->AsWeakPtr(); | 806 map_iter->second = new_cache->AsWeakPtr(); |
| 774 return new_cache; | 807 return new_cache; |
| 775 } | 808 } |
| 776 | 809 |
| 777 return make_scoped_refptr(cache.get()); | 810 return make_scoped_refptr(cache.get()); |
| 778 } | 811 } |
| 779 | 812 |
| 813 void ServiceWorkerCacheStorage::CloseAllCachesImpl( |
| 814 const base::Closure& callback) { |
| 815 int live_cache_count = 0; |
| 816 for (const auto& key_value : cache_map_) { |
| 817 if (key_value.second) |
| 818 live_cache_count += 1; |
| 819 } |
| 820 |
| 821 if (live_cache_count == 0) { |
| 822 callback.Run(); |
| 823 return; |
| 824 } |
| 825 |
| 826 // The closure might modify this object so delay calling it until after |
| 827 // iterating through cache_map_ by adding one to the barrier. |
| 828 base::Closure barrier_closure = |
| 829 base::BarrierClosure(live_cache_count + 1, base::Bind(callback)); |
| 830 |
| 831 for (auto& key_value : cache_map_) { |
| 832 if (key_value.second) { |
| 833 key_value.second->Close(base::Bind( |
| 834 CloseAllCachesDidCloseCache, |
| 835 make_scoped_refptr(key_value.second.get()), barrier_closure)); |
| 836 } |
| 837 } |
| 838 |
| 839 barrier_closure.Run(); |
| 840 } |
| 841 |
| 842 void ServiceWorkerCacheStorage::PendingClosure(const base::Closure& callback) { |
| 843 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = |
| 844 weak_factory_.GetWeakPtr(); |
| 845 |
| 846 callback.Run(); |
| 847 if (cache_storage) |
| 848 scheduler_->CompleteOperationAndRunNext(); |
| 849 } |
| 850 |
| 851 void ServiceWorkerCacheStorage::PendingBoolAndErrorCallback( |
| 852 const BoolAndErrorCallback& callback, |
| 853 bool found, |
| 854 CacheStorageError error) { |
| 855 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = |
| 856 weak_factory_.GetWeakPtr(); |
| 857 |
| 858 callback.Run(found, error); |
| 859 if (cache_storage) |
| 860 scheduler_->CompleteOperationAndRunNext(); |
| 861 } |
| 862 |
| 863 void ServiceWorkerCacheStorage::PendingCacheAndErrorCallback( |
| 864 const CacheAndErrorCallback& callback, |
| 865 const scoped_refptr<ServiceWorkerCache>& cache, |
| 866 CacheStorageError error) { |
| 867 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = |
| 868 weak_factory_.GetWeakPtr(); |
| 869 |
| 870 callback.Run(cache, error); |
| 871 if (cache_storage) |
| 872 scheduler_->CompleteOperationAndRunNext(); |
| 873 } |
| 874 |
| 875 void ServiceWorkerCacheStorage::PendingStringsAndErrorCallback( |
| 876 const StringsAndErrorCallback& callback, |
| 877 const StringVector& strings, |
| 878 CacheStorageError error) { |
| 879 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = |
| 880 weak_factory_.GetWeakPtr(); |
| 881 |
| 882 callback.Run(strings, error); |
| 883 if (cache_storage) |
| 884 scheduler_->CompleteOperationAndRunNext(); |
| 885 } |
| 886 |
| 887 void ServiceWorkerCacheStorage::PendingResponseCallback( |
| 888 const ServiceWorkerCache::ResponseCallback& callback, |
| 889 ServiceWorkerCache::ErrorType error, |
| 890 scoped_ptr<ServiceWorkerResponse> response, |
| 891 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
| 892 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = |
| 893 weak_factory_.GetWeakPtr(); |
| 894 |
| 895 callback.Run(error, response.Pass(), blob_data_handle.Pass()); |
| 896 if (cache_storage) |
| 897 scheduler_->CompleteOperationAndRunNext(); |
| 898 } |
| 899 |
| 780 } // namespace content | 900 } // namespace content |
| OLD | NEW |