OLD | NEW |
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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) | 293 ServiceWorkerDatabase::ServiceWorkerDatabase(const base::FilePath& path) |
294 : path_(path), | 294 : path_(path), |
295 next_avail_registration_id_(0), | 295 next_avail_registration_id_(0), |
296 next_avail_resource_id_(0), | 296 next_avail_resource_id_(0), |
297 next_avail_version_id_(0), | 297 next_avail_version_id_(0), |
298 state_(UNINITIALIZED) { | 298 state_(UNINITIALIZED) { |
299 sequence_checker_.DetachFromSequence(); | 299 sequence_checker_.DetachFromSequence(); |
300 } | 300 } |
301 | 301 |
302 ServiceWorkerDatabase::~ServiceWorkerDatabase() { | 302 ServiceWorkerDatabase::~ServiceWorkerDatabase() { |
303 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 303 DCHECK(sequence_checker_.CalledOnValidSequence()); |
304 db_.reset(); | 304 db_.reset(); |
305 } | 305 } |
306 | 306 |
307 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetNextAvailableIds( | 307 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetNextAvailableIds( |
308 int64_t* next_avail_registration_id, | 308 int64_t* next_avail_registration_id, |
309 int64_t* next_avail_version_id, | 309 int64_t* next_avail_version_id, |
310 int64_t* next_avail_resource_id) { | 310 int64_t* next_avail_resource_id) { |
311 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 311 DCHECK(sequence_checker_.CalledOnValidSequence()); |
312 DCHECK(next_avail_registration_id); | 312 DCHECK(next_avail_registration_id); |
313 DCHECK(next_avail_version_id); | 313 DCHECK(next_avail_version_id); |
314 DCHECK(next_avail_resource_id); | 314 DCHECK(next_avail_resource_id); |
315 | 315 |
316 Status status = LazyOpen(false); | 316 Status status = LazyOpen(false); |
317 if (IsNewOrNonexistentDatabase(status)) { | 317 if (IsNewOrNonexistentDatabase(status)) { |
318 *next_avail_registration_id = 0; | 318 *next_avail_registration_id = 0; |
319 *next_avail_version_id = 0; | 319 *next_avail_version_id = 0; |
320 *next_avail_resource_id = 0; | 320 *next_avail_resource_id = 0; |
321 return STATUS_OK; | 321 return STATUS_OK; |
(...skipping 12 matching lines...) Expand all Loading... |
334 return status; | 334 return status; |
335 | 335 |
336 *next_avail_registration_id = next_avail_registration_id_; | 336 *next_avail_registration_id = next_avail_registration_id_; |
337 *next_avail_version_id = next_avail_version_id_; | 337 *next_avail_version_id = next_avail_version_id_; |
338 *next_avail_resource_id = next_avail_resource_id_; | 338 *next_avail_resource_id = next_avail_resource_id_; |
339 return STATUS_OK; | 339 return STATUS_OK; |
340 } | 340 } |
341 | 341 |
342 ServiceWorkerDatabase::Status | 342 ServiceWorkerDatabase::Status |
343 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { | 343 ServiceWorkerDatabase::GetOriginsWithRegistrations(std::set<GURL>* origins) { |
344 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 344 DCHECK(sequence_checker_.CalledOnValidSequence()); |
345 DCHECK(origins->empty()); | 345 DCHECK(origins->empty()); |
346 | 346 |
347 Status status = LazyOpen(false); | 347 Status status = LazyOpen(false); |
348 if (IsNewOrNonexistentDatabase(status)) | 348 if (IsNewOrNonexistentDatabase(status)) |
349 return STATUS_OK; | 349 return STATUS_OK; |
350 if (status != STATUS_OK) | 350 if (status != STATUS_OK) |
351 return status; | 351 return status; |
352 | 352 |
353 { | 353 { |
354 std::unique_ptr<leveldb::Iterator> itr( | 354 std::unique_ptr<leveldb::Iterator> itr( |
(...skipping 20 matching lines...) Expand all Loading... |
375 } | 375 } |
376 } | 376 } |
377 | 377 |
378 HandleReadResult(FROM_HERE, status); | 378 HandleReadResult(FROM_HERE, status); |
379 return status; | 379 return status; |
380 } | 380 } |
381 | 381 |
382 ServiceWorkerDatabase::Status | 382 ServiceWorkerDatabase::Status |
383 ServiceWorkerDatabase::GetOriginsWithForeignFetchRegistrations( | 383 ServiceWorkerDatabase::GetOriginsWithForeignFetchRegistrations( |
384 std::set<GURL>* origins) { | 384 std::set<GURL>* origins) { |
385 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 385 DCHECK(sequence_checker_.CalledOnValidSequence()); |
386 DCHECK(origins->empty()); | 386 DCHECK(origins->empty()); |
387 | 387 |
388 Status status = LazyOpen(false); | 388 Status status = LazyOpen(false); |
389 if (IsNewOrNonexistentDatabase(status)) | 389 if (IsNewOrNonexistentDatabase(status)) |
390 return STATUS_OK; | 390 return STATUS_OK; |
391 if (status != STATUS_OK) | 391 if (status != STATUS_OK) |
392 return status; | 392 return status; |
393 | 393 |
394 { | 394 { |
395 std::unique_ptr<leveldb::Iterator> itr( | 395 std::unique_ptr<leveldb::Iterator> itr( |
(...skipping 22 matching lines...) Expand all Loading... |
418 } | 418 } |
419 | 419 |
420 HandleReadResult(FROM_HERE, status); | 420 HandleReadResult(FROM_HERE, status); |
421 return status; | 421 return status; |
422 } | 422 } |
423 | 423 |
424 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin( | 424 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetRegistrationsForOrigin( |
425 const GURL& origin, | 425 const GURL& origin, |
426 std::vector<RegistrationData>* registrations, | 426 std::vector<RegistrationData>* registrations, |
427 std::vector<std::vector<ResourceRecord>>* opt_resources_list) { | 427 std::vector<std::vector<ResourceRecord>>* opt_resources_list) { |
428 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 428 DCHECK(sequence_checker_.CalledOnValidSequence()); |
429 DCHECK(registrations->empty()); | 429 DCHECK(registrations->empty()); |
430 | 430 |
431 Status status = LazyOpen(false); | 431 Status status = LazyOpen(false); |
432 if (IsNewOrNonexistentDatabase(status)) | 432 if (IsNewOrNonexistentDatabase(status)) |
433 return STATUS_OK; | 433 return STATUS_OK; |
434 if (status != STATUS_OK) | 434 if (status != STATUS_OK) |
435 return status; | 435 return status; |
436 | 436 |
437 std::string prefix = CreateRegistrationKeyPrefix(origin); | 437 std::string prefix = CreateRegistrationKeyPrefix(origin); |
438 { | 438 { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 } | 472 } |
473 } | 473 } |
474 } | 474 } |
475 | 475 |
476 HandleReadResult(FROM_HERE, status); | 476 HandleReadResult(FROM_HERE, status); |
477 return status; | 477 return status; |
478 } | 478 } |
479 | 479 |
480 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( | 480 ServiceWorkerDatabase::Status ServiceWorkerDatabase::GetAllRegistrations( |
481 std::vector<RegistrationData>* registrations) { | 481 std::vector<RegistrationData>* registrations) { |
482 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 482 DCHECK(sequence_checker_.CalledOnValidSequence()); |
483 DCHECK(registrations->empty()); | 483 DCHECK(registrations->empty()); |
484 | 484 |
485 Status status = LazyOpen(false); | 485 Status status = LazyOpen(false); |
486 if (IsNewOrNonexistentDatabase(status)) | 486 if (IsNewOrNonexistentDatabase(status)) |
487 return STATUS_OK; | 487 return STATUS_OK; |
488 if (status != STATUS_OK) | 488 if (status != STATUS_OK) |
489 return status; | 489 return status; |
490 | 490 |
491 { | 491 { |
492 std::unique_ptr<leveldb::Iterator> itr( | 492 std::unique_ptr<leveldb::Iterator> itr( |
(...skipping 20 matching lines...) Expand all Loading... |
513 | 513 |
514 HandleReadResult(FROM_HERE, status); | 514 HandleReadResult(FROM_HERE, status); |
515 return status; | 515 return status; |
516 } | 516 } |
517 | 517 |
518 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadRegistration( | 518 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadRegistration( |
519 int64_t registration_id, | 519 int64_t registration_id, |
520 const GURL& origin, | 520 const GURL& origin, |
521 RegistrationData* registration, | 521 RegistrationData* registration, |
522 std::vector<ResourceRecord>* resources) { | 522 std::vector<ResourceRecord>* resources) { |
523 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 523 DCHECK(sequence_checker_.CalledOnValidSequence()); |
524 DCHECK(registration); | 524 DCHECK(registration); |
525 DCHECK(resources); | 525 DCHECK(resources); |
526 | 526 |
527 Status status = LazyOpen(false); | 527 Status status = LazyOpen(false); |
528 if (IsNewOrNonexistentDatabase(status)) | 528 if (IsNewOrNonexistentDatabase(status)) |
529 return STATUS_ERROR_NOT_FOUND; | 529 return STATUS_ERROR_NOT_FOUND; |
530 if (status != STATUS_OK) | 530 if (status != STATUS_OK) |
531 return status; | 531 return status; |
532 | 532 |
533 RegistrationData value; | 533 RegistrationData value; |
534 status = ReadRegistrationData(registration_id, origin, &value); | 534 status = ReadRegistrationData(registration_id, origin, &value); |
535 if (status != STATUS_OK) | 535 if (status != STATUS_OK) |
536 return status; | 536 return status; |
537 | 537 |
538 status = ReadResourceRecords(value.version_id, resources); | 538 status = ReadResourceRecords(value.version_id, resources); |
539 if (status != STATUS_OK) | 539 if (status != STATUS_OK) |
540 return status; | 540 return status; |
541 | 541 |
542 // ResourceRecord must contain the ServiceWorker's main script. | 542 // ResourceRecord must contain the ServiceWorker's main script. |
543 if (resources->empty()) | 543 if (resources->empty()) |
544 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; | 544 return ServiceWorkerDatabase::STATUS_ERROR_CORRUPTED; |
545 | 545 |
546 *registration = value; | 546 *registration = value; |
547 return STATUS_OK; | 547 return STATUS_OK; |
548 } | 548 } |
549 | 549 |
550 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadRegistrationOrigin( | 550 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadRegistrationOrigin( |
551 int64_t registration_id, | 551 int64_t registration_id, |
552 GURL* origin) { | 552 GURL* origin) { |
553 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 553 DCHECK(sequence_checker_.CalledOnValidSequence()); |
554 DCHECK(origin); | 554 DCHECK(origin); |
555 | 555 |
556 Status status = LazyOpen(true); | 556 Status status = LazyOpen(true); |
557 if (IsNewOrNonexistentDatabase(status)) | 557 if (IsNewOrNonexistentDatabase(status)) |
558 return STATUS_ERROR_NOT_FOUND; | 558 return STATUS_ERROR_NOT_FOUND; |
559 if (status != STATUS_OK) | 559 if (status != STATUS_OK) |
560 return status; | 560 return status; |
561 | 561 |
562 std::string value; | 562 std::string value; |
563 status = LevelDBStatusToStatus( | 563 status = LevelDBStatusToStatus( |
(...skipping 15 matching lines...) Expand all Loading... |
579 *origin = parsed; | 579 *origin = parsed; |
580 HandleReadResult(FROM_HERE, STATUS_OK); | 580 HandleReadResult(FROM_HERE, STATUS_OK); |
581 return STATUS_OK; | 581 return STATUS_OK; |
582 } | 582 } |
583 | 583 |
584 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteRegistration( | 584 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteRegistration( |
585 const RegistrationData& registration, | 585 const RegistrationData& registration, |
586 const std::vector<ResourceRecord>& resources, | 586 const std::vector<ResourceRecord>& resources, |
587 RegistrationData* old_registration, | 587 RegistrationData* old_registration, |
588 std::vector<int64_t>* newly_purgeable_resources) { | 588 std::vector<int64_t>* newly_purgeable_resources) { |
589 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 589 DCHECK(sequence_checker_.CalledOnValidSequence()); |
590 DCHECK(old_registration); | 590 DCHECK(old_registration); |
591 DCHECK(!resources.empty()); | 591 DCHECK(!resources.empty()); |
592 Status status = LazyOpen(true); | 592 Status status = LazyOpen(true); |
593 old_registration->version_id = kInvalidServiceWorkerVersionId; | 593 old_registration->version_id = kInvalidServiceWorkerVersionId; |
594 if (status != STATUS_OK) | 594 if (status != STATUS_OK) |
595 return status; | 595 return status; |
596 | 596 |
597 leveldb::WriteBatch batch; | 597 leveldb::WriteBatch batch; |
598 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch); | 598 BumpNextRegistrationIdIfNeeded(registration.registration_id, &batch); |
599 BumpNextVersionIdIfNeeded(registration.version_id, &batch); | 599 BumpNextVersionIdIfNeeded(registration.version_id, &batch); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 CreateForeignFetchOriginKey(registration.scope.GetOrigin())); | 681 CreateForeignFetchOriginKey(registration.scope.GetOrigin())); |
682 } | 682 } |
683 } | 683 } |
684 | 684 |
685 return WriteBatch(&batch); | 685 return WriteBatch(&batch); |
686 } | 686 } |
687 | 687 |
688 ServiceWorkerDatabase::Status ServiceWorkerDatabase::UpdateVersionToActive( | 688 ServiceWorkerDatabase::Status ServiceWorkerDatabase::UpdateVersionToActive( |
689 int64_t registration_id, | 689 int64_t registration_id, |
690 const GURL& origin) { | 690 const GURL& origin) { |
691 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 691 DCHECK(sequence_checker_.CalledOnValidSequence()); |
692 Status status = LazyOpen(false); | 692 Status status = LazyOpen(false); |
693 if (IsNewOrNonexistentDatabase(status)) | 693 if (IsNewOrNonexistentDatabase(status)) |
694 return STATUS_ERROR_NOT_FOUND; | 694 return STATUS_ERROR_NOT_FOUND; |
695 if (status != STATUS_OK) | 695 if (status != STATUS_OK) |
696 return status; | 696 return status; |
697 if (!origin.is_valid()) | 697 if (!origin.is_valid()) |
698 return STATUS_ERROR_FAILED; | 698 return STATUS_ERROR_FAILED; |
699 | 699 |
700 RegistrationData registration; | 700 RegistrationData registration; |
701 status = ReadRegistrationData(registration_id, origin, ®istration); | 701 status = ReadRegistrationData(registration_id, origin, ®istration); |
702 if (status != STATUS_OK) | 702 if (status != STATUS_OK) |
703 return status; | 703 return status; |
704 | 704 |
705 registration.is_active = true; | 705 registration.is_active = true; |
706 | 706 |
707 leveldb::WriteBatch batch; | 707 leveldb::WriteBatch batch; |
708 WriteRegistrationDataInBatch(registration, &batch); | 708 WriteRegistrationDataInBatch(registration, &batch); |
709 return WriteBatch(&batch); | 709 return WriteBatch(&batch); |
710 } | 710 } |
711 | 711 |
712 ServiceWorkerDatabase::Status ServiceWorkerDatabase::UpdateLastCheckTime( | 712 ServiceWorkerDatabase::Status ServiceWorkerDatabase::UpdateLastCheckTime( |
713 int64_t registration_id, | 713 int64_t registration_id, |
714 const GURL& origin, | 714 const GURL& origin, |
715 const base::Time& time) { | 715 const base::Time& time) { |
716 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 716 DCHECK(sequence_checker_.CalledOnValidSequence()); |
717 Status status = LazyOpen(false); | 717 Status status = LazyOpen(false); |
718 if (IsNewOrNonexistentDatabase(status)) | 718 if (IsNewOrNonexistentDatabase(status)) |
719 return STATUS_ERROR_NOT_FOUND; | 719 return STATUS_ERROR_NOT_FOUND; |
720 if (status != STATUS_OK) | 720 if (status != STATUS_OK) |
721 return status; | 721 return status; |
722 if (!origin.is_valid()) | 722 if (!origin.is_valid()) |
723 return STATUS_ERROR_FAILED; | 723 return STATUS_ERROR_FAILED; |
724 | 724 |
725 RegistrationData registration; | 725 RegistrationData registration; |
726 status = ReadRegistrationData(registration_id, origin, ®istration); | 726 status = ReadRegistrationData(registration_id, origin, ®istration); |
727 if (status != STATUS_OK) | 727 if (status != STATUS_OK) |
728 return status; | 728 return status; |
729 | 729 |
730 registration.last_update_check = time; | 730 registration.last_update_check = time; |
731 | 731 |
732 leveldb::WriteBatch batch; | 732 leveldb::WriteBatch batch; |
733 WriteRegistrationDataInBatch(registration, &batch); | 733 WriteRegistrationDataInBatch(registration, &batch); |
734 return WriteBatch(&batch); | 734 return WriteBatch(&batch); |
735 } | 735 } |
736 | 736 |
737 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteRegistration( | 737 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteRegistration( |
738 int64_t registration_id, | 738 int64_t registration_id, |
739 const GURL& origin, | 739 const GURL& origin, |
740 RegistrationData* deleted_version, | 740 RegistrationData* deleted_version, |
741 std::vector<int64_t>* newly_purgeable_resources) { | 741 std::vector<int64_t>* newly_purgeable_resources) { |
742 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 742 DCHECK(sequence_checker_.CalledOnValidSequence()); |
743 DCHECK(deleted_version); | 743 DCHECK(deleted_version); |
744 deleted_version->version_id = kInvalidServiceWorkerVersionId; | 744 deleted_version->version_id = kInvalidServiceWorkerVersionId; |
745 Status status = LazyOpen(false); | 745 Status status = LazyOpen(false); |
746 if (IsNewOrNonexistentDatabase(status)) | 746 if (IsNewOrNonexistentDatabase(status)) |
747 return STATUS_OK; | 747 return STATUS_OK; |
748 if (status != STATUS_OK) | 748 if (status != STATUS_OK) |
749 return status; | 749 return status; |
750 if (!origin.is_valid()) | 750 if (!origin.is_valid()) |
751 return STATUS_ERROR_FAILED; | 751 return STATUS_ERROR_FAILED; |
752 | 752 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 } | 798 } |
799 } | 799 } |
800 | 800 |
801 return WriteBatch(&batch); | 801 return WriteBatch(&batch); |
802 } | 802 } |
803 | 803 |
804 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadUserData( | 804 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadUserData( |
805 int64_t registration_id, | 805 int64_t registration_id, |
806 const std::vector<std::string>& user_data_names, | 806 const std::vector<std::string>& user_data_names, |
807 std::vector<std::string>* user_data_values) { | 807 std::vector<std::string>* user_data_values) { |
808 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 808 DCHECK(sequence_checker_.CalledOnValidSequence()); |
809 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 809 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
810 DCHECK(!user_data_names.empty()); | 810 DCHECK(!user_data_names.empty()); |
811 DCHECK(user_data_values); | 811 DCHECK(user_data_values); |
812 | 812 |
813 Status status = LazyOpen(false); | 813 Status status = LazyOpen(false); |
814 if (IsNewOrNonexistentDatabase(status)) | 814 if (IsNewOrNonexistentDatabase(status)) |
815 return STATUS_ERROR_NOT_FOUND; | 815 return STATUS_ERROR_NOT_FOUND; |
816 if (status != STATUS_OK) | 816 if (status != STATUS_OK) |
817 return status; | 817 return status; |
818 | 818 |
(...skipping 10 matching lines...) Expand all Loading... |
829 } | 829 } |
830 HandleReadResult(FROM_HERE, | 830 HandleReadResult(FROM_HERE, |
831 status == STATUS_ERROR_NOT_FOUND ? STATUS_OK : status); | 831 status == STATUS_ERROR_NOT_FOUND ? STATUS_OK : status); |
832 return status; | 832 return status; |
833 } | 833 } |
834 | 834 |
835 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteUserData( | 835 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteUserData( |
836 int64_t registration_id, | 836 int64_t registration_id, |
837 const GURL& origin, | 837 const GURL& origin, |
838 const std::vector<std::pair<std::string, std::string>>& name_value_pairs) { | 838 const std::vector<std::pair<std::string, std::string>>& name_value_pairs) { |
839 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 839 DCHECK(sequence_checker_.CalledOnValidSequence()); |
840 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 840 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
841 DCHECK(!name_value_pairs.empty()); | 841 DCHECK(!name_value_pairs.empty()); |
842 | 842 |
843 Status status = LazyOpen(false); | 843 Status status = LazyOpen(false); |
844 if (IsNewOrNonexistentDatabase(status)) | 844 if (IsNewOrNonexistentDatabase(status)) |
845 return STATUS_ERROR_NOT_FOUND; | 845 return STATUS_ERROR_NOT_FOUND; |
846 if (status != STATUS_OK) | 846 if (status != STATUS_OK) |
847 return status; | 847 return status; |
848 | 848 |
849 // There should be the registration specified by |registration_id|. | 849 // There should be the registration specified by |registration_id|. |
850 RegistrationData registration; | 850 RegistrationData registration; |
851 status = ReadRegistrationData(registration_id, origin, ®istration); | 851 status = ReadRegistrationData(registration_id, origin, ®istration); |
852 if (status != STATUS_OK) | 852 if (status != STATUS_OK) |
853 return status; | 853 return status; |
854 | 854 |
855 leveldb::WriteBatch batch; | 855 leveldb::WriteBatch batch; |
856 for (const auto& pair : name_value_pairs) { | 856 for (const auto& pair : name_value_pairs) { |
857 DCHECK(!pair.first.empty()); | 857 DCHECK(!pair.first.empty()); |
858 batch.Put(CreateUserDataKey(registration_id, pair.first), pair.second); | 858 batch.Put(CreateUserDataKey(registration_id, pair.first), pair.second); |
859 batch.Put(CreateHasUserDataKey(registration_id, pair.first), ""); | 859 batch.Put(CreateHasUserDataKey(registration_id, pair.first), ""); |
860 } | 860 } |
861 return WriteBatch(&batch); | 861 return WriteBatch(&batch); |
862 } | 862 } |
863 | 863 |
864 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteUserData( | 864 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteUserData( |
865 int64_t registration_id, | 865 int64_t registration_id, |
866 const std::vector<std::string>& user_data_names) { | 866 const std::vector<std::string>& user_data_names) { |
867 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 867 DCHECK(sequence_checker_.CalledOnValidSequence()); |
868 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 868 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
869 DCHECK(!user_data_names.empty()); | 869 DCHECK(!user_data_names.empty()); |
870 | 870 |
871 Status status = LazyOpen(false); | 871 Status status = LazyOpen(false); |
872 if (IsNewOrNonexistentDatabase(status)) | 872 if (IsNewOrNonexistentDatabase(status)) |
873 return STATUS_OK; | 873 return STATUS_OK; |
874 if (status != STATUS_OK) | 874 if (status != STATUS_OK) |
875 return status; | 875 return status; |
876 | 876 |
877 leveldb::WriteBatch batch; | 877 leveldb::WriteBatch batch; |
878 for (const std::string& name : user_data_names) { | 878 for (const std::string& name : user_data_names) { |
879 DCHECK(!name.empty()); | 879 DCHECK(!name.empty()); |
880 batch.Delete(CreateUserDataKey(registration_id, name)); | 880 batch.Delete(CreateUserDataKey(registration_id, name)); |
881 batch.Delete(CreateHasUserDataKey(registration_id, name)); | 881 batch.Delete(CreateHasUserDataKey(registration_id, name)); |
882 } | 882 } |
883 return WriteBatch(&batch); | 883 return WriteBatch(&batch); |
884 } | 884 } |
885 | 885 |
886 ServiceWorkerDatabase::Status | 886 ServiceWorkerDatabase::Status |
887 ServiceWorkerDatabase::ReadUserDataForAllRegistrations( | 887 ServiceWorkerDatabase::ReadUserDataForAllRegistrations( |
888 const std::string& user_data_name, | 888 const std::string& user_data_name, |
889 std::vector<std::pair<int64_t, std::string>>* user_data) { | 889 std::vector<std::pair<int64_t, std::string>>* user_data) { |
890 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 890 DCHECK(sequence_checker_.CalledOnValidSequence()); |
891 DCHECK(user_data->empty()); | 891 DCHECK(user_data->empty()); |
892 | 892 |
893 Status status = LazyOpen(false); | 893 Status status = LazyOpen(false); |
894 if (IsNewOrNonexistentDatabase(status)) | 894 if (IsNewOrNonexistentDatabase(status)) |
895 return STATUS_OK; | 895 return STATUS_OK; |
896 if (status != STATUS_OK) | 896 if (status != STATUS_OK) |
897 return status; | 897 return status; |
898 | 898 |
899 std::string key_prefix = CreateHasUserDataKeyPrefix(user_data_name); | 899 std::string key_prefix = CreateHasUserDataKeyPrefix(user_data_name); |
900 { | 900 { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 return status; | 977 return status; |
978 status = WriteResourceIdsInBatch(kPurgeableResIdKeyPrefix, ids, &batch); | 978 status = WriteResourceIdsInBatch(kPurgeableResIdKeyPrefix, ids, &batch); |
979 if (status != STATUS_OK) | 979 if (status != STATUS_OK) |
980 return status; | 980 return status; |
981 return WriteBatch(&batch); | 981 return WriteBatch(&batch); |
982 } | 982 } |
983 | 983 |
984 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteAllDataForOrigins( | 984 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteAllDataForOrigins( |
985 const std::set<GURL>& origins, | 985 const std::set<GURL>& origins, |
986 std::vector<int64_t>* newly_purgeable_resources) { | 986 std::vector<int64_t>* newly_purgeable_resources) { |
987 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 987 DCHECK(sequence_checker_.CalledOnValidSequence()); |
988 Status status = LazyOpen(false); | 988 Status status = LazyOpen(false); |
989 if (IsNewOrNonexistentDatabase(status)) | 989 if (IsNewOrNonexistentDatabase(status)) |
990 return STATUS_OK; | 990 return STATUS_OK; |
991 if (status != STATUS_OK) | 991 if (status != STATUS_OK) |
992 return status; | 992 return status; |
993 leveldb::WriteBatch batch; | 993 leveldb::WriteBatch batch; |
994 | 994 |
995 for (const GURL& origin : origins) { | 995 for (const GURL& origin : origins) { |
996 if (!origin.is_valid()) | 996 if (!origin.is_valid()) |
997 return STATUS_ERROR_FAILED; | 997 return STATUS_ERROR_FAILED; |
(...skipping 22 matching lines...) Expand all Loading... |
1020 status = DeleteUserDataForRegistration(data.registration_id, &batch); | 1020 status = DeleteUserDataForRegistration(data.registration_id, &batch); |
1021 if (status != STATUS_OK) | 1021 if (status != STATUS_OK) |
1022 return status; | 1022 return status; |
1023 } | 1023 } |
1024 } | 1024 } |
1025 | 1025 |
1026 return WriteBatch(&batch); | 1026 return WriteBatch(&batch); |
1027 } | 1027 } |
1028 | 1028 |
1029 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DestroyDatabase() { | 1029 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DestroyDatabase() { |
1030 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 1030 DCHECK(sequence_checker_.CalledOnValidSequence()); |
1031 Disable(FROM_HERE, STATUS_OK); | 1031 Disable(FROM_HERE, STATUS_OK); |
1032 | 1032 |
1033 if (IsDatabaseInMemory()) { | 1033 if (IsDatabaseInMemory()) { |
1034 env_.reset(); | 1034 env_.reset(); |
1035 return STATUS_OK; | 1035 return STATUS_OK; |
1036 } | 1036 } |
1037 | 1037 |
1038 // Directly delete the database directory instead of leveldb::DestroyDB() | 1038 // Directly delete the database directory instead of leveldb::DestroyDB() |
1039 // because the API does not delete the directory if there are unrelated files. | 1039 // because the API does not delete the directory if there are unrelated files. |
1040 // (https://code.google.com/p/chromium/issues/detail?id=468926#c24) | 1040 // (https://code.google.com/p/chromium/issues/detail?id=468926#c24) |
1041 Status status = base::DeleteFile(path_, true /* recursive */) | 1041 Status status = base::DeleteFile(path_, true /* recursive */) |
1042 ? STATUS_OK | 1042 ? STATUS_OK |
1043 : STATUS_ERROR_FAILED; | 1043 : STATUS_ERROR_FAILED; |
1044 ServiceWorkerMetrics::RecordDestroyDatabaseResult(status); | 1044 ServiceWorkerMetrics::RecordDestroyDatabaseResult(status); |
1045 return status; | 1045 return status; |
1046 } | 1046 } |
1047 | 1047 |
1048 ServiceWorkerDatabase::Status ServiceWorkerDatabase::LazyOpen( | 1048 ServiceWorkerDatabase::Status ServiceWorkerDatabase::LazyOpen( |
1049 bool create_if_missing) { | 1049 bool create_if_missing) { |
1050 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 1050 DCHECK(sequence_checker_.CalledOnValidSequence()); |
1051 | 1051 |
1052 // Do not try to open a database if we tried and failed once. | 1052 // Do not try to open a database if we tried and failed once. |
1053 if (state_ == DISABLED) | 1053 if (state_ == DISABLED) |
1054 return STATUS_ERROR_FAILED; | 1054 return STATUS_ERROR_FAILED; |
1055 if (IsOpen()) | 1055 if (IsOpen()) |
1056 return STATUS_OK; | 1056 return STATUS_OK; |
1057 | 1057 |
1058 if (!create_if_missing) { | 1058 if (!create_if_missing) { |
1059 // Avoid opening a database if it does not exist at the |path_|. | 1059 // Avoid opening a database if it does not exist at the |path_|. |
1060 if (IsDatabaseInMemory() || !base::PathExists(path_) || | 1060 if (IsDatabaseInMemory() || !base::PathExists(path_) || |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1390 } | 1390 } |
1391 } | 1391 } |
1392 | 1392 |
1393 HandleReadResult(FROM_HERE, status); | 1393 HandleReadResult(FROM_HERE, status); |
1394 return status; | 1394 return status; |
1395 } | 1395 } |
1396 | 1396 |
1397 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceIds( | 1397 ServiceWorkerDatabase::Status ServiceWorkerDatabase::ReadResourceIds( |
1398 const char* id_key_prefix, | 1398 const char* id_key_prefix, |
1399 std::set<int64_t>* ids) { | 1399 std::set<int64_t>* ids) { |
1400 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 1400 DCHECK(sequence_checker_.CalledOnValidSequence()); |
1401 DCHECK(id_key_prefix); | 1401 DCHECK(id_key_prefix); |
1402 DCHECK(ids->empty()); | 1402 DCHECK(ids->empty()); |
1403 | 1403 |
1404 Status status = LazyOpen(false); | 1404 Status status = LazyOpen(false); |
1405 if (IsNewOrNonexistentDatabase(status)) | 1405 if (IsNewOrNonexistentDatabase(status)) |
1406 return STATUS_OK; | 1406 return STATUS_OK; |
1407 if (status != STATUS_OK) | 1407 if (status != STATUS_OK) |
1408 return status; | 1408 return status; |
1409 | 1409 |
1410 { | 1410 { |
(...skipping 21 matching lines...) Expand all Loading... |
1432 } | 1432 } |
1433 | 1433 |
1434 HandleReadResult(FROM_HERE, status); | 1434 HandleReadResult(FROM_HERE, status); |
1435 return status; | 1435 return status; |
1436 } | 1436 } |
1437 | 1437 |
1438 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteResourceIdsInBatch( | 1438 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteResourceIdsInBatch( |
1439 const char* id_key_prefix, | 1439 const char* id_key_prefix, |
1440 const std::set<int64_t>& ids, | 1440 const std::set<int64_t>& ids, |
1441 leveldb::WriteBatch* batch) { | 1441 leveldb::WriteBatch* batch) { |
1442 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 1442 DCHECK(sequence_checker_.CalledOnValidSequence()); |
1443 DCHECK(id_key_prefix); | 1443 DCHECK(id_key_prefix); |
1444 | 1444 |
1445 Status status = LazyOpen(true); | 1445 Status status = LazyOpen(true); |
1446 if (status != STATUS_OK) | 1446 if (status != STATUS_OK) |
1447 return status; | 1447 return status; |
1448 | 1448 |
1449 if (ids.empty()) | 1449 if (ids.empty()) |
1450 return STATUS_OK; | 1450 return STATUS_OK; |
1451 for (std::set<int64_t>::const_iterator itr = ids.begin(); itr != ids.end(); | 1451 for (std::set<int64_t>::const_iterator itr = ids.begin(); itr != ids.end(); |
1452 ++itr) { | 1452 ++itr) { |
1453 // Value should be empty. | 1453 // Value should be empty. |
1454 batch->Put(CreateResourceIdKey(id_key_prefix, *itr), ""); | 1454 batch->Put(CreateResourceIdKey(id_key_prefix, *itr), ""); |
1455 } | 1455 } |
1456 // std::set is sorted, so the last element is the largest. | 1456 // std::set is sorted, so the last element is the largest. |
1457 BumpNextResourceIdIfNeeded(*ids.rbegin(), batch); | 1457 BumpNextResourceIdIfNeeded(*ids.rbegin(), batch); |
1458 return STATUS_OK; | 1458 return STATUS_OK; |
1459 } | 1459 } |
1460 | 1460 |
1461 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteResourceIdsInBatch( | 1461 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteResourceIdsInBatch( |
1462 const char* id_key_prefix, | 1462 const char* id_key_prefix, |
1463 const std::set<int64_t>& ids, | 1463 const std::set<int64_t>& ids, |
1464 leveldb::WriteBatch* batch) { | 1464 leveldb::WriteBatch* batch) { |
1465 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 1465 DCHECK(sequence_checker_.CalledOnValidSequence()); |
1466 DCHECK(id_key_prefix); | 1466 DCHECK(id_key_prefix); |
1467 | 1467 |
1468 Status status = LazyOpen(false); | 1468 Status status = LazyOpen(false); |
1469 if (IsNewOrNonexistentDatabase(status)) | 1469 if (IsNewOrNonexistentDatabase(status)) |
1470 return STATUS_OK; | 1470 return STATUS_OK; |
1471 if (status != STATUS_OK) | 1471 if (status != STATUS_OK) |
1472 return status; | 1472 return status; |
1473 | 1473 |
1474 for (std::set<int64_t>::const_iterator itr = ids.begin(); itr != ids.end(); | 1474 for (std::set<int64_t>::const_iterator itr = ids.begin(); itr != ids.end(); |
1475 ++itr) { | 1475 ++itr) { |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1622 if (status != STATUS_OK) | 1622 if (status != STATUS_OK) |
1623 Disable(from_here, status); | 1623 Disable(from_here, status); |
1624 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1624 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
1625 } | 1625 } |
1626 | 1626 |
1627 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { | 1627 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { |
1628 return path_.empty(); | 1628 return path_.empty(); |
1629 } | 1629 } |
1630 | 1630 |
1631 } // namespace content | 1631 } // namespace content |
OLD | NEW |