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