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