| 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::enqueueToCurrentQueue( |
| 55 Element* element, |
| 56 CustomElementReaction* reaction) |
| 57 { |
| 58 enqueue(m_stack.last(), element, reaction); |
| 48 } | 59 } |
| 49 | 60 |
| 50 void CustomElementReactionStack::enqueue( | 61 void CustomElementReactionStack::enqueue( |
| 62 Member<ElementQueue>& queue, |
| 51 Element* element, | 63 Element* element, |
| 52 CustomElementReaction* reaction) | 64 CustomElementReaction* reaction) |
| 53 { | 65 { |
| 54 ElementQueue* queue = m_stack.last(); | |
| 55 if (!queue) | 66 if (!queue) |
| 56 m_stack.last() = queue = new ElementQueue(); | 67 queue = new ElementQueue(); |
| 57 queue->append(element); | 68 queue->append(element); |
| 58 | 69 |
| 59 CustomElementReactionQueue* reactions = m_map.get(element); | 70 CustomElementReactionQueue* reactions = m_map.get(element); |
| 60 if (!reactions) { | 71 if (!reactions) { |
| 61 reactions = new CustomElementReactionQueue(); | 72 reactions = new CustomElementReactionQueue(); |
| 62 m_map.add(element, reactions); | 73 m_map.add(element, reactions); |
| 63 } | 74 } |
| 64 | 75 |
| 65 reactions->add(reaction); | 76 reactions->add(reaction); |
| 66 } | 77 } |
| 67 | 78 |
| 79 void CustomElementReactionStack::enqueueToBackupQueue( |
| 80 Element* element, |
| 81 CustomElementReaction* reaction) |
| 82 { |
| 83 // https://html.spec.whatwg.org/multipage/scripting.html#backup-element-queu
e |
| 84 |
| 85 DCHECK(!CEReactionsScope::current()); |
| 86 DCHECK(m_stack.isEmpty()); |
| 87 DCHECK(isMainThread()); |
| 88 |
| 89 // If the processing the backup element queue is not set: |
| 90 if (!m_backupQueue || m_backupQueue->isEmpty()) { |
| 91 Microtask::enqueueMicrotask(WTF::bind( |
| 92 &CustomElementReactionStack::invokeBackupQueue, |
| 93 Persistent<CustomElementReactionStack>(this))); |
| 94 } |
| 95 |
| 96 enqueue(m_backupQueue, element, reaction); |
| 97 } |
| 98 |
| 99 void CustomElementReactionStack::invokeBackupQueue() |
| 100 { |
| 101 DCHECK(isMainThread()); |
| 102 invokeReactions(*m_backupQueue); |
| 103 m_backupQueue->clear(); |
| 104 } |
| 105 |
| 68 } // namespace blink | 106 } // namespace blink |
| OLD | NEW |