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

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

Issue 2947753002: CacheStorage: Migrate to BindOnce/OnceCallback/OnceClosure (Closed)
Patch Set: 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 // TODO(jsbell): Untangle this hideous mess.
jsbell 2017/06/19 23:45:00 I need to tackle this before landing.
jsbell 2017/06/20 23:00:56 I reworked this in the latest CL, although I retai
474 ErrorCallback* callback_ptr = callback_copy.get(); 481 std::unique_ptr<base::RepeatingCallback<void(CacheStorageError)>>
475 base::Closure barrier_closure = base::BarrierClosure( 482 callback_copy(new base::RepeatingCallback<void(CacheStorageError)>(
476 operations.size(), base::Bind(&CacheStorageCache::BatchDidAllOperations, 483 base::AdaptCallbackForRepeating(std::move(callback))));
477 weak_ptr_factory_.GetWeakPtr(), 484 base::RepeatingCallback<void(CacheStorageError)>* callback_ptr =
478 base::Passed(std::move(callback_copy)))); 485 callback_copy.get();
479 ErrorCallback completion_callback = 486 base::RepeatingClosure barrier_closure = base::BarrierClosure(
480 base::Bind(&CacheStorageCache::BatchDidOneOperation, 487 operations.size(),
481 weak_ptr_factory_.GetWeakPtr(), barrier_closure, callback_ptr); 488 base::BindOnce(&CacheStorageCache::BatchDidAllOperations,
489 weak_ptr_factory_.GetWeakPtr(),
490 base::Passed(std::move(callback_copy))));
491 base::RepeatingCallback<void(CacheStorageError)> completion_callback =
492 base::BindRepeating(&CacheStorageCache::BatchDidOneOperation,
493 weak_ptr_factory_.GetWeakPtr(), barrier_closure,
494 callback_ptr);
482 495
483 for (const auto& operation : operations) { 496 for (const auto& operation : operations) {
484 switch (operation.operation_type) { 497 switch (operation.operation_type) {
485 case CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT: 498 case CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT:
486 Put(operation, completion_callback); 499 Put(operation, completion_callback);
487 break; 500 break;
488 case CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE: 501 case CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE:
489 DCHECK_EQ(1u, operations.size()); 502 DCHECK_EQ(1u, operations.size());
490 Delete(operation, completion_callback); 503 Delete(operation, completion_callback);
491 break; 504 break;
492 case CACHE_STORAGE_CACHE_OPERATION_TYPE_UNDEFINED: 505 case CACHE_STORAGE_CACHE_OPERATION_TYPE_UNDEFINED:
493 NOTREACHED(); 506 NOTREACHED();
494 // TODO(nhiroki): This should return "TypeError". 507 // TODO(nhiroki): This should return "TypeError".
495 // http://crbug.com/425505 508 // http://crbug.com/425505
496 completion_callback.Run(CACHE_STORAGE_ERROR_STORAGE); 509 completion_callback.Run(CACHE_STORAGE_ERROR_STORAGE);
497 break; 510 break;
498 } 511 }
499 } 512 }
500 } 513 }
501 514
502 void CacheStorageCache::BatchDidOneOperation( 515 void CacheStorageCache::BatchDidOneOperation(
503 const base::Closure& barrier_closure, 516 const base::RepeatingClosure& barrier_closure,
504 ErrorCallback* callback, 517 base::RepeatingCallback<void(CacheStorageError)>* callback,
505 CacheStorageError error) { 518 CacheStorageError error) {
506 if (callback->is_null() || error == CACHE_STORAGE_OK) { 519 if (callback->is_null() || error == CACHE_STORAGE_OK) {
507 barrier_closure.Run(); 520 barrier_closure.Run();
508 return; 521 return;
509 } 522 }
510 callback->Run(error); 523 callback->Run(error);
511 callback->Reset(); // Only call the callback once. 524 callback->Reset(); // Only call the callback once.
512 525
513 barrier_closure.Run(); 526 barrier_closure.Run();
514 } 527 }
515 528
516 void CacheStorageCache::BatchDidAllOperations( 529 void CacheStorageCache::BatchDidAllOperations(
517 std::unique_ptr<ErrorCallback> callback) { 530 std::unique_ptr<base::RepeatingCallback<void(CacheStorageError)>>
531 callback) {
518 if (callback->is_null()) 532 if (callback->is_null())
519 return; 533 return;
520 callback->Run(CACHE_STORAGE_OK); 534 callback->Run(CACHE_STORAGE_OK);
521 } 535 }
522 536
523 void CacheStorageCache::Keys(std::unique_ptr<ServiceWorkerFetchRequest> request, 537 void CacheStorageCache::Keys(std::unique_ptr<ServiceWorkerFetchRequest> request,
524 const CacheStorageCacheQueryParams& options, 538 const CacheStorageCacheQueryParams& options,
525 const RequestsCallback& callback) { 539 RequestsCallback callback) {
526 if (backend_state_ == BACKEND_CLOSED) { 540 if (backend_state_ == BACKEND_CLOSED) {
527 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>()); 541 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
542 std::unique_ptr<Requests>());
528 return; 543 return;
529 } 544 }
530 545
531 scheduler_->ScheduleOperation( 546 scheduler_->ScheduleOperation(base::BindOnce(
532 base::Bind(&CacheStorageCache::KeysImpl, weak_ptr_factory_.GetWeakPtr(), 547 &CacheStorageCache::KeysImpl, weak_ptr_factory_.GetWeakPtr(),
533 base::Passed(std::move(request)), options, 548 base::Passed(std::move(request)), options,
534 scheduler_->WrapCallbackToRunNext(callback))); 549 scheduler_->WrapCallbackToRunNext(std::move(callback))));
535 } 550 }
536 551
537 void CacheStorageCache::Close(const base::Closure& callback) { 552 void CacheStorageCache::Close(base::OnceClosure callback) {
538 DCHECK_NE(BACKEND_CLOSED, backend_state_) 553 DCHECK_NE(BACKEND_CLOSED, backend_state_)
539 << "Was CacheStorageCache::Close() called twice?"; 554 << "Was CacheStorageCache::Close() called twice?";
540 555
541 scheduler_->ScheduleOperation( 556 scheduler_->ScheduleOperation(base::BindOnce(
542 base::Bind(&CacheStorageCache::CloseImpl, weak_ptr_factory_.GetWeakPtr(), 557 &CacheStorageCache::CloseImpl, weak_ptr_factory_.GetWeakPtr(),
543 scheduler_->WrapCallbackToRunNext(callback))); 558 scheduler_->WrapCallbackToRunNext(std::move(callback))));
544 } 559 }
545 560
546 void CacheStorageCache::Size(const SizeCallback& callback) { 561 void CacheStorageCache::Size(SizeCallback callback) {
547 if (backend_state_ == BACKEND_CLOSED) { 562 if (backend_state_ == BACKEND_CLOSED) {
548 // TODO(jkarlin): Delete caches that can't be initialized. 563 // TODO(jkarlin): Delete caches that can't be initialized.
549 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 564 base::ThreadTaskRunnerHandle::Get()->PostTask(
550 base::Bind(callback, 0)); 565 FROM_HERE, base::BindOnce(std::move(callback), 0));
551 return; 566 return;
552 } 567 }
553 568
554 scheduler_->ScheduleOperation( 569 scheduler_->ScheduleOperation(base::BindOnce(
555 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(), 570 &CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(),
556 scheduler_->WrapCallbackToRunNext(callback))); 571 scheduler_->WrapCallbackToRunNext(std::move(callback))));
557 } 572 }
558 573
559 void CacheStorageCache::GetSizeThenClose(const SizeCallback& callback) { 574 void CacheStorageCache::GetSizeThenClose(SizeCallback callback) {
560 if (backend_state_ == BACKEND_CLOSED) { 575 if (backend_state_ == BACKEND_CLOSED) {
561 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 576 base::ThreadTaskRunnerHandle::Get()->PostTask(
562 base::Bind(callback, 0)); 577 FROM_HERE, base::BindOnce(std::move(callback), 0));
563 return; 578 return;
564 } 579 }
565 580
566 scheduler_->ScheduleOperation( 581 scheduler_->ScheduleOperation(base::BindOnce(
567 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(), 582 &CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(),
568 base::Bind(&CacheStorageCache::GetSizeThenCloseDidGetSize, 583 base::BindOnce(&CacheStorageCache::GetSizeThenCloseDidGetSize,
569 weak_ptr_factory_.GetWeakPtr(), 584 weak_ptr_factory_.GetWeakPtr(),
570 scheduler_->WrapCallbackToRunNext(callback)))); 585 scheduler_->WrapCallbackToRunNext(std::move(callback)))));
571 } 586 }
572 587
573 void CacheStorageCache::SetObserver(CacheStorageCacheObserver* observer) { 588 void CacheStorageCache::SetObserver(CacheStorageCacheObserver* observer) {
574 DCHECK((observer == nullptr) ^ (cache_observer_ == nullptr)); 589 DCHECK((observer == nullptr) ^ (cache_observer_ == nullptr));
575 cache_observer_ = observer; 590 cache_observer_ = observer;
576 } 591 }
577 592
578 CacheStorageCache::~CacheStorageCache() { 593 CacheStorageCache::~CacheStorageCache() {
579 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_); 594 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_);
580 } 595 }
(...skipping 24 matching lines...) Expand all
605 DCHECK(!origin_.is_empty()); 620 DCHECK(!origin_.is_empty());
606 DCHECK(quota_manager_proxy_.get()); 621 DCHECK(quota_manager_proxy_.get());
607 622
608 quota_manager_proxy_->NotifyOriginInUse(origin_); 623 quota_manager_proxy_->NotifyOriginInUse(origin_);
609 } 624 }
610 625
611 void CacheStorageCache::QueryCache( 626 void CacheStorageCache::QueryCache(
612 std::unique_ptr<ServiceWorkerFetchRequest> request, 627 std::unique_ptr<ServiceWorkerFetchRequest> request,
613 const CacheStorageCacheQueryParams& options, 628 const CacheStorageCacheQueryParams& options,
614 QueryCacheType query_type, 629 QueryCacheType query_type,
615 const QueryCacheCallback& callback) { 630 QueryCacheCallback callback) {
616 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 631 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
617 if (backend_state_ != BACKEND_OPEN) { 632 if (backend_state_ != BACKEND_OPEN) {
618 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 633 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
619 std::unique_ptr<QueryCacheResults>()); 634 std::unique_ptr<QueryCacheResults>());
620 return; 635 return;
621 } 636 }
622 637
623 if (!options.ignore_method && request && !request->method.empty() && 638 if (!options.ignore_method && request && !request->method.empty() &&
624 request->method != "GET") { 639 request->method != "GET") {
625 callback.Run(CACHE_STORAGE_OK, base::MakeUnique<QueryCacheResults>()); 640 std::move(callback).Run(CACHE_STORAGE_OK,
641 base::MakeUnique<QueryCacheResults>());
626 return; 642 return;
627 } 643 }
628 644
629 ServiceWorkerFetchRequest* request_ptr = request.get(); 645 ServiceWorkerFetchRequest* request_ptr = request.get();
630 std::unique_ptr<QueryCacheContext> query_cache_context( 646 std::unique_ptr<QueryCacheContext> query_cache_context(
631 new QueryCacheContext(std::move(request), options, callback)); 647 new QueryCacheContext(std::move(request), options, std::move(callback)));
632 query_cache_context->query_type = query_type; 648 query_cache_context->query_type = query_type;
633 649
634 if (query_cache_context->request && 650 if (query_cache_context->request &&
635 !query_cache_context->request->url.is_empty() && !options.ignore_search) { 651 !query_cache_context->request->url.is_empty() && !options.ignore_search) {
636 // There is no need to scan the entire backend, just open the exact 652 // There is no need to scan the entire backend, just open the exact
637 // URL. 653 // URL.
638 disk_cache::Entry** entry_ptr = &query_cache_context->enumerated_entry; 654 disk_cache::Entry** entry_ptr = &query_cache_context->enumerated_entry;
639 net::CompletionCallback open_entry_callback = 655 net::CompletionCallback open_entry_callback =
640 base::Bind(&CacheStorageCache::QueryCacheDidOpenFastPath, 656 base::AdaptCallbackForRepeating(
641 weak_ptr_factory_.GetWeakPtr(), 657 base::BindOnce(&CacheStorageCache::QueryCacheDidOpenFastPath,
642 base::Passed(std::move(query_cache_context))); 658 weak_ptr_factory_.GetWeakPtr(),
659 base::Passed(std::move(query_cache_context))));
643 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, 660 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr,
644 open_entry_callback); 661 open_entry_callback);
645 if (rv != net::ERR_IO_PENDING) 662 if (rv != net::ERR_IO_PENDING)
646 open_entry_callback.Run(rv); 663 open_entry_callback.Run(rv);
647 return; 664 return;
648 } 665 }
649 666
650 query_cache_context->backend_iterator = backend_->CreateIterator(); 667 query_cache_context->backend_iterator = backend_->CreateIterator();
651 QueryCacheOpenNextEntry(std::move(query_cache_context)); 668 QueryCacheOpenNextEntry(std::move(query_cache_context));
652 } 669 }
653 670
654 void CacheStorageCache::QueryCacheDidOpenFastPath( 671 void CacheStorageCache::QueryCacheDidOpenFastPath(
655 std::unique_ptr<QueryCacheContext> query_cache_context, 672 std::unique_ptr<QueryCacheContext> query_cache_context,
656 int rv) { 673 int rv) {
657 if (rv != net::OK) { 674 if (rv != net::OK) {
658 QueryCacheContext* results = query_cache_context.get(); 675 QueryCacheContext* results = query_cache_context.get();
659 results->callback.Run(CACHE_STORAGE_OK, 676 std::move(results->callback)
660 std::move(query_cache_context->matches)); 677 .Run(CACHE_STORAGE_OK, std::move(query_cache_context->matches));
661 return; 678 return;
662 } 679 }
663 QueryCacheFilterEntry(std::move(query_cache_context), rv); 680 QueryCacheFilterEntry(std::move(query_cache_context), rv);
664 } 681 }
665 682
666 void CacheStorageCache::QueryCacheOpenNextEntry( 683 void CacheStorageCache::QueryCacheOpenNextEntry(
667 std::unique_ptr<QueryCacheContext> query_cache_context) { 684 std::unique_ptr<QueryCacheContext> query_cache_context) {
668 DCHECK_EQ(nullptr, query_cache_context->enumerated_entry); 685 DCHECK_EQ(nullptr, query_cache_context->enumerated_entry);
669 686
670 if (!query_cache_context->backend_iterator) { 687 if (!query_cache_context->backend_iterator) {
671 // Iteration is complete. 688 // Iteration is complete.
672 std::sort(query_cache_context->matches->begin(), 689 std::sort(query_cache_context->matches->begin(),
673 query_cache_context->matches->end(), QueryCacheResultCompare); 690 query_cache_context->matches->end(), QueryCacheResultCompare);
674 691
675 query_cache_context->callback.Run(CACHE_STORAGE_OK, 692 std::move(query_cache_context->callback)
676 std::move(query_cache_context->matches)); 693 .Run(CACHE_STORAGE_OK, std::move(query_cache_context->matches));
677 return; 694 return;
678 } 695 }
679 696
680 disk_cache::Backend::Iterator& iterator = 697 disk_cache::Backend::Iterator& iterator =
681 *query_cache_context->backend_iterator; 698 *query_cache_context->backend_iterator;
682 disk_cache::Entry** enumerated_entry = &query_cache_context->enumerated_entry; 699 disk_cache::Entry** enumerated_entry = &query_cache_context->enumerated_entry;
683 net::CompletionCallback open_entry_callback = base::Bind( 700 net::CompletionCallback open_entry_callback = base::AdaptCallbackForRepeating(
684 &CacheStorageCache::QueryCacheFilterEntry, weak_ptr_factory_.GetWeakPtr(), 701 base::BindOnce(&CacheStorageCache::QueryCacheFilterEntry,
685 base::Passed(std::move(query_cache_context))); 702 weak_ptr_factory_.GetWeakPtr(),
703 base::Passed(std::move(query_cache_context))));
686 704
687 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); 705 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback);
688 706
689 if (rv != net::ERR_IO_PENDING) 707 if (rv != net::ERR_IO_PENDING)
690 open_entry_callback.Run(rv); 708 open_entry_callback.Run(rv);
691 } 709 }
692 710
693 void CacheStorageCache::QueryCacheFilterEntry( 711 void CacheStorageCache::QueryCacheFilterEntry(
694 std::unique_ptr<QueryCacheContext> query_cache_context, 712 std::unique_ptr<QueryCacheContext> query_cache_context,
695 int rv) { 713 int rv) {
696 if (rv == net::ERR_FAILED) { 714 if (rv == net::ERR_FAILED) {
697 // This is the indicator that iteration is complete. 715 // This is the indicator that iteration is complete.
698 query_cache_context->backend_iterator.reset(); 716 query_cache_context->backend_iterator.reset();
699 QueryCacheOpenNextEntry(std::move(query_cache_context)); 717 QueryCacheOpenNextEntry(std::move(query_cache_context));
700 return; 718 return;
701 } 719 }
702 720
703 if (rv < 0) { 721 if (rv < 0) {
704 QueryCacheCallback callback = query_cache_context->callback; 722 std::move(query_cache_context->callback)
705 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 723 .Run(CACHE_STORAGE_ERROR_STORAGE,
706 std::move(query_cache_context->matches)); 724 std::move(query_cache_context->matches));
707 return; 725 return;
708 } 726 }
709 727
710 disk_cache::ScopedEntryPtr entry(query_cache_context->enumerated_entry); 728 disk_cache::ScopedEntryPtr entry(query_cache_context->enumerated_entry);
711 query_cache_context->enumerated_entry = nullptr; 729 query_cache_context->enumerated_entry = nullptr;
712 730
713 if (backend_state_ != BACKEND_OPEN) { 731 if (backend_state_ != BACKEND_OPEN) {
714 QueryCacheCallback callback = query_cache_context->callback; 732 std::move(query_cache_context->callback)
715 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, 733 .Run(CACHE_STORAGE_ERROR_NOT_FOUND,
716 std::move(query_cache_context->matches)); 734 std::move(query_cache_context->matches));
717 return; 735 return;
718 } 736 }
719 737
720 if (query_cache_context->request && 738 if (query_cache_context->request &&
721 !query_cache_context->request->url.is_empty()) { 739 !query_cache_context->request->url.is_empty()) {
722 GURL requestURL = query_cache_context->request->url; 740 GURL requestURL = query_cache_context->request->url;
723 GURL cachedURL = GURL(entry->GetKey()); 741 GURL cachedURL = GURL(entry->GetKey());
724 742
725 if (query_cache_context->options.ignore_search) { 743 if (query_cache_context->options.ignore_search) {
726 requestURL = RemoveQueryParam(requestURL); 744 requestURL = RemoveQueryParam(requestURL);
727 cachedURL = RemoveQueryParam(cachedURL); 745 cachedURL = RemoveQueryParam(cachedURL);
728 } 746 }
729 747
730 if (cachedURL != requestURL) { 748 if (cachedURL != requestURL) {
731 QueryCacheOpenNextEntry(std::move(query_cache_context)); 749 QueryCacheOpenNextEntry(std::move(query_cache_context));
732 return; 750 return;
733 } 751 }
734 } 752 }
735 753
736 disk_cache::Entry* entry_ptr = entry.get(); 754 disk_cache::Entry* entry_ptr = entry.get();
737 ReadMetadata(entry_ptr, 755 ReadMetadata(entry_ptr,
738 base::Bind(&CacheStorageCache::QueryCacheDidReadMetadata, 756 base::BindOnce(&CacheStorageCache::QueryCacheDidReadMetadata,
739 weak_ptr_factory_.GetWeakPtr(), 757 weak_ptr_factory_.GetWeakPtr(),
740 base::Passed(std::move(query_cache_context)), 758 base::Passed(std::move(query_cache_context)),
741 base::Passed(std::move(entry)))); 759 base::Passed(std::move(entry))));
742 } 760 }
743 761
744 void CacheStorageCache::QueryCacheDidReadMetadata( 762 void CacheStorageCache::QueryCacheDidReadMetadata(
745 std::unique_ptr<QueryCacheContext> query_cache_context, 763 std::unique_ptr<QueryCacheContext> query_cache_context,
746 disk_cache::ScopedEntryPtr entry, 764 disk_cache::ScopedEntryPtr entry,
747 std::unique_ptr<proto::CacheMetadata> metadata) { 765 std::unique_ptr<proto::CacheMetadata> metadata) {
748 if (!metadata) { 766 if (!metadata) {
749 entry->Doom(); 767 entry->Doom();
750 QueryCacheOpenNextEntry(std::move(query_cache_context)); 768 QueryCacheOpenNextEntry(std::move(query_cache_context));
751 return; 769 return;
(...skipping 24 matching lines...) Expand all
776 match->request.reset(); 794 match->request.reset();
777 match->response.reset(); 795 match->response.reset();
778 match->entry = std::move(entry); 796 match->entry = std::move(entry);
779 QueryCacheOpenNextEntry(std::move(query_cache_context)); 797 QueryCacheOpenNextEntry(std::move(query_cache_context));
780 return; 798 return;
781 } 799 }
782 800
783 query_cache_context->estimated_out_bytes += 801 query_cache_context->estimated_out_bytes +=
784 match->request->EstimatedStructSize(); 802 match->request->EstimatedStructSize();
785 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) { 803 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
786 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, 804 std::move(query_cache_context->callback)
787 std::unique_ptr<QueryCacheResults>()); 805 .Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
806 std::unique_ptr<QueryCacheResults>());
788 return; 807 return;
789 } 808 }
790 809
791 if (query_cache_context->query_type == QueryCacheType::REQUESTS) { 810 if (query_cache_context->query_type == QueryCacheType::REQUESTS) {
792 match->response.reset(); 811 match->response.reset();
793 QueryCacheOpenNextEntry(std::move(query_cache_context)); 812 QueryCacheOpenNextEntry(std::move(query_cache_context));
794 return; 813 return;
795 } 814 }
796 815
797 DCHECK_EQ(QueryCacheType::REQUESTS_AND_RESPONSES, 816 DCHECK_EQ(QueryCacheType::REQUESTS_AND_RESPONSES,
798 query_cache_context->query_type); 817 query_cache_context->query_type);
799 818
800 query_cache_context->estimated_out_bytes += 819 query_cache_context->estimated_out_bytes +=
801 match->response->EstimatedStructSize(); 820 match->response->EstimatedStructSize();
802 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) { 821 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) {
803 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, 822 std::move(query_cache_context->callback)
804 std::unique_ptr<QueryCacheResults>()); 823 .Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE,
824 std::unique_ptr<QueryCacheResults>());
805 return; 825 return;
806 } 826 }
807 827
808 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { 828 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
809 QueryCacheOpenNextEntry(std::move(query_cache_context)); 829 QueryCacheOpenNextEntry(std::move(query_cache_context));
810 return; 830 return;
811 } 831 }
812 832
813 if (!blob_storage_context_) { 833 if (!blob_storage_context_) {
814 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE, 834 std::move(query_cache_context->callback)
815 base::MakeUnique<QueryCacheResults>()); 835 .Run(CACHE_STORAGE_ERROR_STORAGE,
836 base::MakeUnique<QueryCacheResults>());
816 return; 837 return;
817 } 838 }
818 839
819 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 840 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
820 PopulateResponseBody(std::move(entry), match->response.get()); 841 PopulateResponseBody(std::move(entry), match->response.get());
821 match->blob_handle = std::move(blob_data_handle); 842 match->blob_handle = std::move(blob_data_handle);
822 843
823 QueryCacheOpenNextEntry(std::move(query_cache_context)); 844 QueryCacheOpenNextEntry(std::move(query_cache_context));
824 } 845 }
825 846
826 // static 847 // static
827 bool CacheStorageCache::QueryCacheResultCompare(const QueryCacheResult& lhs, 848 bool CacheStorageCache::QueryCacheResultCompare(const QueryCacheResult& lhs,
828 const QueryCacheResult& rhs) { 849 const QueryCacheResult& rhs) {
829 return lhs.entry_time < rhs.entry_time; 850 return lhs.entry_time < rhs.entry_time;
830 } 851 }
831 852
832 void CacheStorageCache::MatchImpl( 853 void CacheStorageCache::MatchImpl(
833 std::unique_ptr<ServiceWorkerFetchRequest> request, 854 std::unique_ptr<ServiceWorkerFetchRequest> request,
834 const CacheStorageCacheQueryParams& match_params, 855 const CacheStorageCacheQueryParams& match_params,
835 const ResponseCallback& callback) { 856 ResponseCallback callback) {
836 MatchAllImpl(std::move(request), match_params, 857 MatchAllImpl(
837 base::Bind(&CacheStorageCache::MatchDidMatchAll, 858 std::move(request), match_params,
838 weak_ptr_factory_.GetWeakPtr(), callback)); 859 base::BindOnce(&CacheStorageCache::MatchDidMatchAll,
860 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
839 } 861 }
840 862
841 void CacheStorageCache::MatchDidMatchAll( 863 void CacheStorageCache::MatchDidMatchAll(
842 const ResponseCallback& callback, 864 ResponseCallback callback,
843 CacheStorageError match_all_error, 865 CacheStorageError match_all_error,
844 std::unique_ptr<Responses> match_all_responses, 866 std::unique_ptr<Responses> match_all_responses,
845 std::unique_ptr<BlobDataHandles> match_all_handles) { 867 std::unique_ptr<BlobDataHandles> match_all_handles) {
846 if (match_all_error != CACHE_STORAGE_OK) { 868 if (match_all_error != CACHE_STORAGE_OK) {
847 callback.Run(match_all_error, std::unique_ptr<ServiceWorkerResponse>(), 869 std::move(callback).Run(match_all_error,
848 std::unique_ptr<storage::BlobDataHandle>()); 870 std::unique_ptr<ServiceWorkerResponse>(),
871 std::unique_ptr<storage::BlobDataHandle>());
849 return; 872 return;
850 } 873 }
851 874
852 if (match_all_responses->empty()) { 875 if (match_all_responses->empty()) {
853 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, 876 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND,
854 std::unique_ptr<ServiceWorkerResponse>(), 877 std::unique_ptr<ServiceWorkerResponse>(),
855 std::unique_ptr<storage::BlobDataHandle>()); 878 std::unique_ptr<storage::BlobDataHandle>());
856 return; 879 return;
857 } 880 }
858 881
859 std::unique_ptr<ServiceWorkerResponse> response = 882 std::unique_ptr<ServiceWorkerResponse> response =
860 base::MakeUnique<ServiceWorkerResponse>(match_all_responses->at(0)); 883 base::MakeUnique<ServiceWorkerResponse>(match_all_responses->at(0));
861 884
862 callback.Run(CACHE_STORAGE_OK, std::move(response), 885 std::move(callback).Run(CACHE_STORAGE_OK, std::move(response),
863 std::move(match_all_handles->at(0))); 886 std::move(match_all_handles->at(0)));
864 } 887 }
865 888
866 void CacheStorageCache::MatchAllImpl( 889 void CacheStorageCache::MatchAllImpl(
867 std::unique_ptr<ServiceWorkerFetchRequest> request, 890 std::unique_ptr<ServiceWorkerFetchRequest> request,
868 const CacheStorageCacheQueryParams& options, 891 const CacheStorageCacheQueryParams& options,
869 const ResponsesCallback& callback) { 892 ResponsesCallback callback) {
870 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 893 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
871 if (backend_state_ != BACKEND_OPEN) { 894 if (backend_state_ != BACKEND_OPEN) {
872 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), 895 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
873 std::unique_ptr<BlobDataHandles>()); 896 std::unique_ptr<Responses>(),
897 std::unique_ptr<BlobDataHandles>());
874 return; 898 return;
875 } 899 }
876 900
877 QueryCache(std::move(request), options, 901 QueryCache(
878 QueryCacheType::REQUESTS_AND_RESPONSES, 902 std::move(request), options, QueryCacheType::REQUESTS_AND_RESPONSES,
879 base::Bind(&CacheStorageCache::MatchAllDidQueryCache, 903 base::BindOnce(&CacheStorageCache::MatchAllDidQueryCache,
880 weak_ptr_factory_.GetWeakPtr(), callback)); 904 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
881 } 905 }
882 906
883 void CacheStorageCache::MatchAllDidQueryCache( 907 void CacheStorageCache::MatchAllDidQueryCache(
884 const ResponsesCallback& callback, 908 ResponsesCallback callback,
885 CacheStorageError error, 909 CacheStorageError error,
886 std::unique_ptr<QueryCacheResults> query_cache_results) { 910 std::unique_ptr<QueryCacheResults> query_cache_results) {
887 if (error != CACHE_STORAGE_OK) { 911 if (error != CACHE_STORAGE_OK) {
888 callback.Run(error, std::unique_ptr<Responses>(), 912 std::move(callback).Run(error, std::unique_ptr<Responses>(),
889 std::unique_ptr<BlobDataHandles>()); 913 std::unique_ptr<BlobDataHandles>());
890 return; 914 return;
891 } 915 }
892 916
893 std::unique_ptr<Responses> out_responses = base::MakeUnique<Responses>(); 917 std::unique_ptr<Responses> out_responses = base::MakeUnique<Responses>();
894 std::unique_ptr<BlobDataHandles> out_handles = 918 std::unique_ptr<BlobDataHandles> out_handles =
895 base::MakeUnique<BlobDataHandles>(); 919 base::MakeUnique<BlobDataHandles>();
896 out_responses->reserve(query_cache_results->size()); 920 out_responses->reserve(query_cache_results->size());
897 out_handles->reserve(query_cache_results->size()); 921 out_handles->reserve(query_cache_results->size());
898 922
899 for (auto& result : *query_cache_results) { 923 for (auto& result : *query_cache_results) {
900 out_responses->push_back(*result.response); 924 out_responses->push_back(*result.response);
901 out_handles->push_back(std::move(result.blob_handle)); 925 out_handles->push_back(std::move(result.blob_handle));
902 } 926 }
903 927
904 callback.Run(CACHE_STORAGE_OK, std::move(out_responses), 928 std::move(callback).Run(CACHE_STORAGE_OK, std::move(out_responses),
905 std::move(out_handles)); 929 std::move(out_handles));
906 } 930 }
907 931
908 void CacheStorageCache::WriteSideDataDidGetQuota( 932 void CacheStorageCache::WriteSideDataDidGetQuota(
909 const ErrorCallback& callback, 933 ErrorCallback callback,
910 const GURL& url, 934 const GURL& url,
911 base::Time expected_response_time, 935 base::Time expected_response_time,
912 scoped_refptr<net::IOBuffer> buffer, 936 scoped_refptr<net::IOBuffer> buffer,
913 int buf_len, 937 int buf_len,
914 storage::QuotaStatusCode status_code, 938 storage::QuotaStatusCode status_code,
915 int64_t usage, 939 int64_t usage,
916 int64_t quota) { 940 int64_t quota) {
917 if (status_code != storage::kQuotaStatusOk || (buf_len > quota - usage)) { 941 if (status_code != storage::kQuotaStatusOk || (buf_len > quota - usage)) {
918 base::ThreadTaskRunnerHandle::Get()->PostTask( 942 base::ThreadTaskRunnerHandle::Get()->PostTask(
919 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_QUOTA_EXCEEDED)); 943 FROM_HERE, base::BindOnce(std::move(callback),
944 CACHE_STORAGE_ERROR_QUOTA_EXCEEDED));
920 return; 945 return;
921 } 946 }
922 947
923 scheduler_->ScheduleOperation(base::Bind( 948 scheduler_->ScheduleOperation(base::BindOnce(
924 &CacheStorageCache::WriteSideDataImpl, weak_ptr_factory_.GetWeakPtr(), 949 &CacheStorageCache::WriteSideDataImpl, weak_ptr_factory_.GetWeakPtr(),
925 scheduler_->WrapCallbackToRunNext(callback), url, expected_response_time, 950 scheduler_->WrapCallbackToRunNext(std::move(callback)), url,
926 buffer, buf_len)); 951 expected_response_time, buffer, buf_len));
927 } 952 }
928 953
929 void CacheStorageCache::WriteSideDataImpl(const ErrorCallback& callback, 954 void CacheStorageCache::WriteSideDataImpl(ErrorCallback callback,
930 const GURL& url, 955 const GURL& url,
931 base::Time expected_response_time, 956 base::Time expected_response_time,
932 scoped_refptr<net::IOBuffer> buffer, 957 scoped_refptr<net::IOBuffer> buffer,
933 int buf_len) { 958 int buf_len) {
934 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 959 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
935 if (backend_state_ != BACKEND_OPEN) { 960 if (backend_state_ != BACKEND_OPEN) {
936 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 961 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
937 return; 962 return;
938 } 963 }
939 964
940 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr( 965 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr(
941 new disk_cache::Entry*()); 966 new disk_cache::Entry*());
942 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); 967 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
943 net::CompletionCallback open_entry_callback = base::Bind( 968 net::CompletionCallback open_entry_callback = base::AdaptCallbackForRepeating(
944 &CacheStorageCache::WriteSideDataDidOpenEntry, 969 base::BindOnce(&CacheStorageCache::WriteSideDataDidOpenEntry,
945 weak_ptr_factory_.GetWeakPtr(), callback, expected_response_time, buffer, 970 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
946 buf_len, base::Passed(std::move(scoped_entry_ptr))); 971 expected_response_time, buffer, buf_len,
972 base::Passed(std::move(scoped_entry_ptr))));
947 973
948 int rv = backend_->OpenEntry(url.spec(), entry_ptr, open_entry_callback); 974 int rv = backend_->OpenEntry(url.spec(), entry_ptr, open_entry_callback);
949 if (rv != net::ERR_IO_PENDING) 975 if (rv != net::ERR_IO_PENDING)
950 open_entry_callback.Run(rv); 976 open_entry_callback.Run(rv);
951 } 977 }
952 978
953 void CacheStorageCache::WriteSideDataDidOpenEntry( 979 void CacheStorageCache::WriteSideDataDidOpenEntry(
954 const ErrorCallback& callback, 980 ErrorCallback callback,
955 base::Time expected_response_time, 981 base::Time expected_response_time,
956 scoped_refptr<net::IOBuffer> buffer, 982 scoped_refptr<net::IOBuffer> buffer,
957 int buf_len, 983 int buf_len,
958 std::unique_ptr<disk_cache::Entry*> entry_ptr, 984 std::unique_ptr<disk_cache::Entry*> entry_ptr,
959 int rv) { 985 int rv) {
960 if (rv != net::OK) { 986 if (rv != net::OK) {
961 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); 987 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND);
962 return; 988 return;
963 } 989 }
964 disk_cache::ScopedEntryPtr entry(*entry_ptr); 990 disk_cache::ScopedEntryPtr entry(*entry_ptr);
965 991
966 ReadMetadata(*entry_ptr, 992 ReadMetadata(*entry_ptr,
967 base::Bind(&CacheStorageCache::WriteSideDataDidReadMetaData, 993 base::BindOnce(&CacheStorageCache::WriteSideDataDidReadMetaData,
968 weak_ptr_factory_.GetWeakPtr(), callback, 994 weak_ptr_factory_.GetWeakPtr(),
969 expected_response_time, buffer, buf_len, 995 std::move(callback), expected_response_time,
970 base::Passed(std::move(entry)))); 996 buffer, buf_len, base::Passed(std::move(entry))));
971 } 997 }
972 998
973 void CacheStorageCache::WriteSideDataDidReadMetaData( 999 void CacheStorageCache::WriteSideDataDidReadMetaData(
974 const ErrorCallback& callback, 1000 ErrorCallback callback,
975 base::Time expected_response_time, 1001 base::Time expected_response_time,
976 scoped_refptr<net::IOBuffer> buffer, 1002 scoped_refptr<net::IOBuffer> buffer,
977 int buf_len, 1003 int buf_len,
978 disk_cache::ScopedEntryPtr entry, 1004 disk_cache::ScopedEntryPtr entry,
979 std::unique_ptr<proto::CacheMetadata> headers) { 1005 std::unique_ptr<proto::CacheMetadata> headers) {
980 if (!headers || 1006 if (!headers ||
981 headers->response().response_time() != 1007 headers->response().response_time() !=
982 expected_response_time.ToInternalValue()) { 1008 expected_response_time.ToInternalValue()) {
983 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); 1009 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND);
984 return; 1010 return;
985 } 1011 }
986 // Get a temporary copy of the entry pointer before passing it in base::Bind. 1012 // Get a temporary copy of the entry pointer before passing it in base::Bind.
987 disk_cache::Entry* temp_entry_ptr = entry.get(); 1013 disk_cache::Entry* temp_entry_ptr = entry.get();
988 1014
989 net::CompletionCallback write_side_data_callback = base::Bind( 1015 net::CompletionCallback write_side_data_callback =
990 &CacheStorageCache::WriteSideDataDidWrite, weak_ptr_factory_.GetWeakPtr(), 1016 base::AdaptCallbackForRepeating(
991 callback, base::Passed(std::move(entry)), buf_len); 1017 base::BindOnce(&CacheStorageCache::WriteSideDataDidWrite,
1018 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
1019 base::Passed(std::move(entry)), buf_len));
992 1020
993 int rv = temp_entry_ptr->WriteData( 1021 int rv = temp_entry_ptr->WriteData(
994 INDEX_SIDE_DATA, 0 /* offset */, buffer.get(), buf_len, 1022 INDEX_SIDE_DATA, 0 /* offset */, buffer.get(), buf_len,
995 write_side_data_callback, true /* truncate */); 1023 write_side_data_callback, true /* truncate */);
996 1024
997 if (rv != net::ERR_IO_PENDING) 1025 if (rv != net::ERR_IO_PENDING)
998 write_side_data_callback.Run(rv); 1026 write_side_data_callback.Run(rv);
999 } 1027 }
1000 1028
1001 void CacheStorageCache::WriteSideDataDidWrite(const ErrorCallback& callback, 1029 void CacheStorageCache::WriteSideDataDidWrite(ErrorCallback callback,
1002 disk_cache::ScopedEntryPtr entry, 1030 disk_cache::ScopedEntryPtr entry,
1003 int expected_bytes, 1031 int expected_bytes,
1004 int rv) { 1032 int rv) {
1005 if (rv != expected_bytes) { 1033 if (rv != expected_bytes) {
1006 entry->Doom(); 1034 entry->Doom();
1007 UpdateCacheSize(base::Bind(callback, CACHE_STORAGE_ERROR_NOT_FOUND)); 1035 UpdateCacheSize(
1036 base::BindOnce(std::move(callback), CACHE_STORAGE_ERROR_NOT_FOUND));
1008 return; 1037 return;
1009 } 1038 }
1010 1039
1011 if (rv > 0) 1040 if (rv > 0)
1012 storage::RecordBytesWritten(kRecordBytesLabel, rv); 1041 storage::RecordBytesWritten(kRecordBytesLabel, rv);
1013 1042
1014 UpdateCacheSize(base::Bind(callback, CACHE_STORAGE_OK)); 1043 UpdateCacheSize(base::BindOnce(std::move(callback), CACHE_STORAGE_OK));
1015 } 1044 }
1016 1045
1017 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, 1046 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation,
1018 const ErrorCallback& callback) { 1047 ErrorCallback callback) {
1019 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 1048 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
1020 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); 1049 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type);
1021 1050
1022 std::unique_ptr<ServiceWorkerFetchRequest> request( 1051 std::unique_ptr<ServiceWorkerFetchRequest> request(
1023 new ServiceWorkerFetchRequest( 1052 new ServiceWorkerFetchRequest(
1024 operation.request.url, operation.request.method, 1053 operation.request.url, operation.request.method,
1025 operation.request.headers, operation.request.referrer, 1054 operation.request.headers, operation.request.referrer,
1026 operation.request.is_reload)); 1055 operation.request.is_reload));
1027 1056
1028 std::unique_ptr<ServiceWorkerResponse> response = 1057 std::unique_ptr<ServiceWorkerResponse> response =
1029 base::MakeUnique<ServiceWorkerResponse>(operation.response); 1058 base::MakeUnique<ServiceWorkerResponse>(operation.response);
1030 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; 1059 std::unique_ptr<storage::BlobDataHandle> blob_data_handle;
1031 1060
1032 if (!response->blob_uuid.empty()) { 1061 if (!response->blob_uuid.empty()) {
1033 if (!blob_storage_context_) { 1062 if (!blob_storage_context_) {
1034 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1063 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1035 return; 1064 return;
1036 } 1065 }
1037 blob_data_handle = 1066 blob_data_handle =
1038 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid); 1067 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid);
1039 if (!blob_data_handle) { 1068 if (!blob_data_handle) {
1040 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1069 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1041 return; 1070 return;
1042 } 1071 }
1043 } 1072 }
1044 1073
1045 UMA_HISTOGRAM_ENUMERATION( 1074 UMA_HISTOGRAM_ENUMERATION(
1046 "ServiceWorkerCache.Cache.AllWritesResponseType", 1075 "ServiceWorkerCache.Cache.AllWritesResponseType",
1047 operation.response.response_type, 1076 operation.response.response_type,
1048 blink::WebServiceWorkerResponseType::kWebServiceWorkerResponseTypeLast + 1077 blink::WebServiceWorkerResponseType::kWebServiceWorkerResponseTypeLast +
1049 1); 1078 1);
1050 1079
1051 std::unique_ptr<PutContext> put_context(new PutContext( 1080 std::unique_ptr<PutContext> put_context(new PutContext(
1052 std::move(request), std::move(response), std::move(blob_data_handle), 1081 std::move(request), std::move(response), std::move(blob_data_handle),
1053 scheduler_->WrapCallbackToRunNext(callback))); 1082 scheduler_->WrapCallbackToRunNext(std::move(callback))));
1054 1083
1055 scheduler_->ScheduleOperation( 1084 scheduler_->ScheduleOperation(base::BindOnce(
1056 base::Bind(&CacheStorageCache::PutImpl, weak_ptr_factory_.GetWeakPtr(), 1085 &CacheStorageCache::PutImpl, weak_ptr_factory_.GetWeakPtr(),
1057 base::Passed(std::move(put_context)))); 1086 base::Passed(std::move(put_context))));
1058 } 1087 }
1059 1088
1060 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) { 1089 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) {
1061 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1090 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1062 if (backend_state_ != BACKEND_OPEN) { 1091 if (backend_state_ != BACKEND_OPEN) {
1063 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1092 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1064 return; 1093 return;
1065 } 1094 }
1066 1095
1067 std::string key = put_context->request->url.spec(); 1096 std::string key = put_context->request->url.spec();
1068 1097
1069 net::CompletionCallback callback = base::Bind( 1098 net::CompletionCallback callback =
1070 &CacheStorageCache::PutDidDoomEntry, weak_ptr_factory_.GetWeakPtr(), 1099 base::AdaptCallbackForRepeating(base::BindOnce(
1071 base::Passed(std::move(put_context))); 1100 &CacheStorageCache::PutDidDoomEntry, weak_ptr_factory_.GetWeakPtr(),
1101 base::Passed(std::move(put_context))));
1072 1102
1073 int rv = backend_->DoomEntry(key, callback); 1103 int rv = backend_->DoomEntry(key, callback);
1074 if (rv != net::ERR_IO_PENDING) 1104 if (rv != net::ERR_IO_PENDING)
1075 callback.Run(rv); 1105 callback.Run(rv);
1076 } 1106 }
1077 1107
1078 void CacheStorageCache::PutDidDoomEntry(std::unique_ptr<PutContext> put_context, 1108 void CacheStorageCache::PutDidDoomEntry(std::unique_ptr<PutContext> put_context,
1079 int rv) { 1109 int rv) {
1080 if (backend_state_ != BACKEND_OPEN) { 1110 if (backend_state_ != BACKEND_OPEN) {
1081 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1111 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1082 return; 1112 return;
1083 } 1113 }
1084 1114
1085 // |rv| is ignored as doom entry can fail if the entry doesn't exist. 1115 // |rv| is ignored as doom entry can fail if the entry doesn't exist.
1086 1116
1087 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr( 1117 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr(
1088 new disk_cache::Entry*()); 1118 new disk_cache::Entry*());
1089 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); 1119 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
1090 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); 1120 ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
1091 disk_cache::Backend* backend_ptr = backend_.get(); 1121 disk_cache::Backend* backend_ptr = backend_.get();
1092 1122
1093 net::CompletionCallback create_entry_callback = base::Bind( 1123 net::CompletionCallback create_entry_callback =
1094 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), 1124 base::AdaptCallbackForRepeating(base::BindOnce(
1095 base::Passed(std::move(scoped_entry_ptr)), 1125 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(),
1096 base::Passed(std::move(put_context))); 1126 base::Passed(std::move(scoped_entry_ptr)),
1127 base::Passed(std::move(put_context))));
1097 1128
1098 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, 1129 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr,
1099 create_entry_callback); 1130 create_entry_callback);
1100 1131
1101 if (create_rv != net::ERR_IO_PENDING) 1132 if (create_rv != net::ERR_IO_PENDING)
1102 create_entry_callback.Run(create_rv); 1133 create_entry_callback.Run(create_rv);
1103 } 1134 }
1104 1135
1105 void CacheStorageCache::PutDidCreateEntry( 1136 void CacheStorageCache::PutDidCreateEntry(
1106 std::unique_ptr<disk_cache::Entry*> entry_ptr, 1137 std::unique_ptr<disk_cache::Entry*> entry_ptr,
1107 std::unique_ptr<PutContext> put_context, 1138 std::unique_ptr<PutContext> put_context,
1108 int rv) { 1139 int rv) {
1109 put_context->cache_entry.reset(*entry_ptr); 1140 put_context->cache_entry.reset(*entry_ptr);
1110 1141
1111 if (rv != net::OK) { 1142 if (rv != net::OK) {
1112 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); 1143 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_EXISTS);
1113 return; 1144 return;
1114 } 1145 }
1115 1146
1116 proto::CacheMetadata metadata; 1147 proto::CacheMetadata metadata;
1117 metadata.set_entry_time(base::Time::Now().ToInternalValue()); 1148 metadata.set_entry_time(base::Time::Now().ToInternalValue());
1118 proto::CacheRequest* request_metadata = metadata.mutable_request(); 1149 proto::CacheRequest* request_metadata = metadata.mutable_request();
1119 request_metadata->set_method(put_context->request->method); 1150 request_metadata->set_method(put_context->request->method);
1120 for (ServiceWorkerHeaderMap::const_iterator it = 1151 for (ServiceWorkerHeaderMap::const_iterator it =
1121 put_context->request->headers.begin(); 1152 put_context->request->headers.begin();
1122 it != put_context->request->headers.end(); ++it) { 1153 it != put_context->request->headers.end(); ++it) {
(...skipping 20 matching lines...) Expand all
1143 DCHECK_EQ(std::string::npos, it->second.find('\0')); 1174 DCHECK_EQ(std::string::npos, it->second.find('\0'));
1144 proto::CacheHeaderMap* header_map = response_metadata->add_headers(); 1175 proto::CacheHeaderMap* header_map = response_metadata->add_headers();
1145 header_map->set_name(it->first); 1176 header_map->set_name(it->first);
1146 header_map->set_value(it->second); 1177 header_map->set_value(it->second);
1147 } 1178 }
1148 for (const auto& header : put_context->response->cors_exposed_header_names) 1179 for (const auto& header : put_context->response->cors_exposed_header_names)
1149 response_metadata->add_cors_exposed_header_names(header); 1180 response_metadata->add_cors_exposed_header_names(header);
1150 1181
1151 std::unique_ptr<std::string> serialized(new std::string()); 1182 std::unique_ptr<std::string> serialized(new std::string());
1152 if (!metadata.SerializeToString(serialized.get())) { 1183 if (!metadata.SerializeToString(serialized.get())) {
1153 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1184 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1154 return; 1185 return;
1155 } 1186 }
1156 1187
1157 scoped_refptr<net::StringIOBuffer> buffer( 1188 scoped_refptr<net::StringIOBuffer> buffer(
1158 new net::StringIOBuffer(std::move(serialized))); 1189 new net::StringIOBuffer(std::move(serialized)));
1159 1190
1160 // Get a temporary copy of the entry pointer before passing it in base::Bind. 1191 // 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(); 1192 disk_cache::Entry* temp_entry_ptr = put_context->cache_entry.get();
1162 1193
1163 net::CompletionCallback write_headers_callback = base::Bind( 1194 net::CompletionCallback write_headers_callback =
1164 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), 1195 base::AdaptCallbackForRepeating(
1165 base::Passed(std::move(put_context)), buffer->size()); 1196 base::BindOnce(&CacheStorageCache::PutDidWriteHeaders,
1197 weak_ptr_factory_.GetWeakPtr(),
1198 base::Passed(std::move(put_context)), buffer->size()));
1166 1199
1167 rv = temp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), 1200 rv = temp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(),
1168 buffer->size(), write_headers_callback, 1201 buffer->size(), write_headers_callback,
1169 true /* truncate */); 1202 true /* truncate */);
1170 1203
1171 if (rv != net::ERR_IO_PENDING) 1204 if (rv != net::ERR_IO_PENDING)
1172 write_headers_callback.Run(rv); 1205 write_headers_callback.Run(rv);
1173 } 1206 }
1174 1207
1175 void CacheStorageCache::PutDidWriteHeaders( 1208 void CacheStorageCache::PutDidWriteHeaders(
1176 std::unique_ptr<PutContext> put_context, 1209 std::unique_ptr<PutContext> put_context,
1177 int expected_bytes, 1210 int expected_bytes,
1178 int rv) { 1211 int rv) {
1179 if (rv != expected_bytes) { 1212 if (rv != expected_bytes) {
1180 put_context->cache_entry->Doom(); 1213 put_context->cache_entry->Doom();
1181 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1214 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1182 return; 1215 return;
1183 } 1216 }
1184 1217
1185 if (rv > 0) 1218 if (rv > 0)
1186 storage::RecordBytesWritten(kRecordBytesLabel, rv); 1219 storage::RecordBytesWritten(kRecordBytesLabel, rv);
1187 1220
1188 // The metadata is written, now for the response content. The data is streamed 1221 // The metadata is written, now for the response content. The data is streamed
1189 // from the blob into the cache entry. 1222 // from the blob into the cache entry.
1190 1223
1191 if (put_context->response->blob_uuid.empty()) { 1224 if (put_context->response->blob_uuid.empty()) {
1192 UpdateCacheSize(base::Bind(put_context->callback, CACHE_STORAGE_OK)); 1225 UpdateCacheSize(
1226 base::BindOnce(std::move(put_context->callback), CACHE_STORAGE_OK));
1193 return; 1227 return;
1194 } 1228 }
1195 1229
1196 DCHECK(put_context->blob_data_handle); 1230 DCHECK(put_context->blob_data_handle);
1197 1231
1198 disk_cache::ScopedEntryPtr entry(std::move(put_context->cache_entry)); 1232 disk_cache::ScopedEntryPtr entry(std::move(put_context->cache_entry));
1199 put_context->cache_entry = NULL; 1233 put_context->cache_entry = NULL;
1200 1234
1201 auto blob_to_cache = base::MakeUnique<CacheStorageBlobToDiskCache>(); 1235 auto blob_to_cache = base::MakeUnique<CacheStorageBlobToDiskCache>();
1202 CacheStorageBlobToDiskCache* blob_to_cache_raw = blob_to_cache.get(); 1236 CacheStorageBlobToDiskCache* blob_to_cache_raw = blob_to_cache.get();
1203 BlobToDiskCacheIDMap::KeyType blob_to_cache_key = 1237 BlobToDiskCacheIDMap::KeyType blob_to_cache_key =
1204 active_blob_to_disk_cache_writers_.Add(std::move(blob_to_cache)); 1238 active_blob_to_disk_cache_writers_.Add(std::move(blob_to_cache));
1205 1239
1206 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 1240 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
1207 std::move(put_context->blob_data_handle); 1241 std::move(put_context->blob_data_handle);
1208 1242
1209 blob_to_cache_raw->StreamBlobToCache( 1243 blob_to_cache_raw->StreamBlobToCache(
1210 std::move(entry), INDEX_RESPONSE_BODY, request_context_getter_.get(), 1244 std::move(entry), INDEX_RESPONSE_BODY, request_context_getter_.get(),
1211 std::move(blob_data_handle), 1245 std::move(blob_data_handle),
1212 base::Bind(&CacheStorageCache::PutDidWriteBlobToCache, 1246 base::BindOnce(&CacheStorageCache::PutDidWriteBlobToCache,
1213 weak_ptr_factory_.GetWeakPtr(), 1247 weak_ptr_factory_.GetWeakPtr(),
1214 base::Passed(std::move(put_context)), blob_to_cache_key)); 1248 base::Passed(std::move(put_context)), blob_to_cache_key));
1215 } 1249 }
1216 1250
1217 void CacheStorageCache::PutDidWriteBlobToCache( 1251 void CacheStorageCache::PutDidWriteBlobToCache(
1218 std::unique_ptr<PutContext> put_context, 1252 std::unique_ptr<PutContext> put_context,
1219 BlobToDiskCacheIDMap::KeyType blob_to_cache_key, 1253 BlobToDiskCacheIDMap::KeyType blob_to_cache_key,
1220 disk_cache::ScopedEntryPtr entry, 1254 disk_cache::ScopedEntryPtr entry,
1221 bool success) { 1255 bool success) {
1222 DCHECK(entry); 1256 DCHECK(entry);
1223 put_context->cache_entry = std::move(entry); 1257 put_context->cache_entry = std::move(entry);
1224 1258
1225 active_blob_to_disk_cache_writers_.Remove(blob_to_cache_key); 1259 active_blob_to_disk_cache_writers_.Remove(blob_to_cache_key);
1226 1260
1227 if (!success) { 1261 if (!success) {
1228 put_context->cache_entry->Doom(); 1262 put_context->cache_entry->Doom();
1229 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1263 std::move(put_context->callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1230 return; 1264 return;
1231 } 1265 }
1232 1266
1233 UpdateCacheSize(base::Bind(put_context->callback, CACHE_STORAGE_OK)); 1267 UpdateCacheSize(
1268 base::BindOnce(std::move(put_context->callback), CACHE_STORAGE_OK));
1234 } 1269 }
1235 1270
1236 void CacheStorageCache::UpdateCacheSize(const base::Closure& callback) { 1271 void CacheStorageCache::UpdateCacheSize(base::OnceClosure callback) {
1237 if (backend_state_ != BACKEND_OPEN) 1272 if (backend_state_ != BACKEND_OPEN)
1238 return; 1273 return;
1239 1274
1240 // Note that the callback holds a cache handle to keep the cache alive during 1275 // 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 1276 // the operation since this UpdateCacheSize is often run after an operation
1242 // completes and runs its callback. 1277 // completes and runs its callback.
1243 int rv = backend_->CalculateSizeOfAllEntries( 1278 net::CompletionCallback size_callback = base::AdaptCallbackForRepeating(
1244 base::Bind(&CacheStorageCache::UpdateCacheSizeGotSize, 1279 base::BindOnce(&CacheStorageCache::UpdateCacheSizeGotSize,
1245 weak_ptr_factory_.GetWeakPtr(), 1280 weak_ptr_factory_.GetWeakPtr(),
1246 base::Passed(CreateCacheHandle()), callback)); 1281 base::Passed(CreateCacheHandle()), std::move(callback)));
1247 1282
1283 int rv = backend_->CalculateSizeOfAllEntries(size_callback);
1248 if (rv != net::ERR_IO_PENDING) 1284 if (rv != net::ERR_IO_PENDING)
1249 UpdateCacheSizeGotSize(CreateCacheHandle(), callback, rv); 1285 std::move(size_callback).Run(rv);
1250 } 1286 }
1251 1287
1252 void CacheStorageCache::UpdateCacheSizeGotSize( 1288 void CacheStorageCache::UpdateCacheSizeGotSize(
1253 std::unique_ptr<CacheStorageCacheHandle> cache_handle, 1289 std::unique_ptr<CacheStorageCacheHandle> cache_handle,
1254 const base::Closure& callback, 1290 base::OnceClosure callback,
1255 int current_cache_size) { 1291 int current_cache_size) {
1256 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown); 1292 DCHECK_NE(current_cache_size, CacheStorage::kSizeUnknown);
1257 int64_t old_cache_size = cache_size_; 1293 int64_t old_cache_size = cache_size_;
1258 cache_size_ = current_cache_size; 1294 cache_size_ = current_cache_size;
1259 1295
1260 int64_t size_delta = current_cache_size - old_cache_size; 1296 int64_t size_delta = current_cache_size - old_cache_size;
1261 1297
1262 quota_manager_proxy_->NotifyStorageModified( 1298 quota_manager_proxy_->NotifyStorageModified(
1263 storage::QuotaClient::kServiceWorkerCache, origin_, 1299 storage::QuotaClient::kServiceWorkerCache, origin_,
1264 storage::kStorageTypeTemporary, size_delta); 1300 storage::kStorageTypeTemporary, size_delta);
1265 1301
1266 if (cache_observer_) 1302 if (cache_observer_)
1267 cache_observer_->CacheSizeUpdated(this, current_cache_size); 1303 cache_observer_->CacheSizeUpdated(this, current_cache_size);
1268 1304
1269 callback.Run(); 1305 std::move(callback).Run();
1270 } 1306 }
1271 1307
1272 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation, 1308 void CacheStorageCache::Delete(const CacheStorageBatchOperation& operation,
1273 const ErrorCallback& callback) { 1309 ErrorCallback callback) {
1274 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 1310 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
1275 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE, 1311 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE,
1276 operation.operation_type); 1312 operation.operation_type);
1277 1313
1278 std::unique_ptr<ServiceWorkerFetchRequest> request( 1314 std::unique_ptr<ServiceWorkerFetchRequest> request(
1279 new ServiceWorkerFetchRequest( 1315 new ServiceWorkerFetchRequest(
1280 operation.request.url, operation.request.method, 1316 operation.request.url, operation.request.method,
1281 operation.request.headers, operation.request.referrer, 1317 operation.request.headers, operation.request.referrer,
1282 operation.request.is_reload)); 1318 operation.request.is_reload));
1283 1319
1284 scheduler_->ScheduleOperation( 1320 scheduler_->ScheduleOperation(base::BindOnce(
1285 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(), 1321 &CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(),
1286 base::Passed(std::move(request)), operation.match_params, 1322 base::Passed(std::move(request)), operation.match_params,
1287 scheduler_->WrapCallbackToRunNext(callback))); 1323 scheduler_->WrapCallbackToRunNext(std::move(callback))));
1288 } 1324 }
1289 1325
1290 void CacheStorageCache::DeleteImpl( 1326 void CacheStorageCache::DeleteImpl(
1291 std::unique_ptr<ServiceWorkerFetchRequest> request, 1327 std::unique_ptr<ServiceWorkerFetchRequest> request,
1292 const CacheStorageCacheQueryParams& match_params, 1328 const CacheStorageCacheQueryParams& match_params,
1293 const ErrorCallback& callback) { 1329 ErrorCallback callback) {
1294 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1330 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1295 if (backend_state_ != BACKEND_OPEN) { 1331 if (backend_state_ != BACKEND_OPEN) {
1296 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1332 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1297 return; 1333 return;
1298 } 1334 }
1299 1335
1300 QueryCache(std::move(request), match_params, QueryCacheType::CACHE_ENTRIES, 1336 QueryCache(
1301 base::Bind(&CacheStorageCache::DeleteDidQueryCache, 1337 std::move(request), match_params, QueryCacheType::CACHE_ENTRIES,
1302 weak_ptr_factory_.GetWeakPtr(), callback)); 1338 base::BindOnce(&CacheStorageCache::DeleteDidQueryCache,
1339 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
1303 } 1340 }
1304 1341
1305 void CacheStorageCache::DeleteDidQueryCache( 1342 void CacheStorageCache::DeleteDidQueryCache(
1306 const ErrorCallback& callback, 1343 ErrorCallback callback,
1307 CacheStorageError error, 1344 CacheStorageError error,
1308 std::unique_ptr<QueryCacheResults> query_cache_results) { 1345 std::unique_ptr<QueryCacheResults> query_cache_results) {
1309 if (error != CACHE_STORAGE_OK) { 1346 if (error != CACHE_STORAGE_OK) {
1310 callback.Run(error); 1347 std::move(callback).Run(error);
1311 return; 1348 return;
1312 } 1349 }
1313 1350
1314 if (query_cache_results->empty()) { 1351 if (query_cache_results->empty()) {
1315 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); 1352 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND);
1316 return; 1353 return;
1317 } 1354 }
1318 1355
1319 for (auto& result : *query_cache_results) { 1356 for (auto& result : *query_cache_results) {
1320 disk_cache::ScopedEntryPtr entry = std::move(result.entry); 1357 disk_cache::ScopedEntryPtr entry = std::move(result.entry);
1321 entry->Doom(); 1358 entry->Doom();
1322 } 1359 }
1323 1360
1324 UpdateCacheSize(base::Bind(callback, CACHE_STORAGE_OK)); 1361 UpdateCacheSize(base::BindOnce(std::move(callback), CACHE_STORAGE_OK));
1325 } 1362 }
1326 1363
1327 void CacheStorageCache::KeysImpl( 1364 void CacheStorageCache::KeysImpl(
1328 std::unique_ptr<ServiceWorkerFetchRequest> request, 1365 std::unique_ptr<ServiceWorkerFetchRequest> request,
1329 const CacheStorageCacheQueryParams& options, 1366 const CacheStorageCacheQueryParams& options,
1330 const RequestsCallback& callback) { 1367 RequestsCallback callback) {
1331 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1368 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1332 if (backend_state_ != BACKEND_OPEN) { 1369 if (backend_state_ != BACKEND_OPEN) {
1333 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>()); 1370 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE,
1371 std::unique_ptr<Requests>());
1334 return; 1372 return;
1335 } 1373 }
1336 1374
1337 QueryCache(std::move(request), options, QueryCacheType::REQUESTS, 1375 QueryCache(
1338 base::Bind(&CacheStorageCache::KeysDidQueryCache, 1376 std::move(request), options, QueryCacheType::REQUESTS,
1339 weak_ptr_factory_.GetWeakPtr(), callback)); 1377 base::BindOnce(&CacheStorageCache::KeysDidQueryCache,
1378 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
1340 } 1379 }
1341 1380
1342 void CacheStorageCache::KeysDidQueryCache( 1381 void CacheStorageCache::KeysDidQueryCache(
1343 const RequestsCallback& callback, 1382 RequestsCallback callback,
1344 CacheStorageError error, 1383 CacheStorageError error,
1345 std::unique_ptr<QueryCacheResults> query_cache_results) { 1384 std::unique_ptr<QueryCacheResults> query_cache_results) {
1346 if (error != CACHE_STORAGE_OK) { 1385 if (error != CACHE_STORAGE_OK) {
1347 callback.Run(error, std::unique_ptr<Requests>()); 1386 std::move(callback).Run(error, std::unique_ptr<Requests>());
1348 return; 1387 return;
1349 } 1388 }
1350 1389
1351 std::unique_ptr<Requests> out_requests = base::MakeUnique<Requests>(); 1390 std::unique_ptr<Requests> out_requests = base::MakeUnique<Requests>();
1352 out_requests->reserve(query_cache_results->size()); 1391 out_requests->reserve(query_cache_results->size());
1353 for (const auto& result : *query_cache_results) 1392 for (const auto& result : *query_cache_results)
1354 out_requests->push_back(*result.request); 1393 out_requests->push_back(*result.request);
1355 1394
1356 callback.Run(CACHE_STORAGE_OK, std::move(out_requests)); 1395 std::move(callback).Run(CACHE_STORAGE_OK, std::move(out_requests));
1357 } 1396 }
1358 1397
1359 void CacheStorageCache::CloseImpl(const base::Closure& callback) { 1398 void CacheStorageCache::CloseImpl(base::OnceClosure callback) {
1360 DCHECK_NE(BACKEND_CLOSED, backend_state_); 1399 DCHECK_NE(BACKEND_CLOSED, backend_state_);
1361 1400
1362 backend_state_ = BACKEND_CLOSED; 1401 backend_state_ = BACKEND_CLOSED;
1363 backend_.reset(); 1402 backend_.reset();
1364 callback.Run(); 1403 std::move(callback).Run();
1365 } 1404 }
1366 1405
1367 void CacheStorageCache::SizeImpl(const SizeCallback& callback) { 1406 void CacheStorageCache::SizeImpl(SizeCallback callback) {
1368 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1407 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1369 1408
1370 int64_t size = backend_state_ == BACKEND_OPEN ? cache_size_ : 0; 1409 int64_t size = backend_state_ == BACKEND_OPEN ? cache_size_ : 0;
1371 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1410 base::ThreadTaskRunnerHandle::Get()->PostTask(
1372 base::Bind(callback, size)); 1411 FROM_HERE, base::BindOnce(std::move(callback), size));
1373 } 1412 }
1374 1413
1375 void CacheStorageCache::GetSizeThenCloseDidGetSize(const SizeCallback& callback, 1414 void CacheStorageCache::GetSizeThenCloseDidGetSize(SizeCallback callback,
1376 int64_t cache_size) { 1415 int64_t cache_size) {
1377 CloseImpl(base::Bind(callback, cache_size)); 1416 CloseImpl(base::BindOnce(std::move(callback), cache_size));
1378 } 1417 }
1379 1418
1380 void CacheStorageCache::CreateBackend(const ErrorCallback& callback) { 1419 void CacheStorageCache::CreateBackend(ErrorCallback callback) {
1381 DCHECK(!backend_); 1420 DCHECK(!backend_);
1382 1421
1383 // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction. 1422 // 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; 1423 net::CacheType cache_type = memory_only_ ? net::MEMORY_CACHE : net::APP_CACHE;
1385 1424
1386 std::unique_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr()); 1425 std::unique_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr());
1387 1426
1388 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below. 1427 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below.
1389 ScopedBackendPtr* backend = backend_ptr.get(); 1428 ScopedBackendPtr* backend = backend_ptr.get();
1390 1429
1391 net::CompletionCallback create_cache_callback = 1430 net::CompletionCallback create_cache_callback =
1392 base::Bind(&CacheStorageCache::CreateBackendDidCreate, 1431 base::AdaptCallbackForRepeating(
1393 weak_ptr_factory_.GetWeakPtr(), callback, 1432 base::BindOnce(&CacheStorageCache::CreateBackendDidCreate,
1394 base::Passed(std::move(backend_ptr))); 1433 weak_ptr_factory_.GetWeakPtr(), std::move(callback),
1434 base::Passed(std::move(backend_ptr))));
1395 1435
1396 // TODO(jkarlin): Use the cache task runner that ServiceWorkerCacheCore 1436 // TODO(jkarlin): Use the cache task runner that ServiceWorkerCacheCore
1397 // has for disk caches. 1437 // has for disk caches.
1398 int rv = disk_cache::CreateCacheBackend( 1438 int rv = disk_cache::CreateCacheBackend(
1399 cache_type, net::CACHE_BACKEND_SIMPLE, path_, kMaxCacheBytes, 1439 cache_type, net::CACHE_BACKEND_SIMPLE, path_, kMaxCacheBytes,
1400 false, /* force */ 1440 false, /* force */
1401 BrowserThread::GetTaskRunnerForThread(BrowserThread::CACHE).get(), NULL, 1441 BrowserThread::GetTaskRunnerForThread(BrowserThread::CACHE).get(), NULL,
1402 backend, create_cache_callback); 1442 backend, create_cache_callback);
1403 if (rv != net::ERR_IO_PENDING) 1443 if (rv != net::ERR_IO_PENDING)
1404 create_cache_callback.Run(rv); 1444 create_cache_callback.Run(rv);
1405 } 1445 }
1406 1446
1407 void CacheStorageCache::CreateBackendDidCreate( 1447 void CacheStorageCache::CreateBackendDidCreate(
1408 const CacheStorageCache::ErrorCallback& callback, 1448 CacheStorageCache::ErrorCallback callback,
1409 std::unique_ptr<ScopedBackendPtr> backend_ptr, 1449 std::unique_ptr<ScopedBackendPtr> backend_ptr,
1410 int rv) { 1450 int rv) {
1411 if (rv != net::OK) { 1451 if (rv != net::OK) {
1412 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1452 std::move(callback).Run(CACHE_STORAGE_ERROR_STORAGE);
1413 return; 1453 return;
1414 } 1454 }
1415 1455
1416 backend_ = std::move(*backend_ptr); 1456 backend_ = std::move(*backend_ptr);
1417 callback.Run(CACHE_STORAGE_OK); 1457 std::move(callback).Run(CACHE_STORAGE_OK);
1418 } 1458 }
1419 1459
1420 void CacheStorageCache::InitBackend() { 1460 void CacheStorageCache::InitBackend() {
1421 DCHECK_EQ(BACKEND_UNINITIALIZED, backend_state_); 1461 DCHECK_EQ(BACKEND_UNINITIALIZED, backend_state_);
1422 DCHECK(!initializing_); 1462 DCHECK(!initializing_);
1423 DCHECK(!scheduler_->ScheduledOperations()); 1463 DCHECK(!scheduler_->ScheduledOperations());
1424 initializing_ = true; 1464 initializing_ = true;
1425 1465
1426 scheduler_->ScheduleOperation(base::Bind( 1466 scheduler_->ScheduleOperation(base::BindOnce(
1427 &CacheStorageCache::CreateBackend, weak_ptr_factory_.GetWeakPtr(), 1467 &CacheStorageCache::CreateBackend, weak_ptr_factory_.GetWeakPtr(),
1428 base::Bind( 1468 base::BindOnce(&CacheStorageCache::InitDidCreateBackend,
1429 &CacheStorageCache::InitDidCreateBackend, 1469 weak_ptr_factory_.GetWeakPtr(),
1430 weak_ptr_factory_.GetWeakPtr(), 1470 scheduler_->WrapCallbackToRunNext(
1431 scheduler_->WrapCallbackToRunNext(base::Bind(&base::DoNothing))))); 1471 base::BindOnce(&base::DoNothing)))));
1432 } 1472 }
1433 1473
1434 void CacheStorageCache::InitDidCreateBackend( 1474 void CacheStorageCache::InitDidCreateBackend(
1435 const base::Closure& callback, 1475 base::OnceClosure callback,
1436 CacheStorageError cache_create_error) { 1476 CacheStorageError cache_create_error) {
1437 if (cache_create_error != CACHE_STORAGE_OK) { 1477 if (cache_create_error != CACHE_STORAGE_OK) {
1438 InitGotCacheSize(callback, cache_create_error, 0); 1478 InitGotCacheSize(std::move(callback), cache_create_error, 0);
1439 return; 1479 return;
1440 } 1480 }
1441 1481
1442 int rv = backend_->CalculateSizeOfAllEntries( 1482 net::CompletionCallback size_callback =
1443 base::Bind(&CacheStorageCache::InitGotCacheSize, 1483 base::AdaptCallbackForRepeating(base::BindOnce(
1444 weak_ptr_factory_.GetWeakPtr(), callback, cache_create_error)); 1484 &CacheStorageCache::InitGotCacheSize, weak_ptr_factory_.GetWeakPtr(),
1485 std::move(callback), cache_create_error));
1445 1486
1487 int rv = backend_->CalculateSizeOfAllEntries(size_callback);
1446 if (rv != net::ERR_IO_PENDING) 1488 if (rv != net::ERR_IO_PENDING)
1447 InitGotCacheSize(callback, cache_create_error, rv); 1489 std::move(size_callback).Run(rv);
1448 } 1490 }
1449 1491
1450 void CacheStorageCache::InitGotCacheSize(const base::Closure& callback, 1492 void CacheStorageCache::InitGotCacheSize(base::OnceClosure callback,
1451 CacheStorageError cache_create_error, 1493 CacheStorageError cache_create_error,
1452 int cache_size) { 1494 int cache_size) {
1453 // Now that we know the cache size either 1) the cache size should be unknown 1495 // 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 1496 // (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 1497 // 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. 1498 // is saved in the store's index.
1457 if (cache_size_ != CacheStorage::kSizeUnknown) { 1499 if (cache_size_ != CacheStorage::kSizeUnknown) {
1458 LOG_IF(ERROR, cache_size_ != cache_size) 1500 LOG_IF(ERROR, cache_size_ != cache_size)
1459 << "Cache size: " << cache_size 1501 << "Cache size: " << cache_size
1460 << " does not match size from index: " << cache_size_; 1502 << " does not match size from index: " << cache_size_;
1461 UMA_HISTOGRAM_COUNTS_10M("ServiceWorkerCache.IndexSizeDifference", 1503 UMA_HISTOGRAM_COUNTS_10M("ServiceWorkerCache.IndexSizeDifference",
1462 std::abs(cache_size_ - cache_size)); 1504 std::abs(cache_size_ - cache_size));
1463 // Disabled for crbug.com/681900. 1505 // Disabled for crbug.com/681900.
1464 // DCHECK_EQ(cache_size_, cache_size); 1506 // DCHECK_EQ(cache_size_, cache_size);
1465 } 1507 }
1466 cache_size_ = cache_size; 1508 cache_size_ = cache_size;
1467 initializing_ = false; 1509 initializing_ = false;
1468 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ && 1510 backend_state_ = (cache_create_error == CACHE_STORAGE_OK && backend_ &&
1469 backend_state_ == BACKEND_UNINITIALIZED) 1511 backend_state_ == BACKEND_UNINITIALIZED)
1470 ? BACKEND_OPEN 1512 ? BACKEND_OPEN
1471 : BACKEND_CLOSED; 1513 : BACKEND_CLOSED;
1472 1514
1473 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", 1515 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult",
1474 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1); 1516 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1);
1475 1517
1476 if (cache_observer_) 1518 if (cache_observer_)
1477 cache_observer_->CacheSizeUpdated(this, cache_size_); 1519 cache_observer_->CacheSizeUpdated(this, cache_size_);
1478 1520
1479 callback.Run(); 1521 std::move(callback).Run();
1480 } 1522 }
1481 1523
1482 std::unique_ptr<storage::BlobDataHandle> 1524 std::unique_ptr<storage::BlobDataHandle>
1483 CacheStorageCache::PopulateResponseBody(disk_cache::ScopedEntryPtr entry, 1525 CacheStorageCache::PopulateResponseBody(disk_cache::ScopedEntryPtr entry,
1484 ServiceWorkerResponse* response) { 1526 ServiceWorkerResponse* response) {
1485 DCHECK(blob_storage_context_); 1527 DCHECK(blob_storage_context_);
1486 1528
1487 // Create a blob with the response body data. 1529 // Create a blob with the response body data.
1488 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY); 1530 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY);
1489 response->blob_uuid = base::GenerateGUID(); 1531 response->blob_uuid = base::GenerateGUID();
1490 storage::BlobDataBuilder blob_data(response->blob_uuid); 1532 storage::BlobDataBuilder blob_data(response->blob_uuid);
1491 1533
1492 disk_cache::Entry* temp_entry = entry.get(); 1534 disk_cache::Entry* temp_entry = entry.get();
1493 blob_data.AppendDiskCacheEntryWithSideData( 1535 blob_data.AppendDiskCacheEntryWithSideData(
1494 new CacheStorageCacheDataHandle(CreateCacheHandle(), std::move(entry)), 1536 new CacheStorageCacheDataHandle(CreateCacheHandle(), std::move(entry)),
1495 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); 1537 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA);
1496 return blob_storage_context_->AddFinishedBlob(&blob_data); 1538 return blob_storage_context_->AddFinishedBlob(&blob_data);
1497 } 1539 }
1498 1540
1499 std::unique_ptr<CacheStorageCacheHandle> 1541 std::unique_ptr<CacheStorageCacheHandle>
1500 CacheStorageCache::CreateCacheHandle() { 1542 CacheStorageCache::CreateCacheHandle() {
1501 return cache_storage_->CreateCacheHandle(this); 1543 return cache_storage_->CreateCacheHandle(this);
1502 } 1544 }
1503 1545
1504 } // namespace content 1546 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698