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_FETCH_STORES_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ |
7 | 7 |
8 #include <map> | |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/id_map.h" | |
12 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
13 | 15 |
14 namespace base { | 16 namespace base { |
15 class MessageLoopProxy; | 17 class MessageLoopProxy; |
16 } | 18 } |
17 | 19 |
18 namespace content { | 20 namespace content { |
19 | 21 |
20 class ServiceWorkerFetchStore; | 22 class ServiceWorkerFetchStore; |
21 | 23 |
22 // The set of stores for a given ServiceWorker. It is owned by the | 24 // The set of stores for a given ServiceWorker. It is owned by the |
michaeln
2014/08/05 23:34:30
s/b The set of stores for a given Origin.
jkarlin
2014/08/06 18:56:00
Done.
| |
23 // ServiceWorkerFetchStoresManager. Provided callbacks are called on the | 25 // ServiceWorkerFetchStoresManager. Provided callbacks are called on the |
24 // |callback_loop| provided in the constructor. | 26 // |callback_loop| provided in the constructor. |
25 class ServiceWorkerFetchStores { | 27 class ServiceWorkerFetchStores { |
26 public: | 28 public: |
27 enum FetchStoresError { | 29 enum FetchStoresError { |
28 FETCH_STORES_ERROR_NO_ERROR, | 30 FETCH_STORES_ERROR_NO_ERROR, |
29 FETCH_STORES_ERROR_NOT_IMPLEMENTED, | 31 FETCH_STORES_ERROR_NOT_IMPLEMENTED, |
30 FETCH_STORES_ERROR_NOT_FOUND, | 32 FETCH_STORES_ERROR_NOT_FOUND, |
31 FETCH_STORES_ERROR_EXISTS, | 33 FETCH_STORES_ERROR_EXISTS, |
32 FETCH_STORES_ERROR_STORAGE | 34 FETCH_STORES_ERROR_STORAGE, |
35 FETCH_STORES_ERROR_EMPTY_KEY, | |
33 }; | 36 }; |
34 | 37 |
35 enum BackendType { BACKEND_TYPE_SIMPLE_CACHE, BACKEND_TYPE_MEMORY }; | |
36 | |
37 typedef base::Callback<void(bool, FetchStoresError)> BoolAndErrorCallback; | 38 typedef base::Callback<void(bool, FetchStoresError)> BoolAndErrorCallback; |
38 typedef base::Callback<void(int, FetchStoresError)> StoreAndErrorCallback; | 39 typedef base::Callback<void(int, FetchStoresError)> StoreAndErrorCallback; |
39 typedef base::Callback<void(const std::vector<std::string>&, | 40 typedef base::Callback<void(const std::vector<std::string>&, |
40 FetchStoresError)> StringsAndErrorCallback; | 41 FetchStoresError)> StringsAndErrorCallback; |
41 | 42 |
42 ServiceWorkerFetchStores( | 43 ServiceWorkerFetchStores( |
43 const base::FilePath& origin_path, | 44 const base::FilePath& origin_path, |
44 BackendType backend, | 45 bool memory_only, |
45 const scoped_refptr<base::MessageLoopProxy>& callback_loop); | 46 const scoped_refptr<base::MessageLoopProxy>& callback_loop); |
46 virtual ~ServiceWorkerFetchStores(); | 47 virtual ~ServiceWorkerFetchStores(); |
47 | 48 |
49 // Create a ServiceWorkerFetchStore if it doesn't already exist and call the | |
50 // callback with the cache's id. If it already | |
51 // exists the callback is called with FETCH_STORES_ERROR_EXISTS. | |
48 void CreateStore(const std::string& key, | 52 void CreateStore(const std::string& key, |
49 const StoreAndErrorCallback& callback); | 53 const StoreAndErrorCallback& callback); |
54 | |
55 // Get the cache id for the given key. If not found returns | |
michaeln
2014/08/05 23:34:30
nit: extra space after the first sentence
jkarlin
2014/08/06 18:56:00
Done.
| |
56 // FETCH_STORES_ERROR_NOT_FOUND. | |
50 void Get(const std::string& key, const StoreAndErrorCallback& callback); | 57 void Get(const std::string& key, const StoreAndErrorCallback& callback); |
51 void Has(const std::string& key, const BoolAndErrorCallback& callback) const; | 58 |
52 void Delete(const std::string& key, const StoreAndErrorCallback& callback); | 59 // Calls the callback with whether or not the cache exists. |
53 void Keys(const StringsAndErrorCallback& callback) const; | 60 void Has(const std::string& key, const BoolAndErrorCallback& callback); |
61 | |
62 // Deletes the cache if it exists. If it doesn't exist, | |
63 // FETCH_STORES_ERROR_NOT_FOUND is returned. | |
64 void Delete(const std::string& key, const BoolAndErrorCallback& callback); | |
65 | |
66 // Calls the callback with a vector of cache names (keys) available. | |
67 void Keys(const StringsAndErrorCallback& callback); | |
68 | |
54 // TODO(jkarlin): Add match() function. | 69 // TODO(jkarlin): Add match() function. |
55 | 70 |
56 private: | 71 private: |
72 class MemoryLoader; | |
73 class SimpleCacheLoader; | |
74 class StoresLoader; | |
75 | |
76 typedef IDMap<ServiceWorkerFetchStore, IDMapOwnPointer> StoreMap; | |
77 typedef StoreMap::KeyType StoreID; | |
78 typedef std::map<std::string, StoreID> NameMap; | |
79 | |
80 ServiceWorkerFetchStore* GetLoadedStore(const std::string& key) const; | |
81 | |
82 void LazyInit(); | |
83 void InitStore(ServiceWorkerFetchStore* store); | |
84 | |
85 StoreMap loaded_store_ids_; | |
michaeln
2014/08/05 23:34:30
i think the name of this data member is misleading
jkarlin
2014/08/06 18:56:00
Done.
| |
86 NameMap loaded_store_names_; | |
87 bool initialized_; | |
57 base::FilePath origin_path_; | 88 base::FilePath origin_path_; |
58 BackendType backend_type_; | |
59 scoped_refptr<base::MessageLoopProxy> callback_loop_; | 89 scoped_refptr<base::MessageLoopProxy> callback_loop_; |
90 scoped_ptr<StoresLoader> stores_loader_; | |
60 | 91 |
61 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchStores); | 92 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchStores); |
62 }; | 93 }; |
63 | 94 |
64 } // namespace content | 95 } // namespace content |
65 | 96 |
66 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ | 97 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ |
OLD | NEW |