Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_cache_storage.h
bloba102707356ff9a02e9c70a01b11567dff8dc4112
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_
8 #include <map>
9 #include <string>
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"
16 namespace base {
17 class SequencedTaskRunner;
20 namespace net {
21 class URLRequestContext;
24 namespace storage {
25 class BlobStorageContext;
28 namespace content {
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
34 // on the IO thread.
35 class CONTENT_EXPORT ServiceWorkerCacheStorage {
36 public:
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 static const char kIndexFileName[];
54 ServiceWorkerCacheStorage(
55 const base::FilePath& origin_path,
56 bool memory_only,
57 base::SequencedTaskRunner* cache_task_runner,
58 net::URLRequestContext* request_context,
59 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
60 base::WeakPtr<storage::BlobStorageContext> blob_context,
61 const GURL& origin);
63 // Any unfinished asynchronous operations may not complete or call their
64 // callbacks.
65 virtual ~ServiceWorkerCacheStorage();
67 // Get the cache for the given key. If the cache is not found it is
68 // created.
69 void OpenCache(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.
86 void CloseAllCaches(const base::Closure& callback);
88 // The size of all of the origin's contents in memory. Returns 0 if the cache
89 // backend is not a memory backend.
90 int64 MemoryBackedSize() const;
92 private:
93 class MemoryLoader;
94 class SimpleCacheLoader;
95 class CacheLoader;
97 typedef std::map<std::string, base::WeakPtr<ServiceWorkerCache> > CacheMap;
99 // Return a ServiceWorkerCache for the given name if the name is known. If the
100 // ServiceWorkerCache has been deleted, creates a new one.
101 scoped_refptr<ServiceWorkerCache> GetLoadedCache(
102 const std::string& cache_name);
104 // Initializer and its callback are below. While LazyInit is running any new
105 // operations will be queued and started in order after initialization.
106 void LazyInit(const base::Closure& closure);
107 void LazyInitDidLoadIndex(
108 const base::Closure& callback,
109 scoped_ptr<std::vector<std::string> > indexed_cache_names);
111 void AddCacheToMap(const std::string& cache_name,
112 base::WeakPtr<ServiceWorkerCache> cache);
114 // The CreateCache callbacks are below.
115 void CreateCacheDidCreateCache(
116 const std::string& cache_name,
117 const CacheAndErrorCallback& callback,
118 const scoped_refptr<ServiceWorkerCache>& cache);
119 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback,
120 const scoped_refptr<ServiceWorkerCache>& cache,
121 bool success);
123 // The DeleteCache callbacks are below.
124 void DeleteCacheDidClose(const std::string& cache_name,
125 const BoolAndErrorCallback& callback,
126 const StringVector& ordered_cache_names,
127 const scoped_refptr<ServiceWorkerCache>& cache);
128 void DeleteCacheDidWriteIndex(const std::string& cache_name,
129 const BoolAndErrorCallback& callback,
130 bool success);
131 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback,
132 bool success);
134 // Whether or not we've loaded the list of cache names into memory.
135 bool initialized_;
137 // The list of operations waiting on initialization.
138 std::vector<base::Closure> init_callbacks_;
140 // The map of cache names to ServiceWorkerCache objects.
141 CacheMap cache_map_;
143 // The names of caches in the order that they were created.
144 StringVector ordered_cache_names_;
146 // The file path for this CacheStorage.
147 base::FilePath origin_path_;
149 // The TaskRunner to run file IO on.
150 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
152 // Whether or not to store data in disk or memory.
153 bool memory_only_;
155 // Performs backend specific operations (memory vs disk).
156 scoped_ptr<CacheLoader> cache_loader_;
158 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_;
160 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage);
163 } // namespace content
165 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_