| 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. Callbacks to the public functions | 44 // asynchronous methods are executed serially. Callbacks to the public functions |
| 45 // will be called so long as the cache object lives. | 45 // 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 // will exit early. Close should only be called once per CacheStorageCache. | 136 // will exit early. Close should only be called once per CacheStorageCache. |
| 136 void Close(const base::Closure& callback); | 137 void Close(const base::Closure& callback); |
| 137 | 138 |
| 138 // The size of the cache's contents. | 139 // The size of the cache's contents. |
| 139 void Size(const SizeCallback& callback); | 140 void Size(const SizeCallback& callback); |
| 140 | 141 |
| 141 // Gets the cache's size, closes the backend, and then runs |callback| with | 142 // Gets the cache's size, closes the backend, and then runs |callback| with |
| 142 // the cache's size. | 143 // the cache's size. |
| 143 void GetSizeThenClose(const SizeCallback& callback); | 144 void GetSizeThenClose(const SizeCallback& callback); |
| 144 | 145 |
| 146 // Async operations in progress will cancel and not run their callbacks. |
| 147 virtual ~CacheStorageCache(); |
| 148 |
| 145 base::FilePath path() const { return path_; } | 149 base::FilePath path() const { return path_; } |
| 146 | 150 |
| 151 std::string cache_name() const { return cache_name_; } |
| 152 |
| 147 base::WeakPtr<CacheStorageCache> AsWeakPtr(); | 153 base::WeakPtr<CacheStorageCache> AsWeakPtr(); |
| 148 | 154 |
| 149 private: | 155 private: |
| 150 friend class base::RefCounted<CacheStorageCache>; | 156 friend class base::RefCounted<CacheStorageCache>; |
| 151 friend class TestCacheStorageCache; | 157 friend class TestCacheStorageCache; |
| 152 | 158 |
| 153 struct OpenAllEntriesContext; | 159 struct OpenAllEntriesContext; |
| 154 struct MatchAllContext; | 160 struct MatchAllContext; |
| 155 struct KeysContext; | 161 struct KeysContext; |
| 156 struct PutContext; | 162 struct PutContext; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 168 using BlobToDiskCacheIDMap = | 174 using BlobToDiskCacheIDMap = |
| 169 IDMap<CacheStorageBlobToDiskCache, IDMapOwnPointer>; | 175 IDMap<CacheStorageBlobToDiskCache, IDMapOwnPointer>; |
| 170 using OpenAllEntriesCallback = | 176 using OpenAllEntriesCallback = |
| 171 base::Callback<void(std::unique_ptr<OpenAllEntriesContext>, | 177 base::Callback<void(std::unique_ptr<OpenAllEntriesContext>, |
| 172 CacheStorageError)>; | 178 CacheStorageError)>; |
| 173 | 179 |
| 174 CacheStorageCache( | 180 CacheStorageCache( |
| 175 const GURL& origin, | 181 const GURL& origin, |
| 176 const std::string& cache_name, | 182 const std::string& cache_name, |
| 177 const base::FilePath& path, | 183 const base::FilePath& path, |
| 184 CacheStorage* cache_storage, |
| 178 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 185 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 179 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 186 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| 180 base::WeakPtr<storage::BlobStorageContext> blob_context); | 187 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 181 | 188 |
| 182 // Async operations in progress will cancel and not run their callbacks. | |
| 183 virtual ~CacheStorageCache(); | |
| 184 | |
| 185 // Returns true if the backend is ready to operate. | 189 // Returns true if the backend is ready to operate. |
| 186 bool LazyInitialize(); | 190 bool LazyInitialize(); |
| 187 | 191 |
| 188 // Returns all entries in this cache. | 192 // Returns all entries in this cache. |
| 189 void OpenAllEntries(const OpenAllEntriesCallback& callback); | 193 void OpenAllEntries(const OpenAllEntriesCallback& callback); |
| 190 void DidOpenNextEntry(std::unique_ptr<OpenAllEntriesContext> entries_context, | 194 void DidOpenNextEntry(std::unique_ptr<OpenAllEntriesContext> entries_context, |
| 191 const OpenAllEntriesCallback& callback, | 195 const OpenAllEntriesCallback& callback, |
| 192 int rv); | 196 int rv); |
| 193 | 197 |
| 194 // Match callbacks | 198 // Match callbacks |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 void PutDidWriteHeaders(std::unique_ptr<PutContext> put_context, | 277 void PutDidWriteHeaders(std::unique_ptr<PutContext> put_context, |
| 274 int expected_bytes, | 278 int expected_bytes, |
| 275 int rv); | 279 int rv); |
| 276 void PutDidWriteBlobToCache(std::unique_ptr<PutContext> put_context, | 280 void PutDidWriteBlobToCache(std::unique_ptr<PutContext> put_context, |
| 277 BlobToDiskCacheIDMap::KeyType blob_to_cache_key, | 281 BlobToDiskCacheIDMap::KeyType blob_to_cache_key, |
| 278 disk_cache::ScopedEntryPtr entry, | 282 disk_cache::ScopedEntryPtr entry, |
| 279 bool success); | 283 bool success); |
| 280 | 284 |
| 281 // Asynchronously calculates the current cache size, notifies the quota | 285 // Asynchronously calculates the current cache size, notifies the quota |
| 282 // manager of any change from the last report, and sets cache_size_ to the new | 286 // manager of any change from the last report, and sets cache_size_ to the new |
| 283 // size. Runs |callback| once complete. | 287 // size. |
| 284 void UpdateCacheSize(); | 288 void UpdateCacheSize(); |
| 285 void UpdateCacheSizeGotSize(int current_cache_size); | 289 void UpdateCacheSizeGotSize(std::unique_ptr<CacheStorageCacheHandle>, |
| 290 int current_cache_size); |
| 286 | 291 |
| 287 // Returns ERROR_NOT_FOUND if not found. Otherwise deletes and returns OK. | 292 // Returns ERROR_NOT_FOUND if not found. Otherwise deletes and returns OK. |
| 288 void Delete(const CacheStorageBatchOperation& operation, | 293 void Delete(const CacheStorageBatchOperation& operation, |
| 289 const ErrorCallback& callback); | 294 const ErrorCallback& callback); |
| 290 void DeleteImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, | 295 void DeleteImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 291 const CacheStorageCacheQueryParams& match_params, | 296 const CacheStorageCacheQueryParams& match_params, |
| 292 const ErrorCallback& callback); | 297 const ErrorCallback& callback); |
| 293 void DeleteDidOpenAllEntries( | 298 void DeleteDidOpenAllEntries( |
| 294 std::unique_ptr<ServiceWorkerFetchRequest> request, | 299 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 295 const ErrorCallback& callback, | 300 const ErrorCallback& callback, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 CacheStorageError error, | 353 CacheStorageError error, |
| 349 std::unique_ptr<Requests> requests); | 354 std::unique_ptr<Requests> requests); |
| 350 void PendingSizeCallback(const SizeCallback& callback, int64_t size); | 355 void PendingSizeCallback(const SizeCallback& callback, int64_t size); |
| 351 | 356 |
| 352 void PopulateResponseMetadata(const CacheMetadata& metadata, | 357 void PopulateResponseMetadata(const CacheMetadata& metadata, |
| 353 ServiceWorkerResponse* response); | 358 ServiceWorkerResponse* response); |
| 354 std::unique_ptr<storage::BlobDataHandle> PopulateResponseBody( | 359 std::unique_ptr<storage::BlobDataHandle> PopulateResponseBody( |
| 355 disk_cache::ScopedEntryPtr entry, | 360 disk_cache::ScopedEntryPtr entry, |
| 356 ServiceWorkerResponse* response); | 361 ServiceWorkerResponse* response); |
| 357 | 362 |
| 363 // Virtual for testing. |
| 364 virtual std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle(); |
| 365 |
| 358 // Be sure to check |backend_state_| before use. | 366 // Be sure to check |backend_state_| before use. |
| 359 std::unique_ptr<disk_cache::Backend> backend_; | 367 std::unique_ptr<disk_cache::Backend> backend_; |
| 360 | 368 |
| 361 GURL origin_; | 369 GURL origin_; |
| 362 const std::string cache_name_; | 370 const std::string cache_name_; |
| 363 base::FilePath path_; | 371 base::FilePath path_; |
| 372 |
| 373 // Raw pointer is safe because CacheStorage owns this object. |
| 374 CacheStorage* cache_storage_; |
| 375 |
| 364 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 376 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 365 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 377 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
| 366 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 378 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
| 367 BackendState backend_state_ = BACKEND_UNINITIALIZED; | 379 BackendState backend_state_ = BACKEND_UNINITIALIZED; |
| 368 std::unique_ptr<CacheStorageScheduler> scheduler_; | 380 std::unique_ptr<CacheStorageScheduler> scheduler_; |
| 369 bool initializing_ = false; | 381 bool initializing_ = false; |
| 370 int64_t cache_size_ = 0; | 382 int64_t cache_size_ = 0; |
| 371 | 383 |
| 372 // Owns the elements of the list | 384 // Owns the elements of the list |
| 373 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_; | 385 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_; |
| 374 | 386 |
| 375 // Whether or not to store data in disk or memory. | 387 // Whether or not to store data in disk or memory. |
| 376 bool memory_only_; | 388 bool memory_only_; |
| 377 | 389 |
| 378 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; | 390 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; |
| 379 | 391 |
| 380 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); | 392 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); |
| 381 }; | 393 }; |
| 382 | 394 |
| 383 } // namespace content | 395 } // namespace content |
| 384 | 396 |
| 385 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 397 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
| OLD | NEW |