| 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> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 14 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 15 #include "content/browser/service_worker/service_worker_context_core.h" | 15 #include "content/browser/service_worker/service_worker_context_core.h" |
| 16 #include "content/browser/service_worker/service_worker_register_job.h" | 16 #include "content/browser/service_worker/service_worker_register_job.h" |
| 17 #include "content/browser/service_worker/service_worker_registration.h" | 17 #include "content/browser/service_worker/service_worker_registration.h" |
| 18 #include "content/browser/service_worker/service_worker_test_utils.h" | 18 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 19 #include "content/browser/service_worker/service_worker_version.h" | 19 #include "content/browser/service_worker/service_worker_version.h" |
| 20 #include "content/common/url_schemes.h" | 20 #include "content/common/url_schemes.h" |
| 21 #include "content/public/common/browser_side_navigation_policy.h" |
| 21 #include "content/public/common/child_process_host.h" | 22 #include "content/public/common/child_process_host.h" |
| 22 #include "content/public/common/origin_util.h" | 23 #include "content/public/common/origin_util.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "content/public/test/test_utils.h" | 25 #include "content/public/test/test_utils.h" |
| 25 #include "content/test/test_content_browser_client.h" | 26 #include "content/test/test_content_browser_client.h" |
| 26 #include "content/test/test_content_client.h" | 27 #include "content/test/test_content_client.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 29 |
| 29 namespace content { | 30 namespace content { |
| 30 | 31 |
| 31 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; | 32 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; |
| 32 | 33 |
| 33 class ServiceWorkerTestContentClient : public TestContentClient { | 34 class ServiceWorkerTestContentClient : public TestContentClient { |
| 34 public: | 35 public: |
| 35 void AddAdditionalSchemes(Schemes* schemes) override { | 36 void AddAdditionalSchemes(Schemes* schemes) override { |
| 36 schemes->service_worker_schemes.push_back(kServiceWorkerScheme); | 37 schemes->service_worker_schemes.push_back(kServiceWorkerScheme); |
| 37 } | 38 } |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 class ServiceWorkerProviderHostTest : public testing::Test { | 41 class ServiceWorkerProviderHostTest : public testing::Test { |
| 41 protected: | 42 protected: |
| 42 ServiceWorkerProviderHostTest() | 43 ServiceWorkerProviderHostTest() |
| 43 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 44 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 44 next_provider_id_(1) { | 45 next_renderer_provided_id_(1), |
| 46 next_browser_provided_id_(-2) { |
| 45 SetContentClient(&test_content_client_); | 47 SetContentClient(&test_content_client_); |
| 46 } | 48 } |
| 47 ~ServiceWorkerProviderHostTest() override {} | 49 ~ServiceWorkerProviderHostTest() override {} |
| 48 | 50 |
| 49 void SetUp() override { | 51 void SetUp() override { |
| 50 old_content_browser_client_ = | 52 old_content_browser_client_ = |
| 51 SetBrowserClientForTesting(&test_content_browser_client_); | 53 SetBrowserClientForTesting(&test_content_browser_client_); |
| 52 ResetSchemesAndOriginsWhitelist(); | 54 ResetSchemesAndOriginsWhitelist(); |
| 53 | 55 |
| 54 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 56 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 69 SetBrowserClientForTesting(old_content_browser_client_); | 71 SetBrowserClientForTesting(old_content_browser_client_); |
| 70 // Reset cached security schemes so we don't affect other tests. | 72 // Reset cached security schemes so we don't affect other tests. |
| 71 ResetSchemesAndOriginsWhitelist(); | 73 ResetSchemesAndOriginsWhitelist(); |
| 72 } | 74 } |
| 73 | 75 |
| 74 bool PatternHasProcessToRun(const GURL& pattern) const { | 76 bool PatternHasProcessToRun(const GURL& pattern) const { |
| 75 return context_->process_manager()->PatternHasProcessToRun(pattern); | 77 return context_->process_manager()->PatternHasProcessToRun(pattern); |
| 76 } | 78 } |
| 77 | 79 |
| 78 ServiceWorkerProviderHost* CreateProviderHost(const GURL& document_url) { | 80 ServiceWorkerProviderHost* CreateProviderHost(const GURL& document_url) { |
| 79 std::unique_ptr<ServiceWorkerProviderHost> host = | 81 remote_endpoints_.emplace_back(); |
| 80 CreateProviderHostForWindow( | 82 std::unique_ptr<ServiceWorkerProviderHost> host; |
| 81 helper_->mock_render_process_id(), next_provider_id_++, | 83 if (IsBrowserSideNavigationEnabled()) { |
| 82 true /* is_parent_frame_secure */, helper_->context()->AsWeakPtr()); | 84 host = ServiceWorkerProviderHost::PreCreateNavigationHost( |
| 85 helper_->context()->AsWeakPtr(), true, |
| 86 base::Callback<WebContents*(void)>()); |
| 87 ServiceWorkerProviderHostInfo info(next_browser_provided_id_--, |
| 88 1 /* route_id */, |
| 89 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 90 true /* is_parent_frame_secure */); |
| 91 remote_endpoints_.back().BindWithProviderHostInfo(&info); |
| 92 host->CompleteNavigationInitialized(helper_->mock_render_process_id(), |
| 93 std::move(info), nullptr); |
| 94 } else { |
| 95 host = CreateProviderHostForWindow( |
| 96 helper_->mock_render_process_id(), next_renderer_provided_id_++, |
| 97 true /* is_parent_frame_secure */, helper_->context()->AsWeakPtr(), |
| 98 &remote_endpoints_.back()); |
| 99 } |
| 100 |
| 83 ServiceWorkerProviderHost* host_raw = host.get(); | 101 ServiceWorkerProviderHost* host_raw = host.get(); |
| 84 host->SetDocumentUrl(document_url); | 102 host->SetDocumentUrl(document_url); |
| 85 context_->AddProviderHost(std::move(host)); | 103 context_->AddProviderHost(std::move(host)); |
| 86 return host_raw; | 104 return host_raw; |
| 87 } | 105 } |
| 88 | 106 |
| 89 ServiceWorkerProviderHost* CreateProviderHostWithInsecureParentFrame( | 107 ServiceWorkerProviderHost* CreateProviderHostWithInsecureParentFrame( |
| 90 const GURL& document_url) { | 108 const GURL& document_url) { |
| 109 remote_endpoints_.emplace_back(); |
| 91 std::unique_ptr<ServiceWorkerProviderHost> host = | 110 std::unique_ptr<ServiceWorkerProviderHost> host = |
| 92 CreateProviderHostForWindow(helper_->mock_render_process_id(), | 111 CreateProviderHostForWindow( |
| 93 next_provider_id_++, | 112 helper_->mock_render_process_id(), next_renderer_provided_id_++, |
| 94 false /* is_parent_frame_secure */, | 113 false /* is_parent_frame_secure */, helper_->context()->AsWeakPtr(), |
| 95 helper_->context()->AsWeakPtr()); | 114 &remote_endpoints_.back()); |
| 96 ServiceWorkerProviderHost* host_raw = host.get(); | 115 ServiceWorkerProviderHost* host_raw = host.get(); |
| 97 host->SetDocumentUrl(document_url); | 116 host->SetDocumentUrl(document_url); |
| 98 context_->AddProviderHost(std::move(host)); | 117 context_->AddProviderHost(std::move(host)); |
| 99 return host_raw; | 118 return host_raw; |
| 100 } | 119 } |
| 101 | 120 |
| 102 TestBrowserThreadBundle thread_bundle_; | 121 TestBrowserThreadBundle thread_bundle_; |
| 103 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 122 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 104 ServiceWorkerContextCore* context_; | 123 ServiceWorkerContextCore* context_; |
| 105 scoped_refptr<ServiceWorkerRegistration> registration1_; | 124 scoped_refptr<ServiceWorkerRegistration> registration1_; |
| 106 scoped_refptr<ServiceWorkerRegistration> registration2_; | 125 scoped_refptr<ServiceWorkerRegistration> registration2_; |
| 107 scoped_refptr<ServiceWorkerRegistration> registration3_; | 126 scoped_refptr<ServiceWorkerRegistration> registration3_; |
| 108 GURL script_url_; | 127 GURL script_url_; |
| 109 ServiceWorkerTestContentClient test_content_client_; | 128 ServiceWorkerTestContentClient test_content_client_; |
| 110 TestContentBrowserClient test_content_browser_client_; | 129 TestContentBrowserClient test_content_browser_client_; |
| 111 ContentBrowserClient* old_content_browser_client_; | 130 ContentBrowserClient* old_content_browser_client_; |
| 112 int next_provider_id_; | 131 int next_renderer_provided_id_; |
| 132 int next_browser_provided_id_; |
| 133 std::vector<ServiceWorkerRemoteProviderEndpoint> remote_endpoints_; |
| 113 | 134 |
| 114 private: | 135 private: |
| 115 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 136 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| 116 }; | 137 }; |
| 117 | 138 |
| 118 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { | 139 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { |
| 119 ServiceWorkerProviderHost* provider_host1 = | 140 ServiceWorkerProviderHost* provider_host1 = |
| 120 CreateProviderHost(GURL("https://www.example.com/example1.html")); | 141 CreateProviderHost(GURL("https://www.example.com/example1.html")); |
| 121 ServiceWorkerProviderHost* provider_host2 = | 142 ServiceWorkerProviderHost* provider_host2 = |
| 122 CreateProviderHost(GURL("https://www.example.com/example2.html")); | 143 CreateProviderHost(GURL("https://www.example.com/example2.html")); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 provider_host_secure_parent->SetDocumentUrl(url); | 240 provider_host_secure_parent->SetDocumentUrl(url); |
| 220 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker()); | 241 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker()); |
| 221 | 242 |
| 222 // Exceptional service worker scheme with insecure parent frame. | 243 // Exceptional service worker scheme with insecure parent frame. |
| 223 provider_host_insecure_parent->SetDocumentUrl(url); | 244 provider_host_insecure_parent->SetDocumentUrl(url); |
| 224 EXPECT_FALSE( | 245 EXPECT_FALSE( |
| 225 provider_host_insecure_parent->IsContextSecureForServiceWorker()); | 246 provider_host_insecure_parent->IsContextSecureForServiceWorker()); |
| 226 } | 247 } |
| 227 | 248 |
| 228 TEST_F(ServiceWorkerProviderHostTest, CrossSiteTransfer) { | 249 TEST_F(ServiceWorkerProviderHostTest, CrossSiteTransfer) { |
| 250 if (IsBrowserSideNavigationEnabled()) |
| 251 return; |
| 229 ServiceWorkerProviderHost* provider_host = | 252 ServiceWorkerProviderHost* provider_host = |
| 230 CreateProviderHost(GURL("https://www.example.com/example.html")); | 253 CreateProviderHost(GURL("https://www.example.com/example.html")); |
| 231 const int process_id = provider_host->process_id(); | 254 const int process_id = provider_host->process_id(); |
| 232 const int provider_id = provider_host->provider_id(); | 255 const int provider_id = provider_host->provider_id(); |
| 233 const int frame_id = provider_host->frame_id(); | 256 const int frame_id = provider_host->frame_id(); |
| 234 const ServiceWorkerProviderType type = provider_host->provider_type(); | 257 const ServiceWorkerProviderType type = provider_host->provider_type(); |
| 235 const bool is_parent_frame_secure = provider_host->is_parent_frame_secure(); | 258 const bool is_parent_frame_secure = provider_host->is_parent_frame_secure(); |
| 236 const ServiceWorkerDispatcherHost* dispatcher_host = | 259 const ServiceWorkerDispatcherHost* dispatcher_host = |
| 237 provider_host->dispatcher_host(); | 260 provider_host->dispatcher_host(); |
| 238 | 261 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 262 EXPECT_EQ(is_parent_frame_secure, provider_host->is_parent_frame_secure()); | 285 EXPECT_EQ(is_parent_frame_secure, provider_host->is_parent_frame_secure()); |
| 263 EXPECT_EQ(dispatcher_host, provider_host->dispatcher_host()); | 286 EXPECT_EQ(dispatcher_host, provider_host->dispatcher_host()); |
| 264 | 287 |
| 265 EXPECT_EQ(kInvalidServiceWorkerProviderId, provisional_host->provider_id()); | 288 EXPECT_EQ(kInvalidServiceWorkerProviderId, provisional_host->provider_id()); |
| 266 EXPECT_EQ(MSG_ROUTING_NONE, provisional_host->frame_id()); | 289 EXPECT_EQ(MSG_ROUTING_NONE, provisional_host->frame_id()); |
| 267 EXPECT_EQ(SERVICE_WORKER_PROVIDER_UNKNOWN, provisional_host->provider_type()); | 290 EXPECT_EQ(SERVICE_WORKER_PROVIDER_UNKNOWN, provisional_host->provider_type()); |
| 268 EXPECT_FALSE(provisional_host->is_parent_frame_secure()); | 291 EXPECT_FALSE(provisional_host->is_parent_frame_secure()); |
| 269 EXPECT_EQ(nullptr, provisional_host->dispatcher_host()); | 292 EXPECT_EQ(nullptr, provisional_host->dispatcher_host()); |
| 270 } | 293 } |
| 271 | 294 |
| 295 TEST_F(ServiceWorkerProviderHostTest, RemoveProvider) { |
| 296 // Create a provider host connected with the renderer process. |
| 297 ServiceWorkerProviderHost* provider_host = |
| 298 CreateProviderHost(GURL("https://www.example.com/example1.html")); |
| 299 int process_id = provider_host->process_id(); |
| 300 int provider_id = provider_host->provider_id(); |
| 301 EXPECT_TRUE(context_->GetProviderHost(process_id, provider_id)); |
| 302 |
| 303 // Disconnect the mojo pipe from the renderer side. |
| 304 ASSERT_TRUE(remote_endpoints_.back().host_ptr()->is_bound()); |
| 305 remote_endpoints_.back().host_ptr()->reset(); |
| 306 base::RunLoop().RunUntilIdle(); |
| 307 EXPECT_FALSE(context_->GetProviderHost(process_id, provider_id)); |
| 308 } |
| 309 |
| 272 } // namespace content | 310 } // namespace content |
| OLD | NEW |