#include "base/callback.h"
#include "base/files/file_path.h"
-#include "base/id_map.h"
#include "base/memory/weak_ptr.h"
+#include "content/browser/service_worker/service_worker_cache.h"
namespace base {
class SequencedTaskRunner;
class URLRequestContext;
}
-namespace webkit_blob {
+namespace storage {
class BlobStorageContext;
}
namespace content {
-class ServiceWorkerCache;
// TODO(jkarlin): Constrain the total bytes used per origin.
// ServiceWorkerCacheStorage holds the set of caches for a given origin. It is
// owned by the ServiceWorkerCacheStorageManager. This class expects to be run
// on the IO thread.
-class ServiceWorkerCacheStorage {
+class CONTENT_EXPORT ServiceWorkerCacheStorage {
public:
enum CacheStorageError {
CACHE_STORAGE_ERROR_NO_ERROR,
CACHE_STORAGE_ERROR_NOT_FOUND,
CACHE_STORAGE_ERROR_EXISTS,
CACHE_STORAGE_ERROR_STORAGE,
- CACHE_STORAGE_ERROR_EMPTY_KEY,
+ CACHE_STORAGE_ERROR_CLOSING
};
-
+ typedef std::vector<std::string> StringVector;
typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback;
- typedef base::Callback<void(int, CacheStorageError)> CacheAndErrorCallback;
- typedef base::Callback<void(const std::vector<std::string>&,
- CacheStorageError)> StringsAndErrorCallback;
+ typedef base::Callback<void(const scoped_refptr<ServiceWorkerCache>&,
+ CacheStorageError)> CacheAndErrorCallback;
+ typedef base::Callback<void(const StringVector&, CacheStorageError)>
+ StringsAndErrorCallback;
ServiceWorkerCacheStorage(
const base::FilePath& origin_path,
bool memory_only,
base::SequencedTaskRunner* cache_task_runner,
net::URLRequestContext* request_context,
- base::WeakPtr<webkit_blob::BlobStorageContext> blob_context);
+ base::WeakPtr<storage::BlobStorageContext> blob_context);
virtual ~ServiceWorkerCacheStorage();
class SimpleCacheLoader;
class CacheLoader;
- typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap;
- typedef CacheMap::KeyType CacheID;
- typedef std::map<std::string, CacheID> NameMap;
+ typedef std::map<std::string, base::WeakPtr<ServiceWorkerCache> > CacheMap;
- ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const;
+ // Return a ServiceWorkerCache for the given name if the name is known. If the
+ // ServiceWorkerCache has been deleted, creates a new one.
+ scoped_refptr<ServiceWorkerCache> GetLoadedCache(
+ const std::string& cache_name);
- // Initializer and its callback are below.
+ // Initializer and its callback are below. While LazyInit is running any new
+ // operations will be queued and started in order after initialization.
void LazyInit(const base::Closure& closure);
void LazyInitDidLoadIndex(
const base::Closure& callback,
scoped_ptr<std::vector<std::string> > indexed_cache_names);
- void LazyInitIterateAndLoadCacheName(
- const base::Closure& callback,
- scoped_ptr<std::vector<std::string> > indexed_cache_names,
- const std::vector<std::string>::const_iterator& iter,
- scoped_ptr<ServiceWorkerCache> cache);
- void LazyInitDone();
- void DidCreateBackend(base::WeakPtr<ServiceWorkerCache> cache,
- const CacheAndErrorCallback& callback,
- bool success);
-
- void AddCacheToMaps(scoped_ptr<ServiceWorkerCache> cache);
+ void AddCacheToMap(const std::string& cache_name,
+ base::WeakPtr<ServiceWorkerCache> cache);
// The CreateCache callbacks are below.
- void CreateCacheDidCreateCache(const std::string& cache_name,
- const CacheAndErrorCallback& callback,
- scoped_ptr<ServiceWorkerCache> cache);
+ void CreateCacheDidCreateCache(
+ const std::string& cache_name,
+ const CacheAndErrorCallback& callback,
+ const scoped_refptr<ServiceWorkerCache>& cache);
void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback,
- base::WeakPtr<ServiceWorkerCache> cache,
+ const scoped_refptr<ServiceWorkerCache>& cache,
bool success);
// The DeleteCache callbacks are below.
// The list of operations waiting on initialization.
std::vector<base::Closure> init_callbacks_;
- // The map of ServiceWorkerCache objects to their integer ids that
- // ServiceWorkers reference. Owns the cache objects.
+ // The map of cache names to ServiceWorkerCache objects.
CacheMap cache_map_;
- // The map of cache names to their integer ids.
- NameMap name_map_;
+ // The names of caches in the order that they were created.
+ StringVector ordered_cache_names_;
// The file path for this CacheStorage.
base::FilePath origin_path_;
// The TaskRunner to run file IO on.
scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
+ // Whether or not to store data in disk or memory.
+ bool memory_only_;
+
// Performs backend specific operations (memory vs disk).
- scoped_refptr<CacheLoader> cache_loader_;
+ scoped_ptr<CacheLoader> cache_loader_;
base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_;