| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * 3. Neither the name of Google Inc. nor the names of its contributors | |
| 15 * may be used to endorse or promote products derived from this | |
| 16 * software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #include "core/dom/custom/CustomElementScheduler.h" | |
| 32 | |
| 33 #include "core/dom/Document.h" | |
| 34 #include "core/dom/Element.h" | |
| 35 #include "core/dom/custom/CustomElementCallbackInvocation.h" | |
| 36 #include "core/dom/custom/CustomElementLifecycleCallbacks.h" | |
| 37 #include "core/dom/custom/CustomElementMicrotaskDispatcher.h" | |
| 38 #include "core/dom/custom/CustomElementMicrotaskImportStep.h" | |
| 39 #include "core/dom/custom/CustomElementMicrotaskResolutionStep.h" | |
| 40 #include "core/dom/custom/CustomElementMicrotaskRunQueue.h" | |
| 41 #include "core/dom/custom/CustomElementProcessingStack.h" | |
| 42 #include "core/dom/custom/CustomElementRegistrationContext.h" | |
| 43 #include "core/dom/custom/CustomElementSyncMicrotaskQueue.h" | |
| 44 #include "core/html/imports/HTMLImportChild.h" | |
| 45 #include "core/html/imports/HTMLImportsController.h" | |
| 46 | |
| 47 namespace blink { | |
| 48 | |
| 49 // FIXME: Consider moving the element's callback queue to ElementRareData. | |
| 50 typedef HeapHashMap<Member<Element>, Member<CustomElementCallbackQueue>> Element
CallbackQueueMap; | |
| 51 | |
| 52 static ElementCallbackQueueMap& callbackQueues() | |
| 53 { | |
| 54 DEFINE_STATIC_LOCAL(ElementCallbackQueueMap, map, (new ElementCallbackQueueM
ap)); | |
| 55 return map; | |
| 56 } | |
| 57 | |
| 58 static CustomElementCallbackQueue& ensureCallbackQueue(Element* element) | |
| 59 { | |
| 60 ElementCallbackQueueMap::ValueType* it = callbackQueues().add(element, nullp
tr).storedValue; | |
| 61 if (!it->value) | |
| 62 it->value = CustomElementCallbackQueue::create(element); | |
| 63 return *it->value.get(); | |
| 64 } | |
| 65 | |
| 66 // Finds or creates the callback queue for element. | |
| 67 static CustomElementCallbackQueue& scheduleCallbackQueue(Element* element) | |
| 68 { | |
| 69 CustomElementCallbackQueue& callbackQueue = ensureCallbackQueue(element); | |
| 70 if (callbackQueue.inCreatedCallback()) { | |
| 71 // Don't move it. Authors use the createdCallback like a | |
| 72 // constructor. By not moving it, the createdCallback | |
| 73 // completes before any other callbacks are entered for this | |
| 74 // element. | |
| 75 return callbackQueue; | |
| 76 } | |
| 77 | |
| 78 if (CustomElementProcessingStack::inCallbackDeliveryScope()) { | |
| 79 // The processing stack is active. | |
| 80 CustomElementProcessingStack::instance().enqueue(&callbackQueue); | |
| 81 return callbackQueue; | |
| 82 } | |
| 83 | |
| 84 CustomElementMicrotaskDispatcher::instance().enqueue(&callbackQueue); | |
| 85 return callbackQueue; | |
| 86 } | |
| 87 | |
| 88 void CustomElementScheduler::scheduleCallback(CustomElementLifecycleCallbacks* c
allbacks, Element* element, CustomElementLifecycleCallbacks::CallbackType type) | |
| 89 { | |
| 90 DCHECK(type != CustomElementLifecycleCallbacks::AttributeChangedCallback); | |
| 91 | |
| 92 if (!callbacks->hasCallback(type)) | |
| 93 return; | |
| 94 | |
| 95 CustomElementCallbackQueue& queue = scheduleCallbackQueue(element); | |
| 96 queue.append(CustomElementCallbackInvocation::createInvocation(callbacks, ty
pe)); | |
| 97 } | |
| 98 | |
| 99 void CustomElementScheduler::scheduleAttributeChangedCallback(CustomElementLifec
ycleCallbacks* callbacks, Element* element, const AtomicString& name, const Atom
icString& oldValue, const AtomicString& newValue) | |
| 100 { | |
| 101 if (!callbacks->hasCallback(CustomElementLifecycleCallbacks::AttributeChange
dCallback)) | |
| 102 return; | |
| 103 | |
| 104 CustomElementCallbackQueue& queue = scheduleCallbackQueue(element); | |
| 105 queue.append(CustomElementCallbackInvocation::createAttributeChangedInvocati
on(callbacks, name, oldValue, newValue)); | |
| 106 } | |
| 107 | |
| 108 void CustomElementScheduler::resolveOrScheduleResolution(CustomElementRegistrati
onContext* context, Element* element, const CustomElementDescriptor& descriptor) | |
| 109 { | |
| 110 if (CustomElementProcessingStack::inCallbackDeliveryScope()) { | |
| 111 context->resolve(element, descriptor); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 Document& document = element->document(); | |
| 116 CustomElementMicrotaskResolutionStep* step = CustomElementMicrotaskResolutio
nStep::create(context, element, descriptor); | |
| 117 enqueueMicrotaskStep(document, step); | |
| 118 } | |
| 119 | |
| 120 CustomElementMicrotaskImportStep* CustomElementScheduler::scheduleImport(HTMLImp
ortChild* import) | |
| 121 { | |
| 122 DCHECK(!import->hasFinishedLoading()); | |
| 123 DCHECK(import->parent()); | |
| 124 | |
| 125 // Ownership of the new step is transferred to the parent | |
| 126 // processing step, or the base queue. | |
| 127 CustomElementMicrotaskImportStep* step = CustomElementMicrotaskImportStep::c
reate(import); | |
| 128 CustomElementMicrotaskImportStep* rawStep = step; | |
| 129 enqueueMicrotaskStep(*(import->parent()->document()), step, import->isSync()
); | |
| 130 return rawStep; | |
| 131 } | |
| 132 | |
| 133 void CustomElementScheduler::enqueueMicrotaskStep(Document& document, CustomElem
entMicrotaskStep* step, bool importIsSync) | |
| 134 { | |
| 135 Document& master = document.importsController() ? *(document.importsControll
er()->master()) : document; | |
| 136 master.customElementMicrotaskRunQueue()->enqueue(document.importLoader(), st
ep, importIsSync); | |
| 137 } | |
| 138 | |
| 139 | |
| 140 void CustomElementScheduler::callbackDispatcherDidFinish() | |
| 141 { | |
| 142 if (CustomElementMicrotaskDispatcher::instance().elementQueueIsEmpty()) | |
| 143 callbackQueues().clear(); | |
| 144 } | |
| 145 | |
| 146 void CustomElementScheduler::microtaskDispatcherDidFinish() | |
| 147 { | |
| 148 DCHECK(!CustomElementProcessingStack::inCallbackDeliveryScope()); | |
| 149 callbackQueues().clear(); | |
| 150 } | |
| 151 | |
| 152 } // namespace blink | |
| OLD | NEW |