Chromium Code Reviews| 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"; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 SetBrowserClientForTesting(old_content_browser_client_); | 69 SetBrowserClientForTesting(old_content_browser_client_); |
| 69 // Reset cached security schemes so we don't affect other tests. | 70 // Reset cached security schemes so we don't affect other tests. |
| 70 ResetSchemesAndOriginsWhitelist(); | 71 ResetSchemesAndOriginsWhitelist(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 bool PatternHasProcessToRun(const GURL& pattern) const { | 74 bool PatternHasProcessToRun(const GURL& pattern) const { |
| 74 return context_->process_manager()->PatternHasProcessToRun(pattern); | 75 return context_->process_manager()->PatternHasProcessToRun(pattern); |
| 75 } | 76 } |
| 76 | 77 |
| 77 ServiceWorkerProviderHost* CreateProviderHost(const GURL& document_url) { | 78 ServiceWorkerProviderHost* CreateProviderHost(const GURL& document_url) { |
| 78 std::unique_ptr<ServiceWorkerProviderHost> host = | 79 remote_endpoints_.emplace_back(); |
| 79 CreateProviderHostForWindow( | 80 std::unique_ptr<ServiceWorkerProviderHost> host; |
| 80 helper_->mock_render_process_id(), next_provider_id_++, | 81 if (IsBrowserSideNavigationEnabled()) { |
| 81 true /* is_parent_frame_secure */, helper_->context()->AsWeakPtr()); | 82 static int next_browser_provided_id = -2; |
|
falken
2017/05/22 08:28:12
could this be a class member like |next_provider_i
shimazu
2017/05/23 06:29:34
Done.
| |
| 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 mojom::ServiceWorkerProviderAssociatedPtr client_ptr; | |
| 91 remote_endpoints_.back().client_request = | |
| 92 mojo::MakeIsolatedRequest(&client_ptr); | |
| 93 info.client_ptr_info = client_ptr.PassInterface(); | |
| 94 info.host_request = | |
| 95 mojo::MakeIsolatedRequest(&remote_endpoints_.back().host_ptr); | |
| 96 host->CompleteNavigationInitialized(helper_->mock_render_process_id(), | |
| 97 std::move(info), nullptr); | |
| 98 } else { | |
| 99 host = CreateProviderHostForWindow( | |
| 100 helper_->mock_render_process_id(), next_provider_id_++, | |
| 101 true /* is_parent_frame_secure */, helper_->context()->AsWeakPtr(), | |
| 102 &remote_endpoints_.back()); | |
| 103 } | |
| 104 | |
| 82 ServiceWorkerProviderHost* host_raw = host.get(); | 105 ServiceWorkerProviderHost* host_raw = host.get(); |
| 83 host->SetDocumentUrl(document_url); | 106 host->SetDocumentUrl(document_url); |
| 84 context_->AddProviderHost(std::move(host)); | 107 context_->AddProviderHost(std::move(host)); |
| 85 return host_raw; | 108 return host_raw; |
| 86 } | 109 } |
| 87 | 110 |
| 88 ServiceWorkerProviderHost* CreateProviderHostWithInsecureParentFrame( | 111 ServiceWorkerProviderHost* CreateProviderHostWithInsecureParentFrame( |
| 89 const GURL& document_url) { | 112 const GURL& document_url) { |
| 113 remote_endpoints_.emplace_back(); | |
| 90 std::unique_ptr<ServiceWorkerProviderHost> host = | 114 std::unique_ptr<ServiceWorkerProviderHost> host = |
| 91 CreateProviderHostForWindow(helper_->mock_render_process_id(), | 115 CreateProviderHostForWindow( |
| 92 next_provider_id_++, | 116 helper_->mock_render_process_id(), next_provider_id_++, |
| 93 false /* is_parent_frame_secure */, | 117 false /* is_parent_frame_secure */, helper_->context()->AsWeakPtr(), |
| 94 helper_->context()->AsWeakPtr()); | 118 &remote_endpoints_.back()); |
| 95 ServiceWorkerProviderHost* host_raw = host.get(); | 119 ServiceWorkerProviderHost* host_raw = host.get(); |
| 96 host->SetDocumentUrl(document_url); | 120 host->SetDocumentUrl(document_url); |
| 97 context_->AddProviderHost(std::move(host)); | 121 context_->AddProviderHost(std::move(host)); |
| 98 return host_raw; | 122 return host_raw; |
| 99 } | 123 } |
| 100 | 124 |
| 101 TestBrowserThreadBundle thread_bundle_; | 125 TestBrowserThreadBundle thread_bundle_; |
| 102 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 126 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 103 ServiceWorkerContextCore* context_; | 127 ServiceWorkerContextCore* context_; |
| 104 scoped_refptr<ServiceWorkerRegistration> registration1_; | 128 scoped_refptr<ServiceWorkerRegistration> registration1_; |
| 105 scoped_refptr<ServiceWorkerRegistration> registration2_; | 129 scoped_refptr<ServiceWorkerRegistration> registration2_; |
| 106 scoped_refptr<ServiceWorkerRegistration> registration3_; | 130 scoped_refptr<ServiceWorkerRegistration> registration3_; |
| 107 GURL script_url_; | 131 GURL script_url_; |
| 108 ServiceWorkerTestContentClient test_content_client_; | 132 ServiceWorkerTestContentClient test_content_client_; |
| 109 TestContentBrowserClient test_content_browser_client_; | 133 TestContentBrowserClient test_content_browser_client_; |
| 110 ContentBrowserClient* old_content_browser_client_; | 134 ContentBrowserClient* old_content_browser_client_; |
| 111 int next_provider_id_; | 135 int next_provider_id_; |
| 136 std::vector<ServiceWorkerRemoteProviderEndpoint> remote_endpoints_; | |
| 112 | 137 |
| 113 private: | 138 private: |
| 114 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 139 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| 115 }; | 140 }; |
| 116 | 141 |
| 117 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { | 142 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { |
| 118 ServiceWorkerProviderHost* provider_host1 = | 143 ServiceWorkerProviderHost* provider_host1 = |
| 119 CreateProviderHost(GURL("https://www.example.com/example1.html")); | 144 CreateProviderHost(GURL("https://www.example.com/example1.html")); |
| 120 ServiceWorkerProviderHost* provider_host2 = | 145 ServiceWorkerProviderHost* provider_host2 = |
| 121 CreateProviderHost(GURL("https://www.example.com/example2.html")); | 146 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)); | 242 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); |
| 218 provider_host_secure_parent->SetDocumentUrl(url); | 243 provider_host_secure_parent->SetDocumentUrl(url); |
| 219 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker()); | 244 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker()); |
| 220 | 245 |
| 221 // Exceptional service worker scheme with insecure parent frame. | 246 // Exceptional service worker scheme with insecure parent frame. |
| 222 provider_host_insecure_parent->SetDocumentUrl(url); | 247 provider_host_insecure_parent->SetDocumentUrl(url); |
| 223 EXPECT_FALSE( | 248 EXPECT_FALSE( |
| 224 provider_host_insecure_parent->IsContextSecureForServiceWorker()); | 249 provider_host_insecure_parent->IsContextSecureForServiceWorker()); |
| 225 } | 250 } |
| 226 | 251 |
| 252 TEST_F(ServiceWorkerProviderHostTest, RemoveProvider) { | |
| 253 // Create a provider host connected with the renderer process. | |
| 254 ServiceWorkerProviderHost* provider_host = | |
| 255 CreateProviderHost(GURL("https://www.example.com/example1.html")); | |
| 256 int process_id = provider_host->process_id(); | |
| 257 int provider_id = provider_host->provider_id(); | |
| 258 EXPECT_TRUE(context_->GetProviderHost(process_id, provider_id)); | |
| 259 | |
| 260 // Disconnect the mojo pipe from the renderer side. | |
| 261 ASSERT_TRUE(remote_endpoints_.back().host_ptr.is_bound()); | |
| 262 remote_endpoints_.back().host_ptr.reset(); | |
| 263 base::RunLoop().RunUntilIdle(); | |
| 264 EXPECT_FALSE(context_->GetProviderHost(process_id, provider_id)); | |
| 265 } | |
| 266 | |
| 227 } // namespace content | 267 } // namespace content |
| OLD | NEW |