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 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 25 // The set of stores for a given origin. It is owned by the |
23 // ServiceWorkerFetchStoresManager. Provided callbacks are called on the | 26 // ServiceWorkerFetchStoresManager. Provided callbacks are called on the |
24 // |callback_loop| provided in the constructor. | 27 // |callback_loop| provided in the constructor. |
25 class ServiceWorkerFetchStores { | 28 class ServiceWorkerFetchStores { |
26 public: | 29 public: |
27 enum FetchStoresError { | 30 enum FetchStoresError { |
28 FETCH_STORES_ERROR_NO_ERROR, | 31 FETCH_STORES_ERROR_NO_ERROR, |
29 FETCH_STORES_ERROR_NOT_IMPLEMENTED, | 32 FETCH_STORES_ERROR_NOT_IMPLEMENTED, |
30 FETCH_STORES_ERROR_NOT_FOUND, | 33 FETCH_STORES_ERROR_NOT_FOUND, |
31 FETCH_STORES_ERROR_EXISTS, | 34 FETCH_STORES_ERROR_EXISTS, |
32 FETCH_STORES_ERROR_STORAGE | 35 FETCH_STORES_ERROR_STORAGE, |
| 36 FETCH_STORES_ERROR_EMPTY_KEY, |
33 }; | 37 }; |
34 | 38 |
35 enum BackendType { BACKEND_TYPE_SIMPLE_CACHE, BACKEND_TYPE_MEMORY }; | |
36 | |
37 typedef base::Callback<void(bool, FetchStoresError)> BoolAndErrorCallback; | 39 typedef base::Callback<void(bool, FetchStoresError)> BoolAndErrorCallback; |
38 typedef base::Callback<void(int, FetchStoresError)> StoreAndErrorCallback; | 40 typedef base::Callback<void(int, FetchStoresError)> StoreAndErrorCallback; |
39 typedef base::Callback<void(const std::vector<std::string>&, | 41 typedef base::Callback<void(const std::vector<std::string>&, |
40 FetchStoresError)> StringsAndErrorCallback; | 42 FetchStoresError)> StringsAndErrorCallback; |
41 | 43 |
42 ServiceWorkerFetchStores( | 44 ServiceWorkerFetchStores( |
43 const base::FilePath& origin_path, | 45 const base::FilePath& origin_path, |
44 BackendType backend, | 46 bool memory_only, |
45 const scoped_refptr<base::MessageLoopProxy>& callback_loop); | 47 const scoped_refptr<base::MessageLoopProxy>& callback_loop); |
| 48 |
46 virtual ~ServiceWorkerFetchStores(); | 49 virtual ~ServiceWorkerFetchStores(); |
47 | 50 |
| 51 // Create a ServiceWorkerFetchStore if it doesn't already exist and call the |
| 52 // callback with the cache's id. If it already |
| 53 // exists the callback is called with FETCH_STORES_ERROR_EXISTS. |
48 void CreateStore(const std::string& key, | 54 void CreateStore(const std::string& key, |
49 const StoreAndErrorCallback& callback); | 55 const StoreAndErrorCallback& callback); |
| 56 |
| 57 // Get the cache id for the given key. If not found returns |
| 58 // FETCH_STORES_ERROR_NOT_FOUND. |
50 void Get(const std::string& key, const StoreAndErrorCallback& callback); | 59 void Get(const std::string& key, const StoreAndErrorCallback& callback); |
51 void Has(const std::string& key, const BoolAndErrorCallback& callback) const; | 60 |
52 void Delete(const std::string& key, const StoreAndErrorCallback& callback); | 61 // Calls the callback with whether or not the cache exists. |
53 void Keys(const StringsAndErrorCallback& callback) const; | 62 void Has(const std::string& key, const BoolAndErrorCallback& callback); |
| 63 |
| 64 // Deletes the cache if it exists. If it doesn't exist, |
| 65 // FETCH_STORES_ERROR_NOT_FOUND is returned. |
| 66 void Delete(const std::string& key, const BoolAndErrorCallback& callback); |
| 67 |
| 68 // Calls the callback with a vector of cache names (keys) available. |
| 69 void Keys(const StringsAndErrorCallback& callback); |
| 70 |
54 // TODO(jkarlin): Add match() function. | 71 // TODO(jkarlin): Add match() function. |
55 | 72 |
| 73 void InitializeStoreCallback(const ServiceWorkerFetchStore* store, |
| 74 const StoreAndErrorCallback& callback, |
| 75 bool success); |
| 76 |
56 private: | 77 private: |
| 78 class MemoryLoader; |
| 79 class SimpleCacheLoader; |
| 80 class StoresLoader; |
| 81 |
| 82 typedef IDMap<ServiceWorkerFetchStore, IDMapOwnPointer> StoreMap; |
| 83 typedef StoreMap::KeyType StoreID; |
| 84 typedef std::map<std::string, StoreID> NameMap; |
| 85 |
| 86 ServiceWorkerFetchStore* GetLoadedStore(const std::string& key) const; |
| 87 |
| 88 void LazyInit(); |
| 89 void InitStore(ServiceWorkerFetchStore* store); |
| 90 |
| 91 bool initialized_; |
| 92 StoreMap store_map_; |
| 93 NameMap name_map_; |
57 base::FilePath origin_path_; | 94 base::FilePath origin_path_; |
58 BackendType backend_type_; | |
59 scoped_refptr<base::MessageLoopProxy> callback_loop_; | 95 scoped_refptr<base::MessageLoopProxy> callback_loop_; |
| 96 scoped_ptr<StoresLoader> stores_loader_; |
60 | 97 |
61 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchStores); | 98 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchStores); |
62 }; | 99 }; |
63 | 100 |
64 } // namespace content | 101 } // namespace content |
65 | 102 |
66 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ | 103 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ |
OLD | NEW |