| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "core/dom/CustomElementCallbackDispatcher.h" | 32 #include "core/dom/CustomElementCallbackDispatcher.h" |
| 33 | 33 |
| 34 #include "core/dom/CustomElementCallbackInvocation.h" |
| 34 #include "wtf/MainThread.h" | 35 #include "wtf/MainThread.h" |
| 35 | 36 |
| 36 namespace WebCore { | 37 namespace WebCore { |
| 37 | 38 |
| 38 size_t CustomElementCallbackDispatcher::s_elementQueueStart = 0; | 39 size_t CustomElementCallbackDispatcher::s_elementQueueStart = 0; |
| 39 | 40 |
| 40 // The base of the stack has a null sentinel value. | 41 // The base of the stack has a null sentinel value. |
| 41 size_t CustomElementCallbackDispatcher::s_elementQueueEnd = 1; | 42 size_t CustomElementCallbackDispatcher::s_elementQueueEnd = 1; |
| 42 | 43 |
| 43 CustomElementCallbackDispatcher& CustomElementCallbackDispatcher::instance() | 44 CustomElementCallbackDispatcher& CustomElementCallbackDispatcher::instance() |
| 44 { | 45 { |
| 45 DEFINE_STATIC_LOCAL(CustomElementCallbackDispatcher, instance, ()); | 46 DEFINE_STATIC_LOCAL(CustomElementCallbackDispatcher, instance, ()); |
| 46 return instance; | 47 return instance; |
| 47 } | 48 } |
| 48 | 49 |
| 50 void CustomElementCallbackDispatcher::enqueueAttributeChangedCallback(PassRefPtr
<CustomElementLifecycleCallbacks> callbacks, PassRefPtr<Element> element, const
AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue) |
| 51 { |
| 52 if (!callbacks->hasAttributeChanged()) |
| 53 return; |
| 54 |
| 55 CustomElementCallbackQueue* queue = ensureCallbackQueue(callbacks, element); |
| 56 bool isInCurrentQueue = queue->owner() == currentElementQueue(); |
| 57 if (!isInCurrentQueue) { |
| 58 queue->setOwner(currentElementQueue()); |
| 59 m_flattenedProcessingStack.append(queue); |
| 60 s_elementQueueEnd++; |
| 61 } |
| 62 |
| 63 queue->append(CustomElementCallbackInvocation::createAttributeChangedInvocat
ion(name, oldValue, newValue)); |
| 64 } |
| 65 |
| 49 void CustomElementCallbackDispatcher::enqueueCreatedCallback(PassRefPtr<CustomEl
ementLifecycleCallbacks> callbacks, PassRefPtr<Element> element) | 66 void CustomElementCallbackDispatcher::enqueueCreatedCallback(PassRefPtr<CustomEl
ementLifecycleCallbacks> callbacks, PassRefPtr<Element> element) |
| 50 { | 67 { |
| 51 Element* key = element.get(); | 68 if (!callbacks->hasCreated()) |
| 52 ElementCallbackQueueMap::AddResult result = m_elementCallbackQueueMap.add(ke
y, CustomElementCallbackQueue::create(callbacks, element)); | 69 return; |
| 53 ASSERT(result.isNewEntry); // creation callbacks are always scheduled first | |
| 54 | 70 |
| 55 CustomElementCallbackQueue* queue = result.iterator->value.get(); | 71 CustomElementCallbackQueue* queue = createCallbackQueue(callbacks, element); |
| 56 queue->setOwner(currentElementQueue()); | 72 queue->setOwner(currentElementQueue()); |
| 57 queue->append(CustomElementLifecycleCallbacks::Created); | |
| 58 | 73 |
| 59 // The created callback is unique in being prepended to the front | 74 // The created callback is unique in being prepended to the front |
| 60 // of the element queue | 75 // of the element queue |
| 61 m_flattenedProcessingStack.insert(inCallbackDeliveryScope() ? s_elementQueue
Start : /* skip null sentinel */ 1, queue); | 76 m_flattenedProcessingStack.insert(inCallbackDeliveryScope() ? s_elementQueue
Start : /* skip null sentinel */ 1, queue); |
| 62 s_elementQueueEnd++; | 77 s_elementQueueEnd++; |
| 78 |
| 79 queue->append(CustomElementCallbackInvocation::createCreatedInvocation()); |
| 63 } | 80 } |
| 64 | 81 |
| 65 // Dispatches callbacks at microtask checkpoint. | 82 // Dispatches callbacks at microtask checkpoint. |
| 66 bool CustomElementCallbackDispatcher::dispatch() | 83 bool CustomElementCallbackDispatcher::dispatch() |
| 67 { | 84 { |
| 68 ASSERT(isMainThread()); | 85 ASSERT(isMainThread()); |
| 69 ASSERT(!inCallbackDeliveryScope()); | 86 if (inCallbackDeliveryScope()) |
| 87 return false; |
| 88 |
| 70 size_t start = 1; // skip null sentinel | 89 size_t start = 1; // skip null sentinel |
| 71 size_t end = s_elementQueueEnd; | 90 size_t end = s_elementQueueEnd; |
| 72 | 91 |
| 73 for (size_t i = start; i < end; i++) { | 92 for (size_t i = start; i < end; i++) { |
| 74 m_flattenedProcessingStack[i]->processInElementQueue(currentElementQueue
()); | 93 m_flattenedProcessingStack[i]->processInElementQueue(currentElementQueue
()); |
| 75 | 94 |
| 76 // new callbacks as a result of recursion must be scheduled in | 95 // new callbacks as a result of recursion must be scheduled in |
| 77 // a CallbackDeliveryScope which restore this queue on completion | 96 // a CallbackDeliveryScope which restore this queue on completion |
| 78 ASSERT(!s_elementQueueStart); | 97 ASSERT(!s_elementQueueStart); |
| 79 ASSERT(s_elementQueueEnd == end); | 98 ASSERT(s_elementQueueEnd == end); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 109 } | 128 } |
| 110 | 129 |
| 111 // Pop the element queue from the processing stack | 130 // Pop the element queue from the processing stack |
| 112 m_flattenedProcessingStack.resize(start); | 131 m_flattenedProcessingStack.resize(start); |
| 113 s_elementQueueEnd = start; | 132 s_elementQueueEnd = start; |
| 114 | 133 |
| 115 if (start == /* allow sentinel */ 1) | 134 if (start == /* allow sentinel */ 1) |
| 116 m_elementCallbackQueueMap.clear(); | 135 m_elementCallbackQueueMap.clear(); |
| 117 } | 136 } |
| 118 | 137 |
| 138 CustomElementCallbackQueue* CustomElementCallbackDispatcher::createCallbackQueue
(PassRefPtr<CustomElementLifecycleCallbacks> callbacks, PassRefPtr<Element> elem
ent) |
| 139 { |
| 140 Element* key = element.get(); |
| 141 ElementCallbackQueueMap::AddResult result = m_elementCallbackQueueMap.add(ke
y, CustomElementCallbackQueue::create(callbacks, element)); |
| 142 ASSERT(result.isNewEntry); |
| 143 return result.iterator->value.get(); |
| 144 } |
| 145 |
| 146 CustomElementCallbackQueue* CustomElementCallbackDispatcher::ensureCallbackQueue
(PassRefPtr<CustomElementLifecycleCallbacks> callbacks, PassRefPtr<Element> elem
ent) |
| 147 { |
| 148 Element* key = element.get(); |
| 149 ElementCallbackQueueMap::iterator it = m_elementCallbackQueueMap.find(key); |
| 150 if (it == m_elementCallbackQueueMap.end()) |
| 151 it = m_elementCallbackQueueMap.add(key, CustomElementCallbackQueue::crea
te(callbacks, element)).iterator; |
| 152 return it->value.get(); |
| 153 } |
| 154 |
| 119 } // namespace WebCore | 155 } // namespace WebCore |
| OLD | NEW |