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 static const char kIndexFileName
[];
54 ServiceWorkerCacheStorage(
55 const base::FilePath
& origin_path
,
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
,
63 // Any unfinished asynchronous operations may not complete or call their
65 virtual ~ServiceWorkerCacheStorage();
67 // Get the cache for the given key. If the cache is not found it is
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 // Calls match on the cache with the given |cache_name|.
85 void MatchCache(const std::string
& cache_name
,
86 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
87 const ServiceWorkerCache::ResponseCallback
& callback
);
89 // Calls match on all of the caches in parallel, calling |callback| with the
90 // first response found. Note that if multiple caches have the same
91 // request/response then it is not defined which cache's response will be
92 // returned. If no response is found then |callback| is called with
93 // ServiceWorkerCache::ErrorTypeNotFound.
94 void MatchAllCaches(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
95 const ServiceWorkerCache::ResponseCallback
& callback
);
97 void CloseAllCaches(const base::Closure
& callback
);
99 // The size of all of the origin's contents in memory. Returns 0 if the cache
100 // backend is not a memory backend.
101 int64
MemoryBackedSize() const;
105 class SimpleCacheLoader
;
108 typedef std::map
<std::string
, base::WeakPtr
<ServiceWorkerCache
> > CacheMap
;
110 // Return a ServiceWorkerCache for the given name if the name is known. If the
111 // ServiceWorkerCache has been deleted, creates a new one.
112 scoped_refptr
<ServiceWorkerCache
> GetLoadedCache(
113 const std::string
& cache_name
);
115 // Initializer and its callback are below. While LazyInit is running any new
116 // operations will be queued and started in order after initialization.
117 void LazyInit(const base::Closure
& closure
);
118 void LazyInitDidLoadIndex(
119 const base::Closure
& callback
,
120 scoped_ptr
<std::vector
<std::string
> > indexed_cache_names
);
122 void AddCacheToMap(const std::string
& cache_name
,
123 base::WeakPtr
<ServiceWorkerCache
> cache
);
125 // The CreateCache callbacks are below.
126 void CreateCacheDidCreateCache(
127 const std::string
& cache_name
,
128 const CacheAndErrorCallback
& callback
,
129 const scoped_refptr
<ServiceWorkerCache
>& cache
);
130 void CreateCacheDidWriteIndex(const CacheAndErrorCallback
& callback
,
131 const scoped_refptr
<ServiceWorkerCache
>& cache
,
134 // The DeleteCache callbacks are below.
135 void DeleteCacheDidClose(const std::string
& cache_name
,
136 const BoolAndErrorCallback
& callback
,
137 const StringVector
& ordered_cache_names
,
138 const scoped_refptr
<ServiceWorkerCache
>& cache
);
139 void DeleteCacheDidWriteIndex(const std::string
& cache_name
,
140 const BoolAndErrorCallback
& callback
,
142 void DeleteCacheDidCleanUp(const BoolAndErrorCallback
& callback
,
145 // The MatchCache callbacks are below.
146 void MatchCacheDidMatch(const scoped_refptr
<ServiceWorkerCache
>& cache
,
147 const ServiceWorkerCache::ResponseCallback
& callback
,
148 ServiceWorkerCache::ErrorType error
,
149 scoped_ptr
<ServiceWorkerResponse
> response
,
150 scoped_ptr
<storage::BlobDataHandle
> handle
);
152 // The MatchAllCaches callbacks are below.
153 void MatchAllCachesDidMatch(scoped_refptr
<ServiceWorkerCache
> cache
,
154 const base::Closure
& barrier_closure
,
155 ServiceWorkerCache::ResponseCallback
* callback
,
156 ServiceWorkerCache::ErrorType error
,
157 scoped_ptr
<ServiceWorkerResponse
> response
,
158 scoped_ptr
<storage::BlobDataHandle
> handle
);
159 void MatchAllCachesDidMatchAll(
160 scoped_ptr
<ServiceWorkerCache::ResponseCallback
> callback
);
162 // Whether or not we've loaded the list of cache names into memory.
165 // The list of operations waiting on initialization.
166 std::vector
<base::Closure
> init_callbacks_
;
168 // The map of cache names to ServiceWorkerCache objects.
171 // The names of caches in the order that they were created.
172 StringVector ordered_cache_names_
;
174 // The file path for this CacheStorage.
175 base::FilePath origin_path_
;
177 // The TaskRunner to run file IO on.
178 scoped_refptr
<base::SequencedTaskRunner
> cache_task_runner_
;
180 // Whether or not to store data in disk or memory.
183 // Performs backend specific operations (memory vs disk).
184 scoped_ptr
<CacheLoader
> cache_loader_
;
186 base::WeakPtrFactory
<ServiceWorkerCacheStorage
> weak_factory_
;
188 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage
);
191 } // namespace content
193 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_