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 942 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
953 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); | 953 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); |
954 | 954 |
955 if (rv != net::ERR_IO_PENDING) | 955 if (rv != net::ERR_IO_PENDING) |
956 open_entry_callback.Run(rv); | 956 open_entry_callback.Run(rv); |
957 } | 957 } |
958 | 958 |
959 void ServiceWorkerCache::Close() { | 959 void ServiceWorkerCache::Close() { |
960 backend_.reset(); | 960 backend_.reset(); |
961 } | 961 } |
962 | 962 |
963 void NotReachedCompletionCallback(int rv) { | |
falken
2014/10/24 03:14:15
can you move this helper into the nameless namespa
jkarlin
2014/10/24 11:53:48
Done. Thanks for catching that!
| |
964 NOTREACHED(); | |
965 } | |
966 | |
967 int64 ServiceWorkerCache::MemoryBackedSize() const { | |
968 if (!backend_ || !memory_only_) | |
969 return 0; | |
970 | |
971 scoped_ptr<disk_cache::Backend::Iterator> backend_iter = | |
972 backend_->CreateIterator(); | |
973 disk_cache::Entry* entry = NULL; | |
falken
2014/10/24 02:24:34
nullptr
jkarlin
2014/10/24 11:53:49
Done.
| |
974 | |
975 int64 sum = 0; | |
976 | |
977 std::vector<disk_cache::Entry*> entries; | |
978 int rv = net::OK; | |
979 while ((rv = backend_iter->OpenNextEntry( | |
980 &entry, base::Bind(NotReachedCompletionCallback))) == net::OK) { | |
981 entries.push_back(entry); // Open the entries without mutating them. | |
falken
2014/10/24 02:24:34
can you just add the entry's size here instead of
jkarlin
2014/10/24 11:53:49
Sadly, no. Getting the size mutates the ordering o
| |
982 } | |
983 DCHECK(rv != | |
984 net::ERR_IO_PENDING); // Expect all memory ops to be synchronous. | |
985 | |
986 for (disk_cache::Entry* entry : entries) { | |
987 sum += entry->GetDataSize(INDEX_HEADERS) + | |
988 entry->GetDataSize(INDEX_RESPONSE_BODY); | |
989 entry->Close(); | |
990 } | |
991 | |
992 return sum; | |
993 } | |
994 | |
963 ServiceWorkerCache::ServiceWorkerCache( | 995 ServiceWorkerCache::ServiceWorkerCache( |
964 const GURL& origin, | 996 const GURL& origin, |
965 const base::FilePath& path, | 997 const base::FilePath& path, |
966 net::URLRequestContext* request_context, | 998 net::URLRequestContext* request_context, |
967 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 999 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
968 base::WeakPtr<storage::BlobStorageContext> blob_context) | 1000 base::WeakPtr<storage::BlobStorageContext> blob_context) |
969 : origin_(origin), | 1001 : origin_(origin), |
970 path_(path), | 1002 path_(path), |
971 request_context_(request_context), | 1003 request_context_(request_context), |
972 quota_manager_proxy_(quota_manager_proxy), | 1004 quota_manager_proxy_(quota_manager_proxy), |
973 blob_storage_context_(blob_context), | 1005 blob_storage_context_(blob_context), |
974 initialized_(false), | 1006 initialized_(false), |
1007 memory_only_(path.empty()), | |
975 weak_ptr_factory_(this) { | 1008 weak_ptr_factory_(this) { |
976 } | 1009 } |
977 | 1010 |
978 void ServiceWorkerCache::PutImpl( | 1011 void ServiceWorkerCache::PutImpl( |
979 scoped_ptr<ServiceWorkerFetchRequest> request, | 1012 scoped_ptr<ServiceWorkerFetchRequest> request, |
980 scoped_ptr<ServiceWorkerResponse> response, | 1013 scoped_ptr<ServiceWorkerResponse> response, |
981 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 1014 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
982 const ResponseCallback& callback) { | 1015 const ResponseCallback& callback) { |
983 if (!backend_) { | 1016 if (!backend_) { |
984 callback.Run(ErrorTypeStorage, | 1017 callback.Run(ErrorTypeStorage, |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1099 entry->Doom(); | 1132 entry->Doom(); |
1100 } | 1133 } |
1101 | 1134 |
1102 KeysProcessNextEntry(keys_context.Pass(), iter + 1); | 1135 KeysProcessNextEntry(keys_context.Pass(), iter + 1); |
1103 } | 1136 } |
1104 | 1137 |
1105 void ServiceWorkerCache::CreateBackend(const ErrorCallback& callback) { | 1138 void ServiceWorkerCache::CreateBackend(const ErrorCallback& callback) { |
1106 DCHECK(!backend_); | 1139 DCHECK(!backend_); |
1107 | 1140 |
1108 // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction. | 1141 // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction. |
1109 net::CacheType cache_type = | 1142 net::CacheType cache_type = memory_only_ ? net::MEMORY_CACHE : net::APP_CACHE; |
1110 path_.empty() ? net::MEMORY_CACHE : net::APP_CACHE; | |
1111 | 1143 |
1112 scoped_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr()); | 1144 scoped_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr()); |
1113 | 1145 |
1114 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below. | 1146 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below. |
1115 ScopedBackendPtr* backend = backend_ptr.get(); | 1147 ScopedBackendPtr* backend = backend_ptr.get(); |
1116 | 1148 |
1117 net::CompletionCallback create_cache_callback = | 1149 net::CompletionCallback create_cache_callback = |
1118 base::Bind(CreateBackendDidCreate, | 1150 base::Bind(CreateBackendDidCreate, |
1119 callback, | 1151 callback, |
1120 base::Passed(backend_ptr.Pass()), | 1152 base::Passed(backend_ptr.Pass()), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1152 initialized_ = true; | 1184 initialized_ = true; |
1153 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); | 1185 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); |
1154 it != init_callbacks_.end(); | 1186 it != init_callbacks_.end(); |
1155 ++it) { | 1187 ++it) { |
1156 it->Run(); | 1188 it->Run(); |
1157 } | 1189 } |
1158 init_callbacks_.clear(); | 1190 init_callbacks_.clear(); |
1159 } | 1191 } |
1160 | 1192 |
1161 } // namespace content | 1193 } // namespace content |
OLD | NEW |