OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/id_map.h" | 13 #include "base/id_map.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class MessageLoopProxy; | 17 class SequencedTaskRunner; |
18 } | 18 } |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | |
22 class ServiceWorkerCache; | 21 class ServiceWorkerCache; |
23 | 22 |
24 // TODO(jkarlin): Constrain the total bytes used per origin. | 23 // TODO(jkarlin): Constrain the total bytes used per origin. |
25 // The set of caches for a given origin. It is owned by the | 24 // The set of caches for a given origin. It is owned by the |
26 // ServiceWorkerCacheStorageManager. Provided callbacks are called on the | 25 // ServiceWorkerCacheStorageManager. Provided callbacks are called on the |
27 // |callback_loop| provided in the constructor. | 26 // |callback_loop| provided in the constructor. |
michaeln
2014/08/11 19:30:17
please update the comment to describe the new rule
jkarlin
2014/08/12 14:55:03
Done.
| |
28 class ServiceWorkerCacheStorage { | 27 class ServiceWorkerCacheStorage { |
29 public: | 28 public: |
30 enum CacheStorageError { | 29 enum CacheStorageError { |
31 CACHE_STORAGE_ERROR_NO_ERROR, | 30 CACHE_STORAGE_ERROR_NO_ERROR, |
32 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, | 31 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, |
33 CACHE_STORAGE_ERROR_NOT_FOUND, | 32 CACHE_STORAGE_ERROR_NOT_FOUND, |
34 CACHE_STORAGE_ERROR_EXISTS, | 33 CACHE_STORAGE_ERROR_EXISTS, |
35 CACHE_STORAGE_ERROR_STORAGE, | 34 CACHE_STORAGE_ERROR_STORAGE, |
36 CACHE_STORAGE_ERROR_EMPTY_KEY, | 35 CACHE_STORAGE_ERROR_EMPTY_KEY, |
37 }; | 36 }; |
38 | 37 |
39 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 38 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
40 typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback; | 39 typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback; |
41 typedef base::Callback<void(const std::vector<std::string>&, | 40 typedef base::Callback<void(const std::vector<std::string>&, |
42 CacheStorageError)> StringsAndErrorCallback; | 41 CacheStorageError)> StringsAndErrorCallback; |
43 | 42 |
44 ServiceWorkerCacheStorage( | 43 ServiceWorkerCacheStorage(const base::FilePath& origin_path, |
45 const base::FilePath& origin_path, | 44 bool memory_only, |
46 bool memory_only, | 45 base::SequencedTaskRunner* cache_task_runner); |
47 const scoped_refptr<base::MessageLoopProxy>& callback_loop); | |
48 | 46 |
49 virtual ~ServiceWorkerCacheStorage(); | 47 virtual ~ServiceWorkerCacheStorage(); |
50 | 48 |
51 // Create a ServiceWorkerCache if it doesn't already exist and call the | 49 // Create a ServiceWorkerCache if it doesn't already exist and call the |
52 // callback with the cache's id. If it already | 50 // callback with the cache's id. If it already |
53 // exists the callback is called with CACHE_STORAGE_ERROR_EXISTS. | 51 // exists the callback is called with CACHE_STORAGE_ERROR_EXISTS. |
54 void CreateCache(const std::string& cache_name, | 52 void CreateCache(const std::string& cache_name, |
55 const CacheAndErrorCallback& callback); | 53 const CacheAndErrorCallback& callback); |
56 | 54 |
57 // Get the cache id for the given key. If not found returns | 55 // Get the cache id for the given key. If not found returns |
58 // CACHE_STORAGE_ERROR_NOT_FOUND. | 56 // CACHE_STORAGE_ERROR_NOT_FOUND. |
59 void GetCache(const std::string& cache_name, | 57 void GetCache(const std::string& cache_name, |
60 const CacheAndErrorCallback& callback); | 58 const CacheAndErrorCallback& callback); |
61 | 59 |
62 // Calls the callback with whether or not the cache exists. | 60 // Calls the callback with whether or not the cache exists. |
63 void HasCache(const std::string& cache_name, | 61 void HasCache(const std::string& cache_name, |
64 const BoolAndErrorCallback& callback); | 62 const BoolAndErrorCallback& callback); |
65 | 63 |
66 // Deletes the cache if it exists. If it doesn't exist, | 64 // Deletes the cache if it exists. If it doesn't exist, |
67 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. | 65 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. |
68 void DeleteCache(const std::string& cache_name, | 66 void DeleteCache(const std::string& cache_name, |
69 const BoolAndErrorCallback& callback); | 67 const BoolAndErrorCallback& callback); |
70 | 68 |
71 // Calls the callback with a vector of cache names (keys) available. | 69 // Calls the callback with a vector of cache names (keys) available. |
72 void EnumerateCaches(const StringsAndErrorCallback& callback); | 70 void EnumerateCaches(const StringsAndErrorCallback& callback); |
73 | 71 |
74 // TODO(jkarlin): Add match() function. | 72 // TODO(jkarlin): Add match() function. |
75 | 73 |
76 void InitializeCacheCallback(const ServiceWorkerCache* cache, | |
77 const CacheAndErrorCallback& callback, | |
78 bool success); | |
79 | |
80 private: | 74 private: |
81 class MemoryLoader; | 75 class MemoryLoader; |
82 class SimpleCacheLoader; | 76 class SimpleCacheLoader; |
83 class CacheLoader; | 77 class CacheLoader; |
84 | 78 |
85 typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap; | 79 typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap; |
86 typedef CacheMap::KeyType CacheID; | 80 typedef CacheMap::KeyType CacheID; |
87 typedef std::map<std::string, CacheID> NameMap; | 81 typedef std::map<std::string, CacheID> NameMap; |
88 | 82 |
89 ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const; | 83 ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const; |
90 | 84 |
91 void LazyInit(); | 85 // Initializer and its callback are below. |
92 void InitCache(ServiceWorkerCache* cache); | 86 void LazyInit(const base::Closure& closure); |
87 void LazyInitDidLoadIndex( | |
88 const base::Closure& callback, | |
89 scoped_ptr<std::vector<std::string> > indexed_cache_names); | |
90 void LazyInitIterateAndLoadCacheName( | |
91 const base::Closure& callback, | |
92 scoped_ptr<std::vector<std::string> > indexed_cache_names, | |
93 std::vector<std::string>::const_iterator iter, | |
94 ServiceWorkerCache* cache); | |
95 void LazyInitDone(); | |
93 | 96 |
97 void DidCreateBackend(const ServiceWorkerCache* cache, | |
98 const CacheAndErrorCallback& callback, | |
99 bool success); | |
100 | |
101 void AddCacheToMaps(ServiceWorkerCache* cache); | |
102 | |
103 // The CreateCache callbacks are below. | |
104 void CreateCacheDidCreateCache(const std::string& cache_name, | |
105 const CacheAndErrorCallback& callback, | |
106 ServiceWorkerCache* cache); | |
107 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, | |
108 ServiceWorkerCache* cache, | |
109 bool success); | |
110 | |
111 // The DeleteCache callbacks are below. | |
112 void DeleteCacheDidWriteIndex(const std::string& cache_name, | |
113 const BoolAndErrorCallback& callback, | |
114 bool success); | |
115 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | |
116 bool success); | |
117 | |
118 // Whether or not we've loaded the list of cache names into memory. | |
94 bool initialized_; | 119 bool initialized_; |
120 | |
121 // The list of operations waiting on initialization. | |
122 std::vector<base::Closure> init_callbacks_; | |
123 | |
124 // The map of ServiceWorkerCache objects to their integer ids that | |
125 // ServiceWorkers reference. Owns the cache objects. | |
95 CacheMap cache_map_; | 126 CacheMap cache_map_; |
127 | |
128 // The map of cache names to their integer ids. | |
96 NameMap name_map_; | 129 NameMap name_map_; |
130 | |
131 // The file path for this CacheStorage. | |
97 base::FilePath origin_path_; | 132 base::FilePath origin_path_; |
98 scoped_refptr<base::MessageLoopProxy> callback_loop_; | 133 |
134 // The TaskRunner to run file IO on. | |
135 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | |
136 | |
137 // Performs backend specific operations (memory vs disk). | |
99 scoped_ptr<CacheLoader> cache_loader_; | 138 scoped_ptr<CacheLoader> cache_loader_; |
100 | 139 |
140 base::ThreadChecker thread_checker_; | |
michaeln
2014/08/11 19:30:17
wdyt of calling this io_thread_checker_ ? it's rea
jkarlin
2014/08/12 14:55:03
Done. Used DCHECK_CURRENTLY_ON(BrowserThread::IO)
| |
141 | |
101 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); | 142 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); |
102 }; | 143 }; |
103 | 144 |
104 } // namespace content | 145 } // namespace content |
105 | 146 |
106 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 147 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
OLD | NEW |