| 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 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 namespace content { | 28 namespace content { |
| 29 | 29 |
| 30 // TODO(jkarlin): Constrain the total bytes used per origin. | 30 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 31 | 31 |
| 32 // 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 |
| 33 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run | 33 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run |
| 34 // on the IO thread. | 34 // on the IO thread. |
| 35 class CONTENT_EXPORT ServiceWorkerCacheStorage { | 35 class CONTENT_EXPORT ServiceWorkerCacheStorage { |
| 36 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 | |
| 44 enum CacheStorageError { | 37 enum CacheStorageError { |
| 45 CACHE_STORAGE_ERROR_NO_ERROR, | 38 CACHE_STORAGE_ERROR_NO_ERROR, |
| 46 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, | 39 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, |
| 47 CACHE_STORAGE_ERROR_NOT_FOUND, | 40 CACHE_STORAGE_ERROR_NOT_FOUND, |
| 48 CACHE_STORAGE_ERROR_EXISTS, | 41 CACHE_STORAGE_ERROR_EXISTS, |
| 49 CACHE_STORAGE_ERROR_STORAGE, | 42 CACHE_STORAGE_ERROR_STORAGE, |
| 50 CACHE_STORAGE_ERROR_CLOSING | 43 CACHE_STORAGE_ERROR_CLOSING |
| 51 }; | 44 }; |
| 52 | 45 |
| 53 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 46 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
| 54 typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback; | 47 typedef base::Callback<void(const scoped_refptr<ServiceWorkerCache>&, |
| 48 CacheStorageError)> CacheAndErrorCallback; |
| 55 typedef base::Callback<void(const std::vector<std::string>&, | 49 typedef base::Callback<void(const std::vector<std::string>&, |
| 56 CacheStorageError)> StringsAndErrorCallback; | 50 CacheStorageError)> StringsAndErrorCallback; |
| 57 | 51 |
| 58 ServiceWorkerCacheStorage( | 52 ServiceWorkerCacheStorage( |
| 59 const base::FilePath& origin_path, | 53 const base::FilePath& origin_path, |
| 60 bool memory_only, | 54 bool memory_only, |
| 61 base::SequencedTaskRunner* cache_task_runner, | 55 base::SequencedTaskRunner* cache_task_runner, |
| 62 net::URLRequestContext* request_context, | 56 net::URLRequestContext* request_context, |
| 63 base::WeakPtr<storage::BlobStorageContext> blob_context); | 57 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 64 | 58 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 86 | 80 |
| 87 // Calls the callback with a vector of cache names (keys) available. | 81 // Calls the callback with a vector of cache names (keys) available. |
| 88 void EnumerateCaches(const StringsAndErrorCallback& callback); | 82 void EnumerateCaches(const StringsAndErrorCallback& callback); |
| 89 | 83 |
| 90 // TODO(jkarlin): Add match() function. | 84 // TODO(jkarlin): Add match() function. |
| 91 | 85 |
| 92 private: | 86 private: |
| 93 class MemoryLoader; | 87 class MemoryLoader; |
| 94 class SimpleCacheLoader; | 88 class SimpleCacheLoader; |
| 95 class CacheLoader; | 89 class CacheLoader; |
| 96 struct CacheContext; | |
| 97 | 90 |
| 98 typedef std::map<CacheID, CacheContext*> CacheMap; | 91 typedef std::map<std::string, base::WeakPtr<ServiceWorkerCache> > CacheMap; |
| 99 typedef std::map<std::string, CacheID> NameMap; | |
| 100 | 92 |
| 101 CacheContext* GetLoadedCache(const std::string& cache_name) const; | 93 // Return a ServiceWorkerCache for the given name if the name is known. If the |
| 94 // ServiceWorkerCache has been deleted, creates a new one. |
| 95 scoped_refptr<ServiceWorkerCache> GetLoadedCache( |
| 96 const std::string& cache_name); |
| 102 | 97 |
| 103 // Initializer and its callback are below. | 98 // Initializer and its callback are below. |
| 104 void LazyInit(const base::Closure& closure); | 99 void LazyInit(const base::Closure& closure); |
| 105 void LazyInitDidLoadIndex( | 100 void LazyInitDidLoadIndex( |
| 106 const base::Closure& callback, | 101 const base::Closure& callback, |
| 107 scoped_ptr<std::vector<std::string> > indexed_cache_names); | 102 scoped_ptr<std::vector<std::string> > indexed_cache_names); |
| 108 | 103 |
| 109 CacheContext* AddCacheToMaps(const std::string& cache_name, | 104 void AddCacheToMap(const std::string& cache_name, |
| 110 scoped_ptr<ServiceWorkerCache> cache); | 105 base::WeakPtr<ServiceWorkerCache> cache); |
| 111 | 106 |
| 112 // The CreateCache callbacks are below. | 107 // The CreateCache callbacks are below. |
| 113 void CreateCacheDidCreateCache(const std::string& cache_name, | 108 void CreateCacheDidCreateCache( |
| 114 const CacheAndErrorCallback& callback, | 109 const std::string& cache_name, |
| 115 scoped_ptr<ServiceWorkerCache> cache); | 110 const CacheAndErrorCallback& callback, |
| 111 const scoped_refptr<ServiceWorkerCache>& cache); |
| 116 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, | 112 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, |
| 117 base::WeakPtr<ServiceWorkerCache> cache, | 113 const scoped_refptr<ServiceWorkerCache>& cache, |
| 118 CacheID id, | |
| 119 bool success); | 114 bool success); |
| 120 | 115 |
| 121 // The DeleteCache callbacks are below. | 116 // The DeleteCache callbacks are below. |
| 122 void DeleteCacheDidWriteIndex(const std::string& cache_name, | 117 void DeleteCacheDidWriteIndex(const std::string& cache_name, |
| 123 const BoolAndErrorCallback& callback, | 118 const BoolAndErrorCallback& callback, |
| 124 bool success); | 119 bool success); |
| 125 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | 120 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, |
| 126 bool success); | 121 bool success); |
| 127 | 122 |
| 128 // Whether or not we've loaded the list of cache names into memory. | 123 // Whether or not we've loaded the list of cache names into memory. |
| 129 bool initialized_; | 124 bool initialized_; |
| 130 | 125 |
| 131 // The list of operations waiting on initialization. | 126 // The list of operations waiting on initialization. |
| 132 std::vector<base::Closure> init_callbacks_; | 127 std::vector<base::Closure> init_callbacks_; |
| 133 | 128 |
| 134 // The map of CacheIDs to their CacheContext objects. Owns the CacheContext | 129 // The map of cache names to ServiceWorkerCache objects. |
| 135 // object. The CacheIDs are used by JavaScript to reference a | |
| 136 // ServiceWorkerCache. | |
| 137 CacheMap cache_map_; | 130 CacheMap cache_map_; |
| 138 CacheID next_cache_id_; // The next CacheID to use in cache_map_ | |
| 139 | |
| 140 // The map of cache names to their integer ids. | |
| 141 NameMap name_map_; | |
| 142 | 131 |
| 143 // The file path for this CacheStorage. | 132 // The file path for this CacheStorage. |
| 144 base::FilePath origin_path_; | 133 base::FilePath origin_path_; |
| 145 | 134 |
| 146 // The TaskRunner to run file IO on. | 135 // The TaskRunner to run file IO on. |
| 147 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 136 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 148 | 137 |
| 138 // Whether or not to store data in disk or memory. |
| 139 bool memory_only_; |
| 140 |
| 149 // Performs backend specific operations (memory vs disk). | 141 // Performs backend specific operations (memory vs disk). |
| 150 scoped_ptr<CacheLoader> cache_loader_; | 142 scoped_ptr<CacheLoader> cache_loader_; |
| 151 | 143 |
| 152 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; | 144 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; |
| 153 | 145 |
| 154 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); | 146 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); |
| 155 }; | 147 }; |
| 156 | 148 |
| 157 } // namespace content | 149 } // namespace content |
| 158 | 150 |
| 159 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 151 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| OLD | NEW |