1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// Copyright 2016 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.
#include "core/dom/custom/CustomElementReactionStack.h"
#include "bindings/core/v8/Microtask.h"
#include "core/dom/Element.h"
#include "core/dom/custom/CEReactionsScope.h"
#include "core/dom/custom/CustomElementReactionQueue.h"
#include "core/frame/FrameHost.h"
namespace blink {
namespace {
Persistent<CustomElementReactionStack>& customElementReactionStack() {
DEFINE_STATIC_LOCAL(Persistent<CustomElementReactionStack>,
customElementReactionStack,
(new CustomElementReactionStack));
return customElementReactionStack;
}
} // namespace
// TODO(dominicc): Consider using linked heap structures, avoiding
// finalizers, to make short-lived entries fast.
CustomElementReactionStack::CustomElementReactionStack() {}
DEFINE_TRACE(CustomElementReactionStack) {
visitor->trace(m_map);
visitor->trace(m_stack);
visitor->trace(m_backupQueue);
}
DEFINE_TRACE_WRAPPERS(CustomElementReactionStack) {
for (auto key : m_map.keys()) {
visitor->traceWrappers(key);
}
}
void CustomElementReactionStack::push() {
m_stack.push_back(nullptr);
}
void CustomElementReactionStack::popInvokingReactions() {
ElementQueue* queue = m_stack.back();
if (queue)
invokeReactions(*queue);
m_stack.pop_back();
}
void CustomElementReactionStack::invokeReactions(ElementQueue& queue) {
for (size_t i = 0; i < queue.size(); ++i) {
Element* element = queue[i];
if (CustomElementReactionQueue* reactions = m_map.get(element)) {
reactions->invokeReactions(element);
CHECK(reactions->isEmpty());
m_map.remove(element);
}
}
}
void CustomElementReactionStack::enqueueToCurrentQueue(
Element* element,
CustomElementReaction* reaction) {
enqueue(m_stack.back(), element, reaction);
}
void CustomElementReactionStack::enqueue(Member<ElementQueue>& queue,
Element* element,
CustomElementReaction* reaction) {
if (!queue)
queue = new ElementQueue();
queue->push_back(element);
CustomElementReactionQueue* reactions = m_map.get(element);
if (!reactions) {
reactions = new CustomElementReactionQueue();
m_map.add(TraceWrapperMember<Element>(this, element), reactions);
}
reactions->add(reaction);
}
void CustomElementReactionStack::enqueueToBackupQueue(
Element* element,
CustomElementReaction* reaction) {
// https://html.spec.whatwg.org/multipage/scripting.html#backup-element-queue
DCHECK(!CEReactionsScope::current());
DCHECK(m_stack.isEmpty());
DCHECK(isMainThread());
// If the processing the backup element queue is not set:
if (!m_backupQueue || m_backupQueue->isEmpty()) {
Microtask::enqueueMicrotask(
WTF::bind(&CustomElementReactionStack::invokeBackupQueue,
Persistent<CustomElementReactionStack>(this)));
}
enqueue(m_backupQueue, element, reaction);
}
void CustomElementReactionStack::clearQueue(Element* element) {
if (CustomElementReactionQueue* reactions = m_map.get(element))
reactions->clear();
}
void CustomElementReactionStack::invokeBackupQueue() {
DCHECK(isMainThread());
invokeReactions(*m_backupQueue);
m_backupQueue->clear();
}
CustomElementReactionStack& CustomElementReactionStack::current() {
return *customElementReactionStack();
}
CustomElementReactionStack*
CustomElementReactionStackTestSupport::setCurrentForTest(
CustomElementReactionStack* newStack) {
Persistent<CustomElementReactionStack>& stack = customElementReactionStack();
CustomElementReactionStack* oldStack = stack.get();
stack = newStack;
return oldStack;
}
} // namespace blink
|