OLD | NEW |
1 | 1 |
2 // Copyright 2014 The Chromium Authors. All rights reserved. | 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
3 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
4 // found in the LICENSE file. | 4 // found in the LICENSE file. |
5 | 5 |
6 #include "modules/serviceworkers/ServiceWorkerContainer.h" | 6 #include "modules/serviceworkers/ServiceWorkerContainer.h" |
7 | 7 |
8 #include "bindings/core/v8/Dictionary.h" | 8 #include "bindings/core/v8/Dictionary.h" |
9 #include "bindings/core/v8/ScriptFunction.h" | 9 #include "bindings/core/v8/ScriptFunction.h" |
10 #include "bindings/core/v8/ScriptPromise.h" | 10 #include "bindings/core/v8/ScriptPromise.h" |
11 #include "bindings/core/v8/ScriptState.h" | 11 #include "bindings/core/v8/ScriptState.h" |
12 #include "bindings/core/v8/V8DOMException.h" | 12 #include "bindings/core/v8/V8DOMException.h" |
13 #include "bindings/core/v8/V8GCController.h" | 13 #include "bindings/core/v8/V8GCController.h" |
14 #include "core/dom/DOMException.h" | 14 #include "core/dom/DOMException.h" |
15 #include "core/dom/Document.h" | 15 #include "core/dom/Document.h" |
16 #include "core/dom/ExecutionContext.h" | 16 #include "core/dom/ExecutionContext.h" |
17 #include "core/page/FocusController.h" | 17 #include "core/page/FocusController.h" |
18 #include "core/testing/DummyPageHolder.h" | 18 #include "core/testing/DummyPageHolder.h" |
19 #include "modules/serviceworkers/ServiceWorkerContainerClient.h" | 19 #include "modules/serviceworkers/ServiceWorkerContainerClient.h" |
20 #include "platform/weborigin/KURL.h" | 20 #include "platform/weborigin/KURL.h" |
21 #include "platform/weborigin/SecurityOrigin.h" | 21 #include "platform/weborigin/SecurityOrigin.h" |
22 #include "public/platform/WebURL.h" | 22 #include "public/platform/WebURL.h" |
23 #include "public/platform/modules/serviceworker/WebServiceWorkerClientsInfo.h" | 23 #include "public/platform/modules/serviceworker/WebServiceWorkerClientsInfo.h" |
24 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h" | 24 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "wtf/OwnPtr.h" | 26 #include "wtf/PtrUtil.h" |
27 #include "wtf/PassOwnPtr.h" | |
28 #include "wtf/text/WTFString.h" | 27 #include "wtf/text/WTFString.h" |
| 28 #include <memory> |
29 #include <v8.h> | 29 #include <v8.h> |
30 | 30 |
31 namespace blink { | 31 namespace blink { |
32 namespace { | 32 namespace { |
33 | 33 |
34 // Promise-related test support. | 34 // Promise-related test support. |
35 | 35 |
36 struct StubScriptFunction { | 36 struct StubScriptFunction { |
37 public: | 37 public: |
38 StubScriptFunction() | 38 StubScriptFunction() |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 ~ServiceWorkerContainerTest() | 155 ~ServiceWorkerContainerTest() |
156 { | 156 { |
157 m_page.reset(); | 157 m_page.reset(); |
158 V8GCController::collectAllGarbageForTesting(isolate()); | 158 V8GCController::collectAllGarbageForTesting(isolate()); |
159 } | 159 } |
160 | 160 |
161 ExecutionContext* getExecutionContext() { return &(m_page->document()); } | 161 ExecutionContext* getExecutionContext() { return &(m_page->document()); } |
162 v8::Isolate* isolate() { return v8::Isolate::GetCurrent(); } | 162 v8::Isolate* isolate() { return v8::Isolate::GetCurrent(); } |
163 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc
ument().frame()); } | 163 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc
ument().frame()); } |
164 | 164 |
165 void provide(PassOwnPtr<WebServiceWorkerProvider> provider) | 165 void provide(std::unique_ptr<WebServiceWorkerProvider> provider) |
166 { | 166 { |
167 Supplement<Document>::provideTo(m_page->document(), ServiceWorkerContain
erClient::supplementName(), ServiceWorkerContainerClient::create(std::move(provi
der))); | 167 Supplement<Document>::provideTo(m_page->document(), ServiceWorkerContain
erClient::supplementName(), ServiceWorkerContainerClient::create(std::move(provi
der))); |
168 } | 168 } |
169 | 169 |
170 void setPageURL(const String& url) | 170 void setPageURL(const String& url) |
171 { | 171 { |
172 // For URL completion. | 172 // For URL completion. |
173 m_page->document().setURL(KURL(KURL(), url)); | 173 m_page->document().setURL(KURL(KURL(), url)); |
174 | 174 |
175 // The basis for security checks. | 175 // The basis for security checks. |
176 m_page->document().setSecurityOrigin(SecurityOrigin::createFromString(ur
l)); | 176 m_page->document().setSecurityOrigin(SecurityOrigin::createFromString(ur
l)); |
177 } | 177 } |
178 | 178 |
179 void testRegisterRejected(const String& scriptURL, const String& scope, cons
t ScriptValueTest& valueTest) | 179 void testRegisterRejected(const String& scriptURL, const String& scope, cons
t ScriptValueTest& valueTest) |
180 { | 180 { |
181 // When the registration is rejected, a register call must not reach | 181 // When the registration is rejected, a register call must not reach |
182 // the provider. | 182 // the provider. |
183 provide(adoptPtr(new NotReachedWebServiceWorkerProvider())); | 183 provide(wrapUnique(new NotReachedWebServiceWorkerProvider())); |
184 | 184 |
185 ServiceWorkerContainer* container = ServiceWorkerContainer::create(getEx
ecutionContext()); | 185 ServiceWorkerContainer* container = ServiceWorkerContainer::create(getEx
ecutionContext()); |
186 ScriptState::Scope scriptScope(getScriptState()); | 186 ScriptState::Scope scriptScope(getScriptState()); |
187 RegistrationOptions options; | 187 RegistrationOptions options; |
188 options.setScope(scope); | 188 options.setScope(scope); |
189 ScriptPromise promise = container->registerServiceWorker(getScriptState(
), scriptURL, options); | 189 ScriptPromise promise = container->registerServiceWorker(getScriptState(
), scriptURL, options); |
190 expectRejected(getScriptState(), promise, valueTest); | 190 expectRejected(getScriptState(), promise, valueTest); |
191 | 191 |
192 container->willBeDetachedFromFrame(); | 192 container->willBeDetachedFromFrame(); |
193 } | 193 } |
194 | 194 |
195 void testGetRegistrationRejected(const String& documentURL, const ScriptValu
eTest& valueTest) | 195 void testGetRegistrationRejected(const String& documentURL, const ScriptValu
eTest& valueTest) |
196 { | 196 { |
197 provide(adoptPtr(new NotReachedWebServiceWorkerProvider())); | 197 provide(wrapUnique(new NotReachedWebServiceWorkerProvider())); |
198 | 198 |
199 ServiceWorkerContainer* container = ServiceWorkerContainer::create(getEx
ecutionContext()); | 199 ServiceWorkerContainer* container = ServiceWorkerContainer::create(getEx
ecutionContext()); |
200 ScriptState::Scope scriptScope(getScriptState()); | 200 ScriptState::Scope scriptScope(getScriptState()); |
201 ScriptPromise promise = container->getRegistration(getScriptState(), doc
umentURL); | 201 ScriptPromise promise = container->getRegistration(getScriptState(), doc
umentURL); |
202 expectRejected(getScriptState(), promise, valueTest); | 202 expectRejected(getScriptState(), promise, valueTest); |
203 | 203 |
204 container->willBeDetachedFromFrame(); | 204 container->willBeDetachedFromFrame(); |
205 } | 205 } |
206 | 206 |
207 private: | 207 private: |
208 OwnPtr<DummyPageHolder> m_page; | 208 std::unique_ptr<DummyPageHolder> m_page; |
209 }; | 209 }; |
210 | 210 |
211 TEST_F(ServiceWorkerContainerTest, Register_NonSecureOriginIsRejected) | 211 TEST_F(ServiceWorkerContainerTest, Register_NonSecureOriginIsRejected) |
212 { | 212 { |
213 setPageURL("http://www.example.com/"); | 213 setPageURL("http://www.example.com/"); |
214 testRegisterRejected( | 214 testRegisterRejected( |
215 "http://www.example.com/worker.js", | 215 "http://www.example.com/worker.js", |
216 "http://www.example.com/", | 216 "http://www.example.com/", |
217 ExpectDOMException("SecurityError", "Only secure origins are allowed (se
e: https://goo.gl/Y0ZkNV).")); | 217 ExpectDOMException("SecurityError", "Only secure origins are allowed (se
e: https://goo.gl/Y0ZkNV).")); |
218 } | 218 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 StubWebServiceWorkerProvider() | 256 StubWebServiceWorkerProvider() |
257 : m_registerCallCount(0) | 257 : m_registerCallCount(0) |
258 , m_getRegistrationCallCount(0) | 258 , m_getRegistrationCallCount(0) |
259 { | 259 { |
260 } | 260 } |
261 | 261 |
262 // Creates a WebServiceWorkerProvider. This can outlive the | 262 // Creates a WebServiceWorkerProvider. This can outlive the |
263 // StubWebServiceWorkerProvider, but |registerServiceWorker| and | 263 // StubWebServiceWorkerProvider, but |registerServiceWorker| and |
264 // other methods must not be called after the | 264 // other methods must not be called after the |
265 // StubWebServiceWorkerProvider dies. | 265 // StubWebServiceWorkerProvider dies. |
266 PassOwnPtr<WebServiceWorkerProvider> provider() | 266 std::unique_ptr<WebServiceWorkerProvider> provider() |
267 { | 267 { |
268 return adoptPtr(new WebServiceWorkerProviderImpl(*this)); | 268 return wrapUnique(new WebServiceWorkerProviderImpl(*this)); |
269 } | 269 } |
270 | 270 |
271 size_t registerCallCount() { return m_registerCallCount; } | 271 size_t registerCallCount() { return m_registerCallCount; } |
272 const WebURL& registerScope() { return m_registerScope; } | 272 const WebURL& registerScope() { return m_registerScope; } |
273 const WebURL& registerScriptURL() { return m_registerScriptURL; } | 273 const WebURL& registerScriptURL() { return m_registerScriptURL; } |
274 size_t getRegistrationCallCount() { return m_getRegistrationCallCount; } | 274 size_t getRegistrationCallCount() { return m_getRegistrationCallCount; } |
275 const WebURL& getRegistrationURL() { return m_getRegistrationURL; } | 275 const WebURL& getRegistrationURL() { return m_getRegistrationURL; } |
276 | 276 |
277 private: | 277 private: |
278 class WebServiceWorkerProviderImpl : public WebServiceWorkerProvider { | 278 class WebServiceWorkerProviderImpl : public WebServiceWorkerProvider { |
279 public: | 279 public: |
280 WebServiceWorkerProviderImpl(StubWebServiceWorkerProvider& owner) | 280 WebServiceWorkerProviderImpl(StubWebServiceWorkerProvider& owner) |
281 : m_owner(owner) | 281 : m_owner(owner) |
282 { | 282 { |
283 } | 283 } |
284 | 284 |
285 ~WebServiceWorkerProviderImpl() override { } | 285 ~WebServiceWorkerProviderImpl() override { } |
286 | 286 |
287 void registerServiceWorker(const WebURL& pattern, const WebURL& scriptUR
L, WebServiceWorkerRegistrationCallbacks* callbacks) override | 287 void registerServiceWorker(const WebURL& pattern, const WebURL& scriptUR
L, WebServiceWorkerRegistrationCallbacks* callbacks) override |
288 { | 288 { |
289 m_owner.m_registerCallCount++; | 289 m_owner.m_registerCallCount++; |
290 m_owner.m_registerScope = pattern; | 290 m_owner.m_registerScope = pattern; |
291 m_owner.m_registerScriptURL = scriptURL; | 291 m_owner.m_registerScriptURL = scriptURL; |
292 m_registrationCallbacksToDelete.append(adoptPtr(callbacks)); | 292 m_registrationCallbacksToDelete.append(wrapUnique(callbacks)); |
293 } | 293 } |
294 | 294 |
295 void getRegistration(const WebURL& documentURL, WebServiceWorkerGetRegis
trationCallbacks* callbacks) override | 295 void getRegistration(const WebURL& documentURL, WebServiceWorkerGetRegis
trationCallbacks* callbacks) override |
296 { | 296 { |
297 m_owner.m_getRegistrationCallCount++; | 297 m_owner.m_getRegistrationCallCount++; |
298 m_owner.m_getRegistrationURL = documentURL; | 298 m_owner.m_getRegistrationURL = documentURL; |
299 m_getRegistrationCallbacksToDelete.append(adoptPtr(callbacks)); | 299 m_getRegistrationCallbacksToDelete.append(wrapUnique(callbacks)); |
300 } | 300 } |
301 | 301 |
302 bool validateScopeAndScriptURL(const WebURL& scope, const WebURL& script
URL, WebString* errorMessage) | 302 bool validateScopeAndScriptURL(const WebURL& scope, const WebURL& script
URL, WebString* errorMessage) |
303 { | 303 { |
304 return true; | 304 return true; |
305 } | 305 } |
306 | 306 |
307 private: | 307 private: |
308 StubWebServiceWorkerProvider& m_owner; | 308 StubWebServiceWorkerProvider& m_owner; |
309 Vector<OwnPtr<WebServiceWorkerRegistrationCallbacks>> m_registrationCall
backsToDelete; | 309 Vector<std::unique_ptr<WebServiceWorkerRegistrationCallbacks>> m_registr
ationCallbacksToDelete; |
310 Vector<OwnPtr<WebServiceWorkerGetRegistrationCallbacks>> m_getRegistrati
onCallbacksToDelete; | 310 Vector<std::unique_ptr<WebServiceWorkerGetRegistrationCallbacks>> m_getR
egistrationCallbacksToDelete; |
311 }; | 311 }; |
312 | 312 |
313 private: | 313 private: |
314 size_t m_registerCallCount; | 314 size_t m_registerCallCount; |
315 WebURL m_registerScope; | 315 WebURL m_registerScope; |
316 WebURL m_registerScriptURL; | 316 WebURL m_registerScriptURL; |
317 size_t m_getRegistrationCallCount; | 317 size_t m_getRegistrationCallCount; |
318 WebURL m_getRegistrationURL; | 318 WebURL m_getRegistrationURL; |
319 }; | 319 }; |
320 | 320 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 container->getRegistration(getScriptState(), ""); | 356 container->getRegistration(getScriptState(), ""); |
357 EXPECT_EQ(1ul, stubProvider.getRegistrationCallCount()); | 357 EXPECT_EQ(1ul, stubProvider.getRegistrationCallCount()); |
358 EXPECT_EQ(WebURL(KURL(KURL(), "http://localhost/x/index.html")), stubPro
vider.getRegistrationURL()); | 358 EXPECT_EQ(WebURL(KURL(KURL(), "http://localhost/x/index.html")), stubPro
vider.getRegistrationURL()); |
359 } | 359 } |
360 | 360 |
361 container->willBeDetachedFromFrame(); | 361 container->willBeDetachedFromFrame(); |
362 } | 362 } |
363 | 363 |
364 } // namespace | 364 } // namespace |
365 } // namespace blink | 365 } // namespace blink |
OLD | NEW |