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, | |
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 // The provider_id is not fixed because it's unique beyond tests. | |
falken
2017/06/14 08:10:34
The "unique beyond tests" is still confusing.
"Si
shimazu
2017/06/16 04:18:43
Thanks, I missed to fix this. Updated.
| |
613 scoped_refptr<ServiceWorkerRegistration> registration = | |
614 new ServiceWorkerRegistration(GURL("http://www.example.com/test/"), | |
615 1L /* registration_id */, | |
616 helper_->context()->AsWeakPtr()); | |
617 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion( | |
618 registration.get(), GURL("http://www.example.com/test/script_url"), | |
619 1L /* version_id */, helper_->context()->AsWeakPtr()); | |
620 helper_->SimulateAddProcessToPattern( | |
621 GURL("http://www.example.com/test/script_url"), kRenderProcessId2); | |
613 remote_endpoints.emplace_back(); | 622 remote_endpoints.emplace_back(); |
614 std::unique_ptr<ServiceWorkerProviderHost> host4 = | 623 std::unique_ptr<ServiceWorkerProviderHost> host4 = |
615 CreateProviderHostForServiceWorkerContext( | 624 CreateProviderHostForServiceWorkerContext( |
616 kRenderProcessId2, provider_id++, true /* is_parent_frame_secure */, | 625 kRenderProcessId2, true /* is_parent_frame_secure */, version.get(), |
617 context()->AsWeakPtr(), &remote_endpoints.back()); | 626 context()->AsWeakPtr(), &remote_endpoints.back()); |
618 host4->SetDocumentUrl(kOrigin2); | 627 host4->SetDocumentUrl(kOrigin2); |
628 const int host4_provider_id = host4->provider_id(); | |
629 EXPECT_LT(host4_provider_id, kInvalidServiceWorkerProviderId); | |
619 | 630 |
620 ServiceWorkerProviderHost* host1_raw = host1.get(); | 631 ServiceWorkerProviderHost* host1_raw = host1.get(); |
621 ServiceWorkerProviderHost* host2_raw = host2.get(); | 632 ServiceWorkerProviderHost* host2_raw = host2.get(); |
622 ServiceWorkerProviderHost* host3_raw = host3.get(); | 633 ServiceWorkerProviderHost* host3_raw = host3.get(); |
623 ServiceWorkerProviderHost* host4_raw = host4.get(); | 634 ServiceWorkerProviderHost* host4_raw = host4.get(); |
624 context()->AddProviderHost(std::move(host1)); | 635 context()->AddProviderHost(std::move(host1)); |
625 context()->AddProviderHost(std::move(host2)); | 636 context()->AddProviderHost(std::move(host2)); |
626 context()->AddProviderHost(std::move(host3)); | 637 context()->AddProviderHost(std::move(host3)); |
627 context()->AddProviderHost(std::move(host4)); | 638 context()->AddProviderHost(std::move(host4)); |
628 | 639 |
(...skipping 25 matching lines...) Expand all Loading... | |
654 for (auto it = context()->GetClientProviderHostIterator(kOrigin2); | 665 for (auto it = context()->GetClientProviderHostIterator(kOrigin2); |
655 !it->IsAtEnd(); it->Advance()) { | 666 !it->IsAtEnd(); it->Advance()) { |
656 results.insert(it->GetProviderHost()); | 667 results.insert(it->GetProviderHost()); |
657 } | 668 } |
658 EXPECT_EQ(1u, results.size()); | 669 EXPECT_EQ(1u, results.size()); |
659 EXPECT_TRUE(ContainsKey(results, host2_raw)); | 670 EXPECT_TRUE(ContainsKey(results, host2_raw)); |
660 | 671 |
661 context()->RemoveProviderHost(kRenderProcessId1, 1); | 672 context()->RemoveProviderHost(kRenderProcessId1, 1); |
662 context()->RemoveProviderHost(kRenderProcessId2, 2); | 673 context()->RemoveProviderHost(kRenderProcessId2, 2); |
663 context()->RemoveProviderHost(kRenderProcessId2, 3); | 674 context()->RemoveProviderHost(kRenderProcessId2, 3); |
664 context()->RemoveProviderHost(kRenderProcessId2, 4); | 675 context()->RemoveProviderHost(kRenderProcessId2, host4_provider_id); |
665 } | 676 } |
666 | 677 |
667 class ServiceWorkerContextRecoveryTest | 678 class ServiceWorkerContextRecoveryTest |
668 : public ServiceWorkerContextTest, | 679 : public ServiceWorkerContextTest, |
669 public testing::WithParamInterface<bool> { | 680 public testing::WithParamInterface<bool> { |
670 public: | 681 public: |
671 ServiceWorkerContextRecoveryTest() {} | 682 ServiceWorkerContextRecoveryTest() {} |
672 virtual ~ServiceWorkerContextRecoveryTest() {} | 683 virtual ~ServiceWorkerContextRecoveryTest() {} |
673 | 684 |
674 protected: | 685 protected: |
(...skipping 26 matching lines...) Expand all Loading... | |
701 | 712 |
702 context()->storage()->FindRegistrationForId( | 713 context()->storage()->FindRegistrationForId( |
703 registration_id, | 714 registration_id, |
704 pattern.GetOrigin(), | 715 pattern.GetOrigin(), |
705 base::Bind(&ExpectRegisteredWorkers, | 716 base::Bind(&ExpectRegisteredWorkers, |
706 SERVICE_WORKER_OK, | 717 SERVICE_WORKER_OK, |
707 false /* expect_waiting */, | 718 false /* expect_waiting */, |
708 true /* expect_active */)); | 719 true /* expect_active */)); |
709 base::RunLoop().RunUntilIdle(); | 720 base::RunLoop().RunUntilIdle(); |
710 | 721 |
711 // Next handle ids should be 0 (the next call should return 1). | 722 // Next handle ids should be 1 (the next call should return 2) because |
712 EXPECT_EQ(0, context()->GetNewServiceWorkerHandleId()); | 723 // registered worker should have taken ID 0. |
713 EXPECT_EQ(0, context()->GetNewRegistrationHandleId()); | 724 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId()); |
725 EXPECT_EQ(1, context()->GetNewRegistrationHandleId()); | |
714 | 726 |
715 context()->ScheduleDeleteAndStartOver(); | 727 context()->ScheduleDeleteAndStartOver(); |
716 | 728 |
717 // The storage is disabled while the recovery process is running, so the | 729 // The storage is disabled while the recovery process is running, so the |
718 // operation should be aborted. | 730 // operation should be aborted. |
719 context()->storage()->FindRegistrationForId( | 731 context()->storage()->FindRegistrationForId( |
720 registration_id, pattern.GetOrigin(), | 732 registration_id, pattern.GetOrigin(), |
721 base::Bind(&ExpectRegisteredWorkers, SERVICE_WORKER_ERROR_ABORT, | 733 base::Bind(&ExpectRegisteredWorkers, SERVICE_WORKER_ERROR_ABORT, |
722 false /* expect_waiting */, true /* expect_active */)); | 734 false /* expect_waiting */, true /* expect_active */)); |
723 base::RunLoop().RunUntilIdle(); | 735 base::RunLoop().RunUntilIdle(); |
(...skipping 22 matching lines...) Expand all Loading... | |
746 | 758 |
747 context()->storage()->FindRegistrationForId( | 759 context()->storage()->FindRegistrationForId( |
748 registration_id, | 760 registration_id, |
749 pattern.GetOrigin(), | 761 pattern.GetOrigin(), |
750 base::Bind(&ExpectRegisteredWorkers, | 762 base::Bind(&ExpectRegisteredWorkers, |
751 SERVICE_WORKER_OK, | 763 SERVICE_WORKER_OK, |
752 false /* expect_waiting */, | 764 false /* expect_waiting */, |
753 true /* expect_active */)); | 765 true /* expect_active */)); |
754 base::RunLoop().RunUntilIdle(); | 766 base::RunLoop().RunUntilIdle(); |
755 | 767 |
756 // The new context should take over next handle ids. | 768 // The new context should take over next handle ids. ID 2 should have been |
757 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId()); | 769 // taken by the running registration, so the following method calls return 3. |
758 EXPECT_EQ(1, context()->GetNewRegistrationHandleId()); | 770 EXPECT_EQ(3, context()->GetNewServiceWorkerHandleId()); |
771 EXPECT_EQ(3, context()->GetNewRegistrationHandleId()); | |
759 | 772 |
760 ASSERT_EQ(3u, notifications_.size()); | 773 ASSERT_EQ(3u, notifications_.size()); |
761 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); | 774 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); |
762 EXPECT_EQ(pattern, notifications_[0].pattern); | 775 EXPECT_EQ(pattern, notifications_[0].pattern); |
763 EXPECT_EQ(registration_id, notifications_[0].registration_id); | 776 EXPECT_EQ(registration_id, notifications_[0].registration_id); |
764 EXPECT_EQ(STORAGE_RECOVERED, notifications_[1].type); | 777 EXPECT_EQ(STORAGE_RECOVERED, notifications_[1].type); |
765 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); | 778 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); |
766 EXPECT_EQ(pattern, notifications_[2].pattern); | 779 EXPECT_EQ(pattern, notifications_[2].pattern); |
767 EXPECT_EQ(registration_id, notifications_[2].registration_id); | 780 EXPECT_EQ(registration_id, notifications_[2].registration_id); |
768 } | 781 } |
769 | 782 |
770 INSTANTIATE_TEST_CASE_P(ServiceWorkerContextRecoveryTest, | 783 INSTANTIATE_TEST_CASE_P(ServiceWorkerContextRecoveryTest, |
771 ServiceWorkerContextRecoveryTest, | 784 ServiceWorkerContextRecoveryTest, |
772 testing::Bool()); | 785 testing::Bool()); |
773 | 786 |
774 } // namespace content | 787 } // namespace content |
OLD | NEW |