| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/link_header_support.h" | 5 #include "content/browser/service_worker/link_header_support.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "content/browser/loader/resource_request_info_impl.h" | 10 #include "content/browser/loader/resource_request_info_impl.h" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 CreateProviderHostForServiceWorkerContext( | 111 CreateProviderHostForServiceWorkerContext( |
| 112 render_process_id(), kMockProviderId, | 112 render_process_id(), kMockProviderId, |
| 113 true /* is_parent_frame_secure */, context()->AsWeakPtr(), | 113 true /* is_parent_frame_secure */, context()->AsWeakPtr(), |
| 114 &remote_endpoints_.back()); | 114 &remote_endpoints_.back()); |
| 115 provider_host_ = host->AsWeakPtr(); | 115 provider_host_ = host->AsWeakPtr(); |
| 116 EXPECT_FALSE( | 116 EXPECT_FALSE( |
| 117 context()->GetProviderHost(host->process_id(), host->provider_id())); | 117 context()->GetProviderHost(host->process_id(), host->provider_id())); |
| 118 context()->AddProviderHost(std::move(host)); | 118 context()->AddProviderHost(std::move(host)); |
| 119 | 119 |
| 120 scoped_refptr<ServiceWorkerRegistration> registration = | 120 scoped_refptr<ServiceWorkerRegistration> registration = |
| 121 new ServiceWorkerRegistration(GURL("https://host/scope"), 1L, | 121 new ServiceWorkerRegistration( |
| 122 context()->AsWeakPtr()); | 122 ServiceWorkerRegistrationOptions(GURL("https://host/scope")), 1L, |
| 123 context()->AsWeakPtr()); |
| 123 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion( | 124 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion( |
| 124 registration.get(), GURL("https://host/script.js"), 1L, | 125 registration.get(), GURL("https://host/script.js"), 1L, |
| 125 context()->AsWeakPtr()); | 126 context()->AsWeakPtr()); |
| 126 | 127 |
| 127 provider_host_->running_hosted_version_ = version; | 128 provider_host_->running_hosted_version_ = version; |
| 128 } | 129 } |
| 129 | 130 |
| 130 std::unique_ptr<net::URLRequest> CreateRequest(const GURL& request_url, | 131 std::unique_ptr<net::URLRequest> CreateRequest(const GURL& request_url, |
| 131 ResourceType resource_type, | 132 ResourceType resource_type, |
| 132 int provider_id) { | 133 int provider_id) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 provider_host()->provider_id()) | 188 provider_host()->provider_id()) |
| 188 .get(), | 189 .get(), |
| 189 "<../foo.js>; rel=serviceworker", context_wrapper()); | 190 "<../foo.js>; rel=serviceworker", context_wrapper()); |
| 190 base::RunLoop().RunUntilIdle(); | 191 base::RunLoop().RunUntilIdle(); |
| 191 | 192 |
| 192 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 193 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 193 ASSERT_EQ(1u, registrations.size()); | 194 ASSERT_EQ(1u, registrations.size()); |
| 194 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); | 195 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 195 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), | 196 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 196 registrations[0].active_version.script_url); | 197 registrations[0].active_version.script_url); |
| 198 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 199 registrations[0].update_via_cache); |
| 197 } | 200 } |
| 198 | 201 |
| 199 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeWithFragment) { | 202 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeWithFragment) { |
| 200 CreateDocumentProviderHost(); | 203 CreateDocumentProviderHost(); |
| 201 ProcessLinkHeaderForRequest( | 204 ProcessLinkHeaderForRequest( |
| 202 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), | 205 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 203 provider_host()->provider_id()) | 206 provider_host()->provider_id()) |
| 204 .get(), | 207 .get(), |
| 205 "<../bar.js>; rel=serviceworker; scope=\"scope#ref\"", context_wrapper()); | 208 "<../bar.js>; rel=serviceworker; scope=\"scope#ref\"", context_wrapper()); |
| 206 base::RunLoop().RunUntilIdle(); | 209 base::RunLoop().RunUntilIdle(); |
| 207 | 210 |
| 208 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 211 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 209 ASSERT_EQ(1u, registrations.size()); | 212 ASSERT_EQ(1u, registrations.size()); |
| 210 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), | 213 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), |
| 211 registrations[0].pattern); | 214 registrations[0].pattern); |
| 212 EXPECT_EQ(GURL("https://example.com/foo/bar.js"), | 215 EXPECT_EQ(GURL("https://example.com/foo/bar.js"), |
| 213 registrations[0].active_version.script_url); | 216 registrations[0].active_version.script_url); |
| 217 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 218 registrations[0].update_via_cache); |
| 214 } | 219 } |
| 215 | 220 |
| 216 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeAbsoluteUrl) { | 221 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeAbsoluteUrl) { |
| 217 CreateDocumentProviderHost(); | 222 CreateDocumentProviderHost(); |
| 218 ProcessLinkHeaderForRequest( | 223 ProcessLinkHeaderForRequest( |
| 219 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), | 224 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 220 provider_host()->provider_id()) | 225 provider_host()->provider_id()) |
| 221 .get(), | 226 .get(), |
| 222 "<bar.js>; rel=serviceworker; " | 227 "<bar.js>; rel=serviceworker; " |
| 223 "scope=\"https://example.com:443/foo/bar/scope\"", | 228 "scope=\"https://example.com:443/foo/bar/scope\"", |
| 224 context_wrapper()); | 229 context_wrapper()); |
| 225 base::RunLoop().RunUntilIdle(); | 230 base::RunLoop().RunUntilIdle(); |
| 226 | 231 |
| 227 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 232 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 228 ASSERT_EQ(1u, registrations.size()); | 233 ASSERT_EQ(1u, registrations.size()); |
| 229 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), | 234 EXPECT_EQ(GURL("https://example.com/foo/bar/scope"), |
| 230 registrations[0].pattern); | 235 registrations[0].pattern); |
| 231 EXPECT_EQ(GURL("https://example.com/foo/bar/bar.js"), | 236 EXPECT_EQ(GURL("https://example.com/foo/bar/bar.js"), |
| 232 registrations[0].active_version.script_url); | 237 registrations[0].active_version.script_url); |
| 238 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 239 registrations[0].update_via_cache); |
| 233 } | 240 } |
| 234 | 241 |
| 235 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeDifferentOrigin) { | 242 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeDifferentOrigin) { |
| 236 CreateDocumentProviderHost(); | 243 CreateDocumentProviderHost(); |
| 237 ProcessLinkHeaderForRequest( | 244 ProcessLinkHeaderForRequest( |
| 238 CreateSubresourceRequest(GURL("https://example.com/foobar/"), | 245 CreateSubresourceRequest(GURL("https://example.com/foobar/"), |
| 239 provider_host()->provider_id()) | 246 provider_host()->provider_id()) |
| 240 .get(), | 247 .get(), |
| 241 "<bar.js>; rel=serviceworker; scope=\"https://google.com/scope\"", | 248 "<bar.js>; rel=serviceworker; scope=\"https://google.com/scope\"", |
| 242 context_wrapper()); | 249 context_wrapper()); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 .get(), | 288 .get(), |
| 282 "<https://example.com/bar.js>; rel=serviceworker; scope=foo", | 289 "<https://example.com/bar.js>; rel=serviceworker; scope=foo", |
| 283 context_wrapper()); | 290 context_wrapper()); |
| 284 base::RunLoop().RunUntilIdle(); | 291 base::RunLoop().RunUntilIdle(); |
| 285 | 292 |
| 286 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 293 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 287 ASSERT_EQ(1u, registrations.size()); | 294 ASSERT_EQ(1u, registrations.size()); |
| 288 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); | 295 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); |
| 289 EXPECT_EQ(GURL("https://example.com/bar.js"), | 296 EXPECT_EQ(GURL("https://example.com/bar.js"), |
| 290 registrations[0].active_version.script_url); | 297 registrations[0].active_version.script_url); |
| 298 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 299 registrations[0].update_via_cache); |
| 291 } | 300 } |
| 292 | 301 |
| 293 TEST_F(LinkHeaderServiceWorkerTest, | 302 TEST_F(LinkHeaderServiceWorkerTest, |
| 294 InstallServiceWorker_ScriptDifferentOrigin) { | 303 InstallServiceWorker_ScriptDifferentOrigin) { |
| 295 CreateDocumentProviderHost(); | 304 CreateDocumentProviderHost(); |
| 296 ProcessLinkHeaderForRequest( | 305 ProcessLinkHeaderForRequest( |
| 297 CreateSubresourceRequest(GURL("https://example.com/foobar/"), | 306 CreateSubresourceRequest(GURL("https://example.com/foobar/"), |
| 298 provider_host()->provider_id()) | 307 provider_host()->provider_id()) |
| 299 .get(), | 308 .get(), |
| 300 "<https://google.com/bar.js>; rel=serviceworker; scope=foo", | 309 "<https://google.com/bar.js>; rel=serviceworker; scope=foo", |
| 301 context_wrapper()); | 310 context_wrapper()); |
| 302 base::RunLoop().RunUntilIdle(); | 311 base::RunLoop().RunUntilIdle(); |
| 303 | 312 |
| 304 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 313 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 305 ASSERT_EQ(0u, registrations.size()); | 314 ASSERT_EQ(0u, registrations.size()); |
| 306 } | 315 } |
| 307 | 316 |
| 317 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_CacheNone) { |
| 318 CreateDocumentProviderHost(); |
| 319 ProcessLinkHeaderForRequest( |
| 320 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 321 provider_host()->provider_id()) |
| 322 .get(), |
| 323 "<../foo.js>; rel=serviceworker; updateviacache=none", context_wrapper()); |
| 324 base::RunLoop().RunUntilIdle(); |
| 325 |
| 326 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 327 ASSERT_EQ(1u, registrations.size()); |
| 328 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 329 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 330 registrations[0].active_version.script_url); |
| 331 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kNone, |
| 332 registrations[0].update_via_cache); |
| 333 } |
| 334 |
| 335 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_CacheImports) { |
| 336 CreateDocumentProviderHost(); |
| 337 ProcessLinkHeaderForRequest( |
| 338 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 339 provider_host()->provider_id()) |
| 340 .get(), |
| 341 "<../foo.js>; rel=serviceworker; updateviacache=imports", |
| 342 context_wrapper()); |
| 343 base::RunLoop().RunUntilIdle(); |
| 344 |
| 345 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 346 ASSERT_EQ(1u, registrations.size()); |
| 347 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 348 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 349 registrations[0].active_version.script_url); |
| 350 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 351 registrations[0].update_via_cache); |
| 352 } |
| 353 |
| 354 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_CacheAll) { |
| 355 CreateDocumentProviderHost(); |
| 356 ProcessLinkHeaderForRequest( |
| 357 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 358 provider_host()->provider_id()) |
| 359 .get(), |
| 360 "<../foo.js>; rel=serviceworker; updateviacache=all", context_wrapper()); |
| 361 base::RunLoop().RunUntilIdle(); |
| 362 |
| 363 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 364 ASSERT_EQ(1u, registrations.size()); |
| 365 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 366 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 367 registrations[0].active_version.script_url); |
| 368 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kAll, |
| 369 registrations[0].update_via_cache); |
| 370 } |
| 371 |
| 372 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_CacheInvalidValue) { |
| 373 CreateDocumentProviderHost(); |
| 374 ProcessLinkHeaderForRequest( |
| 375 CreateSubresourceRequest(GURL("https://example.com/foo/bar/"), |
| 376 provider_host()->provider_id()) |
| 377 .get(), |
| 378 "<../foo.js>; rel=serviceworker; updateviacache=invalidvalue", |
| 379 context_wrapper()); |
| 380 base::RunLoop().RunUntilIdle(); |
| 381 |
| 382 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 383 ASSERT_EQ(1u, registrations.size()); |
| 384 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 385 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 386 registrations[0].active_version.script_url); |
| 387 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 388 registrations[0].update_via_cache); |
| 389 } |
| 390 |
| 308 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_MultipleWorkers) { | 391 TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_MultipleWorkers) { |
| 309 CreateDocumentProviderHost(); | 392 CreateDocumentProviderHost(); |
| 310 ProcessLinkHeaderForRequest( | 393 ProcessLinkHeaderForRequest( |
| 311 CreateSubresourceRequest(GURL("https://example.com/foobar/"), | 394 CreateSubresourceRequest(GURL("https://example.com/foobar/"), |
| 312 provider_host()->provider_id()) | 395 provider_host()->provider_id()) |
| 313 .get(), | 396 .get(), |
| 314 "<bar.js>; rel=serviceworker; scope=foo, <baz.js>; " | 397 "<bar.js>; rel=serviceworker; scope=foo, <baz.js>; " |
| 315 "rel=serviceworker; scope=scope", | 398 "rel=serviceworker; scope=scope; updateviacache=all", |
| 316 context_wrapper()); | 399 context_wrapper()); |
| 317 base::RunLoop().RunUntilIdle(); | 400 base::RunLoop().RunUntilIdle(); |
| 318 | 401 |
| 319 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 402 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 320 ASSERT_EQ(2u, registrations.size()); | 403 ASSERT_EQ(2u, registrations.size()); |
| 321 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); | 404 EXPECT_EQ(GURL("https://example.com/foobar/foo"), registrations[0].pattern); |
| 322 EXPECT_EQ(GURL("https://example.com/foobar/bar.js"), | 405 EXPECT_EQ(GURL("https://example.com/foobar/bar.js"), |
| 323 registrations[0].active_version.script_url); | 406 registrations[0].active_version.script_url); |
| 407 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kAll, |
| 408 registrations[0].update_via_cache); |
| 324 EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[1].pattern); | 409 EXPECT_EQ(GURL("https://example.com/foobar/scope"), registrations[1].pattern); |
| 325 EXPECT_EQ(GURL("https://example.com/foobar/baz.js"), | 410 EXPECT_EQ(GURL("https://example.com/foobar/baz.js"), |
| 326 registrations[1].active_version.script_url); | 411 registrations[1].active_version.script_url); |
| 412 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kAll, |
| 413 registrations[1].update_via_cache); |
| 327 } | 414 } |
| 328 | 415 |
| 329 TEST_F(LinkHeaderServiceWorkerTest, | 416 TEST_F(LinkHeaderServiceWorkerTest, |
| 330 InstallServiceWorker_ValidAndInvalidValues) { | 417 InstallServiceWorker_ValidAndInvalidValues) { |
| 331 CreateDocumentProviderHost(); | 418 CreateDocumentProviderHost(); |
| 332 ProcessLinkHeaderForRequest( | 419 ProcessLinkHeaderForRequest( |
| 333 CreateSubresourceRequest(GURL("https://example.com/foobar/"), | 420 CreateSubresourceRequest(GURL("https://example.com/foobar/"), |
| 334 provider_host()->provider_id()) | 421 provider_host()->provider_id()) |
| 335 .get(), | 422 .get(), |
| 336 "<https://google.com/bar.js>; rel=serviceworker; scope=foo, <baz.js>; " | 423 "<https://google.com/bar.js>; rel=serviceworker; scope=foo, <baz.js>; " |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 | 459 |
| 373 ProcessLinkHeaderForRequest(request.get(), "<../foo.js>; rel=serviceworker", | 460 ProcessLinkHeaderForRequest(request.get(), "<../foo.js>; rel=serviceworker", |
| 374 context_wrapper()); | 461 context_wrapper()); |
| 375 base::RunLoop().RunUntilIdle(); | 462 base::RunLoop().RunUntilIdle(); |
| 376 | 463 |
| 377 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 464 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 378 ASSERT_EQ(1u, registrations.size()); | 465 ASSERT_EQ(1u, registrations.size()); |
| 379 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); | 466 EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
| 380 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), | 467 EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
| 381 registrations[0].active_version.script_url); | 468 registrations[0].active_version.script_url); |
| 469 EXPECT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 470 registrations[0].update_via_cache); |
| 382 } | 471 } |
| 383 | 472 |
| 384 TEST_F(LinkHeaderServiceWorkerTest, | 473 TEST_F(LinkHeaderServiceWorkerTest, |
| 385 InstallServiceWorker_NavigationToInsecureContext) { | 474 InstallServiceWorker_NavigationToInsecureContext) { |
| 386 CreateDocumentProviderHost(); | 475 CreateDocumentProviderHost(); |
| 387 provider_host()->SetDocumentUrl(GURL("http://example.com/foo/bar/")); | 476 provider_host()->SetDocumentUrl(GURL("http://example.com/foo/bar/")); |
| 388 ProcessLinkHeaderForRequest( | 477 ProcessLinkHeaderForRequest( |
| 389 CreateRequest(GURL("http://example.com/foo/bar/"), | 478 CreateRequest(GURL("http://example.com/foo/bar/"), |
| 390 RESOURCE_TYPE_MAIN_FRAME, provider_host()->provider_id()) | 479 RESOURCE_TYPE_MAIN_FRAME, provider_host()->provider_id()) |
| 391 .get(), | 480 .get(), |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 provider_host()->provider_id()) | 531 provider_host()->provider_id()) |
| 443 .get(), | 532 .get(), |
| 444 "<../foo.js>; rel=serviceworker", context_wrapper()); | 533 "<../foo.js>; rel=serviceworker", context_wrapper()); |
| 445 base::RunLoop().RunUntilIdle(); | 534 base::RunLoop().RunUntilIdle(); |
| 446 | 535 |
| 447 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 536 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 448 ASSERT_EQ(1u, registrations.size()); | 537 ASSERT_EQ(1u, registrations.size()); |
| 449 } | 538 } |
| 450 | 539 |
| 451 } // namespace content | 540 } // namespace content |
| OLD | NEW |