Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_cache_storage.h
blob21a80f63283c0456365e3410083206d9dbab77a9
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 // TODO(jkarlin): Convert this (and everything that uses it) to int64_t so
38 // that we don't run out of id space.
39 typedef int32_t CacheID;
41 // The CacheID returned on failed cache operations.
42 const static int kInvalidCacheID;
44 enum CacheStorageError {
45 CACHE_STORAGE_ERROR_NO_ERROR,
46 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED,
47 CACHE_STORAGE_ERROR_NOT_FOUND,
48 CACHE_STORAGE_ERROR_EXISTS,
49 CACHE_STORAGE_ERROR_STORAGE,
50 CACHE_STORAGE_ERROR_EMPTY_KEY,
51 CACHE_STORAGE_ERROR_CLOSING
54 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback;
55 typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback;
56 typedef base::Callback<void(const std::vector<std::string>&,
57 CacheStorageError)> StringsAndErrorCallback;
59 ServiceWorkerCacheStorage(
60 const base::FilePath& origin_path,
61 bool memory_only,
62 base::SequencedTaskRunner* cache_task_runner,
63 net::URLRequestContext* request_context,
64 base::WeakPtr<storage::BlobStorageContext> blob_context);
66 virtual ~ServiceWorkerCacheStorage();
68 // Create a ServiceWorkerCache if it doesn't already exist and call the
69 // callback with the cache's id. If it already
70 // exists the callback is called with CACHE_STORAGE_ERROR_EXISTS.
71 void CreateCache(const std::string& cache_name,
72 const CacheAndErrorCallback& callback);
74 // Get the cache id for the given key. If not found returns
75 // CACHE_STORAGE_ERROR_NOT_FOUND.
76 void GetCache(const std::string& cache_name,
77 const CacheAndErrorCallback& callback);
79 // Calls the callback with whether or not the cache exists.
80 void HasCache(const std::string& cache_name,
81 const BoolAndErrorCallback& callback);
83 // Deletes the cache if it exists. If it doesn't exist,
84 // CACHE_STORAGE_ERROR_NOT_FOUND is returned.
85 void DeleteCache(const std::string& cache_name,
86 const BoolAndErrorCallback& callback);
88 // Calls the callback with a vector of cache names (keys) available.
89 void EnumerateCaches(const StringsAndErrorCallback& callback);
91 // TODO(jkarlin): Add match() function.
93 private:
94 class MemoryLoader;
95 class SimpleCacheLoader;
96 class CacheLoader;
97 struct CacheContext;
99 typedef std::map<CacheID, CacheContext*> CacheMap;
100 typedef std::map<std::string, CacheID> NameMap;
102 CacheContext* GetLoadedCache(const std::string& cache_name) const;
104 // Initializer and its callback are below.
105 void LazyInit(const base::Closure& closure);
106 void LazyInitDidLoadIndex(
107 const base::Closure& callback,
108 scoped_ptr<std::vector<std::string> > indexed_cache_names);
109 void LazyInitIterateAndLoadCacheName(
110 const base::Closure& callback,
111 scoped_ptr<std::vector<std::string> > indexed_cache_names,
112 const std::vector<std::string>::const_iterator& iter,
113 const std::string& cache_name,
114 scoped_ptr<ServiceWorkerCache> cache);
115 void LazyInitDone();
117 void DidCreateBackend(base::WeakPtr<ServiceWorkerCache> cache,
118 CacheID cache_id,
119 const CacheAndErrorCallback& callback,
120 ServiceWorkerCache::ErrorType error);
122 CacheContext* AddCacheToMaps(const std::string& cache_name,
123 scoped_ptr<ServiceWorkerCache> cache);
125 // The CreateCache callbacks are below.
126 void CreateCacheDidCreateCache(const std::string& cache_name,
127 const CacheAndErrorCallback& callback,
128 scoped_ptr<ServiceWorkerCache> cache);
129 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback,
130 base::WeakPtr<ServiceWorkerCache> cache,
131 CacheID id,
132 bool success);
134 // The DeleteCache callbacks are below.
135 void DeleteCacheDidWriteIndex(const std::string& cache_name,
136 const BoolAndErrorCallback& callback,
137 bool success);
138 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback,
139 bool success);
141 // Whether or not we've loaded the list of cache names into memory.
142 bool initialized_;
144 // The list of operations waiting on initialization.
145 std::vector<base::Closure> init_callbacks_;
147 // The map of CacheIDs to their CacheContext objects. Owns the CacheContext
148 // object. The CacheIDs are used by JavaScript to reference a
149 // ServiceWorkerCache.
150 CacheMap cache_map_;
151 CacheID next_cache_id_; // The next CacheID to use in cache_map_
153 // The map of cache names to their integer ids.
154 NameMap name_map_;
156 // The file path for this CacheStorage.
157 base::FilePath origin_path_;
159 // The TaskRunner to run file IO on.
160 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
162 // Performs backend specific operations (memory vs disk).
163 scoped_ptr<CacheLoader> cache_loader_;
165 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_;
167 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage);
170 } // namespace content
172 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_