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::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, | 233 // StopWorker should not be recorded because hosting ProviderHost has a |
231 client->events()[1]); | 234 // reference to the ServiceWorkerVersion. |
falken
2017/06/06 14:06:33
This comment is nice for understanding the change
shimazu
2017/06/12 06:08:12
Thanks, I agree that this comment doesn't describe
| |
232 | 235 |
233 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 236 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
234 | 237 |
235 ASSERT_EQ(1u, notifications_.size()); | 238 ASSERT_EQ(1u, notifications_.size()); |
236 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 239 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
237 EXPECT_EQ(pattern, notifications_[0].pattern); | 240 EXPECT_EQ(pattern, notifications_[0].pattern); |
238 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 241 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
239 | 242 |
240 context()->storage()->FindRegistrationForId( | 243 context()->storage()->FindRegistrationForId( |
241 registration_id, | 244 registration_id, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 bool called = false; | 321 bool called = false; |
319 context()->RegisterServiceWorker( | 322 context()->RegisterServiceWorker( |
320 pattern, script_url, NULL, | 323 pattern, script_url, NULL, |
321 MakeRegisteredCallback(&called, ®istration_id)); | 324 MakeRegisteredCallback(&called, ®istration_id)); |
322 | 325 |
323 ASSERT_FALSE(called); | 326 ASSERT_FALSE(called); |
324 base::RunLoop().RunUntilIdle(); | 327 base::RunLoop().RunUntilIdle(); |
325 EXPECT_TRUE(called); | 328 EXPECT_TRUE(called); |
326 | 329 |
327 ASSERT_EQ(2UL, helper_->dispatched_events()->size()); | 330 ASSERT_EQ(2UL, helper_->dispatched_events()->size()); |
328 ASSERT_EQ(2UL, client->events().size()); | 331 ASSERT_EQ(1UL, client->events().size()); |
329 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker, | 332 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker, |
330 client->events()[0]); | 333 client->events()[0]); |
331 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Install, | 334 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Install, |
332 helper_->dispatched_events()->at(0)); | 335 helper_->dispatched_events()->at(0)); |
333 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Activate, | 336 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Activate, |
334 helper_->dispatched_events()->at(1)); | 337 helper_->dispatched_events()->at(1)); |
335 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StopWorker, | 338 // Rejection of activate event should not triggers the shutdown of the service |
336 client->events()[1]); | 339 // worker. |
falken
2017/06/06 14:06:33
ditto: not sure this comment is worth leaving in t
shimazu
2017/06/12 06:08:12
Done.
| |
337 | 340 |
338 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 341 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
339 | 342 |
340 ASSERT_EQ(1u, notifications_.size()); | 343 ASSERT_EQ(1u, notifications_.size()); |
341 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 344 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
342 EXPECT_EQ(pattern, notifications_[0].pattern); | 345 EXPECT_EQ(pattern, notifications_[0].pattern); |
343 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 346 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
344 | 347 |
345 context()->storage()->FindRegistrationForId( | 348 context()->storage()->FindRegistrationForId( |
346 registration_id, pattern.GetOrigin(), | 349 registration_id, pattern.GetOrigin(), |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
602 host2->SetDocumentUrl(kOrigin2); | 605 host2->SetDocumentUrl(kOrigin2); |
603 | 606 |
604 // Host3 (provider_id=3): process_id=2, origin1. | 607 // Host3 (provider_id=3): process_id=2, origin1. |
605 remote_endpoints.emplace_back(); | 608 remote_endpoints.emplace_back(); |
606 std::unique_ptr<ServiceWorkerProviderHost> host3 = | 609 std::unique_ptr<ServiceWorkerProviderHost> host3 = |
607 CreateProviderHostForWindow( | 610 CreateProviderHostForWindow( |
608 kRenderProcessId2, provider_id++, true /* is_parent_frame_secure */, | 611 kRenderProcessId2, provider_id++, true /* is_parent_frame_secure */, |
609 context()->AsWeakPtr(), &remote_endpoints.back()); | 612 context()->AsWeakPtr(), &remote_endpoints.back()); |
610 host3->SetDocumentUrl(kOrigin1); | 613 host3->SetDocumentUrl(kOrigin1); |
611 | 614 |
612 // Host4 (provider_id=4): process_id=2, origin2, for ServiceWorker. | 615 // Host4 (provider_id<-1): process_id=2, origin2, for ServiceWorker. |
616 // The provider_id is not fixed because it's unique beyond tests. | |
falken
2017/06/06 14:06:33
"unique beyond tests" means that other tests influ
shimazu
2017/06/12 06:08:12
Yes, the provider id is unique because it's saved
| |
617 scoped_refptr<ServiceWorkerRegistration> registration = | |
618 new ServiceWorkerRegistration(GURL("http://www.example.com/test/"), | |
619 1L /* registration_id */, | |
620 helper_->context()->AsWeakPtr()); | |
621 scoped_refptr<ServiceWorkerVersion> version = new 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 no 0. |
falken
2017/06/06 14:06:33
"no 0" means "number 0"? Recommend ID 0.
shimazu
2017/06/12 06:08:12
Done.
| |
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. No 2 should have been |
falken
2017/06/06 14:06:33
s/No/Number or ID
(No sounds like the opposite of
shimazu
2017/06/12 06:08:12
Thanks. I felt the same when I read the comment ag
| |
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 |