| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "content/browser/browser_thread_impl.h" | 11 #include "content/browser/browser_thread_impl.h" |
| 12 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
| 13 #include "content/browser/service_worker/service_worker_disk_cache.h" | 13 #include "content/browser/service_worker/service_worker_disk_cache.h" |
| 14 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
| 15 #include "content/browser/service_worker/service_worker_storage.h" | 15 #include "content/browser/service_worker/service_worker_storage.h" |
| 16 #include "content/browser/service_worker/service_worker_utils.h" | 16 #include "content/browser/service_worker/service_worker_utils.h" |
| 17 #include "content/browser/service_worker/service_worker_version.h" | 17 #include "content/browser/service_worker/service_worker_version.h" |
| 18 #include "content/common/service_worker/service_worker_status_code.h" | 18 #include "content/common/service_worker/service_worker_status_code.h" |
| 19 #include "content/public/test/mock_special_storage_policy.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
| 21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 22 #include "net/base/test_completion_callback.h" | 23 #include "net/base/test_completion_callback.h" |
| 23 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 using net::IOBuffer; | 27 using net::IOBuffer; |
| 27 using net::TestCompletionCallback; | 28 using net::TestCompletionCallback; |
| 28 using net::WrappedIOBuffer; | 29 using net::WrappedIOBuffer; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 186 |
| 186 } // namespace | 187 } // namespace |
| 187 | 188 |
| 188 class ServiceWorkerStorageTest : public testing::Test { | 189 class ServiceWorkerStorageTest : public testing::Test { |
| 189 public: | 190 public: |
| 190 ServiceWorkerStorageTest() | 191 ServiceWorkerStorageTest() |
| 191 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { | 192 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { |
| 192 } | 193 } |
| 193 | 194 |
| 194 virtual void SetUp() OVERRIDE { | 195 virtual void SetUp() OVERRIDE { |
| 196 special_storage_policy_ = new MockSpecialStoragePolicy(); |
| 195 context_.reset( | 197 context_.reset( |
| 196 new ServiceWorkerContextCore(GetUserDataDirectory(), | 198 new ServiceWorkerContextCore(GetUserDataDirectory(), |
| 197 base::ThreadTaskRunnerHandle::Get(), | 199 base::ThreadTaskRunnerHandle::Get(), |
| 198 base::ThreadTaskRunnerHandle::Get(), | 200 base::ThreadTaskRunnerHandle::Get(), |
| 199 base::ThreadTaskRunnerHandle::Get(), | 201 base::ThreadTaskRunnerHandle::Get(), |
| 200 NULL, | 202 NULL, |
| 203 special_storage_policy_.get(), |
| 201 NULL, | 204 NULL, |
| 202 NULL)); | 205 NULL)); |
| 203 context_ptr_ = context_->AsWeakPtr(); | 206 context_ptr_ = context_->AsWeakPtr(); |
| 204 } | 207 } |
| 205 | 208 |
| 206 virtual void TearDown() OVERRIDE { | 209 virtual void TearDown() OVERRIDE { |
| 207 context_.reset(); | 210 context_.reset(); |
| 208 } | 211 } |
| 209 | 212 |
| 210 virtual base::FilePath GetUserDataDirectory() { return base::FilePath(); } | 213 virtual base::FilePath GetUserDataDirectory() { return base::FilePath(); } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 storage()->FindRegistrationForId( | 316 storage()->FindRegistrationForId( |
| 314 registration_id, origin, | 317 registration_id, origin, |
| 315 MakeFindCallback(&was_called, &result, registration)); | 318 MakeFindCallback(&was_called, &result, registration)); |
| 316 base::RunLoop().RunUntilIdle(); | 319 base::RunLoop().RunUntilIdle(); |
| 317 EXPECT_TRUE(was_called); | 320 EXPECT_TRUE(was_called); |
| 318 return result; | 321 return result; |
| 319 } | 322 } |
| 320 | 323 |
| 321 scoped_ptr<ServiceWorkerContextCore> context_; | 324 scoped_ptr<ServiceWorkerContextCore> context_; |
| 322 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; | 325 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; |
| 326 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; |
| 323 TestBrowserThreadBundle browser_thread_bundle_; | 327 TestBrowserThreadBundle browser_thread_bundle_; |
| 324 }; | 328 }; |
| 325 | 329 |
| 326 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { | 330 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { |
| 327 const GURL kScope("http://www.test.not/scope/"); | 331 const GURL kScope("http://www.test.not/scope/"); |
| 328 const GURL kScript("http://www.test.not/script.js"); | 332 const GURL kScript("http://www.test.not/script.js"); |
| 329 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); | 333 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); |
| 330 const int64 kRegistrationId = 0; | 334 const int64 kRegistrationId = 0; |
| 331 const int64 kVersionId = 0; | 335 const int64 kVersionId = 0; |
| 332 const base::Time kToday = base::Time::Now(); | 336 const base::Time kToday = base::Time::Now(); |
| (...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 | 813 |
| 810 // Simulate browser shutdown. The purgeable and uncommitted resources are now | 814 // Simulate browser shutdown. The purgeable and uncommitted resources are now |
| 811 // stale. | 815 // stale. |
| 812 context_.reset(); | 816 context_.reset(); |
| 813 context_.reset( | 817 context_.reset( |
| 814 new ServiceWorkerContextCore(GetUserDataDirectory(), | 818 new ServiceWorkerContextCore(GetUserDataDirectory(), |
| 815 base::ThreadTaskRunnerHandle::Get(), | 819 base::ThreadTaskRunnerHandle::Get(), |
| 816 base::ThreadTaskRunnerHandle::Get(), | 820 base::ThreadTaskRunnerHandle::Get(), |
| 817 base::ThreadTaskRunnerHandle::Get(), | 821 base::ThreadTaskRunnerHandle::Get(), |
| 818 NULL, | 822 NULL, |
| 823 special_storage_policy_.get(), |
| 819 NULL, | 824 NULL, |
| 820 NULL)); | 825 NULL)); |
| 821 storage()->LazyInitialize(base::Bind(&base::DoNothing)); | 826 storage()->LazyInitialize(base::Bind(&base::DoNothing)); |
| 822 base::RunLoop().RunUntilIdle(); | 827 base::RunLoop().RunUntilIdle(); |
| 823 | 828 |
| 824 // Store a new uncommitted resource. This triggers stale resource cleanup. | 829 // Store a new uncommitted resource. This triggers stale resource cleanup. |
| 825 int64 kNewResourceId = storage()->NewResourceId(); | 830 int64 kNewResourceId = storage()->NewResourceId(); |
| 826 WriteBasicResponse(storage(), kNewResourceId); | 831 WriteBasicResponse(storage(), kNewResourceId); |
| 827 storage()->StoreUncommittedResponseId(kNewResourceId); | 832 storage()->StoreUncommittedResponseId(kNewResourceId); |
| 828 base::RunLoop().RunUntilIdle(); | 833 base::RunLoop().RunUntilIdle(); |
| 829 | 834 |
| 830 // The stale resources should be purged, but the new resource should persist. | 835 // The stale resources should be purged, but the new resource should persist. |
| 831 verify_ids.clear(); | 836 verify_ids.clear(); |
| 832 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 837 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 833 storage()->database_->GetUncommittedResourceIds(&verify_ids)); | 838 storage()->database_->GetUncommittedResourceIds(&verify_ids)); |
| 834 ASSERT_EQ(1u, verify_ids.size()); | 839 ASSERT_EQ(1u, verify_ids.size()); |
| 835 EXPECT_EQ(kNewResourceId, *verify_ids.begin()); | 840 EXPECT_EQ(kNewResourceId, *verify_ids.begin()); |
| 836 | 841 |
| 837 verify_ids.clear(); | 842 verify_ids.clear(); |
| 838 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, | 843 EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK, |
| 839 storage()->database_->GetPurgeableResourceIds(&verify_ids)); | 844 storage()->database_->GetPurgeableResourceIds(&verify_ids)); |
| 840 EXPECT_TRUE(verify_ids.empty()); | 845 EXPECT_TRUE(verify_ids.empty()); |
| 841 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); | 846 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id1_, false)); |
| 842 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); | 847 EXPECT_FALSE(VerifyBasicResponse(storage(), resource_id2_, false)); |
| 843 EXPECT_FALSE( | 848 EXPECT_FALSE( |
| 844 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false)); | 849 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false)); |
| 845 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true)); | 850 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true)); |
| 846 } | 851 } |
| 847 | 852 |
| 853 TEST_F(ServiceWorkerResourceStorageDiskTest, ClearOnExit) { |
| 854 // Store a new registration. |
| 855 GURL cleared_scope("http://www.test.clear/scope/"); |
| 856 GURL cleared_script("http://www.test.clear/script.js"); |
| 857 int64 cleared_registration_id = storage()->NewRegistrationId(); |
| 858 int64 cleared_version_id = storage()->NewVersionId(); |
| 859 |
| 860 RegistrationData data; |
| 861 data.registration_id = cleared_registration_id; |
| 862 data.scope = cleared_scope; |
| 863 data.script = cleared_script; |
| 864 data.version_id = cleared_version_id; |
| 865 data.is_active = false; |
| 866 |
| 867 scoped_refptr<ServiceWorkerRegistration> cleared_registration = |
| 868 storage()->GetOrCreateRegistration(data, std::vector<ResourceRecord>()); |
| 869 cleared_registration->waiting_version()->SetStatus( |
| 870 ServiceWorkerVersion::INSTALLED); |
| 871 EXPECT_EQ(SERVICE_WORKER_OK, |
| 872 StoreRegistration(cleared_registration, |
| 873 cleared_registration->waiting_version())); |
| 874 |
| 875 // Set its origin as session-only. |
| 876 special_storage_policy_->AddSessionOnly(cleared_scope.GetOrigin()); |
| 877 |
| 878 // Verify that the registrations are findable. |
| 879 scoped_refptr<ServiceWorkerRegistration> found_registration; |
| 880 EXPECT_EQ(SERVICE_WORKER_OK, |
| 881 FindRegistrationForId(cleared_registration_id, |
| 882 cleared_scope.GetOrigin(), |
| 883 &found_registration)); |
| 884 EXPECT_EQ(SERVICE_WORKER_OK, |
| 885 FindRegistrationForId( |
| 886 registration_id_, scope_.GetOrigin(), &found_registration)); |
| 887 |
| 888 // Simulate browser restart. |
| 889 context_.reset(); |
| 890 context_.reset( |
| 891 new ServiceWorkerContextCore(GetUserDataDirectory(), |
| 892 base::ThreadTaskRunnerHandle::Get(), |
| 893 base::ThreadTaskRunnerHandle::Get(), |
| 894 base::ThreadTaskRunnerHandle::Get(), |
| 895 NULL, |
| 896 special_storage_policy_.get(), |
| 897 NULL, |
| 898 NULL)); |
| 899 storage()->LazyInitialize(base::Bind(&base::DoNothing)); |
| 900 base::RunLoop().RunUntilIdle(); |
| 901 |
| 902 // Verify that the session-only origin registration is gone. |
| 903 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
| 904 FindRegistrationForId(cleared_registration_id, |
| 905 cleared_scope.GetOrigin(), |
| 906 &found_registration)); |
| 907 EXPECT_EQ(SERVICE_WORKER_OK, |
| 908 FindRegistrationForId( |
| 909 registration_id_, scope_.GetOrigin(), &found_registration)); |
| 910 } |
| 911 |
| 848 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { | 912 TEST_F(ServiceWorkerResourceStorageTest, UpdateRegistration) { |
| 849 // Promote the worker to active worker and add a controllee. | 913 // Promote the worker to active worker and add a controllee. |
| 850 registration_->SetActiveVersion(registration_->waiting_version()); | 914 registration_->SetActiveVersion(registration_->waiting_version()); |
| 851 storage()->UpdateToActiveState( | 915 storage()->UpdateToActiveState( |
| 852 registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 916 registration_.get(), base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 853 scoped_ptr<ServiceWorkerProviderHost> host( | 917 scoped_ptr<ServiceWorkerProviderHost> host( |
| 854 new ServiceWorkerProviderHost(33 /* dummy render process id */, | 918 new ServiceWorkerProviderHost(33 /* dummy render process id */, |
| 855 1 /* dummy provider_id */, | 919 1 /* dummy provider_id */, |
| 856 context_->AsWeakPtr(), | 920 context_->AsWeakPtr(), |
| 857 NULL)); | 921 NULL)); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1038 are_equal = true; | 1102 are_equal = true; |
| 1039 storage()->CompareScriptResources( | 1103 storage()->CompareScriptResources( |
| 1040 5, 6, | 1104 5, 6, |
| 1041 base::Bind(&OnCompareComplete, &status, &are_equal)); | 1105 base::Bind(&OnCompareComplete, &status, &are_equal)); |
| 1042 base::RunLoop().RunUntilIdle(); | 1106 base::RunLoop().RunUntilIdle(); |
| 1043 EXPECT_EQ(SERVICE_WORKER_OK, status); | 1107 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 1044 EXPECT_FALSE(are_equal); | 1108 EXPECT_FALSE(are_equal); |
| 1045 } | 1109 } |
| 1046 | 1110 |
| 1047 } // namespace content | 1111 } // namespace content |
| OLD | NEW |