Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_cache_storage.h
index 71037b4..cec6389 100644 (file)
@@ -10,8 +10,8 @@
 
 #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;
@@ -21,19 +21,18 @@ namespace net {
 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,
@@ -41,20 +40,21 @@ class ServiceWorkerCacheStorage {
     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();
 
@@ -88,36 +88,30 @@ class 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.
@@ -133,12 +127,11 @@ class ServiceWorkerCacheStorage {
   // 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_;
@@ -146,8 +139,11 @@ class ServiceWorkerCacheStorage {
   // 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_;