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 "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.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_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
13 #include "content/browser/service_worker/service_worker_register_job.h" | 13 #include "content/browser/service_worker/service_worker_register_job.h" |
14 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
15 #include "content/browser/service_worker/service_worker_version.h" | 15 #include "content/browser/service_worker/service_worker_version.h" |
16 #include "content/public/common/origin_util.h" | |
17 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "content/test/test_content_browser_client.h" | |
19 #include "content/test/test_content_client.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
21 | 18 |
22 namespace content { | 19 namespace content { |
23 | 20 |
24 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; | |
25 | |
26 class ServiceWorkerTestContentClient : public TestContentClient { | |
27 public: | |
28 void AddServiceWorkerSchemes(std::set<std::string>* schemes) override { | |
29 schemes->insert(kServiceWorkerScheme); | |
30 } | |
31 }; | |
32 | |
33 class ServiceWorkerProviderHostTest : public testing::Test { | 21 class ServiceWorkerProviderHostTest : public testing::Test { |
34 protected: | 22 protected: |
35 ServiceWorkerProviderHostTest() | 23 ServiceWorkerProviderHostTest() |
36 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { | 24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
37 SetContentClient(&test_content_client_); | |
38 } | |
39 ~ServiceWorkerProviderHostTest() override {} | 25 ~ServiceWorkerProviderHostTest() override {} |
40 | 26 |
41 void SetUp() override { | 27 void SetUp() override { |
42 old_content_browser_client_ = | |
43 SetBrowserClientForTesting(&test_content_browser_client_); | |
44 | |
45 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 28 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
46 context_ = helper_->context(); | 29 context_ = helper_->context(); |
47 script_url_ = GURL("http://www.example.com/service_worker.js"); | 30 script_url_ = GURL("http://www.example.com/service_worker.js"); |
48 registration1_ = new ServiceWorkerRegistration( | 31 registration1_ = new ServiceWorkerRegistration( |
49 GURL("http://www.example.com/"), 1L, context_->AsWeakPtr()); | 32 GURL("http://www.example.com/"), 1L, context_->AsWeakPtr()); |
50 registration2_ = new ServiceWorkerRegistration( | 33 registration2_ = new ServiceWorkerRegistration( |
51 GURL("http://www.example.com/example"), 2L, context_->AsWeakPtr()); | 34 GURL("http://www.example.com/example"), 2L, context_->AsWeakPtr()); |
52 | 35 |
53 // Prepare provider hosts (for the same process). | 36 // Prepare provider hosts (for the same process). |
54 std::unique_ptr<ServiceWorkerProviderHost> host1( | 37 std::unique_ptr<ServiceWorkerProviderHost> host1( |
55 new ServiceWorkerProviderHost( | 38 new ServiceWorkerProviderHost(helper_->mock_render_process_id(), |
56 helper_->mock_render_process_id(), MSG_ROUTING_NONE, | 39 MSG_ROUTING_NONE, 1 /* provider_id */, |
57 1 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 40 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
58 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, | 41 context_->AsWeakPtr(), NULL)); |
59 context_->AsWeakPtr(), NULL)); | |
60 host1->SetDocumentUrl(GURL("http://www.example.com/example1.html")); | 42 host1->SetDocumentUrl(GURL("http://www.example.com/example1.html")); |
61 std::unique_ptr<ServiceWorkerProviderHost> host2( | 43 std::unique_ptr<ServiceWorkerProviderHost> host2( |
62 new ServiceWorkerProviderHost( | 44 new ServiceWorkerProviderHost(helper_->mock_render_process_id(), |
63 helper_->mock_render_process_id(), MSG_ROUTING_NONE, | 45 MSG_ROUTING_NONE, 2 /* provider_id */, |
64 2 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 46 SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
65 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, | 47 context_->AsWeakPtr(), NULL)); |
66 context_->AsWeakPtr(), NULL)); | |
67 host2->SetDocumentUrl(GURL("http://www.example.com/example2.html")); | 48 host2->SetDocumentUrl(GURL("http://www.example.com/example2.html")); |
68 provider_host1_ = host1->AsWeakPtr(); | 49 provider_host1_ = host1->AsWeakPtr(); |
69 provider_host2_ = host2->AsWeakPtr(); | 50 provider_host2_ = host2->AsWeakPtr(); |
70 context_->AddProviderHost(base::WrapUnique(host1.release())); | 51 context_->AddProviderHost(base::WrapUnique(host1.release())); |
71 context_->AddProviderHost(base::WrapUnique(host2.release())); | 52 context_->AddProviderHost(base::WrapUnique(host2.release())); |
72 } | 53 } |
73 | 54 |
74 void TearDown() override { | 55 void TearDown() override { |
75 registration1_ = 0; | 56 registration1_ = 0; |
76 registration2_ = 0; | 57 registration2_ = 0; |
77 helper_.reset(); | 58 helper_.reset(); |
78 SetBrowserClientForTesting(old_content_browser_client_); | |
79 } | 59 } |
80 | 60 |
81 bool PatternHasProcessToRun(const GURL& pattern) const { | 61 bool PatternHasProcessToRun(const GURL& pattern) const { |
82 return context_->process_manager()->PatternHasProcessToRun(pattern); | 62 return context_->process_manager()->PatternHasProcessToRun(pattern); |
83 } | 63 } |
84 | 64 |
85 content::TestBrowserThreadBundle thread_bundle_; | 65 content::TestBrowserThreadBundle thread_bundle_; |
86 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 66 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
87 ServiceWorkerContextCore* context_; | 67 ServiceWorkerContextCore* context_; |
88 scoped_refptr<ServiceWorkerRegistration> registration1_; | 68 scoped_refptr<ServiceWorkerRegistration> registration1_; |
89 scoped_refptr<ServiceWorkerRegistration> registration2_; | 69 scoped_refptr<ServiceWorkerRegistration> registration2_; |
90 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 70 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
91 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 71 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
92 GURL script_url_; | 72 GURL script_url_; |
93 ServiceWorkerTestContentClient test_content_client_; | |
94 TestContentBrowserClient test_content_browser_client_; | |
95 ContentBrowserClient* old_content_browser_client_; | |
96 | 73 |
97 private: | 74 private: |
98 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 75 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
99 }; | 76 }; |
100 | 77 |
101 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { | 78 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { |
102 provider_host1_->AddMatchingRegistration(registration1_.get()); | 79 provider_host1_->AddMatchingRegistration(registration1_.get()); |
103 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); | 80 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
104 | 81 |
105 // Adding the same registration twice has no effect. | 82 // Adding the same registration twice has no effect. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 // Match registration should return the longest matching one. | 121 // Match registration should return the longest matching one. |
145 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_); | 122 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_); |
146 provider_host1_->RemoveMatchingRegistration(registration2_.get()); | 123 provider_host1_->RemoveMatchingRegistration(registration2_.get()); |
147 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_); | 124 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_); |
148 | 125 |
149 // Should return nullptr after removing all matching registrations. | 126 // Should return nullptr after removing all matching registrations. |
150 provider_host1_->RemoveMatchingRegistration(registration1_.get()); | 127 provider_host1_->RemoveMatchingRegistration(registration1_.get()); |
151 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr); | 128 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr); |
152 } | 129 } |
153 | 130 |
154 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { | |
155 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; | |
156 | |
157 // Insecure document URL. | |
158 provider_host1_->SetDocumentUrl(GURL("http://host")); | |
159 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | |
160 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | |
161 | |
162 // Insecure parent frame. | |
163 provider_host1_->SetDocumentUrl(GURL("https://host")); | |
164 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | |
165 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | |
166 | |
167 // Secure URL and parent frame. | |
168 provider_host1_->SetDocumentUrl(GURL("https://host")); | |
169 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | |
170 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); | |
171 | |
172 // Exceptional service worker scheme. | |
173 GURL url(std::string(kServiceWorkerScheme) + "://host"); | |
174 EXPECT_TRUE(url.is_valid()); | |
175 provider_host1_->SetDocumentUrl(url); | |
176 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | |
177 EXPECT_FALSE(IsOriginSecure(url)); | |
178 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); | |
179 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); | |
180 | |
181 // Exceptional service worker scheme with insecure parent frame. | |
182 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | |
183 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | |
184 } | |
185 | |
186 } // namespace content | 131 } // namespace content |
OLD | NEW |