71037b4327e695b2bdbf1d806e5d0ceaa557fe01
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_cache_storage.h
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.
4
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/id_map.h"
14 #include "base/memory/weak_ptr.h"
15
16 namespace base {
17 class SequencedTaskRunner;
18 }
19
20 namespace net {
21 class URLRequestContext;
22 }
23
24 namespace webkit_blob {
25 class BlobStorageContext;
26 }
27
28 namespace content {
29 class ServiceWorkerCache;
30
31 // TODO(jkarlin): Constrain the total bytes used per origin.
32
33 // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is
34 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run
35 // on the IO thread.
36 class ServiceWorkerCacheStorage {
37  public:
38   enum CacheStorageError {
39     CACHE_STORAGE_ERROR_NO_ERROR,
40     CACHE_STORAGE_ERROR_NOT_IMPLEMENTED,
41     CACHE_STORAGE_ERROR_NOT_FOUND,
42     CACHE_STORAGE_ERROR_EXISTS,
43     CACHE_STORAGE_ERROR_STORAGE,
44     CACHE_STORAGE_ERROR_EMPTY_KEY,
45   };
46
47   typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback;
48   typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback;
49   typedef base::Callback<void(const std::vector<std::string>&,
50                               CacheStorageError)> StringsAndErrorCallback;
51
52   ServiceWorkerCacheStorage(
53       const base::FilePath& origin_path,
54       bool memory_only,
55       base::SequencedTaskRunner* cache_task_runner,
56       net::URLRequestContext* request_context,
57       base::WeakPtr<webkit_blob::BlobStorageContext> blob_context);
58
59   virtual ~ServiceWorkerCacheStorage();
60
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);
66
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);
71
72   // Calls the callback with whether or not the cache exists.
73   void HasCache(const std::string& cache_name,
74                 const BoolAndErrorCallback& callback);
75
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);
80
81   // Calls the callback with a vector of cache names (keys) available.
82   void EnumerateCaches(const StringsAndErrorCallback& callback);
83
84   // TODO(jkarlin): Add match() function.
85
86  private:
87   class MemoryLoader;
88   class SimpleCacheLoader;
89   class CacheLoader;
90
91   typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap;
92   typedef CacheMap::KeyType CacheID;
93   typedef std::map<std::string, CacheID> NameMap;
94
95   ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const;
96
97   // Initializer and its callback are below.
98   void LazyInit(const base::Closure& closure);
99   void LazyInitDidLoadIndex(
100       const base::Closure& callback,
101       scoped_ptr<std::vector<std::string> > indexed_cache_names);
102   void LazyInitIterateAndLoadCacheName(
103       const base::Closure& callback,
104       scoped_ptr<std::vector<std::string> > indexed_cache_names,
105       const std::vector<std::string>::const_iterator& iter,
106       scoped_ptr<ServiceWorkerCache> cache);
107   void LazyInitDone();
108
109   void DidCreateBackend(base::WeakPtr<ServiceWorkerCache> cache,
110                         const CacheAndErrorCallback& callback,
111                         bool success);
112
113   void AddCacheToMaps(scoped_ptr<ServiceWorkerCache> cache);
114
115   // The CreateCache callbacks are below.
116   void CreateCacheDidCreateCache(const std::string& cache_name,
117                                  const CacheAndErrorCallback& callback,
118                                  scoped_ptr<ServiceWorkerCache> cache);
119   void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback,
120                                 base::WeakPtr<ServiceWorkerCache> cache,
121                                 bool success);
122
123   // The DeleteCache callbacks are below.
124   void DeleteCacheDidWriteIndex(const std::string& cache_name,
125                                 const BoolAndErrorCallback& callback,
126                                 bool success);
127   void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback,
128                              bool success);
129
130   // Whether or not we've loaded the list of cache names into memory.
131   bool initialized_;
132
133   // The list of operations waiting on initialization.
134   std::vector<base::Closure> init_callbacks_;
135
136   // The map of ServiceWorkerCache objects to their integer ids that
137   // ServiceWorkers reference.  Owns the cache objects.
138   CacheMap cache_map_;
139
140   // The map of cache names to their integer ids.
141   NameMap name_map_;
142
143   // The file path for this CacheStorage.
144   base::FilePath origin_path_;
145
146   // The TaskRunner to run file IO on.
147   scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
148
149   // Performs backend specific operations (memory vs disk).
150   scoped_refptr<CacheLoader> cache_loader_;
151
152   base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_;
153
154   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage);
155 };
156
157 }  // namespace content
158
159 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_