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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/id_map.h"
14 #include "base/memory/weak_ptr.h"
17 class SequencedTaskRunner;
21 class URLRequestContext;
24 namespace webkit_blob {
25 class BlobStorageContext;
29 class ServiceWorkerCache;
31 // TODO(jkarlin): Constrain the total bytes used per origin.
33 // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is
34 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run
36 class ServiceWorkerCacheStorage {
38 enum CacheStorageError {
39 CACHE_STORAGE_ERROR_NO_ERROR,
40 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED,
41 CACHE_STORAGE_ERROR_NOT_FOUND,
42 CACHE_STORAGE_ERROR_EXISTS,
43 CACHE_STORAGE_ERROR_STORAGE,
44 CACHE_STORAGE_ERROR_EMPTY_KEY,
47 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback;
48 typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback;
49 typedef base::Callback<void(const std::vector<std::string>&,
50 CacheStorageError)> StringsAndErrorCallback;
52 ServiceWorkerCacheStorage(
53 const base::FilePath& origin_path,
55 base::SequencedTaskRunner* cache_task_runner,
56 net::URLRequestContext* request_context,
57 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context);
59 virtual ~ServiceWorkerCacheStorage();
61 // Create a ServiceWorkerCache if it doesn't already exist and call the
62 // callback with the cache's id. If it already
63 // exists the callback is called with CACHE_STORAGE_ERROR_EXISTS.
64 void CreateCache(const std::string& cache_name,
65 const CacheAndErrorCallback& callback);
67 // Get the cache id for the given key. If not found returns
68 // CACHE_STORAGE_ERROR_NOT_FOUND.
69 void GetCache(const std::string& cache_name,
70 const CacheAndErrorCallback& callback);
72 // Calls the callback with whether or not the cache exists.
73 void HasCache(const std::string& cache_name,
74 const BoolAndErrorCallback& callback);
76 // Deletes the cache if it exists. If it doesn't exist,
77 // CACHE_STORAGE_ERROR_NOT_FOUND is returned.
78 void DeleteCache(const std::string& cache_name,
79 const BoolAndErrorCallback& callback);
81 // Calls the callback with a vector of cache names (keys) available.
82 void EnumerateCaches(const StringsAndErrorCallback& callback);
84 // TODO(jkarlin): Add match() function.
88 class SimpleCacheLoader;
91 typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap;
92 typedef CacheMap::KeyType CacheID;
93 typedef std::map<std::string, CacheID> NameMap;
95 ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const;
97 // Initializer and its callback are below.
98 void LazyInit(const base::Closure& closure);
99 void LazyInitDidLoadIndex(
100 const base::Closure& callback,
101 scoped_ptr<std::vector<std::string> > indexed_cache_names);
102 void LazyInitIterateAndLoadCacheName(
103 const base::Closure& callback,
104 scoped_ptr<std::vector<std::string> > indexed_cache_names,
105 const std::vector<std::string>::const_iterator& iter,
106 scoped_ptr<ServiceWorkerCache> cache);
109 void DidCreateBackend(base::WeakPtr<ServiceWorkerCache> cache,
110 const CacheAndErrorCallback& callback,
113 void AddCacheToMaps(scoped_ptr<ServiceWorkerCache> cache);
115 // The CreateCache callbacks are below.
116 void CreateCacheDidCreateCache(const std::string& cache_name,
117 const CacheAndErrorCallback& callback,
118 scoped_ptr<ServiceWorkerCache> cache);
119 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback,
120 base::WeakPtr<ServiceWorkerCache> cache,
123 // The DeleteCache callbacks are below.
124 void DeleteCacheDidWriteIndex(const std::string& cache_name,
125 const BoolAndErrorCallback& callback,
127 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback,
130 // Whether or not we've loaded the list of cache names into memory.
133 // The list of operations waiting on initialization.
134 std::vector<base::Closure> init_callbacks_;
136 // The map of ServiceWorkerCache objects to their integer ids that
137 // ServiceWorkers reference. Owns the cache objects.
140 // The map of cache names to their integer ids.
143 // The file path for this CacheStorage.
144 base::FilePath origin_path_;
146 // The TaskRunner to run file IO on.
147 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
149 // Performs backend specific operations (memory vs disk).
150 scoped_refptr<CacheLoader> cache_loader_;
152 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_;
154 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage);
157 } // namespace content
159 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_