| 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 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
| 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/id_map.h" | 14 #include "base/id_map.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 18 #include "content/common/cache_storage/cache_storage_types.h" | 17 #include "content/common/cache_storage/cache_storage_types.h" |
| 19 #include "content/common/service_worker/service_worker_types.h" | 18 #include "content/common/service_worker/service_worker_types.h" |
| 20 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 21 #include "net/disk_cache/disk_cache.h" | 20 #include "net/disk_cache/disk_cache.h" |
| 22 #include "storage/common/quota/quota_status_code.h" | 21 #include "storage/common/quota/quota_status_code.h" |
| 23 | 22 |
| 24 namespace net { | 23 namespace net { |
| 25 class URLRequestContextGetter; | 24 class URLRequestContextGetter; |
| 26 class IOBufferWithSize; | 25 class IOBufferWithSize; |
| 27 } | 26 } |
| 28 | 27 |
| 29 namespace storage { | 28 namespace storage { |
| 30 class BlobDataHandle; | 29 class BlobDataHandle; |
| 31 class BlobStorageContext; | 30 class BlobStorageContext; |
| 32 class QuotaManagerProxy; | 31 class QuotaManagerProxy; |
| 33 } | 32 } |
| 34 | 33 |
| 35 namespace content { | 34 namespace content { |
| 36 | 35 class CacheStorage; |
| 37 class CacheStorageBlobToDiskCache; | 36 class CacheStorageBlobToDiskCache; |
| 37 class CacheStorageCacheHandle; |
| 38 class CacheMetadata; | 38 class CacheMetadata; |
| 39 class CacheStorageScheduler; | 39 class CacheStorageScheduler; |
| 40 class TestCacheStorageCache; | 40 class TestCacheStorageCache; |
| 41 | 41 |
| 42 // Represents a ServiceWorker Cache as seen in | 42 // Represents a ServiceWorker Cache as seen in |
| 43 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/ The | 43 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/ The |
| 44 // asynchronous methods are executed serially (except for Size). Callbacks to | 44 // asynchronous methods are executed serially (except for Size). Callbacks to |
| 45 // the public functions will be called so long as the cache object lives. | 45 // the public functions will be called so long as the cache object lives. |
| 46 class CONTENT_EXPORT CacheStorageCache | 46 class CONTENT_EXPORT CacheStorageCache { |
| 47 : public base::RefCounted<CacheStorageCache> { | |
| 48 public: | 47 public: |
| 49 using ErrorCallback = base::Callback<void(CacheStorageError)>; | 48 using ErrorCallback = base::Callback<void(CacheStorageError)>; |
| 50 using ResponseCallback = | 49 using ResponseCallback = |
| 51 base::Callback<void(CacheStorageError, | 50 base::Callback<void(CacheStorageError, |
| 52 std::unique_ptr<ServiceWorkerResponse>, | 51 std::unique_ptr<ServiceWorkerResponse>, |
| 53 std::unique_ptr<storage::BlobDataHandle>)>; | 52 std::unique_ptr<storage::BlobDataHandle>)>; |
| 54 using Responses = std::vector<ServiceWorkerResponse>; | 53 using Responses = std::vector<ServiceWorkerResponse>; |
| 55 using BlobDataHandles = std::vector<storage::BlobDataHandle>; | 54 using BlobDataHandles = std::vector<storage::BlobDataHandle>; |
| 56 using ResponsesCallback = | 55 using ResponsesCallback = |
| 57 base::Callback<void(CacheStorageError, | 56 base::Callback<void(CacheStorageError, |
| 58 std::unique_ptr<Responses>, | 57 std::unique_ptr<Responses>, |
| 59 std::unique_ptr<BlobDataHandles>)>; | 58 std::unique_ptr<BlobDataHandles>)>; |
| 60 using Requests = std::vector<ServiceWorkerFetchRequest>; | 59 using Requests = std::vector<ServiceWorkerFetchRequest>; |
| 61 using RequestsCallback = | 60 using RequestsCallback = |
| 62 base::Callback<void(CacheStorageError, std::unique_ptr<Requests>)>; | 61 base::Callback<void(CacheStorageError, std::unique_ptr<Requests>)>; |
| 63 using SizeCallback = base::Callback<void(int64_t)>; | 62 using SizeCallback = base::Callback<void(int64_t)>; |
| 64 | 63 |
| 65 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA }; | 64 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA }; |
| 66 | 65 |
| 67 static scoped_refptr<CacheStorageCache> CreateMemoryCache( | 66 static std::unique_ptr<CacheStorageCache> CreateMemoryCache( |
| 68 const GURL& origin, | 67 const GURL& origin, |
| 69 const std::string& cache_name, | 68 const std::string& cache_name, |
| 69 CacheStorage* cache_storage, |
| 70 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 70 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 71 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 71 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| 72 base::WeakPtr<storage::BlobStorageContext> blob_context); | 72 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 73 static scoped_refptr<CacheStorageCache> CreatePersistentCache( | 73 static std::unique_ptr<CacheStorageCache> CreatePersistentCache( |
| 74 const GURL& origin, | 74 const GURL& origin, |
| 75 const std::string& cache_name, | 75 const std::string& cache_name, |
| 76 CacheStorage* cache_storage, |
| 76 const base::FilePath& path, | 77 const base::FilePath& path, |
| 77 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 78 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 78 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 79 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| 79 base::WeakPtr<storage::BlobStorageContext> blob_context); | 80 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 80 | 81 |
| 81 // Returns ERROR_TYPE_NOT_FOUND if not found. | 82 // Returns ERROR_TYPE_NOT_FOUND if not found. |
| 82 void Match(std::unique_ptr<ServiceWorkerFetchRequest> request, | 83 void Match(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 83 const ResponseCallback& callback); | 84 const ResponseCallback& callback); |
| 84 | 85 |
| 85 // Returns CACHE_STORAGE_OK and matched responses in this cache. If there are | 86 // Returns CACHE_STORAGE_OK and matched responses in this cache. If there are |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // operations. This is because QuotaManager is a dependency of the Put | 133 // operations. This is because QuotaManager is a dependency of the Put |
| 133 // operation and QuotaManager calls Size. If the cache isn't yet initialized, | 134 // operation and QuotaManager calls Size. If the cache isn't yet initialized, |
| 134 // runs immediately after initialization, before any pending operations in the | 135 // runs immediately after initialization, before any pending operations in the |
| 135 // scheduler are run. | 136 // scheduler are run. |
| 136 void Size(const SizeCallback& callback); | 137 void Size(const SizeCallback& callback); |
| 137 | 138 |
| 138 // Gets the cache's size, closes the backend, and then runs |callback| with | 139 // Gets the cache's size, closes the backend, and then runs |callback| with |
| 139 // the cache's size. | 140 // the cache's size. |
| 140 void GetSizeThenClose(const SizeCallback& callback); | 141 void GetSizeThenClose(const SizeCallback& callback); |
| 141 | 142 |
| 143 // Async operations in progress will cancel and not run their callbacks. |
| 144 virtual ~CacheStorageCache(); |
| 145 |
| 142 base::FilePath path() const { return path_; } | 146 base::FilePath path() const { return path_; } |
| 143 | 147 |
| 148 std::string cache_name() const { return cache_name_; } |
| 149 |
| 144 base::WeakPtr<CacheStorageCache> AsWeakPtr(); | 150 base::WeakPtr<CacheStorageCache> AsWeakPtr(); |
| 145 | 151 |
| 146 private: | 152 private: |
| 147 friend class base::RefCounted<CacheStorageCache>; | 153 friend class base::RefCounted<CacheStorageCache>; |
| 148 friend class TestCacheStorageCache; | 154 friend class TestCacheStorageCache; |
| 149 | 155 |
| 150 struct OpenAllEntriesContext; | 156 struct OpenAllEntriesContext; |
| 151 struct MatchAllContext; | 157 struct MatchAllContext; |
| 152 struct KeysContext; | 158 struct KeysContext; |
| 153 struct PutContext; | 159 struct PutContext; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 165 using BlobToDiskCacheIDMap = | 171 using BlobToDiskCacheIDMap = |
| 166 IDMap<CacheStorageBlobToDiskCache, IDMapOwnPointer>; | 172 IDMap<CacheStorageBlobToDiskCache, IDMapOwnPointer>; |
| 167 using OpenAllEntriesCallback = | 173 using OpenAllEntriesCallback = |
| 168 base::Callback<void(std::unique_ptr<OpenAllEntriesContext>, | 174 base::Callback<void(std::unique_ptr<OpenAllEntriesContext>, |
| 169 CacheStorageError)>; | 175 CacheStorageError)>; |
| 170 | 176 |
| 171 CacheStorageCache( | 177 CacheStorageCache( |
| 172 const GURL& origin, | 178 const GURL& origin, |
| 173 const std::string& cache_name, | 179 const std::string& cache_name, |
| 174 const base::FilePath& path, | 180 const base::FilePath& path, |
| 181 CacheStorage* cache_storage, |
| 175 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 182 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 176 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 183 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| 177 base::WeakPtr<storage::BlobStorageContext> blob_context); | 184 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 178 | 185 |
| 179 // Async operations in progress will cancel and not run their callbacks. | |
| 180 virtual ~CacheStorageCache(); | |
| 181 | |
| 182 // Returns true if the backend is ready to operate. | 186 // Returns true if the backend is ready to operate. |
| 183 bool LazyInitialize(); | 187 bool LazyInitialize(); |
| 184 | 188 |
| 185 // Returns all entries in this cache. | 189 // Returns all entries in this cache. |
| 186 void OpenAllEntries(const OpenAllEntriesCallback& callback); | 190 void OpenAllEntries(const OpenAllEntriesCallback& callback); |
| 187 void DidOpenNextEntry(std::unique_ptr<OpenAllEntriesContext> entries_context, | 191 void DidOpenNextEntry(std::unique_ptr<OpenAllEntriesContext> entries_context, |
| 188 const OpenAllEntriesCallback& callback, | 192 const OpenAllEntriesCallback& callback, |
| 189 int rv); | 193 int rv); |
| 190 | 194 |
| 191 // Match callbacks | 195 // Match callbacks |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 void PutDidWriteHeaders(std::unique_ptr<PutContext> put_context, | 265 void PutDidWriteHeaders(std::unique_ptr<PutContext> put_context, |
| 262 int expected_bytes, | 266 int expected_bytes, |
| 263 int rv); | 267 int rv); |
| 264 void PutDidWriteBlobToCache(std::unique_ptr<PutContext> put_context, | 268 void PutDidWriteBlobToCache(std::unique_ptr<PutContext> put_context, |
| 265 BlobToDiskCacheIDMap::KeyType blob_to_cache_key, | 269 BlobToDiskCacheIDMap::KeyType blob_to_cache_key, |
| 266 disk_cache::ScopedEntryPtr entry, | 270 disk_cache::ScopedEntryPtr entry, |
| 267 bool success); | 271 bool success); |
| 268 | 272 |
| 269 // Asynchronously calculates the current cache size, notifies the quota | 273 // Asynchronously calculates the current cache size, notifies the quota |
| 270 // manager of any change from the last report, and sets cache_size_ to the new | 274 // manager of any change from the last report, and sets cache_size_ to the new |
| 271 // size. Runs |callback| once complete. | 275 // size. |
| 272 void UpdateCacheSize(); | 276 void UpdateCacheSize(); |
| 273 void UpdateCacheSizeGotSize(int current_cache_size); | 277 void UpdateCacheSizeGotSize(std::unique_ptr<CacheStorageCacheHandle>, |
| 278 int current_cache_size); |
| 274 | 279 |
| 275 // Returns ERROR_NOT_FOUND if not found. Otherwise deletes and returns OK. | 280 // Returns ERROR_NOT_FOUND if not found. Otherwise deletes and returns OK. |
| 276 void Delete(const CacheStorageBatchOperation& operation, | 281 void Delete(const CacheStorageBatchOperation& operation, |
| 277 const ErrorCallback& callback); | 282 const ErrorCallback& callback); |
| 278 void DeleteImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, | 283 void DeleteImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 279 const CacheStorageCacheQueryParams& match_params, | 284 const CacheStorageCacheQueryParams& match_params, |
| 280 const ErrorCallback& callback); | 285 const ErrorCallback& callback); |
| 281 void DeleteDidOpenAllEntries( | 286 void DeleteDidOpenAllEntries( |
| 282 std::unique_ptr<ServiceWorkerFetchRequest> request, | 287 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 283 const ErrorCallback& callback, | 288 const ErrorCallback& callback, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 CacheStorageError error, | 341 CacheStorageError error, |
| 337 std::unique_ptr<Requests> requests); | 342 std::unique_ptr<Requests> requests); |
| 338 void PendingSizeCallback(const SizeCallback& callback, int64_t size); | 343 void PendingSizeCallback(const SizeCallback& callback, int64_t size); |
| 339 | 344 |
| 340 void PopulateResponseMetadata(const CacheMetadata& metadata, | 345 void PopulateResponseMetadata(const CacheMetadata& metadata, |
| 341 ServiceWorkerResponse* response); | 346 ServiceWorkerResponse* response); |
| 342 std::unique_ptr<storage::BlobDataHandle> PopulateResponseBody( | 347 std::unique_ptr<storage::BlobDataHandle> PopulateResponseBody( |
| 343 disk_cache::ScopedEntryPtr entry, | 348 disk_cache::ScopedEntryPtr entry, |
| 344 ServiceWorkerResponse* response); | 349 ServiceWorkerResponse* response); |
| 345 | 350 |
| 351 // Virtual for testing. |
| 352 virtual std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle(); |
| 353 |
| 346 // Be sure to check |backend_state_| before use. | 354 // Be sure to check |backend_state_| before use. |
| 347 std::unique_ptr<disk_cache::Backend> backend_; | 355 std::unique_ptr<disk_cache::Backend> backend_; |
| 348 | 356 |
| 349 GURL origin_; | 357 GURL origin_; |
| 350 const std::string cache_name_; | 358 const std::string cache_name_; |
| 351 base::FilePath path_; | 359 base::FilePath path_; |
| 360 |
| 361 // Raw pointer is safe because CacheStorage owns this object. |
| 362 CacheStorage* cache_storage_; |
| 363 |
| 352 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 364 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 353 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 365 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
| 354 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 366 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
| 355 BackendState backend_state_ = BACKEND_UNINITIALIZED; | 367 BackendState backend_state_ = BACKEND_UNINITIALIZED; |
| 356 std::unique_ptr<CacheStorageScheduler> scheduler_; | 368 std::unique_ptr<CacheStorageScheduler> scheduler_; |
| 357 std::vector<SizeCallback> pending_size_callbacks_; | 369 std::vector<SizeCallback> pending_size_callbacks_; |
| 358 bool initializing_ = false; | 370 bool initializing_ = false; |
| 359 int64_t cache_size_ = 0; | 371 int64_t cache_size_ = 0; |
| 360 | 372 |
| 361 // Owns the elements of the list | 373 // Owns the elements of the list |
| 362 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_; | 374 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_; |
| 363 | 375 |
| 364 // Whether or not to store data in disk or memory. | 376 // Whether or not to store data in disk or memory. |
| 365 bool memory_only_; | 377 bool memory_only_; |
| 366 | 378 |
| 367 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; | 379 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; |
| 368 | 380 |
| 369 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); | 381 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); |
| 370 }; | 382 }; |
| 371 | 383 |
| 372 } // namespace content | 384 } // namespace content |
| 373 | 385 |
| 374 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 386 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
| OLD | NEW |