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..1a5e05f3fb009904fdaaac30e67c772298d7636f |
| --- /dev/null |
| +++ b/Source/core/dom/AsyncInitializerResolver.h |
| @@ -0,0 +1,116 @@ |
| +// 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 "platform/heap/Heap.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. |
| +// |
| +template<typename T> |
| +class AsyncInitializerResolver : public ContextLifecycleObserver { |
| +public: |
| + typedef T Initializer; |
| + |
| + static WeakPtr<AsyncInitializerResolver<T> > create(ScriptState* scriptState) |
| + { |
| + 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.clear(); |
| + } |
| + template<typename U> void reject(U u) |
| + { |
| + m_resolver->reject(u); |
| + m_protect.clear(); |
| + } |
| + |
| + // 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 |
| + { |
| + ContextLifecycleObserver::contextDestroyed(); |
| + m_initializer->contextDestroyed(); |
| + // |this| will be deleted in the next Oilpan GC. |
| + m_protect.clear(); |
| + } |
| + |
| + WeakPtr<AsyncInitializerResolver<Initializer> > weakPtr() { return m_factory.createWeakPtr(); } |
| + |
| +private: |
| + class Holder : public GarbageCollectedFinalized<Holder> { |
| + public: |
| + Holder(PassOwnPtr<AsyncInitializerResolver<T> > target) : m_protect(target) { } |
| + virtual void trace(Visitor*) { } |
| + |
| + private: |
| + OwnPtr<AsyncInitializerResolver<T> > m_protect; |
| + }; |
| + |
| + AsyncInitializerResolver(ScriptState* scriptState) |
| + : ContextLifecycleObserver(scriptState->executionContext()) |
| + , m_resolver(ScriptPromiseResolverWithContext::create(scriptState)) |
| + , m_factory(this) |
| + , m_protect(new Holder(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, AsyncInitizlierResolver has an OwnPtr referencing self. |
| + // Clearing it means deleting it. |
| + Persistent<Holder> m_protect; |
|
kouhei (in TOK)
2014/06/05 07:54:47
Why don't we make AsyncInitializerResolver Garbage
yhirano
2014/06/05 09:09:19
My compiler complains at having Persistent in Garb
|
| +}; |
| + |
| +} // namespace WebCore |
| + |
| +#endif // #ifndef AsyncInitializerResolver_h |