| 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_CACHE_STORAGE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_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/memory/weak_ptr.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 class MessageLoopProxy; | 17 class SequencedTaskRunner; |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 | |
| 22 class ServiceWorkerCache; | 21 class ServiceWorkerCache; |
| 23 | 22 |
| 24 // TODO(jkarlin): Constrain the total bytes used per origin. | 23 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 25 // The set of caches for a given origin. It is owned by the | 24 |
| 26 // ServiceWorkerCacheStorageManager. Provided callbacks are called on the | 25 // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is |
| 27 // |callback_loop| provided in the constructor. | 26 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run |
| 27 // on the IO thread. |
| 28 class ServiceWorkerCacheStorage { | 28 class ServiceWorkerCacheStorage { |
| 29 public: | 29 public: |
| 30 enum CacheStorageError { | 30 enum CacheStorageError { |
| 31 CACHE_STORAGE_ERROR_NO_ERROR, | 31 CACHE_STORAGE_ERROR_NO_ERROR, |
| 32 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, | 32 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, |
| 33 CACHE_STORAGE_ERROR_NOT_FOUND, | 33 CACHE_STORAGE_ERROR_NOT_FOUND, |
| 34 CACHE_STORAGE_ERROR_EXISTS, | 34 CACHE_STORAGE_ERROR_EXISTS, |
| 35 CACHE_STORAGE_ERROR_STORAGE, | 35 CACHE_STORAGE_ERROR_STORAGE, |
| 36 CACHE_STORAGE_ERROR_EMPTY_KEY, | 36 CACHE_STORAGE_ERROR_EMPTY_KEY, |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 39 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
| 40 typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback; | 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 CacheStorageError)> StringsAndErrorCallback; | 42 CacheStorageError)> StringsAndErrorCallback; |
| 43 | 43 |
| 44 ServiceWorkerCacheStorage( | 44 ServiceWorkerCacheStorage(const base::FilePath& origin_path, |
| 45 const base::FilePath& origin_path, | 45 bool memory_only, |
| 46 bool memory_only, | 46 base::SequencedTaskRunner* cache_task_runner); |
| 47 const scoped_refptr<base::MessageLoopProxy>& callback_loop); | |
| 48 | 47 |
| 49 virtual ~ServiceWorkerCacheStorage(); | 48 virtual ~ServiceWorkerCacheStorage(); |
| 50 | 49 |
| 51 // Create a ServiceWorkerCache if it doesn't already exist and call the | 50 // Create a ServiceWorkerCache if it doesn't already exist and call the |
| 52 // callback with the cache's id. If it already | 51 // callback with the cache's id. If it already |
| 53 // exists the callback is called with CACHE_STORAGE_ERROR_EXISTS. | 52 // exists the callback is called with CACHE_STORAGE_ERROR_EXISTS. |
| 54 void CreateCache(const std::string& cache_name, | 53 void CreateCache(const std::string& cache_name, |
| 55 const CacheAndErrorCallback& callback); | 54 const CacheAndErrorCallback& callback); |
| 56 | 55 |
| 57 // Get the cache id for the given key. If not found returns | 56 // Get the cache id for the given key. If not found returns |
| 58 // CACHE_STORAGE_ERROR_NOT_FOUND. | 57 // CACHE_STORAGE_ERROR_NOT_FOUND. |
| 59 void GetCache(const std::string& cache_name, | 58 void GetCache(const std::string& cache_name, |
| 60 const CacheAndErrorCallback& callback); | 59 const CacheAndErrorCallback& callback); |
| 61 | 60 |
| 62 // Calls the callback with whether or not the cache exists. | 61 // Calls the callback with whether or not the cache exists. |
| 63 void HasCache(const std::string& cache_name, | 62 void HasCache(const std::string& cache_name, |
| 64 const BoolAndErrorCallback& callback); | 63 const BoolAndErrorCallback& callback); |
| 65 | 64 |
| 66 // Deletes the cache if it exists. If it doesn't exist, | 65 // Deletes the cache if it exists. If it doesn't exist, |
| 67 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. | 66 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. |
| 68 void DeleteCache(const std::string& cache_name, | 67 void DeleteCache(const std::string& cache_name, |
| 69 const BoolAndErrorCallback& callback); | 68 const BoolAndErrorCallback& callback); |
| 70 | 69 |
| 71 // Calls the callback with a vector of cache names (keys) available. | 70 // Calls the callback with a vector of cache names (keys) available. |
| 72 void EnumerateCaches(const StringsAndErrorCallback& callback); | 71 void EnumerateCaches(const StringsAndErrorCallback& callback); |
| 73 | 72 |
| 74 // TODO(jkarlin): Add match() function. | 73 // TODO(jkarlin): Add match() function. |
| 75 | 74 |
| 76 void InitializeCacheCallback(const ServiceWorkerCache* cache, | |
| 77 const CacheAndErrorCallback& callback, | |
| 78 bool success); | |
| 79 | |
| 80 private: | 75 private: |
| 81 class MemoryLoader; | 76 class MemoryLoader; |
| 82 class SimpleCacheLoader; | 77 class SimpleCacheLoader; |
| 83 class CacheLoader; | 78 class CacheLoader; |
| 84 | 79 |
| 85 typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap; | 80 typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap; |
| 86 typedef CacheMap::KeyType CacheID; | 81 typedef CacheMap::KeyType CacheID; |
| 87 typedef std::map<std::string, CacheID> NameMap; | 82 typedef std::map<std::string, CacheID> NameMap; |
| 88 | 83 |
| 89 ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const; | 84 ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const; |
| 90 | 85 |
| 91 void LazyInit(); | 86 // Initializer and its callback are below. |
| 92 void InitCache(ServiceWorkerCache* cache); | 87 void LazyInit(const base::Closure& closure); |
| 88 void LazyInitDidLoadIndex( |
| 89 const base::Closure& callback, |
| 90 scoped_ptr<std::vector<std::string> > indexed_cache_names); |
| 91 void LazyInitIterateAndLoadCacheName( |
| 92 const base::Closure& callback, |
| 93 scoped_ptr<std::vector<std::string> > indexed_cache_names, |
| 94 const std::vector<std::string>::const_iterator& iter, |
| 95 scoped_ptr<ServiceWorkerCache> cache); |
| 96 void LazyInitDone(); |
| 93 | 97 |
| 98 void DidCreateBackend(base::WeakPtr<ServiceWorkerCache> cache, |
| 99 const CacheAndErrorCallback& callback, |
| 100 bool success); |
| 101 |
| 102 void AddCacheToMaps(scoped_ptr<ServiceWorkerCache> cache); |
| 103 |
| 104 // The CreateCache callbacks are below. |
| 105 void CreateCacheDidCreateCache(const std::string& cache_name, |
| 106 const CacheAndErrorCallback& callback, |
| 107 scoped_ptr<ServiceWorkerCache> cache); |
| 108 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, |
| 109 base::WeakPtr<ServiceWorkerCache> cache, |
| 110 bool success); |
| 111 |
| 112 // The DeleteCache callbacks are below. |
| 113 void DeleteCacheDidWriteIndex(const std::string& cache_name, |
| 114 const BoolAndErrorCallback& callback, |
| 115 bool success); |
| 116 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, |
| 117 bool success); |
| 118 |
| 119 // Whether or not we've loaded the list of cache names into memory. |
| 94 bool initialized_; | 120 bool initialized_; |
| 121 |
| 122 // The list of operations waiting on initialization. |
| 123 std::vector<base::Closure> init_callbacks_; |
| 124 |
| 125 // The map of ServiceWorkerCache objects to their integer ids that |
| 126 // ServiceWorkers reference. Owns the cache objects. |
| 95 CacheMap cache_map_; | 127 CacheMap cache_map_; |
| 128 |
| 129 // The map of cache names to their integer ids. |
| 96 NameMap name_map_; | 130 NameMap name_map_; |
| 131 |
| 132 // The file path for this CacheStorage. |
| 97 base::FilePath origin_path_; | 133 base::FilePath origin_path_; |
| 98 scoped_refptr<base::MessageLoopProxy> callback_loop_; | 134 |
| 99 scoped_ptr<CacheLoader> cache_loader_; | 135 // The TaskRunner to run file IO on. |
| 136 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 137 |
| 138 // Performs backend specific operations (memory vs disk). |
| 139 scoped_refptr<CacheLoader> cache_loader_; |
| 140 |
| 141 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; |
| 100 | 142 |
| 101 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); | 143 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); |
| 102 }; | 144 }; |
| 103 | 145 |
| 104 } // namespace content | 146 } // namespace content |
| 105 | 147 |
| 106 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 148 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| OLD | NEW |