| 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_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/id_map.h" | 13 #include "base/id_map.h" |
| 14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 class MessageLoopProxy; | 17 class MessageLoopProxy; |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 | 21 |
| 22 class ServiceWorkerFetchStore; | 22 class ServiceWorkerCache; |
| 23 | 23 |
| 24 // TODO(jkarlin): Constrain the total bytes used per origin. | 24 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 25 // The set of stores for a given origin. It is owned by the | 25 // The set of caches for a given origin. It is owned by the |
| 26 // ServiceWorkerFetchStoresManager. Provided callbacks are called on the | 26 // ServiceWorkerCacheStorageManager. Provided callbacks are called on the |
| 27 // |callback_loop| provided in the constructor. | 27 // |callback_loop| provided in the constructor. |
| 28 class ServiceWorkerFetchStores { | 28 class ServiceWorkerCacheStorage { |
| 29 public: | 29 public: |
| 30 enum FetchStoresError { | 30 enum CacheStorageError { |
| 31 FETCH_STORES_ERROR_NO_ERROR, | 31 CACHE_STORAGE_ERROR_NO_ERROR, |
| 32 FETCH_STORES_ERROR_NOT_IMPLEMENTED, | 32 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, |
| 33 FETCH_STORES_ERROR_NOT_FOUND, | 33 CACHE_STORAGE_ERROR_NOT_FOUND, |
| 34 FETCH_STORES_ERROR_EXISTS, | 34 CACHE_STORAGE_ERROR_EXISTS, |
| 35 FETCH_STORES_ERROR_STORAGE, | 35 CACHE_STORAGE_ERROR_STORAGE, |
| 36 FETCH_STORES_ERROR_EMPTY_KEY, | 36 CACHE_STORAGE_ERROR_EMPTY_KEY, |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 typedef base::Callback<void(bool, FetchStoresError)> BoolAndErrorCallback; | 39 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
| 40 typedef base::Callback<void(int, FetchStoresError)> StoreAndErrorCallback; | 40 typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback; |
| 41 typedef base::Callback<void(const std::vector<std::string>&, | 41 typedef base::Callback<void(const std::vector<std::string>&, |
| 42 FetchStoresError)> StringsAndErrorCallback; | 42 CacheStorageError)> StringsAndErrorCallback; |
| 43 | 43 |
| 44 ServiceWorkerFetchStores( | 44 ServiceWorkerCacheStorage( |
| 45 const base::FilePath& origin_path, | 45 const base::FilePath& origin_path, |
| 46 bool memory_only, | 46 bool memory_only, |
| 47 const scoped_refptr<base::MessageLoopProxy>& callback_loop); | 47 const scoped_refptr<base::MessageLoopProxy>& callback_loop); |
| 48 | 48 |
| 49 virtual ~ServiceWorkerFetchStores(); | 49 virtual ~ServiceWorkerCacheStorage(); |
| 50 | 50 |
| 51 // Create a ServiceWorkerFetchStore if it doesn't already exist and call the | 51 // Create a ServiceWorkerCache if it doesn't already exist and call the |
| 52 // callback with the cache's id. If it already | 52 // callback with the cache's id. If it already |
| 53 // exists the callback is called with FETCH_STORES_ERROR_EXISTS. | 53 // exists the callback is called with CACHE_STORAGE_ERROR_EXISTS. |
| 54 void CreateStore(const std::string& store_name, | 54 void CreateCache(const std::string& cache_name, |
| 55 const StoreAndErrorCallback& callback); | 55 const CacheAndErrorCallback& callback); |
| 56 | 56 |
| 57 // Get the cache id for the given key. If not found returns | 57 // Get the cache id for the given key. If not found returns |
| 58 // FETCH_STORES_ERROR_NOT_FOUND. | 58 // CACHE_STORAGE_ERROR_NOT_FOUND. |
| 59 void GetStore(const std::string& store_name, | 59 void GetCache(const std::string& cache_name, |
| 60 const StoreAndErrorCallback& callback); | 60 const CacheAndErrorCallback& callback); |
| 61 | 61 |
| 62 // Calls the callback with whether or not the cache exists. | 62 // Calls the callback with whether or not the cache exists. |
| 63 void HasStore(const std::string& store_name, | 63 void HasCache(const std::string& cache_name, |
| 64 const BoolAndErrorCallback& callback); | 64 const BoolAndErrorCallback& callback); |
| 65 | 65 |
| 66 // Deletes the cache if it exists. If it doesn't exist, | 66 // Deletes the cache if it exists. If it doesn't exist, |
| 67 // FETCH_STORES_ERROR_NOT_FOUND is returned. | 67 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. |
| 68 void DeleteStore(const std::string& store_name, | 68 void DeleteCache(const std::string& cache_name, |
| 69 const BoolAndErrorCallback& callback); | 69 const BoolAndErrorCallback& callback); |
| 70 | 70 |
| 71 // Calls the callback with a vector of cache names (keys) available. | 71 // Calls the callback with a vector of cache names (keys) available. |
| 72 void EnumerateStores(const StringsAndErrorCallback& callback); | 72 void EnumerateCaches(const StringsAndErrorCallback& callback); |
| 73 | 73 |
| 74 // TODO(jkarlin): Add match() function. | 74 // TODO(jkarlin): Add match() function. |
| 75 | 75 |
| 76 void InitializeStoreCallback(const ServiceWorkerFetchStore* store, | 76 void InitializeCacheCallback(const ServiceWorkerCache* cache, |
| 77 const StoreAndErrorCallback& callback, | 77 const CacheAndErrorCallback& callback, |
| 78 bool success); | 78 bool success); |
| 79 | 79 |
| 80 private: | 80 private: |
| 81 class MemoryLoader; | 81 class MemoryLoader; |
| 82 class SimpleCacheLoader; | 82 class SimpleCacheLoader; |
| 83 class StoresLoader; | 83 class CacheLoader; |
| 84 | 84 |
| 85 typedef IDMap<ServiceWorkerFetchStore, IDMapOwnPointer> StoreMap; | 85 typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap; |
| 86 typedef StoreMap::KeyType StoreID; | 86 typedef CacheMap::KeyType CacheID; |
| 87 typedef std::map<std::string, StoreID> NameMap; | 87 typedef std::map<std::string, CacheID> NameMap; |
| 88 | 88 |
| 89 ServiceWorkerFetchStore* GetLoadedStore(const std::string& key) const; | 89 ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const; |
| 90 | 90 |
| 91 void LazyInit(); | 91 void LazyInit(); |
| 92 void InitStore(ServiceWorkerFetchStore* store); | 92 void InitCache(ServiceWorkerCache* cache); |
| 93 | 93 |
| 94 bool initialized_; | 94 bool initialized_; |
| 95 StoreMap store_map_; | 95 CacheMap cache_map_; |
| 96 NameMap name_map_; | 96 NameMap name_map_; |
| 97 base::FilePath origin_path_; | 97 base::FilePath origin_path_; |
| 98 scoped_refptr<base::MessageLoopProxy> callback_loop_; | 98 scoped_refptr<base::MessageLoopProxy> callback_loop_; |
| 99 scoped_ptr<StoresLoader> stores_loader_; | 99 scoped_ptr<CacheLoader> cache_loader_; |
| 100 | 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchStores); | 101 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); |
| 102 }; | 102 }; |
| 103 | 103 |
| 104 } // namespace content | 104 } // namespace content |
| 105 | 105 |
| 106 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ | 106 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| OLD | NEW |