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

Side by Side Diff: content/browser/service_worker/service_worker_database.cc

Issue 672813002: [ServiceWorker] Added size deltas and total size computation for QuotaM. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added tests Created 6 years, 1 month 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/service_worker/service_worker_database.h" 5 #include "content/browser/service_worker/service_worker_database.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 if (status != STATUS_OK) 485 if (status != STATUS_OK)
486 return status; 486 return status;
487 487
488 *registration = value; 488 *registration = value;
489 return STATUS_OK; 489 return STATUS_OK;
490 } 490 }
491 491
492 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteRegistration( 492 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteRegistration(
493 const RegistrationData& registration, 493 const RegistrationData& registration,
494 const std::vector<ResourceRecord>& resources, 494 const std::vector<ResourceRecord>& resources,
495 int64* deleted_version_id, 495 RegistrationData* old_registration,
496 std::vector<int64>* newly_purgeable_resources) { 496 std::vector<int64>* newly_purgeable_resources) {
497 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); 497 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
498 *deleted_version_id = kInvalidServiceWorkerVersionId; 498 DCHECK(old_registration);
499 Status status = LazyOpen(true); 499 Status status = LazyOpen(true);
500 old_registration->version_id = kInvalidServiceWorkerVersionId;
500 if (status != STATUS_OK) 501 if (status != STATUS_OK)
501 return status; 502 return status;
502 503
503 leveldb::WriteBatch batch; 504 leveldb::WriteBatch batch;
504 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch); 505 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch);
505 BumpNextVersionIdIfNeeded(registration.version_id, &batch); 506 BumpNextVersionIdIfNeeded(registration.version_id, &batch);
506 507
507 PutUniqueOriginToBatch(registration.scope.GetOrigin(), &batch); 508 PutUniqueOriginToBatch(registration.scope.GetOrigin(), &batch);
508 #if DCHECK_IS_ON 509 #if DCHECK_IS_ON
509 uint64 total_size_bytes = 0; 510 int64_t total_size_bytes = 0;
510 for (const auto& resource : resources) { 511 for (const auto& resource : resources) {
511 total_size_bytes += resource.size_bytes; 512 total_size_bytes += resource.size_bytes;
512 } 513 }
513 DCHECK_EQ(total_size_bytes, registration.resources_total_size_bytes) 514 DCHECK_EQ(total_size_bytes, registration.resources_total_size_bytes)
514 << "The total size in the registration must match the cumulative " 515 << "The total size in the registration must match the cumulative "
515 << "sizes of the resources."; 516 << "sizes of the resources.";
516 #endif 517 #endif
517 PutRegistrationDataToBatch(registration, &batch); 518 PutRegistrationDataToBatch(registration, &batch);
518 519
519 // Used for avoiding multiple writes for the same resource id or url. 520 // Used for avoiding multiple writes for the same resource id or url.
(...skipping 12 matching lines...) Expand all
532 533
533 // Delete a resource from the uncommitted list. 534 // Delete a resource from the uncommitted list.
534 batch.Delete(CreateResourceIdKey( 535 batch.Delete(CreateResourceIdKey(
535 kUncommittedResIdKeyPrefix, itr->resource_id)); 536 kUncommittedResIdKeyPrefix, itr->resource_id));
536 // Delete from the purgeable list in case this version was once deleted. 537 // Delete from the purgeable list in case this version was once deleted.
537 batch.Delete( 538 batch.Delete(
538 CreateResourceIdKey(kPurgeableResIdKeyPrefix, itr->resource_id)); 539 CreateResourceIdKey(kPurgeableResIdKeyPrefix, itr->resource_id));
539 } 540 }
540 541
541 // Retrieve a previous version to sweep purgeable resources. 542 // Retrieve a previous version to sweep purgeable resources.
542 RegistrationData old_registration;
543 status = ReadRegistrationData(registration.registration_id, 543 status = ReadRegistrationData(registration.registration_id,
544 registration.scope.GetOrigin(), 544 registration.scope.GetOrigin(),
545 &old_registration); 545 old_registration);
546 if (status != STATUS_OK && status != STATUS_ERROR_NOT_FOUND) 546 if (status != STATUS_OK && status != STATUS_ERROR_NOT_FOUND)
547 return status; 547 return status;
548 if (status == STATUS_OK) { 548 if (status == STATUS_OK) {
549 DCHECK_LT(old_registration.version_id, registration.version_id); 549 DCHECK_LT(old_registration->version_id, registration.version_id);
550 *deleted_version_id = old_registration.version_id;
551 status = DeleteResourceRecords( 550 status = DeleteResourceRecords(
552 old_registration.version_id, newly_purgeable_resources, &batch); 551 old_registration->version_id, newly_purgeable_resources, &batch);
553 if (status != STATUS_OK) 552 if (status != STATUS_OK)
554 return status; 553 return status;
555 554
556 // Currently resource sharing across versions and registrations is not 555 // Currently resource sharing across versions and registrations is not
557 // supported, so resource ids should not be overlapped between 556 // supported, so resource ids should not be overlapped between
558 // |registration| and |old_registration|. 557 // |registration| and |old_registration|.
559 std::set<int64> deleted_resources(newly_purgeable_resources->begin(), 558 std::set<int64> deleted_resources(newly_purgeable_resources->begin(),
560 newly_purgeable_resources->end()); 559 newly_purgeable_resources->end());
561 DCHECK(base::STLSetIntersection<std::set<int64> >( 560 DCHECK(base::STLSetIntersection<std::set<int64> >(
562 pushed_resources, deleted_resources).empty()); 561 pushed_resources, deleted_resources).empty());
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 registration.last_update_check = time; 609 registration.last_update_check = time;
611 610
612 leveldb::WriteBatch batch; 611 leveldb::WriteBatch batch;
613 PutRegistrationDataToBatch(registration, &batch); 612 PutRegistrationDataToBatch(registration, &batch);
614 return WriteBatch(&batch); 613 return WriteBatch(&batch);
615 } 614 }
616 615
617 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteRegistration( 616 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteRegistration(
618 int64 registration_id, 617 int64 registration_id,
619 const GURL& origin, 618 const GURL& origin,
620 int64* version_id, 619 RegistrationData* deleted_version,
621 std::vector<int64>* newly_purgeable_resources) { 620 std::vector<int64>* newly_purgeable_resources) {
622 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); 621 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
623 *version_id = kInvalidServiceWorkerVersionId; 622 DCHECK(deleted_version);
623 deleted_version->version_id = kInvalidServiceWorkerVersionId;
624 Status status = LazyOpen(false); 624 Status status = LazyOpen(false);
625 if (IsNewOrNonexistentDatabase(status)) 625 if (IsNewOrNonexistentDatabase(status))
626 return STATUS_OK; 626 return STATUS_OK;
627 if (status != STATUS_OK) 627 if (status != STATUS_OK)
628 return status; 628 return status;
629 if (!origin.is_valid()) 629 if (!origin.is_valid())
630 return STATUS_ERROR_FAILED; 630 return STATUS_ERROR_FAILED;
631 631
632 leveldb::WriteBatch batch; 632 leveldb::WriteBatch batch;
633 633
634 // Remove |origin| from unique origins if a registration specified by 634 // Remove |origin| from unique origins if a registration specified by
635 // |registration_id| is the only one for |origin|. 635 // |registration_id| is the only one for |origin|.
636 // TODO(nhiroki): Check the uniqueness by more efficient way. 636 // TODO(nhiroki): Check the uniqueness by more efficient way.
637 std::vector<RegistrationData> registrations; 637 std::vector<RegistrationData> registrations;
638 status = GetRegistrationsForOrigin(origin, &registrations); 638 status = GetRegistrationsForOrigin(origin, &registrations);
639 if (status != STATUS_OK) 639 if (status != STATUS_OK)
640 return status; 640 return status;
641 641
642 if (registrations.size() == 1 && 642 if (registrations.size() == 1 &&
643 registrations[0].registration_id == registration_id) { 643 registrations[0].registration_id == registration_id) {
644 batch.Delete(CreateUniqueOriginKey(origin)); 644 batch.Delete(CreateUniqueOriginKey(origin));
645 } 645 }
646 646
647 // Delete a registration specified by |registration_id|. 647 // Delete a registration specified by |registration_id|.
648 batch.Delete(CreateRegistrationKey(registration_id, origin)); 648 batch.Delete(CreateRegistrationKey(registration_id, origin));
649 649
650 // Delete resource records associated with the registration. 650 // Delete resource records associated with the registration.
651 for (std::vector<RegistrationData>::const_iterator itr = 651 for (const auto& registration : registrations) {
652 registrations.begin(); itr != registrations.end(); ++itr) { 652 if (registration.registration_id == registration_id) {
653 if (itr->registration_id == registration_id) { 653 *deleted_version = registration;
654 *version_id = itr->version_id;
655 status = DeleteResourceRecords( 654 status = DeleteResourceRecords(
656 itr->version_id, newly_purgeable_resources, &batch); 655 registration.version_id, newly_purgeable_resources, &batch);
657 if (status != STATUS_OK) 656 if (status != STATUS_OK)
658 return status; 657 return status;
659 break; 658 break;
660 } 659 }
661 } 660 }
662 661
663 return WriteBatch(&batch); 662 return WriteBatch(&batch);
664 } 663 }
665 664
666 ServiceWorkerDatabase::Status 665 ServiceWorkerDatabase::Status
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
1145 1144
1146 void ServiceWorkerDatabase::HandleWriteResult( 1145 void ServiceWorkerDatabase::HandleWriteResult(
1147 const tracked_objects::Location& from_here, 1146 const tracked_objects::Location& from_here,
1148 Status status) { 1147 Status status) {
1149 if (status != STATUS_OK) 1148 if (status != STATUS_OK)
1150 Disable(from_here, status); 1149 Disable(from_here, status);
1151 ServiceWorkerMetrics::CountWriteDatabaseResult(status); 1150 ServiceWorkerMetrics::CountWriteDatabaseResult(status);
1152 } 1151 }
1153 1152
1154 } // namespace content 1153 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698