| 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 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | 36 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; |
| 37 | 37 |
| 38 // The maximum size of an individual cache. Ultimately cache size is controlled | 38 // The maximum size of an individual cache. Ultimately cache size is controlled |
| 39 // per-origin. | 39 // per-origin. |
| 40 const int kMaxCacheBytes = 512 * 1024 * 1024; | 40 const int kMaxCacheBytes = 512 * 1024 * 1024; |
| 41 | 41 |
| 42 // Buffer size for cache and blob reading/writing. | 42 // Buffer size for cache and blob reading/writing. |
| 43 const int kBufferSize = 1024 * 512; | 43 const int kBufferSize = 1024 * 512; |
| 44 | 44 |
| 45 void NotReachedCompletionCallback(int rv) { |
| 46 NOTREACHED(); |
| 47 } |
| 48 |
| 45 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType( | 49 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType( |
| 46 ServiceWorkerRequestResponseHeaders_ResponseType response_type) { | 50 ServiceWorkerRequestResponseHeaders_ResponseType response_type) { |
| 47 switch (response_type) { | 51 switch (response_type) { |
| 48 case ServiceWorkerRequestResponseHeaders_ResponseType_BASIC_TYPE: | 52 case ServiceWorkerRequestResponseHeaders_ResponseType_BASIC_TYPE: |
| 49 return blink::WebServiceWorkerResponseTypeBasic; | 53 return blink::WebServiceWorkerResponseTypeBasic; |
| 50 case ServiceWorkerRequestResponseHeaders_ResponseType_CORS_TYPE: | 54 case ServiceWorkerRequestResponseHeaders_ResponseType_CORS_TYPE: |
| 51 return blink::WebServiceWorkerResponseTypeCORS; | 55 return blink::WebServiceWorkerResponseTypeCORS; |
| 52 case ServiceWorkerRequestResponseHeaders_ResponseType_DEFAULT_TYPE: | 56 case ServiceWorkerRequestResponseHeaders_ResponseType_DEFAULT_TYPE: |
| 53 return blink::WebServiceWorkerResponseTypeDefault; | 57 return blink::WebServiceWorkerResponseTypeDefault; |
| 54 case ServiceWorkerRequestResponseHeaders_ResponseType_ERROR_TYPE: | 58 case ServiceWorkerRequestResponseHeaders_ResponseType_ERROR_TYPE: |
| (...skipping 904 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 959 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); | 963 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); |
| 960 | 964 |
| 961 if (rv != net::ERR_IO_PENDING) | 965 if (rv != net::ERR_IO_PENDING) |
| 962 open_entry_callback.Run(rv); | 966 open_entry_callback.Run(rv); |
| 963 } | 967 } |
| 964 | 968 |
| 965 void ServiceWorkerCache::Close() { | 969 void ServiceWorkerCache::Close() { |
| 966 backend_.reset(); | 970 backend_.reset(); |
| 967 } | 971 } |
| 968 | 972 |
| 973 int64 ServiceWorkerCache::MemoryBackedSize() const { |
| 974 if (!backend_ || !memory_only_) |
| 975 return 0; |
| 976 |
| 977 scoped_ptr<disk_cache::Backend::Iterator> backend_iter = |
| 978 backend_->CreateIterator(); |
| 979 disk_cache::Entry* entry = nullptr; |
| 980 |
| 981 int64 sum = 0; |
| 982 |
| 983 std::vector<disk_cache::Entry*> entries; |
| 984 int rv = net::OK; |
| 985 while ((rv = backend_iter->OpenNextEntry( |
| 986 &entry, base::Bind(NotReachedCompletionCallback))) == net::OK) { |
| 987 entries.push_back(entry); // Open the entries without mutating them. |
| 988 } |
| 989 DCHECK(rv != |
| 990 net::ERR_IO_PENDING); // Expect all memory ops to be synchronous. |
| 991 |
| 992 for (disk_cache::Entry* entry : entries) { |
| 993 sum += entry->GetDataSize(INDEX_HEADERS) + |
| 994 entry->GetDataSize(INDEX_RESPONSE_BODY); |
| 995 entry->Close(); |
| 996 } |
| 997 |
| 998 return sum; |
| 999 } |
| 1000 |
| 969 ServiceWorkerCache::ServiceWorkerCache( | 1001 ServiceWorkerCache::ServiceWorkerCache( |
| 970 const GURL& origin, | 1002 const GURL& origin, |
| 971 const base::FilePath& path, | 1003 const base::FilePath& path, |
| 972 net::URLRequestContext* request_context, | 1004 net::URLRequestContext* request_context, |
| 973 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 1005 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 974 base::WeakPtr<storage::BlobStorageContext> blob_context) | 1006 base::WeakPtr<storage::BlobStorageContext> blob_context) |
| 975 : origin_(origin), | 1007 : origin_(origin), |
| 976 path_(path), | 1008 path_(path), |
| 977 request_context_(request_context), | 1009 request_context_(request_context), |
| 978 quota_manager_proxy_(quota_manager_proxy), | 1010 quota_manager_proxy_(quota_manager_proxy), |
| 979 blob_storage_context_(blob_context), | 1011 blob_storage_context_(blob_context), |
| 980 initialized_(false), | 1012 initialized_(false), |
| 1013 memory_only_(path.empty()), |
| 981 weak_ptr_factory_(this) { | 1014 weak_ptr_factory_(this) { |
| 982 } | 1015 } |
| 983 | 1016 |
| 984 void ServiceWorkerCache::PutImpl( | 1017 void ServiceWorkerCache::PutImpl( |
| 985 scoped_ptr<ServiceWorkerFetchRequest> request, | 1018 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 986 scoped_ptr<ServiceWorkerResponse> response, | 1019 scoped_ptr<ServiceWorkerResponse> response, |
| 987 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 1020 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
| 988 const ResponseCallback& callback) { | 1021 const ResponseCallback& callback) { |
| 989 if (!backend_) { | 1022 if (!backend_) { |
| 990 callback.Run(ErrorTypeStorage, | 1023 callback.Run(ErrorTypeStorage, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1105 entry->Doom(); | 1138 entry->Doom(); |
| 1106 } | 1139 } |
| 1107 | 1140 |
| 1108 KeysProcessNextEntry(keys_context.Pass(), iter + 1); | 1141 KeysProcessNextEntry(keys_context.Pass(), iter + 1); |
| 1109 } | 1142 } |
| 1110 | 1143 |
| 1111 void ServiceWorkerCache::CreateBackend(const ErrorCallback& callback) { | 1144 void ServiceWorkerCache::CreateBackend(const ErrorCallback& callback) { |
| 1112 DCHECK(!backend_); | 1145 DCHECK(!backend_); |
| 1113 | 1146 |
| 1114 // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction. | 1147 // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction. |
| 1115 net::CacheType cache_type = | 1148 net::CacheType cache_type = memory_only_ ? net::MEMORY_CACHE : net::APP_CACHE; |
| 1116 path_.empty() ? net::MEMORY_CACHE : net::APP_CACHE; | |
| 1117 | 1149 |
| 1118 scoped_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr()); | 1150 scoped_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr()); |
| 1119 | 1151 |
| 1120 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below. | 1152 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below. |
| 1121 ScopedBackendPtr* backend = backend_ptr.get(); | 1153 ScopedBackendPtr* backend = backend_ptr.get(); |
| 1122 | 1154 |
| 1123 net::CompletionCallback create_cache_callback = | 1155 net::CompletionCallback create_cache_callback = |
| 1124 base::Bind(CreateBackendDidCreate, | 1156 base::Bind(CreateBackendDidCreate, |
| 1125 callback, | 1157 callback, |
| 1126 base::Passed(backend_ptr.Pass()), | 1158 base::Passed(backend_ptr.Pass()), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1158 initialized_ = true; | 1190 initialized_ = true; |
| 1159 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); | 1191 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); |
| 1160 it != init_callbacks_.end(); | 1192 it != init_callbacks_.end(); |
| 1161 ++it) { | 1193 ++it) { |
| 1162 it->Run(); | 1194 it->Run(); |
| 1163 } | 1195 } |
| 1164 init_callbacks_.clear(); | 1196 init_callbacks_.clear(); |
| 1165 } | 1197 } |
| 1166 | 1198 |
| 1167 } // namespace content | 1199 } // namespace content |
| OLD | NEW |