| 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/public/browser/service_worker_context.h" | 5 #include "content/public/browser/service_worker_context.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "content/browser/browser_thread_impl.h" | 14 #include "content/browser/browser_thread_impl.h" |
| 15 #include "content/browser/service_worker/embedded_worker_registry.h" | 15 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 16 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 16 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 17 #include "content/browser/service_worker/service_worker_context_core.h" | 17 #include "content/browser/service_worker/service_worker_context_core.h" |
| 18 #include "content/browser/service_worker/service_worker_context_core_observer.h" | 18 #include "content/browser/service_worker/service_worker_context_core_observer.h" |
| 19 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 19 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 20 #include "content/browser/service_worker/service_worker_provider_host.h" | 20 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 21 #include "content/browser/service_worker/service_worker_registration.h" | 21 #include "content/browser/service_worker/service_worker_registration.h" |
| 22 #include "content/browser/service_worker/service_worker_storage.h" | 22 #include "content/browser/service_worker/service_worker_storage.h" |
| 23 #include "content/browser/service_worker/service_worker_test_utils.h" | 23 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 24 #include "content/browser/service_worker/service_worker_version.h" | |
| 25 #include "content/common/service_worker/embedded_worker_messages.h" | 24 #include "content/common/service_worker/embedded_worker_messages.h" |
| 26 #include "content/common/service_worker/service_worker_messages.h" | 25 #include "content/common/service_worker/service_worker_messages.h" |
| 27 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
| 28 #include "content/public/test/test_utils.h" | 27 #include "content/public/test/test_utils.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 29 |
| 31 namespace content { | 30 namespace content { |
| 32 | 31 |
| 33 namespace { | 32 namespace { |
| 34 | 33 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 public: | 171 public: |
| 173 enum class Message { StartWorker, StopWorker }; | 172 enum class Message { StartWorker, StopWorker }; |
| 174 | 173 |
| 175 explicit RecordableEmbeddedWorkerInstanceClient( | 174 explicit RecordableEmbeddedWorkerInstanceClient( |
| 176 base::WeakPtr<EmbeddedWorkerTestHelper> helper) | 175 base::WeakPtr<EmbeddedWorkerTestHelper> helper) |
| 177 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} | 176 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} |
| 178 | 177 |
| 179 const std::vector<Message>& events() const { return events_; } | 178 const std::vector<Message>& events() const { return events_; } |
| 180 | 179 |
| 181 protected: | 180 protected: |
| 182 void StartWorker( | 181 void StartWorker(const EmbeddedWorkerStartParams& params, |
| 183 const EmbeddedWorkerStartParams& params, | 182 mojom::ServiceWorkerEventDispatcherRequest request, |
| 184 mojom::ServiceWorkerEventDispatcherRequest request, | 183 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo |
| 185 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, | 184 instance_host) override { |
| 186 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info) | |
| 187 override { | |
| 188 events_.push_back(Message::StartWorker); | 185 events_.push_back(Message::StartWorker); |
| 189 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( | 186 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( |
| 190 params, std::move(request), std::move(instance_host), | 187 params, std::move(request), std::move(instance_host)); |
| 191 std::move(provider_info)); | |
| 192 } | 188 } |
| 193 | 189 |
| 194 void StopWorker() override { | 190 void StopWorker() override { |
| 195 events_.push_back(Message::StopWorker); | 191 events_.push_back(Message::StopWorker); |
| 196 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StopWorker(); | 192 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StopWorker(); |
| 197 } | 193 } |
| 198 | 194 |
| 199 std::vector<Message> events_; | 195 std::vector<Message> events_; |
| 200 | 196 |
| 201 private: | 197 private: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 217 pattern, | 213 pattern, |
| 218 script_url, | 214 script_url, |
| 219 NULL, | 215 NULL, |
| 220 MakeRegisteredCallback(&called, ®istration_id)); | 216 MakeRegisteredCallback(&called, ®istration_id)); |
| 221 | 217 |
| 222 ASSERT_FALSE(called); | 218 ASSERT_FALSE(called); |
| 223 base::RunLoop().RunUntilIdle(); | 219 base::RunLoop().RunUntilIdle(); |
| 224 EXPECT_TRUE(called); | 220 EXPECT_TRUE(called); |
| 225 | 221 |
| 226 ASSERT_EQ(2UL, helper_->dispatched_events()->size()); | 222 ASSERT_EQ(2UL, helper_->dispatched_events()->size()); |
| 227 ASSERT_EQ(1UL, client->events().size()); | 223 ASSERT_EQ(2UL, client->events().size()); |
| 228 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker, | 224 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker, |
| 229 client->events()[0]); | 225 client->events()[0]); |
| 230 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Install, | 226 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Install, |
| 231 helper_->dispatched_events()->at(0)); | 227 helper_->dispatched_events()->at(0)); |
| 232 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Activate, | 228 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Activate, |
| 233 helper_->dispatched_events()->at(1)); | 229 helper_->dispatched_events()->at(1)); |
| 230 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StopWorker, |
| 231 client->events()[1]); |
| 234 | 232 |
| 235 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 233 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| 236 | 234 |
| 237 ASSERT_EQ(1u, notifications_.size()); | 235 ASSERT_EQ(1u, notifications_.size()); |
| 238 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 236 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 239 EXPECT_EQ(pattern, notifications_[0].pattern); | 237 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 240 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 238 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 241 | 239 |
| 242 context()->storage()->FindRegistrationForId( | 240 context()->storage()->FindRegistrationForId( |
| 243 registration_id, | 241 registration_id, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 bool called = false; | 318 bool called = false; |
| 321 context()->RegisterServiceWorker( | 319 context()->RegisterServiceWorker( |
| 322 pattern, script_url, NULL, | 320 pattern, script_url, NULL, |
| 323 MakeRegisteredCallback(&called, ®istration_id)); | 321 MakeRegisteredCallback(&called, ®istration_id)); |
| 324 | 322 |
| 325 ASSERT_FALSE(called); | 323 ASSERT_FALSE(called); |
| 326 base::RunLoop().RunUntilIdle(); | 324 base::RunLoop().RunUntilIdle(); |
| 327 EXPECT_TRUE(called); | 325 EXPECT_TRUE(called); |
| 328 | 326 |
| 329 ASSERT_EQ(2UL, helper_->dispatched_events()->size()); | 327 ASSERT_EQ(2UL, helper_->dispatched_events()->size()); |
| 330 ASSERT_EQ(1UL, client->events().size()); | 328 ASSERT_EQ(2UL, client->events().size()); |
| 331 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker, | 329 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker, |
| 332 client->events()[0]); | 330 client->events()[0]); |
| 333 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Install, | 331 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Install, |
| 334 helper_->dispatched_events()->at(0)); | 332 helper_->dispatched_events()->at(0)); |
| 335 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Activate, | 333 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Activate, |
| 336 helper_->dispatched_events()->at(1)); | 334 helper_->dispatched_events()->at(1)); |
| 335 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StopWorker, |
| 336 client->events()[1]); |
| 337 | 337 |
| 338 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 338 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| 339 | 339 |
| 340 ASSERT_EQ(1u, notifications_.size()); | 340 ASSERT_EQ(1u, notifications_.size()); |
| 341 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 341 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 342 EXPECT_EQ(pattern, notifications_[0].pattern); | 342 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 343 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 343 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 344 | 344 |
| 345 context()->storage()->FindRegistrationForId( | 345 context()->storage()->FindRegistrationForId( |
| 346 registration_id, pattern.GetOrigin(), | 346 registration_id, pattern.GetOrigin(), |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 host2->SetDocumentUrl(kOrigin2); | 602 host2->SetDocumentUrl(kOrigin2); |
| 603 | 603 |
| 604 // Host3 (provider_id=3): process_id=2, origin1. | 604 // Host3 (provider_id=3): process_id=2, origin1. |
| 605 remote_endpoints.emplace_back(); | 605 remote_endpoints.emplace_back(); |
| 606 std::unique_ptr<ServiceWorkerProviderHost> host3 = | 606 std::unique_ptr<ServiceWorkerProviderHost> host3 = |
| 607 CreateProviderHostForWindow( | 607 CreateProviderHostForWindow( |
| 608 kRenderProcessId2, provider_id++, true /* is_parent_frame_secure */, | 608 kRenderProcessId2, provider_id++, true /* is_parent_frame_secure */, |
| 609 context()->AsWeakPtr(), &remote_endpoints.back()); | 609 context()->AsWeakPtr(), &remote_endpoints.back()); |
| 610 host3->SetDocumentUrl(kOrigin1); | 610 host3->SetDocumentUrl(kOrigin1); |
| 611 | 611 |
| 612 // Host4 (provider_id < -1): process_id=2, origin2, for ServiceWorker. | 612 // Host4 (provider_id=4): process_id=2, origin2, for ServiceWorker. |
| 613 // Since the provider host is created via | |
| 614 // CreateProviderHostForServiceWorkerContext, the provider_id is not a fixed | |
| 615 // number. | |
| 616 scoped_refptr<ServiceWorkerRegistration> registration = | |
| 617 base::MakeRefCounted<ServiceWorkerRegistration>( | |
| 618 GURL("http://www.example.com/test/"), 1L /* registration_id */, | |
| 619 helper_->context()->AsWeakPtr()); | |
| 620 scoped_refptr<ServiceWorkerVersion> version = | |
| 621 base::MakeRefCounted<ServiceWorkerVersion>( | |
| 622 registration.get(), GURL("http://www.example.com/test/script_url"), | |
| 623 1L /* version_id */, helper_->context()->AsWeakPtr()); | |
| 624 helper_->SimulateAddProcessToPattern( | |
| 625 GURL("http://www.example.com/test/script_url"), kRenderProcessId2); | |
| 626 remote_endpoints.emplace_back(); | 613 remote_endpoints.emplace_back(); |
| 627 std::unique_ptr<ServiceWorkerProviderHost> host4 = | 614 std::unique_ptr<ServiceWorkerProviderHost> host4 = |
| 628 CreateProviderHostForServiceWorkerContext( | 615 CreateProviderHostForServiceWorkerContext( |
| 629 kRenderProcessId2, true /* is_parent_frame_secure */, version.get(), | 616 kRenderProcessId2, provider_id++, true /* is_parent_frame_secure */, |
| 630 context()->AsWeakPtr(), &remote_endpoints.back()); | 617 context()->AsWeakPtr(), &remote_endpoints.back()); |
| 631 host4->SetDocumentUrl(kOrigin2); | 618 host4->SetDocumentUrl(kOrigin2); |
| 632 const int host4_provider_id = host4->provider_id(); | |
| 633 EXPECT_LT(host4_provider_id, kInvalidServiceWorkerProviderId); | |
| 634 | 619 |
| 635 ServiceWorkerProviderHost* host1_raw = host1.get(); | 620 ServiceWorkerProviderHost* host1_raw = host1.get(); |
| 636 ServiceWorkerProviderHost* host2_raw = host2.get(); | 621 ServiceWorkerProviderHost* host2_raw = host2.get(); |
| 637 ServiceWorkerProviderHost* host3_raw = host3.get(); | 622 ServiceWorkerProviderHost* host3_raw = host3.get(); |
| 638 ServiceWorkerProviderHost* host4_raw = host4.get(); | 623 ServiceWorkerProviderHost* host4_raw = host4.get(); |
| 639 context()->AddProviderHost(std::move(host1)); | 624 context()->AddProviderHost(std::move(host1)); |
| 640 context()->AddProviderHost(std::move(host2)); | 625 context()->AddProviderHost(std::move(host2)); |
| 641 context()->AddProviderHost(std::move(host3)); | 626 context()->AddProviderHost(std::move(host3)); |
| 642 context()->AddProviderHost(std::move(host4)); | 627 context()->AddProviderHost(std::move(host4)); |
| 643 | 628 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 669 for (auto it = context()->GetClientProviderHostIterator(kOrigin2); | 654 for (auto it = context()->GetClientProviderHostIterator(kOrigin2); |
| 670 !it->IsAtEnd(); it->Advance()) { | 655 !it->IsAtEnd(); it->Advance()) { |
| 671 results.insert(it->GetProviderHost()); | 656 results.insert(it->GetProviderHost()); |
| 672 } | 657 } |
| 673 EXPECT_EQ(1u, results.size()); | 658 EXPECT_EQ(1u, results.size()); |
| 674 EXPECT_TRUE(ContainsKey(results, host2_raw)); | 659 EXPECT_TRUE(ContainsKey(results, host2_raw)); |
| 675 | 660 |
| 676 context()->RemoveProviderHost(kRenderProcessId1, 1); | 661 context()->RemoveProviderHost(kRenderProcessId1, 1); |
| 677 context()->RemoveProviderHost(kRenderProcessId2, 2); | 662 context()->RemoveProviderHost(kRenderProcessId2, 2); |
| 678 context()->RemoveProviderHost(kRenderProcessId2, 3); | 663 context()->RemoveProviderHost(kRenderProcessId2, 3); |
| 679 context()->RemoveProviderHost(kRenderProcessId2, host4_provider_id); | 664 context()->RemoveProviderHost(kRenderProcessId2, 4); |
| 680 } | 665 } |
| 681 | 666 |
| 682 class ServiceWorkerContextRecoveryTest | 667 class ServiceWorkerContextRecoveryTest |
| 683 : public ServiceWorkerContextTest, | 668 : public ServiceWorkerContextTest, |
| 684 public testing::WithParamInterface<bool> { | 669 public testing::WithParamInterface<bool> { |
| 685 public: | 670 public: |
| 686 ServiceWorkerContextRecoveryTest() {} | 671 ServiceWorkerContextRecoveryTest() {} |
| 687 virtual ~ServiceWorkerContextRecoveryTest() {} | 672 virtual ~ServiceWorkerContextRecoveryTest() {} |
| 688 | 673 |
| 689 protected: | 674 protected: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 716 | 701 |
| 717 context()->storage()->FindRegistrationForId( | 702 context()->storage()->FindRegistrationForId( |
| 718 registration_id, | 703 registration_id, |
| 719 pattern.GetOrigin(), | 704 pattern.GetOrigin(), |
| 720 base::Bind(&ExpectRegisteredWorkers, | 705 base::Bind(&ExpectRegisteredWorkers, |
| 721 SERVICE_WORKER_OK, | 706 SERVICE_WORKER_OK, |
| 722 false /* expect_waiting */, | 707 false /* expect_waiting */, |
| 723 true /* expect_active */)); | 708 true /* expect_active */)); |
| 724 base::RunLoop().RunUntilIdle(); | 709 base::RunLoop().RunUntilIdle(); |
| 725 | 710 |
| 726 // Next handle ids should be 1 (the next call should return 2) because | 711 // Next handle ids should be 0 (the next call should return 1). |
| 727 // registered worker should have taken ID 0. | 712 EXPECT_EQ(0, context()->GetNewServiceWorkerHandleId()); |
| 728 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId()); | 713 EXPECT_EQ(0, context()->GetNewRegistrationHandleId()); |
| 729 EXPECT_EQ(1, context()->GetNewRegistrationHandleId()); | |
| 730 | 714 |
| 731 context()->ScheduleDeleteAndStartOver(); | 715 context()->ScheduleDeleteAndStartOver(); |
| 732 | 716 |
| 733 // The storage is disabled while the recovery process is running, so the | 717 // The storage is disabled while the recovery process is running, so the |
| 734 // operation should be aborted. | 718 // operation should be aborted. |
| 735 context()->storage()->FindRegistrationForId( | 719 context()->storage()->FindRegistrationForId( |
| 736 registration_id, pattern.GetOrigin(), | 720 registration_id, pattern.GetOrigin(), |
| 737 base::Bind(&ExpectRegisteredWorkers, SERVICE_WORKER_ERROR_ABORT, | 721 base::Bind(&ExpectRegisteredWorkers, SERVICE_WORKER_ERROR_ABORT, |
| 738 false /* expect_waiting */, true /* expect_active */)); | 722 false /* expect_waiting */, true /* expect_active */)); |
| 739 base::RunLoop().RunUntilIdle(); | 723 base::RunLoop().RunUntilIdle(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 762 | 746 |
| 763 context()->storage()->FindRegistrationForId( | 747 context()->storage()->FindRegistrationForId( |
| 764 registration_id, | 748 registration_id, |
| 765 pattern.GetOrigin(), | 749 pattern.GetOrigin(), |
| 766 base::Bind(&ExpectRegisteredWorkers, | 750 base::Bind(&ExpectRegisteredWorkers, |
| 767 SERVICE_WORKER_OK, | 751 SERVICE_WORKER_OK, |
| 768 false /* expect_waiting */, | 752 false /* expect_waiting */, |
| 769 true /* expect_active */)); | 753 true /* expect_active */)); |
| 770 base::RunLoop().RunUntilIdle(); | 754 base::RunLoop().RunUntilIdle(); |
| 771 | 755 |
| 772 // The new context should take over next handle ids. ID 2 should have been | 756 // The new context should take over next handle ids. |
| 773 // taken by the running registration, so the following method calls return 3. | 757 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId()); |
| 774 EXPECT_EQ(3, context()->GetNewServiceWorkerHandleId()); | 758 EXPECT_EQ(1, context()->GetNewRegistrationHandleId()); |
| 775 EXPECT_EQ(3, context()->GetNewRegistrationHandleId()); | |
| 776 | 759 |
| 777 ASSERT_EQ(3u, notifications_.size()); | 760 ASSERT_EQ(3u, notifications_.size()); |
| 778 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 761 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
| 779 EXPECT_EQ(pattern, notifications_[0].pattern); | 762 EXPECT_EQ(pattern, notifications_[0].pattern); |
| 780 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 763 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
| 781 EXPECT_EQ(STORAGE_RECOVERED, notifications_[1].type); | 764 EXPECT_EQ(STORAGE_RECOVERED, notifications_[1].type); |
| 782 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); | 765 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); |
| 783 EXPECT_EQ(pattern, notifications_[2].pattern); | 766 EXPECT_EQ(pattern, notifications_[2].pattern); |
| 784 EXPECT_EQ(registration_id, notifications_[2].registration_id); | 767 EXPECT_EQ(registration_id, notifications_[2].registration_id); |
| 785 } | 768 } |
| 786 | 769 |
| 787 INSTANTIATE_TEST_CASE_P(ServiceWorkerContextRecoveryTest, | 770 INSTANTIATE_TEST_CASE_P(ServiceWorkerContextRecoveryTest, |
| 788 ServiceWorkerContextRecoveryTest, | 771 ServiceWorkerContextRecoveryTest, |
| 789 testing::Bool()); | 772 testing::Bool()); |
| 790 | 773 |
| 791 } // namespace content | 774 } // namespace content |
| OLD | NEW |