Chromium Code Reviews| 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 18 matching lines...) Expand all Loading... | |
| 29 class ServiceWorkerTestContentClient : public TestContentClient { | 29 class ServiceWorkerTestContentClient : public TestContentClient { |
| 30 public: | 30 public: |
| 31 void AddAdditionalSchemes(Schemes* schemes) override { | 31 void AddAdditionalSchemes(Schemes* schemes) override { |
| 32 schemes->service_worker_schemes.push_back(kServiceWorkerScheme); | 32 schemes->service_worker_schemes.push_back(kServiceWorkerScheme); |
| 33 } | 33 } |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 class ServiceWorkerProviderHostTest : public testing::Test { | 36 class ServiceWorkerProviderHostTest : public testing::Test { |
| 37 protected: | 37 protected: |
| 38 ServiceWorkerProviderHostTest() | 38 ServiceWorkerProviderHostTest() |
| 39 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { | 39 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 40 next_provider_id_(1) { | |
| 40 SetContentClient(&test_content_client_); | 41 SetContentClient(&test_content_client_); |
| 41 } | 42 } |
| 42 ~ServiceWorkerProviderHostTest() override {} | 43 ~ServiceWorkerProviderHostTest() override {} |
| 43 | 44 |
| 44 void SetUp() override { | 45 void SetUp() override { |
| 45 old_content_browser_client_ = | 46 old_content_browser_client_ = |
| 46 SetBrowserClientForTesting(&test_content_browser_client_); | 47 SetBrowserClientForTesting(&test_content_browser_client_); |
| 47 ResetSchemesAndOriginsWhitelist(); | 48 ResetSchemesAndOriginsWhitelist(); |
| 48 | 49 |
| 49 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 50 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 50 context_ = helper_->context(); | 51 context_ = helper_->context(); |
| 51 script_url_ = GURL("https://www.example.com/service_worker.js"); | 52 script_url_ = GURL("https://www.example.com/service_worker.js"); |
| 52 registration1_ = new ServiceWorkerRegistration( | 53 registration1_ = new ServiceWorkerRegistration( |
| 53 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); | 54 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); |
| 54 registration2_ = new ServiceWorkerRegistration( | 55 registration2_ = new ServiceWorkerRegistration( |
| 55 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); | 56 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); |
| 56 registration3_ = new ServiceWorkerRegistration( | 57 registration3_ = new ServiceWorkerRegistration( |
| 57 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr()); | 58 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr()); |
| 58 | |
| 59 // Prepare provider hosts (for the same process). | |
| 60 std::unique_ptr<ServiceWorkerProviderHost> host1( | |
| 61 new ServiceWorkerProviderHost( | |
| 62 helper_->mock_render_process_id(), MSG_ROUTING_NONE, | |
| 63 1 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | |
| 64 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, | |
| 65 context_->AsWeakPtr(), NULL)); | |
| 66 host1->SetDocumentUrl(GURL("https://www.example.com/example1.html")); | |
| 67 std::unique_ptr<ServiceWorkerProviderHost> host2( | |
| 68 new ServiceWorkerProviderHost( | |
| 69 helper_->mock_render_process_id(), MSG_ROUTING_NONE, | |
| 70 2 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | |
| 71 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, | |
| 72 context_->AsWeakPtr(), NULL)); | |
| 73 host2->SetDocumentUrl(GURL("https://www.example.com/example2.html")); | |
| 74 provider_host1_ = host1->AsWeakPtr(); | |
| 75 provider_host2_ = host2->AsWeakPtr(); | |
| 76 context_->AddProviderHost(base::WrapUnique(host1.release())); | |
| 77 context_->AddProviderHost(base::WrapUnique(host2.release())); | |
| 78 } | 59 } |
| 79 | 60 |
| 80 void TearDown() override { | 61 void TearDown() override { |
| 81 registration1_ = 0; | 62 registration1_ = 0; |
| 82 registration2_ = 0; | 63 registration2_ = 0; |
| 83 helper_.reset(); | 64 helper_.reset(); |
| 84 SetBrowserClientForTesting(old_content_browser_client_); | 65 SetBrowserClientForTesting(old_content_browser_client_); |
| 85 // Reset cached security schemes so we don't affect other tests. | 66 // Reset cached security schemes so we don't affect other tests. |
| 86 ResetSchemesAndOriginsWhitelist(); | 67 ResetSchemesAndOriginsWhitelist(); |
| 87 } | 68 } |
| 88 | 69 |
| 89 bool PatternHasProcessToRun(const GURL& pattern) const { | 70 bool PatternHasProcessToRun(const GURL& pattern) const { |
| 90 return context_->process_manager()->PatternHasProcessToRun(pattern); | 71 return context_->process_manager()->PatternHasProcessToRun(pattern); |
| 91 } | 72 } |
| 92 | 73 |
| 74 ServiceWorkerProviderHost* CreateProviderHost(const GURL& document_url) { | |
| 75 std::unique_ptr<ServiceWorkerProviderHost> host = | |
|
dcheng
2017/02/14 08:47:46
#include <memory> for unique_ptr
shimazu
2017/02/15 02:24:18
Done.
| |
| 76 CreateProviderHostForWindow( | |
| 77 helper_->mock_render_process_id(), next_provider_id_++, | |
| 78 true /* is_parent_frame_secure */, helper_->context()->AsWeakPtr()); | |
| 79 ServiceWorkerProviderHost* host_raw = host.get(); | |
| 80 host->SetDocumentUrl(document_url); | |
| 81 context_->AddProviderHost(std::move(host)); | |
|
dcheng
2017/02/14 08:47:46
#include <utility> for std::move
shimazu
2017/02/15 02:24:18
Done.
| |
| 82 return host_raw; | |
| 83 } | |
| 84 | |
| 85 ServiceWorkerProviderHost* CreateProviderHostWithInsecureParentFrame( | |
| 86 const GURL& document_url) { | |
| 87 std::unique_ptr<ServiceWorkerProviderHost> host = | |
| 88 CreateProviderHostForWindow(helper_->mock_render_process_id(), | |
| 89 next_provider_id_++, | |
| 90 false /* is_parent_frame_secure */, | |
| 91 helper_->context()->AsWeakPtr()); | |
| 92 ServiceWorkerProviderHost* host_raw = host.get(); | |
| 93 host->SetDocumentUrl(document_url); | |
| 94 context_->AddProviderHost(std::move(host)); | |
| 95 return host_raw; | |
| 96 } | |
| 97 | |
| 93 TestBrowserThreadBundle thread_bundle_; | 98 TestBrowserThreadBundle thread_bundle_; |
| 94 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 99 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 95 ServiceWorkerContextCore* context_; | 100 ServiceWorkerContextCore* context_; |
| 96 scoped_refptr<ServiceWorkerRegistration> registration1_; | 101 scoped_refptr<ServiceWorkerRegistration> registration1_; |
| 97 scoped_refptr<ServiceWorkerRegistration> registration2_; | 102 scoped_refptr<ServiceWorkerRegistration> registration2_; |
| 98 scoped_refptr<ServiceWorkerRegistration> registration3_; | 103 scoped_refptr<ServiceWorkerRegistration> registration3_; |
| 99 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | |
| 100 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | |
| 101 GURL script_url_; | 104 GURL script_url_; |
| 102 ServiceWorkerTestContentClient test_content_client_; | 105 ServiceWorkerTestContentClient test_content_client_; |
| 103 TestContentBrowserClient test_content_browser_client_; | 106 TestContentBrowserClient test_content_browser_client_; |
| 104 ContentBrowserClient* old_content_browser_client_; | 107 ContentBrowserClient* old_content_browser_client_; |
| 108 int next_provider_id_; | |
| 105 | 109 |
| 106 private: | 110 private: |
| 107 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 111 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| 108 }; | 112 }; |
| 109 | 113 |
| 110 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { | 114 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { |
| 115 ServiceWorkerProviderHost* provider_host1 = | |
| 116 CreateProviderHost(GURL("https://www.example.com/example1.html")); | |
| 117 ServiceWorkerProviderHost* provider_host2 = | |
| 118 CreateProviderHost(GURL("https://www.example.com/example2.html")); | |
| 119 | |
| 111 // Matching registrations have already been set by SetDocumentUrl. | 120 // Matching registrations have already been set by SetDocumentUrl. |
| 112 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 121 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 113 | 122 |
| 114 // Different matching registrations have already been added. | 123 // Different matching registrations have already been added. |
| 115 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); | 124 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); |
| 116 | 125 |
| 117 // Adding the same registration twice has no effect. | 126 // Adding the same registration twice has no effect. |
| 118 provider_host1_->AddMatchingRegistration(registration1_.get()); | 127 provider_host1->AddMatchingRegistration(registration1_.get()); |
| 119 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 128 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 120 | 129 |
| 121 // Removing a matching registration will decrease the process refs for its | 130 // Removing a matching registration will decrease the process refs for its |
| 122 // pattern. | 131 // pattern. |
| 123 provider_host1_->RemoveMatchingRegistration(registration1_.get()); | 132 provider_host1->RemoveMatchingRegistration(registration1_.get()); |
| 124 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 133 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 125 provider_host2_->RemoveMatchingRegistration(registration1_.get()); | 134 provider_host2->RemoveMatchingRegistration(registration1_.get()); |
| 126 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); | 135 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); |
| 127 | 136 |
| 128 // Matching registration will be removed when moving out of scope | 137 // Matching registration will be removed when moving out of scope |
| 129 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host1,2 | 138 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host1,2 |
| 130 ASSERT_FALSE(PatternHasProcessToRun(registration3_->pattern())); // no host | 139 ASSERT_FALSE(PatternHasProcessToRun(registration3_->pattern())); // no host |
| 131 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/")); | 140 provider_host1->SetDocumentUrl(GURL("https://other.example.com/")); |
| 132 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host2 | 141 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host2 |
| 133 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1 | 142 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1 |
| 134 provider_host2_->SetDocumentUrl(GURL("https://other.example.com/")); | 143 provider_host2->SetDocumentUrl(GURL("https://other.example.com/")); |
| 135 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); // no host | 144 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); // no host |
| 136 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1,2 | 145 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1,2 |
| 137 } | 146 } |
| 138 | 147 |
| 139 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { | 148 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { |
| 149 ServiceWorkerProviderHost* provider_host1 = | |
| 150 CreateProviderHost(GURL("https://www.example.com/example1.html")); | |
| 151 | |
| 140 // Associating the registration will also increase the process refs for | 152 // Associating the registration will also increase the process refs for |
| 141 // the registration's pattern. | 153 // the registration's pattern. |
| 142 provider_host1_->AssociateRegistration(registration1_.get(), | 154 provider_host1->AssociateRegistration(registration1_.get(), |
| 143 false /* notify_controllerchange */); | 155 false /* notify_controllerchange */); |
| 144 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 156 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 145 | 157 |
| 146 // Disassociating the registration shouldn't affect the process refs for | 158 // Disassociating the registration shouldn't affect the process refs for |
| 147 // the registration's pattern. | 159 // the registration's pattern. |
| 148 provider_host1_->DisassociateRegistration(); | 160 provider_host1->DisassociateRegistration(); |
| 149 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 161 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
| 150 } | 162 } |
| 151 | 163 |
| 152 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { | 164 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { |
| 165 ServiceWorkerProviderHost* provider_host1 = | |
| 166 CreateProviderHost(GURL("https://www.example.com/example1.html")); | |
| 167 | |
| 153 // Match registration should return the longest matching one. | 168 // Match registration should return the longest matching one. |
| 154 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration()); | 169 ASSERT_EQ(registration2_, provider_host1->MatchRegistration()); |
| 155 provider_host1_->RemoveMatchingRegistration(registration2_.get()); | 170 provider_host1->RemoveMatchingRegistration(registration2_.get()); |
| 156 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration()); | 171 ASSERT_EQ(registration1_, provider_host1->MatchRegistration()); |
| 157 | 172 |
| 158 // Should return nullptr after removing all matching registrations. | 173 // Should return nullptr after removing all matching registrations. |
| 159 provider_host1_->RemoveMatchingRegistration(registration1_.get()); | 174 provider_host1->RemoveMatchingRegistration(registration1_.get()); |
| 160 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); | 175 ASSERT_EQ(nullptr, provider_host1->MatchRegistration()); |
| 161 | 176 |
| 162 // SetDocumentUrl sets all of matching registrations | 177 // SetDocumentUrl sets all of matching registrations |
| 163 provider_host1_->SetDocumentUrl(GURL("https://www.example.com/example1")); | 178 provider_host1->SetDocumentUrl(GURL("https://www.example.com/example1")); |
| 164 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration()); | 179 ASSERT_EQ(registration2_, provider_host1->MatchRegistration()); |
| 165 provider_host1_->RemoveMatchingRegistration(registration2_.get()); | 180 provider_host1->RemoveMatchingRegistration(registration2_.get()); |
| 166 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration()); | 181 ASSERT_EQ(registration1_, provider_host1->MatchRegistration()); |
| 167 | 182 |
| 168 // SetDocumentUrl with another origin also updates matching registrations | 183 // SetDocumentUrl with another origin also updates matching registrations |
| 169 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example")); | 184 provider_host1->SetDocumentUrl(GURL("https://other.example.com/example")); |
| 170 ASSERT_EQ(registration3_, provider_host1_->MatchRegistration()); | 185 ASSERT_EQ(registration3_, provider_host1->MatchRegistration()); |
| 171 provider_host1_->RemoveMatchingRegistration(registration3_.get()); | 186 provider_host1->RemoveMatchingRegistration(registration3_.get()); |
| 172 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); | 187 ASSERT_EQ(nullptr, provider_host1->MatchRegistration()); |
| 173 } | 188 } |
| 174 | 189 |
| 175 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { | 190 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { |
| 176 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; | 191 ServiceWorkerProviderHost* provider_host_secure_parent = |
| 192 CreateProviderHost(GURL("https://www.example.com/example1.html")); | |
| 193 ServiceWorkerProviderHost* provider_host_insecure_parent = | |
| 194 CreateProviderHostWithInsecureParentFrame( | |
| 195 GURL("https://www.example.com/example1.html")); | |
| 177 | 196 |
| 178 // Insecure document URL. | 197 // Insecure document URL. |
| 179 provider_host1_->SetDocumentUrl(GURL("http://host")); | 198 provider_host_secure_parent->SetDocumentUrl(GURL("http://host")); |
| 180 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | 199 EXPECT_FALSE(provider_host_secure_parent->IsContextSecureForServiceWorker()); |
| 181 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | |
| 182 | 200 |
| 183 // Insecure parent frame. | 201 // Insecure parent frame. |
| 184 provider_host1_->SetDocumentUrl(GURL("https://host")); | 202 provider_host_insecure_parent->SetDocumentUrl(GURL("https://host")); |
| 185 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | 203 EXPECT_FALSE( |
| 186 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 204 provider_host_insecure_parent->IsContextSecureForServiceWorker()); |
| 187 | 205 |
| 188 // Secure URL and parent frame. | 206 // Secure URL and parent frame. |
| 189 provider_host1_->SetDocumentUrl(GURL("https://host")); | 207 provider_host_secure_parent->SetDocumentUrl(GURL("https://host")); |
| 190 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | 208 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker()); |
| 191 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); | |
| 192 | 209 |
| 193 // Exceptional service worker scheme. | 210 // Exceptional service worker scheme. |
| 194 GURL url(std::string(kServiceWorkerScheme) + "://host"); | 211 GURL url(std::string(kServiceWorkerScheme) + "://host"); |
| 195 EXPECT_TRUE(url.is_valid()); | 212 EXPECT_TRUE(url.is_valid()); |
| 196 provider_host1_->SetDocumentUrl(url); | |
| 197 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | |
| 198 EXPECT_FALSE(IsOriginSecure(url)); | 213 EXPECT_FALSE(IsOriginSecure(url)); |
| 199 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); | 214 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); |
| 200 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); | 215 provider_host_secure_parent->SetDocumentUrl(url); |
| 216 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker()); | |
| 201 | 217 |
| 202 // Exceptional service worker scheme with insecure parent frame. | 218 // Exceptional service worker scheme with insecure parent frame. |
| 203 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | 219 provider_host_insecure_parent->SetDocumentUrl(url); |
| 204 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 220 EXPECT_FALSE( |
| 221 provider_host_insecure_parent->IsContextSecureForServiceWorker()); | |
| 205 } | 222 } |
| 206 | 223 |
| 207 } // namespace content | 224 } // namespace content |
| OLD | NEW |