| 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 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 | 590 |
| 591 } // namespace | 591 } // namespace |
| 592 | 592 |
| 593 // The state needed to pass between ServiceWorkerCache::Keys callbacks. | 593 // The state needed to pass between ServiceWorkerCache::Keys callbacks. |
| 594 struct ServiceWorkerCache::KeysContext { | 594 struct ServiceWorkerCache::KeysContext { |
| 595 KeysContext(const ServiceWorkerCache::RequestsCallback& callback, | 595 KeysContext(const ServiceWorkerCache::RequestsCallback& callback, |
| 596 base::WeakPtr<ServiceWorkerCache> cache) | 596 base::WeakPtr<ServiceWorkerCache> cache) |
| 597 : original_callback(callback), | 597 : original_callback(callback), |
| 598 cache(cache), | 598 cache(cache), |
| 599 out_keys(new ServiceWorkerCache::Requests()), | 599 out_keys(new ServiceWorkerCache::Requests()), |
| 600 backend_iterator(NULL), | |
| 601 enumerated_entry(NULL) {} | 600 enumerated_entry(NULL) {} |
| 602 | 601 |
| 603 ~KeysContext() { | 602 ~KeysContext() { |
| 604 for (size_t i = 0, max = entries.size(); i < max; ++i) | 603 for (size_t i = 0, max = entries.size(); i < max; ++i) |
| 605 entries[i]->Close(); | 604 entries[i]->Close(); |
| 606 if (enumerated_entry) | 605 if (enumerated_entry) |
| 607 enumerated_entry->Close(); | 606 enumerated_entry->Close(); |
| 608 if (cache && backend_iterator && cache->backend_) | |
| 609 cache->backend_->EndEnumeration(&backend_iterator); | |
| 610 } | 607 } |
| 611 | 608 |
| 612 // The callback passed to the Keys() function. | 609 // The callback passed to the Keys() function. |
| 613 ServiceWorkerCache::RequestsCallback original_callback; | 610 ServiceWorkerCache::RequestsCallback original_callback; |
| 614 | 611 |
| 615 // The ServiceWorkerCache that Keys was called on. | 612 // The ServiceWorkerCache that Keys was called on. |
| 616 base::WeakPtr<ServiceWorkerCache> cache; | 613 base::WeakPtr<ServiceWorkerCache> cache; |
| 617 | 614 |
| 618 // The vector of open entries in the backend. | 615 // The vector of open entries in the backend. |
| 619 Entries entries; | 616 Entries entries; |
| 620 | 617 |
| 621 // The output of the Keys function. | 618 // The output of the Keys function. |
| 622 scoped_ptr<ServiceWorkerCache::Requests> out_keys; | 619 scoped_ptr<ServiceWorkerCache::Requests> out_keys; |
| 623 | 620 |
| 624 // Used for enumerating cache entries. | 621 // Used for enumerating cache entries. |
| 625 void* backend_iterator; | 622 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; |
| 626 disk_cache::Entry* enumerated_entry; | 623 disk_cache::Entry* enumerated_entry; |
| 627 }; | 624 }; |
| 628 | 625 |
| 629 // static | 626 // static |
| 630 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache( | 627 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache( |
| 631 net::URLRequestContext* request_context, | 628 net::URLRequestContext* request_context, |
| 632 base::WeakPtr<storage::BlobStorageContext> blob_context) { | 629 base::WeakPtr<storage::BlobStorageContext> blob_context) { |
| 633 return make_scoped_refptr( | 630 return make_scoped_refptr( |
| 634 new ServiceWorkerCache(base::FilePath(), request_context, blob_context)); | 631 new ServiceWorkerCache(base::FilePath(), request_context, blob_context)); |
| 635 } | 632 } |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 767 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). | 764 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). |
| 768 // 2.3. Push the response into a vector of requests to be returned. | 765 // 2.3. Push the response into a vector of requests to be returned. |
| 769 // 3. Return the vector of requests (keys). | 766 // 3. Return the vector of requests (keys). |
| 770 | 767 |
| 771 // The entries have to be loaded into a vector first because enumeration loops | 768 // The entries have to be loaded into a vector first because enumeration loops |
| 772 // forever if you read data from a cache entry while enumerating. | 769 // forever if you read data from a cache entry while enumerating. |
| 773 | 770 |
| 774 scoped_ptr<KeysContext> keys_context( | 771 scoped_ptr<KeysContext> keys_context( |
| 775 new KeysContext(callback, weak_ptr_factory_.GetWeakPtr())); | 772 new KeysContext(callback, weak_ptr_factory_.GetWeakPtr())); |
| 776 | 773 |
| 777 void** backend_iterator = &keys_context->backend_iterator; | 774 keys_context->backend_iterator = backend_->CreateIterator(); |
| 778 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; | 775 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; |
| 779 | 776 |
| 780 net::CompletionCallback open_entry_callback = | 777 net::CompletionCallback open_entry_callback = |
| 781 base::Bind(KeysDidOpenNextEntry, base::Passed(keys_context.Pass())); | 778 base::Bind(KeysDidOpenNextEntry, base::Passed(keys_context.Pass())); |
| 782 | 779 |
| 783 int rv = backend_->OpenNextEntry( | 780 int rv = keys_context->backend_iterator->OpenNextEntry(enumerated_entry, |
| 784 backend_iterator, enumerated_entry, open_entry_callback); | 781 open_entry_callback); |
| 785 | |
| 786 if (rv != net::ERR_IO_PENDING) | 782 if (rv != net::ERR_IO_PENDING) |
| 787 open_entry_callback.Run(rv); | 783 open_entry_callback.Run(rv); |
| 788 } | 784 } |
| 789 | 785 |
| 790 void ServiceWorkerCache::Close() { | 786 void ServiceWorkerCache::Close() { |
| 791 backend_.reset(); | 787 backend_.reset(); |
| 792 } | 788 } |
| 793 | 789 |
| 794 ServiceWorkerCache::ServiceWorkerCache( | 790 ServiceWorkerCache::ServiceWorkerCache( |
| 795 const base::FilePath& path, | 791 const base::FilePath& path, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 857 keys_context->original_callback.Run(ErrorTypeNotFound, | 853 keys_context->original_callback.Run(ErrorTypeNotFound, |
| 858 scoped_ptr<Requests>()); | 854 scoped_ptr<Requests>()); |
| 859 return; | 855 return; |
| 860 } | 856 } |
| 861 | 857 |
| 862 // Store the entry. | 858 // Store the entry. |
| 863 keys_context->entries.push_back(keys_context->enumerated_entry); | 859 keys_context->entries.push_back(keys_context->enumerated_entry); |
| 864 keys_context->enumerated_entry = NULL; | 860 keys_context->enumerated_entry = NULL; |
| 865 | 861 |
| 866 // Enumerate the next entry. | 862 // Enumerate the next entry. |
| 867 void** backend_iterator = &keys_context->backend_iterator; | |
| 868 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; | 863 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; |
| 869 | |
| 870 net::CompletionCallback open_entry_callback = | 864 net::CompletionCallback open_entry_callback = |
| 871 base::Bind(KeysDidOpenNextEntry, base::Passed(keys_context.Pass())); | 865 base::Bind(KeysDidOpenNextEntry, base::Passed(keys_context.Pass())); |
| 872 | 866 |
| 873 rv = cache->backend_->OpenNextEntry( | 867 rv = keys_context->backend_iterator->OpenNextEntry(enumerated_entry, |
| 874 backend_iterator, enumerated_entry, open_entry_callback); | 868 open_entry_callback); |
| 875 | |
| 876 if (rv != net::ERR_IO_PENDING) | 869 if (rv != net::ERR_IO_PENDING) |
| 877 open_entry_callback.Run(rv); | 870 open_entry_callback.Run(rv); |
| 878 } | 871 } |
| 879 | 872 |
| 880 // static | 873 // static |
| 881 void ServiceWorkerCache::KeysProcessNextEntry( | 874 void ServiceWorkerCache::KeysProcessNextEntry( |
| 882 scoped_ptr<KeysContext> keys_context, | 875 scoped_ptr<KeysContext> keys_context, |
| 883 const Entries::iterator& iter) { | 876 const Entries::iterator& iter) { |
| 884 if (iter == keys_context->entries.end()) { | 877 if (iter == keys_context->entries.end()) { |
| 885 // All done. Return all of the keys. | 878 // All done. Return all of the keys. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 initialized_ = true; | 966 initialized_ = true; |
| 974 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); | 967 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); |
| 975 it != init_callbacks_.end(); | 968 it != init_callbacks_.end(); |
| 976 ++it) { | 969 ++it) { |
| 977 it->Run(); | 970 it->Run(); |
| 978 } | 971 } |
| 979 init_callbacks_.clear(); | 972 init_callbacks_.clear(); |
| 980 } | 973 } |
| 981 | 974 |
| 982 } // namespace content | 975 } // namespace content |
| OLD | NEW |