| 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" | |
| 14 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 15 #include "content/browser/service_worker/service_worker_cache.h" | 14 #include "content/browser/service_worker/service_worker_cache.h" |
| 16 | 15 |
| 17 namespace base { | 16 namespace base { |
| 18 class SequencedTaskRunner; | 17 class SequencedTaskRunner; |
| 19 } | 18 } |
| 20 | 19 |
| 21 namespace net { | 20 namespace net { |
| 22 class URLRequestContext; | 21 class URLRequestContext; |
| 23 } | 22 } |
| 24 | 23 |
| 25 namespace storage { | 24 namespace storage { |
| 26 class BlobStorageContext; | 25 class BlobStorageContext; |
| 27 } | 26 } |
| 28 | 27 |
| 29 namespace content { | 28 namespace content { |
| 30 | 29 |
| 31 // TODO(jkarlin): Constrain the total bytes used per origin. | 30 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 32 | 31 |
| 33 // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is | 32 // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is |
| 34 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run | 33 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run |
| 35 // on the IO thread. | 34 // on the IO thread. |
| 36 class ServiceWorkerCacheStorage { | 35 class CONTENT_EXPORT ServiceWorkerCacheStorage { |
| 37 public: | 36 public: |
| 37 // TODO(jkarlin): Convert this (and everything that uses it) to int64_t so |
| 38 // that we don't run out of id space. |
| 39 typedef int32_t CacheID; |
| 40 |
| 41 // The CacheID returned on failed cache operations. |
| 42 const static int kInvalidCacheID; |
| 43 |
| 38 enum CacheStorageError { | 44 enum CacheStorageError { |
| 39 CACHE_STORAGE_ERROR_NO_ERROR, | 45 CACHE_STORAGE_ERROR_NO_ERROR, |
| 40 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, | 46 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, |
| 41 CACHE_STORAGE_ERROR_NOT_FOUND, | 47 CACHE_STORAGE_ERROR_NOT_FOUND, |
| 42 CACHE_STORAGE_ERROR_EXISTS, | 48 CACHE_STORAGE_ERROR_EXISTS, |
| 43 CACHE_STORAGE_ERROR_STORAGE, | 49 CACHE_STORAGE_ERROR_STORAGE, |
| 44 CACHE_STORAGE_ERROR_EMPTY_KEY, | 50 CACHE_STORAGE_ERROR_EMPTY_KEY, |
| 51 CACHE_STORAGE_ERROR_CLOSING |
| 45 }; | 52 }; |
| 46 | 53 |
| 47 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 54 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
| 48 typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback; | 55 typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback; |
| 49 typedef base::Callback<void(const std::vector<std::string>&, | 56 typedef base::Callback<void(const std::vector<std::string>&, |
| 50 CacheStorageError)> StringsAndErrorCallback; | 57 CacheStorageError)> StringsAndErrorCallback; |
| 51 | 58 |
| 52 ServiceWorkerCacheStorage( | 59 ServiceWorkerCacheStorage( |
| 53 const base::FilePath& origin_path, | 60 const base::FilePath& origin_path, |
| 54 bool memory_only, | 61 bool memory_only, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 80 | 87 |
| 81 // Calls the callback with a vector of cache names (keys) available. | 88 // Calls the callback with a vector of cache names (keys) available. |
| 82 void EnumerateCaches(const StringsAndErrorCallback& callback); | 89 void EnumerateCaches(const StringsAndErrorCallback& callback); |
| 83 | 90 |
| 84 // TODO(jkarlin): Add match() function. | 91 // TODO(jkarlin): Add match() function. |
| 85 | 92 |
| 86 private: | 93 private: |
| 87 class MemoryLoader; | 94 class MemoryLoader; |
| 88 class SimpleCacheLoader; | 95 class SimpleCacheLoader; |
| 89 class CacheLoader; | 96 class CacheLoader; |
| 97 struct CacheContext; |
| 90 | 98 |
| 91 typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap; | 99 typedef std::map<CacheID, CacheContext*> CacheMap; |
| 92 typedef CacheMap::KeyType CacheID; | |
| 93 typedef std::map<std::string, CacheID> NameMap; | 100 typedef std::map<std::string, CacheID> NameMap; |
| 94 | 101 |
| 95 ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const; | 102 CacheContext* GetLoadedCache(const std::string& cache_name) const; |
| 96 | 103 |
| 97 // Initializer and its callback are below. | 104 // Initializer and its callback are below. |
| 98 void LazyInit(const base::Closure& closure); | 105 void LazyInit(const base::Closure& closure); |
| 99 void LazyInitDidLoadIndex( | 106 void LazyInitDidLoadIndex( |
| 100 const base::Closure& callback, | 107 const base::Closure& callback, |
| 101 scoped_ptr<std::vector<std::string> > indexed_cache_names); | 108 scoped_ptr<std::vector<std::string> > indexed_cache_names); |
| 102 void LazyInitIterateAndLoadCacheName( | 109 void LazyInitIterateAndLoadCacheName( |
| 103 const base::Closure& callback, | 110 const base::Closure& callback, |
| 104 scoped_ptr<std::vector<std::string> > indexed_cache_names, | 111 scoped_ptr<std::vector<std::string> > indexed_cache_names, |
| 105 const std::vector<std::string>::const_iterator& iter, | 112 const std::vector<std::string>::const_iterator& iter, |
| 113 const std::string& cache_name, |
| 106 scoped_ptr<ServiceWorkerCache> cache); | 114 scoped_ptr<ServiceWorkerCache> cache); |
| 107 void LazyInitDone(); | 115 void LazyInitDone(); |
| 108 | 116 |
| 109 void DidCreateBackend(base::WeakPtr<ServiceWorkerCache> cache, | 117 void DidCreateBackend(base::WeakPtr<ServiceWorkerCache> cache, |
| 118 CacheID cache_id, |
| 110 const CacheAndErrorCallback& callback, | 119 const CacheAndErrorCallback& callback, |
| 111 ServiceWorkerCache::ErrorType error); | 120 ServiceWorkerCache::ErrorType error); |
| 112 | 121 |
| 113 void AddCacheToMaps(scoped_ptr<ServiceWorkerCache> cache); | 122 CacheContext* AddCacheToMaps(const std::string& cache_name, |
| 123 scoped_ptr<ServiceWorkerCache> cache); |
| 114 | 124 |
| 115 // The CreateCache callbacks are below. | 125 // The CreateCache callbacks are below. |
| 116 void CreateCacheDidCreateCache(const std::string& cache_name, | 126 void CreateCacheDidCreateCache(const std::string& cache_name, |
| 117 const CacheAndErrorCallback& callback, | 127 const CacheAndErrorCallback& callback, |
| 118 scoped_ptr<ServiceWorkerCache> cache); | 128 scoped_ptr<ServiceWorkerCache> cache); |
| 119 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, | 129 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, |
| 120 base::WeakPtr<ServiceWorkerCache> cache, | 130 base::WeakPtr<ServiceWorkerCache> cache, |
| 131 CacheID id, |
| 121 bool success); | 132 bool success); |
| 122 | 133 |
| 123 // The DeleteCache callbacks are below. | 134 // The DeleteCache callbacks are below. |
| 124 void DeleteCacheDidWriteIndex(const std::string& cache_name, | 135 void DeleteCacheDidWriteIndex(const std::string& cache_name, |
| 125 const BoolAndErrorCallback& callback, | 136 const BoolAndErrorCallback& callback, |
| 126 bool success); | 137 bool success); |
| 127 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | 138 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, |
| 128 bool success); | 139 bool success); |
| 129 | 140 |
| 130 // Whether or not we've loaded the list of cache names into memory. | 141 // Whether or not we've loaded the list of cache names into memory. |
| 131 bool initialized_; | 142 bool initialized_; |
| 132 | 143 |
| 133 // The list of operations waiting on initialization. | 144 // The list of operations waiting on initialization. |
| 134 std::vector<base::Closure> init_callbacks_; | 145 std::vector<base::Closure> init_callbacks_; |
| 135 | 146 |
| 136 // The map of ServiceWorkerCache objects to their integer ids that | 147 // The map of CacheIDs to their CacheContext objects. Owns the CacheContext |
| 137 // ServiceWorkers reference. Owns the cache objects. | 148 // object. The CacheIDs are used by JavaScript to reference a |
| 149 // ServiceWorkerCache. |
| 138 CacheMap cache_map_; | 150 CacheMap cache_map_; |
| 151 CacheID next_cache_id_; // The next CacheID to use in cache_map_ |
| 139 | 152 |
| 140 // The map of cache names to their integer ids. | 153 // The map of cache names to their integer ids. |
| 141 NameMap name_map_; | 154 NameMap name_map_; |
| 142 | 155 |
| 143 // The file path for this CacheStorage. | 156 // The file path for this CacheStorage. |
| 144 base::FilePath origin_path_; | 157 base::FilePath origin_path_; |
| 145 | 158 |
| 146 // The TaskRunner to run file IO on. | 159 // The TaskRunner to run file IO on. |
| 147 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 160 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 148 | 161 |
| 149 // Performs backend specific operations (memory vs disk). | 162 // Performs backend specific operations (memory vs disk). |
| 150 scoped_refptr<CacheLoader> cache_loader_; | 163 scoped_refptr<CacheLoader> cache_loader_; |
| 151 | 164 |
| 152 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; | 165 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; |
| 153 | 166 |
| 154 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); | 167 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); |
| 155 }; | 168 }; |
| 156 | 169 |
| 157 } // namespace content | 170 } // namespace content |
| 158 | 171 |
| 159 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 172 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| OLD | NEW |