Upstream version 10.39.225.0
[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/memory/weak_ptr.h"
14 #include "content/browser/service_worker/service_worker_cache.h"
15
16 namespace base {
17 class SequencedTaskRunner;
18 }
19
20 namespace net {
21 class URLRequestContext;
22 }
23
24 namespace storage {
25 class BlobStorageContext;
26 }
27
28 namespace content {
29
30 // TODO(jkarlin): Constrain the total bytes used per origin.
31
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
44   };
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;
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<storage::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 std::map<std::string, base::WeakPtr<ServiceWorkerCache> > CacheMap;
92
93   // Return a ServiceWorkerCache for the given name if the name is known. If the
94   // ServiceWorkerCache has been deleted, creates a new one.
95   scoped_refptr<ServiceWorkerCache> GetLoadedCache(
96       const std::string& cache_name);
97
98   // Initializer and its callback are below. While LazyInit is running any new
99   // operations will be queued and started in order after initialization.
100   void LazyInit(const base::Closure& closure);
101   void LazyInitDidLoadIndex(
102       const base::Closure& callback,
103       scoped_ptr<std::vector<std::string> > indexed_cache_names);
104
105   void AddCacheToMap(const std::string& cache_name,
106                      base::WeakPtr<ServiceWorkerCache> cache);
107
108   // The CreateCache callbacks are below.
109   void CreateCacheDidCreateCache(
110       const std::string& cache_name,
111       const CacheAndErrorCallback& callback,
112       const scoped_refptr<ServiceWorkerCache>& cache);
113   void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback,
114                                 const scoped_refptr<ServiceWorkerCache>& cache,
115                                 bool success);
116
117   // The DeleteCache callbacks are below.
118   void DeleteCacheDidWriteIndex(const std::string& cache_name,
119                                 const BoolAndErrorCallback& callback,
120                                 bool success);
121   void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback,
122                              bool success);
123
124   // Whether or not we've loaded the list of cache names into memory.
125   bool initialized_;
126
127   // The list of operations waiting on initialization.
128   std::vector<base::Closure> init_callbacks_;
129
130   // The map of cache names to ServiceWorkerCache objects.
131   CacheMap cache_map_;
132
133   // The names of caches in the order that they were created.
134   StringVector ordered_cache_names_;
135
136   // The file path for this CacheStorage.
137   base::FilePath origin_path_;
138
139   // The TaskRunner to run file IO on.
140   scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
141
142   // Whether or not to store data in disk or memory.
143   bool memory_only_;
144
145   // Performs backend specific operations (memory vs disk).
146   scoped_ptr<CacheLoader> cache_loader_;
147
148   base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_;
149
150   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage);
151 };
152
153 }  // namespace content
154
155 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_