| 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/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_EMPTY_KEY); | 440 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_EMPTY_KEY); |
| 441 return; | 441 return; |
| 442 } | 442 } |
| 443 | 443 |
| 444 CacheContext* cache_context = GetLoadedCache(cache_name); | 444 CacheContext* cache_context = GetLoadedCache(cache_name); |
| 445 if (!cache_context) { | 445 if (!cache_context) { |
| 446 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_NOT_FOUND); | 446 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_NOT_FOUND); |
| 447 return; | 447 return; |
| 448 } | 448 } |
| 449 | 449 |
| 450 ServiceWorkerCache* cache = cache_context->cache.get(); | 450 callback.Run(cache_context->id, CACHE_STORAGE_ERROR_NO_ERROR); |
| 451 | |
| 452 if (cache->HasCreatedBackend()) | |
| 453 return callback.Run(cache_context->id, CACHE_STORAGE_ERROR_NO_ERROR); | |
| 454 | |
| 455 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, | |
| 456 weak_factory_.GetWeakPtr(), | |
| 457 cache->AsWeakPtr(), | |
| 458 cache_context->id, | |
| 459 callback)); | |
| 460 } | 451 } |
| 461 | 452 |
| 462 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, | 453 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, |
| 463 const BoolAndErrorCallback& callback) { | 454 const BoolAndErrorCallback& callback) { |
| 464 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 455 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 465 | 456 |
| 466 if (!initialized_) { | 457 if (!initialized_) { |
| 467 LazyInit(base::Bind(&ServiceWorkerCacheStorage::HasCache, | 458 LazyInit(base::Bind(&ServiceWorkerCacheStorage::HasCache, |
| 468 weak_factory_.GetWeakPtr(), | 459 weak_factory_.GetWeakPtr(), |
| 469 cache_name, | 460 cache_name, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 | 522 |
| 532 std::vector<std::string> names; | 523 std::vector<std::string> names; |
| 533 for (NameMap::const_iterator it = name_map_.begin(); it != name_map_.end(); | 524 for (NameMap::const_iterator it = name_map_.begin(); it != name_map_.end(); |
| 534 ++it) { | 525 ++it) { |
| 535 names.push_back(it->first); | 526 names.push_back(it->first); |
| 536 } | 527 } |
| 537 | 528 |
| 538 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); | 529 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); |
| 539 } | 530 } |
| 540 | 531 |
| 541 void ServiceWorkerCacheStorage::DidCreateBackend( | |
| 542 base::WeakPtr<ServiceWorkerCache> cache, | |
| 543 CacheID cache_id, | |
| 544 const CacheAndErrorCallback& callback, | |
| 545 ServiceWorkerCache::ErrorType error) { | |
| 546 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 547 | |
| 548 if (error != ServiceWorkerCache::ErrorTypeOK || !cache) { | |
| 549 // TODO(jkarlin): This should delete the directory and try again in case | |
| 550 // the cache is simply corrupt. | |
| 551 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_STORAGE); | |
| 552 return; | |
| 553 } | |
| 554 | |
| 555 callback.Run(cache_id, CACHE_STORAGE_ERROR_NO_ERROR); | |
| 556 } | |
| 557 | |
| 558 // Init is run lazily so that it is called on the proper MessageLoop. | 532 // Init is run lazily so that it is called on the proper MessageLoop. |
| 559 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { | 533 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { |
| 560 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 534 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 561 DCHECK(!initialized_); | 535 DCHECK(!initialized_); |
| 562 | 536 |
| 563 init_callbacks_.push_back(callback); | 537 init_callbacks_.push_back(callback); |
| 564 | 538 |
| 565 // If this isn't the first call to LazyInit then return as the initialization | 539 // If this isn't the first call to LazyInit then return as the initialization |
| 566 // has already started. | 540 // has already started. |
| 567 if (init_callbacks_.size() > 1u) | 541 if (init_callbacks_.size() > 1u) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 cache_context->id)); | 651 cache_context->id)); |
| 678 } | 652 } |
| 679 | 653 |
| 680 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( | 654 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( |
| 681 const CacheAndErrorCallback& callback, | 655 const CacheAndErrorCallback& callback, |
| 682 base::WeakPtr<ServiceWorkerCache> cache, | 656 base::WeakPtr<ServiceWorkerCache> cache, |
| 683 CacheID id, | 657 CacheID id, |
| 684 bool success) { | 658 bool success) { |
| 685 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 659 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 686 if (!cache) { | 660 if (!cache) { |
| 687 callback.Run(false, CACHE_STORAGE_ERROR_CLOSING); | 661 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_CLOSING); |
| 688 return; | 662 return; |
| 689 } | 663 } |
| 690 | 664 |
| 691 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, | 665 callback.Run(id, CACHE_STORAGE_ERROR_NO_ERROR); |
| 692 weak_factory_.GetWeakPtr(), | |
| 693 cache, | |
| 694 id, | |
| 695 callback)); | |
| 696 } | 666 } |
| 697 | 667 |
| 698 void ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex( | 668 void ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex( |
| 699 const std::string& cache_name, | 669 const std::string& cache_name, |
| 700 const BoolAndErrorCallback& callback, | 670 const BoolAndErrorCallback& callback, |
| 701 bool success) { | 671 bool success) { |
| 702 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 672 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 703 | 673 |
| 704 cache_loader_->CleanUpDeletedCache( | 674 cache_loader_->CleanUpDeletedCache( |
| 705 cache_name, | 675 cache_name, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 724 NameMap::const_iterator name_iter = name_map_.find(cache_name); | 694 NameMap::const_iterator name_iter = name_map_.find(cache_name); |
| 725 if (name_iter == name_map_.end()) | 695 if (name_iter == name_map_.end()) |
| 726 return NULL; | 696 return NULL; |
| 727 | 697 |
| 728 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); | 698 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); |
| 729 DCHECK(map_iter != cache_map_.end()); | 699 DCHECK(map_iter != cache_map_.end()); |
| 730 return map_iter->second; | 700 return map_iter->second; |
| 731 } | 701 } |
| 732 | 702 |
| 733 } // namespace content | 703 } // namespace content |
| OLD | NEW |