| 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/origin_util.h" | 22 #include "content/public/common/origin_util.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 23 #include "content/public/test/test_utils.h" | 24 #include "content/public/test/test_utils.h" |
| 24 #include "content/test/test_content_browser_client.h" | 25 #include "content/test/test_content_browser_client.h" |
| 25 #include "content/test/test_content_client.h" | 26 #include "content/test/test_content_client.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 | 30 |
| 30 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; | 31 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; |
| 31 | 32 |
| 32 class ServiceWorkerTestContentClient : public TestContentClient { | 33 class ServiceWorkerTestContentClient : public TestContentClient { |
| 33 public: | 34 public: |
| 34 void AddAdditionalSchemes(Schemes* schemes) override { | 35 void AddAdditionalSchemes(Schemes* schemes) override { |
| 35 schemes->service_worker_schemes.push_back(kServiceWorkerScheme); | 36 schemes->service_worker_schemes.push_back(kServiceWorkerScheme); |
| 36 } | 37 } |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 class ServiceWorkerProviderHostTest : public testing::Test { | 40 class ServiceWorkerProviderHostTest : public testing::Test { |
| 40 protected: | 41 protected: |
| 41 ServiceWorkerProviderHostTest() | 42 ServiceWorkerProviderHostTest() |
| 42 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 43 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 43 next_provider_id_(1) { | 44 next_renderer_provided_id_(1), |
| 45 next_browser_provided_id_(-2) { |
| 44 SetContentClient(&test_content_client_); | 46 SetContentClient(&test_content_client_); |
| 45 } | 47 } |
| 46 ~ServiceWorkerProviderHostTest() override {} | 48 ~ServiceWorkerProviderHostTest() override {} |
| 47 | 49 |
| 48 void SetUp() override { | 50 void SetUp() override { |
| 49 old_content_browser_client_ = | 51 old_content_browser_client_ = |
| 50 SetBrowserClientForTesting(&test_content_browser_client_); | 52 SetBrowserClientForTesting(&test_content_browser_client_); |
| 51 ResetSchemesAndOriginsWhitelist(); | 53 ResetSchemesAndOriginsWhitelist(); |
| 52 | 54 |
| 53 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 55 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 68 SetBrowserClientForTesting(old_content_browser_client_); | 70 SetBrowserClientForTesting(old_content_browser_client_); |
| 69 // Reset cached security schemes so we don't affect other tests. | 71 // Reset cached security schemes so we don't affect other tests. |
| 70 ResetSchemesAndOriginsWhitelist(); | 72 ResetSchemesAndOriginsWhitelist(); |
| 71 } | 73 } |
| 72 | 74 |
| 73 bool PatternHasProcessToRun(const GURL& pattern) const { | 75 bool PatternHasProcessToRun(const GURL& pattern) const { |
| 74 return context_->process_manager()->PatternHasProcessToRun(pattern); | 76 return context_->process_manager()->PatternHasProcessToRun(pattern); |
| 75 } | 77 } |
| 76 | 78 |
| 77 ServiceWorkerProviderHost* CreateProviderHost(const GURL& document_url) { | 79 ServiceWorkerProviderHost* CreateProviderHost(const GURL& document_url) { |
| 78 std::unique_ptr<ServiceWorkerProviderHost> host = | 80 remote_endpoints_.emplace_back(); |
| 79 CreateProviderHostForWindow( | 81 std::unique_ptr<ServiceWorkerProviderHost> host; |
| 80 helper_->mock_render_process_id(), next_provider_id_++, | 82 if (IsBrowserSideNavigationEnabled()) { |
| 81 true /* is_parent_frame_secure */, helper_->context()->AsWeakPtr()); | 83 host = ServiceWorkerProviderHost::PreCreateNavigationHost( |
| 84 helper_->context()->AsWeakPtr(), true, |
| 85 base::Callback<WebContents*(void)>()); |
| 86 ServiceWorkerProviderHostInfo info(next_browser_provided_id_--, |
| 87 1 /* route_id */, |
| 88 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
| 89 true /* is_parent_frame_secure */); |
| 90 remote_endpoints_.back().BindWithProviderHostInfo(&info); |
| 91 host->CompleteNavigationInitialized(helper_->mock_render_process_id(), |
| 92 std::move(info), nullptr); |
| 93 } else { |
| 94 host = CreateProviderHostForWindow( |
| 95 helper_->mock_render_process_id(), next_renderer_provided_id_++, |
| 96 true /* is_parent_frame_secure */, helper_->context()->AsWeakPtr(), |
| 97 &remote_endpoints_.back()); |
| 98 } |
| 99 |
| 82 ServiceWorkerProviderHost* host_raw = host.get(); | 100 ServiceWorkerProviderHost* host_raw = host.get(); |
| 83 host->SetDocumentUrl(document_url); | 101 host->SetDocumentUrl(document_url); |
| 84 context_->AddProviderHost(std::move(host)); | 102 context_->AddProviderHost(std::move(host)); |
| 85 return host_raw; | 103 return host_raw; |
| 86 } | 104 } |
| 87 | 105 |
| 88 ServiceWorkerProviderHost* CreateProviderHostWithInsecureParentFrame( | 106 ServiceWorkerProviderHost* CreateProviderHostWithInsecureParentFrame( |
| 89 const GURL& document_url) { | 107 const GURL& document_url) { |
| 108 remote_endpoints_.emplace_back(); |
| 90 std::unique_ptr<ServiceWorkerProviderHost> host = | 109 std::unique_ptr<ServiceWorkerProviderHost> host = |
| 91 CreateProviderHostForWindow(helper_->mock_render_process_id(), | 110 CreateProviderHostForWindow( |
| 92 next_provider_id_++, | 111 helper_->mock_render_process_id(), next_renderer_provided_id_++, |
| 93 false /* is_parent_frame_secure */, | 112 false /* is_parent_frame_secure */, helper_->context()->AsWeakPtr(), |
| 94 helper_->context()->AsWeakPtr()); | 113 &remote_endpoints_.back()); |
| 95 ServiceWorkerProviderHost* host_raw = host.get(); | 114 ServiceWorkerProviderHost* host_raw = host.get(); |
| 96 host->SetDocumentUrl(document_url); | 115 host->SetDocumentUrl(document_url); |
| 97 context_->AddProviderHost(std::move(host)); | 116 context_->AddProviderHost(std::move(host)); |
| 98 return host_raw; | 117 return host_raw; |
| 99 } | 118 } |
| 100 | 119 |
| 101 TestBrowserThreadBundle thread_bundle_; | 120 TestBrowserThreadBundle thread_bundle_; |
| 102 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 121 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 103 ServiceWorkerContextCore* context_; | 122 ServiceWorkerContextCore* context_; |
| 104 scoped_refptr<ServiceWorkerRegistration> registration1_; | 123 scoped_refptr<ServiceWorkerRegistration> registration1_; |
| 105 scoped_refptr<ServiceWorkerRegistration> registration2_; | 124 scoped_refptr<ServiceWorkerRegistration> registration2_; |
| 106 scoped_refptr<ServiceWorkerRegistration> registration3_; | 125 scoped_refptr<ServiceWorkerRegistration> registration3_; |
| 107 GURL script_url_; | 126 GURL script_url_; |
| 108 ServiceWorkerTestContentClient test_content_client_; | 127 ServiceWorkerTestContentClient test_content_client_; |
| 109 TestContentBrowserClient test_content_browser_client_; | 128 TestContentBrowserClient test_content_browser_client_; |
| 110 ContentBrowserClient* old_content_browser_client_; | 129 ContentBrowserClient* old_content_browser_client_; |
| 111 int next_provider_id_; | 130 int next_renderer_provided_id_; |
| 131 int next_browser_provided_id_; |
| 132 std::vector<ServiceWorkerRemoteProviderEndpoint> remote_endpoints_; |
| 112 | 133 |
| 113 private: | 134 private: |
| 114 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 135 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| 115 }; | 136 }; |
| 116 | 137 |
| 117 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { | 138 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { |
| 118 ServiceWorkerProviderHost* provider_host1 = | 139 ServiceWorkerProviderHost* provider_host1 = |
| 119 CreateProviderHost(GURL("https://www.example.com/example1.html")); | 140 CreateProviderHost(GURL("https://www.example.com/example1.html")); |
| 120 ServiceWorkerProviderHost* provider_host2 = | 141 ServiceWorkerProviderHost* provider_host2 = |
| 121 CreateProviderHost(GURL("https://www.example.com/example2.html")); | 142 CreateProviderHost(GURL("https://www.example.com/example2.html")); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); | 238 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); |
| 218 provider_host_secure_parent->SetDocumentUrl(url); | 239 provider_host_secure_parent->SetDocumentUrl(url); |
| 219 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker()); | 240 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker()); |
| 220 | 241 |
| 221 // Exceptional service worker scheme with insecure parent frame. | 242 // Exceptional service worker scheme with insecure parent frame. |
| 222 provider_host_insecure_parent->SetDocumentUrl(url); | 243 provider_host_insecure_parent->SetDocumentUrl(url); |
| 223 EXPECT_FALSE( | 244 EXPECT_FALSE( |
| 224 provider_host_insecure_parent->IsContextSecureForServiceWorker()); | 245 provider_host_insecure_parent->IsContextSecureForServiceWorker()); |
| 225 } | 246 } |
| 226 | 247 |
| 248 TEST_F(ServiceWorkerProviderHostTest, RemoveProvider) { |
| 249 // Create a provider host connected with the renderer process. |
| 250 ServiceWorkerProviderHost* provider_host = |
| 251 CreateProviderHost(GURL("https://www.example.com/example1.html")); |
| 252 int process_id = provider_host->process_id(); |
| 253 int provider_id = provider_host->provider_id(); |
| 254 EXPECT_TRUE(context_->GetProviderHost(process_id, provider_id)); |
| 255 |
| 256 // Disconnect the mojo pipe from the renderer side. |
| 257 ASSERT_TRUE(remote_endpoints_.back().host_ptr()->is_bound()); |
| 258 remote_endpoints_.back().host_ptr()->reset(); |
| 259 base::RunLoop().RunUntilIdle(); |
| 260 EXPECT_FALSE(context_->GetProviderHost(process_id, provider_id)); |
| 261 } |
| 262 |
| 227 } // namespace content | 263 } // namespace content |
| OLD | NEW |