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" |
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_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
13 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
14 #include "content/browser/service_worker/service_worker_request_handler.h" | 14 #include "content/browser/service_worker/service_worker_request_handler.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/common/service_worker/service_worker_utils.h" |
16 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
17 #include "content/public/test/mock_resource_context.h" | 18 #include "content/public/test/mock_resource_context.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
20 #include "net/url_request/url_request_test_job.h" | 21 #include "net/url_request/url_request_test_job.h" |
21 #include "net/url_request/url_request_test_util.h" | 22 #include "net/url_request/url_request_test_util.h" |
22 #include "storage/browser/blob/blob_storage_context.h" | 23 #include "storage/browser/blob/blob_storage_context.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 namespace content { | 26 namespace content { |
(...skipping 16 matching lines...) Expand all Loading... |
42 ServiceWorkerContextWrapper::GetRegistrationsInfosCallback | 43 ServiceWorkerContextWrapper::GetRegistrationsInfosCallback |
43 SaveFoundRegistrations( | 44 SaveFoundRegistrations( |
44 ServiceWorkerStatusCode expected_status, | 45 ServiceWorkerStatusCode expected_status, |
45 bool* called, | 46 bool* called, |
46 std::vector<ServiceWorkerRegistrationInfo>* registrations) { | 47 std::vector<ServiceWorkerRegistrationInfo>* registrations) { |
47 *called = false; | 48 *called = false; |
48 return base::Bind(&SaveFoundRegistrationsCallback, expected_status, called, | 49 return base::Bind(&SaveFoundRegistrationsCallback, expected_status, called, |
49 registrations); | 50 registrations); |
50 } | 51 } |
51 | 52 |
| 53 } // namespace |
| 54 |
52 class LinkHeaderServiceWorkerTest : public ::testing::Test { | 55 class LinkHeaderServiceWorkerTest : public ::testing::Test { |
53 public: | 56 public: |
54 LinkHeaderServiceWorkerTest() | 57 LinkHeaderServiceWorkerTest() |
55 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 58 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
56 resource_context_(&request_context_) { | 59 resource_context_(&request_context_) { |
57 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 60 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
58 switches::kEnableExperimentalWebPlatformFeatures); | 61 switches::kEnableExperimentalWebPlatformFeatures); |
59 } | 62 } |
60 ~LinkHeaderServiceWorkerTest() override {} | 63 ~LinkHeaderServiceWorkerTest() override {} |
61 | 64 |
62 void SetUp() override { | 65 void SetUp() override { |
63 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 66 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
64 | 67 |
65 // An empty host. | 68 // An empty host. |
66 std::unique_ptr<ServiceWorkerProviderHost> host( | 69 std::unique_ptr<ServiceWorkerProviderHost> host( |
67 new ServiceWorkerProviderHost( | 70 new ServiceWorkerProviderHost( |
68 helper_->mock_render_process_id(), MSG_ROUTING_NONE, | 71 render_process_id(), MSG_ROUTING_NONE, kMockProviderId, |
69 kMockProviderId, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 72 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
70 ServiceWorkerProviderHost::FrameSecurityLevel::UNINITIALIZED, | 73 ServiceWorkerProviderHost::FrameSecurityLevel::UNINITIALIZED, |
71 context()->AsWeakPtr(), nullptr)); | 74 context()->AsWeakPtr(), nullptr)); |
72 provider_host_ = host->AsWeakPtr(); | 75 provider_host_ = host->AsWeakPtr(); |
73 context()->AddProviderHost(std::move(host)); | 76 context()->AddProviderHost(std::move(host)); |
74 } | 77 } |
75 | 78 |
76 void TearDown() override { helper_.reset(); } | 79 void TearDown() override { helper_.reset(); } |
77 | 80 |
78 ServiceWorkerContextCore* context() { return helper_->context(); } | 81 ServiceWorkerContextCore* context() { return helper_->context(); } |
79 ServiceWorkerContextWrapper* context_wrapper() { | 82 ServiceWorkerContextWrapper* context_wrapper() { |
80 return helper_->context_wrapper(); | 83 return helper_->context_wrapper(); |
81 } | 84 } |
82 ServiceWorkerProviderHost* provider_host() { return provider_host_.get(); } | 85 ServiceWorkerProviderHost* provider_host() { return provider_host_.get(); } |
| 86 int render_process_id() const { return helper_->mock_render_process_id(); } |
| 87 |
| 88 void CreateServiceWorkerProviderHost() { |
| 89 std::unique_ptr<ServiceWorkerProviderHost> host( |
| 90 new ServiceWorkerProviderHost( |
| 91 render_process_id(), MSG_ROUTING_NONE, kMockProviderId, |
| 92 SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, |
| 93 ServiceWorkerProviderHost::FrameSecurityLevel::UNINITIALIZED, |
| 94 context()->AsWeakPtr(), nullptr)); |
| 95 provider_host_ = host->AsWeakPtr(); |
| 96 context()->RemoveProviderHost(host->process_id(), host->provider_id()); |
| 97 context()->AddProviderHost(std::move(host)); |
| 98 |
| 99 scoped_refptr<ServiceWorkerRegistration> registration = |
| 100 new ServiceWorkerRegistration(GURL("https://host/scope"), 1L, |
| 101 context()->AsWeakPtr()); |
| 102 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion( |
| 103 registration.get(), GURL("https://host/script.js"), 1L, |
| 104 context()->AsWeakPtr()); |
| 105 |
| 106 provider_host_->running_hosted_version_ = version; |
| 107 } |
83 | 108 |
84 std::unique_ptr<net::URLRequest> CreateRequest(const GURL& request_url, | 109 std::unique_ptr<net::URLRequest> CreateRequest(const GURL& request_url, |
85 ResourceType resource_type) { | 110 ResourceType resource_type) { |
86 std::unique_ptr<net::URLRequest> request = request_context_.CreateRequest( | 111 std::unique_ptr<net::URLRequest> request = request_context_.CreateRequest( |
87 request_url, net::DEFAULT_PRIORITY, &request_delegate_); | 112 request_url, net::DEFAULT_PRIORITY, &request_delegate_); |
88 ResourceRequestInfo::AllocateForTesting( | 113 ResourceRequestInfo::AllocateForTesting( |
89 request.get(), resource_type, &resource_context_, | 114 request.get(), resource_type, &resource_context_, |
90 -1 /* render_process_id */, -1 /* render_view_id */, | 115 -1 /* render_process_id */, -1 /* render_view_id */, |
91 -1 /* render_frame_id */, resource_type == RESOURCE_TYPE_MAIN_FRAME, | 116 -1 /* render_frame_id */, resource_type == RESOURCE_TYPE_MAIN_FRAME, |
92 false /* parent_is_main_frame */, true /* allow_download */, | 117 false /* parent_is_main_frame */, true /* allow_download */, |
93 true /* is_async */, false /* is_using_lofi */); | 118 true /* is_async */, false /* is_using_lofi */); |
94 ResourceRequestInfoImpl::ForRequest(request.get()) | 119 ResourceRequestInfoImpl::ForRequest(request.get()) |
95 ->set_initiated_in_secure_context_for_testing(true); | 120 ->set_initiated_in_secure_context_for_testing(true); |
96 | 121 |
97 ServiceWorkerRequestHandler::InitializeHandler( | 122 ServiceWorkerRequestHandler::InitializeHandler( |
98 request.get(), context_wrapper(), &blob_storage_context_, | 123 request.get(), context_wrapper(), &blob_storage_context_, |
99 helper_->mock_render_process_id(), kMockProviderId, | 124 render_process_id(), kMockProviderId, false /* skip_service_worker */, |
100 false /* skip_service_worker */, FETCH_REQUEST_MODE_NO_CORS, | 125 FETCH_REQUEST_MODE_NO_CORS, FETCH_CREDENTIALS_MODE_OMIT, |
101 FETCH_CREDENTIALS_MODE_OMIT, FetchRedirectMode::FOLLOW_MODE, | 126 FetchRedirectMode::FOLLOW_MODE, resource_type, |
102 resource_type, REQUEST_CONTEXT_TYPE_HYPERLINK, | 127 REQUEST_CONTEXT_TYPE_HYPERLINK, REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, |
103 REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, nullptr); | 128 nullptr); |
104 | 129 |
105 return request; | 130 return request; |
106 } | 131 } |
107 | 132 |
108 std::unique_ptr<net::URLRequest> CreateSubresourceRequest( | 133 std::unique_ptr<net::URLRequest> CreateSubresourceRequest( |
109 const GURL& request_url) { | 134 const GURL& request_url) { |
110 return CreateRequest(request_url, RESOURCE_TYPE_SCRIPT); | 135 return CreateRequest(request_url, RESOURCE_TYPE_SCRIPT); |
111 } | 136 } |
112 | 137 |
113 std::vector<ServiceWorkerRegistrationInfo> GetRegistrations() { | 138 std::vector<ServiceWorkerRegistrationInfo> GetRegistrations() { |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 CreateRequest(GURL("https://example.com/foo/bar/"), | 352 CreateRequest(GURL("https://example.com/foo/bar/"), |
328 RESOURCE_TYPE_MAIN_FRAME) | 353 RESOURCE_TYPE_MAIN_FRAME) |
329 .get(), | 354 .get(), |
330 "<../foo.js>; rel=serviceworker", context_wrapper()); | 355 "<../foo.js>; rel=serviceworker", context_wrapper()); |
331 base::RunLoop().RunUntilIdle(); | 356 base::RunLoop().RunUntilIdle(); |
332 | 357 |
333 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); | 358 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
334 ASSERT_EQ(0u, registrations.size()); | 359 ASSERT_EQ(0u, registrations.size()); |
335 } | 360 } |
336 | 361 |
| 362 TEST_P(LinkHeaderServiceWorkerTestP, |
| 363 InstallServiceWorker_FromWorkerWithoutControllees) { |
| 364 CreateServiceWorkerProviderHost(); |
| 365 ProcessLinkHeaderForRequest( |
| 366 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(), |
| 367 "<../foo.js>; rel=serviceworker", context_wrapper()); |
| 368 base::RunLoop().RunUntilIdle(); |
| 369 |
| 370 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 371 ASSERT_EQ(0u, registrations.size()); |
| 372 } |
| 373 |
| 374 TEST_P(LinkHeaderServiceWorkerTestP, |
| 375 InstallServiceWorker_FromWorkerWithControllees) { |
| 376 CreateServiceWorkerProviderHost(); |
| 377 |
| 378 std::unique_ptr<ServiceWorkerProviderHost> controllee( |
| 379 new ServiceWorkerProviderHost( |
| 380 render_process_id(), MSG_ROUTING_NONE, kMockProviderId, |
| 381 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 382 ServiceWorkerProviderHost::FrameSecurityLevel::UNINITIALIZED, |
| 383 context()->AsWeakPtr(), nullptr)); |
| 384 provider_host()->running_hosted_version()->AddControllee(controllee.get()); |
| 385 |
| 386 ProcessLinkHeaderForRequest( |
| 387 CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(), |
| 388 "<../foo.js>; rel=serviceworker", context_wrapper()); |
| 389 base::RunLoop().RunUntilIdle(); |
| 390 |
| 391 std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
| 392 ASSERT_EQ(1u, registrations.size()); |
| 393 } |
| 394 |
337 INSTANTIATE_TEST_CASE_P(LinkHeaderServiceWorkerTest, | 395 INSTANTIATE_TEST_CASE_P(LinkHeaderServiceWorkerTest, |
338 LinkHeaderServiceWorkerTestP, | 396 LinkHeaderServiceWorkerTestP, |
339 testing::Bool()); | 397 testing::Bool()); |
340 | 398 |
341 } // namespace | |
342 | |
343 } // namespace content | 399 } // namespace content |
OLD | NEW |