Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: content/browser/cache_storage/cache_storage.cc

Issue 2416713002: Write out CacheStorageCache size to index file. (Closed)
Patch Set: Only updating cache sizes for non-doomed caches Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
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
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
jkarlin 2016/12/09 19:49:19 We're just invalidating the size of the index entr
cmumford 2016/12/15 22:29:14 Done.
439 // after the index (making it out-of-date)
jkarlin 2016/12/09 19:49:19 Needs a period.
cmumford 2016/12/15 22:29:14 Done.
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>()),
jkarlin 2016/12/09 19:49:19 Why create this in the constructor? It's a bug if
cmumford 2016/12/15 22:29:14 Done.
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
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
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;
jkarlin 2016/12/09 19:49:19 kWriteIndexDelaySecs
cmumford 2016/12/15 22:29:14 Done.
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 scheduler_->ScheduleOperation(
663 base::Bind(&CacheStorage::WriteIndexImpl, weak_factory_.GetWeakPtr()));
664 }
665
666 void CacheStorage::WriteIndexImpl() {
667 index_write_pending_ = false;
668 cache_loader_->WriteIndex(*cache_index_, base::Bind(&DoNothingWithBool));
jkarlin 2016/12/09 19:49:19 The callback needs to call RunNext on the schedule
cmumford 2016/12/15 22:29:14 Done. I also tweaked the method names slightly sin
669 }
670
671 bool CacheStorage::InitiateScheduledIndexWriteForTest() {
672 if (index_write_pending_) {
673 index_write_task_.Cancel();
674 WriteIndex();
675 return true;
676 }
677 return false;
678 }
679
680 void CacheStorage::CacheSizeUpdated(const CacheStorageCache* cache,
681 int64_t size) {
682 // If the cache is doomed (but still running), ignore any size updates.
683 if (base::ContainsKey(doomed_caches_, const_cast<CacheStorageCache*>(cache)))
684 return;
685 cache_index_->SetCacheSize(cache->cache_name(), size);
686 ScheduleWriteIndex();
687 }
688
649 void CacheStorage::StartAsyncOperationForTesting() { 689 void CacheStorage::StartAsyncOperationForTesting() {
650 scheduler_->ScheduleOperation(base::Bind(&base::DoNothing)); 690 scheduler_->ScheduleOperation(base::Bind(&base::DoNothing));
651 } 691 }
652 692
653 void CacheStorage::CompleteAsyncOperationForTesting() { 693 void CacheStorage::CompleteAsyncOperationForTesting() {
654 scheduler_->CompleteOperationAndRunNext(); 694 scheduler_->CompleteOperationAndRunNext();
655 } 695 }
656 696
657 // Init is run lazily so that it is called on the proper MessageLoop. 697 // Init is run lazily so that it is called on the proper MessageLoop.
658 void CacheStorage::LazyInit() { 698 void CacheStorage::LazyInit() {
659 DCHECK_CURRENTLY_ON(BrowserThread::IO); 699 DCHECK_CURRENTLY_ON(BrowserThread::IO);
660 DCHECK(!initialized_); 700 DCHECK(!initialized_);
661 701
662 if (initializing_) 702 if (initializing_)
663 return; 703 return;
664 704
665 DCHECK(!scheduler_->ScheduledOperations()); 705 DCHECK(!scheduler_->ScheduledOperations());
666 706
667 initializing_ = true; 707 initializing_ = true;
668 scheduler_->ScheduleOperation( 708 scheduler_->ScheduleOperation(
669 base::Bind(&CacheStorage::LazyInitImpl, weak_factory_.GetWeakPtr())); 709 base::Bind(&CacheStorage::LazyInitImpl, weak_factory_.GetWeakPtr()));
670 } 710 }
671 711
672 void CacheStorage::LazyInitImpl() { 712 void CacheStorage::LazyInitImpl() {
673 DCHECK_CURRENTLY_ON(BrowserThread::IO); 713 DCHECK_CURRENTLY_ON(BrowserThread::IO);
674 DCHECK(!initialized_); 714 DCHECK(!initialized_);
675 DCHECK(initializing_); 715 DCHECK(initializing_);
676 716
677 // 1. Get the list of cache names (async call) 717 // 1. Get the cache index (async call)
678 // 2. For each cache name, load the cache (async call) 718 // 2. For each cache name, load the cache (async call)
679 // 3. Once each load is complete, update the map variables. 719 // 3. Once each load is complete, update the map variables.
680 // 4. Call the list of waiting callbacks. 720 // 4. Call the list of waiting callbacks.
681 721
682 std::unique_ptr<std::vector<std::string>> indexed_cache_names( 722 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())); 723 weak_factory_.GetWeakPtr()));
688 } 724 }
689 725
690 void CacheStorage::LazyInitDidLoadIndex( 726 void CacheStorage::LazyInitDidLoadIndex(
691 std::unique_ptr<std::vector<std::string>> indexed_cache_names) { 727 std::unique_ptr<CacheStorageIndex> index) {
692 DCHECK_CURRENTLY_ON(BrowserThread::IO); 728 DCHECK_CURRENTLY_ON(BrowserThread::IO);
729 DCHECK(cache_map_.empty());
693 730
694 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) { 731 for (const auto& cache_metadata : index->ordered_cache_metadata()) {
695 cache_map_.insert(std::make_pair(indexed_cache_names->at(i), 732 cache_map_.insert(std::make_pair(cache_metadata.name,
696 std::unique_ptr<CacheStorageCache>())); 733 std::unique_ptr<CacheStorageCache>()));
697 ordered_cache_names_.push_back(indexed_cache_names->at(i));
698 } 734 }
699 735
736 cache_index_ = std::move(index);
737
700 initializing_ = false; 738 initializing_ = false;
701 initialized_ = true; 739 initialized_ = true;
702 740
703 scheduler_->CompleteOperationAndRunNext(); 741 scheduler_->CompleteOperationAndRunNext();
704 } 742 }
705 743
706 void CacheStorage::OpenCacheImpl(const std::string& cache_name, 744 void CacheStorage::OpenCacheImpl(const std::string& cache_name,
707 const CacheAndErrorCallback& callback) { 745 const CacheAndErrorCallback& callback) {
708 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 746 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
709 GetLoadedCache(cache_name); 747 GetLoadedCache(cache_name);
(...skipping 18 matching lines...) Expand all
728 766
729 if (!cache) { 767 if (!cache) {
730 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), 768 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(),
731 CACHE_STORAGE_ERROR_STORAGE); 769 CACHE_STORAGE_ERROR_STORAGE);
732 return; 770 return;
733 } 771 }
734 772
735 CacheStorageCache* cache_ptr = cache.get(); 773 CacheStorageCache* cache_ptr = cache.get();
736 774
737 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); 775 cache_map_.insert(std::make_pair(cache_name, std::move(cache)));
738 ordered_cache_names_.push_back(cache_name); 776 cache_index_->Insert(
777 CacheStorageIndex::CacheMetadata(cache_name, cache_ptr->cache_size()));
739 778
740 cache_loader_->WriteIndex( 779 cache_loader_->WriteIndex(
741 ordered_cache_names_, 780 *cache_index_, base::Bind(&CacheStorage::CreateCacheDidWriteIndex,
742 base::Bind(&CacheStorage::CreateCacheDidWriteIndex, 781 weak_factory_.GetWeakPtr(), callback,
743 weak_factory_.GetWeakPtr(), callback, 782 base::Passed(CreateCacheHandle(cache_ptr))));
744 base::Passed(CreateCacheHandle(cache_ptr))));
745 783
746 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); 784 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr));
747 } 785 }
748 786
749 void CacheStorage::CreateCacheDidWriteIndex( 787 void CacheStorage::CreateCacheDidWriteIndex(
750 const CacheAndErrorCallback& callback, 788 const CacheAndErrorCallback& callback,
751 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 789 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
752 bool success) { 790 bool success) {
753 DCHECK_CURRENTLY_ON(BrowserThread::IO); 791 DCHECK_CURRENTLY_ON(BrowserThread::IO);
754 DCHECK(cache_handle); 792 DCHECK(cache_handle);
(...skipping 10 matching lines...) Expand all
765 } 803 }
766 804
767 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, 805 void CacheStorage::DeleteCacheImpl(const std::string& cache_name,
768 const BoolAndErrorCallback& callback) { 806 const BoolAndErrorCallback& callback) {
769 if (!GetLoadedCache(cache_name)) { 807 if (!GetLoadedCache(cache_name)) {
770 base::ThreadTaskRunnerHandle::Get()->PostTask( 808 base::ThreadTaskRunnerHandle::Get()->PostTask(
771 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND)); 809 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND));
772 return; 810 return;
773 } 811 }
774 812
775 // Delete the name from ordered_cache_names_. 813 // Save the current index so that it can be restored if the write fails.
776 StringVector original_ordered_cache_names = ordered_cache_names_; 814 // TODO(cmumford): Avoid creating a copy, and make CacheStorageCacheIndex
777 StringVector::iterator iter = std::find( 815 // DISALLOW_COPY_AND_ASSIGN.
778 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name); 816 auto prior_index = base::MakeUnique<CacheStorageIndex>(*cache_index_);
779 DCHECK(iter != ordered_cache_names_.end()); 817 cache_index_->Delete(cache_name);
780 ordered_cache_names_.erase(iter);
781 818
782 cache_loader_->WriteIndex(ordered_cache_names_, 819 cache_loader_->WriteIndex(
783 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex, 820 *cache_index_,
784 weak_factory_.GetWeakPtr(), cache_name, 821 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex,
785 original_ordered_cache_names, callback)); 822 weak_factory_.GetWeakPtr(), cache_name,
823 base::Passed(std::move(prior_index)), callback));
786 } 824 }
787 825
788 void CacheStorage::DeleteCacheDidWriteIndex( 826 void CacheStorage::DeleteCacheDidWriteIndex(
789 const std::string& cache_name, 827 const std::string& cache_name,
790 const StringVector& original_ordered_cache_names, 828 std::unique_ptr<CacheStorageIndex> index_before_delete,
791 const BoolAndErrorCallback& callback, 829 const BoolAndErrorCallback& callback,
792 bool success) { 830 bool success) {
793 DCHECK_CURRENTLY_ON(BrowserThread::IO); 831 DCHECK_CURRENTLY_ON(BrowserThread::IO);
794 832
795 if (!success) { 833 if (!success) {
796 // Undo any changes if the change couldn't be written to disk. 834 // Undo any changes if the change couldn't be written to disk.
797 ordered_cache_names_ = original_ordered_cache_names; 835 cache_index_ = std::move(index_before_delete);
798 callback.Run(false, CACHE_STORAGE_ERROR_STORAGE); 836 callback.Run(false, CACHE_STORAGE_ERROR_STORAGE);
799 return; 837 return;
800 } 838 }
801 839
802 // Make sure that a cache handle exists for the doomed cache to ensure that 840 // Make sure that a cache handle exists for the doomed cache to ensure that
803 // DeleteCacheFinalize is called. 841 // DeleteCacheFinalize is called.
804 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 842 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
805 GetLoadedCache(cache_name); 843 GetLoadedCache(cache_name);
806 844
807 CacheMap::iterator map_iter = cache_map_.find(cache_name); 845 CacheMap::iterator map_iter = cache_map_.find(cache_name);
808 doomed_caches_.insert( 846 doomed_caches_.insert(
809 std::make_pair(map_iter->second.get(), std::move(map_iter->second))); 847 std::make_pair(map_iter->second.get(), std::move(map_iter->second)));
810 cache_map_.erase(map_iter); 848 cache_map_.erase(map_iter);
811 849
812 cache_loader_->NotifyCacheDoomed(std::move(cache_handle)); 850 cache_loader_->NotifyCacheDoomed(std::move(cache_handle));
813 851
814 callback.Run(true, CACHE_STORAGE_OK); 852 callback.Run(true, CACHE_STORAGE_OK);
815 } 853 }
816 854
817 // Call this once the last handle to a doomed cache is gone. It's okay if this 855 // 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 856 // doesn't get to complete before shutdown, the cache will be removed from disk
819 // on next startup in that case. 857 // on next startup in that case.
820 void CacheStorage::DeleteCacheFinalize( 858 void CacheStorage::DeleteCacheFinalize(
821 std::unique_ptr<CacheStorageCache> doomed_cache) { 859 std::unique_ptr<CacheStorageCache> doomed_cache) {
822 CacheStorageCache* cache = doomed_cache.get(); 860 CacheStorageCache* cache = doomed_cache.get();
861 cache->SetObserver(nullptr);
jkarlin 2016/12/09 19:49:19 You could call this as soon as a cache is doomed,
cmumford 2016/12/15 22:29:14 Done.
823 cache->Size(base::Bind(&CacheStorage::DeleteCacheDidGetSize, 862 cache->Size(base::Bind(&CacheStorage::DeleteCacheDidGetSize,
824 weak_factory_.GetWeakPtr(), 863 weak_factory_.GetWeakPtr(),
825 base::Passed(std::move(doomed_cache)))); 864 base::Passed(std::move(doomed_cache))));
826 } 865 }
827 866
828 void CacheStorage::DeleteCacheDidGetSize( 867 void CacheStorage::DeleteCacheDidGetSize(
829 std::unique_ptr<CacheStorageCache> cache, 868 std::unique_ptr<CacheStorageCache> cache,
830 int64_t cache_size) { 869 int64_t cache_size) {
831 quota_manager_proxy_->NotifyStorageModified( 870 quota_manager_proxy_->NotifyStorageModified(
832 storage::QuotaClient::kServiceWorkerCache, origin_, 871 storage::QuotaClient::kServiceWorkerCache, origin_,
833 storage::kStorageTypeTemporary, -1 * cache_size); 872 storage::kStorageTypeTemporary, -1 * cache_size);
834 873
835 cache_loader_->CleanUpDeletedCache(cache.get()); 874 cache_loader_->CleanUpDeletedCache(cache.get());
836 } 875 }
837 876
838 void CacheStorage::EnumerateCachesImpl(const StringsCallback& callback) { 877 void CacheStorage::EnumerateCachesImpl(const IndexCallback& callback) {
839 callback.Run(ordered_cache_names_); 878 callback.Run(*cache_index_);
840 } 879 }
841 880
842 void CacheStorage::MatchCacheImpl( 881 void CacheStorage::MatchCacheImpl(
843 const std::string& cache_name, 882 const std::string& cache_name,
844 std::unique_ptr<ServiceWorkerFetchRequest> request, 883 std::unique_ptr<ServiceWorkerFetchRequest> request,
845 const CacheStorageCacheQueryParams& match_params, 884 const CacheStorageCacheQueryParams& match_params,
846 const CacheStorageCache::ResponseCallback& callback) { 885 const CacheStorageCache::ResponseCallback& callback) {
847 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 886 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
848 GetLoadedCache(cache_name); 887 GetLoadedCache(cache_name);
849 888
(...skipping 20 matching lines...) Expand all
870 std::unique_ptr<ServiceWorkerResponse> response, 909 std::unique_ptr<ServiceWorkerResponse> response,
871 std::unique_ptr<storage::BlobDataHandle> handle) { 910 std::unique_ptr<storage::BlobDataHandle> handle) {
872 callback.Run(error, std::move(response), std::move(handle)); 911 callback.Run(error, std::move(response), std::move(handle));
873 } 912 }
874 913
875 void CacheStorage::MatchAllCachesImpl( 914 void CacheStorage::MatchAllCachesImpl(
876 std::unique_ptr<ServiceWorkerFetchRequest> request, 915 std::unique_ptr<ServiceWorkerFetchRequest> request,
877 const CacheStorageCacheQueryParams& match_params, 916 const CacheStorageCacheQueryParams& match_params,
878 const CacheStorageCache::ResponseCallback& callback) { 917 const CacheStorageCache::ResponseCallback& callback) {
879 std::vector<CacheMatchResponse>* match_responses = 918 std::vector<CacheMatchResponse>* match_responses =
880 new std::vector<CacheMatchResponse>(ordered_cache_names_.size()); 919 new std::vector<CacheMatchResponse>(cache_index_->num_entries());
881 920
882 base::Closure barrier_closure = base::BarrierClosure( 921 base::Closure barrier_closure = base::BarrierClosure(
883 ordered_cache_names_.size(), 922 cache_index_->num_entries(),
884 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll, 923 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll,
885 weak_factory_.GetWeakPtr(), 924 weak_factory_.GetWeakPtr(),
886 base::Passed(base::WrapUnique(match_responses)), callback)); 925 base::Passed(base::WrapUnique(match_responses)), callback));
887 926
888 for (size_t i = 0, max = ordered_cache_names_.size(); i < max; ++i) { 927 size_t idx = 0;
928 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) {
889 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 929 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
890 GetLoadedCache(ordered_cache_names_[i]); 930 GetLoadedCache(cache_metadata.name);
891 DCHECK(cache_handle); 931 DCHECK(cache_handle);
892 932
893 CacheStorageCache* cache_ptr = cache_handle->value(); 933 CacheStorageCache* cache_ptr = cache_handle->value();
894 cache_ptr->Match(base::MakeUnique<ServiceWorkerFetchRequest>(*request), 934 cache_ptr->Match(base::MakeUnique<ServiceWorkerFetchRequest>(*request),
895 match_params, 935 match_params,
896 base::Bind(&CacheStorage::MatchAllCachesDidMatch, 936 base::Bind(&CacheStorage::MatchAllCachesDidMatch,
897 weak_factory_.GetWeakPtr(), 937 weak_factory_.GetWeakPtr(),
898 base::Passed(std::move(cache_handle)), 938 base::Passed(std::move(cache_handle)),
899 &match_responses->at(i), barrier_closure)); 939 &match_responses->at(idx), barrier_closure));
940 idx++;
900 } 941 }
901 } 942 }
902 943
903 void CacheStorage::MatchAllCachesDidMatch( 944 void CacheStorage::MatchAllCachesDidMatch(
904 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 945 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
905 CacheMatchResponse* out_match_response, 946 CacheMatchResponse* out_match_response,
906 const base::Closure& barrier_closure, 947 const base::Closure& barrier_closure,
907 CacheStorageError error, 948 CacheStorageError error,
908 std::unique_ptr<ServiceWorkerResponse> service_worker_response, 949 std::unique_ptr<ServiceWorkerResponse> service_worker_response,
909 std::unique_ptr<storage::BlobDataHandle> handle) { 950 std::unique_ptr<storage::BlobDataHandle> handle) {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1018 DCHECK_CURRENTLY_ON(BrowserThread::IO);
978 DCHECK(initialized_); 1019 DCHECK(initialized_);
979 1020
980 CacheMap::iterator map_iter = cache_map_.find(cache_name); 1021 CacheMap::iterator map_iter = cache_map_.find(cache_name);
981 if (map_iter == cache_map_.end()) 1022 if (map_iter == cache_map_.end())
982 return std::unique_ptr<CacheStorageCacheHandle>(); 1023 return std::unique_ptr<CacheStorageCacheHandle>();
983 1024
984 CacheStorageCache* cache = map_iter->second.get(); 1025 CacheStorageCache* cache = map_iter->second.get();
985 1026
986 if (!cache) { 1027 if (!cache) {
987 std::unique_ptr<CacheStorageCache> new_cache = 1028 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache(
988 cache_loader_->CreateCache(cache_name); 1029 cache_name, cache_index_->GetCacheSize(cache_name));
989 CacheStorageCache* cache_ptr = new_cache.get(); 1030 CacheStorageCache* cache_ptr = new_cache.get();
990 map_iter->second = std::move(new_cache); 1031 map_iter->second = std::move(new_cache);
991 1032
992 return CreateCacheHandle(cache_ptr); 1033 return CreateCacheHandle(cache_ptr);
993 } 1034 }
994 1035
995 return CreateCacheHandle(cache); 1036 return CreateCacheHandle(cache);
996 } 1037 }
997 1038
1039 void CacheStorage::SizeRetrievedFromCache(
1040 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
1041 const base::Closure& closure,
1042 int64_t* accumulator,
1043 int64_t size) {
1044 cache_index_->SetCacheSize(cache_handle->value()->cache_name(), size);
1045 *accumulator += size;
1046 closure.Run();
1047 }
1048
998 void CacheStorage::GetSizeThenCloseAllCachesImpl(const SizeCallback& callback) { 1049 void CacheStorage::GetSizeThenCloseAllCachesImpl(const SizeCallback& callback) {
999 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1050 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1000 DCHECK(initialized_); 1051 DCHECK(initialized_);
1001 1052
1002 std::unique_ptr<int64_t> accumulator(new int64_t(0)); 1053 std::unique_ptr<int64_t> accumulator(new int64_t(0));
1003 int64_t* accumulator_ptr = accumulator.get(); 1054 int64_t* accumulator_ptr = accumulator.get();
1004 1055
1005 base::Closure barrier_closure = base::BarrierClosure( 1056 base::Closure barrier_closure = base::BarrierClosure(
1006 ordered_cache_names_.size(), 1057 cache_index_->num_entries(),
1007 base::Bind(&SizeRetrievedFromAllCaches, 1058 base::Bind(&SizeRetrievedFromAllCaches,
1008 base::Passed(std::move(accumulator)), callback)); 1059 base::Passed(std::move(accumulator)), callback));
1009 1060
1010 for (const std::string& cache_name : ordered_cache_names_) { 1061 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) {
1011 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 1062 auto cache_handle = GetLoadedCache(cache_metadata.name);
1012 GetLoadedCache(cache_name);
1013 CacheStorageCache* cache = cache_handle->value(); 1063 CacheStorageCache* cache = cache_handle->value();
1014 cache->GetSizeThenClose(base::Bind(&SizeRetrievedFromCache, 1064 cache->GetSizeThenClose(base::Bind(&CacheStorage::SizeRetrievedFromCache,
1065 weak_factory_.GetWeakPtr(),
1015 base::Passed(std::move(cache_handle)), 1066 base::Passed(std::move(cache_handle)),
1016 barrier_closure, accumulator_ptr)); 1067 barrier_closure, accumulator_ptr));
1017 } 1068 }
1018 } 1069 }
1019 1070
1020 void CacheStorage::SizeImpl(const SizeCallback& callback) { 1071 void CacheStorage::SizeImpl(const SizeCallback& callback) {
1021 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1072 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1022 DCHECK(initialized_); 1073 DCHECK(initialized_);
1023 1074
1075 if (cache_index_->GetStorageSize() != kSizeUnknown) {
1076 base::ThreadTaskRunnerHandle::Get()->PostTask(
1077 FROM_HERE, base::Bind(callback, cache_index_->GetStorageSize()));
1078 return;
1079 }
1080
1024 std::unique_ptr<int64_t> accumulator(new int64_t(0)); 1081 std::unique_ptr<int64_t> accumulator(new int64_t(0));
1025 int64_t* accumulator_ptr = accumulator.get(); 1082 int64_t* accumulator_ptr = accumulator.get();
1026 1083
1027 base::Closure barrier_closure = base::BarrierClosure( 1084 base::Closure barrier_closure = base::BarrierClosure(
1028 ordered_cache_names_.size(), 1085 cache_index_->num_entries(),
1029 base::Bind(&SizeRetrievedFromAllCaches, 1086 base::Bind(&SizeRetrievedFromAllCaches,
1030 base::Passed(std::move(accumulator)), callback)); 1087 base::Passed(std::move(accumulator)), callback));
1031 1088
1032 for (const std::string& cache_name : ordered_cache_names_) { 1089 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) {
1090 if (cache_metadata.size != CacheStorage::kSizeUnknown) {
1091 *accumulator_ptr += cache_metadata.size;
1092 barrier_closure.Run();
1093 continue;
1094 }
1033 std::unique_ptr<CacheStorageCacheHandle> cache_handle = 1095 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
1034 GetLoadedCache(cache_name); 1096 GetLoadedCache(cache_metadata.name);
1035 CacheStorageCache* cache = cache_handle->value(); 1097 CacheStorageCache* cache = cache_handle->value();
1036 cache->Size(base::Bind(&SizeRetrievedFromCache, 1098 cache->Size(base::Bind(&CacheStorage::SizeRetrievedFromCache,
1099 weak_factory_.GetWeakPtr(),
1037 base::Passed(std::move(cache_handle)), 1100 base::Passed(std::move(cache_handle)),
1038 barrier_closure, accumulator_ptr)); 1101 barrier_closure, accumulator_ptr));
1039 } 1102 }
1040 } 1103 }
1041 1104
1042 } // namespace content 1105 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698