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