| 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 "content/browser/service_worker/service_worker_provider_host.h" | 5 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 old_content_browser_client_ = | 42 old_content_browser_client_ = |
| 43 SetBrowserClientForTesting(&test_content_browser_client_); | 43 SetBrowserClientForTesting(&test_content_browser_client_); |
| 44 | 44 |
| 45 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 45 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 46 context_ = helper_->context(); | 46 context_ = helper_->context(); |
| 47 script_url_ = GURL("https://www.example.com/service_worker.js"); | 47 script_url_ = GURL("https://www.example.com/service_worker.js"); |
| 48 registration1_ = new ServiceWorkerRegistration( | 48 registration1_ = new ServiceWorkerRegistration( |
| 49 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); | 49 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); |
| 50 registration2_ = new ServiceWorkerRegistration( | 50 registration2_ = new ServiceWorkerRegistration( |
| 51 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); | 51 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); |
| 52 registration3_ = new ServiceWorkerRegistration( |
| 53 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr()); |
| 52 | 54 |
| 53 // Prepare provider hosts (for the same process). | 55 // Prepare provider hosts (for the same process). |
| 54 std::unique_ptr<ServiceWorkerProviderHost> host1( | 56 std::unique_ptr<ServiceWorkerProviderHost> host1( |
| 55 new ServiceWorkerProviderHost( | 57 new ServiceWorkerProviderHost( |
| 56 helper_->mock_render_process_id(), MSG_ROUTING_NONE, | 58 helper_->mock_render_process_id(), MSG_ROUTING_NONE, |
| 57 1 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 59 1 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 58 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, | 60 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, |
| 59 context_->AsWeakPtr(), NULL)); | 61 context_->AsWeakPtr(), NULL)); |
| 60 host1->SetDocumentUrl(GURL("https://www.example.com/example1.html")); | 62 host1->SetDocumentUrl(GURL("https://www.example.com/example1.html")); |
| 61 std::unique_ptr<ServiceWorkerProviderHost> host2( | 63 std::unique_ptr<ServiceWorkerProviderHost> host2( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 80 | 82 |
| 81 bool PatternHasProcessToRun(const GURL& pattern) const { | 83 bool PatternHasProcessToRun(const GURL& pattern) const { |
| 82 return context_->process_manager()->PatternHasProcessToRun(pattern); | 84 return context_->process_manager()->PatternHasProcessToRun(pattern); |
| 83 } | 85 } |
| 84 | 86 |
| 85 content::TestBrowserThreadBundle thread_bundle_; | 87 content::TestBrowserThreadBundle thread_bundle_; |
| 86 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 88 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 87 ServiceWorkerContextCore* context_; | 89 ServiceWorkerContextCore* context_; |
| 88 scoped_refptr<ServiceWorkerRegistration> registration1_; | 90 scoped_refptr<ServiceWorkerRegistration> registration1_; |
| 89 scoped_refptr<ServiceWorkerRegistration> registration2_; | 91 scoped_refptr<ServiceWorkerRegistration> registration2_; |
| 92 scoped_refptr<ServiceWorkerRegistration> registration3_; |
| 90 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 93 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
| 91 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 94 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
| 92 GURL script_url_; | 95 GURL script_url_; |
| 93 ServiceWorkerTestContentClient test_content_client_; | 96 ServiceWorkerTestContentClient test_content_client_; |
| 94 TestContentBrowserClient test_content_browser_client_; | 97 TestContentBrowserClient test_content_browser_client_; |
| 95 ContentBrowserClient* old_content_browser_client_; | 98 ContentBrowserClient* old_content_browser_client_; |
| 96 | 99 |
| 97 private: | 100 private: |
| 98 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 101 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| 99 }; | 102 }; |
| 100 | 103 |
| 101 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { | 104 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { |
| 102 provider_host1_->AddMatchingRegistration(registration1_.get()); | 105 // Matching registrations have already been set by SetDocumentUrl. |
| 103 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 106 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 104 | 107 |
| 108 // Different matching registrations have already been added. |
| 109 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); |
| 110 |
| 105 // Adding the same registration twice has no effect. | 111 // Adding the same registration twice has no effect. |
| 106 provider_host1_->AddMatchingRegistration(registration1_.get()); | 112 provider_host1_->AddMatchingRegistration(registration1_.get()); |
| 107 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 113 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 108 | 114 |
| 109 // Different matching registrations can be added. | |
| 110 provider_host1_->AddMatchingRegistration(registration2_.get()); | |
| 111 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); | |
| 112 | |
| 113 // Removing a matching registration will decrease the process refs for its | 115 // Removing a matching registration will decrease the process refs for its |
| 114 // pattern. | 116 // pattern. |
| 115 provider_host1_->RemoveMatchingRegistration(registration1_.get()); | 117 provider_host1_->RemoveMatchingRegistration(registration1_.get()); |
| 118 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 119 provider_host2_->RemoveMatchingRegistration(registration1_.get()); |
| 116 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); | 120 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); |
| 117 | 121 |
| 118 // Multiple provider hosts could add the same matching registration. | 122 // Matching registration will be removed when moving out of scope |
| 119 // The process refs will become 0 after all provider hosts removed them. | 123 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host1,2 |
| 120 provider_host2_->AddMatchingRegistration(registration2_.get()); | 124 ASSERT_FALSE(PatternHasProcessToRun(registration3_->pattern())); // no host |
| 121 provider_host1_->RemoveMatchingRegistration(registration2_.get()); | 125 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/")); |
| 122 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); | 126 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host2 |
| 123 provider_host2_->RemoveMatchingRegistration(registration2_.get()); | 127 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1 |
| 124 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); | 128 provider_host2_->SetDocumentUrl(GURL("https://other.example.com/")); |
| 129 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); // no host |
| 130 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1,2 |
| 125 } | 131 } |
| 126 | 132 |
| 127 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { | 133 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { |
| 128 // Associating the registration will also increase the process refs for | 134 // Associating the registration will also increase the process refs for |
| 129 // the registration's pattern. | 135 // the registration's pattern. |
| 130 provider_host1_->AssociateRegistration(registration1_.get(), | 136 provider_host1_->AssociateRegistration(registration1_.get(), |
| 131 false /* notify_controllerchange */); | 137 false /* notify_controllerchange */); |
| 132 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 138 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 133 | 139 |
| 134 // Disassociating the registration shouldn't affect the process refs for | 140 // Disassociating the registration shouldn't affect the process refs for |
| 135 // the registration's pattern. | 141 // the registration's pattern. |
| 136 provider_host1_->DisassociateRegistration(); | 142 provider_host1_->DisassociateRegistration(); |
| 137 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 143 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 138 } | 144 } |
| 139 | 145 |
| 140 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { | 146 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { |
| 141 provider_host1_->AddMatchingRegistration(registration1_.get()); | |
| 142 provider_host1_->AddMatchingRegistration(registration2_.get()); | |
| 143 | |
| 144 // Match registration should return the longest matching one. | 147 // Match registration should return the longest matching one. |
| 145 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_); | 148 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration()); |
| 146 provider_host1_->RemoveMatchingRegistration(registration2_.get()); | 149 provider_host1_->RemoveMatchingRegistration(registration2_.get()); |
| 147 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_); | 150 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration()); |
| 148 | 151 |
| 149 // Should return nullptr after removing all matching registrations. | 152 // Should return nullptr after removing all matching registrations. |
| 150 provider_host1_->RemoveMatchingRegistration(registration1_.get()); | 153 provider_host1_->RemoveMatchingRegistration(registration1_.get()); |
| 151 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr); | 154 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); |
| 155 |
| 156 // SetDocumentUrl sets all of matching registrations |
| 157 provider_host1_->SetDocumentUrl(GURL("https://www.example.com/example1")); |
| 158 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration()); |
| 159 provider_host1_->RemoveMatchingRegistration(registration2_.get()); |
| 160 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration()); |
| 161 |
| 162 // SetDocumentUrl with another origin also updates matching registrations |
| 163 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example")); |
| 164 ASSERT_EQ(registration3_, provider_host1_->MatchRegistration()); |
| 165 provider_host1_->RemoveMatchingRegistration(registration3_.get()); |
| 166 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); |
| 152 } | 167 } |
| 153 | 168 |
| 154 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { | 169 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { |
| 155 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; | 170 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; |
| 156 content::ResetSchemesAndOriginsWhitelistForTesting(); | 171 content::ResetSchemesAndOriginsWhitelistForTesting(); |
| 157 | 172 |
| 158 // Insecure document URL. | 173 // Insecure document URL. |
| 159 provider_host1_->SetDocumentUrl(GURL("http://host")); | 174 provider_host1_->SetDocumentUrl(GURL("http://host")); |
| 160 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | 175 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; |
| 161 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 176 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 178 EXPECT_FALSE(IsOriginSecure(url)); | 193 EXPECT_FALSE(IsOriginSecure(url)); |
| 179 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); | 194 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); |
| 180 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); | 195 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); |
| 181 | 196 |
| 182 // Exceptional service worker scheme with insecure parent frame. | 197 // Exceptional service worker scheme with insecure parent frame. |
| 183 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | 198 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; |
| 184 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 199 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
| 185 } | 200 } |
| 186 | 201 |
| 187 } // namespace content | 202 } // namespace content |
| OLD | NEW |