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.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", | |
michaeln
2015/03/13 22:47:16
Should the label be "CreateCacheResult"?
jkarlin
2015/03/14 01:47:19
The names are a bit misleading. ServiceWorkerCache
| |
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) { |
michaeln
2015/03/13 22:47:16
unrelated but why is 'success' not important here?
jkarlin
2015/03/14 01:47:19
Eek. Added a todo.
| |
631 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 635 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
632 DCHECK(cache.get()); | 636 DCHECK(cache.get()); |
633 | 637 |
634 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); | 638 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); |
635 } | 639 } |
636 | 640 |
637 void ServiceWorkerCacheStorage::HasCacheImpl( | 641 void ServiceWorkerCacheStorage::HasCacheImpl( |
638 const std::string& cache_name, | 642 const std::string& cache_name, |
639 const BoolAndErrorCallback& callback) { | 643 const BoolAndErrorCallback& callback) { |
640 bool has_cache = cache_map_.find(cache_name) != cache_map_.end(); | 644 bool has_cache = cache_map_.find(cache_name) != cache_map_.end(); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
710 callback.Run(ordered_cache_names_, CACHE_STORAGE_ERROR_NO_ERROR); | 714 callback.Run(ordered_cache_names_, CACHE_STORAGE_ERROR_NO_ERROR); |
711 } | 715 } |
712 | 716 |
713 void ServiceWorkerCacheStorage::MatchCacheImpl( | 717 void ServiceWorkerCacheStorage::MatchCacheImpl( |
714 const std::string& cache_name, | 718 const std::string& cache_name, |
715 scoped_ptr<ServiceWorkerFetchRequest> request, | 719 scoped_ptr<ServiceWorkerFetchRequest> request, |
716 const ServiceWorkerCache::ResponseCallback& callback) { | 720 const ServiceWorkerCache::ResponseCallback& callback) { |
717 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); | 721 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); |
718 | 722 |
719 if (!cache.get()) { | 723 if (!cache.get()) { |
720 callback.Run(ServiceWorkerCache::ErrorTypeNotFound, | 724 callback.Run(ServiceWorkerCache::ERROR_TYPE_NOT_FOUND, |
721 scoped_ptr<ServiceWorkerResponse>(), | 725 scoped_ptr<ServiceWorkerResponse>(), |
722 scoped_ptr<storage::BlobDataHandle>()); | 726 scoped_ptr<storage::BlobDataHandle>()); |
723 return; | 727 return; |
724 } | 728 } |
725 | 729 |
726 // Pass the cache along to the callback to keep the cache open until match is | 730 // Pass the cache along to the callback to keep the cache open until match is |
727 // done. | 731 // done. |
728 cache->Match(request.Pass(), | 732 cache->Match(request.Pass(), |
729 base::Bind(&ServiceWorkerCacheStorage::MatchCacheDidMatch, | 733 base::Bind(&ServiceWorkerCacheStorage::MatchCacheDidMatch, |
730 weak_factory_.GetWeakPtr(), cache, callback)); | 734 weak_factory_.GetWeakPtr(), cache, callback)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
763 } | 767 } |
764 } | 768 } |
765 | 769 |
766 void ServiceWorkerCacheStorage::MatchAllCachesDidMatch( | 770 void ServiceWorkerCacheStorage::MatchAllCachesDidMatch( |
767 scoped_refptr<ServiceWorkerCache> cache, | 771 scoped_refptr<ServiceWorkerCache> cache, |
768 const base::Closure& barrier_closure, | 772 const base::Closure& barrier_closure, |
769 ServiceWorkerCache::ResponseCallback* callback, | 773 ServiceWorkerCache::ResponseCallback* callback, |
770 ServiceWorkerCache::ErrorType error, | 774 ServiceWorkerCache::ErrorType error, |
771 scoped_ptr<ServiceWorkerResponse> response, | 775 scoped_ptr<ServiceWorkerResponse> response, |
772 scoped_ptr<storage::BlobDataHandle> handle) { | 776 scoped_ptr<storage::BlobDataHandle> handle) { |
773 if (callback->is_null() || error == ServiceWorkerCache::ErrorTypeNotFound) { | 777 if (callback->is_null() || |
778 error == ServiceWorkerCache::ERROR_TYPE_NOT_FOUND) { | |
774 barrier_closure.Run(); | 779 barrier_closure.Run(); |
775 return; | 780 return; |
776 } | 781 } |
777 callback->Run(error, response.Pass(), handle.Pass()); | 782 callback->Run(error, response.Pass(), handle.Pass()); |
778 callback->Reset(); // Only call the callback once. | 783 callback->Reset(); // Only call the callback once. |
779 | 784 |
780 barrier_closure.Run(); | 785 barrier_closure.Run(); |
781 } | 786 } |
782 | 787 |
783 void ServiceWorkerCacheStorage::MatchAllCachesDidMatchAll( | 788 void ServiceWorkerCacheStorage::MatchAllCachesDidMatchAll( |
784 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback) { | 789 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback) { |
785 if (!callback->is_null()) { | 790 if (!callback->is_null()) { |
786 callback->Run(ServiceWorkerCache::ErrorTypeNotFound, | 791 callback->Run(ServiceWorkerCache::ERROR_TYPE_NOT_FOUND, |
787 scoped_ptr<ServiceWorkerResponse>(), | 792 scoped_ptr<ServiceWorkerResponse>(), |
788 scoped_ptr<storage::BlobDataHandle>()); | 793 scoped_ptr<storage::BlobDataHandle>()); |
789 } | 794 } |
790 } | 795 } |
791 | 796 |
792 scoped_refptr<ServiceWorkerCache> ServiceWorkerCacheStorage::GetLoadedCache( | 797 scoped_refptr<ServiceWorkerCache> ServiceWorkerCacheStorage::GetLoadedCache( |
793 const std::string& cache_name) { | 798 const std::string& cache_name) { |
794 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 799 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
795 DCHECK(initialized_); | 800 DCHECK(initialized_); |
796 | 801 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
891 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 896 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
892 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = | 897 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = |
893 weak_factory_.GetWeakPtr(); | 898 weak_factory_.GetWeakPtr(); |
894 | 899 |
895 callback.Run(error, response.Pass(), blob_data_handle.Pass()); | 900 callback.Run(error, response.Pass(), blob_data_handle.Pass()); |
896 if (cache_storage) | 901 if (cache_storage) |
897 scheduler_->CompleteOperationAndRunNext(); | 902 scheduler_->CompleteOperationAndRunNext(); |
898 } | 903 } |
899 | 904 |
900 } // namespace content | 905 } // namespace content |
OLD | NEW |