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

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

Issue 1421863002: [CacheStorage] Garbage collect unreferenced caches (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@random_cache_name
Patch Set: Address comments from PS2 Created 5 years, 1 month 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
« no previous file with comments | « no previous file | content/browser/cache_storage/cache_storage_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <set>
7 #include <string> 8 #include <string>
8 9
9 #include "base/barrier_closure.h" 10 #include "base/barrier_closure.h"
10 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
11 #include "base/files/memory_mapped_file.h" 12 #include "base/files/memory_mapped_file.h"
12 #include "base/guid.h" 13 #include "base/guid.h"
13 #include "base/location.h" 14 #include "base/location.h"
14 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
15 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
16 #include "base/numerics/safe_conversions.h" 17 #include "base/numerics/safe_conversions.h"
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, 319 base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile,
319 weak_ptr_factory_.GetWeakPtr(), base::Passed(&names), 320 weak_ptr_factory_.GetWeakPtr(), base::Passed(&names),
320 callback)); 321 callback));
321 } 322 }
322 323
323 void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string>> names, 324 void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string>> names,
324 const StringVectorCallback& callback, 325 const StringVectorCallback& callback,
325 const std::string& serialized) { 326 const std::string& serialized) {
326 DCHECK_CURRENTLY_ON(BrowserThread::IO); 327 DCHECK_CURRENTLY_ON(BrowserThread::IO);
327 328
329 scoped_ptr<std::set<std::string>> cache_dirs(new std::set<std::string>);
330
328 CacheStorageIndex index; 331 CacheStorageIndex index;
329 if (index.ParseFromString(serialized)) { 332 if (index.ParseFromString(serialized)) {
330 for (int i = 0, max = index.cache_size(); i < max; ++i) { 333 for (int i = 0, max = index.cache_size(); i < max; ++i) {
331 const CacheStorageIndex::Cache& cache = index.cache(i); 334 const CacheStorageIndex::Cache& cache = index.cache(i);
332 DCHECK(cache.has_cache_dir()); 335 DCHECK(cache.has_cache_dir());
333 names->push_back(cache.name()); 336 names->push_back(cache.name());
334 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); 337 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir();
338 cache_dirs->insert(cache.cache_dir());
335 } 339 }
336 } 340 }
337 341
338 // TODO(jkarlin): Delete caches that are in the directory and not returned 342 cache_task_runner_->PostTask(
339 // in LoadIndex. 343 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_,
344 base::Passed(&cache_dirs)));
340 callback.Run(names.Pass()); 345 callback.Run(names.Pass());
341 } 346 }
342 347
343 private: 348 private:
344 friend class MigratedLegacyCacheDirectoryNameTest; 349 friend class MigratedLegacyCacheDirectoryNameTest;
345 ~SimpleCacheLoader() override {} 350 ~SimpleCacheLoader() override {}
346 351
352 // Iterates over the caches and deletes any directory not found in
353 // |cache_dirs|. Runs on cache_task_runner_
354 static void DeleteUnreferencedCachesInPool(
355 const base::FilePath& cache_base_dir,
356 scoped_ptr<std::set<std::string>> cache_dirs) {
357 base::FileEnumerator file_enum(cache_base_dir, false /* recursive */,
358 base::FileEnumerator::DIRECTORIES);
359 std::vector<base::FilePath> dirs_to_delete;
360 base::FilePath cache_path;
361 while (!(cache_path = file_enum.Next()).empty()) {
362 if (!ContainsKey(*cache_dirs, cache_path.BaseName().AsUTF8Unsafe()))
363 dirs_to_delete.push_back(cache_path);
364 }
365
366 for (const base::FilePath& cache_path : dirs_to_delete)
367 base::DeleteFile(cache_path, true /* recursive */);
368 }
369
347 // Runs on cache_task_runner_ 370 // Runs on cache_task_runner_
348 static std::string MigrateCachesIfNecessaryInPool( 371 static std::string MigrateCachesIfNecessaryInPool(
349 const std::string& body, 372 const std::string& body,
350 const base::FilePath& index_path) { 373 const base::FilePath& index_path) {
351 CacheStorageIndex index; 374 CacheStorageIndex index;
352 if (!index.ParseFromString(body)) 375 if (!index.ParseFromString(body))
353 return body; 376 return body;
354 377
355 base::FilePath origin_path = index_path.DirName(); 378 base::FilePath origin_path = index_path.DirName();
356 bool index_is_dirty = false; 379 bool index_is_dirty = false;
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after
911 scoped_ptr<ServiceWorkerResponse> response, 934 scoped_ptr<ServiceWorkerResponse> response,
912 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { 935 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
913 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); 936 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
914 937
915 callback.Run(error, response.Pass(), blob_data_handle.Pass()); 938 callback.Run(error, response.Pass(), blob_data_handle.Pass());
916 if (cache_storage) 939 if (cache_storage)
917 scheduler_->CompleteOperationAndRunNext(); 940 scheduler_->CompleteOperationAndRunNext();
918 } 941 }
919 942
920 } // namespace content 943 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/cache_storage/cache_storage_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698