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

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

Issue 2056983004: [CacheStorage] Give ownership of all CacheStorageCaches to CacheStorage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Self review Created 4 years, 6 months 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>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/barrier_closure.h" 13 #include "base/barrier_closure.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/files/memory_mapped_file.h" 15 #include "base/files/memory_mapped_file.h"
16 #include "base/guid.h" 16 #include "base/guid.h"
17 #include "base/location.h" 17 #include "base/location.h"
18 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
19 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
20 #include "base/metrics/histogram_macros.h" 20 #include "base/metrics/histogram_macros.h"
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_scheduler.h" 31 #include "content/browser/cache_storage/cache_storage_scheduler.h"
31 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
32 #include "net/base/directory_lister.h" 33 #include "net/base/directory_lister.h"
33 #include "net/base/net_errors.h" 34 #include "net/base/net_errors.h"
34 #include "net/url_request/url_request_context_getter.h" 35 #include "net/url_request/url_request_context_getter.h"
35 #include "storage/browser/blob/blob_storage_context.h" 36 #include "storage/browser/blob/blob_storage_context.h"
36 #include "storage/browser/quota/quota_manager_proxy.h" 37 #include "storage/browser/quota/quota_manager_proxy.h"
37 38
38 namespace content { 39 namespace content {
39 40
40 namespace { 41 namespace {
41 42
42 const int kCachePreservationInSecs = 30; 43 const int kCachePreservationInSecs = 30;
43 44
44 std::string HexedHash(const std::string& value) { 45 std::string HexedHash(const std::string& value) {
45 std::string value_hash = base::SHA1HashString(value); 46 std::string value_hash = base::SHA1HashString(value);
46 std::string valued_hexed_hash = base::ToLowerASCII( 47 std::string valued_hexed_hash = base::ToLowerASCII(
47 base::HexEncode(value_hash.c_str(), value_hash.length())); 48 base::HexEncode(value_hash.c_str(), value_hash.length()));
48 return valued_hexed_hash; 49 return valued_hexed_hash;
49 } 50 }
50 51
51 void SizeRetrievedFromCache(const scoped_refptr<CacheStorageCache>& cache, 52 void SizeRetrievedFromCache(
52 const base::Closure& closure, 53 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
53 int64_t* accumulator, 54 const base::Closure& closure,
54 int64_t size) { 55 int64_t* accumulator,
56 int64_t size) {
55 *accumulator += size; 57 *accumulator += size;
56 closure.Run(); 58 closure.Run();
57 } 59 }
58 60
59 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator, 61 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator,
60 const CacheStorage::SizeCallback& callback) { 62 const CacheStorage::SizeCallback& callback) {
61 base::ThreadTaskRunnerHandle::Get()->PostTask( 63 base::ThreadTaskRunnerHandle::Get()->PostTask(
62 FROM_HERE, base::Bind(callback, *accumulator)); 64 FROM_HERE, base::Bind(callback, *accumulator));
63 } 65 }
64 66
65 } // namespace 67 } // namespace
66 68
67 const char CacheStorage::kIndexFileName[] = "index.txt"; 69 const char CacheStorage::kIndexFileName[] = "index.txt";
68 70
69 struct CacheStorage::CacheMatchResponse { 71 struct CacheStorage::CacheMatchResponse {
70 CacheMatchResponse() = default; 72 CacheMatchResponse() = default;
71 ~CacheMatchResponse() = default; 73 ~CacheMatchResponse() = default;
72 74
73 CacheStorageError error; 75 CacheStorageError error;
74 std::unique_ptr<ServiceWorkerResponse> service_worker_response; 76 std::unique_ptr<ServiceWorkerResponse> service_worker_response;
75 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; 77 std::unique_ptr<storage::BlobDataHandle> blob_data_handle;
76 }; 78 };
77 79
78 // Handles the loading and clean up of CacheStorageCache objects. 80 // Handles the loading and clean up of CacheStorageCache objects.
79 class CacheStorage::CacheLoader { 81 class CacheStorage::CacheLoader {
80 public: 82 public:
81 typedef base::Callback<void(scoped_refptr<CacheStorageCache>)> CacheCallback; 83 typedef base::Callback<void(std::unique_ptr<CacheStorageCache>)>
84 CacheCallback;
82 typedef base::Callback<void(bool)> BoolCallback; 85 typedef base::Callback<void(bool)> BoolCallback;
83 typedef base::Callback<void(std::unique_ptr<std::vector<std::string>>)> 86 typedef base::Callback<void(std::unique_ptr<std::vector<std::string>>)>
84 StringVectorCallback; 87 StringVectorCallback;
85 88
86 CacheLoader( 89 CacheLoader(
87 base::SequencedTaskRunner* cache_task_runner, 90 base::SequencedTaskRunner* cache_task_runner,
88 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 91 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
89 storage::QuotaManagerProxy* quota_manager_proxy, 92 storage::QuotaManagerProxy* quota_manager_proxy,
90 base::WeakPtr<storage::BlobStorageContext> blob_context, 93 base::WeakPtr<storage::BlobStorageContext> blob_context,
94 CacheStorage* cache_storage,
91 const GURL& origin) 95 const GURL& origin)
92 : cache_task_runner_(cache_task_runner), 96 : cache_task_runner_(cache_task_runner),
93 request_context_getter_(request_context_getter), 97 request_context_getter_(request_context_getter),
94 quota_manager_proxy_(quota_manager_proxy), 98 quota_manager_proxy_(quota_manager_proxy),
95 blob_context_(blob_context), 99 blob_context_(blob_context),
100 cache_storage_(cache_storage),
96 origin_(origin) { 101 origin_(origin) {
97 DCHECK(!origin_.is_empty()); 102 DCHECK(!origin_.is_empty());
98 } 103 }
99 104
100 virtual ~CacheLoader() {} 105 virtual ~CacheLoader() {}
101 106
102 // Creates a CacheStorageCache with the given name. It does not attempt to 107 // Creates a CacheStorageCache with the given name. It does not attempt to
103 // load the backend, that happens lazily when the cache is used. 108 // load the backend, that happens lazily when the cache is used.
104 virtual scoped_refptr<CacheStorageCache> CreateCache( 109 virtual std::unique_ptr<CacheStorageCache> CreateCache(
105 const std::string& cache_name) = 0; 110 const std::string& cache_name) = 0;
106 111
107 // Deletes any pre-existing cache of the same name and then loads it. 112 // Deletes any pre-existing cache of the same name and then loads it.
108 virtual void PrepareNewCacheDestination(const std::string& cache_name, 113 virtual void PrepareNewCacheDestination(const std::string& cache_name,
109 const CacheCallback& callback) = 0; 114 const CacheCallback& callback) = 0;
110 115
111 // After the backend has been deleted, do any extra house keeping such as 116 // After the backend has been deleted, do any extra house keeping such as
112 // removing the cache's directory. 117 // removing the cache's directory.
113 virtual void CleanUpDeletedCache(const std::string& key, 118 virtual void CleanUpDeletedCache(const std::string& key,
114 const BoolCallback& callback) = 0; 119 const BoolCallback& callback) = 0;
115 120
116 // Writes the cache names (and sizes) to disk if applicable. 121 // Writes the cache names (and sizes) to disk if applicable.
117 virtual void WriteIndex(const StringVector& cache_names, 122 virtual void WriteIndex(const StringVector& cache_names,
118 const BoolCallback& callback) = 0; 123 const BoolCallback& callback) = 0;
119 124
120 // Loads the cache names from disk if applicable. 125 // Loads the cache names from disk if applicable.
121 virtual void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, 126 virtual void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names,
122 const StringVectorCallback& callback) = 0; 127 const StringVectorCallback& callback) = 0;
123 128
124 protected: 129 protected:
125 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; 130 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
126 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 131 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
127 132
128 // Owned by CacheStorage which owns this. 133 // Owned by CacheStorage which owns this.
129 storage::QuotaManagerProxy* quota_manager_proxy_; 134 storage::QuotaManagerProxy* quota_manager_proxy_;
130 135
131 base::WeakPtr<storage::BlobStorageContext> blob_context_; 136 base::WeakPtr<storage::BlobStorageContext> blob_context_;
137
138 // Raw pointer is safe because this object is owned by cache_storage_.
139 CacheStorage* cache_storage_;
140
132 GURL origin_; 141 GURL origin_;
133 }; 142 };
134 143
135 // Creates memory-only ServiceWorkerCaches. Because these caches have no 144 // Creates memory-only ServiceWorkerCaches. Because these caches have no
136 // persistent storage it is not safe to free them from memory if they might be 145 // persistent storage it is not safe to free them from memory if they might be
137 // used again. Therefore this class holds a reference to each cache until the 146 // used again. Therefore this class holds a reference to each cache until the
138 // cache is deleted. 147 // cache is deleted.
139 class CacheStorage::MemoryLoader : public CacheStorage::CacheLoader { 148 class CacheStorage::MemoryLoader : public CacheStorage::CacheLoader {
140 public: 149 public:
141 MemoryLoader(base::SequencedTaskRunner* cache_task_runner, 150 MemoryLoader(base::SequencedTaskRunner* cache_task_runner,
142 scoped_refptr<net::URLRequestContextGetter> request_context, 151 scoped_refptr<net::URLRequestContextGetter> request_context,
143 storage::QuotaManagerProxy* quota_manager_proxy, 152 storage::QuotaManagerProxy* quota_manager_proxy,
144 base::WeakPtr<storage::BlobStorageContext> blob_context, 153 base::WeakPtr<storage::BlobStorageContext> blob_context,
154 CacheStorage* cache_storage,
145 const GURL& origin) 155 const GURL& origin)
146 : CacheLoader(cache_task_runner, 156 : CacheLoader(cache_task_runner,
147 request_context, 157 request_context,
148 quota_manager_proxy, 158 quota_manager_proxy,
149 blob_context, 159 blob_context,
160 cache_storage,
150 origin) {} 161 origin) {}
151 162
152 scoped_refptr<CacheStorageCache> CreateCache( 163 std::unique_ptr<CacheStorageCache> CreateCache(
153 const std::string& cache_name) override { 164 const std::string& cache_name) override {
154 return CacheStorageCache::CreateMemoryCache( 165 return CacheStorageCache::CreateMemoryCache(
155 origin_, cache_name, request_context_getter_, quota_manager_proxy_, 166 origin_, cache_name, cache_storage_, request_context_getter_,
156 blob_context_); 167 quota_manager_proxy_, blob_context_);
157 } 168 }
158 169
159 void PrepareNewCacheDestination(const std::string& cache_name, 170 void PrepareNewCacheDestination(const std::string& cache_name,
160 const CacheCallback& callback) override { 171 const CacheCallback& callback) override {
161 scoped_refptr<CacheStorageCache> cache = CreateCache(cache_name); 172 std::unique_ptr<CacheStorageCache> cache = CreateCache(cache_name);
162 cache_refs_.insert(std::make_pair(cache_name, cache));
163 callback.Run(std::move(cache)); 173 callback.Run(std::move(cache));
164 } 174 }
165 175
166 void CleanUpDeletedCache(const std::string& cache_name, 176 void CleanUpDeletedCache(const std::string& cache_name,
167 const BoolCallback& callback) override { 177 const BoolCallback& callback) override {
168 CacheRefMap::iterator it = cache_refs_.find(cache_name); 178 CacheHandles::iterator it = cache_handles_.find(cache_name);
169 DCHECK(it != cache_refs_.end()); 179 DCHECK(it != cache_handles_.end());
170 cache_refs_.erase(it); 180 cache_handles_.erase(it);
171 callback.Run(true); 181 callback.Run(true);
172 } 182 }
173 183
174 void WriteIndex(const StringVector& cache_names, 184 void WriteIndex(const StringVector& cache_names,
175 const BoolCallback& callback) override { 185 const BoolCallback& callback) override {
176 callback.Run(false); 186 callback.Run(false);
177 } 187 }
178 188
179 void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, 189 void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names,
180 const StringVectorCallback& callback) override { 190 const StringVectorCallback& callback) override {
181 callback.Run(std::move(cache_names)); 191 callback.Run(std::move(cache_names));
182 } 192 }
183 193
194 void StoreCacheHandle(const std::string& cache_name,
195 std::unique_ptr<CacheStorageCacheHandle> cache_handle) {
196 DCHECK(!ContainsKey(cache_handles_, cache_name));
197 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle)));
198 }
199
184 private: 200 private:
185 typedef std::map<std::string, scoped_refptr<CacheStorageCache>> CacheRefMap; 201 typedef std::map<std::string, std::unique_ptr<CacheStorageCacheHandle>>
202 CacheHandles;
186 ~MemoryLoader() override {} 203 ~MemoryLoader() override {}
187 204
188 // Keep a reference to each cache to ensure that it's not freed before the 205 // Keep a reference to each cache to ensure that it's not freed before the
189 // client calls CacheStorage::Delete or the CacheStorage is 206 // client calls CacheStorage::Delete or the CacheStorage is
190 // freed. 207 // freed.
191 CacheRefMap cache_refs_; 208 CacheHandles cache_handles_;
192 }; 209 };
193 210
194 class CacheStorage::SimpleCacheLoader : public CacheStorage::CacheLoader { 211 class CacheStorage::SimpleCacheLoader : public CacheStorage::CacheLoader {
195 public: 212 public:
196 SimpleCacheLoader(const base::FilePath& origin_path, 213 SimpleCacheLoader(const base::FilePath& origin_path,
197 base::SequencedTaskRunner* cache_task_runner, 214 base::SequencedTaskRunner* cache_task_runner,
198 scoped_refptr<net::URLRequestContextGetter> request_context, 215 scoped_refptr<net::URLRequestContextGetter> request_context,
199 storage::QuotaManagerProxy* quota_manager_proxy, 216 storage::QuotaManagerProxy* quota_manager_proxy,
200 base::WeakPtr<storage::BlobStorageContext> blob_context, 217 base::WeakPtr<storage::BlobStorageContext> blob_context,
218 CacheStorage* cache_storage,
201 const GURL& origin) 219 const GURL& origin)
202 : CacheLoader(cache_task_runner, 220 : CacheLoader(cache_task_runner,
203 request_context, 221 request_context,
204 quota_manager_proxy, 222 quota_manager_proxy,
205 blob_context, 223 blob_context,
224 cache_storage,
206 origin), 225 origin),
207 origin_path_(origin_path), 226 origin_path_(origin_path),
208 weak_ptr_factory_(this) {} 227 weak_ptr_factory_(this) {}
209 228
210 scoped_refptr<CacheStorageCache> CreateCache( 229 std::unique_ptr<CacheStorageCache> CreateCache(
211 const std::string& cache_name) override { 230 const std::string& cache_name) override {
212 DCHECK_CURRENTLY_ON(BrowserThread::IO); 231 DCHECK_CURRENTLY_ON(BrowserThread::IO);
213 DCHECK(ContainsKey(cache_name_to_cache_dir_, cache_name)); 232 DCHECK(ContainsKey(cache_name_to_cache_dir_, cache_name));
214 233
215 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; 234 std::string cache_dir = cache_name_to_cache_dir_[cache_name];
216 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); 235 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir);
217 return CacheStorageCache::CreatePersistentCache( 236 return CacheStorageCache::CreatePersistentCache(
218 origin_, cache_name, cache_path, request_context_getter_, 237 origin_, cache_name, cache_storage_, cache_path,
219 quota_manager_proxy_, blob_context_); 238 request_context_getter_, quota_manager_proxy_, blob_context_);
220 } 239 }
221 240
222 void PrepareNewCacheDestination(const std::string& cache_name, 241 void PrepareNewCacheDestination(const std::string& cache_name,
223 const CacheCallback& callback) override { 242 const CacheCallback& callback) override {
224 DCHECK_CURRENTLY_ON(BrowserThread::IO); 243 DCHECK_CURRENTLY_ON(BrowserThread::IO);
225 244
226 PostTaskAndReplyWithResult( 245 PostTaskAndReplyWithResult(
227 cache_task_runner_.get(), FROM_HERE, 246 cache_task_runner_.get(), FROM_HERE,
228 base::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, 247 base::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool,
229 origin_path_), 248 origin_path_),
(...skipping 11 matching lines...) Expand all
241 cache_path = origin_path.AppendASCII(cache_dir); 260 cache_path = origin_path.AppendASCII(cache_dir);
242 } while (base::PathExists(cache_path)); 261 } while (base::PathExists(cache_path));
243 262
244 return base::CreateDirectory(cache_path) ? cache_dir : ""; 263 return base::CreateDirectory(cache_path) ? cache_dir : "";
245 } 264 }
246 265
247 void PrepareNewCacheCreateCache(const std::string& cache_name, 266 void PrepareNewCacheCreateCache(const std::string& cache_name,
248 const CacheCallback& callback, 267 const CacheCallback& callback,
249 const std::string& cache_dir) { 268 const std::string& cache_dir) {
250 if (cache_dir.empty()) { 269 if (cache_dir.empty()) {
251 callback.Run(scoped_refptr<CacheStorageCache>()); 270 callback.Run(std::unique_ptr<CacheStorageCache>());
252 return; 271 return;
253 } 272 }
254 273
255 cache_name_to_cache_dir_[cache_name] = cache_dir; 274 cache_name_to_cache_dir_[cache_name] = cache_dir;
256 callback.Run(CreateCache(cache_name)); 275 callback.Run(CreateCache(cache_name));
257 } 276 }
258 277
259 void CleanUpDeletedCache(const std::string& cache_name, 278 void CleanUpDeletedCache(const std::string& cache_name,
260 const BoolCallback& callback) override { 279 const BoolCallback& callback) override {
261 DCHECK_CURRENTLY_ON(BrowserThread::IO); 280 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 CacheStorage::CacheStorage( 479 CacheStorage::CacheStorage(
461 const base::FilePath& path, 480 const base::FilePath& path,
462 bool memory_only, 481 bool memory_only,
463 base::SequencedTaskRunner* cache_task_runner, 482 base::SequencedTaskRunner* cache_task_runner,
464 scoped_refptr<net::URLRequestContextGetter> request_context, 483 scoped_refptr<net::URLRequestContextGetter> request_context,
465 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 484 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
466 base::WeakPtr<storage::BlobStorageContext> blob_context, 485 base::WeakPtr<storage::BlobStorageContext> blob_context,
467 const GURL& origin) 486 const GURL& origin)
468 : initialized_(false), 487 : initialized_(false),
469 initializing_(false), 488 initializing_(false),
489 memory_only_(memory_only),
470 scheduler_(new CacheStorageScheduler()), 490 scheduler_(new CacheStorageScheduler()),
471 origin_path_(path), 491 origin_path_(path),
472 cache_task_runner_(cache_task_runner), 492 cache_task_runner_(cache_task_runner),
473 quota_manager_proxy_(quota_manager_proxy), 493 quota_manager_proxy_(quota_manager_proxy),
474 origin_(origin), 494 origin_(origin),
475 weak_factory_(this) { 495 weak_factory_(this) {
476 if (memory_only) 496 if (memory_only)
477 cache_loader_.reset( 497 cache_loader_.reset(new MemoryLoader(
478 new MemoryLoader(cache_task_runner_.get(), std::move(request_context), 498 cache_task_runner_.get(), std::move(request_context),
479 quota_manager_proxy.get(), blob_context, origin)); 499 quota_manager_proxy.get(), blob_context, this, origin));
480 else 500 else
481 cache_loader_.reset(new SimpleCacheLoader( 501 cache_loader_.reset(new SimpleCacheLoader(
482 origin_path_, cache_task_runner_.get(), std::move(request_context), 502 origin_path_, cache_task_runner_.get(), std::move(request_context),
483 quota_manager_proxy.get(), blob_context, origin)); 503 quota_manager_proxy.get(), blob_context, this, origin));
484 } 504 }
485 505
486 CacheStorage::~CacheStorage() { 506 CacheStorage::~CacheStorage() {
487 } 507 }
488 508
489 void CacheStorage::OpenCache(const std::string& cache_name, 509 void CacheStorage::OpenCache(const std::string& cache_name,
490 const CacheAndErrorCallback& callback) { 510 const CacheAndErrorCallback& callback) {
491 DCHECK_CURRENTLY_ON(BrowserThread::IO); 511 DCHECK_CURRENTLY_ON(BrowserThread::IO);
492 512
493 if (!initialized_) 513 if (!initialized_)
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 base::Bind(&CacheStorage::LazyInitDidLoadIndex, 689 base::Bind(&CacheStorage::LazyInitDidLoadIndex,
670 weak_factory_.GetWeakPtr())); 690 weak_factory_.GetWeakPtr()));
671 } 691 }
672 692
673 void CacheStorage::LazyInitDidLoadIndex( 693 void CacheStorage::LazyInitDidLoadIndex(
674 std::unique_ptr<std::vector<std::string>> indexed_cache_names) { 694 std::unique_ptr<std::vector<std::string>> indexed_cache_names) {
675 DCHECK_CURRENTLY_ON(BrowserThread::IO); 695 DCHECK_CURRENTLY_ON(BrowserThread::IO);
676 696
677 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) { 697 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) {
678 cache_map_.insert(std::make_pair(indexed_cache_names->at(i), 698 cache_map_.insert(std::make_pair(indexed_cache_names->at(i),
679 base::WeakPtr<CacheStorageCache>())); 699 std::unique_ptr<CacheStorageCache>()));
680 ordered_cache_names_.push_back(indexed_cache_names->at(i)); 700 ordered_cache_names_.push_back(indexed_cache_names->at(i));
681 } 701 }
682 702
683 initializing_ = false; 703 initializing_ = false;
684 initialized_ = true; 704 initialized_ = true;
685 705
686 scheduler_->CompleteOperationAndRunNext(); 706 scheduler_->CompleteOperationAndRunNext();
687 } 707 }
688 708
689 void CacheStorage::OpenCacheImpl(const std::string& cache_name, 709 void CacheStorage::OpenCacheImpl(const std::string& cache_name,
690 const CacheAndErrorCallback& callback) { 710 const CacheAndErrorCallback& callback) {
691 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); 711 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
692 if (cache.get()) { 712 GetLoadedCache(cache_name);
693 callback.Run(std::move(cache), CACHE_STORAGE_OK); 713 if (cache_handle) {
714 callback.Run(std::move(cache_handle), CACHE_STORAGE_OK);
694 return; 715 return;
695 } 716 }
696 717
697 cache_loader_->PrepareNewCacheDestination( 718 cache_loader_->PrepareNewCacheDestination(
698 cache_name, base::Bind(&CacheStorage::CreateCacheDidCreateCache, 719 cache_name, base::Bind(&CacheStorage::CreateCacheDidCreateCache,
699 weak_factory_.GetWeakPtr(), cache_name, callback)); 720 weak_factory_.GetWeakPtr(), cache_name, callback));
700 } 721 }
701 722
702 void CacheStorage::CreateCacheDidCreateCache( 723 void CacheStorage::CreateCacheDidCreateCache(
703 const std::string& cache_name, 724 const std::string& cache_name,
704 const CacheAndErrorCallback& callback, 725 const CacheAndErrorCallback& callback,
705 scoped_refptr<CacheStorageCache> cache) { 726 std::unique_ptr<CacheStorageCache> cache) {
706 DCHECK_CURRENTLY_ON(BrowserThread::IO); 727 DCHECK_CURRENTLY_ON(BrowserThread::IO);
707 728
708 UMA_HISTOGRAM_BOOLEAN("ServiceWorkerCache.CreateCacheStorageResult", 729 UMA_HISTOGRAM_BOOLEAN("ServiceWorkerCache.CreateCacheStorageResult",
709 static_cast<bool>(cache)); 730 static_cast<bool>(cache));
710 731
711 if (!cache.get()) { 732 if (!cache) {
712 callback.Run(scoped_refptr<CacheStorageCache>(), 733 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(),
713 CACHE_STORAGE_ERROR_STORAGE); 734 CACHE_STORAGE_ERROR_STORAGE);
714 return; 735 return;
715 } 736 }
716 737
717 cache_map_.insert(std::make_pair(cache_name, cache->AsWeakPtr())); 738 CacheStorageCache* cache_ptr = cache.get();
739
740 cache_map_.insert(std::make_pair(cache_name, std::move(cache)));
718 ordered_cache_names_.push_back(cache_name); 741 ordered_cache_names_.push_back(cache_name);
719 742
720 TemporarilyPreserveCache(cache); 743 if (memory_only_) {
744 static_cast<MemoryLoader*>(cache_loader_.get())
745 ->StoreCacheHandle(cache_name, CreateCacheHandle(cache_ptr));
746 }
747
748 TemporarilyPreserveCache(CreateCacheHandle(cache_ptr));
721 749
722 cache_loader_->WriteIndex( 750 cache_loader_->WriteIndex(
723 ordered_cache_names_, 751 ordered_cache_names_,
724 base::Bind(&CacheStorage::CreateCacheDidWriteIndex, 752 base::Bind(&CacheStorage::CreateCacheDidWriteIndex,
725 weak_factory_.GetWeakPtr(), callback, std::move(cache))); 753 weak_factory_.GetWeakPtr(), callback,
754 base::Passed(CreateCacheHandle(cache_ptr))));
726 } 755 }
727 756
728 void CacheStorage::CreateCacheDidWriteIndex( 757 void CacheStorage::CreateCacheDidWriteIndex(
729 const CacheAndErrorCallback& callback, 758 const CacheAndErrorCallback& callback,
730 scoped_refptr<CacheStorageCache> cache, 759 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
731 bool success) { 760 bool success) {
732 DCHECK_CURRENTLY_ON(BrowserThread::IO); 761 DCHECK_CURRENTLY_ON(BrowserThread::IO);
733 DCHECK(cache.get()); 762 DCHECK(cache_handle);
734 763
735 // TODO(jkarlin): Handle !success. 764 // TODO(jkarlin): Handle !success.
736 765
737 callback.Run(std::move(cache), CACHE_STORAGE_OK); 766 callback.Run(std::move(cache_handle), CACHE_STORAGE_OK);
738 } 767 }
739 768
740 void CacheStorage::HasCacheImpl(const std::string& cache_name, 769 void CacheStorage::HasCacheImpl(const std::string& cache_name,
741 const BoolAndErrorCallback& callback) { 770 const BoolAndErrorCallback& callback) {
742 bool has_cache = ContainsKey(cache_map_, cache_name); 771 bool has_cache = ContainsKey(cache_map_, cache_name);
743 callback.Run(has_cache, CACHE_STORAGE_OK); 772 callback.Run(has_cache, CACHE_STORAGE_OK);
744 } 773 }
745 774
746 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, 775 void CacheStorage::DeleteCacheImpl(const std::string& cache_name,
747 const BoolAndErrorCallback& callback) { 776 const BoolAndErrorCallback& callback) {
748 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); 777 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
749 if (!cache.get()) { 778 GetLoadedCache(cache_name);
779 if (!cache_handle) {
750 base::ThreadTaskRunnerHandle::Get()->PostTask( 780 base::ThreadTaskRunnerHandle::Get()->PostTask(
751 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND)); 781 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND));
752 return; 782 return;
753 } 783 }
754 784
755 CacheMap::iterator map_iter = cache_map_.find(cache_name); 785 CacheMap::iterator map_iter = cache_map_.find(cache_name);
786 deleted_caches_.insert(
787 std::make_pair(cache_handle->value(), std::move(map_iter->second)));
756 cache_map_.erase(map_iter); 788 cache_map_.erase(map_iter);
757 789
758 // Delete the name from ordered_cache_names_. 790 // Delete the name from ordered_cache_names_.
759 StringVector::iterator iter = std::find( 791 StringVector::iterator iter = std::find(
760 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name); 792 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name);
761 DCHECK(iter != ordered_cache_names_.end()); 793 DCHECK(iter != ordered_cache_names_.end());
762 ordered_cache_names_.erase(iter); 794 ordered_cache_names_.erase(iter);
763 795
764 cache->GetSizeThenClose(base::Bind(&CacheStorage::DeleteCacheDidClose, 796 CacheStorageCache* cache_ptr = cache_handle->value();
765 weak_factory_.GetWeakPtr(), cache_name, 797 cache_ptr->GetSizeThenClose(
766 callback, ordered_cache_names_, cache)); 798 base::Bind(&CacheStorage::DeleteCacheDidClose, weak_factory_.GetWeakPtr(),
799 cache_name, callback, ordered_cache_names_,
800 base::Passed(std::move(cache_handle))));
767 } 801 }
768 802
769 void CacheStorage::DeleteCacheDidClose(const std::string& cache_name, 803 void CacheStorage::DeleteCacheDidClose(
770 const BoolAndErrorCallback& callback, 804 const std::string& cache_name,
771 const StringVector& ordered_cache_names, 805 const BoolAndErrorCallback& callback,
772 scoped_refptr<CacheStorageCache> cache, 806 const StringVector& ordered_cache_names,
773 int64_t cache_size) { 807 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
808 int64_t cache_size) {
774 cache_loader_->WriteIndex( 809 cache_loader_->WriteIndex(
775 ordered_cache_names, 810 ordered_cache_names,
776 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex, 811 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex,
777 weak_factory_.GetWeakPtr(), cache_name, callback, cache_size)); 812 weak_factory_.GetWeakPtr(), cache_name, callback, cache_size));
778 } 813 }
779 814
780 void CacheStorage::DeleteCacheDidWriteIndex( 815 void CacheStorage::DeleteCacheDidWriteIndex(
781 const std::string& cache_name, 816 const std::string& cache_name,
782 const BoolAndErrorCallback& callback, 817 const BoolAndErrorCallback& callback,
783 int cache_size, 818 int cache_size,
(...skipping 18 matching lines...) Expand all
802 837
803 void CacheStorage::EnumerateCachesImpl( 838 void CacheStorage::EnumerateCachesImpl(
804 const StringsAndErrorCallback& callback) { 839 const StringsAndErrorCallback& callback) {
805 callback.Run(ordered_cache_names_, CACHE_STORAGE_OK); 840 callback.Run(ordered_cache_names_, CACHE_STORAGE_OK);
806 } 841 }
807 842
808 void CacheStorage::MatchCacheImpl( 843 void CacheStorage::MatchCacheImpl(
809 const std::string& cache_name, 844 const std::string& cache_name,
810 std::unique_ptr<ServiceWorkerFetchRequest> request, 845 std::unique_ptr<ServiceWorkerFetchRequest> request,
811 const CacheStorageCache::ResponseCallback& callback) { 846 const CacheStorageCache::ResponseCallback& callback) {
812 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); 847 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
848 GetLoadedCache(cache_name);
813 849
814 if (!cache.get()) { 850 if (!cache_handle) {
815 callback.Run(CACHE_STORAGE_ERROR_CACHE_NAME_NOT_FOUND, 851 callback.Run(CACHE_STORAGE_ERROR_CACHE_NAME_NOT_FOUND,
816 std::unique_ptr<ServiceWorkerResponse>(), 852 std::unique_ptr<ServiceWorkerResponse>(),
817 std::unique_ptr<storage::BlobDataHandle>()); 853 std::unique_ptr<storage::BlobDataHandle>());
818 return; 854 return;
819 } 855 }
820 856
821 // Pass the cache along to the callback to keep the cache open until match is 857 // Pass the cache handle along to the callback to keep the cache open until
822 // done. 858 // match is done.
823 cache->Match(std::move(request), 859 CacheStorageCache* cache_ptr = cache_handle->value();
824 base::Bind(&CacheStorage::MatchCacheDidMatch, 860 cache_ptr->Match(
825 weak_factory_.GetWeakPtr(), cache, callback)); 861 std::move(request),
862 base::Bind(&CacheStorage::MatchCacheDidMatch, weak_factory_.GetWeakPtr(),
863 base::Passed(std::move(cache_handle)), callback));
826 } 864 }
827 865
828 void CacheStorage::MatchCacheDidMatch( 866 void CacheStorage::MatchCacheDidMatch(
829 scoped_refptr<CacheStorageCache> cache, 867 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
830 const CacheStorageCache::ResponseCallback& callback, 868 const CacheStorageCache::ResponseCallback& callback,
831 CacheStorageError error, 869 CacheStorageError error,
832 std::unique_ptr<ServiceWorkerResponse> response, 870 std::unique_ptr<ServiceWorkerResponse> response,
833 std::unique_ptr<storage::BlobDataHandle> handle) { 871 std::unique_ptr<storage::BlobDataHandle> handle) {
834 callback.Run(error, std::move(response), std::move(handle)); 872 callback.Run(error, std::move(response), std::move(handle));
835 } 873 }
836 874
837 void CacheStorage::MatchAllCachesImpl( 875 void CacheStorage::MatchAllCachesImpl(
838 std::unique_ptr<ServiceWorkerFetchRequest> request, 876 std::unique_ptr<ServiceWorkerFetchRequest> request,
839 const CacheStorageCache::ResponseCallback& callback) { 877 const CacheStorageCache::ResponseCallback& callback) {
840 std::vector<CacheMatchResponse>* match_responses = 878 std::vector<CacheMatchResponse>* match_responses =
841 new std::vector<CacheMatchResponse>(ordered_cache_names_.size()); 879 new std::vector<CacheMatchResponse>(ordered_cache_names_.size());
842 880
843 base::Closure barrier_closure = base::BarrierClosure( 881 base::Closure barrier_closure = base::BarrierClosure(
844 ordered_cache_names_.size(), 882 ordered_cache_names_.size(),
845 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll, 883 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll,
846 weak_factory_.GetWeakPtr(), 884 weak_factory_.GetWeakPtr(),
847 base::Passed(base::WrapUnique(match_responses)), callback)); 885 base::Passed(base::WrapUnique(match_responses)), callback));
848 886
849 for (size_t i = 0, max = ordered_cache_names_.size(); i < max; ++i) { 887 for (size_t i = 0, max = ordered_cache_names_.size(); i < max; ++i) {
850 scoped_refptr<CacheStorageCache> cache = 888 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
851 GetLoadedCache(ordered_cache_names_[i]); 889 GetLoadedCache(ordered_cache_names_[i]);
852 DCHECK(cache.get()); 890 DCHECK(cache_handle);
853 891
854 cache->Match(base::WrapUnique(new ServiceWorkerFetchRequest(*request)), 892 CacheStorageCache* cache_ptr = cache_handle->value();
855 base::Bind(&CacheStorage::MatchAllCachesDidMatch, 893 cache_ptr->Match(base::WrapUnique(new ServiceWorkerFetchRequest(*request)),
856 weak_factory_.GetWeakPtr(), cache, 894 base::Bind(&CacheStorage::MatchAllCachesDidMatch,
857 &match_responses->at(i), barrier_closure)); 895 weak_factory_.GetWeakPtr(),
896 base::Passed(std::move(cache_handle)),
897 &match_responses->at(i), barrier_closure));
858 } 898 }
859 } 899 }
860 900
861 void CacheStorage::MatchAllCachesDidMatch( 901 void CacheStorage::MatchAllCachesDidMatch(
862 scoped_refptr<CacheStorageCache> cache, 902 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
863 CacheMatchResponse* out_match_response, 903 CacheMatchResponse* out_match_response,
864 const base::Closure& barrier_closure, 904 const base::Closure& barrier_closure,
865 CacheStorageError error, 905 CacheStorageError error,
866 std::unique_ptr<ServiceWorkerResponse> service_worker_response, 906 std::unique_ptr<ServiceWorkerResponse> service_worker_response,
867 std::unique_ptr<storage::BlobDataHandle> handle) { 907 std::unique_ptr<storage::BlobDataHandle> handle) {
868 out_match_response->error = error; 908 out_match_response->error = error;
869 out_match_response->service_worker_response = 909 out_match_response->service_worker_response =
870 std::move(service_worker_response); 910 std::move(service_worker_response);
871 out_match_response->blob_data_handle = std::move(handle); 911 out_match_response->blob_data_handle = std::move(handle);
872 barrier_closure.Run(); 912 barrier_closure.Run();
873 } 913 }
874 914
875 void CacheStorage::MatchAllCachesDidMatchAll( 915 void CacheStorage::MatchAllCachesDidMatchAll(
876 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, 916 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses,
877 const CacheStorageCache::ResponseCallback& callback) { 917 const CacheStorageCache::ResponseCallback& callback) {
878 for (CacheMatchResponse& match_response : *match_responses) { 918 for (CacheMatchResponse& match_response : *match_responses) {
879 if (match_response.error == CACHE_STORAGE_ERROR_NOT_FOUND) 919 if (match_response.error == CACHE_STORAGE_ERROR_NOT_FOUND)
880 continue; 920 continue;
881 callback.Run(match_response.error, 921 callback.Run(match_response.error,
882 std::move(match_response.service_worker_response), 922 std::move(match_response.service_worker_response),
883 std::move(match_response.blob_data_handle)); 923 std::move(match_response.blob_data_handle));
884 return; 924 return;
885 } 925 }
886 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, 926 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND,
887 std::unique_ptr<ServiceWorkerResponse>(), 927 std::unique_ptr<ServiceWorkerResponse>(),
888 std::unique_ptr<storage::BlobDataHandle>()); 928 std::unique_ptr<storage::BlobDataHandle>());
889 } 929 }
890 930
891 scoped_refptr<CacheStorageCache> CacheStorage::GetLoadedCache( 931 void CacheStorage::AddCacheHandleRef(CacheStorageCache* cache) {
horo 2016/06/14 06:31:12 Add DCHECK_CURRENTLY_ON().
jkarlin 2016/06/14 06:41:08 Done.
932 auto iter = cache_handle_counts_.find(cache);
933 if (iter == cache_handle_counts_.end()) {
934 cache_handle_counts_[cache] = 1;
935 return;
936 }
937
938 iter->second += 1;
939 }
940
941 void CacheStorage::DropCacheHandleRef(CacheStorageCache* cache) {
horo 2016/06/14 06:31:12 Add DCHECK_CURRENTLY_ON().
jkarlin 2016/06/14 06:41:08 Done.
942 auto iter = cache_handle_counts_.find(cache);
943 DCHECK(iter != cache_handle_counts_.end());
944 DCHECK(iter->second >= 1);
945
946 iter->second -= 1;
947 if (iter->second == 0) {
948 // Delete the CacheStorageCache object. It's either in the main cache map or
949 // the CacheStorage::Delete operation has run on the cache, in which case
950 // it's in the deleted caches map.
951 auto cache_map_iter = cache_map_.find(cache->cache_name());
952
953 if (cache_map_iter == cache_map_.end()) {
954 auto deleted_caches_iter = deleted_caches_.find(cache);
955 DCHECK(deleted_caches_iter != deleted_caches_.end());
956 deleted_caches_.erase(deleted_caches_iter);
957 return;
958 }
959
960 cache_map_iter->second.reset();
961 cache_handle_counts_.erase(iter);
962 }
963 }
964
965 std::unique_ptr<CacheStorageCacheHandle> CacheStorage::CreateCacheHandle(
966 CacheStorageCache* cache) {
967 DCHECK(cache);
968 return std::unique_ptr<CacheStorageCacheHandle>(new CacheStorageCacheHandle(
969 cache->AsWeakPtr(), weak_factory_.GetWeakPtr()));
970 }
971
972 std::unique_ptr<CacheStorageCacheHandle> CacheStorage::GetLoadedCache(
892 const std::string& cache_name) { 973 const std::string& cache_name) {
893 DCHECK_CURRENTLY_ON(BrowserThread::IO); 974 DCHECK_CURRENTLY_ON(BrowserThread::IO);
894 DCHECK(initialized_); 975 DCHECK(initialized_);
895 976
896 CacheMap::iterator map_iter = cache_map_.find(cache_name); 977 CacheMap::iterator map_iter = cache_map_.find(cache_name);
897 if (map_iter == cache_map_.end()) 978 if (map_iter == cache_map_.end())
898 return scoped_refptr<CacheStorageCache>(); 979 return std::unique_ptr<CacheStorageCacheHandle>();
899 980
900 base::WeakPtr<CacheStorageCache> cache = map_iter->second; 981 CacheStorageCache* cache = map_iter->second.get();
901 982
902 if (!cache) { 983 if (!cache) {
903 scoped_refptr<CacheStorageCache> new_cache = 984 std::unique_ptr<CacheStorageCache> new_cache =
904 cache_loader_->CreateCache(cache_name); 985 cache_loader_->CreateCache(cache_name);
905 map_iter->second = new_cache->AsWeakPtr(); 986 CacheStorageCache* cache_ptr = new_cache.get();
987 map_iter->second = std::move(new_cache);
906 988
907 TemporarilyPreserveCache(new_cache); 989 TemporarilyPreserveCache(CreateCacheHandle(cache_ptr));
908 return new_cache; 990 return CreateCacheHandle(cache_ptr);
909 } 991 }
910 992
911 return make_scoped_refptr(cache.get()); 993 return CreateCacheHandle(cache);
912 } 994 }
913 995
914 void CacheStorage::TemporarilyPreserveCache( 996 void CacheStorage::TemporarilyPreserveCache(
915 scoped_refptr<CacheStorageCache> cache) { 997 std::unique_ptr<CacheStorageCacheHandle> cache_handle) {
916 DCHECK_CURRENTLY_ON(BrowserThread::IO); 998 DCHECK_CURRENTLY_ON(BrowserThread::IO);
917 DCHECK(!ContainsKey(preserved_caches_, cache.get())); 999 DCHECK(!ContainsKey(preserved_caches_, cache_handle->value()));
918 1000
919 preserved_caches_[cache.get()] = cache; 1001 CacheStorageCache* cache_ptr = cache_handle->value();
920 SchedulePreservedCacheRemoval(base::Bind(&CacheStorage::RemovePreservedCache, 1002 SchedulePreservedCacheRemoval(base::Bind(&CacheStorage::RemovePreservedCache,
921 weak_factory_.GetWeakPtr(), 1003 weak_factory_.GetWeakPtr(),
922 base::Unretained(cache.get()))); 1004 cache_ptr));
1005 preserved_caches_[cache_ptr] = std::move(cache_handle);
923 } 1006 }
924 1007
925 void CacheStorage::SchedulePreservedCacheRemoval( 1008 void CacheStorage::SchedulePreservedCacheRemoval(
926 const base::Closure& callback) { 1009 const base::Closure& callback) {
927 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1010 DCHECK_CURRENTLY_ON(BrowserThread::IO);
928 1011
929 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 1012 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
930 FROM_HERE, callback, 1013 FROM_HERE, callback,
931 base::TimeDelta::FromSeconds(kCachePreservationInSecs)); 1014 base::TimeDelta::FromSeconds(kCachePreservationInSecs));
932 } 1015 }
(...skipping 11 matching lines...) Expand all
944 1027
945 std::unique_ptr<int64_t> accumulator(new int64_t(0)); 1028 std::unique_ptr<int64_t> accumulator(new int64_t(0));
946 int64_t* accumulator_ptr = accumulator.get(); 1029 int64_t* accumulator_ptr = accumulator.get();
947 1030
948 base::Closure barrier_closure = base::BarrierClosure( 1031 base::Closure barrier_closure = base::BarrierClosure(
949 ordered_cache_names_.size(), 1032 ordered_cache_names_.size(),
950 base::Bind(&SizeRetrievedFromAllCaches, 1033 base::Bind(&SizeRetrievedFromAllCaches,
951 base::Passed(std::move(accumulator)), callback)); 1034 base::Passed(std::move(accumulator)), callback));
952 1035
953 for (const std::string& cache_name : ordered_cache_names_) { 1036 for (const std::string& cache_name : ordered_cache_names_) {
954 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); 1037 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
955 cache->GetSizeThenClose(base::Bind(&SizeRetrievedFromCache, cache, 1038 GetLoadedCache(cache_name);
1039 CacheStorageCache* cache = cache_handle->value();
1040 cache->GetSizeThenClose(base::Bind(&SizeRetrievedFromCache,
1041 base::Passed(std::move(cache_handle)),
956 barrier_closure, accumulator_ptr)); 1042 barrier_closure, accumulator_ptr));
957 } 1043 }
958 } 1044 }
959 1045
960 void CacheStorage::SizeImpl(const SizeCallback& callback) { 1046 void CacheStorage::SizeImpl(const SizeCallback& callback) {
961 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1047 DCHECK_CURRENTLY_ON(BrowserThread::IO);
962 DCHECK(initialized_); 1048 DCHECK(initialized_);
963 1049
964 std::unique_ptr<int64_t> accumulator(new int64_t(0)); 1050 std::unique_ptr<int64_t> accumulator(new int64_t(0));
965 int64_t* accumulator_ptr = accumulator.get(); 1051 int64_t* accumulator_ptr = accumulator.get();
966 1052
967 base::Closure barrier_closure = base::BarrierClosure( 1053 base::Closure barrier_closure = base::BarrierClosure(
968 ordered_cache_names_.size(), 1054 ordered_cache_names_.size(),
969 base::Bind(&SizeRetrievedFromAllCaches, 1055 base::Bind(&SizeRetrievedFromAllCaches,
970 base::Passed(std::move(accumulator)), callback)); 1056 base::Passed(std::move(accumulator)), callback));
971 1057
972 for (const std::string& cache_name : ordered_cache_names_) { 1058 for (const std::string& cache_name : ordered_cache_names_) {
973 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); 1059 std::unique_ptr<CacheStorageCacheHandle> cache_handle =
974 cache->Size(base::Bind(&SizeRetrievedFromCache, cache, barrier_closure, 1060 GetLoadedCache(cache_name);
975 accumulator_ptr)); 1061 CacheStorageCache* cache = cache_handle->value();
1062 cache->Size(base::Bind(&SizeRetrievedFromCache,
1063 base::Passed(std::move(cache_handle)),
1064 barrier_closure, accumulator_ptr));
976 } 1065 }
977 } 1066 }
978 1067
979 void CacheStorage::PendingClosure(const base::Closure& callback) { 1068 void CacheStorage::PendingClosure(const base::Closure& callback) {
980 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); 1069 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
981 1070
982 callback.Run(); 1071 callback.Run();
983 if (cache_storage) 1072 if (cache_storage)
984 scheduler_->CompleteOperationAndRunNext(); 1073 scheduler_->CompleteOperationAndRunNext();
985 } 1074 }
986 1075
987 void CacheStorage::PendingBoolAndErrorCallback( 1076 void CacheStorage::PendingBoolAndErrorCallback(
988 const BoolAndErrorCallback& callback, 1077 const BoolAndErrorCallback& callback,
989 bool found, 1078 bool found,
990 CacheStorageError error) { 1079 CacheStorageError error) {
991 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); 1080 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
992 1081
993 callback.Run(found, error); 1082 callback.Run(found, error);
994 if (cache_storage) 1083 if (cache_storage)
995 scheduler_->CompleteOperationAndRunNext(); 1084 scheduler_->CompleteOperationAndRunNext();
996 } 1085 }
997 1086
998 void CacheStorage::PendingCacheAndErrorCallback( 1087 void CacheStorage::PendingCacheAndErrorCallback(
999 const CacheAndErrorCallback& callback, 1088 const CacheAndErrorCallback& callback,
1000 scoped_refptr<CacheStorageCache> cache, 1089 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
1001 CacheStorageError error) { 1090 CacheStorageError error) {
1002 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); 1091 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
1003 1092
1004 callback.Run(std::move(cache), error); 1093 callback.Run(std::move(cache_handle), error);
1005 if (cache_storage) 1094 if (cache_storage)
1006 scheduler_->CompleteOperationAndRunNext(); 1095 scheduler_->CompleteOperationAndRunNext();
1007 } 1096 }
1008 1097
1009 void CacheStorage::PendingStringsAndErrorCallback( 1098 void CacheStorage::PendingStringsAndErrorCallback(
1010 const StringsAndErrorCallback& callback, 1099 const StringsAndErrorCallback& callback,
1011 const StringVector& strings, 1100 const StringVector& strings,
1012 CacheStorageError error) { 1101 CacheStorageError error) {
1013 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); 1102 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
1014 1103
(...skipping 17 matching lines...) Expand all
1032 void CacheStorage::PendingSizeCallback(const SizeCallback& callback, 1121 void CacheStorage::PendingSizeCallback(const SizeCallback& callback,
1033 int64_t size) { 1122 int64_t size) {
1034 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); 1123 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
1035 1124
1036 callback.Run(size); 1125 callback.Run(size);
1037 if (cache_storage) 1126 if (cache_storage)
1038 scheduler_->CompleteOperationAndRunNext(); 1127 scheduler_->CompleteOperationAndRunNext();
1039 } 1128 }
1040 1129
1041 } // namespace content 1130 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698