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" |
11 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 11 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
12 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
13 #include "content/browser/service_worker/service_worker_register_job.h" | 13 #include "content/browser/service_worker/service_worker_register_job.h" |
14 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
15 #include "content/browser/service_worker/service_worker_test_utils.h" | 15 #include "content/browser/service_worker/service_worker_test_utils.h" |
16 #include "content/browser/service_worker/service_worker_version.h" | 16 #include "content/browser/service_worker/service_worker_version.h" |
| 17 #include "content/common/url_schemes.h" |
17 #include "content/public/common/origin_util.h" | 18 #include "content/public/common/origin_util.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "content/test/test_content_browser_client.h" | 20 #include "content/test/test_content_browser_client.h" |
20 #include "content/test/test_content_client.h" | 21 #include "content/test/test_content_client.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 namespace content { | 24 namespace content { |
24 | 25 |
25 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; | 26 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; |
26 | 27 |
27 class ServiceWorkerTestContentClient : public TestContentClient { | 28 class ServiceWorkerTestContentClient : public TestContentClient { |
28 public: | 29 public: |
29 void AddServiceWorkerSchemes(std::set<std::string>* schemes) override { | 30 void AddAdditionalSchemes(Schemes* schemes) override { |
30 schemes->insert(kServiceWorkerScheme); | 31 schemes->service_worker_schemes.push_back(kServiceWorkerScheme); |
31 } | 32 } |
32 }; | 33 }; |
33 | 34 |
34 class ServiceWorkerProviderHostTest : public testing::Test { | 35 class ServiceWorkerProviderHostTest : public testing::Test { |
35 protected: | 36 protected: |
36 ServiceWorkerProviderHostTest() | 37 ServiceWorkerProviderHostTest() |
37 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { | 38 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { |
38 SetContentClient(&test_content_client_); | 39 SetContentClient(&test_content_client_); |
39 } | 40 } |
40 ~ServiceWorkerProviderHostTest() override {} | 41 ~ServiceWorkerProviderHostTest() override {} |
41 | 42 |
42 void SetUp() override { | 43 void SetUp() override { |
43 old_content_browser_client_ = | 44 old_content_browser_client_ = |
44 SetBrowserClientForTesting(&test_content_browser_client_); | 45 SetBrowserClientForTesting(&test_content_browser_client_); |
| 46 RefreshSecuritySchemesForTesting(); |
45 | 47 |
46 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 48 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
47 context_ = helper_->context(); | 49 context_ = helper_->context(); |
48 script_url_ = GURL("https://www.example.com/service_worker.js"); | 50 script_url_ = GURL("https://www.example.com/service_worker.js"); |
49 registration1_ = new ServiceWorkerRegistration( | 51 registration1_ = new ServiceWorkerRegistration( |
50 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); | 52 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); |
51 registration2_ = new ServiceWorkerRegistration( | 53 registration2_ = new ServiceWorkerRegistration( |
52 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); | 54 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); |
53 registration3_ = new ServiceWorkerRegistration( | 55 registration3_ = new ServiceWorkerRegistration( |
54 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr()); | 56 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr()); |
(...skipping 17 matching lines...) Expand all Loading... |
72 provider_host2_ = host2->AsWeakPtr(); | 74 provider_host2_ = host2->AsWeakPtr(); |
73 context_->AddProviderHost(base::WrapUnique(host1.release())); | 75 context_->AddProviderHost(base::WrapUnique(host1.release())); |
74 context_->AddProviderHost(base::WrapUnique(host2.release())); | 76 context_->AddProviderHost(base::WrapUnique(host2.release())); |
75 } | 77 } |
76 | 78 |
77 void TearDown() override { | 79 void TearDown() override { |
78 registration1_ = 0; | 80 registration1_ = 0; |
79 registration2_ = 0; | 81 registration2_ = 0; |
80 helper_.reset(); | 82 helper_.reset(); |
81 SetBrowserClientForTesting(old_content_browser_client_); | 83 SetBrowserClientForTesting(old_content_browser_client_); |
| 84 // Reset cached security schemes so we don't affect other tests. |
| 85 RefreshSecuritySchemesForTesting(); |
82 } | 86 } |
83 | 87 |
84 bool PatternHasProcessToRun(const GURL& pattern) const { | 88 bool PatternHasProcessToRun(const GURL& pattern) const { |
85 return context_->process_manager()->PatternHasProcessToRun(pattern); | 89 return context_->process_manager()->PatternHasProcessToRun(pattern); |
86 } | 90 } |
87 | 91 |
88 content::TestBrowserThreadBundle thread_bundle_; | 92 TestBrowserThreadBundle thread_bundle_; |
89 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 93 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
90 ServiceWorkerContextCore* context_; | 94 ServiceWorkerContextCore* context_; |
91 scoped_refptr<ServiceWorkerRegistration> registration1_; | 95 scoped_refptr<ServiceWorkerRegistration> registration1_; |
92 scoped_refptr<ServiceWorkerRegistration> registration2_; | 96 scoped_refptr<ServiceWorkerRegistration> registration2_; |
93 scoped_refptr<ServiceWorkerRegistration> registration3_; | 97 scoped_refptr<ServiceWorkerRegistration> registration3_; |
94 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 98 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
95 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 99 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
96 GURL script_url_; | 100 GURL script_url_; |
97 ServiceWorkerTestContentClient test_content_client_; | 101 ServiceWorkerTestContentClient test_content_client_; |
98 TestContentBrowserClient test_content_browser_client_; | 102 TestContentBrowserClient test_content_browser_client_; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 | 169 |
166 // SetDocumentUrl with another origin also updates matching registrations | 170 // SetDocumentUrl with another origin also updates matching registrations |
167 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example")); | 171 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example")); |
168 ASSERT_EQ(registration3_, provider_host1_->MatchRegistration()); | 172 ASSERT_EQ(registration3_, provider_host1_->MatchRegistration()); |
169 provider_host1_->RemoveMatchingRegistration(registration3_.get()); | 173 provider_host1_->RemoveMatchingRegistration(registration3_.get()); |
170 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); | 174 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); |
171 } | 175 } |
172 | 176 |
173 TEST_P(ServiceWorkerProviderHostTestP, ContextSecurity) { | 177 TEST_P(ServiceWorkerProviderHostTestP, ContextSecurity) { |
174 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; | 178 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; |
175 content::ResetSchemesAndOriginsWhitelistForTesting(); | |
176 | 179 |
177 // Insecure document URL. | 180 // Insecure document URL. |
178 provider_host1_->SetDocumentUrl(GURL("http://host")); | 181 provider_host1_->SetDocumentUrl(GURL("http://host")); |
179 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | 182 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; |
180 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 183 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
181 | 184 |
182 // Insecure parent frame. | 185 // Insecure parent frame. |
183 provider_host1_->SetDocumentUrl(GURL("https://host")); | 186 provider_host1_->SetDocumentUrl(GURL("https://host")); |
184 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | 187 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; |
185 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 188 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
(...skipping 15 matching lines...) Expand all Loading... |
201 // Exceptional service worker scheme with insecure parent frame. | 204 // Exceptional service worker scheme with insecure parent frame. |
202 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | 205 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; |
203 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 206 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
204 } | 207 } |
205 | 208 |
206 INSTANTIATE_TEST_CASE_P(ServiceWorkerProviderHostTest, | 209 INSTANTIATE_TEST_CASE_P(ServiceWorkerProviderHostTest, |
207 ServiceWorkerProviderHostTestP, | 210 ServiceWorkerProviderHostTestP, |
208 ::testing::Values(false, true)); | 211 ::testing::Values(false, true)); |
209 | 212 |
210 } // namespace content | 213 } // namespace content |
OLD | NEW |