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

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

Issue 2947753002: CacheStorage: Migrate to BindOnce/OnceCallback/OnceClosure (Closed)
Patch Set: Untangle Batch logic (relies on AdaptCallbackForRepeating) Created 3 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_cache.h" 5 #include "content/browser/cache_storage/cache_storage_cache.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <limits> 9 #include <limits>
10 #include <memory> 10 #include <memory>
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 62
63 private: 63 private:
64 ~CacheStorageCacheDataHandle() override {} 64 ~CacheStorageCacheDataHandle() override {}
65 65
66 std::unique_ptr<CacheStorageCacheHandle> cache_handle_; 66 std::unique_ptr<CacheStorageCacheHandle> cache_handle_;
67 disk_cache::ScopedEntryPtr entry_; 67 disk_cache::ScopedEntryPtr entry_;
68 68
69 DISALLOW_COPY_AND_ASSIGN(CacheStorageCacheDataHandle); 69 DISALLOW_COPY_AND_ASSIGN(CacheStorageCacheDataHandle);
70 }; 70 };
71 71
72 typedef base::Callback<void(std::unique_ptr<proto::CacheMetadata>)> 72 using MetadataCallback =
73 MetadataCallback; 73 base::OnceCallback<void(std::unique_ptr<proto::CacheMetadata>)>;
74 74
75 // The maximum size of each cache. Ultimately, cache size 75 // The maximum size of each cache. Ultimately, cache size
76 // is controlled per-origin by the QuotaManager. 76 // is controlled per-origin by the QuotaManager.
77 const int kMaxCacheBytes = std::numeric_limits<int>::max(); 77 const int kMaxCacheBytes = std::numeric_limits<int>::max();
78 78
79 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType( 79 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType(
80 proto::CacheResponse::ResponseType response_type) { 80 proto::CacheResponse::ResponseType response_type) {
81 switch (response_type) { 81 switch (response_type) {
82 case proto::CacheResponse::BASIC_TYPE: 82 case proto::CacheResponse::BASIC_TYPE:
83 return blink::kWebServiceWorkerResponseTypeBasic; 83 return blink::kWebServiceWorkerResponseTypeBasic;
(...skipping 27 matching lines...) Expand all
111 return proto::CacheResponse::OPAQUE_TYPE; 111 return proto::CacheResponse::OPAQUE_TYPE;
112 case blink::kWebServiceWorkerResponseTypeOpaqueRedirect: 112 case blink::kWebServiceWorkerResponseTypeOpaqueRedirect:
113 return proto::CacheResponse::OPAQUE_REDIRECT_TYPE; 113 return proto::CacheResponse::OPAQUE_REDIRECT_TYPE;
114 } 114 }
115 NOTREACHED(); 115 NOTREACHED();
116 return proto::CacheResponse::OPAQUE_TYPE; 116 return proto::CacheResponse::OPAQUE_TYPE;
117 } 117 }
118 118
119 // Copy headers out of a cache entry and into a protobuf. The callback is 119 // Copy headers out of a cache entry and into a protobuf. The callback is
120 // guaranteed to be run. 120 // guaranteed to be run.
121 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback); 121 void ReadMetadata(disk_cache::Entry* entry, MetadataCallback callback);
122 void ReadMetadataDidReadMetadata(disk_cache::Entry* entry, 122 void ReadMetadataDidReadMetadata(disk_cache::Entry* entry,
123 const MetadataCallback& callback, 123 MetadataCallback callback,
124 scoped_refptr<net::IOBufferWithSize> buffer, 124 scoped_refptr<net::IOBufferWithSize> buffer,
125 int rv); 125 int rv);
126 126
127 bool VaryMatches(const ServiceWorkerHeaderMap& request, 127 bool VaryMatches(const ServiceWorkerHeaderMap& request,
128 const ServiceWorkerHeaderMap& cached_request, 128 const ServiceWorkerHeaderMap& cached_request,
129 const ServiceWorkerHeaderMap& response) { 129 const ServiceWorkerHeaderMap& response) {
130 ServiceWorkerHeaderMap::const_iterator vary_iter = response.find("vary"); 130 ServiceWorkerHeaderMap::const_iterator vary_iter = response.find("vary");
131 if (vary_iter == response.end()) 131 if (vary_iter == response.end())
132 return true; 132 return true;
133 133
(...skipping 21 matching lines...) Expand all
155 155
156 return true; 156 return true;
157 } 157 }
158 158
159 GURL RemoveQueryParam(const GURL& url) { 159 GURL RemoveQueryParam(const GURL& url) {
160 url::Replacements<char> replacements; 160 url::Replacements<char> replacements;
161 replacements.ClearQuery(); 161 replacements.ClearQuery();
162 return url.ReplaceComponents(replacements); 162 return url.ReplaceComponents(replacements);
163 } 163 }
164 164
165 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback) { 165 void ReadMetadata(disk_cache::Entry* entry, MetadataCallback callback) {
166 DCHECK(entry); 166 DCHECK(entry);
167 167
168 scoped_refptr<net::IOBufferWithSize> buffer(new net::IOBufferWithSize( 168 scoped_refptr<net::IOBufferWithSize> buffer(new net::IOBufferWithSize(
169 entry->GetDataSize(CacheStorageCache::INDEX_HEADERS))); 169 entry->GetDataSize(CacheStorageCache::INDEX_HEADERS)));
170 170
171 net::CompletionCallback read_header_callback = 171 net::CompletionCallback read_header_callback =
172 base::Bind(ReadMetadataDidReadMetadata, entry, callback, buffer); 172 base::AdaptCallbackForRepeating(base::BindOnce(
173 ReadMetadataDidReadMetadata, entry, std::move(callback), buffer));
173 174
174 int read_rv = 175 int read_rv =
175 entry->ReadData(CacheStorageCache::INDEX_HEADERS, 0, buffer.get(), 176 entry->ReadData(CacheStorageCache::INDEX_HEADERS, 0, buffer.get(),
176 buffer->size(), read_header_callback); 177 buffer->size(), read_header_callback);
177 178
178 if (read_rv != net::ERR_IO_PENDING) 179 if (read_rv != net::ERR_IO_PENDING)
179 read_header_callback.Run(read_rv); 180 read_header_callback.Run(read_rv);
180 } 181 }
181 182
182 void ReadMetadataDidReadMetadata(disk_cache::Entry* entry, 183 void ReadMetadataDidReadMetadata(disk_cache::Entry* entry,
183 const MetadataCallback& callback, 184 MetadataCallback callback,
184 scoped_refptr<net::IOBufferWithSize> buffer, 185 scoped_refptr<net::IOBufferWithSize> buffer,
185 int rv) { 186 int rv) {
186 if (rv != buffer->size()) { 187 if (rv != buffer->size()) {
187 callback.Run(std::unique_ptr<proto::CacheMetadata>()); 188 std::move(callback).Run(std::unique_ptr<proto::CacheMetadata>());
188 return; 189 return;
189 } 190 }
190 191
191 if (rv > 0) 192 if (rv > 0)
192 storage::RecordBytesRead(kRecordBytesLabel, rv); 193 storage::RecordBytesRead(kRecordBytesLabel, rv);
193 194
194 std::unique_ptr<proto::CacheMetadata> metadata(new proto::CacheMetadata()); 195 std::unique_ptr<proto::CacheMetadata> metadata(new proto::CacheMetadata());
195 196
196 if (!metadata->ParseFromArray(buffer->data(), buffer->size())) { 197 if (!metadata->ParseFromArray(buffer->data(), buffer->size())) {
197 callback.Run(std::unique_ptr<proto::CacheMetadata>()); 198 std::move(callback).Run(std::unique_ptr<proto::CacheMetadata>());
198 return; 199 return;
199 } 200 }
200 201
201 callback.Run(std::move(metadata)); 202 std::move(callback).Run(std::move(metadata));
202 } 203 }
203 204
204 std::unique_ptr<ServiceWorkerFetchRequest> CreateRequest( 205 std::unique_ptr<ServiceWorkerFetchRequest> CreateRequest(
205 const proto::CacheMetadata& metadata, 206 const proto::CacheMetadata& metadata,
206 const GURL& request_url) { 207 const GURL& request_url) {
207 auto request = base::MakeUnique<ServiceWorkerFetchRequest>( 208 auto request = base::MakeUnique<ServiceWorkerFetchRequest>(
208 request_url, metadata.request().method(), ServiceWorkerHeaderMap(), 209 request_url, metadata.request().method(), ServiceWorkerHeaderMap(),
209 Referrer(), false); 210 Referrer(), false);
210 211
211 for (int i = 0; i < metadata.request().headers_size(); ++i) { 212 for (int i = 0; i < metadata.request().headers_size(); ++i) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 metadata.response().cors_exposed_header_names().end())); 255 metadata.response().cors_exposed_header_names().end()));
255 } 256 }
256 257
257 } // namespace 258 } // namespace
258 259
259 // The state needed to pass between CacheStorageCache::Put callbacks. 260 // The state needed to pass between CacheStorageCache::Put callbacks.
260 struct CacheStorageCache::PutContext { 261 struct CacheStorageCache::PutContext {
261 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request, 262 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
262 std::unique_ptr<ServiceWorkerResponse> response, 263 std::unique_ptr<ServiceWorkerResponse> response,
263 std::unique_ptr<storage::BlobDataHandle> blob_data_handle, 264 std::unique_ptr<storage::BlobDataHandle> blob_data_handle,
264 const CacheStorageCache::ErrorCallback& callback) 265 CacheStorageCache::ErrorCallback callback)
265 : request(std::move(request)), 266 : request(std::move(request)),
266 response(std::move(response)), 267 response(std::move(response)),
267 blob_data_handle(std::move(blob_data_handle)), 268 blob_data_handle(std::move(blob_data_handle)),
268 callback(callback) {} 269 callback(std::move(callback)) {}
269 270
270 // Input parameters to the Put function. 271 // Input parameters to the Put function.
271 std::unique_ptr<ServiceWorkerFetchRequest> request; 272 std::unique_ptr<ServiceWorkerFetchRequest> request;
272 std::unique_ptr<ServiceWorkerResponse> response; 273 std::unique_ptr<ServiceWorkerResponse> response;
273 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; 274 std::unique_ptr<storage::BlobDataHandle> blob_data_handle;
274 CacheStorageCache::ErrorCallback callback; 275 CacheStorageCache::ErrorCallback callback;
275 disk_cache::ScopedEntryPtr cache_entry; 276 disk_cache::ScopedEntryPtr cache_entry;
276 277
277 private: 278 private:
278 DISALLOW_COPY_AND_ASSIGN(PutContext); 279 DISALLOW_COPY_AND_ASSIGN(PutContext);
279 }; 280 };
280 281
281 struct CacheStorageCache::QueryCacheResult { 282 struct CacheStorageCache::QueryCacheResult {
282 explicit QueryCacheResult(base::Time entry_time) : entry_time(entry_time) {} 283 explicit QueryCacheResult(base::Time entry_time) : entry_time(entry_time) {}
283 284
284 std::unique_ptr<ServiceWorkerFetchRequest> request; 285 std::unique_ptr<ServiceWorkerFetchRequest> request;
285 std::unique_ptr<ServiceWorkerResponse> response; 286 std::unique_ptr<ServiceWorkerResponse> response;
286 std::unique_ptr<storage::BlobDataHandle> blob_handle; 287 std::unique_ptr<storage::BlobDataHandle> blob_handle;
287 disk_cache::ScopedEntryPtr entry; 288 disk_cache::ScopedEntryPtr entry;
288 base::Time entry_time; 289 base::Time entry_time;
289 }; 290 };
290 291
291 struct CacheStorageCache::QueryCacheContext { 292 struct CacheStorageCache::QueryCacheContext {
292 QueryCacheContext(std::unique_ptr<ServiceWorkerFetchRequest> request, 293 QueryCacheContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
293 const CacheStorageCacheQueryParams& options, 294 const CacheStorageCacheQueryParams& options,
294 const QueryCacheCallback& callback) 295 QueryCacheCallback callback)
295 : request(std::move(request)), 296 : request(std::move(request)),
296 options(options), 297 options(options),
297 callback(callback), 298 callback(std::move(callback)),
298 matches(base::MakeUnique<QueryCacheResults>()) {} 299 matches(base::MakeUnique<QueryCacheResults>()) {}
299 300
300 ~QueryCacheContext() { 301 ~QueryCacheContext() {
301 // If the CacheStorageCache is deleted before a backend operation to open 302 // If the CacheStorageCache is deleted before a backend operation to open
302 // an entry completes, the callback won't be run and the resulting entry 303 // an entry completes, the callback won't be run and the resulting entry
303 // will be leaked unless we close it here. 304 // will be leaked unless we close it here.
304 if (enumerated_entry) { 305 if (enumerated_entry) {
305 enumerated_entry->Close(); 306 enumerated_entry->Close();
306 enumerated_entry = nullptr; 307 enumerated_entry = nullptr;
307 } 308 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 return base::WrapUnique(cache); 361 return base::WrapUnique(cache);
361 } 362 }
362 363
363 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() { 364 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() {
364 return weak_ptr_factory_.GetWeakPtr(); 365 return weak_ptr_factory_.GetWeakPtr();
365 } 366 }
366 367
367 void CacheStorageCache::Match( 368 void CacheStorageCache::Match(
368 std::unique_ptr<ServiceWorkerFetchRequest> request, 369 std::unique_ptr<ServiceWorkerFetchRequest> request,
369 const CacheStorageCacheQueryParams& match_params, 370 const CacheStorageCacheQueryParams& match_params,
370 const ResponseCallback& callback) { 371 ResponseCallback callback) {
371 if (backend_state_ == BACKEND_CLOSED) { 372 if (backend_state_ == BACKEND_CLOSED) {
372 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 373 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
373 std::unique_ptr<ServiceWorkerResponse>(), 374 std::unique_ptr<ServiceWorkerResponse>(),
374 std::unique_ptr<storage::BlobDataHandle>()); 375 std::unique_ptr<storage::BlobDataHandle>());
375 return; 376 return;
376 } 377 }
377 378
378 scheduler_->ScheduleOperation( 379 scheduler_->ScheduleOperation(base::BindOnce(
379 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), 380 &CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(),
380 base::Passed(std::move(request)), match_params, 381 base::Passed(std::move(request)), match_params,
381 scheduler_->WrapCallbackToRunNext(callback))); 382 scheduler_->WrapCallbackToRunNext(std::move(callback))));
382 } 383 }
383 384
384 void CacheStorageCache::MatchAll( 385 void CacheStorageCache::MatchAll(
385 std::unique_ptr<ServiceWorkerFetchRequest> request, 386 std::unique_ptr<ServiceWorkerFetchRequest> request,
386 const CacheStorageCacheQueryParams& match_params, 387 const CacheStorageCacheQueryParams& match_params,
387 const ResponsesCallback& callback) { 388 ResponsesCallback callback) {
388 if (backend_state_ == BACKEND_CLOSED) { 389 if (backend_state_ == BACKEND_CLOSED) {
389 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), 390 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
390 std::unique_ptr<BlobDataHandles>()); 391 std::unique_ptr<Responses>(),
392 std::unique_ptr<BlobDataHandles>());
391 return; 393 return;
392 } 394 }
393 395
394 scheduler_->ScheduleOperation(base::Bind( 396 scheduler_->ScheduleOperation(base::BindOnce(
395 &CacheStorageCache::MatchAllImpl, weak_ptr_factory_.GetWeakPtr(), 397 &CacheStorageCache::MatchAllImpl, weak_ptr_factory_.GetWeakPtr(),
396 base::Passed(std::move(request)), match_params, 398 base::Passed(std::move(request)), match_params,
397 scheduler_->WrapCallbackToRunNext(callback))); 399 scheduler_->WrapCallbackToRunNext(std::move(callback))));
398 } 400 }
399 401
400 void CacheStorageCache::WriteSideData(const ErrorCallback& callback, 402 void CacheStorageCache::WriteSideData(ErrorCallback callback,
401 const GURL& url, 403 const GURL& url,
402 base::Time expected_response_time, 404 base::Time expected_response_time,
403 scoped_refptr<net::IOBuffer> buffer, 405 scoped_refptr<net::IOBuffer> buffer,
404 int buf_len) { 406 int buf_len) {
405 if (backend_state_ == BACKEND_CLOSED) { 407 if (backend_state_ == BACKEND_CLOSED) {
406 base::ThreadTaskRunnerHandle::Get()->PostTask( 408 base::ThreadTaskRunnerHandle::Get()->PostTask(
407 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_STORAGE)); 409 FROM_HERE,
410 base::BindOnce(std::move(callback), CACHE_STORAGE_ERROR_STORAGE));
408 return; 411 return;
409 } 412 }
410 413
411 // GetUsageAndQuota is called before entering a scheduled operation since it 414 // GetUsageAndQuota is called before entering a scheduled operation since it
412 // can call Size, another scheduled operation. 415 // can call Size, another scheduled operation.
413 quota_manager_proxy_->GetUsageAndQuota( 416 quota_manager_proxy_->GetUsageAndQuota(
414 base::ThreadTaskRunnerHandle::Get().get(), origin_, 417 base::ThreadTaskRunnerHandle::Get().get(), origin_,
415 storage::kStorageTypeTemporary, 418 storage::kStorageTypeTemporary,
416 base::Bind(&CacheStorageCache::WriteSideDataDidGetQuota, 419 base::AdaptCallbackForRepeating(
417 weak_ptr_factory_.GetWeakPtr(), callback, url, 420 base::BindOnce(&CacheStorageCache::WriteSideDataDidGetQuota,
418 expected_response_time, buffer, buf_len)); 421 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
422 url, expected_response_time, buffer, buf_len)));
419 } 423 }
420 424
421 void CacheStorageCache::BatchOperation( 425 void CacheStorageCache::BatchOperation(
422 const std::vector<CacheStorageBatchOperation>& operations, 426 const std::vector<CacheStorageBatchOperation>& operations,
423 const ErrorCallback& callback) { 427 ErrorCallback callback) {
424 if (backend_state_ == BACKEND_CLOSED) { 428 if (backend_state_ == BACKEND_CLOSED) {
425 base::ThreadTaskRunnerHandle::Get()->PostTask( 429 base::ThreadTaskRunnerHandle::Get()->PostTask(
426 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_STORAGE)); 430 FROM_HERE,
431 base::BindOnce(std::move(callback), CACHE_STORAGE_ERROR_STORAGE));
427 return; 432 return;
428 } 433 }
429 434
430 // Estimate the required size of the put operations. The size of the deletes 435 // Estimate the required size of the put operations. The size of the deletes
431 // is unknown and not considered. 436 // is unknown and not considered.
432 int64_t space_required = 0; 437 int64_t space_required = 0;
433 for (const auto& operation : operations) { 438 for (const auto& operation : operations) {
434 if (operation.operation_type == CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT) { 439 if (operation.operation_type == CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT) {
435 space_required += 440 space_required +=
436 operation.request.blob_size + operation.response.blob_size; 441 operation.request.blob_size + operation.response.blob_size;
437 } 442 }
438 } 443 }
439 if (space_required > 0) { 444 if (space_required > 0) {
440 // GetUsageAndQuota is called before entering a scheduled operation since it 445 // GetUsageAndQuota is called before entering a scheduled operation since it
441 // can call Size, another scheduled operation. This is racy. The decision 446 // can call Size, another scheduled operation. This is racy. The decision
442 // to commit is made before the scheduled Put operation runs. By the time 447 // to commit is made before the scheduled Put operation runs. By the time
443 // Put runs, the cache might already be full and the origin will be larger 448 // Put runs, the cache might already be full and the origin will be larger
444 // than it's supposed to be. 449 // than it's supposed to be.
445 quota_manager_proxy_->GetUsageAndQuota( 450 quota_manager_proxy_->GetUsageAndQuota(
446 base::ThreadTaskRunnerHandle::Get().get(), origin_, 451 base::ThreadTaskRunnerHandle::Get().get(), origin_,
447 storage::kStorageTypeTemporary, 452 storage::kStorageTypeTemporary,
448 base::Bind(&CacheStorageCache::BatchDidGetUsageAndQuota, 453 base::AdaptCallbackForRepeating(
449 weak_ptr_factory_.GetWeakPtr(), operations, callback, 454 base::BindOnce(&CacheStorageCache::BatchDidGetUsageAndQuota,
450 space_required)); 455 weak_ptr_factory_.GetWeakPtr(), operations,
456 std::move(callback), space_required)));
451 return; 457 return;
452 } 458 }
453 459
454 BatchDidGetUsageAndQuota(operations, callback, 0 /* space_required */, 460 BatchDidGetUsageAndQuota(operations, std::move(callback),
455 storage::kQuotaStatusOk, 0 /* usage */, 461 0 /* space_required */, storage::kQuotaStatusOk,
456 0 /* quota */); 462 0 /* usage */, 0 /* quota */);
457 } 463 }
458 464
459 void CacheStorageCache::BatchDidGetUsageAndQuota( 465 void CacheStorageCache::BatchDidGetUsageAndQuota(
460 const std::vector<CacheStorageBatchOperation>& operations, 466 const std::vector<CacheStorageBatchOperation>& operations,
461 const ErrorCallback& callback, 467 ErrorCallback callback,
462 int64_t space_required, 468 int64_t space_required,
463 storage::QuotaStatusCode status_code, 469 storage::QuotaStatusCode status_code,
464 int64_t usage, 470 int64_t usage,
465 int64_t quota) { 471 int64_t quota) {
466 if (status_code != storage::kQuotaStatusOk || 472 if (status_code != storage::kQuotaStatusOk ||
467 space_required > quota - usage) { 473 space_required > quota - usage) {
468 base::ThreadTaskRunnerHandle::Get()->PostTask( 474 base::ThreadTaskRunnerHandle::Get()->PostTask(
469 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_QUOTA_EXCEEDED)); 475 FROM_HERE, base::BindOnce(std::move(callback),
476 CACHE_STORAGE_ERROR_QUOTA_EXCEEDED));
470 return; 477 return;
471 } 478 }
472 479
473 std::unique_ptr<ErrorCallback> callback_copy(new ErrorCallback(callback)); 480 // The following relies on the guarantee that the RepeatingCallback returned
474 ErrorCallback* callback_ptr = callback_copy.get(); 481 // from AdaptCallbackForRepeating invokes the original callback on the first
475 base::Closure barrier_closure = base::BarrierClosure( 482 // invocation, and (critically) that subsequent invocations are ignored.
476 operations.size(), base::Bind(&CacheStorageCache::BatchDidAllOperations, 483 // TODO(jsbell): Replace AdaptCallbackForRepeating with ...? crbug.com/730593
477 weak_ptr_factory_.GetWeakPtr(), 484 auto callback_copy = base::AdaptCallbackForRepeating(std::move(callback));
478 base::Passed(std::move(callback_copy)))); 485 auto barrier_closure = base::BarrierClosure(
479 ErrorCallback completion_callback = 486 operations.size(),
480 base::Bind(&CacheStorageCache::BatchDidOneOperation, 487 base::BindOnce(&CacheStorageCache::BatchDidAllOperations,
481 weak_ptr_factory_.GetWeakPtr(), barrier_closure, callback_ptr); 488 weak_ptr_factory_.GetWeakPtr(), callback_copy));
489 auto completion_callback = base::BindRepeating(
490 &CacheStorageCache::BatchDidOneOperation, weak_ptr_factory_.GetWeakPtr(),
491 barrier_closure, callback_copy);
482 492
483 for (const auto& operation : operations) { 493 for (const auto& operation : operations) {
484 switch (operation.operation_type) { 494 switch (operation.operation_type) {
485 case CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT: 495 case CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT:
486 Put(operation, completion_callback); 496 Put(operation, completion_callback);
487 break; 497 break;
488 case CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE: 498 case CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE:
489 DCHECK_EQ(1u, operations.size()); 499 DCHECK_EQ(1u, operations.size());
490 Delete(operation, completion_callback); 500 Delete(operation, completion_callback);
491 break; 501 break;
492 case CACHE_STORAGE_CACHE_OPERATION_TYPE_UNDEFINED: 502 case CACHE_STORAGE_CACHE_OPERATION_TYPE_UNDEFINED:
493 NOTREACHED(); 503 NOTREACHED();
494 // TODO(nhiroki): This should return "TypeError". 504 // TODO(nhiroki): This should return "TypeError".
495 // http://crbug.com/425505 505 // http://crbug.com/425505
496 completion_callback.Run(CACHE_STORAGE_ERROR_STORAGE); 506 completion_callback.Run(CACHE_STORAGE_ERROR_STORAGE);
497 break; 507 break;
498 } 508 }
499 } 509 }
500 } 510 }
501 511
502 void CacheStorageCache::BatchDidOneOperation( 512 void CacheStorageCache::BatchDidOneOperation(
503 const base::Closure& barrier_closure, 513 base::OnceClosure completion_closure,
504 ErrorCallback* callback, 514 ErrorCallback error_callback,
505 CacheStorageError error) { 515 CacheStorageError error) {
506 if (callback->is_null() || error == CACHE_STORAGE_OK) { 516 if (error != CACHE_STORAGE_OK) {
507 barrier_closure.Run(); 517 // This relies on |callback| being created by AdaptCallbackForRepeating
508 return; 518 // and ignoring anything but the first invocation.
519 std::move(error_callback).Run(error);
509 } 520 }
510 callback->Run(error);
511 callback->Reset(); // Only call the callback once.
512 521
513 barrier_closure.Run(); 522 std::move(completion_closure).Run();
514 } 523 }
515 524
516 void CacheStorageCache::BatchDidAllOperations( 525 void CacheStorageCache::BatchDidAllOperations(ErrorCallback callback) {
517 std::unique_ptr<ErrorCallback> callback) { 526 // This relies on |callback| being created by AdaptCallbackForRepeating
518 if (callback->is_null()) 527 // and ignoring anything but the first invocation.
519 return; 528 std::move(callback).Run(CACHE_STORAGE_OK);
520 callback->Run(CACHE_STORAGE_OK);
521 } 529 }
522 530
523 void CacheStorageCache::Keys(std::unique_ptr<ServiceWorkerFetchRequest> request, 531 void CacheStorageCache::Keys(std::unique_ptr<ServiceWorkerFetchRequest> request,
524 const CacheStorageCacheQueryParams& options, 532 const CacheStorageCacheQueryParams& options,
525 const RequestsCallback& callback) { 533 RequestsCallback callback) {
526 if (backend_state_ == BACKEND_CLOSED) { 534 if (backend_state_ == BACKEND_CLOSED) {
527 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>()); 535 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
536 std::unique_ptr<Requests>());
528 return; 537 return;
529 } 538 }
530 539
531 scheduler_->ScheduleOperation( 540 scheduler_->ScheduleOperation(base::BindOnce(
532 base::Bind(&CacheStorageCache::KeysImpl, weak_ptr_factory_.GetWeakPtr(), 541 &CacheStorageCache::KeysImpl, weak_ptr_factory_.GetWeakPtr(),
533 base::Passed(std::move(request)), options, 542 base::Passed(std::move(request)), options,
534 scheduler_->WrapCallbackToRunNext(callback))); 543 scheduler_->WrapCallbackToRunNext(std::move(callback))));
535 } 544 }
536 545
537 void CacheStorageCache::Close(const base::Closure& callback) { 546 void CacheStorageCache::Close(base::OnceClosure callback) {
538 DCHECK_NE(BACKEND_CLOSED, backend_state_) 547 DCHECK_NE(BACKEND_CLOSED, backend_state_)
539 << "Was CacheStorageCache::Close() called twice?"; 548 << "Was CacheStorageCache::Close() called twice?";
540 549
541 scheduler_->ScheduleOperation( 550 scheduler_->ScheduleOperation(base::BindOnce(
542 base::Bind(&CacheStorageCache::CloseImpl, weak_ptr_factory_.GetWeakPtr(), 551 &CacheStorageCache::CloseImpl, weak_ptr_factory_.GetWeakPtr(),
543 scheduler_->WrapCallbackToRunNext(callback))); 552 scheduler_->WrapCallbackToRunNext(std::move(callback))));
544 } 553 }
545 554
546 void CacheStorageCache::Size(const SizeCallback& callback) { 555 void CacheStorageCache::Size(SizeCallback callback) {
547 if (backend_state_ == BACKEND_CLOSED) { 556 if (backend_state_ == BACKEND_CLOSED) {
548 // TODO(jkarlin): Delete caches that can't be initialized. 557 // TODO(jkarlin): Delete caches that can't be initialized.
549 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 558 base::ThreadTaskRunnerHandle::Get()->PostTask(
550 base::Bind(callback, 0)); 559 FROM_HERE, base::BindOnce(std::move(callback), 0));
551 return; 560 return;
552 } 561 }
553 562
554 scheduler_->ScheduleOperation( 563 scheduler_->ScheduleOperation(base::BindOnce(
555 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(), 564 &CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(),
556 scheduler_->WrapCallbackToRunNext(callback))); 565 scheduler_->WrapCallbackToRunNext(std::move(callback))));
557 } 566 }
558 567
559 void CacheStorageCache::GetSizeThenClose(const SizeCallback& callback) { 568 void CacheStorageCache::GetSizeThenClose(SizeCallback callback) {
560 if (backend_state_ == BACKEND_CLOSED) { 569 if (backend_state_ == BACKEND_CLOSED) {
561 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 570 base::ThreadTaskRunnerHandle::Get()->PostTask(
562 base::Bind(callback, 0)); 571 FROM_HERE, base::BindOnce(std::move(callback), 0));
563 return; 572 return;
564 } 573 }
565 574
566 scheduler_->ScheduleOperation( 575 scheduler_->ScheduleOperation(base::BindOnce(
567 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(), 576 &CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(),
568 base::Bind(&CacheStorageCache::GetSizeThenCloseDidGetSize, 577 base::BindOnce(&CacheStorageCache::GetSizeThenCloseDidGetSize,
569 weak_ptr_factory_.GetWeakPtr(), 578 weak_ptr_factory_.GetWeakPtr(),
570 scheduler_->WrapCallbackToRunNext(callback)))); 579 scheduler_->WrapCallbackToRunNext(std::move(callback)))));
571 } 580 }
572 581
573 void CacheStorageCache::SetObserver(CacheStorageCacheObserver* observer) { 582 void CacheStorageCache::SetObserver(CacheStorageCacheObserver* observer) {
574 DCHECK((observer == nullptr) ^ (cache_observer_ == nullptr)); 583 DCHECK((observer == nullptr) ^ (cache_observer_ == nullptr));
575 cache_observer_ = observer; 584 cache_observer_ = observer;
576 } 585 }
577 586
578 CacheStorageCache::~CacheStorageCache() { 587 CacheStorageCache::~CacheStorageCache() {
579 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_); 588 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_);
580 } 589 }
(...skipping 24 matching lines...) Expand all
605 DCHECK(!origin_.is_empty()); 614 DCHECK(!origin_.is_empty());
606 DCHECK(quota_manager_proxy_.get()); 615 DCHECK(quota_manager_proxy_.get());
607 616
608 quota_manager_proxy_->NotifyOriginInUse(origin_); 617 quota_manager_proxy_->NotifyOriginInUse(origin_);
609 } 618 }
610 619
611 void CacheStorageCache::QueryCache( 620 void CacheStorageCache::QueryCache(
612 std::unique_ptr<ServiceWorkerFetchRequest> request, 621 std::unique_ptr<ServiceWorkerFetchRequest> request,
613 const CacheStorageCacheQueryParams& options, 622 const CacheStorageCacheQueryParams& options,
614 QueryCacheType query_type, 623 QueryCacheType query_type,
615 const QueryCacheCallback& callback) { 624 QueryCacheCallback callback) {
616 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 625 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
617 if (backend_state_ != BACKEND_OPEN) { 626 if (backend_state_ != BACKEND_OPEN) {
618 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 627 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
619 std::unique_ptr<QueryCacheResults>()); 628 std::unique_ptr<QueryCacheResults>());
620 return; 629 return;
621 } 630 }
622 631
623 if (!options.ignore_method && request && !request->method.empty() && 632 if (!options.ignore_method && request && !request->method.empty() &&
624 request->method != "GET") { 633 request->method != "GET") {
625 callback.Run(CACHE_STORAGE_OK, base::MakeUnique<QueryCacheResults>()); 634 std::move(callback).Run(CACHE_STORAGE_OK,
635 base::MakeUnique<QueryCacheResults>());
626 return; 636 return;
627 } 637 }
628 638
629 ServiceWorkerFetchRequest* request_ptr = request.get(); 639 ServiceWorkerFetchRequest* request_ptr = request.get();
630 std::unique_ptr<QueryCacheContext> query_cache_context( 640 std::unique_ptr<QueryCacheContext> query_cache_context(
631 new QueryCacheContext(std::move(request), options, callback)); 641 new QueryCacheContext(std::move(request), options, std::move(callback)));
632 query_cache_context->query_type = query_type; 642 query_cache_context->query_type = query_type;
633 643
634 if (query_cache_context->request && 644 if (query_cache_context->request &&
635 !query_cache_context->request->url.is_empty() && !options.ignore_search) { 645 !query_cache_context->request->url.is_empty() && !options.ignore_search) {
636 // There is no need to scan the entire backend, just open the exact 646 // There is no need to scan the entire backend, just open the exact
637 // URL. 647 // URL.
638 disk_cache::Entry** entry_ptr = &query_cache_context->enumerated_entry; 648 disk_cache::Entry** entry_ptr = &query_cache_context->enumerated_entry;
639 net::CompletionCallback open_entry_callback = 649 net::CompletionCallback open_entry_callback =
640 base::Bind(&CacheStorageCache::QueryCacheDidOpenFastPath, 650 base::AdaptCallbackForRepeating(
641 weak_ptr_factory_.GetWeakPtr(), 651 base::BindOnce(&CacheStorageCache::QueryCacheDidOpenFastPath,
642 base::Passed(std::move(query_cache_context))); 652 weak_ptr_factory_.GetWeakPtr(),
653 base::Passed(std::move(query_cache_context))));
643 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, 654 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr,
644 open_entry_callback); 655 open_entry_callback);
645 if (rv != net::ERR_IO_PENDING) 656 if (rv != net::ERR_IO_PENDING)
646 open_entry_callback.Run(rv); 657 open_entry_callback.Run(rv);
647 return; 658 return;
648 } 659 }
649 660
650 query_cache_context->backend_iterator = backend_->CreateIterator(); 661 query_cache_context->backend_iterator = backend_->CreateIterator();
651 QueryCacheOpenNextEntry(std::move(query_cache_context)); 662 QueryCacheOpenNextEntry(std::move(query_cache_context));
652 } 663 }
653 664
654 void CacheStorageCache::QueryCacheDidOpenFastPath( 665 void CacheStorageCache::QueryCacheDidOpenFastPath(
655 std::unique_ptr<QueryCacheContext> query_cache_context, 666 std::unique_ptr<QueryCacheContext> query_cache_context,
656 int rv) { 667 int rv) {
657 if (rv != net::OK) { 668 if (rv != net::OK) {
658 QueryCacheContext* results = query_cache_context.get(); 669 QueryCacheContext* results = query_cache_context.get();
659 results->callback.Run(CACHE_STORAGE_OK, 670 std::move(results->callback)
660 std::move(query_cache_context->matches)); 671 .Run(CACHE_STORAGE_OK, std::move(query_cache_context->matches));
661 return; 672 return;
662 } 673 }
663 QueryCacheFilterEntry(std::move(query_cache_context), rv); 674 QueryCacheFilterEntry(std::move(query_cache_context), rv);
664 } 675 }
665 676
666 void CacheStorageCache::QueryCacheOpenNextEntry( 677 void CacheStorageCache::QueryCacheOpenNextEntry(
667 std::unique_ptr<QueryCacheContext> query_cache_context) { 678 std::unique_ptr<QueryCacheContext> query_cache_context) {
668 DCHECK_EQ(nullptr, query_cache_context->enumerated_entry); 679 DCHECK_EQ(nullptr, query_cache_context->enumerated_entry);
669 680
670 if (!query_cache_context->backend_iterator) { 681 if (!query_cache_context->backend_iterator) {
671 // Iteration is complete. 682 // Iteration is complete.
672 std::sort(query_cache_context->matches->begin(), 683 std::sort(query_cache_context->matches->begin(),
673 query_cache_context->matches->end(), QueryCacheResultCompare); 684 query_cache_context->matches->end(), QueryCacheResultCompare);
674 685
675 query_cache_context->callback.Run(CACHE_STORAGE_OK, 686 std::move(query_cache_context->callback)
676 std::move(query_cache_context->matches)); 687 .Run(CACHE_STORAGE_OK, std::move(query_cache_context->matches));
677 return; 688 return;
678 } 689 }
679 690
680 disk_cache::Backend::Iterator& iterator = 691 disk_cache::Backend::Iterator& iterator =
681 *query_cache_context->backend_iterator; 692 *query_cache_context->backend_iterator;
682 disk_cache::Entry** enumerated_entry = &query_cache_context->enumerated_entry; 693 disk_cache::Entry** enumerated_entry = &query_cache_context->enumerated_entry;
683 net::CompletionCallback open_entry_callback = base::Bind( 694 net::CompletionCallback open_entry_callback = base::AdaptCallbackForRepeating(
684 &CacheStorageCache::QueryCacheFilterEntry, weak_ptr_factory_.GetWeakPtr(), 695 base::BindOnce(&CacheStorageCache::QueryCacheFilterEntry,
685 base::Passed(std::move(query_cache_context))); 696 weak_ptr_factory_.GetWeakPtr(),
697 base::Passed(std::move(query_cache_context))));
686 698
687 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); 699 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback);
688 700
689 if (rv != net::ERR_IO_PENDING) 701 if (rv != net::ERR_IO_PENDING)
690 open_entry_callback.Run(rv); 702 open_entry_callback.Run(rv);
691 } 703 }
692 704
693 void CacheStorageCache::QueryCacheFilterEntry( 705 void CacheStorageCache::QueryCacheFilterEntry(
694 std::unique_ptr<QueryCacheContext> query_cache_context, 706 std::unique_ptr<QueryCacheContext> query_cache_context,
695 int rv) { 707 int rv) {
696 if (rv == net::ERR_FAILED) { 708 if (rv == net::ERR_FAILED) {
697 // This is the indicator that iteration is complete. 709 // This is the indicator that iteration is complete.
698 query_cache_context->backend_iterator.reset(); 710 query_cache_context->backend_iterator.reset();
699 QueryCacheOpenNextEntry(std::move(query_cache_context)); 711 QueryCacheOpenNextEntry(std::move(query_cache_context));
700 return; 712 return;
701 } 713 }
702 714
703 if (rv < 0) { 715 if (rv < 0) {
704 QueryCacheCallback callback = query_cache_context->callback; 716 std::move(query_cache_context->callback)
705 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 717 .Run(CACHE_STORAGE_ERROR_STORAGE,
706 std::move(query_cache_context->matches)); 718 std::move(query_cache_context->matches));
707 return; 719 return;
708 } 720 }
709 721
710 disk_cache::ScopedEntryPtr entry(query_cache_context->enumerated_entry); 722 disk_cache::ScopedEntryPtr entry(query_cache_context->enumerated_entry);
711 query_cache_context->enumerated_entry = nullptr; 723 query_cache_context->enumerated_entry = nullptr;
712 724
713 if (backend_state_ != BACKEND_OPEN) { 725 if (backend_state_ != BACKEND_OPEN) {
714 QueryCacheCallback callback = query_cache_context->callback; 726 std::move(query_cache_context->callback)
715 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, 727 .Run(CACHE_STORAGE_ERROR_NOT_FOUND,
716 std::move(query_cache_context->matches)); 728 std::move(query_cache_context->matches));
717 return; 729 return;
718 } 730 }
719 731
720 if (query_cache_context->request && 732 if (query_cache_context->request &&
721 !query_cache_context->request->url.is_empty()) { 733 !query_cache_context->request->url.is_empty()) {
722 GURL requestURL = query_cache_context->request->url; 734 GURL requestURL = query_cache_context->request->url;
723 GURL cachedURL = GURL(entry->GetKey()); 735 GURL cachedURL = GURL(entry->GetKey());
724 736
725 if (query_cache_context->options.ignore_search) { 737 if (query_cache_context->options.ignore_search) {
726 requestURL = RemoveQueryParam(requestURL); 738 requestURL = RemoveQueryParam(requestURL);
727 cachedURL = RemoveQueryParam(cachedURL); 739 cachedURL = RemoveQueryParam(cachedURL);
728 } 740 }
729 741
730 if (cachedURL != requestURL) { 742 if (cachedURL != requestURL) {
731 QueryCacheOpenNextEntry(std::move(query_cache_context)); 743 QueryCacheOpenNextEntry(std::move(query_cache_context));
732 return; 744 return;
733 } 745 }
734 } 746 }
735 747
736 disk_cache::Entry* entry_ptr = entry.get(); 748 disk_cache::Entry* entry_ptr = entry.get();
737 ReadMetadata(entry_ptr, 749 ReadMetadata(entry_ptr,
738 base::Bind(&CacheStorageCache::QueryCacheDidReadMetadata, 750 base::BindOnce(&CacheStorageCache::QueryCacheDidReadMetadata,
739 weak_ptr_factory_.GetWeakPtr(), 751 weak_ptr_factory_.GetWeakPtr(),
740 base::Passed(std::move(query_cache_context)), 752 base::Passed(std::move(query_cache_context)),
741 base::Passed(std::move(entry)))); 753 base::Passed(std::move(entry))));
742 } 754 }
743 755
744 void CacheStorageCache::QueryCacheDidReadMetadata( 756 void CacheStorageCache::QueryCacheDidReadMetadata(
745 std::unique_ptr<QueryCacheContext> query_cache_context, 757 std::unique_ptr<QueryCacheContext> query_cache_context,
746 disk_cache::ScopedEntryPtr entry, 758 disk_cache::ScopedEntryPtr entry,
747 std::unique_ptr<proto::CacheMetadata> metadata) { 759 std::unique_ptr<proto::CacheMetadata> metadata) {
748 if (!metadata) { 760 if (!metadata) {
749 entry->Doom(); 761 entry->Doom();
750 QueryCacheOpenNextEntry(std::move(query_cache_context)); 762 QueryCacheOpenNextEntry(std::move(query_cache_context));
751 return; 763 return;
(...skipping 24 matching lines...) Expand all
776 match->request.reset(); 788 match->request.reset();
777 match->response.reset(); 789 match->response.reset();
778 match->entry = std::move(entry); 790 match->entry = std::move(entry);
779 QueryCacheOpenNextEntry(std::move(query_cache_context)); 791 QueryCacheOpenNextEntry(std::move(query_cache_context));
780 return; 792 return;
781 } 793 }
782 794
783 query_cache_context->estimated_out_bytes += 795 query_cache_context->estimated_out_bytes +=
784 match->request->EstimatedStructSize(); 796 match->request->EstimatedStructSize();
785 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) { 797 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
786 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, 798 std::move(query_cache_context->callback)
787 std::unique_ptr<QueryCacheResults>()); 799 .Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
800 std::unique_ptr<QueryCacheResults>());
788 return; 801 return;
789 } 802 }
790 803
791 if (query_cache_context->query_type == QueryCacheType::REQUESTS) { 804 if (query_cache_context->query_type == QueryCacheType::REQUESTS) {
792 match->response.reset(); 805 match->response.reset();
793 QueryCacheOpenNextEntry(std::move(query_cache_context)); 806 QueryCacheOpenNextEntry(std::move(query_cache_context));
794 return; 807 return;
795 } 808 }
796 809
797 DCHECK_EQ(QueryCacheType::REQUESTS_AND_RESPONSES, 810 DCHECK_EQ(QueryCacheType::REQUESTS_AND_RESPONSES,
798 query_cache_context->query_type); 811 query_cache_context->query_type);
799 812
800 query_cache_context->estimated_out_bytes += 813 query_cache_context->estimated_out_bytes +=
801 match->response->EstimatedStructSize(); 814 match->response->EstimatedStructSize();
802 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) { 815 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
803 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, 816 std::move(query_cache_context->callback)
804 std::unique_ptr<QueryCacheResults>()); 817 .Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
818 std::unique_ptr<QueryCacheResults>());
805 return; 819 return;
806 } 820 }
807 821
808 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { 822 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
809 QueryCacheOpenNextEntry(std::move(query_cache_context)); 823 QueryCacheOpenNextEntry(std::move(query_cache_context));
810 return; 824 return;
811 } 825 }
812 826
813 if (!blob_storage_context_) { 827 if (!blob_storage_context_) {
814 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE, 828 std::move(query_cache_context->callback)
815 base::MakeUnique<QueryCacheResults>()); 829 .Run(CACHE_STORAGE_ERROR_STORAGE,
830 base::MakeUnique<QueryCacheResults>());
816 return; 831 return;
817 } 832 }
818 833
819 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 834 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
820 PopulateResponseBody(std::move(entry), match->response.get()); 835 PopulateResponseBody(std::move(entry), match->response.get());
821 match->blob_handle = std::move(blob_data_handle); 836 match->blob_handle = std::move(blob_data_handle);
822 837
823 QueryCacheOpenNextEntry(std::move(query_cache_context)); 838 QueryCacheOpenNextEntry(std::move(query_cache_context));
824 } 839 }
825 840
826 // static 841 // static
827 bool CacheStorageCache::QueryCacheResultCompare(const QueryCacheResult& lhs, 842 bool CacheStorageCache::QueryCacheResultCompare(const QueryCacheResult& lhs,
828 const QueryCacheResult& rhs) { 843 const QueryCacheResult& rhs) {
829 return lhs.entry_time < rhs.entry_time; 844 return lhs.entry_time < rhs.entry_time;
830 } 845 }
831 846
832 void CacheStorageCache::MatchImpl( 847 void CacheStorageCache::MatchImpl(
833 std::unique_ptr<ServiceWorkerFetchRequest> request, 848 std::unique_ptr<ServiceWorkerFetchRequest> request,
834 const CacheStorageCacheQueryParams& match_params, 849 const CacheStorageCacheQueryParams& match_params,
835 const ResponseCallback& callback) { 850 ResponseCallback callback) {
836 MatchAllImpl(std::move(request), match_params, 851 MatchAllImpl(
837 base::Bind(&CacheStorageCache::MatchDidMatchAll, 852 std::move(request), match_params,
838 weak_ptr_factory_.GetWeakPtr(), callback)); 853 base::BindOnce(&CacheStorageCache::MatchDidMatchAll,
854 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
839 } 855 }
840 856
841 void CacheStorageCache::MatchDidMatchAll( 857 void CacheStorageCache::MatchDidMatchAll(
842 const ResponseCallback& callback, 858 ResponseCallback callback,
843 CacheStorageError match_all_error, 859 CacheStorageError match_all_error,
844 std::unique_ptr<Responses> match_all_responses, 860 std::unique_ptr<Responses> match_all_responses,
845 std::unique_ptr<BlobDataHandles> match_all_handles) { 861 std::unique_ptr<BlobDataHandles> match_all_handles) {
846 if (match_all_error != CACHE_STORAGE_OK) { 862 if (match_all_error != CACHE_STORAGE_OK) {
847 callback.Run(match_all_error, std::unique_ptr<ServiceWorkerResponse>(), 863 std::move(callback).Run(match_all_error,
848 std::unique_ptr<storage::BlobDataHandle>()); 864 std::unique_ptr<ServiceWorkerResponse>(),
865 std::unique_ptr<storage::BlobDataHandle>());
849 return; 866 return;
850 } 867 }
851 868
852 if (match_all_responses->empty()) { 869 if (match_all_responses->empty()) {
853 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, 870 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND,
854 std::unique_ptr<ServiceWorkerResponse>(), 871 std::unique_ptr<ServiceWorkerResponse>(),
855 std::unique_ptr<storage::BlobDataHandle>()); 872 std::unique_ptr<storage::BlobDataHandle>());
856 return; 873 return;
857 } 874 }
858 875
859 std::unique_ptr<ServiceWorkerResponse> response = 876 std::unique_ptr<ServiceWorkerResponse> response =
860 base::MakeUnique<ServiceWorkerResponse>(match_all_responses->at(0)); 877 base::MakeUnique<ServiceWorkerResponse>(match_all_responses->at(0));
861 878
862 callback.Run(CACHE_STORAGE_OK, std::move(response), 879 std::move(callback).Run(CACHE_STORAGE_OK, std::move(response),
863 std::move(match_all_handles->at(0))); 880 std::move(match_all_handles->at(0)));
864 } 881 }
865 882
866 void CacheStorageCache::MatchAllImpl( 883 void CacheStorageCache::MatchAllImpl(
867 std::unique_ptr<ServiceWorkerFetchRequest> request, 884 std::unique_ptr<ServiceWorkerFetchRequest> request,
868 const CacheStorageCacheQueryParams& options, 885 const CacheStorageCacheQueryParams& options,
869 const ResponsesCallback& callback) { 886 ResponsesCallback callback) {
870 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 887 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
871 if (backend_state_ != BACKEND_OPEN) { 888 if (backend_state_ != BACKEND_OPEN) {
872 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), 889 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
873 std::unique_ptr<BlobDataHandles>()); 890 std::unique_ptr<Responses>(),
891 std::unique_ptr<BlobDataHandles>());
874 return; 892 return;
875 } 893 }
876 894
877 QueryCache(std::move(request), options, 895 QueryCache(
878 QueryCacheType::REQUESTS_AND_RESPONSES, 896 std::move(request), options, QueryCacheType::REQUESTS_AND_RESPONSES,
879 base::Bind(&CacheStorageCache::MatchAllDidQueryCache, 897 base::BindOnce(&CacheStorageCache::MatchAllDidQueryCache,
880 weak_ptr_factory_.GetWeakPtr(), callback)); 898 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
881 } 899 }
882 900
883 void CacheStorageCache::MatchAllDidQueryCache( 901 void CacheStorageCache::MatchAllDidQueryCache(
884 const ResponsesCallback& callback, 902 ResponsesCallback callback,
885 CacheStorageError error, 903 CacheStorageError error,
886 std::unique_ptr<QueryCacheResults> query_cache_results) { 904 std::unique_ptr<QueryCacheResults> query_cache_results) {
887 if (error != CACHE_STORAGE_OK) { 905 if (error != CACHE_STORAGE_OK) {
888 callback.Run(error, std::unique_ptr<Responses>(), 906 std::move(callback).Run(error, std::unique_ptr<Responses>(),
889 std::unique_ptr<BlobDataHandles>()); 907 std::unique_ptr<BlobDataHandles>());
890 return; 908 return;
891 } 909 }
892 910
893 std::unique_ptr<Responses> out_responses = base::MakeUnique<Responses>(); 911 std::unique_ptr<Responses> out_responses = base::MakeUnique<Responses>();
894 std::unique_ptr<BlobDataHandles> out_handles = 912 std::unique_ptr<BlobDataHandles> out_handles =
895 base::MakeUnique<BlobDataHandles>(); 913 base::MakeUnique<BlobDataHandles>();
896 out_responses->reserve(query_cache_results->size()); 914 out_responses->reserve(query_cache_results->size());
897 out_handles->reserve(query_cache_results->size()); 915 out_handles->reserve(query_cache_results->size());
898 916
899 for (auto& result : *query_cache_results) { 917 for (auto& result : *query_cache_results) {
900 out_responses->push_back(*result.response); 918 out_responses->push_back(*result.response);
901 out_handles->push_back(std::move(result.blob_handle)); 919 out_handles->push_back(std::move(result.blob_handle));
902 } 920 }
903 921
904 callback.Run(CACHE_STORAGE_OK, std::move(out_responses), 922 std::move(callback).Run(CACHE_STORAGE_OK, std::move(out_responses),
905 std::move(out_handles)); 923 std::move(out_handles));
906 } 924 }
907 925
908 void CacheStorageCache::WriteSideDataDidGetQuota( 926 void CacheStorageCache::WriteSideDataDidGetQuota(
909 const ErrorCallback& callback, 927 ErrorCallback callback,
910 const GURL& url, 928 const GURL& url,
911 base::Time expected_response_time, 929 base::Time expected_response_time,
912 scoped_refptr<net::IOBuffer> buffer, 930 scoped_refptr<net::IOBuffer> buffer,
913 int buf_len, 931 int buf_len,
914 storage::QuotaStatusCode status_code, 932 storage::QuotaStatusCode status_code,
915 int64_t usage, 933 int64_t usage,
916 int64_t quota) { 934 int64_t quota) {
917 if (status_code != storage::kQuotaStatusOk || (buf_len > quota - usage)) { 935 if (status_code != storage::kQuotaStatusOk || (buf_len > quota - usage)) {
918 base::ThreadTaskRunnerHandle::Get()->PostTask( 936 base::ThreadTaskRunnerHandle::Get()->PostTask(
919 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_QUOTA_EXCEEDED)); 937 FROM_HERE, base::BindOnce(std::move(callback),
938 CACHE_STORAGE_ERROR_QUOTA_EXCEEDED));
920 return; 939 return;
921 } 940 }
922 941
923 scheduler_->ScheduleOperation(base::Bind( 942 scheduler_->ScheduleOperation(base::BindOnce(
924 &CacheStorageCache::WriteSideDataImpl, weak_ptr_factory_.GetWeakPtr(), 943 &CacheStorageCache::WriteSideDataImpl, weak_ptr_factory_.GetWeakPtr(),
925 scheduler_->WrapCallbackToRunNext(callback), url, expected_response_time, 944 scheduler_->WrapCallbackToRunNext(std::move(callback)), url,
926 buffer, buf_len)); 945 expected_response_time, buffer, buf_len));
927 } 946 }
928 947
929 void CacheStorageCache::WriteSideDataImpl(const ErrorCallback& callback, 948 void CacheStorageCache::WriteSideDataImpl(ErrorCallback callback,
930 const GURL& url, 949 const GURL& url,
931 base::Time expected_response_time, 950 base::Time expected_response_time,
932 scoped_refptr<net::IOBuffer> buffer, 951 scoped_refptr<net::IOBuffer> buffer,
933 int buf_len) { 952 int buf_len) {
934 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 953 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
935 if (backend_state_ != BACKEND_OPEN) { 954 if (backend_state_ != BACKEND_OPEN) {
936 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 955 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
937 return; 956 return;
938 } 957 }
939 958
940 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr( 959 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr(
941 new disk_cache::Entry*()); 960 new disk_cache::Entry*());
942 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); 961 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
943 net::CompletionCallback open_entry_callback = base::Bind( 962 net::CompletionCallback open_entry_callback = base::AdaptCallbackForRepeating(
944 &CacheStorageCache::WriteSideDataDidOpenEntry, 963 base::BindOnce(&CacheStorageCache::WriteSideDataDidOpenEntry,
945 weak_ptr_factory_.GetWeakPtr(), callback, expected_response_time, buffer, 964 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
946 buf_len, base::Passed(std::move(scoped_entry_ptr))); 965 expected_response_time, buffer, buf_len,
966 base::Passed(std::move(scoped_entry_ptr))));
947 967
948 int rv = backend_->OpenEntry(url.spec(), entry_ptr, open_entry_callback); 968 int rv = backend_->OpenEntry(url.spec(), entry_ptr, open_entry_callback);
949 if (rv != net::ERR_IO_PENDING) 969 if (rv != net::ERR_IO_PENDING)
950 open_entry_callback.Run(rv); 970 open_entry_callback.Run(rv);
951 } 971 }
952 972
953 void CacheStorageCache::WriteSideDataDidOpenEntry( 973 void CacheStorageCache::WriteSideDataDidOpenEntry(
954 const ErrorCallback& callback, 974 ErrorCallback callback,
955 base::Time expected_response_time, 975 base::Time expected_response_time,
956 scoped_refptr<net::IOBuffer> buffer, 976 scoped_refptr<net::IOBuffer> buffer,
957 int buf_len, 977 int buf_len,
958 std::unique_ptr<disk_cache::Entry*> entry_ptr, 978 std::unique_ptr<disk_cache::Entry*> entry_ptr,
959 int rv) { 979 int rv) {
960 if (rv != net::OK) { 980 if (rv != net::OK) {
961 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); 981 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND);
962 return; 982 return;
963 } 983 }
964 disk_cache::ScopedEntryPtr entry(*entry_ptr); 984 disk_cache::ScopedEntryPtr entry(*entry_ptr);
965 985
966 ReadMetadata(*entry_ptr, 986 ReadMetadata(*entry_ptr,
967 base::Bind(&CacheStorageCache::WriteSideDataDidReadMetaData, 987 base::BindOnce(&CacheStorageCache::WriteSideDataDidReadMetaData,
968 weak_ptr_factory_.GetWeakPtr(), callback, 988 weak_ptr_factory_.GetWeakPtr(),
969 expected_response_time, buffer, buf_len, 989 std::move(callback), expected_response_time,
970 base::Passed(std::move(entry)))); 990 buffer, buf_len, base::Passed(std::move(entry))));
971 } 991 }
972 992
973 void CacheStorageCache::WriteSideDataDidReadMetaData( 993 void CacheStorageCache::WriteSideDataDidReadMetaData(
974 const ErrorCallback& callback, 994 ErrorCallback callback,
975 base::Time expected_response_time, 995 base::Time expected_response_time,
976 scoped_refptr<net::IOBuffer> buffer, 996 scoped_refptr<net::IOBuffer> buffer,
977 int buf_len, 997 int buf_len,
978 disk_cache::ScopedEntryPtr entry, 998 disk_cache::ScopedEntryPtr entry,
979 std::unique_ptr<proto::CacheMetadata> headers) { 999 std::unique_ptr<proto::CacheMetadata> headers) {
980 if (!headers || 1000 if (!headers ||
981 headers->response().response_time() != 1001 headers->response().response_time() !=
982 expected_response_time.ToInternalValue()) { 1002 expected_response_time.ToInternalValue()) {
983 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); 1003 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND);
984 return; 1004 return;
985 } 1005 }
986 // Get a temporary copy of the entry pointer before passing it in base::Bind. 1006 // Get a temporary copy of the entry pointer before passing it in base::Bind.
987 disk_cache::Entry* temp_entry_ptr = entry.get(); 1007 disk_cache::Entry* temp_entry_ptr = entry.get();
988 1008
989 net::CompletionCallback write_side_data_callback = base::Bind( 1009 net::CompletionCallback write_side_data_callback =
990 &CacheStorageCache::WriteSideDataDidWrite, weak_ptr_factory_.GetWeakPtr(), 1010 base::AdaptCallbackForRepeating(
991 callback, base::Passed(std::move(entry)), buf_len); 1011 base::BindOnce(&CacheStorageCache::WriteSideDataDidWrite,
1012 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
1013 base::Passed(std::move(entry)), buf_len));
992 1014
993 int rv = temp_entry_ptr->WriteData( 1015 int rv = temp_entry_ptr->WriteData(
994 INDEX_SIDE_DATA, 0 /* offset */, buffer.get(), buf_len, 1016 INDEX_SIDE_DATA, 0 /* offset */, buffer.get(), buf_len,
995 write_side_data_callback, true /* truncate */); 1017 write_side_data_callback, true /* truncate */);
996 1018
997 if (rv != net::ERR_IO_PENDING) 1019 if (rv != net::ERR_IO_PENDING)
998 write_side_data_callback.Run(rv); 1020 write_side_data_callback.Run(rv);
999 } 1021 }
1000 1022
1001 void CacheStorageCache::WriteSideDataDidWrite(const ErrorCallback& callback, 1023 void CacheStorageCache::WriteSideDataDidWrite(ErrorCallback callback,
1002 disk_cache::ScopedEntryPtr entry, 1024 disk_cache::ScopedEntryPtr entry,
1003 int expected_bytes, 1025 int expected_bytes,
1004 int rv) { 1026 int rv) {
1005 if (rv != expected_bytes) { 1027 if (rv != expected_bytes) {
1006 entry->Doom(); 1028 entry->Doom();
1007 UpdateCacheSize(base::Bind(callback, CACHE_STORAGE_ERROR_NOT_FOUND)); 1029 UpdateCacheSize(
1030 base::BindOnce(std::move(callback), CACHE_STORAGE_ERROR_NOT_FOUND));
1008 return; 1031 return;
1009 } 1032 }
1010 1033
1011 if (rv > 0) 1034 if (rv > 0)
1012 storage::RecordBytesWritten(kRecordBytesLabel, rv); 1035 storage::RecordBytesWritten(kRecordBytesLabel, rv);
1013 1036
1014 UpdateCacheSize(base::Bind(callback, CACHE_STORAGE_OK)); 1037 UpdateCacheSize(base::BindOnce(std::move(callback), CACHE_STORAGE_OK));
1015 } 1038 }
1016 1039
1017 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, 1040 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation,
1018 const ErrorCallback& callback) { 1041 ErrorCallback callback) {
1019 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 1042 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
1020 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); 1043 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type);
1021 1044
1022 std::unique_ptr<ServiceWorkerFetchRequest> request( 1045 std::unique_ptr<ServiceWorkerFetchRequest> request(
1023 new ServiceWorkerFetchRequest( 1046 new ServiceWorkerFetchRequest(
1024 operation.request.url, operation.request.method, 1047 operation.request.url, operation.request.method,
1025 operation.request.headers, operation.request.referrer, 1048 operation.request.headers, operation.request.referrer,
1026 operation.request.is_reload)); 1049 operation.request.is_reload));
1027 1050
1028 std::unique_ptr<ServiceWorkerResponse> response = 1051 std::unique_ptr<ServiceWorkerResponse> response =
1029 base::MakeUnique<ServiceWorkerResponse>(operation.response); 1052 base::MakeUnique<ServiceWorkerResponse>(operation.response);
1030 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; 1053 std::unique_ptr<storage::BlobDataHandle> blob_data_handle;
1031 1054
1032 if (!response->blob_uuid.empty()) { 1055 if (!response->blob_uuid.empty()) {
1033 if (!blob_storage_context_) { 1056 if (!blob_storage_context_) {
1034 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1057 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1035 return; 1058 return;
1036 } 1059 }
1037 blob_data_handle = 1060 blob_data_handle =
1038 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid); 1061 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid);
1039 if (!blob_data_handle) { 1062 if (!blob_data_handle) {
1040 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1063 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1041 return; 1064 return;
1042 } 1065 }
1043 } 1066 }
1044 1067
1045 UMA_HISTOGRAM_ENUMERATION( 1068 UMA_HISTOGRAM_ENUMERATION(
1046 "ServiceWorkerCache.Cache.AllWritesResponseType", 1069 "ServiceWorkerCache.Cache.AllWritesResponseType",
1047 operation.response.response_type, 1070 operation.response.response_type,
1048 blink::WebServiceWorkerResponseType::kWebServiceWorkerResponseTypeLast + 1071 blink::WebServiceWorkerResponseType::kWebServiceWorkerResponseTypeLast +
1049 1); 1072 1);
1050 1073
1051 std::unique_ptr<PutContext> put_context(new PutContext( 1074 std::unique_ptr<PutContext> put_context(new PutContext(
1052 std::move(request), std::move(response), std::move(blob_data_handle), 1075 std::move(request), std::move(response), std::move(blob_data_handle),
1053 scheduler_->WrapCallbackToRunNext(callback))); 1076 scheduler_->WrapCallbackToRunNext(std::move(callback))));
1054 1077
1055 scheduler_->ScheduleOperation( 1078 scheduler_->ScheduleOperation(base::BindOnce(
1056 base::Bind(&CacheStorageCache::PutImpl, weak_ptr_factory_.GetWeakPtr(), 1079 &CacheStorageCache::PutImpl, weak_ptr_factory_.GetWeakPtr(),
1057 base::Passed(std::move(put_context)))); 1080 base::Passed(std::move(put_context))));
1058 } 1081 }
1059 1082
1060 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) { 1083 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) {
1061 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1084 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1062 if (backend_state_ != BACKEND_OPEN) { 1085 if (backend_state_ != BACKEND_OPEN) {
1063 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1086 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1064 return; 1087 return;
1065 } 1088 }
1066 1089
1067 std::string key = put_context->request->url.spec(); 1090 std::string key = put_context->request->url.spec();
1068 1091
1069 net::CompletionCallback callback = base::Bind( 1092 net::CompletionCallback callback =
1070 &CacheStorageCache::PutDidDoomEntry, weak_ptr_factory_.GetWeakPtr(), 1093 base::AdaptCallbackForRepeating(base::BindOnce(
1071 base::Passed(std::move(put_context))); 1094 &CacheStorageCache::PutDidDoomEntry, weak_ptr_factory_.GetWeakPtr(),
1095 base::Passed(std::move(put_context))));
1072 1096
1073 int rv = backend_->DoomEntry(key, callback); 1097 int rv = backend_->DoomEntry(key, callback);
1074 if (rv != net::ERR_IO_PENDING) 1098 if (rv != net::ERR_IO_PENDING)
1075 callback.Run(rv); 1099 callback.Run(rv);
1076 } 1100 }
1077 1101
1078 void CacheStorageCache::PutDidDoomEntry(std::unique_ptr<PutContext> put_context, 1102 void CacheStorageCache::PutDidDoomEntry(std::unique_ptr<PutContext> put_context,
1079 int rv) { 1103 int rv) {
1080 if (backend_state_ != BACKEND_OPEN) { 1104 if (backend_state_ != BACKEND_OPEN) {
1081 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1105 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1082 return; 1106 return;
1083 } 1107 }
1084 1108
1085 // |rv| is ignored as doom entry can fail if the entry doesn't exist. 1109 // |rv| is ignored as doom entry can fail if the entry doesn't exist.
1086 1110
1087 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr( 1111 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr(
1088 new disk_cache::Entry*()); 1112 new disk_cache::Entry*());
1089 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); 1113 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
1090 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); 1114 ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
1091 disk_cache::Backend* backend_ptr = backend_.get(); 1115 disk_cache::Backend* backend_ptr = backend_.get();
1092 1116
1093 net::CompletionCallback create_entry_callback = base::Bind( 1117 net::CompletionCallback create_entry_callback =
1094 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), 1118 base::AdaptCallbackForRepeating(base::BindOnce(
1095 base::Passed(std::move(scoped_entry_ptr)), 1119 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(),
1096 base::Passed(std::move(put_context))); 1120 base::Passed(std::move(scoped_entry_ptr)),
1121 base::Passed(std::move(put_context))));
1097 1122
1098 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, 1123 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr,
1099 create_entry_callback); 1124 create_entry_callback);
1100 1125
1101 if (create_rv != net::ERR_IO_PENDING) 1126 if (create_rv != net::ERR_IO_PENDING)
1102 create_entry_callback.Run(create_rv); 1127 create_entry_callback.Run(create_rv);
1103 } 1128 }
1104 1129
1105 void CacheStorageCache::PutDidCreateEntry( 1130 void CacheStorageCache::PutDidCreateEntry(
1106 std::unique_ptr<disk_cache::Entry*> entry_ptr, 1131 std::unique_ptr<disk_cache::Entry*> entry_ptr,
1107 std::unique_ptr<PutContext> put_context, 1132 std::unique_ptr<PutContext> put_context,
1108 int rv) { 1133 int rv) {
1109 put_context->cache_entry.reset(*entry_ptr); 1134 put_context->cache_entry.reset(*entry_ptr);
1110 1135
1111 if (rv != net::OK) { 1136 if (rv != net::OK) {
1112 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); 1137 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_EXISTS);
1113 return; 1138 return;
1114 } 1139 }
1115 1140
1116 proto::CacheMetadata metadata; 1141 proto::CacheMetadata metadata;
1117 metadata.set_entry_time(base::Time::Now().ToInternalValue()); 1142 metadata.set_entry_time(base::Time::Now().ToInternalValue());
1118 proto::CacheRequest* request_metadata = metadata.mutable_request(); 1143 proto::CacheRequest* request_metadata = metadata.mutable_request();
1119 request_metadata->set_method(put_context->request->method); 1144 request_metadata->set_method(put_context->request->method);
1120 for (ServiceWorkerHeaderMap::const_iterator it = 1145 for (ServiceWorkerHeaderMap::const_iterator it =
1121 put_context->request->headers.begin(); 1146 put_context->request->headers.begin();
1122 it != put_context->request->headers.end(); ++it) { 1147 it != put_context->request->headers.end(); ++it) {
(...skipping 20 matching lines...) Expand all
1143 DCHECK_EQ(std::string::npos, it->second.find('\0')); 1168 DCHECK_EQ(std::string::npos, it->second.find('\0'));
1144 proto::CacheHeaderMap* header_map = response_metadata->add_headers(); 1169 proto::CacheHeaderMap* header_map = response_metadata->add_headers();
1145 header_map->set_name(it->first); 1170 header_map->set_name(it->first);
1146 header_map->set_value(it->second); 1171 header_map->set_value(it->second);
1147 } 1172 }
1148 for (const auto& header : put_context->response->cors_exposed_header_names) 1173 for (const auto& header : put_context->response->cors_exposed_header_names)
1149 response_metadata->add_cors_exposed_header_names(header); 1174 response_metadata->add_cors_exposed_header_names(header);
1150 1175
1151 std::unique_ptr<std::string> serialized(new std::string()); 1176 std::unique_ptr<std::string> serialized(new std::string());
1152 if (!metadata.SerializeToString(serialized.get())) { 1177 if (!metadata.SerializeToString(serialized.get())) {
1153 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1178 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1154 return; 1179 return;
1155 } 1180 }
1156 1181
1157 scoped_refptr<net::StringIOBuffer> buffer( 1182 scoped_refptr<net::StringIOBuffer> buffer(
1158 new net::StringIOBuffer(std::move(serialized))); 1183 new net::StringIOBuffer(std::move(serialized)));
1159 1184
1160 // Get a temporary copy of the entry pointer before passing it in base::Bind. 1185 // Get a temporary copy of the entry pointer before passing it in base::Bind.
1161 disk_cache::Entry* temp_entry_ptr = put_context->cache_entry.get(); 1186 disk_cache::Entry* temp_entry_ptr = put_context->cache_entry.get();
1162 1187
1163 net::CompletionCallback write_headers_callback = base::Bind( 1188 net::CompletionCallback write_headers_callback =
1164 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), 1189 base::AdaptCallbackForRepeating(
1165 base::Passed(std::move(put_context)), buffer->size()); 1190 base::BindOnce(&CacheStorageCache::PutDidWriteHeaders,
1191 weak_ptr_factory_.GetWeakPtr(),
1192 base::Passed(std::move(put_context)), buffer->size()));
1166 1193
1167 rv = temp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), 1194 rv = temp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(),
1168 buffer->size(), write_headers_callback, 1195 buffer->size(), write_headers_callback,
1169 true /* truncate */); 1196 true /* truncate */);
1170 1197
1171 if (rv != net::ERR_IO_PENDING) 1198 if (rv != net::ERR_IO_PENDING)
1172 write_headers_callback.Run(rv); 1199 write_headers_callback.Run(rv);
1173 } 1200 }
1174 1201
1175 void CacheStorageCache::PutDidWriteHeaders( 1202 void CacheStorageCache::PutDidWriteHeaders(
1176 std::unique_ptr<PutContext> put_context, 1203 std::unique_ptr<PutContext> put_context,
1177 int expected_bytes, 1204 int expected_bytes,
1178 int rv) { 1205 int rv) {
1179 if (rv != expected_bytes) { 1206 if (rv != expected_bytes) {
1180 put_context->cache_entry->Doom(); 1207 put_context->cache_entry->Doom();
1181 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1208 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1182 return; 1209 return;
1183 } 1210 }
1184 1211
1185 if (rv > 0) 1212 if (rv > 0)
1186 storage::RecordBytesWritten(kRecordBytesLabel, rv); 1213 storage::RecordBytesWritten(kRecordBytesLabel, rv);
1187 1214
1188 // The metadata is written, now for the response content. The data is streamed 1215 // The metadata is written, now for the response content. The data is streamed
1189 // from the blob into the cache entry. 1216 // from the blob into the cache entry.
1190 1217
1191 if (put_context->response->blob_uuid.empty()) { 1218 if (put_context->response->blob_uuid.empty()) {
1192 UpdateCacheSize(base::Bind(put_context->callback, CACHE_STORAGE_OK)); 1219 UpdateCacheSize(
1220 base::BindOnce(std::move(put_context->callback), CACHE_STORAGE_OK));
1193 return; 1221 return;
1194 } 1222 }
1195 1223
1196 DCHECK(put_context->blob_data_handle); 1224 DCHECK(put_context->blob_data_handle);
1197 1225
1198 disk_cache::ScopedEntryPtr entry(std::move(put_context->cache_entry)); 1226 disk_cache::ScopedEntryPtr entry(std::move(put_context->cache_entry));
1199 put_context->cache_entry = NULL; 1227 put_context->cache_entry = NULL;
1200 1228
1201 auto blob_to_cache = base::MakeUnique<CacheStorageBlobToDiskCache>(); 1229 auto blob_to_cache = base::MakeUnique<CacheStorageBlobToDiskCache>();
1202 CacheStorageBlobToDiskCache* blob_to_cache_raw = blob_to_cache.get(); 1230 CacheStorageBlobToDiskCache* blob_to_cache_raw = blob_to_cache.get();
1203 BlobToDiskCacheIDMap::KeyType blob_to_cache_key = 1231 BlobToDiskCacheIDMap::KeyType blob_to_cache_key =
1204 active_blob_to_disk_cache_writers_.Add(std::move(blob_to_cache)); 1232 active_blob_to_disk_cache_writers_.Add(std::move(blob_to_cache));
1205 1233
1206 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 1234 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
1207 std::move(put_context->blob_data_handle); 1235 std::move(put_context->blob_data_handle);
1208 1236
1209 blob_to_cache_raw->StreamBlobToCache( 1237 blob_to_cache_raw->StreamBlobToCache(
1210 std::move(entry), INDEX_RESPONSE_BODY, request_context_getter_.get(), 1238 std::move(entry), INDEX_RESPONSE_BODY, request_context_getter_.get(),
1211 std::move(blob_data_handle), 1239 std::move(blob_data_handle),
1212 base::Bind(&CacheStorageCache::PutDidWriteBlobToCache, 1240 base::BindOnce(&CacheStorageCache::PutDidWriteBlobToCache,
1213 weak_ptr_factory_.GetWeakPtr(), 1241 weak_ptr_factory_.GetWeakPtr(),
1214 base::Passed(std::move(put_context)), blob_to_cache_key)); 1242 base::Passed(std::move(put_context)), blob_to_cache_key));
1215 } 1243 }
1216 1244
1217 void CacheStorageCache::PutDidWriteBlobToCache( 1245 void CacheStorageCache::PutDidWriteBlobToCache(
1218 std::unique_ptr<PutContext> put_context, 1246 std::unique_ptr<PutContext> put_context,
1219 BlobToDiskCacheIDMap::KeyType blob_to_cache_key, 1247 BlobToDiskCacheIDMap::KeyType blob_to_cache_key,
1220 disk_cache::ScopedEntryPtr entry, 1248 disk_cache::ScopedEntryPtr entry,
1221 bool success) { 1249 bool success) {
1222 DCHECK(entry); 1250 DCHECK(entry);
1223 put_context->cache_entry = std::move(entry); 1251 put_context->cache_entry = std::move(entry);
1224 1252
1225 active_blob_to_disk_cache_writers_.Remove(blob_to_cache_key); 1253 active_blob_to_disk_cache_writers_.Remove(blob_to_cache_key);
1226 1254
1227 if (!success) { 1255 if (!success) {
1228 put_context->cache_entry->Doom(); 1256 put_context->cache_entry->Doom();
1229 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1257 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1230 return; 1258 return;
1231 } 1259 }
1232 1260
1233 UpdateCacheSize(base::Bind(put_context->callback, CACHE_STORAGE_OK)); 1261 UpdateCacheSize(
1262 base::BindOnce(std::move(put_context->callback), CACHE_STORAGE_OK));
1234 } 1263 }
1235 1264
1236 void CacheStorageCache::UpdateCacheSize(const base::Closure& callback) { 1265 void CacheStorageCache::UpdateCacheSize(base::OnceClosure callback) {
1237 if (backend_state_ != BACKEND_OPEN) 1266 if (backend_state_ != BACKEND_OPEN)
1238 return; 1267 return;
1239 1268
1240 // Note that the callback holds a cache handle to keep the cache alive during 1269 // Note that the callback holds a cache handle to keep the cache alive during
1241 // the operation since this UpdateCacheSize is often run after an operation 1270 // the operation since this UpdateCacheSize is often run after an operation
1242 // completes and runs its callback. 1271 // completes and runs its callback.
1243 int rv = backend_->CalculateSizeOfAllEntries( 1272 net::CompletionCallback size_callback = base::AdaptCallbackForRepeating(
1244 base::Bind(&CacheStorageCache::UpdateCacheSizeGotSize, 1273 base::BindOnce(&CacheStorageCache::UpdateCacheSizeGotSize,
1245 weak_ptr_factory_.GetWeakPtr(), 1274 weak_ptr_factory_.GetWeakPtr(),
1246 base::Passed(CreateCacheHandle()), callback)); 1275 base::Passed(CreateCacheHandle()), std::move(callback)));
1247 1276
1277 int rv = backend_->CalculateSizeOfAllEntries(size_callback);
1248 if (rv != net::ERR_IO_PENDING) 1278 if (rv != net::ERR_IO_PENDING)
1249 UpdateCacheSizeGotSize(CreateCacheHandle(), callback, rv); 1279 std::move(size_callback).Run(rv);
1250 } 1280 }
1251 1281
1252 void CacheStorageCache::UpdateCacheSizeGotSize( 1282 void CacheStorageCache::UpdateCacheSizeGotSize(
1253 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 1283 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
1254 const base::Closure& callback, 1284 base::OnceClosure callback,
1255 int current_cache_size) { 1285 int current_cache_size) {
1256 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown); 1286 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown);
1257 int64_t old_cache_size = cache_size_; 1287 int64_t old_cache_size = cache_size_;
1258 cache_size_ = current_cache_size; 1288 cache_size_ = current_cache_size;
1259 1289
1260 int64_t size_delta = current_cache_size - old_cache_size; 1290 int64_t size_delta = current_cache_size - old_cache_size;
1261 1291
1262 quota_manager_proxy_->NotifyStorageModified( 1292 quota_manager_proxy_->NotifyStorageModified(
1263 storage::QuotaClient::kServiceWorkerCache, origin_, 1293 storage::QuotaClient::kServiceWorkerCache, origin_,
1264 storage::kStorageTypeTemporary, size_delta); 1294 storage::kStorageTypeTemporary, size_delta);
1265 1295
1266 if (cache_observer_) 1296 if (cache_observer_)
1267 cache_observer_->CacheSizeUpdated(this, current_cache_size); 1297 cache_observer_->CacheSizeUpdated(this, current_cache_size);
1268 1298
1269 callback.Run(); 1299 std::move(callback).Run();
1270 } 1300 }
1271 1301
1272 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation, 1302 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation,
1273 const ErrorCallback& callback) { 1303 ErrorCallback callback) {
1274 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 1304 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
1275 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE, 1305 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE,
1276 operation.operation_type); 1306 operation.operation_type);
1277 1307
1278 std::unique_ptr<ServiceWorkerFetchRequest> request( 1308 std::unique_ptr<ServiceWorkerFetchRequest> request(
1279 new ServiceWorkerFetchRequest( 1309 new ServiceWorkerFetchRequest(
1280 operation.request.url, operation.request.method, 1310 operation.request.url, operation.request.method,
1281 operation.request.headers, operation.request.referrer, 1311 operation.request.headers, operation.request.referrer,
1282 operation.request.is_reload)); 1312 operation.request.is_reload));
1283 1313
1284 scheduler_->ScheduleOperation( 1314 scheduler_->ScheduleOperation(base::BindOnce(
1285 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(), 1315 &CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(),
1286 base::Passed(std::move(request)), operation.match_params, 1316 base::Passed(std::move(request)), operation.match_params,
1287 scheduler_->WrapCallbackToRunNext(callback))); 1317 scheduler_->WrapCallbackToRunNext(std::move(callback))));
1288 } 1318 }
1289 1319
1290 void CacheStorageCache::DeleteImpl( 1320 void CacheStorageCache::DeleteImpl(
1291 std::unique_ptr<ServiceWorkerFetchRequest> request, 1321 std::unique_ptr<ServiceWorkerFetchRequest> request,
1292 const CacheStorageCacheQueryParams& match_params, 1322 const CacheStorageCacheQueryParams& match_params,
1293 const ErrorCallback& callback) { 1323 ErrorCallback callback) {
1294 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1324 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1295 if (backend_state_ != BACKEND_OPEN) { 1325 if (backend_state_ != BACKEND_OPEN) {
1296 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1326 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1297 return; 1327 return;
1298 } 1328 }
1299 1329
1300 QueryCache(std::move(request), match_params, QueryCacheType::CACHE_ENTRIES, 1330 QueryCache(
1301 base::Bind(&CacheStorageCache::DeleteDidQueryCache, 1331 std::move(request), match_params, QueryCacheType::CACHE_ENTRIES,
1302 weak_ptr_factory_.GetWeakPtr(), callback)); 1332 base::BindOnce(&CacheStorageCache::DeleteDidQueryCache,
1333 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
1303 } 1334 }
1304 1335
1305 void CacheStorageCache::DeleteDidQueryCache( 1336 void CacheStorageCache::DeleteDidQueryCache(
1306 const ErrorCallback& callback, 1337 ErrorCallback callback,
1307 CacheStorageError error, 1338 CacheStorageError error,
1308 std::unique_ptr<QueryCacheResults> query_cache_results) { 1339 std::unique_ptr<QueryCacheResults> query_cache_results) {
1309 if (error != CACHE_STORAGE_OK) { 1340 if (error != CACHE_STORAGE_OK) {
1310 callback.Run(error); 1341 std::move(callback).Run(error);
1311 return; 1342 return;
1312 } 1343 }
1313 1344
1314 if (query_cache_results->empty()) { 1345 if (query_cache_results->empty()) {
1315 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); 1346 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND);
1316 return; 1347 return;
1317 } 1348 }
1318 1349
1319 for (auto& result : *query_cache_results) { 1350 for (auto& result : *query_cache_results) {
1320 disk_cache::ScopedEntryPtr entry = std::move(result.entry); 1351 disk_cache::ScopedEntryPtr entry = std::move(result.entry);
1321 entry->Doom(); 1352 entry->Doom();
1322 } 1353 }
1323 1354
1324 UpdateCacheSize(base::Bind(callback, CACHE_STORAGE_OK)); 1355 UpdateCacheSize(base::BindOnce(std::move(callback), CACHE_STORAGE_OK));
1325 } 1356 }
1326 1357
1327 void CacheStorageCache::KeysImpl( 1358 void CacheStorageCache::KeysImpl(
1328 std::unique_ptr<ServiceWorkerFetchRequest> request, 1359 std::unique_ptr<ServiceWorkerFetchRequest> request,
1329 const CacheStorageCacheQueryParams& options, 1360 const CacheStorageCacheQueryParams& options,
1330 const RequestsCallback& callback) { 1361 RequestsCallback callback) {
1331 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1362 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1332 if (backend_state_ != BACKEND_OPEN) { 1363 if (backend_state_ != BACKEND_OPEN) {
1333 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>()); 1364 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
1365 std::unique_ptr<Requests>());
1334 return; 1366 return;
1335 } 1367 }
1336 1368
1337 QueryCache(std::move(request), options, QueryCacheType::REQUESTS, 1369 QueryCache(
1338 base::Bind(&CacheStorageCache::KeysDidQueryCache, 1370 std::move(request), options, QueryCacheType::REQUESTS,
1339 weak_ptr_factory_.GetWeakPtr(), callback)); 1371 base::BindOnce(&CacheStorageCache::KeysDidQueryCache,
1372 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
1340 } 1373 }
1341 1374
1342 void CacheStorageCache::KeysDidQueryCache( 1375 void CacheStorageCache::KeysDidQueryCache(
1343 const RequestsCallback& callback, 1376 RequestsCallback callback,
1344 CacheStorageError error, 1377 CacheStorageError error,
1345 std::unique_ptr<QueryCacheResults> query_cache_results) { 1378 std::unique_ptr<QueryCacheResults> query_cache_results) {
1346 if (error != CACHE_STORAGE_OK) { 1379 if (error != CACHE_STORAGE_OK) {
1347 callback.Run(error, std::unique_ptr<Requests>()); 1380 std::move(callback).Run(error, std::unique_ptr<Requests>());
1348 return; 1381 return;
1349 } 1382 }
1350 1383
1351 std::unique_ptr<Requests> out_requests = base::MakeUnique<Requests>(); 1384 std::unique_ptr<Requests> out_requests = base::MakeUnique<Requests>();
1352 out_requests->reserve(query_cache_results->size()); 1385 out_requests->reserve(query_cache_results->size());
1353 for (const auto& result : *query_cache_results) 1386 for (const auto& result : *query_cache_results)
1354 out_requests->push_back(*result.request); 1387 out_requests->push_back(*result.request);
1355 1388
1356 callback.Run(CACHE_STORAGE_OK, std::move(out_requests)); 1389 std::move(callback).Run(CACHE_STORAGE_OK, std::move(out_requests));
1357 } 1390 }
1358 1391
1359 void CacheStorageCache::CloseImpl(const base::Closure& callback) { 1392 void CacheStorageCache::CloseImpl(base::OnceClosure callback) {
1360 DCHECK_NE(BACKEND_CLOSED, backend_state_); 1393 DCHECK_NE(BACKEND_CLOSED, backend_state_);
1361 1394
1362 backend_state_ = BACKEND_CLOSED; 1395 backend_state_ = BACKEND_CLOSED;
1363 backend_.reset(); 1396 backend_.reset();
1364 callback.Run(); 1397 std::move(callback).Run();
1365 } 1398 }
1366 1399
1367 void CacheStorageCache::SizeImpl(const SizeCallback& callback) { 1400 void CacheStorageCache::SizeImpl(SizeCallback callback) {
1368 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1401 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1369 1402
1370 int64_t size = backend_state_ == BACKEND_OPEN ? cache_size_ : 0; 1403 int64_t size = backend_state_ == BACKEND_OPEN ? cache_size_ : 0;
1371 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1404 base::ThreadTaskRunnerHandle::Get()->PostTask(
1372 base::Bind(callback, size)); 1405 FROM_HERE, base::BindOnce(std::move(callback), size));
1373 } 1406 }
1374 1407
1375 void CacheStorageCache::GetSizeThenCloseDidGetSize(const SizeCallback& callback, 1408 void CacheStorageCache::GetSizeThenCloseDidGetSize(SizeCallback callback,
1376 int64_t cache_size) { 1409 int64_t cache_size) {
1377 CloseImpl(base::Bind(callback, cache_size)); 1410 CloseImpl(base::BindOnce(std::move(callback), cache_size));
1378 } 1411 }
1379 1412
1380 void CacheStorageCache::CreateBackend(const ErrorCallback& callback) { 1413 void CacheStorageCache::CreateBackend(ErrorCallback callback) {
1381 DCHECK(!backend_); 1414 DCHECK(!backend_);
1382 1415
1383 // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction. 1416 // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction.
1384 net::CacheType cache_type = memory_only_ ? net::MEMORY_CACHE : net::APP_CACHE; 1417 net::CacheType cache_type = memory_only_ ? net::MEMORY_CACHE : net::APP_CACHE;
1385 1418
1386 std::unique_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr()); 1419 std::unique_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr());
1387 1420
1388 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below. 1421 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below.
1389 ScopedBackendPtr* backend = backend_ptr.get(); 1422 ScopedBackendPtr* backend = backend_ptr.get();
1390 1423
1391 net::CompletionCallback create_cache_callback = 1424 net::CompletionCallback create_cache_callback =
1392 base::Bind(&CacheStorageCache::CreateBackendDidCreate, 1425 base::AdaptCallbackForRepeating(
1393 weak_ptr_factory_.GetWeakPtr(), callback, 1426 base::BindOnce(&CacheStorageCache::CreateBackendDidCreate,
1394 base::Passed(std::move(backend_ptr))); 1427 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
1428 base::Passed(std::move(backend_ptr))));
1395 1429
1396 // TODO(jkarlin): Use the cache task runner that ServiceWorkerCacheCore 1430 // TODO(jkarlin): Use the cache task runner that ServiceWorkerCacheCore
1397 // has for disk caches. 1431 // has for disk caches.
1398 int rv = disk_cache::CreateCacheBackend( 1432 int rv = disk_cache::CreateCacheBackend(
1399 cache_type, net::CACHE_BACKEND_SIMPLE, path_, kMaxCacheBytes, 1433 cache_type, net::CACHE_BACKEND_SIMPLE, path_, kMaxCacheBytes,
1400 false, /* force */ 1434 false, /* force */
1401 BrowserThread::GetTaskRunnerForThread(BrowserThread::CACHE).get(), NULL, 1435 BrowserThread::GetTaskRunnerForThread(BrowserThread::CACHE).get(), NULL,
1402 backend, create_cache_callback); 1436 backend, create_cache_callback);
1403 if (rv != net::ERR_IO_PENDING) 1437 if (rv != net::ERR_IO_PENDING)
1404 create_cache_callback.Run(rv); 1438 create_cache_callback.Run(rv);
1405 } 1439 }
1406 1440
1407 void CacheStorageCache::CreateBackendDidCreate( 1441 void CacheStorageCache::CreateBackendDidCreate(
1408 const CacheStorageCache::ErrorCallback& callback, 1442 CacheStorageCache::ErrorCallback callback,
1409 std::unique_ptr<ScopedBackendPtr> backend_ptr, 1443 std::unique_ptr<ScopedBackendPtr> backend_ptr,
1410 int rv) { 1444 int rv) {
1411 if (rv != net::OK) { 1445 if (rv != net::OK) {
1412 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1446 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1413 return; 1447 return;
1414 } 1448 }
1415 1449
1416 backend_ = std::move(*backend_ptr); 1450 backend_ = std::move(*backend_ptr);
1417 callback.Run(CACHE_STORAGE_OK); 1451 std::move(callback).Run(CACHE_STORAGE_OK);
1418 } 1452 }
1419 1453
1420 void CacheStorageCache::InitBackend() { 1454 void CacheStorageCache::InitBackend() {
1421 DCHECK_EQ(BACKEND_UNINITIALIZED, backend_state_); 1455 DCHECK_EQ(BACKEND_UNINITIALIZED, backend_state_);
1422 DCHECK(!initializing_); 1456 DCHECK(!initializing_);
1423 DCHECK(!scheduler_->ScheduledOperations()); 1457 DCHECK(!scheduler_->ScheduledOperations());
1424 initializing_ = true; 1458 initializing_ = true;
1425 1459
1426 scheduler_->ScheduleOperation(base::Bind( 1460 scheduler_->ScheduleOperation(base::BindOnce(
1427 &CacheStorageCache::CreateBackend, weak_ptr_factory_.GetWeakPtr(), 1461 &CacheStorageCache::CreateBackend, weak_ptr_factory_.GetWeakPtr(),
1428 base::Bind( 1462 base::BindOnce(&CacheStorageCache::InitDidCreateBackend,
1429 &CacheStorageCache::InitDidCreateBackend, 1463 weak_ptr_factory_.GetWeakPtr(),
1430 weak_ptr_factory_.GetWeakPtr(), 1464 scheduler_->WrapCallbackToRunNext(
1431 scheduler_->WrapCallbackToRunNext(base::Bind(&base::DoNothing))))); 1465 base::BindOnce(&base::DoNothing)))));
1432 } 1466 }
1433 1467
1434 void CacheStorageCache::InitDidCreateBackend( 1468 void CacheStorageCache::InitDidCreateBackend(
1435 const base::Closure& callback, 1469 base::OnceClosure callback,
1436 CacheStorageError cache_create_error) { 1470 CacheStorageError cache_create_error) {
1437 if (cache_create_error != CACHE_STORAGE_OK) { 1471 if (cache_create_error != CACHE_STORAGE_OK) {
1438 InitGotCacheSize(callback, cache_create_error, 0); 1472 InitGotCacheSize(std::move(callback), cache_create_error, 0);
1439 return; 1473 return;
1440 } 1474 }
1441 1475
1442 int rv = backend_->CalculateSizeOfAllEntries( 1476 net::CompletionCallback size_callback =
1443 base::Bind(&CacheStorageCache::InitGotCacheSize, 1477 base::AdaptCallbackForRepeating(base::BindOnce(
1444 weak_ptr_factory_.GetWeakPtr(), callback, cache_create_error)); 1478 &CacheStorageCache::InitGotCacheSize, weak_ptr_factory_.GetWeakPtr(),
1479 std::move(callback), cache_create_error));
1445 1480
1481 int rv = backend_->CalculateSizeOfAllEntries(size_callback);
cmumford 2017/06/21 19:07:56 Don't you lose copy elision by converting to a loc
jsbell 2017/06/21 20:38:16 Nope - because size_callback is a RepeatingClosure
1446 if (rv != net::ERR_IO_PENDING) 1482 if (rv != net::ERR_IO_PENDING)
1447 InitGotCacheSize(callback, cache_create_error, rv); 1483 std::move(size_callback).Run(rv);
1448 } 1484 }
1449 1485
1450 void CacheStorageCache::InitGotCacheSize(const base::Closure& callback, 1486 void CacheStorageCache::InitGotCacheSize(base::OnceClosure callback,
1451 CacheStorageError cache_create_error, 1487 CacheStorageError cache_create_error,
1452 int cache_size) { 1488 int cache_size) {
1453 // Now that we know the cache size either 1) the cache size should be unknown 1489 // Now that we know the cache size either 1) the cache size should be unknown
1454 // (which is why the size was calculated), or 2) it must match the current 1490 // (which is why the size was calculated), or 2) it must match the current
1455 // size. If the sizes aren't equal then there is a bug in how the cache size 1491 // size. If the sizes aren't equal then there is a bug in how the cache size
1456 // is saved in the store's index. 1492 // is saved in the store's index.
1457 if (cache_size_ != CacheStorage::kSizeUnknown) { 1493 if (cache_size_ != CacheStorage::kSizeUnknown) {
1458 LOG_IF(ERROR, cache_size_ != cache_size) 1494 LOG_IF(ERROR, cache_size_ != cache_size)
1459 << "Cache size: " << cache_size 1495 << "Cache size: " << cache_size
1460 << " does not match size from index: " << cache_size_; 1496 << " does not match size from index: " << cache_size_;
1461 UMA_HISTOGRAM_COUNTS_10M("ServiceWorkerCache.IndexSizeDifference", 1497 UMA_HISTOGRAM_COUNTS_10M("ServiceWorkerCache.IndexSizeDifference",
1462 std::abs(cache_size_ - cache_size)); 1498 std::abs(cache_size_ - cache_size));
1463 // Disabled for crbug.com/681900. 1499 // Disabled for crbug.com/681900.
1464 // DCHECK_EQ(cache_size_, cache_size); 1500 // DCHECK_EQ(cache_size_, cache_size);
1465 } 1501 }
1466 cache_size_ = cache_size; 1502 cache_size_ = cache_size;
1467 initializing_ = false; 1503 initializing_ = false;
1468 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ && 1504 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ &&
1469 backend_state_ == BACKEND_UNINITIALIZED) 1505 backend_state_ == BACKEND_UNINITIALIZED)
1470 ? BACKEND_OPEN 1506 ? BACKEND_OPEN
1471 : BACKEND_CLOSED; 1507 : BACKEND_CLOSED;
1472 1508
1473 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", 1509 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult",
1474 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1); 1510 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1);
1475 1511
1476 if (cache_observer_) 1512 if (cache_observer_)
1477 cache_observer_->CacheSizeUpdated(this, cache_size_); 1513 cache_observer_->CacheSizeUpdated(this, cache_size_);
1478 1514
1479 callback.Run(); 1515 std::move(callback).Run();
1480 } 1516 }
1481 1517
1482 std::unique_ptr<storage::BlobDataHandle> 1518 std::unique_ptr<storage::BlobDataHandle>
1483 CacheStorageCache::PopulateResponseBody(disk_cache::ScopedEntryPtr entry, 1519 CacheStorageCache::PopulateResponseBody(disk_cache::ScopedEntryPtr entry,
1484 ServiceWorkerResponse* response) { 1520 ServiceWorkerResponse* response) {
1485 DCHECK(blob_storage_context_); 1521 DCHECK(blob_storage_context_);
1486 1522
1487 // Create a blob with the response body data. 1523 // Create a blob with the response body data.
1488 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY); 1524 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY);
1489 response->blob_uuid = base::GenerateGUID(); 1525 response->blob_uuid = base::GenerateGUID();
1490 storage::BlobDataBuilder blob_data(response->blob_uuid); 1526 storage::BlobDataBuilder blob_data(response->blob_uuid);
1491 1527
1492 disk_cache::Entry* temp_entry = entry.get(); 1528 disk_cache::Entry* temp_entry = entry.get();
1493 blob_data.AppendDiskCacheEntryWithSideData( 1529 blob_data.AppendDiskCacheEntryWithSideData(
1494 new CacheStorageCacheDataHandle(CreateCacheHandle(), std::move(entry)), 1530 new CacheStorageCacheDataHandle(CreateCacheHandle(), std::move(entry)),
1495 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); 1531 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA);
1496 return blob_storage_context_->AddFinishedBlob(&blob_data); 1532 return blob_storage_context_->AddFinishedBlob(&blob_data);
1497 } 1533 }
1498 1534
1499 std::unique_ptr<CacheStorageCacheHandle> 1535 std::unique_ptr<CacheStorageCacheHandle>
1500 CacheStorageCache::CreateCacheHandle() { 1536 CacheStorageCache::CreateCacheHandle() {
1501 return cache_storage_->CreateCacheHandle(this); 1537 return cache_storage_->CreateCacheHandle(this);
1502 } 1538 }
1503 1539
1504 } // namespace content 1540 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698