| Index: Source/modules/crypto/CryptoResultImpl.cpp
|
| diff --git a/Source/modules/crypto/CryptoResultImpl.cpp b/Source/modules/crypto/CryptoResultImpl.cpp
|
| index 9f7d4324fc01f1419ea451107a90dc6c16ed3a38..cc7984d0209914accb45c13da254220bdc04c37b 100644
|
| --- a/Source/modules/crypto/CryptoResultImpl.cpp
|
| +++ b/Source/modules/crypto/CryptoResultImpl.cpp
|
| @@ -31,12 +31,10 @@
|
| #include "config.h"
|
| #include "modules/crypto/CryptoResultImpl.h"
|
|
|
| -#include "bindings/v8/ScriptPromiseResolverWithContext.h"
|
| #include "bindings/v8/ScriptState.h"
|
| -#include "core/dom/ContextLifecycleObserver.h"
|
| +#include "core/dom/AsyncInitializerResolver.h"
|
| #include "core/dom/DOMError.h"
|
| #include "core/dom/DOMException.h"
|
| -#include "core/dom/ExecutionContext.h"
|
| #include "modules/crypto/Key.h"
|
| #include "modules/crypto/KeyPair.h"
|
| #include "modules/crypto/NormalizeAlgorithm.h"
|
| @@ -79,100 +77,22 @@ ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType)
|
|
|
| } // namespace
|
|
|
| -// The PromiseState class contains all the state which is tied to an
|
| -// ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread,
|
| -// PromiseState is not thread safe and must only be accessed and deleted from
|
| -// the blink thread.
|
| -//
|
| -// This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseState.
|
| -// The PromiseState deletes itself after being notified of completion.
|
| -// Additionally the PromiseState is deleted when the ExecutionContext is
|
| -// destroyed (necessary to avoid leaks when dealing with WebWorker threads,
|
| -// which may die before the operation is completed).
|
| -class CryptoResultImpl::PromiseState FINAL {
|
| +class CryptoResultImpl::Initializer FINAL {
|
| public:
|
| - static WeakPtr<PromiseState> create(ScriptState* scriptState)
|
| + static PassOwnPtr<Initializer> create(CryptoResultImpl* owner) { return adoptPtr(new Initializer(owner)); }
|
| + void start(AsyncInitializerResolver<Initializer>* resolver)
|
| {
|
| - PromiseState* promiseState = new PromiseState(scriptState);
|
| - return promiseState->m_weakFactory.createWeakPtr();
|
| - }
|
| -
|
| - void contextDestroyed()
|
| - {
|
| - delete this;
|
| - }
|
| -
|
| - ScriptPromise promise()
|
| - {
|
| - return m_promiseResolver->promise();
|
| - }
|
| -
|
| - void completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails)
|
| - {
|
| - m_promiseResolver->reject(DOMException::create(toExceptionCode(errorType), errorDetails));
|
| - delete this;
|
| - }
|
| -
|
| - void completeWithBuffer(const blink::WebArrayBuffer& buffer)
|
| - {
|
| - m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer));
|
| - delete this;
|
| - }
|
| -
|
| - void completeWithBoolean(bool b)
|
| - {
|
| - m_promiseResolver->resolve(b);
|
| - delete this;
|
| - }
|
| -
|
| - void completeWithKey(const blink::WebCryptoKey& key)
|
| - {
|
| - m_promiseResolver->resolve(Key::create(key));
|
| - delete this;
|
| - }
|
| -
|
| - void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey)
|
| - {
|
| - m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey));
|
| - delete this;
|
| + ASSERT(m_owner);
|
| + m_factory = adoptPtr(new WeakPtrFactory<AsyncInitializerResolver<Initializer> >(resolver));
|
| + m_owner->m_resolver = m_factory->createWeakPtr();
|
| + m_owner = nullptr;
|
| }
|
| + void contextDestroyed() { }
|
|
|
| private:
|
| - // This subclass of ScriptPromiseResolverWithContext is to be notified
|
| - // when the context was destroyed.
|
| - class PromiseResolver FINAL : public ScriptPromiseResolverWithContext {
|
| - public:
|
| - static PassRefPtr<PromiseResolver> create(ScriptState* scriptState, PromiseState* promiseState)
|
| - {
|
| - RefPtr<PromiseResolver> resolver = adoptRef(new PromiseResolver(scriptState, promiseState));
|
| - resolver->suspendIfNeeded();
|
| - return resolver.release();
|
| - }
|
| -
|
| - virtual void contextDestroyed() OVERRIDE
|
| - {
|
| - ScriptPromiseResolverWithContext::contextDestroyed();
|
| - m_promiseState->contextDestroyed();
|
| - }
|
| -
|
| - private:
|
| - explicit PromiseResolver(ScriptState* scriptState, PromiseState* promiseState)
|
| - : ScriptPromiseResolverWithContext(scriptState)
|
| - , m_promiseState(promiseState)
|
| - {
|
| - }
|
| -
|
| - PromiseState* m_promiseState;
|
| - };
|
| -
|
| - explicit PromiseState(ScriptState* scriptState)
|
| - : m_weakFactory(this)
|
| - , m_promiseResolver(PromiseResolver::create(scriptState, this))
|
| - {
|
| - }
|
| -
|
| - WeakPtrFactory<PromiseState> m_weakFactory;
|
| - RefPtr<PromiseResolver> m_promiseResolver;
|
| + explicit Initializer(CryptoResultImpl* owner) : m_owner(owner) { }
|
| + CryptoResultImpl* m_owner;
|
| + OwnPtr<WeakPtrFactory<AsyncInitializerResolver<Initializer> > > m_factory;
|
| };
|
|
|
| CryptoResultImpl::~CryptoResultImpl()
|
| @@ -186,42 +106,42 @@ PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState)
|
|
|
| void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails)
|
| {
|
| - if (m_promiseState)
|
| - m_promiseState->completeWithError(errorType, errorDetails);
|
| + if (m_resolver)
|
| + m_resolver->reject(DOMException::create(toExceptionCode(errorType), errorDetails));
|
| }
|
|
|
| void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer)
|
| {
|
| - if (m_promiseState)
|
| - m_promiseState->completeWithBuffer(buffer);
|
| + if (m_resolver)
|
| + m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer));
|
| }
|
|
|
| void CryptoResultImpl::completeWithBoolean(bool b)
|
| {
|
| - if (m_promiseState)
|
| - m_promiseState->completeWithBoolean(b);
|
| + if (m_resolver)
|
| + m_resolver->resolve(b);
|
| }
|
|
|
| void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key)
|
| {
|
| - if (m_promiseState)
|
| - m_promiseState->completeWithKey(key);
|
| + if (m_resolver)
|
| + m_resolver->resolve(Key::create(key));
|
| }
|
|
|
| void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey)
|
| {
|
| - if (m_promiseState)
|
| - m_promiseState->completeWithKeyPair(publicKey, privateKey);
|
| + if (m_resolver)
|
| + m_resolver->resolve(KeyPair::create(publicKey, privateKey));
|
| }
|
|
|
| CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
|
| - : m_promiseState(PromiseState::create(scriptState))
|
| {
|
| + AsyncInitializerResolver<Initializer>::start(scriptState, Initializer::create(this));
|
| }
|
|
|
| ScriptPromise CryptoResultImpl::promise()
|
| {
|
| - return m_promiseState->promise();
|
| + return m_resolver ? m_resolver->promise() : ScriptPromise();
|
| }
|
|
|
| } // namespace WebCore
|
|
|