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 // 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;
94 class SimpleCacheLoader
;
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
,
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
,
131 void DeleteCacheDidCleanUp(const BoolAndErrorCallback
& callback
,
134 // Whether or not we've loaded the list of cache names into memory.
137 // The list of operations waiting on initialization.
138 std::vector
<base::Closure
> init_callbacks_
;
140 // The map of cache names to ServiceWorkerCache objects.
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.
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_