LifecycleCallbackQueue.cpp [plain text]
#include "config.h"
#include "LifecycleCallbackQueue.h"
#if ENABLE(CUSTOM_ELEMENTS)
#include "Document.h"
#include "Element.h"
#include "JSCustomElementInterface.h"
#include "JSDOMBinding.h"
#include <heap/Heap.h>
#include <wtf/Optional.h>
#include <wtf/Ref.h>
namespace WebCore {
class LifecycleQueueItem {
public:
enum class Type {
ElementUpgrade,
AttributeChanged,
};
LifecycleQueueItem(Type type, Element& element, JSCustomElementInterface& elementInterface)
: m_type(type)
, m_element(element)
, m_interface(elementInterface)
{ }
LifecycleQueueItem(Element& element, JSCustomElementInterface& elementInterface, const QualifiedName& attributeName, const AtomicString& oldValue, const AtomicString& newValue)
: m_type(Type::AttributeChanged)
, m_element(element)
, m_interface(elementInterface)
, m_attributeName(attributeName)
, m_oldValue(oldValue)
, m_newValue(newValue)
{ }
void invoke()
{
switch (m_type) {
case Type::ElementUpgrade:
m_interface->upgradeElement(m_element.get());
break;
case Type::AttributeChanged:
ASSERT(m_attributeName);
m_interface->attributeChanged(m_element.get(), m_attributeName.value(), m_oldValue, m_newValue);
break;
}
}
private:
Type m_type;
Ref<Element> m_element;
Ref<JSCustomElementInterface> m_interface;
Optional<QualifiedName> m_attributeName;
AtomicString m_oldValue;
AtomicString m_newValue;
};
LifecycleCallbackQueue::LifecycleCallbackQueue()
{ }
LifecycleCallbackQueue::~LifecycleCallbackQueue()
{
ASSERT(m_items.isEmpty());
}
void LifecycleCallbackQueue::enqueueElementUpgrade(Element& element, JSCustomElementInterface& elementInterface)
{
if (auto* queue = CustomElementLifecycleProcessingStack::ensureCurrentQueue())
queue->m_items.append(LifecycleQueueItem(LifecycleQueueItem::Type::ElementUpgrade, element, elementInterface));
}
void LifecycleCallbackQueue::enqueueAttributeChangedCallback(Element& element, JSCustomElementInterface& elementInterface,
const QualifiedName& attributeName, const AtomicString& oldValue, const AtomicString& newValue)
{
if (auto* queue = CustomElementLifecycleProcessingStack::ensureCurrentQueue())
queue->m_items.append(LifecycleQueueItem(element, elementInterface, attributeName, oldValue, newValue));
}
void LifecycleCallbackQueue::invokeAll()
{
Vector<LifecycleQueueItem> items;
items.swap(m_items);
for (auto& item : items)
item.invoke();
}
LifecycleCallbackQueue* CustomElementLifecycleProcessingStack::ensureCurrentQueue()
{
if (!s_currentProcessingStack)
return nullptr;
auto*& queue = s_currentProcessingStack->m_queue;
if (!queue) queue = new LifecycleCallbackQueue;
return queue;
}
CustomElementLifecycleProcessingStack* CustomElementLifecycleProcessingStack::s_currentProcessingStack = nullptr;
void CustomElementLifecycleProcessingStack::processQueue()
{
ASSERT(m_queue);
m_queue->invokeAll();
delete m_queue;
m_queue = nullptr;
}
}
#endif