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 "config.h" | 5 #include "config.h" |
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" |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 ServiceWorkerContainer* container = ServiceWorkerContainer::create(execu
tionContext()); | 198 ServiceWorkerContainer* container = ServiceWorkerContainer::create(execu
tionContext()); |
199 ScriptState::Scope scriptScope(scriptState()); | 199 ScriptState::Scope scriptScope(scriptState()); |
200 Dictionary options = Dictionary::createEmpty(isolate()); | 200 Dictionary options = Dictionary::createEmpty(isolate()); |
201 EXPECT_TRUE(options.set("scope", scope)); | 201 EXPECT_TRUE(options.set("scope", scope)); |
202 ScriptPromise promise = container->unregisterServiceWorker(scriptState()
, scope); | 202 ScriptPromise promise = container->unregisterServiceWorker(scriptState()
, scope); |
203 expectRejected(scriptState(), promise, valueTest); | 203 expectRejected(scriptState(), promise, valueTest); |
204 | 204 |
205 container->willBeDetachedFromFrame(); | 205 container->willBeDetachedFromFrame(); |
206 } | 206 } |
207 | 207 |
| 208 void testGetRegistrationRejected(const String& documentURL, const ScriptValu
eTest& valueTest) |
| 209 { |
| 210 provide(adoptPtr(new NotReachedWebServiceWorkerProvider())); |
| 211 |
| 212 ServiceWorkerContainer* container = ServiceWorkerContainer::create(execu
tionContext()); |
| 213 ScriptState::Scope scriptScope(scriptState()); |
| 214 ScriptPromise promise = container->getRegistration(scriptState(), docume
ntURL); |
| 215 expectRejected(scriptState(), promise, valueTest); |
| 216 |
| 217 container->willBeDetachedFromFrame(); |
| 218 } |
| 219 |
208 private: | 220 private: |
209 OwnPtr<DummyPageHolder> m_page; | 221 OwnPtr<DummyPageHolder> m_page; |
210 }; | 222 }; |
211 | 223 |
212 TEST_F(ServiceWorkerContainerTest, Register_NonSecureOriginIsRejected) | 224 TEST_F(ServiceWorkerContainerTest, Register_NonSecureOriginIsRejected) |
213 { | 225 { |
214 setPageURL("http://www.example.com/"); | 226 setPageURL("http://www.example.com/"); |
215 testRegisterRejected( | 227 testRegisterRejected( |
216 "http://www.example.com/worker.js", | 228 "http://www.example.com/worker.js", |
217 "http://www.example.com/", | 229 "http://www.example.com/", |
(...skipping 27 matching lines...) Expand all Loading... |
245 } | 257 } |
246 | 258 |
247 TEST_F(ServiceWorkerContainerTest, Unregister_CrossOriginScopeIsRejected) | 259 TEST_F(ServiceWorkerContainerTest, Unregister_CrossOriginScopeIsRejected) |
248 { | 260 { |
249 setPageURL("https://www.example.com"); | 261 setPageURL("https://www.example.com"); |
250 testUnregisterRejected( | 262 testUnregisterRejected( |
251 "https://example.com/", // Differs by host | 263 "https://example.com/", // Differs by host |
252 ExpectDOMException("SecurityError", "The scope must match the current or
igin.")); | 264 ExpectDOMException("SecurityError", "The scope must match the current or
igin.")); |
253 } | 265 } |
254 | 266 |
| 267 TEST_F(ServiceWorkerContainerTest, GetRegistration_NonSecureOriginIsRejected) |
| 268 { |
| 269 setPageURL("http://www.example.com/"); |
| 270 testGetRegistrationRejected( |
| 271 "http://www.example.com/", |
| 272 ExpectDOMException("NotSupportedError", "Only secure origins are allowed
. http://goo.gl/lq4gCo")); |
| 273 } |
| 274 |
| 275 TEST_F(ServiceWorkerContainerTest, GetRegistration_CrossOriginURLIsRejected) |
| 276 { |
| 277 setPageURL("https://www.example.com/"); |
| 278 testGetRegistrationRejected( |
| 279 "https://foo.example.com/", // Differs by host |
| 280 ExpectDOMException("SecurityError", "The documentURL must match the curr
ent origin.")); |
| 281 } |
| 282 |
255 class StubWebServiceWorkerProvider { | 283 class StubWebServiceWorkerProvider { |
256 public: | 284 public: |
257 StubWebServiceWorkerProvider() | 285 StubWebServiceWorkerProvider() |
258 : m_registerCallCount(0) | 286 : m_registerCallCount(0) |
259 , m_unregisterCallCount(0) | 287 , m_unregisterCallCount(0) |
| 288 , m_getRegistrationCallCount(0) |
260 { | 289 { |
261 } | 290 } |
262 | 291 |
263 // Creates a WebServiceWorkerProvider. This can outlive the | 292 // Creates a WebServiceWorkerProvider. This can outlive the |
264 // StubWebServiceWorkerProvider, but |registerServiceWorker| and | 293 // StubWebServiceWorkerProvider, but |registerServiceWorker| and |
265 // other methods must not be called after the | 294 // other methods must not be called after the |
266 // StubWebServiceWorkerProvider dies. | 295 // StubWebServiceWorkerProvider dies. |
267 PassOwnPtr<WebServiceWorkerProvider> provider() | 296 PassOwnPtr<WebServiceWorkerProvider> provider() |
268 { | 297 { |
269 return adoptPtr(new WebServiceWorkerProviderImpl(*this)); | 298 return adoptPtr(new WebServiceWorkerProviderImpl(*this)); |
270 } | 299 } |
271 | 300 |
272 size_t registerCallCount() { return m_registerCallCount; } | 301 size_t registerCallCount() { return m_registerCallCount; } |
273 const WebURL& registerScope() { return m_registerScope; } | 302 const WebURL& registerScope() { return m_registerScope; } |
274 const WebURL& registerScriptURL() { return m_registerScriptURL; } | 303 const WebURL& registerScriptURL() { return m_registerScriptURL; } |
275 size_t unregisterCallCount() { return m_unregisterCallCount; } | 304 size_t unregisterCallCount() { return m_unregisterCallCount; } |
276 const WebURL& unregisterScope() { return m_unregisterScope; } | 305 const WebURL& unregisterScope() { return m_unregisterScope; } |
| 306 size_t getRegistrationCallCount() { return m_getRegistrationCallCount; } |
| 307 const WebURL& getRegistrationURL() { return m_getRegistrationURL; } |
277 | 308 |
278 private: | 309 private: |
279 class WebServiceWorkerProviderImpl : public WebServiceWorkerProvider { | 310 class WebServiceWorkerProviderImpl : public WebServiceWorkerProvider { |
280 public: | 311 public: |
281 WebServiceWorkerProviderImpl(StubWebServiceWorkerProvider& owner) | 312 WebServiceWorkerProviderImpl(StubWebServiceWorkerProvider& owner) |
282 : m_owner(owner) | 313 : m_owner(owner) |
283 { | 314 { |
284 } | 315 } |
285 | 316 |
286 virtual ~WebServiceWorkerProviderImpl() OVERRIDE { } | 317 virtual ~WebServiceWorkerProviderImpl() OVERRIDE { } |
287 | 318 |
288 virtual void registerServiceWorker(const WebURL& pattern, const WebURL&
scriptURL, WebServiceWorkerRegistrationCallbacks* callbacks) OVERRIDE | 319 virtual void registerServiceWorker(const WebURL& pattern, const WebURL&
scriptURL, WebServiceWorkerRegistrationCallbacks* callbacks) OVERRIDE |
289 { | 320 { |
290 m_owner.m_registerCallCount++; | 321 m_owner.m_registerCallCount++; |
291 m_owner.m_registerScope = pattern; | 322 m_owner.m_registerScope = pattern; |
292 m_owner.m_registerScriptURL = scriptURL; | 323 m_owner.m_registerScriptURL = scriptURL; |
293 m_registrationCallbacksToDelete.append(adoptPtr(callbacks)); | 324 m_registrationCallbacksToDelete.append(adoptPtr(callbacks)); |
294 } | 325 } |
295 | 326 |
296 virtual void unregisterServiceWorker(const WebURL& pattern, WebServiceWo
rkerUnregistrationCallbacks* callbacks) OVERRIDE | 327 virtual void unregisterServiceWorker(const WebURL& pattern, WebServiceWo
rkerUnregistrationCallbacks* callbacks) OVERRIDE |
297 { | 328 { |
298 m_owner.m_unregisterCallCount++; | 329 m_owner.m_unregisterCallCount++; |
299 m_owner.m_unregisterScope = pattern; | 330 m_owner.m_unregisterScope = pattern; |
300 m_unregistrationCallbacksToDelete.append(adoptPtr(callbacks)); | 331 m_unregistrationCallbacksToDelete.append(adoptPtr(callbacks)); |
301 } | 332 } |
302 | 333 |
| 334 virtual void getRegistration(const WebURL& documentURL, WebServiceWorker
GetRegistrationCallbacks* callbacks) OVERRIDE |
| 335 { |
| 336 m_owner.m_getRegistrationCallCount++; |
| 337 m_owner.m_getRegistrationURL = documentURL; |
| 338 m_getRegistrationCallbacksToDelete.append(adoptPtr(callbacks)); |
| 339 } |
| 340 |
303 private: | 341 private: |
304 StubWebServiceWorkerProvider& m_owner; | 342 StubWebServiceWorkerProvider& m_owner; |
305 Vector<OwnPtr<WebServiceWorkerRegistrationCallbacks> > m_registrationCal
lbacksToDelete; | 343 Vector<OwnPtr<WebServiceWorkerRegistrationCallbacks> > m_registrationCal
lbacksToDelete; |
306 Vector<OwnPtr<WebServiceWorkerUnregistrationCallbacks> > m_unregistratio
nCallbacksToDelete; | 344 Vector<OwnPtr<WebServiceWorkerUnregistrationCallbacks> > m_unregistratio
nCallbacksToDelete; |
| 345 Vector<OwnPtr<WebServiceWorkerGetRegistrationCallbacks> > m_getRegistrat
ionCallbacksToDelete; |
307 }; | 346 }; |
308 | 347 |
309 private: | 348 private: |
310 size_t m_registerCallCount; | 349 size_t m_registerCallCount; |
311 WebURL m_registerScope; | 350 WebURL m_registerScope; |
312 WebURL m_registerScriptURL; | 351 WebURL m_registerScriptURL; |
313 size_t m_unregisterCallCount; | 352 size_t m_unregisterCallCount; |
314 WebURL m_unregisterScope; | 353 WebURL m_unregisterScope; |
| 354 size_t m_getRegistrationCallCount; |
| 355 WebURL m_getRegistrationURL; |
315 }; | 356 }; |
316 | 357 |
317 TEST_F(ServiceWorkerContainerTest, RegisterUnregister_NonHttpsSecureOriginDelega
tesToProvider) | 358 TEST_F(ServiceWorkerContainerTest, RegisterUnregister_NonHttpsSecureOriginDelega
tesToProvider) |
318 { | 359 { |
319 setPageURL("http://localhost/x/index.html"); | 360 setPageURL("http://localhost/x/index.html"); |
320 | 361 |
321 StubWebServiceWorkerProvider stubProvider; | 362 StubWebServiceWorkerProvider stubProvider; |
322 provide(stubProvider.provider()); | 363 provide(stubProvider.provider()); |
323 | 364 |
324 ServiceWorkerContainer* container = ServiceWorkerContainer::create(execution
Context()); | 365 ServiceWorkerContainer* container = ServiceWorkerContainer::create(execution
Context()); |
(...skipping 15 matching lines...) Expand all Loading... |
340 ScriptState::Scope scriptScope(scriptState()); | 381 ScriptState::Scope scriptScope(scriptState()); |
341 container->unregisterServiceWorker(scriptState(), "y/"); | 382 container->unregisterServiceWorker(scriptState(), "y/"); |
342 | 383 |
343 EXPECT_EQ(1ul, stubProvider.unregisterCallCount()); | 384 EXPECT_EQ(1ul, stubProvider.unregisterCallCount()); |
344 EXPECT_EQ(WebURL(KURL(KURL(), "http://localhost/x/y/")), stubProvider.un
registerScope()); | 385 EXPECT_EQ(WebURL(KURL(KURL(), "http://localhost/x/y/")), stubProvider.un
registerScope()); |
345 } | 386 } |
346 | 387 |
347 container->willBeDetachedFromFrame(); | 388 container->willBeDetachedFromFrame(); |
348 } | 389 } |
349 | 390 |
| 391 TEST_F(ServiceWorkerContainerTest, GetRegistration_OmittedDocumentURLDefaultsToP
ageURL) |
| 392 { |
| 393 setPageURL("http://localhost/x/index.html"); |
| 394 |
| 395 StubWebServiceWorkerProvider stubProvider; |
| 396 provide(stubProvider.provider()); |
| 397 |
| 398 ServiceWorkerContainer* container = ServiceWorkerContainer::create(execution
Context()); |
| 399 |
| 400 { |
| 401 ScriptState::Scope scriptScope(scriptState()); |
| 402 container->getRegistration(scriptState(), ""); |
| 403 EXPECT_EQ(1ul, stubProvider.getRegistrationCallCount()); |
| 404 EXPECT_EQ(WebURL(KURL(KURL(), "http://localhost/x/index.html")), stubPro
vider.getRegistrationURL()); |
| 405 } |
| 406 |
| 407 container->willBeDetachedFromFrame(); |
| 408 } |
| 409 |
350 } // namespace | 410 } // namespace |
351 } // namespace blink | 411 } // namespace blink |
OLD | NEW |