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/metrics/histogram_macros.h" |
13 #include "base/sha1.h" | 14 #include "base/sha1.h" |
14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
17 #include "content/browser/service_worker/service_worker_cache.h" | 18 #include "content/browser/service_worker/service_worker_cache.h" |
18 #include "content/browser/service_worker/service_worker_cache.pb.h" | 19 #include "content/browser/service_worker/service_worker_cache.pb.h" |
19 #include "content/browser/service_worker/service_worker_cache_scheduler.h" | 20 #include "content/browser/service_worker/service_worker_cache_scheduler.h" |
20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
21 #include "net/base/directory_lister.h" | 22 #include "net/base/directory_lister.h" |
22 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 const CacheAndErrorCallback& callback, | 607 const CacheAndErrorCallback& callback, |
607 const scoped_refptr<ServiceWorkerCache>& cache) { | 608 const scoped_refptr<ServiceWorkerCache>& cache) { |
608 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 609 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
609 | 610 |
610 if (!cache.get()) { | 611 if (!cache.get()) { |
611 callback.Run(scoped_refptr<ServiceWorkerCache>(), | 612 callback.Run(scoped_refptr<ServiceWorkerCache>(), |
612 CACHE_STORAGE_ERROR_CLOSING); | 613 CACHE_STORAGE_ERROR_CLOSING); |
613 return; | 614 return; |
614 } | 615 } |
615 | 616 |
| 617 UMA_HISTOGRAM_BOOLEAN("ServiceWorkerCache.CreateCacheStorageResult", |
| 618 cache != nullptr); |
| 619 |
616 cache_map_.insert(std::make_pair(cache_name, cache->AsWeakPtr())); | 620 cache_map_.insert(std::make_pair(cache_name, cache->AsWeakPtr())); |
617 ordered_cache_names_.push_back(cache_name); | 621 ordered_cache_names_.push_back(cache_name); |
618 | 622 |
619 cache_loader_->WriteIndex( | 623 cache_loader_->WriteIndex( |
620 ordered_cache_names_, | 624 ordered_cache_names_, |
621 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidWriteIndex, | 625 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidWriteIndex, |
622 weak_factory_.GetWeakPtr(), | 626 weak_factory_.GetWeakPtr(), |
623 callback, | 627 callback, |
624 cache)); | 628 cache)); |
625 } | 629 } |
626 | 630 |
627 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( | 631 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( |
628 const CacheAndErrorCallback& callback, | 632 const CacheAndErrorCallback& callback, |
629 const scoped_refptr<ServiceWorkerCache>& cache, | 633 const scoped_refptr<ServiceWorkerCache>& cache, |
630 bool success) { | 634 bool success) { |
631 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 635 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
632 DCHECK(cache.get()); | 636 DCHECK(cache.get()); |
633 | 637 |
| 638 // TODO(jkarlin): Handle !success. |
| 639 |
634 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); | 640 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); |
635 } | 641 } |
636 | 642 |
637 void ServiceWorkerCacheStorage::HasCacheImpl( | 643 void ServiceWorkerCacheStorage::HasCacheImpl( |
638 const std::string& cache_name, | 644 const std::string& cache_name, |
639 const BoolAndErrorCallback& callback) { | 645 const BoolAndErrorCallback& callback) { |
640 bool has_cache = cache_map_.find(cache_name) != cache_map_.end(); | 646 bool has_cache = cache_map_.find(cache_name) != cache_map_.end(); |
641 | 647 |
642 callback.Run(has_cache, CACHE_STORAGE_ERROR_NO_ERROR); | 648 callback.Run(has_cache, CACHE_STORAGE_ERROR_NO_ERROR); |
643 } | 649 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 callback.Run(ordered_cache_names_, CACHE_STORAGE_ERROR_NO_ERROR); | 716 callback.Run(ordered_cache_names_, CACHE_STORAGE_ERROR_NO_ERROR); |
711 } | 717 } |
712 | 718 |
713 void ServiceWorkerCacheStorage::MatchCacheImpl( | 719 void ServiceWorkerCacheStorage::MatchCacheImpl( |
714 const std::string& cache_name, | 720 const std::string& cache_name, |
715 scoped_ptr<ServiceWorkerFetchRequest> request, | 721 scoped_ptr<ServiceWorkerFetchRequest> request, |
716 const ServiceWorkerCache::ResponseCallback& callback) { | 722 const ServiceWorkerCache::ResponseCallback& callback) { |
717 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); | 723 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); |
718 | 724 |
719 if (!cache.get()) { | 725 if (!cache.get()) { |
720 callback.Run(ServiceWorkerCache::ErrorTypeNotFound, | 726 callback.Run(ServiceWorkerCache::ERROR_TYPE_NOT_FOUND, |
721 scoped_ptr<ServiceWorkerResponse>(), | 727 scoped_ptr<ServiceWorkerResponse>(), |
722 scoped_ptr<storage::BlobDataHandle>()); | 728 scoped_ptr<storage::BlobDataHandle>()); |
723 return; | 729 return; |
724 } | 730 } |
725 | 731 |
726 // Pass the cache along to the callback to keep the cache open until match is | 732 // Pass the cache along to the callback to keep the cache open until match is |
727 // done. | 733 // done. |
728 cache->Match(request.Pass(), | 734 cache->Match(request.Pass(), |
729 base::Bind(&ServiceWorkerCacheStorage::MatchCacheDidMatch, | 735 base::Bind(&ServiceWorkerCacheStorage::MatchCacheDidMatch, |
730 weak_factory_.GetWeakPtr(), cache, callback)); | 736 weak_factory_.GetWeakPtr(), cache, callback)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 } | 769 } |
764 } | 770 } |
765 | 771 |
766 void ServiceWorkerCacheStorage::MatchAllCachesDidMatch( | 772 void ServiceWorkerCacheStorage::MatchAllCachesDidMatch( |
767 scoped_refptr<ServiceWorkerCache> cache, | 773 scoped_refptr<ServiceWorkerCache> cache, |
768 const base::Closure& barrier_closure, | 774 const base::Closure& barrier_closure, |
769 ServiceWorkerCache::ResponseCallback* callback, | 775 ServiceWorkerCache::ResponseCallback* callback, |
770 ServiceWorkerCache::ErrorType error, | 776 ServiceWorkerCache::ErrorType error, |
771 scoped_ptr<ServiceWorkerResponse> response, | 777 scoped_ptr<ServiceWorkerResponse> response, |
772 scoped_ptr<storage::BlobDataHandle> handle) { | 778 scoped_ptr<storage::BlobDataHandle> handle) { |
773 if (callback->is_null() || error == ServiceWorkerCache::ErrorTypeNotFound) { | 779 if (callback->is_null() || |
| 780 error == ServiceWorkerCache::ERROR_TYPE_NOT_FOUND) { |
774 barrier_closure.Run(); | 781 barrier_closure.Run(); |
775 return; | 782 return; |
776 } | 783 } |
777 callback->Run(error, response.Pass(), handle.Pass()); | 784 callback->Run(error, response.Pass(), handle.Pass()); |
778 callback->Reset(); // Only call the callback once. | 785 callback->Reset(); // Only call the callback once. |
779 | 786 |
780 barrier_closure.Run(); | 787 barrier_closure.Run(); |
781 } | 788 } |
782 | 789 |
783 void ServiceWorkerCacheStorage::MatchAllCachesDidMatchAll( | 790 void ServiceWorkerCacheStorage::MatchAllCachesDidMatchAll( |
784 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback) { | 791 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback) { |
785 if (!callback->is_null()) { | 792 if (!callback->is_null()) { |
786 callback->Run(ServiceWorkerCache::ErrorTypeNotFound, | 793 callback->Run(ServiceWorkerCache::ERROR_TYPE_NOT_FOUND, |
787 scoped_ptr<ServiceWorkerResponse>(), | 794 scoped_ptr<ServiceWorkerResponse>(), |
788 scoped_ptr<storage::BlobDataHandle>()); | 795 scoped_ptr<storage::BlobDataHandle>()); |
789 } | 796 } |
790 } | 797 } |
791 | 798 |
792 scoped_refptr<ServiceWorkerCache> ServiceWorkerCacheStorage::GetLoadedCache( | 799 scoped_refptr<ServiceWorkerCache> ServiceWorkerCacheStorage::GetLoadedCache( |
793 const std::string& cache_name) { | 800 const std::string& cache_name) { |
794 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 801 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
795 DCHECK(initialized_); | 802 DCHECK(initialized_); |
796 | 803 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 898 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
892 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = | 899 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = |
893 weak_factory_.GetWeakPtr(); | 900 weak_factory_.GetWeakPtr(); |
894 | 901 |
895 callback.Run(error, response.Pass(), blob_data_handle.Pass()); | 902 callback.Run(error, response.Pass(), blob_data_handle.Pass()); |
896 if (cache_storage) | 903 if (cache_storage) |
897 scheduler_->CompleteOperationAndRunNext(); | 904 scheduler_->CompleteOperationAndRunNext(); |
898 } | 905 } |
899 | 906 |
900 } // namespace content | 907 } // namespace content |
OLD | NEW |