Chromium Code Reviews| Index: Source/core/dom/AsyncInitializerResolver.h |
| diff --git a/Source/core/dom/AsyncInitializerResolver.h b/Source/core/dom/AsyncInitializerResolver.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d00d5b9d46724cdbb1fee500ada8489721aa57f0 |
| --- /dev/null |
| +++ b/Source/core/dom/AsyncInitializerResolver.h |
| @@ -0,0 +1,114 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef AsyncInitializerResolver_h |
| +#define AsyncInitializerResolver_h |
| + |
| +#include "bindings/v8/ScriptPromiseResolverWithContext.h" |
| +#include "core/dom/ContextLifecycleObserver.h" |
| +#include "core/dom/ExecutionContext.h" |
| +#include "wtf/OwnPtr.h" |
| +#include "wtf/RefPtr.h" |
| +#include "wtf/WeakPtr.h" |
| + |
| +namespace WebCore { |
| + |
| +// AsyncInitializerResolver is a helper class of asynchronous initialization |
| +// using Promises. Once AsyncInitializerResolver<T> is created, the resolver |
| +// will live until one of the following conditions is met: |
| +// - the associated promise is resolved |
| +// - the associated promise is rejected |
| +// - the associated execution context is destroyed |
| +// |
| +// AsyncInitializerResolver<Initializer> takes an actual initializer. |
| +// It is guaranteed the resolver stays alive while the initializer is alive |
| +// after start() is called, |
| +// i.e. You can access the resolver pointer set by start() in the initializer |
| +// If you want to access the resolver outside of the initializer, |
| +// use a weak pointer. |
| +// |
| +// Note that an AsyncInitializerResolver must not have another |
| +// AsyncInitializerResolver directly or undirectly, due to a restriction of |
| +// LifecycleNotifier and LifecycleObserver. |
| +template<typename T> |
| +class AsyncInitializerResolver : public LifecycleObserver<AsyncInitializerContext> { |
| +public: |
| + typedef T Initializer; |
| + |
| + static WeakPtr<AsyncInitializerResolver<T> > create(ScriptState* scriptState) |
|
kouhei (in TOK)
2014/06/05 07:54:47
Is this used?
yhirano
2014/06/06 07:40:48
Removed.
|
| + { |
| + AsyncInitializerResolver* resolver = new AsyncInitializerResolver(scriptState); |
| + return resolver->weakPtr(); |
| + } |
| + |
| + void start(PassOwnPtr<Initializer> initializer) |
| + { |
| + ASSERT(!m_initializer); |
| + m_initializer = initializer; |
| + m_initializer->start(this); |
| + } |
| + |
| + static ScriptPromise createAndStart(ScriptState* scriptState, PassOwnPtr<T> initializer) |
| + { |
| + AsyncInitializerResolver* resolver = new AsyncInitializerResolver(scriptState); |
| + ScriptPromise promise = resolver->promise(); |
| + resolver->start(initializer); |
| + return promise; |
| + } |
| + |
| + virtual ~AsyncInitializerResolver() { } |
| + |
| + ScriptPromise promise() { return m_resolver->promise(); } |
| + ScriptState* scriptState() const { return m_resolver->scriptState(); } |
| + template<typename U> void resolve(U u) |
| + { |
| + m_resolver->resolve(u); |
| + m_protect = 0; |
| + } |
| + template<typename U> void reject(U u) |
| + { |
| + m_resolver->reject(u); |
| + m_protect = 0; |
| + } |
| + |
| + // LifecycleObserver implementation. |
| + // Because the lifecycle context is held in the associated execution |
| + // context, this function is called when the execution context is |
| + // destroyed. |
| + virtual void contextDestroyed() OVERRIDE |
| + { |
| + m_initializer->contextDestroyed(); |
| + LifecycleObserver<AsyncInitializerContext>::contextDestroyed(); |
| + m_protect = 0; |
| + } |
| + |
| + WeakPtr<AsyncInitializerResolver<Initializer> > weakPtr() { return m_factory.createWeakPtr(); } |
| + |
| +private: |
| + AsyncInitializerResolver(ScriptState* scriptState) |
| + : LifecycleObserver<AsyncInitializerContext>(&scriptState->executionContext()->asyncInitializerContext()) |
| + , m_resolver(ScriptPromiseResolverWithContext::create(scriptState)) |
| + , m_factory(this) |
| + , m_protect(adoptPtr(this)) |
| + { |
| + } |
| + |
| + RefPtr<ScriptPromiseResolverWithContext> m_resolver; |
| + OwnPtr<Initializer> m_initializer; |
| + WeakPtrFactory<AsyncInitializerResolver<Initializer> > m_factory; |
| + // In order to keep alive self until one of the termination conditions |
| + // is met, AsyncInitizlier has an OwnPtr referencing self. |
| + // Clearing it means deleting it. |
| + OwnPtr<AsyncInitializerResolver<Initializer> > m_protect; |
| +}; |
| + |
| +// |
| +// These functions should be specialized for each LifecycleObserver instances. |
| +// |
| +template<> void observerContext(AsyncInitializerContext*, LifecycleObserver<AsyncInitializerContext>*); |
| +template<> void unobserverContext(AsyncInitializerContext*, LifecycleObserver<AsyncInitializerContext>*); |
| + |
| +} // namespace WebCore |
| + |
| +#endif // #ifndef AsyncInitializerResolver_h |