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 <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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, | 321 base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, |
321 weak_ptr_factory_.GetWeakPtr(), base::Passed(&names), | 322 weak_ptr_factory_.GetWeakPtr(), base::Passed(&names), |
322 callback)); | 323 callback)); |
323 } | 324 } |
324 | 325 |
325 void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string>> names, | 326 void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string>> names, |
326 const StringVectorCallback& callback, | 327 const StringVectorCallback& callback, |
327 const std::string& serialized) { | 328 const std::string& serialized) { |
328 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 329 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
329 | 330 |
331 scoped_ptr<std::set<std::string>> cache_dirs(new std::set<std::string>); | |
332 | |
330 CacheStorageIndex index; | 333 CacheStorageIndex index; |
331 if (index.ParseFromString(serialized)) { | 334 if (index.ParseFromString(serialized)) { |
332 for (int i = 0, max = index.cache_size(); i < max; ++i) { | 335 for (int i = 0, max = index.cache_size(); i < max; ++i) { |
333 const CacheStorageIndex::Cache& cache = index.cache(i); | 336 const CacheStorageIndex::Cache& cache = index.cache(i); |
334 DCHECK(cache.has_cache_dir()); | 337 DCHECK(cache.has_cache_dir()); |
335 names->push_back(cache.name()); | 338 names->push_back(cache.name()); |
336 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); | 339 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); |
340 cache_dirs->insert(cache.cache_dir()); | |
337 } | 341 } |
338 } | 342 } |
339 | 343 |
344 cache_task_runner_->PostTask( | |
345 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, | |
346 base::Passed(&cache_dirs))); | |
347 | |
340 // TODO(jkarlin): Delete caches that are in the directory and not returned | 348 // TODO(jkarlin): Delete caches that are in the directory and not returned |
jsbell
2015/10/23 21:20:22
Remove this TODO?
jkarlin
2015/10/23 23:08:45
Thanks! The whole purpose of the CL was to remove
| |
341 // in LoadIndex. | 349 // in LoadIndex. |
342 callback.Run(names.Pass()); | 350 callback.Run(names.Pass()); |
343 } | 351 } |
344 | 352 |
345 private: | 353 private: |
346 friend class MigratedLegacyCacheDirectoryNameTest; | 354 friend class MigratedLegacyCacheDirectoryNameTest; |
347 ~SimpleCacheLoader() override {} | 355 ~SimpleCacheLoader() override {} |
348 | 356 |
357 // Iterates over the caches and deletes any directory not found in | |
358 // |cache_dirs|. Runs on cache_task_runner_ | |
359 static void DeleteUnreferencedCachesInPool( | |
360 const base::FilePath& cache_base_dir, | |
361 scoped_ptr<std::set<std::string>> cache_dirs) { | |
362 base::FileEnumerator file_enum(cache_base_dir, false /* recursive */, | |
jsbell
2015/10/23 21:20:22
I'm suddenly paranoid that the docs for FileEnumer
jkarlin
2015/10/23 23:08:45
No longer deletes while iterating, just to be safe
| |
363 base::FileEnumerator::DIRECTORIES); | |
364 base::FilePath cache_path; | |
365 while (!(cache_path = file_enum.Next()).empty()) { | |
366 if (!ContainsKey(*cache_dirs, cache_path.BaseName().AsUTF8Unsafe())) | |
367 base::DeleteFile(cache_path, true /* recursive */); | |
368 } | |
369 } | |
370 | |
349 // Runs on cache_task_runner_ | 371 // Runs on cache_task_runner_ |
350 static std::string MigrateCachesIfNecessaryInPool( | 372 static std::string MigrateCachesIfNecessaryInPool( |
351 const std::string& body, | 373 const std::string& body, |
352 const base::FilePath& index_path) { | 374 const base::FilePath& index_path) { |
353 CacheStorageIndex index; | 375 CacheStorageIndex index; |
354 base::FilePath origin_path = index_path.DirName(); | 376 base::FilePath origin_path = index_path.DirName(); |
355 | 377 |
356 if (!index.ParseFromString(body)) | 378 if (!index.ParseFromString(body)) |
357 return body; | 379 return body; |
358 | 380 |
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
909 scoped_ptr<ServiceWorkerResponse> response, | 931 scoped_ptr<ServiceWorkerResponse> response, |
910 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 932 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
911 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); | 933 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); |
912 | 934 |
913 callback.Run(error, response.Pass(), blob_data_handle.Pass()); | 935 callback.Run(error, response.Pass(), blob_data_handle.Pass()); |
914 if (cache_storage) | 936 if (cache_storage) |
915 scheduler_->CompleteOperationAndRunNext(); | 937 scheduler_->CompleteOperationAndRunNext(); |
916 } | 938 } |
917 | 939 |
918 } // namespace content | 940 } // namespace content |
OLD | NEW |