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..87e2764c9154a922067b99eba068abef33db4ef5 |
| --- /dev/null |
| +++ b/Source/core/dom/AsyncInitializerResolver.h |
| @@ -0,0 +1,97 @@ |
| +// 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" |
| + |
| +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. |
| +// i.e. You can access the raw resolver pointer set by start() in the |
| +// initializer. |
| +// |
| +template<typename T> |
| +class AsyncInitializerResolver : public ContextLifecycleObserver { |
| +public: |
| + typedef T Initializer; |
| + |
| + static ScriptPromise start(ScriptState* scriptState, PassOwnPtr<Initializer> initializer) |
| + { |
| + AsyncInitializerResolver* resolver = new AsyncInitializerResolver(scriptState, initializer); |
| + ScriptPromise promise = resolver->promise(); |
| + resolver->m_initializer->start(resolver); |
| + 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(); |
| + } |
| + |
| +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, PassOwnPtr<Initializer> initializer) |
| + : ContextLifecycleObserver(scriptState->executionContext()) |
| + , m_resolver(ScriptPromiseResolverWithContext::create(scriptState)) |
| + , m_initializer(initializer) |
| + , m_protect(new Holder(adoptPtr(this))) |
| + { |
| + } |
| + |
| + RefPtr<ScriptPromiseResolverWithContext> m_resolver; |
| + OwnPtr<Initializer> m_initializer; |
| + // 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; |
|
haraken
2014/06/10 10:54:59
I wonder why you need oilpan to keep the Initializ
yhirano
2014/06/11 06:54:28
We can't destroy a ContextLifecycleObserver in ano
|
| +}; |
| + |
| +} // namespace WebCore |
| + |
| +#endif // #ifndef AsyncInitializerResolver_h |