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

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

Issue 2167483002: [CacheStorage] Simpler way to wrap callbacks to run pending operation completions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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 <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 void CacheStorageCache::Match( 317 void CacheStorageCache::Match(
318 std::unique_ptr<ServiceWorkerFetchRequest> request, 318 std::unique_ptr<ServiceWorkerFetchRequest> request,
319 const ResponseCallback& callback) { 319 const ResponseCallback& callback) {
320 if (!LazyInitialize()) { 320 if (!LazyInitialize()) {
321 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 321 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
322 std::unique_ptr<ServiceWorkerResponse>(), 322 std::unique_ptr<ServiceWorkerResponse>(),
323 std::unique_ptr<storage::BlobDataHandle>()); 323 std::unique_ptr<storage::BlobDataHandle>());
324 return; 324 return;
325 } 325 }
326 326
327 ResponseCallback pending_callback =
328 base::Bind(&CacheStorageCache::PendingResponseCallback,
329 weak_ptr_factory_.GetWeakPtr(), callback);
330 scheduler_->ScheduleOperation( 327 scheduler_->ScheduleOperation(
331 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), 328 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(),
332 base::Passed(std::move(request)), pending_callback)); 329 base::Passed(std::move(request)),
330 scheduler_->WrapCallbackToRunNext(callback)));
333 } 331 }
334 332
335 void CacheStorageCache::MatchAll( 333 void CacheStorageCache::MatchAll(
336 std::unique_ptr<ServiceWorkerFetchRequest> request, 334 std::unique_ptr<ServiceWorkerFetchRequest> request,
337 const CacheStorageCacheQueryParams& match_params, 335 const CacheStorageCacheQueryParams& match_params,
338 const ResponsesCallback& callback) { 336 const ResponsesCallback& callback) {
339 if (!LazyInitialize()) { 337 if (!LazyInitialize()) {
340 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), 338 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(),
341 std::unique_ptr<BlobDataHandles>()); 339 std::unique_ptr<BlobDataHandles>());
342 return; 340 return;
343 } 341 }
344 342
345 ResponsesCallback pending_callback =
346 base::Bind(&CacheStorageCache::PendingResponsesCallback,
347 weak_ptr_factory_.GetWeakPtr(), callback);
348
349 std::unique_ptr<MatchAllContext> context( 343 std::unique_ptr<MatchAllContext> context(
350 new MatchAllContext(std::move(request), match_params, pending_callback)); 344 new MatchAllContext(std::move(request), match_params,
345 scheduler_->WrapCallbackToRunNext(callback)));
351 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::MatchAllImpl, 346 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::MatchAllImpl,
352 weak_ptr_factory_.GetWeakPtr(), 347 weak_ptr_factory_.GetWeakPtr(),
353 base::Passed(std::move(context)))); 348 base::Passed(std::move(context))));
354 } 349 }
355 350
356 void CacheStorageCache::WriteSideData(const ErrorCallback& callback, 351 void CacheStorageCache::WriteSideData(const ErrorCallback& callback,
357 const GURL& url, 352 const GURL& url,
358 base::Time expected_response_time, 353 base::Time expected_response_time,
359 scoped_refptr<net::IOBuffer> buffer, 354 scoped_refptr<net::IOBuffer> buffer,
360 int buf_len) { 355 int buf_len) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 return; 470 return;
476 callback->Run(CACHE_STORAGE_OK); 471 callback->Run(CACHE_STORAGE_OK);
477 } 472 }
478 473
479 void CacheStorageCache::Keys(const RequestsCallback& callback) { 474 void CacheStorageCache::Keys(const RequestsCallback& callback) {
480 if (!LazyInitialize()) { 475 if (!LazyInitialize()) {
481 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>()); 476 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>());
482 return; 477 return;
483 } 478 }
484 479
485 RequestsCallback pending_callback = 480 scheduler_->ScheduleOperation(
486 base::Bind(&CacheStorageCache::PendingRequestsCallback, 481 base::Bind(&CacheStorageCache::KeysImpl, weak_ptr_factory_.GetWeakPtr(),
487 weak_ptr_factory_.GetWeakPtr(), callback); 482 scheduler_->WrapCallbackToRunNext(callback)));
488 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::KeysImpl,
489 weak_ptr_factory_.GetWeakPtr(),
490 pending_callback));
491 } 483 }
492 484
493 void CacheStorageCache::Close(const base::Closure& callback) { 485 void CacheStorageCache::Close(const base::Closure& callback) {
494 DCHECK_NE(BACKEND_CLOSED, backend_state_) 486 DCHECK_NE(BACKEND_CLOSED, backend_state_)
495 << "Was CacheStorageCache::Close() called twice?"; 487 << "Was CacheStorageCache::Close() called twice?";
496 488
497 base::Closure pending_callback = 489 scheduler_->ScheduleOperation(
498 base::Bind(&CacheStorageCache::PendingClosure, 490 base::Bind(&CacheStorageCache::CloseImpl, weak_ptr_factory_.GetWeakPtr(),
499 weak_ptr_factory_.GetWeakPtr(), callback); 491 scheduler_->WrapCallbackToRunNext(callback)));
500
501 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::CloseImpl,
502 weak_ptr_factory_.GetWeakPtr(),
503 pending_callback));
504 } 492 }
505 493
506 void CacheStorageCache::Size(const SizeCallback& callback) { 494 void CacheStorageCache::Size(const SizeCallback& callback) {
507 if (!LazyInitialize()) { 495 if (!LazyInitialize()) {
508 // TODO(jkarlin): Delete caches that can't be initialized. 496 // TODO(jkarlin): Delete caches that can't be initialized.
509 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 497 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
510 base::Bind(callback, 0)); 498 base::Bind(callback, 0));
511 return; 499 return;
512 } 500 }
513 501
514 SizeCallback pending_callback = 502 scheduler_->ScheduleOperation(
515 base::Bind(&CacheStorageCache::PendingSizeCallback, 503 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(),
516 weak_ptr_factory_.GetWeakPtr(), callback); 504 scheduler_->WrapCallbackToRunNext(callback)));
517
518 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::SizeImpl,
519 weak_ptr_factory_.GetWeakPtr(),
520 pending_callback));
521 } 505 }
522 506
523 void CacheStorageCache::GetSizeThenClose(const SizeCallback& callback) { 507 void CacheStorageCache::GetSizeThenClose(const SizeCallback& callback) {
524 if (!LazyInitialize()) { 508 if (!LazyInitialize()) {
525 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 509 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
526 base::Bind(callback, 0)); 510 base::Bind(callback, 0));
527 return; 511 return;
528 } 512 }
529 513
530 SizeCallback pending_callback =
531 base::Bind(&CacheStorageCache::PendingSizeCallback,
532 weak_ptr_factory_.GetWeakPtr(), callback);
533
534 scheduler_->ScheduleOperation( 514 scheduler_->ScheduleOperation(
535 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(), 515 base::Bind(&CacheStorageCache::SizeImpl, weak_ptr_factory_.GetWeakPtr(),
536 base::Bind(&CacheStorageCache::GetSizeThenCloseDidGetSize, 516 base::Bind(&CacheStorageCache::GetSizeThenCloseDidGetSize,
537 weak_ptr_factory_.GetWeakPtr(), pending_callback))); 517 weak_ptr_factory_.GetWeakPtr(),
518 scheduler_->WrapCallbackToRunNext(callback))));
538 } 519 }
539 520
540 CacheStorageCache::~CacheStorageCache() { 521 CacheStorageCache::~CacheStorageCache() {
541 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_); 522 quota_manager_proxy_->NotifyOriginNoLongerInUse(origin_);
542 } 523 }
543 524
544 CacheStorageCache::CacheStorageCache( 525 CacheStorageCache::CacheStorageCache(
545 const GURL& origin, 526 const GURL& origin,
546 const std::string& cache_name, 527 const std::string& cache_name,
547 const base::FilePath& path, 528 const base::FilePath& path,
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 int buf_len, 816 int buf_len,
836 storage::QuotaStatusCode status_code, 817 storage::QuotaStatusCode status_code,
837 int64_t usage, 818 int64_t usage,
838 int64_t quota) { 819 int64_t quota) {
839 if (status_code != storage::kQuotaStatusOk || (buf_len > quota - usage)) { 820 if (status_code != storage::kQuotaStatusOk || (buf_len > quota - usage)) {
840 base::ThreadTaskRunnerHandle::Get()->PostTask( 821 base::ThreadTaskRunnerHandle::Get()->PostTask(
841 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_QUOTA_EXCEEDED)); 822 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_QUOTA_EXCEEDED));
842 return; 823 return;
843 } 824 }
844 825
845 ErrorCallback pending_callback =
846 base::Bind(&CacheStorageCache::PendingErrorCallback,
847 weak_ptr_factory_.GetWeakPtr(), callback);
848
849 scheduler_->ScheduleOperation(base::Bind( 826 scheduler_->ScheduleOperation(base::Bind(
850 &CacheStorageCache::WriteSideDataImpl, weak_ptr_factory_.GetWeakPtr(), 827 &CacheStorageCache::WriteSideDataImpl, weak_ptr_factory_.GetWeakPtr(),
851 pending_callback, url, expected_response_time, buffer, buf_len)); 828 scheduler_->WrapCallbackToRunNext(callback), url, expected_response_time,
829 buffer, buf_len));
852 } 830 }
853 831
854 void CacheStorageCache::WriteSideDataImpl(const ErrorCallback& callback, 832 void CacheStorageCache::WriteSideDataImpl(const ErrorCallback& callback,
855 const GURL& url, 833 const GURL& url,
856 base::Time expected_response_time, 834 base::Time expected_response_time,
857 scoped_refptr<net::IOBuffer> buffer, 835 scoped_refptr<net::IOBuffer> buffer,
858 int buf_len) { 836 int buf_len) {
859 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 837 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
860 if (backend_state_ != BACKEND_OPEN) { 838 if (backend_state_ != BACKEND_OPEN) {
861 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 839 callback.Run(CACHE_STORAGE_ERROR_STORAGE);
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
979 return; 957 return;
980 } 958 }
981 } 959 }
982 960
983 UMA_HISTOGRAM_ENUMERATION( 961 UMA_HISTOGRAM_ENUMERATION(
984 "ServiceWorkerCache.Cache.AllWritesResponseType", 962 "ServiceWorkerCache.Cache.AllWritesResponseType",
985 operation.response.response_type, 963 operation.response.response_type,
986 blink::WebServiceWorkerResponseType::WebServiceWorkerResponseTypeLast + 964 blink::WebServiceWorkerResponseType::WebServiceWorkerResponseTypeLast +
987 1); 965 1);
988 966
989 ErrorCallback pending_callback = 967 std::unique_ptr<PutContext> put_context(new PutContext(
990 base::Bind(&CacheStorageCache::PendingErrorCallback, 968 std::move(request), std::move(response), std::move(blob_data_handle),
991 weak_ptr_factory_.GetWeakPtr(), callback); 969 scheduler_->WrapCallbackToRunNext(callback)));
992
993 std::unique_ptr<PutContext> put_context(
994 new PutContext(std::move(request), std::move(response),
995 std::move(blob_data_handle), pending_callback));
996 970
997 scheduler_->ScheduleOperation( 971 scheduler_->ScheduleOperation(
998 base::Bind(&CacheStorageCache::PutImpl, weak_ptr_factory_.GetWeakPtr(), 972 base::Bind(&CacheStorageCache::PutImpl, weak_ptr_factory_.GetWeakPtr(),
999 base::Passed(std::move(put_context)))); 973 base::Passed(std::move(put_context))));
1000 } 974 }
1001 975
1002 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) { 976 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) {
1003 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 977 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1004 if (backend_state_ != BACKEND_OPEN) { 978 if (backend_state_ != BACKEND_OPEN) {
1005 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 979 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
1199 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); 1173 DCHECK(BACKEND_OPEN == backend_state_ || initializing_);
1200 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE, 1174 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE,
1201 operation.operation_type); 1175 operation.operation_type);
1202 1176
1203 std::unique_ptr<ServiceWorkerFetchRequest> request( 1177 std::unique_ptr<ServiceWorkerFetchRequest> request(
1204 new ServiceWorkerFetchRequest( 1178 new ServiceWorkerFetchRequest(
1205 operation.request.url, operation.request.method, 1179 operation.request.url, operation.request.method,
1206 operation.request.headers, operation.request.referrer, 1180 operation.request.headers, operation.request.referrer,
1207 operation.request.is_reload)); 1181 operation.request.is_reload));
1208 1182
1209 ErrorCallback pending_callback =
1210 base::Bind(&CacheStorageCache::PendingErrorCallback,
1211 weak_ptr_factory_.GetWeakPtr(), callback);
1212 scheduler_->ScheduleOperation( 1183 scheduler_->ScheduleOperation(
1213 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(), 1184 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(),
1214 base::Passed(std::move(request)), operation.match_params, 1185 base::Passed(std::move(request)), operation.match_params,
1215 pending_callback)); 1186 scheduler_->WrapCallbackToRunNext(callback)));
1216 } 1187 }
1217 1188
1218 void CacheStorageCache::DeleteImpl( 1189 void CacheStorageCache::DeleteImpl(
1219 std::unique_ptr<ServiceWorkerFetchRequest> request, 1190 std::unique_ptr<ServiceWorkerFetchRequest> request,
1220 const CacheStorageCacheQueryParams& match_params, 1191 const CacheStorageCacheQueryParams& match_params,
1221 const ErrorCallback& callback) { 1192 const ErrorCallback& callback) {
1222 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1193 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1223 if (backend_state_ != BACKEND_OPEN) { 1194 if (backend_state_ != BACKEND_OPEN) {
1224 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1195 callback.Run(CACHE_STORAGE_ERROR_STORAGE);
1225 return; 1196 return;
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
1471 backend_state_ == BACKEND_UNINITIALIZED) 1442 backend_state_ == BACKEND_UNINITIALIZED)
1472 ? BACKEND_OPEN 1443 ? BACKEND_OPEN
1473 : BACKEND_CLOSED; 1444 : BACKEND_CLOSED;
1474 1445
1475 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", 1446 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult",
1476 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1); 1447 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1);
1477 1448
1478 scheduler_->CompleteOperationAndRunNext(); 1449 scheduler_->CompleteOperationAndRunNext();
1479 } 1450 }
1480 1451
1481 void CacheStorageCache::PendingClosure(const base::Closure& callback) {
1482 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1483
1484 callback.Run();
1485 if (cache)
1486 scheduler_->CompleteOperationAndRunNext();
1487 }
1488
1489 void CacheStorageCache::PendingErrorCallback(const ErrorCallback& callback,
1490 CacheStorageError error) {
1491 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1492
1493 callback.Run(error);
1494 if (cache)
1495 scheduler_->CompleteOperationAndRunNext();
1496 }
1497
1498 void CacheStorageCache::PendingResponseCallback(
1499 const ResponseCallback& callback,
1500 CacheStorageError error,
1501 std::unique_ptr<ServiceWorkerResponse> response,
1502 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) {
1503 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1504
1505 callback.Run(error, std::move(response), std::move(blob_data_handle));
1506 if (cache)
1507 scheduler_->CompleteOperationAndRunNext();
1508 }
1509
1510 void CacheStorageCache::PendingResponsesCallback(
1511 const ResponsesCallback& callback,
1512 CacheStorageError error,
1513 std::unique_ptr<Responses> responses,
1514 std::unique_ptr<BlobDataHandles> blob_data_handles) {
1515 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1516
1517 callback.Run(error, std::move(responses), std::move(blob_data_handles));
1518 if (cache)
1519 scheduler_->CompleteOperationAndRunNext();
1520 }
1521
1522 void CacheStorageCache::PendingRequestsCallback(
1523 const RequestsCallback& callback,
1524 CacheStorageError error,
1525 std::unique_ptr<Requests> requests) {
1526 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1527
1528 callback.Run(error, std::move(requests));
1529 if (cache)
1530 scheduler_->CompleteOperationAndRunNext();
1531 }
1532
1533 void CacheStorageCache::PendingSizeCallback(const SizeCallback& callback,
1534 int64_t size) {
1535 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1536
1537 callback.Run(size);
1538 if (cache)
1539 scheduler_->CompleteOperationAndRunNext();
1540 }
1541
1542 void CacheStorageCache::PopulateResponseMetadata( 1452 void CacheStorageCache::PopulateResponseMetadata(
1543 const CacheMetadata& metadata, 1453 const CacheMetadata& metadata,
1544 ServiceWorkerResponse* response) { 1454 ServiceWorkerResponse* response) {
1545 *response = ServiceWorkerResponse( 1455 *response = ServiceWorkerResponse(
1546 GURL(metadata.response().url()), metadata.response().status_code(), 1456 GURL(metadata.response().url()), metadata.response().status_code(),
1547 metadata.response().status_text(), 1457 metadata.response().status_text(),
1548 ProtoResponseTypeToWebResponseType(metadata.response().response_type()), 1458 ProtoResponseTypeToWebResponseType(metadata.response().response_type()),
1549 ServiceWorkerHeaderMap(), "", 0, GURL(), 1459 ServiceWorkerHeaderMap(), "", 0, GURL(),
1550 blink::WebServiceWorkerResponseErrorUnknown, 1460 blink::WebServiceWorkerResponseErrorUnknown,
1551 base::Time::FromInternalValue(metadata.response().response_time()), 1461 base::Time::FromInternalValue(metadata.response().response_time()),
(...skipping 26 matching lines...) Expand all
1578 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); 1488 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA);
1579 return blob_storage_context_->AddFinishedBlob(&blob_data); 1489 return blob_storage_context_->AddFinishedBlob(&blob_data);
1580 } 1490 }
1581 1491
1582 std::unique_ptr<CacheStorageCacheHandle> 1492 std::unique_ptr<CacheStorageCacheHandle>
1583 CacheStorageCache::CreateCacheHandle() { 1493 CacheStorageCache::CreateCacheHandle() {
1584 return cache_storage_->CreateCacheHandle(this); 1494 return cache_storage_->CreateCacheHandle(this);
1585 } 1495 }
1586 1496
1587 } // namespace content 1497 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698