Chromium Code Reviews| 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 #include "content/browser/cache_storage/cache_storage.h" | 5 #include "content/browser/cache_storage/cache_storage.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "base/numerics/safe_conversions.h" | 21 #include "base/numerics/safe_conversions.h" |
| 22 #include "base/sha1.h" | 22 #include "base/sha1.h" |
| 23 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
| 24 #include "base/stl_util.h" | 24 #include "base/stl_util.h" |
| 25 #include "base/strings/string_number_conversions.h" | 25 #include "base/strings/string_number_conversions.h" |
| 26 #include "base/strings/string_util.h" | 26 #include "base/strings/string_util.h" |
| 27 #include "base/threading/thread_task_runner_handle.h" | 27 #include "base/threading/thread_task_runner_handle.h" |
| 28 #include "content/browser/cache_storage/cache_storage.pb.h" | 28 #include "content/browser/cache_storage/cache_storage.pb.h" |
| 29 #include "content/browser/cache_storage/cache_storage_cache.h" | 29 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 30 #include "content/browser/cache_storage/cache_storage_cache_handle.h" | 30 #include "content/browser/cache_storage/cache_storage_cache_handle.h" |
| 31 #include "content/browser/cache_storage/cache_storage_index.h" | |
| 31 #include "content/browser/cache_storage/cache_storage_scheduler.h" | 32 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
| 32 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 33 #include "net/base/directory_lister.h" | 34 #include "net/base/directory_lister.h" |
| 34 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
| 35 #include "net/url_request/url_request_context_getter.h" | 36 #include "net/url_request/url_request_context_getter.h" |
| 36 #include "storage/browser/blob/blob_storage_context.h" | 37 #include "storage/browser/blob/blob_storage_context.h" |
| 37 #include "storage/browser/quota/quota_manager_proxy.h" | 38 #include "storage/browser/quota/quota_manager_proxy.h" |
| 38 | 39 |
| 39 namespace content { | 40 namespace content { |
| 40 | 41 |
| 41 namespace { | 42 namespace { |
| 42 | 43 |
| 43 std::string HexedHash(const std::string& value) { | 44 std::string HexedHash(const std::string& value) { |
| 44 std::string value_hash = base::SHA1HashString(value); | 45 std::string value_hash = base::SHA1HashString(value); |
| 45 std::string valued_hexed_hash = base::ToLowerASCII( | 46 std::string valued_hexed_hash = base::ToLowerASCII( |
| 46 base::HexEncode(value_hash.c_str(), value_hash.length())); | 47 base::HexEncode(value_hash.c_str(), value_hash.length())); |
| 47 return valued_hexed_hash; | 48 return valued_hexed_hash; |
| 48 } | 49 } |
| 49 | 50 |
| 50 void SizeRetrievedFromCache( | |
| 51 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | |
| 52 const base::Closure& closure, | |
| 53 int64_t* accumulator, | |
| 54 int64_t size) { | |
| 55 *accumulator += size; | |
| 56 closure.Run(); | |
| 57 } | |
| 58 | |
| 59 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator, | 51 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator, |
| 60 const CacheStorage::SizeCallback& callback) { | 52 const CacheStorage::SizeCallback& callback) { |
| 61 base::ThreadTaskRunnerHandle::Get()->PostTask( | 53 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 62 FROM_HERE, base::Bind(callback, *accumulator)); | 54 FROM_HERE, base::Bind(callback, *accumulator)); |
| 63 } | 55 } |
| 64 | 56 |
| 57 void DoNothingWithBool(bool success) {} | |
| 58 | |
| 65 } // namespace | 59 } // namespace |
| 66 | 60 |
| 67 const char CacheStorage::kIndexFileName[] = "index.txt"; | 61 const char CacheStorage::kIndexFileName[] = "index.txt"; |
| 62 const int64_t CacheStorage::kSizeUnknown; | |
| 68 | 63 |
| 69 struct CacheStorage::CacheMatchResponse { | 64 struct CacheStorage::CacheMatchResponse { |
| 70 CacheMatchResponse() = default; | 65 CacheMatchResponse() = default; |
| 71 ~CacheMatchResponse() = default; | 66 ~CacheMatchResponse() = default; |
| 72 | 67 |
| 73 CacheStorageError error; | 68 CacheStorageError error; |
| 74 std::unique_ptr<ServiceWorkerResponse> service_worker_response; | 69 std::unique_ptr<ServiceWorkerResponse> service_worker_response; |
| 75 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; | 70 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; |
| 76 }; | 71 }; |
| 77 | 72 |
| 78 // Handles the loading and clean up of CacheStorageCache objects. | 73 // Handles the loading and clean up of CacheStorageCache objects. |
| 79 class CacheStorage::CacheLoader { | 74 class CacheStorage::CacheLoader { |
| 80 public: | 75 public: |
| 81 typedef base::Callback<void(std::unique_ptr<CacheStorageCache>)> | 76 typedef base::Callback<void(std::unique_ptr<CacheStorageCache>)> |
| 82 CacheCallback; | 77 CacheCallback; |
| 83 typedef base::Callback<void(bool)> BoolCallback; | 78 typedef base::Callback<void(bool)> BoolCallback; |
| 84 typedef base::Callback<void(std::unique_ptr<std::vector<std::string>>)> | 79 using CacheStorageIndexLoadCallback = |
| 85 StringVectorCallback; | 80 base::Callback<void(std::unique_ptr<CacheStorageIndex>)>; |
| 86 | 81 |
| 87 CacheLoader( | 82 CacheLoader( |
| 88 base::SequencedTaskRunner* cache_task_runner, | 83 base::SequencedTaskRunner* cache_task_runner, |
| 89 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 84 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 90 storage::QuotaManagerProxy* quota_manager_proxy, | 85 storage::QuotaManagerProxy* quota_manager_proxy, |
| 91 base::WeakPtr<storage::BlobStorageContext> blob_context, | 86 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 92 CacheStorage* cache_storage, | 87 CacheStorage* cache_storage, |
| 93 const GURL& origin) | 88 const GURL& origin) |
| 94 : cache_task_runner_(cache_task_runner), | 89 : cache_task_runner_(cache_task_runner), |
| 95 request_context_getter_(request_context_getter), | 90 request_context_getter_(request_context_getter), |
| 96 quota_manager_proxy_(quota_manager_proxy), | 91 quota_manager_proxy_(quota_manager_proxy), |
| 97 blob_context_(blob_context), | 92 blob_context_(blob_context), |
| 98 cache_storage_(cache_storage), | 93 cache_storage_(cache_storage), |
| 99 origin_(origin) { | 94 origin_(origin) { |
| 100 DCHECK(!origin_.is_empty()); | 95 DCHECK(!origin_.is_empty()); |
| 101 } | 96 } |
| 102 | 97 |
| 103 virtual ~CacheLoader() {} | 98 virtual ~CacheLoader() {} |
| 104 | 99 |
| 105 // Creates a CacheStorageCache with the given name. It does not attempt to | 100 // Creates a CacheStorageCache with the given name. It does not attempt to |
| 106 // load the backend, that happens lazily when the cache is used. | 101 // load the backend, that happens lazily when the cache is used. |
| 107 virtual std::unique_ptr<CacheStorageCache> CreateCache( | 102 virtual std::unique_ptr<CacheStorageCache> CreateCache( |
| 108 const std::string& cache_name) = 0; | 103 const std::string& cache_name, |
| 104 int64_t cache_size) = 0; | |
| 109 | 105 |
| 110 // Deletes any pre-existing cache of the same name and then loads it. | 106 // Deletes any pre-existing cache of the same name and then loads it. |
| 111 virtual void PrepareNewCacheDestination(const std::string& cache_name, | 107 virtual void PrepareNewCacheDestination(const std::string& cache_name, |
| 112 const CacheCallback& callback) = 0; | 108 const CacheCallback& callback) = 0; |
| 113 | 109 |
| 114 // After the backend has been deleted, do any extra house keeping such as | 110 // After the backend has been deleted, do any extra house keeping such as |
| 115 // removing the cache's directory. | 111 // removing the cache's directory. |
| 116 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; | 112 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; |
| 117 | 113 |
| 118 // Writes the cache names (and sizes) to disk if applicable. | 114 // Writes the cache index to disk if applicable. |
| 119 virtual void WriteIndex(const StringVector& cache_names, | 115 virtual void WriteIndex(const CacheStorageIndex& index, |
| 120 const BoolCallback& callback) = 0; | 116 const BoolCallback& callback) = 0; |
| 121 | 117 |
| 122 // Loads the cache names from disk if applicable. | 118 // Loads the cache index from disk if applicable. |
| 123 virtual void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, | 119 virtual void LoadIndex(const CacheStorageIndexLoadCallback& callback) = 0; |
| 124 const StringVectorCallback& callback) = 0; | |
| 125 | 120 |
| 126 // Called when CacheStorage has created a cache. Used to hold onto a handle to | 121 // Called when CacheStorage has created a cache. Used to hold onto a handle to |
| 127 // the cache if necessary. | 122 // the cache if necessary. |
| 128 virtual void NotifyCacheCreated( | 123 virtual void NotifyCacheCreated( |
| 129 const std::string& cache_name, | 124 const std::string& cache_name, |
| 130 std::unique_ptr<CacheStorageCacheHandle> cache_handle) {} | 125 std::unique_ptr<CacheStorageCacheHandle> cache_handle) {} |
| 131 | 126 |
| 132 // Notification that the cache for |cache_handle| has been doomed. If the | 127 // Notification that the cache for |cache_handle| has been doomed. If the |
| 133 // loader is holding a handle to the cache, it should drop it now. | 128 // loader is holding a handle to the cache, it should drop it now. |
| 134 virtual void NotifyCacheDoomed( | 129 virtual void NotifyCacheDoomed( |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 161 base::WeakPtr<storage::BlobStorageContext> blob_context, | 156 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 162 CacheStorage* cache_storage, | 157 CacheStorage* cache_storage, |
| 163 const GURL& origin) | 158 const GURL& origin) |
| 164 : CacheLoader(cache_task_runner, | 159 : CacheLoader(cache_task_runner, |
| 165 request_context, | 160 request_context, |
| 166 quota_manager_proxy, | 161 quota_manager_proxy, |
| 167 blob_context, | 162 blob_context, |
| 168 cache_storage, | 163 cache_storage, |
| 169 origin) {} | 164 origin) {} |
| 170 | 165 |
| 171 std::unique_ptr<CacheStorageCache> CreateCache( | 166 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, |
| 172 const std::string& cache_name) override { | 167 int64_t cache_size) override { |
| 173 return CacheStorageCache::CreateMemoryCache( | 168 return CacheStorageCache::CreateMemoryCache( |
| 174 origin_, cache_name, cache_storage_, request_context_getter_, | 169 origin_, cache_name, cache_storage_, request_context_getter_, |
| 175 quota_manager_proxy_, blob_context_); | 170 quota_manager_proxy_, blob_context_); |
| 176 } | 171 } |
| 177 | 172 |
| 178 void PrepareNewCacheDestination(const std::string& cache_name, | 173 void PrepareNewCacheDestination(const std::string& cache_name, |
| 179 const CacheCallback& callback) override { | 174 const CacheCallback& callback) override { |
| 180 std::unique_ptr<CacheStorageCache> cache = CreateCache(cache_name); | 175 std::unique_ptr<CacheStorageCache> cache = |
| 176 CreateCache(cache_name, 0 /*cache_size*/); | |
| 181 callback.Run(std::move(cache)); | 177 callback.Run(std::move(cache)); |
| 182 } | 178 } |
| 183 | 179 |
| 184 void CleanUpDeletedCache(CacheStorageCache* cache) override {} | 180 void CleanUpDeletedCache(CacheStorageCache* cache) override {} |
| 185 | 181 |
| 186 void WriteIndex(const StringVector& cache_names, | 182 void WriteIndex(const CacheStorageIndex& index, |
| 187 const BoolCallback& callback) override { | 183 const BoolCallback& callback) override { |
| 188 callback.Run(true); | 184 callback.Run(true); |
| 189 } | 185 } |
| 190 | 186 |
| 191 void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, | 187 void LoadIndex(const CacheStorageIndexLoadCallback& callback) override { |
| 192 const StringVectorCallback& callback) override { | 188 callback.Run(base::MakeUnique<CacheStorageIndex>()); |
| 193 callback.Run(std::move(cache_names)); | |
| 194 } | 189 } |
| 195 | 190 |
| 196 void NotifyCacheCreated( | 191 void NotifyCacheCreated( |
| 197 const std::string& cache_name, | 192 const std::string& cache_name, |
| 198 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { | 193 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { |
| 199 DCHECK(!base::ContainsKey(cache_handles_, cache_name)); | 194 DCHECK(!base::ContainsKey(cache_handles_, cache_name)); |
| 200 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle))); | 195 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle))); |
| 201 }; | 196 }; |
| 202 | 197 |
| 203 void NotifyCacheDoomed( | 198 void NotifyCacheDoomed( |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 229 const GURL& origin) | 224 const GURL& origin) |
| 230 : CacheLoader(cache_task_runner, | 225 : CacheLoader(cache_task_runner, |
| 231 request_context, | 226 request_context, |
| 232 quota_manager_proxy, | 227 quota_manager_proxy, |
| 233 blob_context, | 228 blob_context, |
| 234 cache_storage, | 229 cache_storage, |
| 235 origin), | 230 origin), |
| 236 origin_path_(origin_path), | 231 origin_path_(origin_path), |
| 237 weak_ptr_factory_(this) {} | 232 weak_ptr_factory_(this) {} |
| 238 | 233 |
| 239 std::unique_ptr<CacheStorageCache> CreateCache( | 234 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, |
| 240 const std::string& cache_name) override { | 235 int64_t cache_size) override { |
| 241 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 236 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 242 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_name)); | 237 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_name)); |
| 243 | 238 |
| 244 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; | 239 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; |
| 245 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); | 240 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); |
| 246 return CacheStorageCache::CreatePersistentCache( | 241 return CacheStorageCache::CreatePersistentCache( |
| 247 origin_, cache_name, cache_storage_, cache_path, | 242 origin_, cache_name, cache_storage_, cache_path, |
| 248 request_context_getter_, quota_manager_proxy_, blob_context_); | 243 request_context_getter_, quota_manager_proxy_, blob_context_, |
| 244 cache_size); | |
| 249 } | 245 } |
| 250 | 246 |
| 251 void PrepareNewCacheDestination(const std::string& cache_name, | 247 void PrepareNewCacheDestination(const std::string& cache_name, |
| 252 const CacheCallback& callback) override { | 248 const CacheCallback& callback) override { |
| 253 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 249 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 254 | 250 |
| 255 PostTaskAndReplyWithResult( | 251 PostTaskAndReplyWithResult( |
| 256 cache_task_runner_.get(), FROM_HERE, | 252 cache_task_runner_.get(), FROM_HERE, |
| 257 base::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, | 253 base::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, |
| 258 origin_path_), | 254 origin_path_), |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 275 | 271 |
| 276 void PrepareNewCacheCreateCache(const std::string& cache_name, | 272 void PrepareNewCacheCreateCache(const std::string& cache_name, |
| 277 const CacheCallback& callback, | 273 const CacheCallback& callback, |
| 278 const std::string& cache_dir) { | 274 const std::string& cache_dir) { |
| 279 if (cache_dir.empty()) { | 275 if (cache_dir.empty()) { |
| 280 callback.Run(std::unique_ptr<CacheStorageCache>()); | 276 callback.Run(std::unique_ptr<CacheStorageCache>()); |
| 281 return; | 277 return; |
| 282 } | 278 } |
| 283 | 279 |
| 284 cache_name_to_cache_dir_[cache_name] = cache_dir; | 280 cache_name_to_cache_dir_[cache_name] = cache_dir; |
| 285 callback.Run(CreateCache(cache_name)); | 281 callback.Run(CreateCache(cache_name, CacheStorage::kSizeUnknown)); |
| 286 } | 282 } |
| 287 | 283 |
| 288 void CleanUpDeletedCache(CacheStorageCache* cache) override { | 284 void CleanUpDeletedCache(CacheStorageCache* cache) override { |
| 289 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 285 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 290 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); | 286 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); |
| 291 | 287 |
| 292 base::FilePath cache_path = | 288 base::FilePath cache_path = |
| 293 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); | 289 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); |
| 294 doomed_cache_to_path_.erase(cache); | 290 doomed_cache_to_path_.erase(cache); |
| 295 | 291 |
| 296 cache_task_runner_->PostTask( | 292 cache_task_runner_->PostTask( |
| 297 FROM_HERE, base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, | 293 FROM_HERE, base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, |
| 298 cache_path)); | 294 cache_path)); |
| 299 } | 295 } |
| 300 | 296 |
| 301 static void CleanUpDeleteCacheDirInPool(const base::FilePath& cache_path) { | 297 static void CleanUpDeleteCacheDirInPool(const base::FilePath& cache_path) { |
| 302 base::DeleteFile(cache_path, true /* recursive */); | 298 base::DeleteFile(cache_path, true /* recursive */); |
| 303 } | 299 } |
| 304 | 300 |
| 305 void WriteIndex(const StringVector& cache_names, | 301 void WriteIndex(const CacheStorageIndex& index, |
| 306 const BoolCallback& callback) override { | 302 const BoolCallback& callback) override { |
| 307 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 303 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 308 | 304 |
| 309 // 1. Create the index file as a string. (WriteIndex) | 305 // 1. Create the index file as a string. (WriteIndex) |
| 310 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) | 306 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) |
| 311 | 307 |
| 312 proto::CacheStorageIndex index; | 308 proto::CacheStorageIndex protobuf_index; |
| 313 index.set_origin(origin_.spec()); | 309 protobuf_index.set_origin(origin_.spec()); |
| 314 | 310 |
| 315 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { | 311 for (const auto& cache_metadata : index.ordered_cache_metadata()) { |
| 316 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_names[i])); | 312 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_metadata.name)); |
| 317 | 313 |
| 318 proto::CacheStorageIndex::Cache* index_cache = index.add_cache(); | 314 proto::CacheStorageIndex::Cache* index_cache = protobuf_index.add_cache(); |
| 319 index_cache->set_name(cache_names[i]); | 315 index_cache->set_name(cache_metadata.name); |
| 320 index_cache->set_cache_dir(cache_name_to_cache_dir_[cache_names[i]]); | 316 index_cache->set_cache_dir(cache_name_to_cache_dir_[cache_metadata.name]); |
| 317 if (cache_metadata.size == CacheStorage::kSizeUnknown) | |
| 318 index_cache->clear_size(); | |
| 319 else | |
| 320 index_cache->set_size(cache_metadata.size); | |
| 321 } | 321 } |
| 322 | 322 |
| 323 std::string serialized; | 323 std::string serialized; |
| 324 bool success = index.SerializeToString(&serialized); | 324 bool success = protobuf_index.SerializeToString(&serialized); |
| 325 DCHECK(success); | 325 DCHECK(success); |
| 326 | 326 |
| 327 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); | 327 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); |
| 328 base::FilePath index_path = | 328 base::FilePath index_path = |
| 329 origin_path_.AppendASCII(CacheStorage::kIndexFileName); | 329 origin_path_.AppendASCII(CacheStorage::kIndexFileName); |
| 330 | 330 |
| 331 PostTaskAndReplyWithResult( | 331 PostTaskAndReplyWithResult( |
| 332 cache_task_runner_.get(), FROM_HERE, | 332 cache_task_runner_.get(), FROM_HERE, |
| 333 base::Bind(&SimpleCacheLoader::WriteIndexWriteToFileInPool, tmp_path, | 333 base::Bind(&SimpleCacheLoader::WriteIndexWriteToFileInPool, tmp_path, |
| 334 index_path, serialized), | 334 index_path, serialized), |
| 335 callback); | 335 callback); |
| 336 } | 336 } |
| 337 | 337 |
| 338 static bool WriteIndexWriteToFileInPool(const base::FilePath& tmp_path, | 338 static bool WriteIndexWriteToFileInPool(const base::FilePath& tmp_path, |
| 339 const base::FilePath& index_path, | 339 const base::FilePath& index_path, |
| 340 const std::string& data) { | 340 const std::string& data) { |
| 341 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size()); | 341 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size()); |
| 342 if (bytes_written != base::checked_cast<int>(data.size())) { | 342 if (bytes_written != base::checked_cast<int>(data.size())) { |
| 343 base::DeleteFile(tmp_path, /* recursive */ false); | 343 base::DeleteFile(tmp_path, /* recursive */ false); |
| 344 return false; | 344 return false; |
| 345 } | 345 } |
| 346 | 346 |
| 347 // Atomically rename the temporary index file to become the real one. | 347 // Atomically rename the temporary index file to become the real one. |
| 348 return base::ReplaceFile(tmp_path, index_path, NULL); | 348 return base::ReplaceFile(tmp_path, index_path, NULL); |
| 349 } | 349 } |
| 350 | 350 |
| 351 void LoadIndex(std::unique_ptr<std::vector<std::string>> names, | 351 void LoadIndex(const CacheStorageIndexLoadCallback& callback) override { |
| 352 const StringVectorCallback& callback) override { | |
| 353 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 352 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 354 | 353 |
| 355 // 1. Read the file from disk. (LoadIndexReadFileInPool) | |
| 356 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) | |
| 357 | |
| 358 base::FilePath index_path = | |
| 359 origin_path_.AppendASCII(CacheStorage::kIndexFileName); | |
| 360 | |
| 361 PostTaskAndReplyWithResult( | 354 PostTaskAndReplyWithResult( |
| 362 cache_task_runner_.get(), FROM_HERE, | 355 cache_task_runner_.get(), FROM_HERE, |
| 363 base::Bind(&SimpleCacheLoader::ReadAndMigrateIndexInPool, index_path), | 356 base::Bind(&SimpleCacheLoader::ReadAndMigrateIndexInPool, origin_path_), |
| 364 base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, | 357 base::Bind(&SimpleCacheLoader::LoadIndexDidReadIndex, |
| 365 weak_ptr_factory_.GetWeakPtr(), base::Passed(&names), | 358 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 366 callback)); | |
| 367 } | 359 } |
| 368 | 360 |
| 369 void LoadIndexDidReadFile(std::unique_ptr<std::vector<std::string>> names, | 361 void LoadIndexDidReadIndex(const CacheStorageIndexLoadCallback& callback, |
| 370 const StringVectorCallback& callback, | 362 proto::CacheStorageIndex protobuf_index) { |
| 371 const std::string& serialized) { | |
| 372 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 363 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 373 | 364 |
| 374 std::unique_ptr<std::set<std::string>> cache_dirs( | 365 std::unique_ptr<std::set<std::string>> cache_dirs( |
| 375 new std::set<std::string>); | 366 new std::set<std::string>); |
| 376 | 367 |
| 377 proto::CacheStorageIndex index; | 368 auto index = base::MakeUnique<CacheStorageIndex>(); |
| 378 if (index.ParseFromString(serialized)) { | 369 for (int i = 0, max = protobuf_index.cache_size(); i < max; ++i) { |
| 379 for (int i = 0, max = index.cache_size(); i < max; ++i) { | 370 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); |
| 380 const proto::CacheStorageIndex::Cache& cache = index.cache(i); | 371 DCHECK(cache.has_cache_dir()); |
| 381 DCHECK(cache.has_cache_dir()); | 372 int64_t cache_size = |
| 382 names->push_back(cache.name()); | 373 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; |
| 383 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); | 374 index->Insert(CacheStorageIndex::CacheMetadata(cache.name(), cache_size)); |
| 384 cache_dirs->insert(cache.cache_dir()); | 375 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); |
| 385 } | 376 cache_dirs->insert(cache.cache_dir()); |
| 386 } | 377 } |
| 387 | 378 |
| 388 cache_task_runner_->PostTask( | 379 cache_task_runner_->PostTask( |
| 389 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, | 380 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, |
| 390 base::Passed(&cache_dirs))); | 381 base::Passed(&cache_dirs))); |
| 391 callback.Run(std::move(names)); | 382 callback.Run(std::move(index)); |
| 392 } | 383 } |
| 393 | 384 |
| 394 void NotifyCacheDoomed( | 385 void NotifyCacheDoomed( |
| 395 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { | 386 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { |
| 396 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, | 387 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, |
| 397 cache_handle->value()->cache_name())); | 388 cache_handle->value()->cache_name())); |
| 398 auto iter = | 389 auto iter = |
| 399 cache_name_to_cache_dir_.find(cache_handle->value()->cache_name()); | 390 cache_name_to_cache_dir_.find(cache_handle->value()->cache_name()); |
| 400 doomed_cache_to_path_[cache_handle->value()] = iter->second; | 391 doomed_cache_to_path_[cache_handle->value()] = iter->second; |
| 401 cache_name_to_cache_dir_.erase(iter); | 392 cache_name_to_cache_dir_.erase(iter); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 417 while (!(cache_path = file_enum.Next()).empty()) { | 408 while (!(cache_path = file_enum.Next()).empty()) { |
| 418 if (!base::ContainsKey(*cache_dirs, cache_path.BaseName().AsUTF8Unsafe())) | 409 if (!base::ContainsKey(*cache_dirs, cache_path.BaseName().AsUTF8Unsafe())) |
| 419 dirs_to_delete.push_back(cache_path); | 410 dirs_to_delete.push_back(cache_path); |
| 420 } | 411 } |
| 421 | 412 |
| 422 for (const base::FilePath& cache_path : dirs_to_delete) | 413 for (const base::FilePath& cache_path : dirs_to_delete) |
| 423 base::DeleteFile(cache_path, true /* recursive */); | 414 base::DeleteFile(cache_path, true /* recursive */); |
| 424 } | 415 } |
| 425 | 416 |
| 426 // Runs on cache_task_runner_ | 417 // Runs on cache_task_runner_ |
| 427 static std::string MigrateCachesIfNecessaryInPool( | 418 static proto::CacheStorageIndex ReadAndMigrateIndexInPool( |
| 428 const std::string& body, | 419 const base::FilePath& origin_path) { |
| 429 const base::FilePath& index_path) { | 420 const base::FilePath index_path = |
| 421 origin_path.AppendASCII(CacheStorage::kIndexFileName); | |
| 422 | |
| 430 proto::CacheStorageIndex index; | 423 proto::CacheStorageIndex index; |
| 431 if (!index.ParseFromString(body)) | 424 std::string body; |
| 432 return body; | 425 if (!base::ReadFileToString(index_path, &body) || |
| 426 !index.ParseFromString(body)) | |
| 427 return proto::CacheStorageIndex(); | |
| 428 body.clear(); | |
| 433 | 429 |
| 434 base::FilePath origin_path = index_path.DirName(); | 430 base::File::Info file_info; |
| 435 bool index_is_dirty = false; | 431 base::Time index_last_modified; |
| 436 const std::string kBadIndexState(""); | 432 if (GetFileInfo(index_path, &file_info)) |
| 433 index_last_modified = file_info.last_modified; | |
| 434 bool index_modified = false; | |
| 437 | 435 |
| 438 // Look for caches that have no cache_dir. Give any such caches a directory | 436 // Look for caches that have no cache_dir. Give any such caches a directory |
| 439 // with a random name and move them there. Then, rewrite the index file. | 437 // with a random name and move them there. Then, rewrite the index file. |
| 438 // Additionally invalidate any index entries where the cache was modified | |
| 439 // after the index (making it out-of-date) | |
| 440 for (int i = 0, max = index.cache_size(); i < max; ++i) { | 440 for (int i = 0, max = index.cache_size(); i < max; ++i) { |
| 441 const proto::CacheStorageIndex::Cache& cache = index.cache(i); | 441 const proto::CacheStorageIndex::Cache& cache = index.cache(i); |
| 442 if (!cache.has_cache_dir()) { | 442 if (cache.has_cache_dir()) { |
| 443 if (cache.has_size()) { | |
| 444 base::FilePath cache_dir = origin_path.AppendASCII(cache.cache_dir()); | |
| 445 if (!GetFileInfo(cache_dir, &file_info) || | |
| 446 index_last_modified <= file_info.last_modified) { | |
| 447 // Index is older than this cache, so invalidate index entries that | |
| 448 // may change as a result of cache operations. | |
| 449 index.mutable_cache(i)->clear_size(); | |
| 450 } | |
| 451 } | |
| 452 } else { | |
| 443 // Find a new home for the cache. | 453 // Find a new home for the cache. |
| 444 base::FilePath legacy_cache_path = | 454 base::FilePath legacy_cache_path = |
| 445 origin_path.AppendASCII(HexedHash(cache.name())); | 455 origin_path.AppendASCII(HexedHash(cache.name())); |
| 446 std::string cache_dir; | 456 std::string cache_dir; |
| 447 base::FilePath cache_path; | 457 base::FilePath cache_path; |
| 448 do { | 458 do { |
| 449 cache_dir = base::GenerateGUID(); | 459 cache_dir = base::GenerateGUID(); |
| 450 cache_path = origin_path.AppendASCII(cache_dir); | 460 cache_path = origin_path.AppendASCII(cache_dir); |
| 451 } while (base::PathExists(cache_path)); | 461 } while (base::PathExists(cache_path)); |
| 452 | 462 |
| 453 if (!base::Move(legacy_cache_path, cache_path)) { | 463 if (!base::Move(legacy_cache_path, cache_path)) { |
| 454 // If the move fails then the cache is in a bad state. Return an empty | 464 // If the move fails then the cache is in a bad state. Return an empty |
| 455 // index so that the CacheStorage can start fresh. The unreferenced | 465 // index so that the CacheStorage can start fresh. The unreferenced |
| 456 // caches will be discarded later in initialization. | 466 // caches will be discarded later in initialization. |
| 457 return kBadIndexState; | 467 return proto::CacheStorageIndex(); |
| 458 } | 468 } |
| 459 | 469 |
| 460 index.mutable_cache(i)->set_cache_dir(cache_dir); | 470 index.mutable_cache(i)->set_cache_dir(cache_dir); |
| 461 index_is_dirty = true; | 471 index.mutable_cache(i)->clear_size(); |
| 472 index_modified = true; | |
| 462 } | 473 } |
| 463 } | 474 } |
| 464 | 475 |
| 465 if (index_is_dirty) { | 476 if (index_modified) { |
| 466 std::string new_body; | 477 if (!index.SerializeToString(&body)) |
| 467 if (!index.SerializeToString(&new_body)) | 478 return proto::CacheStorageIndex(); |
| 468 return kBadIndexState; | 479 if (base::WriteFile(index_path, body.c_str(), body.size()) != |
| 469 if (base::WriteFile(index_path, new_body.c_str(), new_body.size()) != | 480 base::checked_cast<int>(body.size())) |
| 470 base::checked_cast<int>(new_body.size())) | 481 return proto::CacheStorageIndex(); |
| 471 return kBadIndexState; | |
| 472 return new_body; | |
| 473 } | 482 } |
| 474 | 483 |
| 475 return body; | 484 return index; |
| 476 } | |
| 477 | |
| 478 // Runs on cache_task_runner_ | |
| 479 static std::string ReadAndMigrateIndexInPool( | |
| 480 const base::FilePath& index_path) { | |
| 481 std::string body; | |
| 482 base::ReadFileToString(index_path, &body); | |
| 483 | |
| 484 return MigrateCachesIfNecessaryInPool(body, index_path); | |
| 485 } | 485 } |
| 486 | 486 |
| 487 const base::FilePath origin_path_; | 487 const base::FilePath origin_path_; |
| 488 std::map<std::string, std::string> cache_name_to_cache_dir_; | 488 std::map<std::string, std::string> cache_name_to_cache_dir_; |
| 489 std::map<CacheStorageCache*, std::string> doomed_cache_to_path_; | 489 std::map<CacheStorageCache*, std::string> doomed_cache_to_path_; |
| 490 | 490 |
| 491 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_; | 491 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_; |
| 492 }; | 492 }; |
| 493 | 493 |
| 494 CacheStorage::CacheStorage( | 494 CacheStorage::CacheStorage( |
| 495 const base::FilePath& path, | 495 const base::FilePath& path, |
| 496 bool memory_only, | 496 bool memory_only, |
| 497 base::SequencedTaskRunner* cache_task_runner, | 497 base::SequencedTaskRunner* cache_task_runner, |
| 498 scoped_refptr<net::URLRequestContextGetter> request_context, | 498 scoped_refptr<net::URLRequestContextGetter> request_context, |
| 499 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 499 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| 500 base::WeakPtr<storage::BlobStorageContext> blob_context, | 500 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 501 const GURL& origin) | 501 const GURL& origin) |
| 502 : initialized_(false), | 502 : initialized_(false), |
| 503 initializing_(false), | 503 initializing_(false), |
| 504 memory_only_(memory_only), | 504 memory_only_(memory_only), |
| 505 scheduler_(new CacheStorageScheduler( | 505 scheduler_(new CacheStorageScheduler( |
| 506 CacheStorageSchedulerClient::CLIENT_STORAGE)), | 506 CacheStorageSchedulerClient::CLIENT_STORAGE)), |
| 507 cache_index_(base::MakeUnique<CacheStorageIndex>()), | |
| 507 origin_path_(path), | 508 origin_path_(path), |
| 508 cache_task_runner_(cache_task_runner), | 509 cache_task_runner_(cache_task_runner), |
| 509 quota_manager_proxy_(quota_manager_proxy), | 510 quota_manager_proxy_(quota_manager_proxy), |
| 510 origin_(origin), | 511 origin_(origin), |
| 512 index_write_pending_(false), | |
| 511 weak_factory_(this) { | 513 weak_factory_(this) { |
| 512 if (memory_only) | 514 if (memory_only) |
| 513 cache_loader_.reset(new MemoryLoader( | 515 cache_loader_.reset(new MemoryLoader( |
| 514 cache_task_runner_.get(), std::move(request_context), | 516 cache_task_runner_.get(), std::move(request_context), |
| 515 quota_manager_proxy.get(), blob_context, this, origin)); | 517 quota_manager_proxy.get(), blob_context, this, origin)); |
| 516 else | 518 else |
| 517 cache_loader_.reset(new SimpleCacheLoader( | 519 cache_loader_.reset(new SimpleCacheLoader( |
| 518 origin_path_, cache_task_runner_.get(), std::move(request_context), | 520 origin_path_, cache_task_runner_.get(), std::move(request_context), |
| 519 quota_manager_proxy.get(), blob_context, this, origin)); | 521 quota_manager_proxy.get(), blob_context, this, origin)); |
| 520 } | 522 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 563 | 565 |
| 564 quota_manager_proxy_->NotifyStorageAccessed( | 566 quota_manager_proxy_->NotifyStorageAccessed( |
| 565 storage::QuotaClient::kServiceWorkerCache, origin_, | 567 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 566 storage::kStorageTypeTemporary); | 568 storage::kStorageTypeTemporary); |
| 567 | 569 |
| 568 scheduler_->ScheduleOperation( | 570 scheduler_->ScheduleOperation( |
| 569 base::Bind(&CacheStorage::DeleteCacheImpl, weak_factory_.GetWeakPtr(), | 571 base::Bind(&CacheStorage::DeleteCacheImpl, weak_factory_.GetWeakPtr(), |
| 570 cache_name, scheduler_->WrapCallbackToRunNext(callback))); | 572 cache_name, scheduler_->WrapCallbackToRunNext(callback))); |
| 571 } | 573 } |
| 572 | 574 |
| 573 void CacheStorage::EnumerateCaches(const StringsCallback& callback) { | 575 void CacheStorage::EnumerateCaches(const IndexCallback& callback) { |
| 574 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 576 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 575 | 577 |
| 576 if (!initialized_) | 578 if (!initialized_) |
| 577 LazyInit(); | 579 LazyInit(); |
| 578 | 580 |
| 579 quota_manager_proxy_->NotifyStorageAccessed( | 581 quota_manager_proxy_->NotifyStorageAccessed( |
| 580 storage::QuotaClient::kServiceWorkerCache, origin_, | 582 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 581 storage::kStorageTypeTemporary); | 583 storage::kStorageTypeTemporary); |
| 582 | 584 |
| 583 scheduler_->ScheduleOperation( | 585 scheduler_->ScheduleOperation( |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 639 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 641 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 640 | 642 |
| 641 if (!initialized_) | 643 if (!initialized_) |
| 642 LazyInit(); | 644 LazyInit(); |
| 643 | 645 |
| 644 scheduler_->ScheduleOperation( | 646 scheduler_->ScheduleOperation( |
| 645 base::Bind(&CacheStorage::SizeImpl, weak_factory_.GetWeakPtr(), | 647 base::Bind(&CacheStorage::SizeImpl, weak_factory_.GetWeakPtr(), |
| 646 scheduler_->WrapCallbackToRunNext(callback))); | 648 scheduler_->WrapCallbackToRunNext(callback))); |
| 647 } | 649 } |
| 648 | 650 |
| 651 void CacheStorage::ScheduleWriteIndex() { | |
| 652 static const int64_t kWriteIndexDelay = 5; | |
| 653 index_write_task_.Reset( | |
| 654 base::Bind(&CacheStorage::WriteIndex, weak_factory_.GetWeakPtr())); | |
| 655 cache_task_runner_->PostDelayedTask( | |
| 656 FROM_HERE, index_write_task_.callback(), | |
| 657 base::TimeDelta::FromSeconds(kWriteIndexDelay)); | |
| 658 index_write_pending_ = true; | |
| 659 } | |
| 660 | |
| 661 void CacheStorage::WriteIndex() { | |
| 662 // TODO(cmumford): Write the index in between other cache writes as | |
|
jkarlin
2016/12/01 17:43:31
This TODO should be done now, otherwise we could w
cmumford
2016/12/05 17:19:02
Done.
| |
| 663 // modification-times are used for out-of-date checks. | |
| 664 index_write_pending_ = false; | |
| 665 cache_loader_->WriteIndex(*cache_index_, base::Bind(&DoNothingWithBool)); | |
| 666 } | |
| 667 | |
| 668 bool CacheStorage::InitiateScheduledIndexWriteForTest() { | |
| 669 if (index_write_pending_) { | |
| 670 index_write_task_.Cancel(); | |
| 671 WriteIndex(); | |
| 672 return true; | |
| 673 } | |
| 674 return false; | |
| 675 } | |
| 676 | |
| 677 void CacheStorage::CacheSizeUpdated(const CacheStorageCache* cache, | |
| 678 int64_t size) { | |
| 679 cache_index_->SetCacheSize(cache->cache_name(), size); | |
| 680 ScheduleWriteIndex(); | |
| 681 } | |
| 682 | |
| 649 void CacheStorage::StartAsyncOperationForTesting() { | 683 void CacheStorage::StartAsyncOperationForTesting() { |
| 650 scheduler_->ScheduleOperation(base::Bind(&base::DoNothing)); | 684 scheduler_->ScheduleOperation(base::Bind(&base::DoNothing)); |
| 651 } | 685 } |
| 652 | 686 |
| 653 void CacheStorage::CompleteAsyncOperationForTesting() { | 687 void CacheStorage::CompleteAsyncOperationForTesting() { |
| 654 scheduler_->CompleteOperationAndRunNext(); | 688 scheduler_->CompleteOperationAndRunNext(); |
| 655 } | 689 } |
| 656 | 690 |
| 657 // Init is run lazily so that it is called on the proper MessageLoop. | 691 // Init is run lazily so that it is called on the proper MessageLoop. |
| 658 void CacheStorage::LazyInit() { | 692 void CacheStorage::LazyInit() { |
| 659 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 693 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 660 DCHECK(!initialized_); | 694 DCHECK(!initialized_); |
| 661 | 695 |
| 662 if (initializing_) | 696 if (initializing_) |
| 663 return; | 697 return; |
| 664 | 698 |
| 665 DCHECK(!scheduler_->ScheduledOperations()); | 699 DCHECK(!scheduler_->ScheduledOperations()); |
| 666 | 700 |
| 667 initializing_ = true; | 701 initializing_ = true; |
| 668 scheduler_->ScheduleOperation( | 702 scheduler_->ScheduleOperation( |
| 669 base::Bind(&CacheStorage::LazyInitImpl, weak_factory_.GetWeakPtr())); | 703 base::Bind(&CacheStorage::LazyInitImpl, weak_factory_.GetWeakPtr())); |
| 670 } | 704 } |
| 671 | 705 |
| 672 void CacheStorage::LazyInitImpl() { | 706 void CacheStorage::LazyInitImpl() { |
| 673 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 707 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 674 DCHECK(!initialized_); | 708 DCHECK(!initialized_); |
| 675 DCHECK(initializing_); | 709 DCHECK(initializing_); |
| 676 | 710 |
| 677 // 1. Get the list of cache names (async call) | 711 // 1. Get the cache index (async call) |
| 678 // 2. For each cache name, load the cache (async call) | 712 // 2. For each cache name, load the cache (async call) |
| 679 // 3. Once each load is complete, update the map variables. | 713 // 3. Once each load is complete, update the map variables. |
| 680 // 4. Call the list of waiting callbacks. | 714 // 4. Call the list of waiting callbacks. |
| 681 | 715 |
| 682 std::unique_ptr<std::vector<std::string>> indexed_cache_names( | 716 cache_loader_->LoadIndex(base::Bind(&CacheStorage::LazyInitDidLoadIndex, |
| 683 new std::vector<std::string>()); | |
| 684 | |
| 685 cache_loader_->LoadIndex(std::move(indexed_cache_names), | |
| 686 base::Bind(&CacheStorage::LazyInitDidLoadIndex, | |
| 687 weak_factory_.GetWeakPtr())); | 717 weak_factory_.GetWeakPtr())); |
| 688 } | 718 } |
| 689 | 719 |
| 690 void CacheStorage::LazyInitDidLoadIndex( | 720 void CacheStorage::LazyInitDidLoadIndex( |
| 691 std::unique_ptr<std::vector<std::string>> indexed_cache_names) { | 721 std::unique_ptr<CacheStorageIndex> index) { |
| 692 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 722 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 723 DCHECK(cache_map_.empty()); | |
| 693 | 724 |
| 694 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) { | 725 for (const auto& cache_metadata : index->ordered_cache_metadata()) { |
| 695 cache_map_.insert(std::make_pair(indexed_cache_names->at(i), | 726 cache_map_.insert(std::make_pair(cache_metadata.name, |
| 696 std::unique_ptr<CacheStorageCache>())); | 727 std::unique_ptr<CacheStorageCache>())); |
| 697 ordered_cache_names_.push_back(indexed_cache_names->at(i)); | |
| 698 } | 728 } |
| 699 | 729 |
| 730 cache_index_ = std::move(index); | |
| 731 | |
| 700 initializing_ = false; | 732 initializing_ = false; |
| 701 initialized_ = true; | 733 initialized_ = true; |
| 702 | 734 |
| 703 scheduler_->CompleteOperationAndRunNext(); | 735 scheduler_->CompleteOperationAndRunNext(); |
| 704 } | 736 } |
| 705 | 737 |
| 706 void CacheStorage::OpenCacheImpl(const std::string& cache_name, | 738 void CacheStorage::OpenCacheImpl(const std::string& cache_name, |
| 707 const CacheAndErrorCallback& callback) { | 739 const CacheAndErrorCallback& callback) { |
| 708 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 740 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 709 GetLoadedCache(cache_name); | 741 GetLoadedCache(cache_name); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 728 | 760 |
| 729 if (!cache) { | 761 if (!cache) { |
| 730 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), | 762 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), |
| 731 CACHE_STORAGE_ERROR_STORAGE); | 763 CACHE_STORAGE_ERROR_STORAGE); |
| 732 return; | 764 return; |
| 733 } | 765 } |
| 734 | 766 |
| 735 CacheStorageCache* cache_ptr = cache.get(); | 767 CacheStorageCache* cache_ptr = cache.get(); |
| 736 | 768 |
| 737 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); | 769 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); |
| 738 ordered_cache_names_.push_back(cache_name); | 770 cache_index_->Insert( |
| 771 CacheStorageIndex::CacheMetadata(cache_name, cache_ptr->cache_size())); | |
| 739 | 772 |
| 740 cache_loader_->WriteIndex( | 773 cache_loader_->WriteIndex( |
| 741 ordered_cache_names_, | 774 *cache_index_, base::Bind(&CacheStorage::CreateCacheDidWriteIndex, |
| 742 base::Bind(&CacheStorage::CreateCacheDidWriteIndex, | 775 weak_factory_.GetWeakPtr(), callback, |
| 743 weak_factory_.GetWeakPtr(), callback, | 776 base::Passed(CreateCacheHandle(cache_ptr)))); |
| 744 base::Passed(CreateCacheHandle(cache_ptr)))); | |
| 745 | 777 |
| 746 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); | 778 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); |
| 747 } | 779 } |
| 748 | 780 |
| 749 void CacheStorage::CreateCacheDidWriteIndex( | 781 void CacheStorage::CreateCacheDidWriteIndex( |
| 750 const CacheAndErrorCallback& callback, | 782 const CacheAndErrorCallback& callback, |
| 751 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 783 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 752 bool success) { | 784 bool success) { |
| 753 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 785 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 754 DCHECK(cache_handle); | 786 DCHECK(cache_handle); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 765 } | 797 } |
| 766 | 798 |
| 767 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, | 799 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, |
| 768 const BoolAndErrorCallback& callback) { | 800 const BoolAndErrorCallback& callback) { |
| 769 if (!GetLoadedCache(cache_name)) { | 801 if (!GetLoadedCache(cache_name)) { |
| 770 base::ThreadTaskRunnerHandle::Get()->PostTask( | 802 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 771 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND)); | 803 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND)); |
| 772 return; | 804 return; |
| 773 } | 805 } |
| 774 | 806 |
| 775 // Delete the name from ordered_cache_names_. | 807 // Save the current index so that it can be restored if the write fails. |
| 776 StringVector original_ordered_cache_names = ordered_cache_names_; | 808 // TODO(cmumford): Avoid creating a copy, and make CacheStorageCacheIndex |
| 777 StringVector::iterator iter = std::find( | 809 // DISALLOW_COPY_AND_ASSIGN. |
| 778 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name); | 810 auto prior_index = base::MakeUnique<CacheStorageIndex>(*cache_index_); |
| 779 DCHECK(iter != ordered_cache_names_.end()); | 811 cache_index_->Delete(cache_name); |
| 780 ordered_cache_names_.erase(iter); | |
| 781 | 812 |
| 782 cache_loader_->WriteIndex(ordered_cache_names_, | 813 cache_loader_->WriteIndex( |
| 783 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex, | 814 *cache_index_, |
| 784 weak_factory_.GetWeakPtr(), cache_name, | 815 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex, |
| 785 original_ordered_cache_names, callback)); | 816 weak_factory_.GetWeakPtr(), cache_name, |
| 817 base::Passed(std::move(prior_index)), callback)); | |
| 786 } | 818 } |
| 787 | 819 |
| 788 void CacheStorage::DeleteCacheDidWriteIndex( | 820 void CacheStorage::DeleteCacheDidWriteIndex( |
| 789 const std::string& cache_name, | 821 const std::string& cache_name, |
| 790 const StringVector& original_ordered_cache_names, | 822 std::unique_ptr<CacheStorageIndex> index_before_delete, |
| 791 const BoolAndErrorCallback& callback, | 823 const BoolAndErrorCallback& callback, |
| 792 bool success) { | 824 bool success) { |
| 793 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 825 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 794 | 826 |
| 795 if (!success) { | 827 if (!success) { |
| 796 // Undo any changes if the change couldn't be written to disk. | 828 // Undo any changes if the change couldn't be written to disk. |
| 797 ordered_cache_names_ = original_ordered_cache_names; | 829 cache_index_ = std::move(index_before_delete); |
| 798 callback.Run(false, CACHE_STORAGE_ERROR_STORAGE); | 830 callback.Run(false, CACHE_STORAGE_ERROR_STORAGE); |
| 799 return; | 831 return; |
| 800 } | 832 } |
| 801 | 833 |
| 802 // Make sure that a cache handle exists for the doomed cache to ensure that | 834 // Make sure that a cache handle exists for the doomed cache to ensure that |
| 803 // DeleteCacheFinalize is called. | 835 // DeleteCacheFinalize is called. |
| 804 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 836 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 805 GetLoadedCache(cache_name); | 837 GetLoadedCache(cache_name); |
| 806 | 838 |
| 807 CacheMap::iterator map_iter = cache_map_.find(cache_name); | 839 CacheMap::iterator map_iter = cache_map_.find(cache_name); |
| 808 doomed_caches_.insert( | 840 doomed_caches_.insert( |
| 809 std::make_pair(map_iter->second.get(), std::move(map_iter->second))); | 841 std::make_pair(map_iter->second.get(), std::move(map_iter->second))); |
| 810 cache_map_.erase(map_iter); | 842 cache_map_.erase(map_iter); |
| 811 | 843 |
| 812 cache_loader_->NotifyCacheDoomed(std::move(cache_handle)); | 844 cache_loader_->NotifyCacheDoomed(std::move(cache_handle)); |
| 813 | 845 |
| 814 callback.Run(true, CACHE_STORAGE_OK); | 846 callback.Run(true, CACHE_STORAGE_OK); |
| 815 } | 847 } |
| 816 | 848 |
| 817 // Call this once the last handle to a doomed cache is gone. It's okay if this | 849 // Call this once the last handle to a doomed cache is gone. It's okay if this |
| 818 // doesn't get to complete before shutdown, the cache will be removed from disk | 850 // doesn't get to complete before shutdown, the cache will be removed from disk |
| 819 // on next startup in that case. | 851 // on next startup in that case. |
| 820 void CacheStorage::DeleteCacheFinalize( | 852 void CacheStorage::DeleteCacheFinalize( |
| 821 std::unique_ptr<CacheStorageCache> doomed_cache) { | 853 std::unique_ptr<CacheStorageCache> doomed_cache) { |
| 822 CacheStorageCache* cache = doomed_cache.get(); | 854 CacheStorageCache* cache = doomed_cache.get(); |
| 855 cache->SetObserver(nullptr); | |
| 823 cache->Size(base::Bind(&CacheStorage::DeleteCacheDidGetSize, | 856 cache->Size(base::Bind(&CacheStorage::DeleteCacheDidGetSize, |
| 824 weak_factory_.GetWeakPtr(), | 857 weak_factory_.GetWeakPtr(), |
| 825 base::Passed(std::move(doomed_cache)))); | 858 base::Passed(std::move(doomed_cache)))); |
| 826 } | 859 } |
| 827 | 860 |
| 828 void CacheStorage::DeleteCacheDidGetSize( | 861 void CacheStorage::DeleteCacheDidGetSize( |
| 829 std::unique_ptr<CacheStorageCache> cache, | 862 std::unique_ptr<CacheStorageCache> cache, |
| 830 int64_t cache_size) { | 863 int64_t cache_size) { |
| 831 quota_manager_proxy_->NotifyStorageModified( | 864 quota_manager_proxy_->NotifyStorageModified( |
| 832 storage::QuotaClient::kServiceWorkerCache, origin_, | 865 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 833 storage::kStorageTypeTemporary, -1 * cache_size); | 866 storage::kStorageTypeTemporary, -1 * cache_size); |
| 834 | 867 |
| 835 cache_loader_->CleanUpDeletedCache(cache.get()); | 868 cache_loader_->CleanUpDeletedCache(cache.get()); |
| 836 } | 869 } |
| 837 | 870 |
| 838 void CacheStorage::EnumerateCachesImpl(const StringsCallback& callback) { | 871 void CacheStorage::EnumerateCachesImpl(const IndexCallback& callback) { |
| 839 callback.Run(ordered_cache_names_); | 872 callback.Run(*cache_index_); |
| 840 } | 873 } |
| 841 | 874 |
| 842 void CacheStorage::MatchCacheImpl( | 875 void CacheStorage::MatchCacheImpl( |
| 843 const std::string& cache_name, | 876 const std::string& cache_name, |
| 844 std::unique_ptr<ServiceWorkerFetchRequest> request, | 877 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 845 const CacheStorageCacheQueryParams& match_params, | 878 const CacheStorageCacheQueryParams& match_params, |
| 846 const CacheStorageCache::ResponseCallback& callback) { | 879 const CacheStorageCache::ResponseCallback& callback) { |
| 847 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 880 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 848 GetLoadedCache(cache_name); | 881 GetLoadedCache(cache_name); |
| 849 | 882 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 870 std::unique_ptr<ServiceWorkerResponse> response, | 903 std::unique_ptr<ServiceWorkerResponse> response, |
| 871 std::unique_ptr<storage::BlobDataHandle> handle) { | 904 std::unique_ptr<storage::BlobDataHandle> handle) { |
| 872 callback.Run(error, std::move(response), std::move(handle)); | 905 callback.Run(error, std::move(response), std::move(handle)); |
| 873 } | 906 } |
| 874 | 907 |
| 875 void CacheStorage::MatchAllCachesImpl( | 908 void CacheStorage::MatchAllCachesImpl( |
| 876 std::unique_ptr<ServiceWorkerFetchRequest> request, | 909 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 877 const CacheStorageCacheQueryParams& match_params, | 910 const CacheStorageCacheQueryParams& match_params, |
| 878 const CacheStorageCache::ResponseCallback& callback) { | 911 const CacheStorageCache::ResponseCallback& callback) { |
| 879 std::vector<CacheMatchResponse>* match_responses = | 912 std::vector<CacheMatchResponse>* match_responses = |
| 880 new std::vector<CacheMatchResponse>(ordered_cache_names_.size()); | 913 new std::vector<CacheMatchResponse>(cache_index_->num_entries()); |
| 881 | 914 |
| 882 base::Closure barrier_closure = base::BarrierClosure( | 915 base::Closure barrier_closure = base::BarrierClosure( |
| 883 ordered_cache_names_.size(), | 916 cache_index_->num_entries(), |
| 884 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll, | 917 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll, |
| 885 weak_factory_.GetWeakPtr(), | 918 weak_factory_.GetWeakPtr(), |
| 886 base::Passed(base::WrapUnique(match_responses)), callback)); | 919 base::Passed(base::WrapUnique(match_responses)), callback)); |
| 887 | 920 |
| 888 for (size_t i = 0, max = ordered_cache_names_.size(); i < max; ++i) { | 921 size_t idx = 0; |
| 922 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { | |
| 889 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 923 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 890 GetLoadedCache(ordered_cache_names_[i]); | 924 GetLoadedCache(cache_metadata.name); |
| 891 DCHECK(cache_handle); | 925 DCHECK(cache_handle); |
| 892 | 926 |
| 893 CacheStorageCache* cache_ptr = cache_handle->value(); | 927 CacheStorageCache* cache_ptr = cache_handle->value(); |
| 894 cache_ptr->Match(base::MakeUnique<ServiceWorkerFetchRequest>(*request), | 928 cache_ptr->Match(base::MakeUnique<ServiceWorkerFetchRequest>(*request), |
| 895 match_params, | 929 match_params, |
| 896 base::Bind(&CacheStorage::MatchAllCachesDidMatch, | 930 base::Bind(&CacheStorage::MatchAllCachesDidMatch, |
| 897 weak_factory_.GetWeakPtr(), | 931 weak_factory_.GetWeakPtr(), |
| 898 base::Passed(std::move(cache_handle)), | 932 base::Passed(std::move(cache_handle)), |
| 899 &match_responses->at(i), barrier_closure)); | 933 &match_responses->at(idx), barrier_closure)); |
| 934 idx++; | |
| 900 } | 935 } |
| 901 } | 936 } |
| 902 | 937 |
| 903 void CacheStorage::MatchAllCachesDidMatch( | 938 void CacheStorage::MatchAllCachesDidMatch( |
| 904 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 939 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 905 CacheMatchResponse* out_match_response, | 940 CacheMatchResponse* out_match_response, |
| 906 const base::Closure& barrier_closure, | 941 const base::Closure& barrier_closure, |
| 907 CacheStorageError error, | 942 CacheStorageError error, |
| 908 std::unique_ptr<ServiceWorkerResponse> service_worker_response, | 943 std::unique_ptr<ServiceWorkerResponse> service_worker_response, |
| 909 std::unique_ptr<storage::BlobDataHandle> handle) { | 944 std::unique_ptr<storage::BlobDataHandle> handle) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 977 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1012 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 978 DCHECK(initialized_); | 1013 DCHECK(initialized_); |
| 979 | 1014 |
| 980 CacheMap::iterator map_iter = cache_map_.find(cache_name); | 1015 CacheMap::iterator map_iter = cache_map_.find(cache_name); |
| 981 if (map_iter == cache_map_.end()) | 1016 if (map_iter == cache_map_.end()) |
| 982 return std::unique_ptr<CacheStorageCacheHandle>(); | 1017 return std::unique_ptr<CacheStorageCacheHandle>(); |
| 983 | 1018 |
| 984 CacheStorageCache* cache = map_iter->second.get(); | 1019 CacheStorageCache* cache = map_iter->second.get(); |
| 985 | 1020 |
| 986 if (!cache) { | 1021 if (!cache) { |
| 987 std::unique_ptr<CacheStorageCache> new_cache = | 1022 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache( |
| 988 cache_loader_->CreateCache(cache_name); | 1023 cache_name, cache_index_->GetCacheSize(cache_name)); |
| 989 CacheStorageCache* cache_ptr = new_cache.get(); | 1024 CacheStorageCache* cache_ptr = new_cache.get(); |
| 990 map_iter->second = std::move(new_cache); | 1025 map_iter->second = std::move(new_cache); |
| 991 | 1026 |
| 992 return CreateCacheHandle(cache_ptr); | 1027 return CreateCacheHandle(cache_ptr); |
| 993 } | 1028 } |
| 994 | 1029 |
| 995 return CreateCacheHandle(cache); | 1030 return CreateCacheHandle(cache); |
| 996 } | 1031 } |
| 997 | 1032 |
| 1033 void CacheStorage::SizeRetrievedFromCache( | |
| 1034 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | |
| 1035 const base::Closure& closure, | |
| 1036 int64_t* accumulator, | |
| 1037 int64_t size) { | |
| 1038 cache_index_->SetCacheSize(cache_handle->value()->cache_name(), size); | |
| 1039 *accumulator += size; | |
| 1040 closure.Run(); | |
| 1041 } | |
| 1042 | |
| 998 void CacheStorage::GetSizeThenCloseAllCachesImpl(const SizeCallback& callback) { | 1043 void CacheStorage::GetSizeThenCloseAllCachesImpl(const SizeCallback& callback) { |
| 999 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1044 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1000 DCHECK(initialized_); | 1045 DCHECK(initialized_); |
| 1001 | 1046 |
| 1002 std::unique_ptr<int64_t> accumulator(new int64_t(0)); | 1047 std::unique_ptr<int64_t> accumulator(new int64_t(0)); |
| 1003 int64_t* accumulator_ptr = accumulator.get(); | 1048 int64_t* accumulator_ptr = accumulator.get(); |
| 1004 | 1049 |
| 1005 base::Closure barrier_closure = base::BarrierClosure( | 1050 base::Closure barrier_closure = base::BarrierClosure( |
| 1006 ordered_cache_names_.size(), | 1051 cache_index_->num_entries(), |
| 1007 base::Bind(&SizeRetrievedFromAllCaches, | 1052 base::Bind(&SizeRetrievedFromAllCaches, |
| 1008 base::Passed(std::move(accumulator)), callback)); | 1053 base::Passed(std::move(accumulator)), callback)); |
| 1009 | 1054 |
| 1010 for (const std::string& cache_name : ordered_cache_names_) { | 1055 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { |
| 1011 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 1056 auto cache_handle = GetLoadedCache(cache_metadata.name); |
| 1012 GetLoadedCache(cache_name); | |
| 1013 CacheStorageCache* cache = cache_handle->value(); | 1057 CacheStorageCache* cache = cache_handle->value(); |
| 1014 cache->GetSizeThenClose(base::Bind(&SizeRetrievedFromCache, | 1058 cache->GetSizeThenClose(base::Bind(&CacheStorage::SizeRetrievedFromCache, |
| 1059 weak_factory_.GetWeakPtr(), | |
| 1015 base::Passed(std::move(cache_handle)), | 1060 base::Passed(std::move(cache_handle)), |
| 1016 barrier_closure, accumulator_ptr)); | 1061 barrier_closure, accumulator_ptr)); |
| 1017 } | 1062 } |
| 1018 } | 1063 } |
| 1019 | 1064 |
| 1020 void CacheStorage::SizeImpl(const SizeCallback& callback) { | 1065 void CacheStorage::SizeImpl(const SizeCallback& callback) { |
| 1021 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1066 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1022 DCHECK(initialized_); | 1067 DCHECK(initialized_); |
| 1023 | 1068 |
| 1069 if (cache_index_->GetStorageSize() != kSizeUnknown) { | |
| 1070 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 1071 FROM_HERE, base::Bind(callback, cache_index_->GetStorageSize())); | |
| 1072 return; | |
| 1073 } | |
| 1074 | |
| 1024 std::unique_ptr<int64_t> accumulator(new int64_t(0)); | 1075 std::unique_ptr<int64_t> accumulator(new int64_t(0)); |
| 1025 int64_t* accumulator_ptr = accumulator.get(); | 1076 int64_t* accumulator_ptr = accumulator.get(); |
| 1026 | 1077 |
| 1027 base::Closure barrier_closure = base::BarrierClosure( | 1078 base::Closure barrier_closure = base::BarrierClosure( |
| 1028 ordered_cache_names_.size(), | 1079 cache_index_->num_entries(), |
| 1029 base::Bind(&SizeRetrievedFromAllCaches, | 1080 base::Bind(&SizeRetrievedFromAllCaches, |
| 1030 base::Passed(std::move(accumulator)), callback)); | 1081 base::Passed(std::move(accumulator)), callback)); |
| 1031 | 1082 |
| 1032 for (const std::string& cache_name : ordered_cache_names_) { | 1083 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { |
| 1084 if (cache_metadata.size != CacheStorage::kSizeUnknown) { | |
| 1085 *accumulator_ptr += cache_metadata.size; | |
| 1086 barrier_closure.Run(); | |
| 1087 continue; | |
| 1088 } | |
| 1033 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 1089 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 1034 GetLoadedCache(cache_name); | 1090 GetLoadedCache(cache_metadata.name); |
| 1035 CacheStorageCache* cache = cache_handle->value(); | 1091 CacheStorageCache* cache = cache_handle->value(); |
| 1036 cache->Size(base::Bind(&SizeRetrievedFromCache, | 1092 cache->Size(base::Bind(&CacheStorage::SizeRetrievedFromCache, |
| 1093 weak_factory_.GetWeakPtr(), | |
| 1037 base::Passed(std::move(cache_handle)), | 1094 base::Passed(std::move(cache_handle)), |
| 1038 barrier_closure, accumulator_ptr)); | 1095 barrier_closure, accumulator_ptr)); |
| 1039 } | 1096 } |
| 1040 } | 1097 } |
| 1041 | 1098 |
| 1042 } // namespace content | 1099 } // namespace content |
| OLD | NEW |