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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 virtual void WriteIndex(CacheMap* caches, const BoolCallback& callback) = 0; | 56 virtual void WriteIndex(CacheMap* caches, const BoolCallback& callback) = 0; |
57 | 57 |
58 // Loads the cache names from disk if applicable. | 58 // Loads the cache names from disk if applicable. |
59 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, | 59 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, |
60 const StringsCallback& callback) = 0; | 60 const StringsCallback& callback) = 0; |
61 | 61 |
62 protected: | 62 protected: |
63 friend class base::RefCountedThreadSafe< | 63 friend class base::RefCountedThreadSafe< |
64 ServiceWorkerCacheStorage::CacheLoader>; | 64 ServiceWorkerCacheStorage::CacheLoader>; |
65 | 65 |
66 virtual ~CacheLoader() {}; | 66 virtual ~CacheLoader() {} |
67 virtual void LoadCacheImpl(const std::string&) {} | 67 virtual void LoadCacheImpl(const std::string&) {} |
68 | 68 |
69 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 69 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
70 net::URLRequestContext* request_context_; | 70 net::URLRequestContext* request_context_; |
71 base::WeakPtr<storage::BlobStorageContext> blob_context_; | 71 base::WeakPtr<storage::BlobStorageContext> blob_context_; |
72 }; | 72 }; |
73 | 73 |
74 class ServiceWorkerCacheStorage::MemoryLoader | 74 class ServiceWorkerCacheStorage::MemoryLoader |
75 : public ServiceWorkerCacheStorage::CacheLoader { | 75 : public ServiceWorkerCacheStorage::CacheLoader { |
76 public: | 76 public: |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 callback, | 335 callback, |
336 body)); | 336 body)); |
337 } | 337 } |
338 | 338 |
339 void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string> > names, | 339 void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string> > names, |
340 const StringsCallback& callback, | 340 const StringsCallback& callback, |
341 const std::string& serialized) { | 341 const std::string& serialized) { |
342 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 342 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
343 | 343 |
344 ServiceWorkerCacheStorageIndex index; | 344 ServiceWorkerCacheStorageIndex index; |
345 index.ParseFromString(serialized); | 345 if (index.ParseFromString(serialized)) { |
346 | 346 for (int i = 0, max = index.cache_size(); i < max; ++i) { |
347 for (int i = 0, max = index.cache_size(); i < max; ++i) { | 347 const ServiceWorkerCacheStorageIndex::Cache& cache = index.cache(i); |
348 const ServiceWorkerCacheStorageIndex::Cache& cache = index.cache(i); | 348 names->push_back(cache.name()); |
349 names->push_back(cache.name()); | 349 } |
350 } | 350 } |
351 | 351 |
352 // TODO(jkarlin): Delete caches that are in the directory and not returned | 352 // TODO(jkarlin): Delete caches that are in the directory and not returned |
353 // in LoadIndex. | 353 // in LoadIndex. |
354 callback.Run(names.Pass()); | 354 callback.Run(names.Pass()); |
355 } | 355 } |
356 | 356 |
357 private: | 357 private: |
358 virtual ~SimpleCacheLoader() {} | 358 virtual ~SimpleCacheLoader() {} |
359 | 359 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 callback.Run(0, CACHE_STORAGE_ERROR_EMPTY_KEY); | 439 callback.Run(0, CACHE_STORAGE_ERROR_EMPTY_KEY); |
440 return; | 440 return; |
441 } | 441 } |
442 | 442 |
443 ServiceWorkerCache* cache = GetLoadedCache(cache_name); | 443 ServiceWorkerCache* cache = GetLoadedCache(cache_name); |
444 if (!cache) { | 444 if (!cache) { |
445 callback.Run(0, CACHE_STORAGE_ERROR_NOT_FOUND); | 445 callback.Run(0, CACHE_STORAGE_ERROR_NOT_FOUND); |
446 return; | 446 return; |
447 } | 447 } |
448 | 448 |
| 449 if (cache->HasCreatedBackend()) |
| 450 return callback.Run(cache->id(), CACHE_STORAGE_ERROR_NO_ERROR); |
| 451 |
449 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, | 452 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, |
450 weak_factory_.GetWeakPtr(), | 453 weak_factory_.GetWeakPtr(), |
451 cache->AsWeakPtr(), | 454 cache->AsWeakPtr(), |
452 callback)); | 455 callback)); |
453 } | 456 } |
454 | 457 |
455 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, | 458 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, |
456 const BoolAndErrorCallback& callback) { | 459 const BoolAndErrorCallback& callback) { |
457 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 460 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
458 | 461 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 ++it) { | 529 ++it) { |
527 names.push_back(it->first); | 530 names.push_back(it->first); |
528 } | 531 } |
529 | 532 |
530 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); | 533 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); |
531 } | 534 } |
532 | 535 |
533 void ServiceWorkerCacheStorage::DidCreateBackend( | 536 void ServiceWorkerCacheStorage::DidCreateBackend( |
534 base::WeakPtr<ServiceWorkerCache> cache, | 537 base::WeakPtr<ServiceWorkerCache> cache, |
535 const CacheAndErrorCallback& callback, | 538 const CacheAndErrorCallback& callback, |
536 bool success) { | 539 ServiceWorkerCache::ErrorType error) { |
537 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 540 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
538 | 541 |
539 if (!success || !cache) { | 542 if (error != ServiceWorkerCache::ErrorTypeOK || !cache) { |
540 // TODO(jkarlin): This should delete the directory and try again in case | 543 // TODO(jkarlin): This should delete the directory and try again in case |
541 // the cache is simply corrupt. | 544 // the cache is simply corrupt. |
542 callback.Run(0, CACHE_STORAGE_ERROR_STORAGE); | 545 callback.Run(0, CACHE_STORAGE_ERROR_STORAGE); |
543 return; | 546 return; |
544 } | 547 } |
| 548 |
545 callback.Run(cache->id(), CACHE_STORAGE_ERROR_NO_ERROR); | 549 callback.Run(cache->id(), CACHE_STORAGE_ERROR_NO_ERROR); |
546 } | 550 } |
547 | 551 |
548 // Init is run lazily so that it is called on the proper MessageLoop. | 552 // Init is run lazily so that it is called on the proper MessageLoop. |
549 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { | 553 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { |
550 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 554 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
551 DCHECK(!initialized_); | 555 DCHECK(!initialized_); |
552 | 556 |
553 init_callbacks_.push_back(callback); | 557 init_callbacks_.push_back(callback); |
554 | 558 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 | 669 |
666 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( | 670 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( |
667 const CacheAndErrorCallback& callback, | 671 const CacheAndErrorCallback& callback, |
668 base::WeakPtr<ServiceWorkerCache> cache, | 672 base::WeakPtr<ServiceWorkerCache> cache, |
669 bool success) { | 673 bool success) { |
670 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 674 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
671 if (!cache) { | 675 if (!cache) { |
672 callback.Run(false, CACHE_STORAGE_ERROR_STORAGE); | 676 callback.Run(false, CACHE_STORAGE_ERROR_STORAGE); |
673 return; | 677 return; |
674 } | 678 } |
| 679 |
675 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, | 680 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, |
676 weak_factory_.GetWeakPtr(), | 681 weak_factory_.GetWeakPtr(), |
677 cache, | 682 cache, |
678 callback)); | 683 callback)); |
679 } | 684 } |
680 | 685 |
681 void ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex( | 686 void ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex( |
682 const std::string& cache_name, | 687 const std::string& cache_name, |
683 const BoolAndErrorCallback& callback, | 688 const BoolAndErrorCallback& callback, |
684 bool success) { | 689 bool success) { |
(...skipping 22 matching lines...) Expand all Loading... |
707 NameMap::const_iterator it = name_map_.find(cache_name); | 712 NameMap::const_iterator it = name_map_.find(cache_name); |
708 if (it == name_map_.end()) | 713 if (it == name_map_.end()) |
709 return NULL; | 714 return NULL; |
710 | 715 |
711 ServiceWorkerCache* cache = cache_map_.Lookup(it->second); | 716 ServiceWorkerCache* cache = cache_map_.Lookup(it->second); |
712 DCHECK(cache); | 717 DCHECK(cache); |
713 return cache; | 718 return cache; |
714 } | 719 } |
715 | 720 |
716 } // namespace content | 721 } // namespace content |
OLD | NEW |