| 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/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 callback)); | 430 callback)); |
| 431 return; | 431 return; |
| 432 } | 432 } |
| 433 | 433 |
| 434 CacheContext* cache_context = GetLoadedCache(cache_name); | 434 CacheContext* cache_context = GetLoadedCache(cache_name); |
| 435 if (!cache_context) { | 435 if (!cache_context) { |
| 436 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_NOT_FOUND); | 436 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_NOT_FOUND); |
| 437 return; | 437 return; |
| 438 } | 438 } |
| 439 | 439 |
| 440 ServiceWorkerCache* cache = cache_context->cache.get(); | 440 callback.Run(cache_context->id, CACHE_STORAGE_ERROR_NO_ERROR); |
| 441 | |
| 442 if (cache->HasCreatedBackend()) | |
| 443 return callback.Run(cache_context->id, CACHE_STORAGE_ERROR_NO_ERROR); | |
| 444 | |
| 445 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, | |
| 446 weak_factory_.GetWeakPtr(), | |
| 447 cache->AsWeakPtr(), | |
| 448 cache_context->id, | |
| 449 callback)); | |
| 450 } | 441 } |
| 451 | 442 |
| 452 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, | 443 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, |
| 453 const BoolAndErrorCallback& callback) { | 444 const BoolAndErrorCallback& callback) { |
| 454 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 445 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 455 | 446 |
| 456 if (!initialized_) { | 447 if (!initialized_) { |
| 457 LazyInit(base::Bind(&ServiceWorkerCacheStorage::HasCache, | 448 LazyInit(base::Bind(&ServiceWorkerCacheStorage::HasCache, |
| 458 weak_factory_.GetWeakPtr(), | 449 weak_factory_.GetWeakPtr(), |
| 459 cache_name, | 450 cache_name, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 | 502 |
| 512 std::vector<std::string> names; | 503 std::vector<std::string> names; |
| 513 for (NameMap::const_iterator it = name_map_.begin(); it != name_map_.end(); | 504 for (NameMap::const_iterator it = name_map_.begin(); it != name_map_.end(); |
| 514 ++it) { | 505 ++it) { |
| 515 names.push_back(it->first); | 506 names.push_back(it->first); |
| 516 } | 507 } |
| 517 | 508 |
| 518 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); | 509 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); |
| 519 } | 510 } |
| 520 | 511 |
| 521 void ServiceWorkerCacheStorage::DidCreateBackend( | |
| 522 base::WeakPtr<ServiceWorkerCache> cache, | |
| 523 CacheID cache_id, | |
| 524 const CacheAndErrorCallback& callback, | |
| 525 ServiceWorkerCache::ErrorType error) { | |
| 526 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 527 | |
| 528 if (error != ServiceWorkerCache::ErrorTypeOK || !cache) { | |
| 529 // TODO(jkarlin): This should delete the directory and try again in case | |
| 530 // the cache is simply corrupt. | |
| 531 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_STORAGE); | |
| 532 return; | |
| 533 } | |
| 534 | |
| 535 callback.Run(cache_id, CACHE_STORAGE_ERROR_NO_ERROR); | |
| 536 } | |
| 537 | |
| 538 // Init is run lazily so that it is called on the proper MessageLoop. | 512 // Init is run lazily so that it is called on the proper MessageLoop. |
| 539 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { | 513 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { |
| 540 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 514 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 541 DCHECK(!initialized_); | 515 DCHECK(!initialized_); |
| 542 | 516 |
| 543 init_callbacks_.push_back(callback); | 517 init_callbacks_.push_back(callback); |
| 544 | 518 |
| 545 // If this isn't the first call to LazyInit then return as the initialization | 519 // If this isn't the first call to LazyInit then return as the initialization |
| 546 // has already started. | 520 // has already started. |
| 547 if (init_callbacks_.size() > 1u) | 521 if (init_callbacks_.size() > 1u) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 cache_context->id)); | 631 cache_context->id)); |
| 658 } | 632 } |
| 659 | 633 |
| 660 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( | 634 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( |
| 661 const CacheAndErrorCallback& callback, | 635 const CacheAndErrorCallback& callback, |
| 662 base::WeakPtr<ServiceWorkerCache> cache, | 636 base::WeakPtr<ServiceWorkerCache> cache, |
| 663 CacheID id, | 637 CacheID id, |
| 664 bool success) { | 638 bool success) { |
| 665 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 639 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 666 if (!cache) { | 640 if (!cache) { |
| 667 callback.Run(false, CACHE_STORAGE_ERROR_CLOSING); | 641 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_CLOSING); |
| 668 return; | 642 return; |
| 669 } | 643 } |
| 670 | 644 |
| 671 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, | 645 callback.Run(id, CACHE_STORAGE_ERROR_NO_ERROR); |
| 672 weak_factory_.GetWeakPtr(), | |
| 673 cache, | |
| 674 id, | |
| 675 callback)); | |
| 676 } | 646 } |
| 677 | 647 |
| 678 void ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex( | 648 void ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex( |
| 679 const std::string& cache_name, | 649 const std::string& cache_name, |
| 680 const BoolAndErrorCallback& callback, | 650 const BoolAndErrorCallback& callback, |
| 681 bool success) { | 651 bool success) { |
| 682 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 652 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 683 | 653 |
| 684 cache_loader_->CleanUpDeletedCache( | 654 cache_loader_->CleanUpDeletedCache( |
| 685 cache_name, | 655 cache_name, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 704 NameMap::const_iterator name_iter = name_map_.find(cache_name); | 674 NameMap::const_iterator name_iter = name_map_.find(cache_name); |
| 705 if (name_iter == name_map_.end()) | 675 if (name_iter == name_map_.end()) |
| 706 return NULL; | 676 return NULL; |
| 707 | 677 |
| 708 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); | 678 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); |
| 709 DCHECK(map_iter != cache_map_.end()); | 679 DCHECK(map_iter != cache_map_.end()); |
| 710 return map_iter->second; | 680 return map_iter->second; |
| 711 } | 681 } |
| 712 | 682 |
| 713 } // namespace content | 683 } // namespace content |
| OLD | NEW |