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.h" | 5 #include "content/browser/service_worker/service_worker_cache.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/guid.h" | 10 #include "base/guid.h" |
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 cache(cache), | 598 cache(cache), |
599 out_keys(new ServiceWorkerCache::Requests()), | 599 out_keys(new ServiceWorkerCache::Requests()), |
600 backend_iterator(NULL), | 600 backend_iterator(NULL), |
601 enumerated_entry(NULL) {} | 601 enumerated_entry(NULL) {} |
602 | 602 |
603 ~KeysContext() { | 603 ~KeysContext() { |
604 for (size_t i = 0, max = entries.size(); i < max; ++i) | 604 for (size_t i = 0, max = entries.size(); i < max; ++i) |
605 entries[i]->Close(); | 605 entries[i]->Close(); |
606 if (enumerated_entry) | 606 if (enumerated_entry) |
607 enumerated_entry->Close(); | 607 enumerated_entry->Close(); |
608 if (cache && backend_iterator) | 608 if (cache && backend_iterator && cache->backend_) |
609 cache->backend_->EndEnumeration(&backend_iterator); | 609 cache->backend_->EndEnumeration(&backend_iterator); |
610 } | 610 } |
611 | 611 |
612 // The callback passed to the Keys() function. | 612 // The callback passed to the Keys() function. |
613 ServiceWorkerCache::RequestsCallback original_callback; | 613 ServiceWorkerCache::RequestsCallback original_callback; |
614 | 614 |
615 // The ServiceWorkerCache that Keys was called on. | 615 // The ServiceWorkerCache that Keys was called on. |
616 base::WeakPtr<ServiceWorkerCache> cache; | 616 base::WeakPtr<ServiceWorkerCache> cache; |
617 | 617 |
618 // The vector of open entries in the backend. | 618 // The vector of open entries in the backend. |
619 Entries entries; | 619 Entries entries; |
620 | 620 |
621 // The output of the Keys function. | 621 // The output of the Keys function. |
622 scoped_ptr<ServiceWorkerCache::Requests> out_keys; | 622 scoped_ptr<ServiceWorkerCache::Requests> out_keys; |
623 | 623 |
624 // Used for enumerating cache entries. | 624 // Used for enumerating cache entries. |
625 void* backend_iterator; | 625 void* backend_iterator; |
626 disk_cache::Entry* enumerated_entry; | 626 disk_cache::Entry* enumerated_entry; |
627 }; | 627 }; |
628 | 628 |
629 // static | 629 // static |
630 scoped_ptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache( | 630 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache( |
631 net::URLRequestContext* request_context, | 631 net::URLRequestContext* request_context, |
632 base::WeakPtr<storage::BlobStorageContext> blob_context) { | 632 base::WeakPtr<storage::BlobStorageContext> blob_context) { |
633 return make_scoped_ptr( | 633 return make_scoped_refptr( |
634 new ServiceWorkerCache(base::FilePath(), request_context, blob_context)); | 634 new ServiceWorkerCache(base::FilePath(), request_context, blob_context)); |
635 } | 635 } |
636 | 636 |
637 // static | 637 // static |
638 scoped_ptr<ServiceWorkerCache> ServiceWorkerCache::CreatePersistentCache( | 638 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreatePersistentCache( |
639 const base::FilePath& path, | 639 const base::FilePath& path, |
640 net::URLRequestContext* request_context, | 640 net::URLRequestContext* request_context, |
641 base::WeakPtr<storage::BlobStorageContext> blob_context) { | 641 base::WeakPtr<storage::BlobStorageContext> blob_context) { |
642 return make_scoped_ptr( | 642 return make_scoped_refptr( |
643 new ServiceWorkerCache(path, request_context, blob_context)); | 643 new ServiceWorkerCache(path, request_context, blob_context)); |
644 } | 644 } |
645 | 645 |
646 ServiceWorkerCache::~ServiceWorkerCache() { | 646 ServiceWorkerCache::~ServiceWorkerCache() { |
647 } | 647 } |
648 | 648 |
649 base::WeakPtr<ServiceWorkerCache> ServiceWorkerCache::AsWeakPtr() { | 649 base::WeakPtr<ServiceWorkerCache> ServiceWorkerCache::AsWeakPtr() { |
650 return weak_ptr_factory_.GetWeakPtr(); | 650 return weak_ptr_factory_.GetWeakPtr(); |
651 } | 651 } |
652 | 652 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 net::CompletionCallback open_entry_callback = | 780 net::CompletionCallback open_entry_callback = |
781 base::Bind(KeysDidOpenNextEntry, base::Passed(keys_context.Pass())); | 781 base::Bind(KeysDidOpenNextEntry, base::Passed(keys_context.Pass())); |
782 | 782 |
783 int rv = backend_->OpenNextEntry( | 783 int rv = backend_->OpenNextEntry( |
784 backend_iterator, enumerated_entry, open_entry_callback); | 784 backend_iterator, enumerated_entry, open_entry_callback); |
785 | 785 |
786 if (rv != net::ERR_IO_PENDING) | 786 if (rv != net::ERR_IO_PENDING) |
787 open_entry_callback.Run(rv); | 787 open_entry_callback.Run(rv); |
788 } | 788 } |
789 | 789 |
| 790 void ServiceWorkerCache::Close() { |
| 791 backend_.reset(); |
| 792 } |
| 793 |
790 ServiceWorkerCache::ServiceWorkerCache( | 794 ServiceWorkerCache::ServiceWorkerCache( |
791 const base::FilePath& path, | 795 const base::FilePath& path, |
792 net::URLRequestContext* request_context, | 796 net::URLRequestContext* request_context, |
793 base::WeakPtr<storage::BlobStorageContext> blob_context) | 797 base::WeakPtr<storage::BlobStorageContext> blob_context) |
794 : path_(path), | 798 : path_(path), |
795 request_context_(request_context), | 799 request_context_(request_context), |
796 blob_storage_context_(blob_context), | 800 blob_storage_context_(blob_context), |
797 initialized_(false), | 801 initialized_(false), |
798 weak_ptr_factory_(this) { | 802 weak_ptr_factory_(this) { |
799 } | 803 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 return; | 846 return; |
843 } | 847 } |
844 | 848 |
845 base::WeakPtr<ServiceWorkerCache> cache = keys_context->cache; | 849 base::WeakPtr<ServiceWorkerCache> cache = keys_context->cache; |
846 if (rv < 0 || !cache) { | 850 if (rv < 0 || !cache) { |
847 keys_context->original_callback.Run(ErrorTypeStorage, | 851 keys_context->original_callback.Run(ErrorTypeStorage, |
848 scoped_ptr<Requests>()); | 852 scoped_ptr<Requests>()); |
849 return; | 853 return; |
850 } | 854 } |
851 | 855 |
| 856 if (!cache->backend_) { |
| 857 keys_context->original_callback.Run(ErrorTypeNotFound, |
| 858 scoped_ptr<Requests>()); |
| 859 return; |
| 860 } |
| 861 |
852 // Store the entry. | 862 // Store the entry. |
853 keys_context->entries.push_back(keys_context->enumerated_entry); | 863 keys_context->entries.push_back(keys_context->enumerated_entry); |
854 keys_context->enumerated_entry = NULL; | 864 keys_context->enumerated_entry = NULL; |
855 | 865 |
856 // Enumerate the next entry. | 866 // Enumerate the next entry. |
857 void** backend_iterator = &keys_context->backend_iterator; | 867 void** backend_iterator = &keys_context->backend_iterator; |
858 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; | 868 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; |
859 | 869 |
860 net::CompletionCallback open_entry_callback = | 870 net::CompletionCallback open_entry_callback = |
861 base::Bind(KeysDidOpenNextEntry, base::Passed(keys_context.Pass())); | 871 base::Bind(KeysDidOpenNextEntry, base::Passed(keys_context.Pass())); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
963 initialized_ = true; | 973 initialized_ = true; |
964 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); | 974 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); |
965 it != init_callbacks_.end(); | 975 it != init_callbacks_.end(); |
966 ++it) { | 976 ++it) { |
967 it->Run(); | 977 it->Run(); |
968 } | 978 } |
969 init_callbacks_.clear(); | 979 init_callbacks_.clear(); |
970 } | 980 } |
971 | 981 |
972 } // namespace content | 982 } // namespace content |
OLD | NEW |