| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/dom/custom/CustomElementReactionStack.h" | 5 #include "core/dom/custom/CustomElementReactionStack.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/Microtask.h" |
| 7 #include "core/dom/Element.h" | 8 #include "core/dom/Element.h" |
| 9 #include "core/dom/custom/CEReactionsScope.h" |
| 8 #include "core/dom/custom/CustomElementReactionQueue.h" | 10 #include "core/dom/custom/CustomElementReactionQueue.h" |
| 11 #include "core/frame/FrameHost.h" |
| 9 | 12 |
| 10 namespace blink { | 13 namespace blink { |
| 11 | 14 |
| 12 // TODO(dominicc): Consider using linked heap structures, avoiding | 15 // TODO(dominicc): Consider using linked heap structures, avoiding |
| 13 // finalizers, to make short-lived entries fast. | 16 // finalizers, to make short-lived entries fast. |
| 14 | 17 |
| 15 CustomElementReactionStack::CustomElementReactionStack() | 18 CustomElementReactionStack::CustomElementReactionStack() |
| 16 { | 19 { |
| 17 } | 20 } |
| 18 | 21 |
| 19 DEFINE_TRACE(CustomElementReactionStack) | 22 DEFINE_TRACE(CustomElementReactionStack) |
| 20 { | 23 { |
| 21 visitor->trace(m_map); | 24 visitor->trace(m_map); |
| 22 visitor->trace(m_stack); | 25 visitor->trace(m_stack); |
| 26 visitor->trace(m_backupQueue); |
| 23 } | 27 } |
| 24 | 28 |
| 25 void CustomElementReactionStack::push() | 29 void CustomElementReactionStack::push() |
| 26 { | 30 { |
| 27 m_stack.append(nullptr); | 31 m_stack.append(nullptr); |
| 28 } | 32 } |
| 29 | 33 |
| 30 void CustomElementReactionStack::popInvokingReactions() | 34 void CustomElementReactionStack::popInvokingReactions() |
| 31 { | 35 { |
| 32 ElementQueue* queue = m_stack.last(); | 36 ElementQueue* queue = m_stack.last(); |
| 33 if (!queue) { | 37 if (queue) |
| 34 m_stack.removeLast(); | 38 invokeReactions(*queue); |
| 35 return; | 39 m_stack.removeLast(); |
| 36 } | 40 } |
| 37 | 41 |
| 38 for (size_t i = 0; i < queue->size(); ++i) { | 42 void CustomElementReactionStack::invokeReactions(ElementQueue& queue) |
| 39 Element* element = (*queue)[i]; | 43 { |
| 44 for (size_t i = 0; i < queue.size(); ++i) { |
| 45 Element* element = queue[i]; |
| 40 if (CustomElementReactionQueue* reactions = m_map.get(element)) { | 46 if (CustomElementReactionQueue* reactions = m_map.get(element)) { |
| 41 reactions->invokeReactions(element); | 47 reactions->invokeReactions(element); |
| 42 CHECK(reactions->isEmpty()); | 48 CHECK(reactions->isEmpty()); |
| 43 m_map.remove(element); | 49 m_map.remove(element); |
| 44 } | 50 } |
| 45 } | 51 } |
| 52 } |
| 46 | 53 |
| 47 m_stack.removeLast(); | 54 void CustomElementReactionStack::enqueue(Element* element, CustomElementReaction
* reaction) |
| 55 { |
| 56 // To enqueue an element on the appropriate element queue |
| 57 // https://html.spec.whatwg.org/multipage/scripting.html#enqueue-an-element-
on-the-appropriate-element-queue |
| 58 |
| 59 // If the custom element reactions stack is not empty, then |
| 60 // Add element to the current element queue. |
| 61 if (CustomElementReactionStack* current = |
| 62 CEReactionsScope::currentCustomElementReactionStack(element)) { |
| 63 current->enqueueToCurrentQueue(element, reaction); |
| 64 return; |
| 65 } |
| 66 |
| 67 // If the custom element reactions stack is empty, then |
| 68 // Add element to the backup element queue. |
| 69 element->document().frameHost()->customElementReactionStack() |
| 70 .enqueueToBackupQueue(element, reaction); |
| 71 } |
| 72 |
| 73 void CustomElementReactionStack::enqueueToCurrentQueue( |
| 74 Element* element, |
| 75 CustomElementReaction* reaction) |
| 76 { |
| 77 enqueue(m_stack.last(), element, reaction); |
| 48 } | 78 } |
| 49 | 79 |
| 50 void CustomElementReactionStack::enqueue( | 80 void CustomElementReactionStack::enqueue( |
| 81 Member<ElementQueue>& queue, |
| 51 Element* element, | 82 Element* element, |
| 52 CustomElementReaction* reaction) | 83 CustomElementReaction* reaction) |
| 53 { | 84 { |
| 54 ElementQueue* queue = m_stack.last(); | |
| 55 if (!queue) | 85 if (!queue) |
| 56 m_stack.last() = queue = new ElementQueue(); | 86 queue = new ElementQueue(); |
| 57 queue->append(element); | 87 queue->append(element); |
| 58 | 88 |
| 59 CustomElementReactionQueue* reactions = m_map.get(element); | 89 CustomElementReactionQueue* reactions = m_map.get(element); |
| 60 if (!reactions) { | 90 if (!reactions) { |
| 61 reactions = new CustomElementReactionQueue(); | 91 reactions = new CustomElementReactionQueue(); |
| 62 m_map.add(element, reactions); | 92 m_map.add(element, reactions); |
| 63 } | 93 } |
| 64 | 94 |
| 65 reactions->add(reaction); | 95 reactions->add(reaction); |
| 66 } | 96 } |
| 67 | 97 |
| 98 void CustomElementReactionStack::enqueueToBackupQueue( |
| 99 Element* element, |
| 100 CustomElementReaction* reaction) |
| 101 { |
| 102 // https://html.spec.whatwg.org/multipage/scripting.html#backup-element-queu
e |
| 103 |
| 104 DCHECK(m_stack.isEmpty()); |
| 105 DCHECK(isMainThread()); |
| 106 |
| 107 // If the processing the backup element queue is not set: |
| 108 if (!m_backupQueue || m_backupQueue->isEmpty()) { |
| 109 Microtask::enqueueMicrotask(WTF::bind( |
| 110 &CustomElementReactionStack::invokeBackupQueue, |
| 111 Persistent<CustomElementReactionStack>(this))); |
| 112 } |
| 113 |
| 114 enqueue(m_backupQueue, element, reaction); |
| 115 } |
| 116 |
| 117 void CustomElementReactionStack::invokeBackupQueue() |
| 118 { |
| 119 DCHECK(isMainThread()); |
| 120 invokeReactions(*m_backupQueue); |
| 121 m_backupQueue->clear(); |
| 122 } |
| 123 |
| 68 } // namespace blink | 124 } // namespace blink |
| OLD | NEW |