| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
| 7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
| 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/browser/service_worker/service_worker_provider_host.h" | 10 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 11 #include "content/browser/service_worker/service_worker_register_job.h" | 11 #include "content/browser/service_worker/service_worker_register_job.h" |
| 12 #include "content/browser/service_worker/service_worker_registration.h" | 12 #include "content/browser/service_worker/service_worker_registration.h" |
| 13 #include "content/browser/service_worker/service_worker_version.h" | 13 #include "content/browser/service_worker/service_worker_version.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 static const int kRenderProcessId = 33; // Dummy process ID for testing. | 19 static const int kRenderProcessId = 33; // Dummy process ID for testing. |
| 20 | 20 |
| 21 class ServiceWorkerProviderHostTest : public testing::Test { | 21 class ServiceWorkerProviderHostTest : public testing::Test { |
| 22 protected: | 22 protected: |
| 23 ServiceWorkerProviderHostTest() | 23 ServiceWorkerProviderHostTest() |
| 24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 25 ~ServiceWorkerProviderHostTest() override {} | 25 ~ServiceWorkerProviderHostTest() override {} |
| 26 | 26 |
| 27 void SetUp() override { | 27 void SetUp() override { |
| 28 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); | 28 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
| 29 context_ = helper_->context(); | 29 context_ = helper_->context(); |
| 30 pattern_ = GURL("http://www.example.com/"); | |
| 31 script_url_ = GURL("http://www.example.com/service_worker.js"); | 30 script_url_ = GURL("http://www.example.com/service_worker.js"); |
| 32 registration_ = new ServiceWorkerRegistration( | 31 registration1_ = new ServiceWorkerRegistration( |
| 33 pattern_, 1L, context_->AsWeakPtr()); | 32 GURL("http://www.example.com/"), 1L, context_->AsWeakPtr()); |
| 34 version_ = new ServiceWorkerVersion( | 33 registration2_ = new ServiceWorkerRegistration( |
| 35 registration_.get(), script_url_, 1L, context_->AsWeakPtr()); | 34 GURL("http://www.example.com/example"), 2L, context_->AsWeakPtr()); |
| 36 | 35 |
| 37 // Prepare provider hosts (for the same process). | 36 // Prepare provider hosts (for the same process). |
| 38 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( | 37 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( |
| 39 kRenderProcessId, MSG_ROUTING_NONE, 1 /* provider_id */, | 38 kRenderProcessId, MSG_ROUTING_NONE, 1 /* provider_id */, |
| 40 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, | 39 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, |
| 41 context_->AsWeakPtr(), NULL)); | 40 context_->AsWeakPtr(), NULL)); |
| 41 host1->SetDocumentUrl(GURL("http://www.example.com/example1.html")); |
| 42 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( | 42 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( |
| 43 kRenderProcessId, MSG_ROUTING_NONE, 2 /* provider_id */, | 43 kRenderProcessId, MSG_ROUTING_NONE, 2 /* provider_id */, |
| 44 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, | 44 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, |
| 45 context_->AsWeakPtr(), NULL)); | 45 context_->AsWeakPtr(), NULL)); |
| 46 host2->SetDocumentUrl(GURL("http://www.example.com/example2.html")); |
| 46 provider_host1_ = host1->AsWeakPtr(); | 47 provider_host1_ = host1->AsWeakPtr(); |
| 47 provider_host2_ = host2->AsWeakPtr(); | 48 provider_host2_ = host2->AsWeakPtr(); |
| 48 context_->AddProviderHost(make_scoped_ptr(host1.release())); | 49 context_->AddProviderHost(make_scoped_ptr(host1.release())); |
| 49 context_->AddProviderHost(make_scoped_ptr(host2.release())); | 50 context_->AddProviderHost(make_scoped_ptr(host2.release())); |
| 50 } | 51 } |
| 51 | 52 |
| 52 void TearDown() override { | 53 void TearDown() override { |
| 53 version_ = 0; | 54 registration1_ = 0; |
| 54 registration_ = 0; | 55 registration2_ = 0; |
| 55 helper_.reset(); | 56 helper_.reset(); |
| 56 } | 57 } |
| 57 | 58 |
| 58 bool HasProcessToRun() const { | 59 bool PatternHasProcessToRun(const GURL& pattern) const { |
| 59 return context_->process_manager()->PatternHasProcessToRun(pattern_); | 60 return context_->process_manager()->PatternHasProcessToRun(pattern); |
| 60 } | 61 } |
| 61 | 62 |
| 62 content::TestBrowserThreadBundle thread_bundle_; | 63 content::TestBrowserThreadBundle thread_bundle_; |
| 63 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 64 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 64 ServiceWorkerContextCore* context_; | 65 ServiceWorkerContextCore* context_; |
| 65 scoped_refptr<ServiceWorkerRegistration> registration_; | 66 scoped_refptr<ServiceWorkerRegistration> registration1_; |
| 66 scoped_refptr<ServiceWorkerVersion> version_; | 67 scoped_refptr<ServiceWorkerRegistration> registration2_; |
| 67 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 68 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
| 68 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 69 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
| 69 GURL pattern_; | |
| 70 GURL script_url_; | 70 GURL script_url_; |
| 71 | 71 |
| 72 private: | 72 private: |
| 73 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 73 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { | 76 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { |
| 77 provider_host1_->AssociateRegistration(registration_.get()); | 77 provider_host1_->AddMatchingRegistration(registration1_.get()); |
| 78 ASSERT_TRUE(HasProcessToRun()); | 78 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 79 | 79 |
| 80 // Associating version_ to a provider_host's active version will internally | 80 // Adding the same registration twice has no effect. |
| 81 // add the provider_host's process ref to the version. | 81 provider_host1_->AddMatchingRegistration(registration1_.get()); |
| 82 registration_->SetActiveVersion(version_.get()); | 82 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 83 ASSERT_TRUE(HasProcessToRun()); | |
| 84 | 83 |
| 85 // Re-associating the same version and provider_host should just work too. | 84 // Different matching registrations can be added. |
| 86 registration_->SetActiveVersion(version_.get()); | 85 provider_host1_->AddMatchingRegistration(registration2_.get()); |
| 87 ASSERT_TRUE(HasProcessToRun()); | 86 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); |
| 88 | 87 |
| 89 // Resetting the provider_host's active version should remove process refs | 88 // Removing a matching registration will decrease the process refs for its |
| 90 // from the version. | 89 // pattern. |
| 91 provider_host1_->DisassociateRegistration(); | 90 provider_host1_->RemoveMatchingRegistration(registration1_.get()); |
| 92 ASSERT_FALSE(HasProcessToRun()); | 91 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); |
| 92 |
| 93 // Multiple provider hosts could add the same matching registration. |
| 94 // The process refs will become 0 after all provider hosts removed them. |
| 95 provider_host2_->AddMatchingRegistration(registration2_.get()); |
| 96 provider_host1_->RemoveMatchingRegistration(registration2_.get()); |
| 97 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); |
| 98 provider_host2_->RemoveMatchingRegistration(registration2_.get()); |
| 99 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); |
| 93 } | 100 } |
| 94 | 101 |
| 95 TEST_F(ServiceWorkerProviderHostTest, | 102 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { |
| 96 SetActiveVersion_MultipleHostsForSameProcess) { | 103 // Associating the registration will also increase the process refs for |
| 97 provider_host1_->AssociateRegistration(registration_.get()); | 104 // the registration's pattern. |
| 98 provider_host2_->AssociateRegistration(registration_.get()); | 105 provider_host1_->AssociateRegistration(registration1_.get()); |
| 99 ASSERT_TRUE(HasProcessToRun()); | 106 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 100 | 107 |
| 101 // Associating version_ to two providers as active version. | 108 // Disassociating the registration shouldn't affect the process refs for |
| 102 registration_->SetActiveVersion(version_.get()); | 109 // the registration's pattern. |
| 103 ASSERT_TRUE(HasProcessToRun()); | |
| 104 | |
| 105 // Disassociating one provider_host shouldn't remove all process refs | |
| 106 // from the version yet. | |
| 107 provider_host1_->DisassociateRegistration(); | 110 provider_host1_->DisassociateRegistration(); |
| 108 ASSERT_TRUE(HasProcessToRun()); | 111 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 109 | |
| 110 // Disassociating the other provider_host will remove all process refs. | |
| 111 provider_host2_->DisassociateRegistration(); | |
| 112 ASSERT_FALSE(HasProcessToRun()); | |
| 113 } | 112 } |
| 114 | 113 |
| 115 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { | 114 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { |
| 116 provider_host1_->AssociateRegistration(registration_.get()); | 115 provider_host1_->AddMatchingRegistration(registration1_.get()); |
| 117 ASSERT_TRUE(HasProcessToRun()); | 116 provider_host1_->AddMatchingRegistration(registration2_.get()); |
| 118 | 117 |
| 119 // Associating version_ to a provider_host's waiting version will internally | 118 // Match registration should return the longest matching one. |
| 120 // add the provider_host's process ref to the version. | 119 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_); |
| 121 registration_->SetWaitingVersion(version_.get()); | 120 provider_host1_->RemoveMatchingRegistration(registration2_.get()); |
| 122 ASSERT_TRUE(HasProcessToRun()); | 121 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_); |
| 123 | 122 |
| 124 // Re-associating the same version and provider_host should just work too. | 123 // Should return nullptr after removing all matching registrations. |
| 125 registration_->SetWaitingVersion(version_.get()); | 124 provider_host1_->RemoveMatchingRegistration(registration1_.get()); |
| 126 ASSERT_TRUE(HasProcessToRun()); | 125 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr); |
| 127 | |
| 128 // Resetting the provider_host's waiting version should remove process refs | |
| 129 // from the version. | |
| 130 provider_host1_->DisassociateRegistration(); | |
| 131 ASSERT_FALSE(HasProcessToRun()); | |
| 132 } | |
| 133 | |
| 134 TEST_F(ServiceWorkerProviderHostTest, | |
| 135 SetWaitingVersion_MultipleHostsForSameProcess) { | |
| 136 provider_host1_->AssociateRegistration(registration_.get()); | |
| 137 provider_host2_->AssociateRegistration(registration_.get()); | |
| 138 ASSERT_TRUE(HasProcessToRun()); | |
| 139 | |
| 140 // Associating version_ to two providers as waiting version. | |
| 141 registration_->SetWaitingVersion(version_.get()); | |
| 142 ASSERT_TRUE(HasProcessToRun()); | |
| 143 | |
| 144 // Disassociating one provider_host shouldn't remove all process refs | |
| 145 // from the version yet. | |
| 146 provider_host1_->DisassociateRegistration(); | |
| 147 ASSERT_TRUE(HasProcessToRun()); | |
| 148 | |
| 149 // Disassociating the other provider_host will remove all process refs. | |
| 150 provider_host2_->DisassociateRegistration(); | |
| 151 ASSERT_FALSE(HasProcessToRun()); | |
| 152 } | 126 } |
| 153 | 127 |
| 154 } // namespace content | 128 } // namespace content |
| OLD | NEW |