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