OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "ServiceWorkerRegistration.h" |
| 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "bindings/core/v8/ScriptState.h" |
| 12 #include "core/dom/DOMException.h" |
| 13 #include "core/dom/ExceptionCode.h" |
| 14 #include "core/dom/ExecutionContext.h" |
| 15 #include "modules/EventTargetModules.h" |
| 16 #include "modules/serviceworkers/ServiceWorkerContainerClient.h" |
| 17 #include "modules/serviceworkers/ServiceWorkerError.h" |
| 18 #include "public/platform/WebServiceWorkerProvider.h" |
| 19 |
| 20 namespace blink { |
| 21 |
| 22 class UndefinedValue { |
| 23 public: |
| 24 #ifdef DISABLE_SERVICE_WORKER_REGISTRATION |
| 25 typedef WebServiceWorker WebType; |
| 26 #else |
| 27 typedef WebServiceWorkerRegistration WebType; |
| 28 #endif |
| 29 static V8UndefinedType from(ScriptPromiseResolver* resolver, WebType* regist
ration) |
| 30 { |
| 31 ASSERT(!registration); // Anything passed here will be leaked. |
| 32 return V8UndefinedType(); |
| 33 } |
| 34 |
| 35 private: |
| 36 UndefinedValue(); |
| 37 }; |
| 38 |
| 39 const AtomicString& ServiceWorkerRegistration::interfaceName() const |
| 40 { |
| 41 return EventTargetNames::ServiceWorkerRegistration; |
| 42 } |
| 43 |
| 44 PassRefPtrWillBeRawPtr<ServiceWorkerRegistration> ServiceWorkerRegistration::fro
m(ScriptPromiseResolver* resolver, WebType* registration) |
| 45 { |
| 46 if (!registration) |
| 47 return nullptr; |
| 48 return create(resolver->scriptState()->executionContext(), adoptPtr(registra
tion)); |
| 49 } |
| 50 |
| 51 String ServiceWorkerRegistration::scope() const |
| 52 { |
| 53 return m_outerRegistration->scope().string(); |
| 54 } |
| 55 |
| 56 ScriptPromise ServiceWorkerRegistration::unregister(ScriptState* scriptState) |
| 57 { |
| 58 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 59 ScriptPromise promise = resolver->promise(); |
| 60 |
| 61 if (!m_provider) { |
| 62 resolver->reject(DOMException::create(InvalidStateError, "No associated
provider is available")); |
| 63 return promise; |
| 64 } |
| 65 |
| 66 RefPtr<SecurityOrigin> documentOrigin = scriptState->executionContext()->sec
urityOrigin(); |
| 67 KURL scopeURL = scriptState->executionContext()->completeURL(scope()); |
| 68 scopeURL.removeFragmentIdentifier(); |
| 69 if (!scope().isEmpty() && !documentOrigin->canRequest(scopeURL)) { |
| 70 resolver->reject(DOMException::create(SecurityError, "Can only unregiste
r for scopes in the document's origin.")); |
| 71 return promise; |
| 72 } |
| 73 |
| 74 m_provider->unregisterServiceWorker(scopeURL, new CallbackPromiseAdapter<Und
efinedValue, ServiceWorkerError>(resolver)); |
| 75 return promise; |
| 76 } |
| 77 |
| 78 PassRefPtrWillBeRawPtr<ServiceWorkerRegistration> ServiceWorkerRegistration::cre
ate(ExecutionContext* executionContext, PassOwnPtr<WebServiceWorkerRegistration>
outerRegistration) |
| 79 { |
| 80 RefPtrWillBeRawPtr<ServiceWorkerRegistration> registration = adoptRefWillBeR
efCountedGarbageCollected(new ServiceWorkerRegistration(executionContext, outerR
egistration)); |
| 81 registration->suspendIfNeeded(); |
| 82 return registration.release(); |
| 83 } |
| 84 |
| 85 ServiceWorkerRegistration::ServiceWorkerRegistration(ExecutionContext* execution
Context, PassOwnPtr<WebServiceWorkerRegistration> outerRegistration) |
| 86 : ActiveDOMObject(executionContext) |
| 87 , m_outerRegistration(outerRegistration) |
| 88 , m_provider(0) |
| 89 { |
| 90 ASSERT(m_outerRegistration); |
| 91 ScriptWrappable::init(this); |
| 92 |
| 93 if (!executionContext) |
| 94 return; |
| 95 if (ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::fro
m(executionContext)) |
| 96 m_provider = client->provider(); |
| 97 } |
| 98 |
| 99 } // namespace blink |
OLD | NEW |