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/memory/weak_ptr.h"
14 #include "content/browser/service_worker/service_worker_cache.h"
17 class SequencedTaskRunner;
21 class URLRequestContext;
25 class BlobStorageContext;
30 // TODO(jkarlin): Constrain the total bytes used per origin.
32 // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is
33 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run
35 class CONTENT_EXPORT ServiceWorkerCacheStorage {
37 enum CacheStorageError {
38 CACHE_STORAGE_ERROR_NO_ERROR,
39 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED,
40 CACHE_STORAGE_ERROR_NOT_FOUND,
41 CACHE_STORAGE_ERROR_EXISTS,
42 CACHE_STORAGE_ERROR_STORAGE,
43 CACHE_STORAGE_ERROR_CLOSING
45 typedef std::vector<std::string> StringVector;
46 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback;
47 typedef base::Callback<void(const scoped_refptr<ServiceWorkerCache>&,
48 CacheStorageError)> CacheAndErrorCallback;
49 typedef base::Callback<void(const StringVector&, CacheStorageError)>
50 StringsAndErrorCallback;
52 ServiceWorkerCacheStorage(
53 const base::FilePath& origin_path,
55 base::SequencedTaskRunner* cache_task_runner,
56 net::URLRequestContext* request_context,
57 base::WeakPtr<storage::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 std::map<std::string, base::WeakPtr<ServiceWorkerCache> > CacheMap;
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);
98 // Initializer and its callback are below. While LazyInit is running any new
99 // operations will be queued and started in order after initialization.
100 void LazyInit(const base::Closure& closure);
101 void LazyInitDidLoadIndex(
102 const base::Closure& callback,
103 scoped_ptr<std::vector<std::string> > indexed_cache_names);
105 void AddCacheToMap(const std::string& cache_name,
106 base::WeakPtr<ServiceWorkerCache> cache);
108 // The CreateCache callbacks are below.
109 void CreateCacheDidCreateCache(
110 const std::string& cache_name,
111 const CacheAndErrorCallback& callback,
112 const scoped_refptr<ServiceWorkerCache>& cache);
113 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback,
114 const scoped_refptr<ServiceWorkerCache>& cache,
117 // The DeleteCache callbacks are below.
118 void DeleteCacheDidWriteIndex(const std::string& cache_name,
119 const BoolAndErrorCallback& callback,
121 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback,
124 // Whether or not we've loaded the list of cache names into memory.
127 // The list of operations waiting on initialization.
128 std::vector<base::Closure> init_callbacks_;
130 // The map of cache names to ServiceWorkerCache objects.
133 // The names of caches in the order that they were created.
134 StringVector ordered_cache_names_;
136 // The file path for this CacheStorage.
137 base::FilePath origin_path_;
139 // The TaskRunner to run file IO on.
140 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
142 // Whether or not to store data in disk or memory.
145 // Performs backend specific operations (memory vs disk).
146 scoped_ptr<CacheLoader> cache_loader_;
148 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_;
150 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage);
153 } // namespace content
155 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_