Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: content/browser/service_worker/service_worker_provider_host_unittest.cc

Issue 2638313002: Manage ServiceWorkerDispatcherHost in ServiceWorkerContextCore (Closed)
Patch Set: Remove a break line Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 48
49 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); 49 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
50 context_ = helper_->context(); 50 context_ = helper_->context();
51 script_url_ = GURL("https://www.example.com/service_worker.js"); 51 script_url_ = GURL("https://www.example.com/service_worker.js");
52 registration1_ = new ServiceWorkerRegistration( 52 registration1_ = new ServiceWorkerRegistration(
53 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); 53 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr());
54 registration2_ = new ServiceWorkerRegistration( 54 registration2_ = new ServiceWorkerRegistration(
55 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); 55 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr());
56 registration3_ = new ServiceWorkerRegistration( 56 registration3_ = new ServiceWorkerRegistration(
57 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr()); 57 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr());
58
59 // Prepare provider hosts (for the same process).
60 std::unique_ptr<ServiceWorkerProviderHost> host1(
61 new ServiceWorkerProviderHost(
62 helper_->mock_render_process_id(), MSG_ROUTING_NONE,
63 1 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
64 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE,
65 context_->AsWeakPtr(), NULL));
66 host1->SetDocumentUrl(GURL("https://www.example.com/example1.html"));
67 std::unique_ptr<ServiceWorkerProviderHost> host2(
68 new ServiceWorkerProviderHost(
69 helper_->mock_render_process_id(), MSG_ROUTING_NONE,
70 2 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
71 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE,
72 context_->AsWeakPtr(), NULL));
73 host2->SetDocumentUrl(GURL("https://www.example.com/example2.html"));
74 provider_host1_ = host1->AsWeakPtr();
75 provider_host2_ = host2->AsWeakPtr();
76 context_->AddProviderHost(base::WrapUnique(host1.release()));
77 context_->AddProviderHost(base::WrapUnique(host2.release()));
78 } 58 }
79 59
80 void TearDown() override { 60 void TearDown() override {
81 registration1_ = 0; 61 registration1_ = 0;
82 registration2_ = 0; 62 registration2_ = 0;
83 helper_.reset(); 63 helper_.reset();
84 SetBrowserClientForTesting(old_content_browser_client_); 64 SetBrowserClientForTesting(old_content_browser_client_);
85 // Reset cached security schemes so we don't affect other tests. 65 // Reset cached security schemes so we don't affect other tests.
86 ResetSchemesAndOriginsWhitelist(); 66 ResetSchemesAndOriginsWhitelist();
87 } 67 }
88 68
89 bool PatternHasProcessToRun(const GURL& pattern) const { 69 bool PatternHasProcessToRun(const GURL& pattern) const {
90 return context_->process_manager()->PatternHasProcessToRun(pattern); 70 return context_->process_manager()->PatternHasProcessToRun(pattern);
91 } 71 }
92 72
73 ServiceWorkerProviderHost* CreateProviderHost(
74 const GURL& document_url,
75 bool is_parent_frame_secure = true) {
falken 2017/02/08 06:53:51 Let's avoid the default param and add a CreateProv
shimazu 2017/02/13 03:25:56 Done.
76 static int provider_id = 1;
77 std::unique_ptr<ServiceWorkerProviderHost> host =
78 CreateProviderHostForWindow(helper_->mock_render_process_id(),
79 provider_id++, is_parent_frame_secure,
80 helper_->context()->AsWeakPtr());
81 ServiceWorkerProviderHost* host_raw = host.get();
82 host->SetDocumentUrl(document_url);
83 context_->AddProviderHost(std::move(host));
84 return host_raw;
85 }
86
93 TestBrowserThreadBundle thread_bundle_; 87 TestBrowserThreadBundle thread_bundle_;
94 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; 88 std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
95 ServiceWorkerContextCore* context_; 89 ServiceWorkerContextCore* context_;
96 scoped_refptr<ServiceWorkerRegistration> registration1_; 90 scoped_refptr<ServiceWorkerRegistration> registration1_;
97 scoped_refptr<ServiceWorkerRegistration> registration2_; 91 scoped_refptr<ServiceWorkerRegistration> registration2_;
98 scoped_refptr<ServiceWorkerRegistration> registration3_; 92 scoped_refptr<ServiceWorkerRegistration> registration3_;
99 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
100 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
101 GURL script_url_; 93 GURL script_url_;
102 ServiceWorkerTestContentClient test_content_client_; 94 ServiceWorkerTestContentClient test_content_client_;
103 TestContentBrowserClient test_content_browser_client_; 95 TestContentBrowserClient test_content_browser_client_;
104 ContentBrowserClient* old_content_browser_client_; 96 ContentBrowserClient* old_content_browser_client_;
105 97
106 private: 98 private:
107 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); 99 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest);
108 }; 100 };
109 101
110 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { 102 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) {
103 ServiceWorkerProviderHost* provider_host1 =
104 CreateProviderHost(GURL("https://www.example.com/example1.html"));
105 ServiceWorkerProviderHost* provider_host2 =
106 CreateProviderHost(GURL("https://www.example.com/example2.html"));
107
111 // Matching registrations have already been set by SetDocumentUrl. 108 // Matching registrations have already been set by SetDocumentUrl.
112 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 109 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
113 110
114 // Different matching registrations have already been added. 111 // Different matching registrations have already been added.
115 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); 112 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern()));
116 113
117 // Adding the same registration twice has no effect. 114 // Adding the same registration twice has no effect.
118 provider_host1_->AddMatchingRegistration(registration1_.get()); 115 provider_host1->AddMatchingRegistration(registration1_.get());
119 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 116 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
120 117
121 // Removing a matching registration will decrease the process refs for its 118 // Removing a matching registration will decrease the process refs for its
122 // pattern. 119 // pattern.
123 provider_host1_->RemoveMatchingRegistration(registration1_.get()); 120 provider_host1->RemoveMatchingRegistration(registration1_.get());
124 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 121 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
125 provider_host2_->RemoveMatchingRegistration(registration1_.get()); 122 provider_host2->RemoveMatchingRegistration(registration1_.get());
126 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); 123 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern()));
127 124
128 // Matching registration will be removed when moving out of scope 125 // Matching registration will be removed when moving out of scope
129 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host1,2 126 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host1,2
130 ASSERT_FALSE(PatternHasProcessToRun(registration3_->pattern())); // no host 127 ASSERT_FALSE(PatternHasProcessToRun(registration3_->pattern())); // no host
131 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/")); 128 provider_host1->SetDocumentUrl(GURL("https://other.example.com/"));
132 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host2 129 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host2
133 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1 130 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1
134 provider_host2_->SetDocumentUrl(GURL("https://other.example.com/")); 131 provider_host2->SetDocumentUrl(GURL("https://other.example.com/"));
135 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); // no host 132 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); // no host
136 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1,2 133 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1,2
137 } 134 }
138 135
139 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { 136 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) {
137 ServiceWorkerProviderHost* provider_host1 =
138 CreateProviderHost(GURL("https://www.example.com/example1.html"));
139
140 // Associating the registration will also increase the process refs for 140 // Associating the registration will also increase the process refs for
141 // the registration's pattern. 141 // the registration's pattern.
142 provider_host1_->AssociateRegistration(registration1_.get(), 142 provider_host1->AssociateRegistration(registration1_.get(),
143 false /* notify_controllerchange */); 143 false /* notify_controllerchange */);
144 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 144 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
145 145
146 // Disassociating the registration shouldn't affect the process refs for 146 // Disassociating the registration shouldn't affect the process refs for
147 // the registration's pattern. 147 // the registration's pattern.
148 provider_host1_->DisassociateRegistration(); 148 provider_host1->DisassociateRegistration();
149 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 149 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
150 } 150 }
151 151
152 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { 152 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) {
153 ServiceWorkerProviderHost* provider_host1 =
154 CreateProviderHost(GURL("https://www.example.com/example1.html"));
155
153 // Match registration should return the longest matching one. 156 // Match registration should return the longest matching one.
154 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration()); 157 ASSERT_EQ(registration2_, provider_host1->MatchRegistration());
155 provider_host1_->RemoveMatchingRegistration(registration2_.get()); 158 provider_host1->RemoveMatchingRegistration(registration2_.get());
156 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration()); 159 ASSERT_EQ(registration1_, provider_host1->MatchRegistration());
157 160
158 // Should return nullptr after removing all matching registrations. 161 // Should return nullptr after removing all matching registrations.
159 provider_host1_->RemoveMatchingRegistration(registration1_.get()); 162 provider_host1->RemoveMatchingRegistration(registration1_.get());
160 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); 163 ASSERT_EQ(nullptr, provider_host1->MatchRegistration());
161 164
162 // SetDocumentUrl sets all of matching registrations 165 // SetDocumentUrl sets all of matching registrations
163 provider_host1_->SetDocumentUrl(GURL("https://www.example.com/example1")); 166 provider_host1->SetDocumentUrl(GURL("https://www.example.com/example1"));
164 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration()); 167 ASSERT_EQ(registration2_, provider_host1->MatchRegistration());
165 provider_host1_->RemoveMatchingRegistration(registration2_.get()); 168 provider_host1->RemoveMatchingRegistration(registration2_.get());
166 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration()); 169 ASSERT_EQ(registration1_, provider_host1->MatchRegistration());
167 170
168 // SetDocumentUrl with another origin also updates matching registrations 171 // SetDocumentUrl with another origin also updates matching registrations
169 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example")); 172 provider_host1->SetDocumentUrl(GURL("https://other.example.com/example"));
170 ASSERT_EQ(registration3_, provider_host1_->MatchRegistration()); 173 ASSERT_EQ(registration3_, provider_host1->MatchRegistration());
171 provider_host1_->RemoveMatchingRegistration(registration3_.get()); 174 provider_host1->RemoveMatchingRegistration(registration3_.get());
172 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); 175 ASSERT_EQ(nullptr, provider_host1->MatchRegistration());
173 } 176 }
174 177
175 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { 178 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) {
176 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; 179 ServiceWorkerProviderHost* provider_host_secure_parent =
180 CreateProviderHost(GURL("https://www.example.com/example1.html"), true);
181 ServiceWorkerProviderHost* provider_host_insecure_parent =
182 CreateProviderHost(GURL("https://www.example.com/example1.html"), false);
177 183
178 // Insecure document URL. 184 // Insecure document URL.
179 provider_host1_->SetDocumentUrl(GURL("http://host")); 185 provider_host_secure_parent->SetDocumentUrl(GURL("http://host"));
180 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; 186 EXPECT_FALSE(provider_host_secure_parent->IsContextSecureForServiceWorker());
181 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker());
182 187
183 // Insecure parent frame. 188 // Insecure parent frame.
184 provider_host1_->SetDocumentUrl(GURL("https://host")); 189 provider_host_insecure_parent->SetDocumentUrl(GURL("https://host"));
185 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; 190 EXPECT_FALSE(
186 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); 191 provider_host_insecure_parent->IsContextSecureForServiceWorker());
187 192
188 // Secure URL and parent frame. 193 // Secure URL and parent frame.
189 provider_host1_->SetDocumentUrl(GURL("https://host")); 194 provider_host_secure_parent->SetDocumentUrl(GURL("https://host"));
190 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; 195 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker());
191 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker());
192 196
193 // Exceptional service worker scheme. 197 // Exceptional service worker scheme.
194 GURL url(std::string(kServiceWorkerScheme) + "://host"); 198 GURL url(std::string(kServiceWorkerScheme) + "://host");
195 EXPECT_TRUE(url.is_valid()); 199 EXPECT_TRUE(url.is_valid());
196 provider_host1_->SetDocumentUrl(url);
197 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE;
198 EXPECT_FALSE(IsOriginSecure(url)); 200 EXPECT_FALSE(IsOriginSecure(url));
199 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); 201 EXPECT_TRUE(OriginCanAccessServiceWorkers(url));
200 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); 202 provider_host_secure_parent->SetDocumentUrl(url);
203 EXPECT_TRUE(provider_host_secure_parent->IsContextSecureForServiceWorker());
201 204
202 // Exceptional service worker scheme with insecure parent frame. 205 // Exceptional service worker scheme with insecure parent frame.
203 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; 206 provider_host_insecure_parent->SetDocumentUrl(url);
204 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); 207 EXPECT_FALSE(
208 provider_host_insecure_parent->IsContextSecureForServiceWorker());
205 } 209 }
206 210
207 } // namespace content 211 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698