| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_storage.h" | 5 #include "content/browser/service_worker/service_worker_storage.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 scoped_refptr<ServiceWorkerRegistration>* registration) { | 516 scoped_refptr<ServiceWorkerRegistration>* registration) { |
| 517 bool was_called = false; | 517 bool was_called = false; |
| 518 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE; | 518 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE; |
| 519 storage()->FindRegistrationForIdOnly( | 519 storage()->FindRegistrationForIdOnly( |
| 520 registration_id, MakeFindCallback(&was_called, &result, registration)); | 520 registration_id, MakeFindCallback(&was_called, &result, registration)); |
| 521 base::RunLoop().RunUntilIdle(); | 521 base::RunLoop().RunUntilIdle(); |
| 522 EXPECT_TRUE(was_called); | 522 EXPECT_TRUE(was_called); |
| 523 return result; | 523 return result; |
| 524 } | 524 } |
| 525 | 525 |
| 526 // Directly writes a registration using |
| 527 // ServiceWorkerDatabase::WriteRegistration rather than |
| 528 // ServiceWorkerStorage::StoreRegistration. Useful for simulating a |
| 529 // registration written by an earlier version of Chrome. |
| 530 void WriteRegistration(const RegistrationData& registration, |
| 531 const std::vector<ResourceRecord>& resources) { |
| 532 ServiceWorkerDatabase::RegistrationData deleted_version; |
| 533 std::vector<int64_t> newly_purgeable_resources; |
| 534 |
| 535 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 536 storage()->database_->WriteRegistration( |
| 537 registration, resources, &deleted_version, |
| 538 &newly_purgeable_resources)); |
| 539 } |
| 540 |
| 526 // user_data_directory_ must be declared first to preserve destructor order. | 541 // user_data_directory_ must be declared first to preserve destructor order. |
| 527 base::ScopedTempDir user_data_directory_; | 542 base::ScopedTempDir user_data_directory_; |
| 528 base::FilePath user_data_directory_path_; | 543 base::FilePath user_data_directory_path_; |
| 529 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 544 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 530 TestBrowserThreadBundle browser_thread_bundle_; | 545 TestBrowserThreadBundle browser_thread_bundle_; |
| 531 }; | 546 }; |
| 532 | 547 |
| 533 class ServiceWorkerStorageTestP | 548 class ServiceWorkerStorageTestP |
| 534 : public MojoServiceWorkerTestP<ServiceWorkerStorageTest> {}; | 549 : public MojoServiceWorkerTestP<ServiceWorkerStorageTest> {}; |
| 535 | 550 |
| (...skipping 1124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1660 live_registration2.get(), NULL, SERVICE_WORKER_OK); | 1675 live_registration2.get(), NULL, SERVICE_WORKER_OK); |
| 1661 storage()->NotifyDoneInstallingRegistration( | 1676 storage()->NotifyDoneInstallingRegistration( |
| 1662 live_registration3.get(), NULL, SERVICE_WORKER_OK); | 1677 live_registration3.get(), NULL, SERVICE_WORKER_OK); |
| 1663 | 1678 |
| 1664 // Find a registration among installed ones. | 1679 // Find a registration among installed ones. |
| 1665 EXPECT_EQ(SERVICE_WORKER_OK, | 1680 EXPECT_EQ(SERVICE_WORKER_OK, |
| 1666 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 1681 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
| 1667 EXPECT_EQ(live_registration2, found_registration); | 1682 EXPECT_EQ(live_registration2, found_registration); |
| 1668 } | 1683 } |
| 1669 | 1684 |
| 1685 // Test fixture that uses disk storage, rather than memory. Useful for tests |
| 1686 // that test persistence by simulating browser shutdown and restart. |
| 1670 class ServiceWorkerStorageDiskTest : public ServiceWorkerStorageTestP { | 1687 class ServiceWorkerStorageDiskTest : public ServiceWorkerStorageTestP { |
| 1671 public: | 1688 public: |
| 1672 void SetUp() override { | 1689 void SetUp() override { |
| 1673 ASSERT_TRUE(InitUserDataDirectory()); | 1690 ASSERT_TRUE(InitUserDataDirectory()); |
| 1674 ServiceWorkerStorageTestP::SetUp(); | 1691 ServiceWorkerStorageTestP::SetUp(); |
| 1675 } | 1692 } |
| 1676 }; | 1693 }; |
| 1677 | 1694 |
| 1678 TEST_P(ServiceWorkerStorageDiskTest, OriginHasForeignFetchRegistrations) { | 1695 TEST_P(ServiceWorkerStorageDiskTest, OriginHasForeignFetchRegistrations) { |
| 1679 LazyInitialize(); | 1696 LazyInitialize(); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1779 | 1796 |
| 1780 // Remove other registration at first origin. | 1797 // Remove other registration at first origin. |
| 1781 EXPECT_EQ(SERVICE_WORKER_OK, | 1798 EXPECT_EQ(SERVICE_WORKER_OK, |
| 1782 DeleteRegistration(kRegistrationId2, kScope2.GetOrigin())); | 1799 DeleteRegistration(kRegistrationId2, kScope2.GetOrigin())); |
| 1783 | 1800 |
| 1784 // No foreign fetch registrations remain. | 1801 // No foreign fetch registrations remain. |
| 1785 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1)); | 1802 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1)); |
| 1786 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2)); | 1803 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2)); |
| 1787 } | 1804 } |
| 1788 | 1805 |
| 1789 class ServiceWorkerStorageOriginTrialsTest : public ServiceWorkerStorageTestP { | 1806 TEST_P(ServiceWorkerStorageTestP, OriginTrialsAbsentEntryAndEmptyEntry) { |
| 1790 public: | |
| 1791 ServiceWorkerStorageOriginTrialsTest() {} | |
| 1792 ~ServiceWorkerStorageOriginTrialsTest() override {} | |
| 1793 | |
| 1794 protected: | |
| 1795 void WriteRegistration(const RegistrationData& registration, | |
| 1796 const std::vector<ResourceRecord>& resources) { | |
| 1797 ServiceWorkerDatabase::RegistrationData deleted_version; | |
| 1798 std::vector<int64_t> newly_purgeable_resources; | |
| 1799 | |
| 1800 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK, | |
| 1801 storage()->database_->WriteRegistration( | |
| 1802 registration, resources, &deleted_version, | |
| 1803 &newly_purgeable_resources)); | |
| 1804 } | |
| 1805 }; | |
| 1806 | |
| 1807 TEST_P(ServiceWorkerStorageOriginTrialsTest, AbsentEntryAndEmptyEntry) { | |
| 1808 const GURL origin1("http://www1.example.com"); | 1807 const GURL origin1("http://www1.example.com"); |
| 1809 const GURL scope1("http://www1.example.com/foo/"); | 1808 const GURL scope1("http://www1.example.com/foo/"); |
| 1810 RegistrationData data1; | 1809 RegistrationData data1; |
| 1811 data1.registration_id = 100; | 1810 data1.registration_id = 100; |
| 1812 data1.scope = scope1; | 1811 data1.scope = scope1; |
| 1813 data1.script = GURL(origin1.spec() + "/script.js"); | 1812 data1.script = GURL(origin1.spec() + "/script.js"); |
| 1814 data1.version_id = 1000; | 1813 data1.version_id = 1000; |
| 1815 data1.is_active = true; | 1814 data1.is_active = true; |
| 1816 data1.resources_total_size_bytes = 100; | 1815 data1.resources_total_size_bytes = 100; |
| 1817 // Don't set origin_trial_tokens to simulate old database entry. | 1816 // Don't set origin_trial_tokens to simulate old database entry. |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1979 const TrialTokenValidator::FeatureToTokensMap& found_tokens = | 1978 const TrialTokenValidator::FeatureToTokensMap& found_tokens = |
| 1980 *found_registration->active_version()->origin_trial_tokens(); | 1979 *found_registration->active_version()->origin_trial_tokens(); |
| 1981 ASSERT_EQ(2UL, found_tokens.size()); | 1980 ASSERT_EQ(2UL, found_tokens.size()); |
| 1982 ASSERT_EQ(1UL, found_tokens.at("Feature1").size()); | 1981 ASSERT_EQ(1UL, found_tokens.at("Feature1").size()); |
| 1983 EXPECT_EQ(kFeature1Token, found_tokens.at("Feature1")[0]); | 1982 EXPECT_EQ(kFeature1Token, found_tokens.at("Feature1")[0]); |
| 1984 ASSERT_EQ(2UL, found_tokens.at("Feature2").size()); | 1983 ASSERT_EQ(2UL, found_tokens.at("Feature2").size()); |
| 1985 EXPECT_EQ(kFeature2Token1, found_tokens.at("Feature2")[0]); | 1984 EXPECT_EQ(kFeature2Token1, found_tokens.at("Feature2")[0]); |
| 1986 EXPECT_EQ(kFeature2Token2, found_tokens.at("Feature2")[1]); | 1985 EXPECT_EQ(kFeature2Token2, found_tokens.at("Feature2")[1]); |
| 1987 } | 1986 } |
| 1988 | 1987 |
| 1988 // Tests loading a registration that has no navigation preload state. |
| 1989 TEST_P(ServiceWorkerStorageTestP, AbsentNavigationPreloadState) { |
| 1990 const GURL origin1("http://www1.example.com"); |
| 1991 const GURL scope1("http://www1.example.com/foo/"); |
| 1992 RegistrationData data1; |
| 1993 data1.registration_id = 100; |
| 1994 data1.scope = scope1; |
| 1995 data1.script = GURL(origin1.spec() + "/script.js"); |
| 1996 data1.version_id = 1000; |
| 1997 data1.is_active = true; |
| 1998 data1.resources_total_size_bytes = 100; |
| 1999 // Don't set navigation preload state to simulate old database entry. |
| 2000 std::vector<ServiceWorkerDatabase::ResourceRecord> resources1; |
| 2001 resources1.push_back( |
| 2002 ServiceWorkerDatabase::ResourceRecord(1, data1.script, 100)); |
| 2003 WriteRegistration(data1, resources1); |
| 2004 |
| 2005 scoped_refptr<ServiceWorkerRegistration> found_registration; |
| 2006 EXPECT_EQ(SERVICE_WORKER_OK, |
| 2007 FindRegistrationForDocument(scope1, &found_registration)); |
| 2008 const NavigationPreloadState& registration_state = |
| 2009 found_registration->navigation_preload_state(); |
| 2010 EXPECT_FALSE(registration_state.enabled); |
| 2011 EXPECT_EQ("true", registration_state.header); |
| 2012 ASSERT_TRUE(found_registration->active_version()); |
| 2013 const NavigationPreloadState& state = |
| 2014 found_registration->active_version()->navigation_preload_state(); |
| 2015 EXPECT_FALSE(state.enabled); |
| 2016 EXPECT_EQ("true", state.header); |
| 2017 } |
| 2018 |
| 2019 // Tests loading a registration with a disabled navigation preload |
| 2020 // state. |
| 2021 TEST_P(ServiceWorkerStorageDiskTest, DisabledNavigationPreloadState) { |
| 2022 LazyInitialize(); |
| 2023 const GURL kScope("https://valid.example.com/scope"); |
| 2024 const GURL kScript("https://valid.example.com/script.js"); |
| 2025 const int64_t kRegistrationId = 1; |
| 2026 const int64_t kVersionId = 1; |
| 2027 scoped_refptr<ServiceWorkerRegistration> registration = |
| 2028 new ServiceWorkerRegistration(kScope, kRegistrationId, |
| 2029 context()->AsWeakPtr()); |
| 2030 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion( |
| 2031 registration.get(), kScript, kVersionId, context()->AsWeakPtr()); |
| 2032 |
| 2033 std::vector<ServiceWorkerDatabase::ResourceRecord> record; |
| 2034 record.push_back(ServiceWorkerDatabase::ResourceRecord(1, kScript, 100)); |
| 2035 version->script_cache_map()->SetResources(record); |
| 2036 version->set_fetch_handler_existence( |
| 2037 ServiceWorkerVersion::FetchHandlerExistence::EXISTS); |
| 2038 version->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 2039 registration->SetActiveVersion(version); |
| 2040 registration->EnableNavigationPreload(false); |
| 2041 |
| 2042 EXPECT_EQ(SERVICE_WORKER_OK, StoreRegistration(registration, version)); |
| 2043 |
| 2044 // Simulate browser shutdown and restart. |
| 2045 registration = nullptr; |
| 2046 version = nullptr; |
| 2047 InitializeTestHelper(); |
| 2048 LazyInitialize(); |
| 2049 |
| 2050 scoped_refptr<ServiceWorkerRegistration> found_registration; |
| 2051 EXPECT_EQ(SERVICE_WORKER_OK, |
| 2052 FindRegistrationForDocument(kScope, &found_registration)); |
| 2053 const NavigationPreloadState& registration_state = |
| 2054 found_registration->navigation_preload_state(); |
| 2055 EXPECT_FALSE(registration_state.enabled); |
| 2056 EXPECT_EQ("true", registration_state.header); |
| 2057 ASSERT_TRUE(found_registration->active_version()); |
| 2058 const NavigationPreloadState& state = |
| 2059 found_registration->active_version()->navigation_preload_state(); |
| 2060 EXPECT_FALSE(state.enabled); |
| 2061 EXPECT_EQ("true", state.header); |
| 2062 } |
| 2063 |
| 2064 // Tests loading a registration with an enabled navigation preload state, as |
| 2065 // well as a custom header value. |
| 2066 TEST_P(ServiceWorkerStorageDiskTest, EnabledNavigationPreloadState) { |
| 2067 LazyInitialize(); |
| 2068 const GURL kScope("https://valid.example.com/scope"); |
| 2069 const GURL kScript("https://valid.example.com/script.js"); |
| 2070 const std::string kHeaderValue("custom header value"); |
| 2071 const int64_t kRegistrationId = 1; |
| 2072 const int64_t kVersionId = 1; |
| 2073 scoped_refptr<ServiceWorkerRegistration> registration = |
| 2074 new ServiceWorkerRegistration(kScope, kRegistrationId, |
| 2075 context()->AsWeakPtr()); |
| 2076 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion( |
| 2077 registration.get(), kScript, kVersionId, context()->AsWeakPtr()); |
| 2078 |
| 2079 std::vector<ServiceWorkerDatabase::ResourceRecord> record; |
| 2080 record.push_back(ServiceWorkerDatabase::ResourceRecord(1, kScript, 100)); |
| 2081 version->script_cache_map()->SetResources(record); |
| 2082 version->set_fetch_handler_existence( |
| 2083 ServiceWorkerVersion::FetchHandlerExistence::EXISTS); |
| 2084 version->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 2085 registration->SetActiveVersion(version); |
| 2086 registration->EnableNavigationPreload(true); |
| 2087 registration->SetNavigationPreloadHeader(kHeaderValue); |
| 2088 |
| 2089 EXPECT_EQ(SERVICE_WORKER_OK, StoreRegistration(registration, version)); |
| 2090 |
| 2091 // Simulate browser shutdown and restart. |
| 2092 registration = nullptr; |
| 2093 version = nullptr; |
| 2094 InitializeTestHelper(); |
| 2095 LazyInitialize(); |
| 2096 |
| 2097 scoped_refptr<ServiceWorkerRegistration> found_registration; |
| 2098 EXPECT_EQ(SERVICE_WORKER_OK, |
| 2099 FindRegistrationForDocument(kScope, &found_registration)); |
| 2100 const NavigationPreloadState& registration_state = |
| 2101 found_registration->navigation_preload_state(); |
| 2102 EXPECT_TRUE(registration_state.enabled); |
| 2103 EXPECT_EQ(kHeaderValue, registration_state.header); |
| 2104 ASSERT_TRUE(found_registration->active_version()); |
| 2105 const NavigationPreloadState& state = |
| 2106 found_registration->active_version()->navigation_preload_state(); |
| 2107 EXPECT_TRUE(state.enabled); |
| 2108 EXPECT_EQ(kHeaderValue, state.header); |
| 2109 } |
| 2110 |
| 1989 INSTANTIATE_TEST_CASE_P(ServiceWorkerResourceStorageDiskTest, | 2111 INSTANTIATE_TEST_CASE_P(ServiceWorkerResourceStorageDiskTest, |
| 1990 ServiceWorkerResourceStorageDiskTest, | 2112 ServiceWorkerResourceStorageDiskTest, |
| 1991 testing::Bool()); | 2113 testing::Bool()); |
| 1992 INSTANTIATE_TEST_CASE_P(ServiceWorkerResourceStorageTest, | 2114 INSTANTIATE_TEST_CASE_P(ServiceWorkerResourceStorageTest, |
| 1993 ServiceWorkerResourceStorageTest, | 2115 ServiceWorkerResourceStorageTest, |
| 1994 testing::Bool()); | 2116 testing::Bool()); |
| 1995 INSTANTIATE_TEST_CASE_P(ServiceWorkerStorageDiskTest, | 2117 INSTANTIATE_TEST_CASE_P(ServiceWorkerStorageDiskTest, |
| 1996 ServiceWorkerStorageDiskTest, | 2118 ServiceWorkerStorageDiskTest, |
| 1997 testing::Bool()); | 2119 testing::Bool()); |
| 1998 INSTANTIATE_TEST_CASE_P(ServiceWorkerStorageOriginTrialsDiskTest, | 2120 INSTANTIATE_TEST_CASE_P(ServiceWorkerStorageOriginTrialsDiskTest, |
| 1999 ServiceWorkerStorageOriginTrialsDiskTest, | 2121 ServiceWorkerStorageOriginTrialsDiskTest, |
| 2000 testing::Bool()); | 2122 testing::Bool()); |
| 2001 INSTANTIATE_TEST_CASE_P(ServiceWorkerStorageOriginTrialsTest, | |
| 2002 ServiceWorkerStorageOriginTrialsTest, | |
| 2003 testing::Bool()); | |
| 2004 INSTANTIATE_TEST_CASE_P(ServiceWorkerStorageTestP, | 2123 INSTANTIATE_TEST_CASE_P(ServiceWorkerStorageTestP, |
| 2005 ServiceWorkerStorageTestP, | 2124 ServiceWorkerStorageTestP, |
| 2006 testing::Bool()); | 2125 testing::Bool()); |
| 2007 | 2126 |
| 2008 } // namespace content | 2127 } // namespace content |
| OLD | NEW |