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 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 return status; | 796 return status; |
797 break; | 797 break; |
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::string& user_data_name, | 806 const std::vector<std::string>& user_data_names, |
807 std::string* user_data) { | 807 std::vector<std::string>* user_data_values) { |
808 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 808 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
809 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 809 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
810 DCHECK(!user_data_name.empty()); | 810 DCHECK(!user_data_names.empty()); |
811 DCHECK(user_data); | 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 |
819 const std::string key = CreateUserDataKey(registration_id, user_data_name); | 819 user_data_values->resize(user_data_names.size()); |
820 status = LevelDBStatusToStatus( | 820 for (size_t i = 0; i < user_data_names.size(); i++) { |
821 db_->Get(leveldb::ReadOptions(), key, user_data)); | 821 const std::string key = |
| 822 CreateUserDataKey(registration_id, user_data_names[i]); |
| 823 status = LevelDBStatusToStatus( |
| 824 db_->Get(leveldb::ReadOptions(), key, &(*user_data_values)[i])); |
| 825 if (status != STATUS_OK) { |
| 826 user_data_values->clear(); |
| 827 break; |
| 828 } |
| 829 } |
822 HandleReadResult(FROM_HERE, | 830 HandleReadResult(FROM_HERE, |
823 status == STATUS_ERROR_NOT_FOUND ? STATUS_OK : status); | 831 status == STATUS_ERROR_NOT_FOUND ? STATUS_OK : status); |
824 return status; | 832 return status; |
825 } | 833 } |
826 | 834 |
827 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteUserData( | 835 ServiceWorkerDatabase::Status ServiceWorkerDatabase::WriteUserData( |
828 int64_t registration_id, | 836 int64_t registration_id, |
829 const GURL& origin, | 837 const GURL& origin, |
830 const std::string& user_data_name, | 838 const std::vector<std::pair<std::string, std::string>>& name_value_pairs) { |
831 const std::string& user_data) { | |
832 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 839 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
833 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 840 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
834 DCHECK(!user_data_name.empty()); | 841 DCHECK(!name_value_pairs.empty()); |
835 | 842 |
836 Status status = LazyOpen(false); | 843 Status status = LazyOpen(false); |
837 if (IsNewOrNonexistentDatabase(status)) | 844 if (IsNewOrNonexistentDatabase(status)) |
838 return STATUS_ERROR_NOT_FOUND; | 845 return STATUS_ERROR_NOT_FOUND; |
839 if (status != STATUS_OK) | 846 if (status != STATUS_OK) |
840 return status; | 847 return status; |
841 | 848 |
842 // There should be the registration specified by |registration_id|. | 849 // There should be the registration specified by |registration_id|. |
843 RegistrationData registration; | 850 RegistrationData registration; |
844 status = ReadRegistrationData(registration_id, origin, ®istration); | 851 status = ReadRegistrationData(registration_id, origin, ®istration); |
845 if (status != STATUS_OK) | 852 if (status != STATUS_OK) |
846 return status; | 853 return status; |
847 | 854 |
848 leveldb::WriteBatch batch; | 855 leveldb::WriteBatch batch; |
849 batch.Put(CreateUserDataKey(registration_id, user_data_name), user_data); | 856 for (const auto& pair : name_value_pairs) { |
850 batch.Put(CreateHasUserDataKey(registration_id, user_data_name), ""); | 857 DCHECK(!pair.first.empty()); |
| 858 batch.Put(CreateUserDataKey(registration_id, pair.first), pair.second); |
| 859 batch.Put(CreateHasUserDataKey(registration_id, pair.first), ""); |
| 860 } |
851 return WriteBatch(&batch); | 861 return WriteBatch(&batch); |
852 } | 862 } |
853 | 863 |
854 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteUserData( | 864 ServiceWorkerDatabase::Status ServiceWorkerDatabase::DeleteUserData( |
855 int64_t registration_id, | 865 int64_t registration_id, |
856 const std::string& user_data_name) { | 866 const std::vector<std::string>& user_data_names) { |
857 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 867 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
858 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 868 DCHECK_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
859 DCHECK(!user_data_name.empty()); | 869 DCHECK(!user_data_names.empty()); |
860 | 870 |
861 Status status = LazyOpen(false); | 871 Status status = LazyOpen(false); |
862 if (IsNewOrNonexistentDatabase(status)) | 872 if (IsNewOrNonexistentDatabase(status)) |
863 return STATUS_OK; | 873 return STATUS_OK; |
864 if (status != STATUS_OK) | 874 if (status != STATUS_OK) |
865 return status; | 875 return status; |
866 | 876 |
867 leveldb::WriteBatch batch; | 877 leveldb::WriteBatch batch; |
868 batch.Delete(CreateUserDataKey(registration_id, user_data_name)); | 878 for (const std::string& name : user_data_names) { |
869 batch.Delete(CreateHasUserDataKey(registration_id, user_data_name)); | 879 DCHECK(!name.empty()); |
| 880 batch.Delete(CreateUserDataKey(registration_id, name)); |
| 881 batch.Delete(CreateHasUserDataKey(registration_id, name)); |
| 882 } |
870 return WriteBatch(&batch); | 883 return WriteBatch(&batch); |
871 } | 884 } |
872 | 885 |
873 ServiceWorkerDatabase::Status | 886 ServiceWorkerDatabase::Status |
874 ServiceWorkerDatabase::ReadUserDataForAllRegistrations( | 887 ServiceWorkerDatabase::ReadUserDataForAllRegistrations( |
875 const std::string& user_data_name, | 888 const std::string& user_data_name, |
876 std::vector<std::pair<int64_t, std::string>>* user_data) { | 889 std::vector<std::pair<int64_t, std::string>>* user_data) { |
877 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 890 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
878 DCHECK(user_data->empty()); | 891 DCHECK(user_data->empty()); |
879 | 892 |
(...skipping 729 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1609 if (status != STATUS_OK) | 1622 if (status != STATUS_OK) |
1610 Disable(from_here, status); | 1623 Disable(from_here, status); |
1611 ServiceWorkerMetrics::CountWriteDatabaseResult(status); | 1624 ServiceWorkerMetrics::CountWriteDatabaseResult(status); |
1612 } | 1625 } |
1613 | 1626 |
1614 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { | 1627 bool ServiceWorkerDatabase::IsDatabaseInMemory() const { |
1615 return path_.empty(); | 1628 return path_.empty(); |
1616 } | 1629 } |
1617 | 1630 |
1618 } // namespace content | 1631 } // namespace content |
OLD | NEW |