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