Chromium Code Reviews| 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(ExecutionContext* executionContext, WebType* registration) | |
| 45 { | |
| 46 if (!registration) | |
| 47 return nullptr; | |
| 48 return create(executionContext, adoptPtr(registration)); | |
| 49 } | |
| 50 | |
| 51 PassRefPtrWillBeRawPtr<ServiceWorkerRegistration> ServiceWorkerRegistration::fro m(ScriptPromiseResolver* resolver, WebType* registration) | |
| 52 { | |
| 53 RefPtrWillBeRawPtr<ServiceWorkerRegistration> serviceWorkerRegistration = Se rviceWorkerRegistration::from(resolver->scriptState()->executionContext(), regis tration); | |
| 54 return serviceWorkerRegistration; | |
|
falken
2014/07/25 06:10:23
can this just be return ServiceWorkerRegistration:
nhiroki
2014/07/25 06:55:14
Done.
| |
| 55 } | |
| 56 | |
| 57 String ServiceWorkerRegistration::scope() const | |
| 58 { | |
| 59 return m_outerRegistration->scope().string(); | |
| 60 } | |
| 61 | |
| 62 ScriptPromise ServiceWorkerRegistration::unregister(ScriptState* scriptState) | |
| 63 { | |
| 64 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | |
| 65 ScriptPromise promise = resolver->promise(); | |
| 66 | |
| 67 if (!m_provider) { | |
| 68 resolver->reject(DOMException::create(InvalidStateError, "No associated provider is available")); | |
| 69 return promise; | |
| 70 } | |
| 71 | |
| 72 RefPtr<SecurityOrigin> documentOrigin = scriptState->executionContext()->sec urityOrigin(); | |
| 73 KURL scopeURL = scriptState->executionContext()->completeURL(scope()); | |
| 74 scopeURL.removeFragmentIdentifier(); | |
| 75 if (!scope().isEmpty() && !documentOrigin->canRequest(scopeURL)) { | |
| 76 resolver->reject(DOMException::create(SecurityError, "Can only unregiste r for scopes in the document's origin.")); | |
| 77 return promise; | |
| 78 } | |
| 79 | |
| 80 m_provider->unregisterServiceWorker(scopeURL, new CallbackPromiseAdapter<Und efinedValue, ServiceWorkerError>(resolver)); | |
| 81 return promise; | |
| 82 } | |
| 83 | |
| 84 PassRefPtrWillBeRawPtr<ServiceWorkerRegistration> ServiceWorkerRegistration::cre ate(ExecutionContext* executionContext, PassOwnPtr<blink::WebServiceWorkerRegist ration> outerRegistration) | |
| 85 { | |
| 86 RefPtrWillBeRawPtr<ServiceWorkerRegistration> registration = adoptRefWillBeR efCountedGarbageCollected(new ServiceWorkerRegistration(executionContext, outerR egistration)); | |
| 87 registration->suspendIfNeeded(); | |
| 88 return registration.release(); | |
| 89 } | |
| 90 | |
| 91 ServiceWorkerRegistration::ServiceWorkerRegistration(ExecutionContext* execution Context, PassOwnPtr<blink::WebServiceWorkerRegistration> outerRegistration) | |
| 92 : ActiveDOMObject(executionContext) | |
| 93 , m_outerRegistration(outerRegistration) | |
| 94 , m_provider(0) | |
| 95 { | |
| 96 ASSERT(m_outerRegistration); | |
| 97 ScriptWrappable::init(this); | |
| 98 | |
| 99 if (!executionContext) | |
| 100 return; | |
| 101 if (ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::fro m(executionContext)) | |
| 102 m_provider = client->provider(); | |
| 103 } | |
| 104 | |
| 105 } // namespace blink | |
| OLD | NEW |