| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "content/browser/service_worker/service_worker_database.pb.h" | 17 #include "content/browser/service_worker/service_worker_database.pb.h" |
| 18 #include "content/common/service_worker/service_worker_types.h" | 18 #include "content/common/service_worker/service_worker_types.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 20 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 21 #include "url/origin.h" |
| 21 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 typedef ServiceWorkerDatabase::RegistrationData RegistrationData; | 27 typedef ServiceWorkerDatabase::RegistrationData RegistrationData; |
| 27 typedef ServiceWorkerDatabase::ResourceRecord Resource; | 28 typedef ServiceWorkerDatabase::ResourceRecord Resource; |
| 28 | 29 |
| 29 struct AvailableIds { | 30 struct AvailableIds { |
| 30 int64_t reg_id; | 31 int64_t reg_id; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 EXPECT_EQ(expected.registration_id, actual.registration_id); | 64 EXPECT_EQ(expected.registration_id, actual.registration_id); |
| 64 EXPECT_EQ(expected.scope, actual.scope); | 65 EXPECT_EQ(expected.scope, actual.scope); |
| 65 EXPECT_EQ(expected.script, actual.script); | 66 EXPECT_EQ(expected.script, actual.script); |
| 66 EXPECT_EQ(expected.version_id, actual.version_id); | 67 EXPECT_EQ(expected.version_id, actual.version_id); |
| 67 EXPECT_EQ(expected.is_active, actual.is_active); | 68 EXPECT_EQ(expected.is_active, actual.is_active); |
| 68 EXPECT_EQ(expected.has_fetch_handler, actual.has_fetch_handler); | 69 EXPECT_EQ(expected.has_fetch_handler, actual.has_fetch_handler); |
| 69 EXPECT_EQ(expected.last_update_check, actual.last_update_check); | 70 EXPECT_EQ(expected.last_update_check, actual.last_update_check); |
| 70 EXPECT_EQ(expected.resources_total_size_bytes, | 71 EXPECT_EQ(expected.resources_total_size_bytes, |
| 71 actual.resources_total_size_bytes); | 72 actual.resources_total_size_bytes); |
| 72 EXPECT_EQ(expected.foreign_fetch_scopes, actual.foreign_fetch_scopes); | 73 EXPECT_EQ(expected.foreign_fetch_scopes, actual.foreign_fetch_scopes); |
| 74 EXPECT_EQ(expected.foreign_fetch_origins, actual.foreign_fetch_origins); |
| 73 } | 75 } |
| 74 | 76 |
| 75 void VerifyResourceRecords(const std::vector<Resource>& expected, | 77 void VerifyResourceRecords(const std::vector<Resource>& expected, |
| 76 const std::vector<Resource>& actual) { | 78 const std::vector<Resource>& actual) { |
| 77 ASSERT_EQ(expected.size(), actual.size()); | 79 ASSERT_EQ(expected.size(), actual.size()); |
| 78 for (size_t i = 0; i < expected.size(); ++i) { | 80 for (size_t i = 0; i < expected.size(); ++i) { |
| 79 EXPECT_EQ(expected[i].resource_id, actual[i].resource_id); | 81 EXPECT_EQ(expected[i].resource_id, actual[i].resource_id); |
| 80 EXPECT_EQ(expected[i].url, actual[i].url); | 82 EXPECT_EQ(expected[i].url, actual[i].url); |
| 81 EXPECT_EQ(expected[i].size_bytes, actual[i].size_bytes); | 83 EXPECT_EQ(expected[i].size_bytes, actual[i].size_bytes); |
| 82 } | 84 } |
| (...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); | 753 scoped_ptr<ServiceWorkerDatabase> database(CreateDatabaseInMemory()); |
| 752 | 754 |
| 753 GURL origin("http://example.com"); | 755 GURL origin("http://example.com"); |
| 754 RegistrationData data; | 756 RegistrationData data; |
| 755 data.registration_id = 100; | 757 data.registration_id = 100; |
| 756 data.scope = URL(origin, "/foo"); | 758 data.scope = URL(origin, "/foo"); |
| 757 data.script = URL(origin, "/script.js"); | 759 data.script = URL(origin, "/script.js"); |
| 758 data.version_id = 200; | 760 data.version_id = 200; |
| 759 data.resources_total_size_bytes = 10 + 11; | 761 data.resources_total_size_bytes = 10 + 11; |
| 760 data.foreign_fetch_scopes.push_back(URL(origin, "/foo")); | 762 data.foreign_fetch_scopes.push_back(URL(origin, "/foo")); |
| 763 data.foreign_fetch_origins.push_back( |
| 764 url::Origin(GURL("https://chromium.org"))); |
| 761 | 765 |
| 762 std::vector<Resource> resources1; | 766 std::vector<Resource> resources1; |
| 763 resources1.push_back(CreateResource(1, URL(origin, "/resource1"), 10)); | 767 resources1.push_back(CreateResource(1, URL(origin, "/resource1"), 10)); |
| 764 resources1.push_back(CreateResource(2, URL(origin, "/resource2"), 11)); | 768 resources1.push_back(CreateResource(2, URL(origin, "/resource2"), 11)); |
| 765 | 769 |
| 766 ServiceWorkerDatabase::RegistrationData deleted_version; | 770 ServiceWorkerDatabase::RegistrationData deleted_version; |
| 767 deleted_version.version_id = 222; // Dummy inital value | 771 deleted_version.version_id = 222; // Dummy inital value |
| 768 std::vector<int64_t> newly_purgeable_resources; | 772 std::vector<int64_t> newly_purgeable_resources; |
| 769 | 773 |
| 770 EXPECT_EQ( | 774 EXPECT_EQ( |
| 771 ServiceWorkerDatabase::STATUS_OK, | 775 ServiceWorkerDatabase::STATUS_OK, |
| 772 database->WriteRegistration( | 776 database->WriteRegistration( |
| 773 data, resources1, &deleted_version, &newly_purgeable_resources)); | 777 data, resources1, &deleted_version, &newly_purgeable_resources)); |
| 774 EXPECT_EQ(kInvalidServiceWorkerVersionId, deleted_version.version_id); | 778 EXPECT_EQ(kInvalidServiceWorkerVersionId, deleted_version.version_id); |
| 775 EXPECT_TRUE(newly_purgeable_resources.empty()); | 779 EXPECT_TRUE(newly_purgeable_resources.empty()); |
| 776 | 780 |
| 777 // Make sure that the registration and resource records are stored. | 781 // Make sure that the registration and resource records are stored. |
| 778 RegistrationData data_out; | 782 RegistrationData data_out; |
| 779 std::vector<Resource> resources_out; | 783 std::vector<Resource> resources_out; |
| 780 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->ReadRegistration( | 784 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, database->ReadRegistration( |
| 781 data.registration_id, origin, &data_out, &resources_out)); | 785 data.registration_id, origin, &data_out, &resources_out)); |
| 782 VerifyRegistrationData(data, data_out); | 786 VerifyRegistrationData(data, data_out); |
| 783 VerifyResourceRecords(resources1, resources_out); | 787 VerifyResourceRecords(resources1, resources_out); |
| 784 | 788 |
| 785 // Update the registration. | 789 // Update the registration. |
| 786 RegistrationData updated_data = data; | 790 RegistrationData updated_data = data; |
| 787 updated_data.version_id = data.version_id + 1; | 791 updated_data.version_id = data.version_id + 1; |
| 788 updated_data.resources_total_size_bytes = 12 + 13; | 792 updated_data.resources_total_size_bytes = 12 + 13; |
| 789 updated_data.foreign_fetch_scopes.clear(); | 793 updated_data.foreign_fetch_scopes.clear(); |
| 794 updated_data.foreign_fetch_origins.push_back( |
| 795 url::Origin(GURL("https://example.com"))); |
| 790 std::vector<Resource> resources2; | 796 std::vector<Resource> resources2; |
| 791 resources2.push_back(CreateResource(3, URL(origin, "/resource3"), 12)); | 797 resources2.push_back(CreateResource(3, URL(origin, "/resource3"), 12)); |
| 792 resources2.push_back(CreateResource(4, URL(origin, "/resource4"), 13)); | 798 resources2.push_back(CreateResource(4, URL(origin, "/resource4"), 13)); |
| 793 | 799 |
| 794 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 800 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 795 database->WriteRegistration(updated_data, | 801 database->WriteRegistration(updated_data, |
| 796 resources2, | 802 resources2, |
| 797 &deleted_version, | 803 &deleted_version, |
| 798 &newly_purgeable_resources)); | 804 &newly_purgeable_resources)); |
| 799 EXPECT_EQ(data.version_id, deleted_version.version_id); | 805 EXPECT_EQ(data.version_id, deleted_version.version_id); |
| (...skipping 917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1717 &deleted_version, | 1723 &deleted_version, |
| 1718 &newly_purgeable_resources)); | 1724 &newly_purgeable_resources)); |
| 1719 | 1725 |
| 1720 origins.clear(); | 1726 origins.clear(); |
| 1721 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 1727 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 1722 database->GetOriginsWithForeignFetchRegistrations(&origins)); | 1728 database->GetOriginsWithForeignFetchRegistrations(&origins)); |
| 1723 EXPECT_EQ(0U, origins.size()); | 1729 EXPECT_EQ(0U, origins.size()); |
| 1724 } | 1730 } |
| 1725 | 1731 |
| 1726 } // namespace content | 1732 } // namespace content |
| OLD | NEW |