| 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_CACHE_STORAGE_MANAGER_H_ | |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "content/browser/service_worker/service_worker_cache_storage.h" | |
| 15 #include "content/common/content_export.h" | |
| 16 #include "storage/browser/quota/quota_client.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class SequencedTaskRunner; | |
| 21 } | |
| 22 | |
| 23 namespace net { | |
| 24 class URLRequestContext; | |
| 25 } | |
| 26 | |
| 27 namespace storage { | |
| 28 class BlobStorageContext; | |
| 29 class QuotaManagerProxy; | |
| 30 } | |
| 31 | |
| 32 namespace content { | |
| 33 | |
| 34 class ServiceWorkerCacheQuotaClient; | |
| 35 | |
| 36 // Keeps track of a ServiceWorkerCacheStorage per origin. There is one | |
| 37 // ServiceWorkerCacheStorageManager per ServiceWorkerContextCore. | |
| 38 // TODO(jkarlin): Remove ServiceWorkerCacheStorage from memory once they're no | |
| 39 // longer in active use. | |
| 40 class CONTENT_EXPORT ServiceWorkerCacheStorageManager { | |
| 41 public: | |
| 42 static scoped_ptr<ServiceWorkerCacheStorageManager> Create( | |
| 43 const base::FilePath& path, | |
| 44 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, | |
| 45 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy); | |
| 46 | |
| 47 static scoped_ptr<ServiceWorkerCacheStorageManager> Create( | |
| 48 ServiceWorkerCacheStorageManager* old_manager); | |
| 49 | |
| 50 virtual ~ServiceWorkerCacheStorageManager(); | |
| 51 | |
| 52 // Methods to support the CacheStorage spec. These methods call the | |
| 53 // corresponding ServiceWorkerCacheStorage method on the appropriate thread. | |
| 54 void OpenCache( | |
| 55 const GURL& origin, | |
| 56 const std::string& cache_name, | |
| 57 const ServiceWorkerCacheStorage::CacheAndErrorCallback& callback); | |
| 58 void HasCache( | |
| 59 const GURL& origin, | |
| 60 const std::string& cache_name, | |
| 61 const ServiceWorkerCacheStorage::BoolAndErrorCallback& callback); | |
| 62 void DeleteCache( | |
| 63 const GURL& origin, | |
| 64 const std::string& cache_name, | |
| 65 const ServiceWorkerCacheStorage::BoolAndErrorCallback& callback); | |
| 66 void EnumerateCaches( | |
| 67 const GURL& origin, | |
| 68 const ServiceWorkerCacheStorage::StringsAndErrorCallback& callback); | |
| 69 void MatchCache(const GURL& origin, | |
| 70 const std::string& cache_name, | |
| 71 scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 72 const ServiceWorkerCache::ResponseCallback& callback); | |
| 73 void MatchAllCaches(const GURL& origin, | |
| 74 scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 75 const ServiceWorkerCache::ResponseCallback& callback); | |
| 76 | |
| 77 // This must be called before creating any of the public *Cache functions | |
| 78 // above. | |
| 79 void SetBlobParametersForCache( | |
| 80 net::URLRequestContext* request_context, | |
| 81 base::WeakPtr<storage::BlobStorageContext> blob_storage_context); | |
| 82 | |
| 83 base::WeakPtr<ServiceWorkerCacheStorageManager> AsWeakPtr() { | |
| 84 return weak_ptr_factory_.GetWeakPtr(); | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 friend class ServiceWorkerCacheQuotaClient; | |
| 89 friend class ServiceWorkerCacheStorageManagerTest; | |
| 90 friend class ServiceWorkerCacheStorageMigrationTest; | |
| 91 | |
| 92 typedef std::map<GURL, ServiceWorkerCacheStorage*> | |
| 93 ServiceWorkerCacheStorageMap; | |
| 94 | |
| 95 ServiceWorkerCacheStorageManager( | |
| 96 const base::FilePath& path, | |
| 97 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, | |
| 98 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy); | |
| 99 | |
| 100 // The returned ServiceWorkerCacheStorage* is owned by | |
| 101 // service_worker_cache_storages_. | |
| 102 ServiceWorkerCacheStorage* FindOrCreateServiceWorkerCacheManager( | |
| 103 const GURL& origin); | |
| 104 | |
| 105 // QuotaClient support | |
| 106 void GetOriginUsage(const GURL& origin_url, | |
| 107 const storage::QuotaClient::GetUsageCallback& callback); | |
| 108 void GetOrigins(const storage::QuotaClient::GetOriginsCallback& callback); | |
| 109 void GetOriginsForHost( | |
| 110 const std::string& host, | |
| 111 const storage::QuotaClient::GetOriginsCallback& callback); | |
| 112 void DeleteOriginData(const GURL& origin, | |
| 113 const storage::QuotaClient::DeletionCallback& callback); | |
| 114 static void DeleteOriginDidClose( | |
| 115 const GURL& origin, | |
| 116 const storage::QuotaClient::DeletionCallback& callback, | |
| 117 scoped_ptr<ServiceWorkerCacheStorage> cache_storage, | |
| 118 base::WeakPtr<ServiceWorkerCacheStorageManager> cache_manager); | |
| 119 | |
| 120 net::URLRequestContext* url_request_context() const { | |
| 121 return request_context_; | |
| 122 } | |
| 123 base::WeakPtr<storage::BlobStorageContext> blob_storage_context() const { | |
| 124 return blob_context_; | |
| 125 } | |
| 126 base::FilePath root_path() const { return root_path_; } | |
| 127 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner() const { | |
| 128 return cache_task_runner_; | |
| 129 } | |
| 130 | |
| 131 bool IsMemoryBacked() const { return root_path_.empty(); } | |
| 132 | |
| 133 // Map a origin to the path. Exposed for testing. | |
| 134 static base::FilePath ConstructLegacyOriginPath( | |
| 135 const base::FilePath& root_path, | |
| 136 const GURL& origin); | |
| 137 // Map a database identifier (computed from an origin) to the path. Exposed | |
| 138 // for testing. | |
| 139 static base::FilePath ConstructOriginPath(const base::FilePath& root_path, | |
| 140 const GURL& origin); | |
| 141 | |
| 142 // Migrate from old origin-based path to storage identifier-based path. | |
| 143 // TODO(jsbell); Remove method and all calls after a few releases. | |
| 144 void MigrateOrigin(const GURL& origin); | |
| 145 static void MigrateOriginOnTaskRunner(const base::FilePath& old_path, | |
| 146 const base::FilePath& new_path); | |
| 147 | |
| 148 base::FilePath root_path_; | |
| 149 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | |
| 150 | |
| 151 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | |
| 152 | |
| 153 // The map owns the CacheStorages and the CacheStorages are only accessed on | |
| 154 // |cache_task_runner_|. | |
| 155 ServiceWorkerCacheStorageMap cache_storage_map_; | |
| 156 | |
| 157 net::URLRequestContext* request_context_; | |
| 158 base::WeakPtr<storage::BlobStorageContext> blob_context_; | |
| 159 | |
| 160 base::WeakPtrFactory<ServiceWorkerCacheStorageManager> weak_ptr_factory_; | |
| 161 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorageManager); | |
| 162 }; | |
| 163 | |
| 164 } // namespace content | |
| 165 | |
| 166 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_MANAGER_H
_ | |
| OLD | NEW |