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 "config.h" 32#include "core/dom/custom/CustomElementScheduler.h" 33 34#include "core/dom/Document.h" 35#include "core/dom/Element.h" 36#include "core/dom/custom/CustomElementCallbackInvocation.h" 37#include "core/dom/custom/CustomElementLifecycleCallbacks.h" 38#include "core/dom/custom/CustomElementMicrotaskDispatcher.h" 39#include "core/dom/custom/CustomElementMicrotaskImportStep.h" 40#include "core/dom/custom/CustomElementMicrotaskResolutionStep.h" 41#include "core/dom/custom/CustomElementMicrotaskRunQueue.h" 42#include "core/dom/custom/CustomElementProcessingStack.h" 43#include "core/dom/custom/CustomElementRegistrationContext.h" 44#include "core/dom/custom/CustomElementSyncMicrotaskQueue.h" 45#include "core/html/imports/HTMLImportChild.h" 46#include "core/html/imports/HTMLImportsController.h" 47 48namespace blink { 49 50DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CustomElementScheduler) 51 52// FIXME: Consider moving the element's callback queue to ElementRareData. 53typedef WillBeHeapHashMap<RawPtrWillBeMember<Element>, OwnPtrWillBeMember<CustomElementCallbackQueue> > ElementCallbackQueueMap; 54 55static ElementCallbackQueueMap& callbackQueues() 56{ 57 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<ElementCallbackQueueMap>, map, (adoptPtrWillBeNoop(new ElementCallbackQueueMap()))); 58 return *map; 59} 60 61static CustomElementCallbackQueue& ensureCallbackQueue(PassRefPtrWillBeRawPtr<Element> element) 62{ 63 ElementCallbackQueueMap::ValueType* it = callbackQueues().add(element.get(), nullptr).storedValue; 64 if (!it->value) 65 it->value = CustomElementCallbackQueue::create(element); 66 return *it->value.get(); 67} 68 69// Finds or creates the callback queue for element. 70static CustomElementCallbackQueue& scheduleCallbackQueue(PassRefPtrWillBeRawPtr<Element> passElement) 71{ 72 RefPtrWillBeRawPtr<Element> element(passElement); 73 74 CustomElementCallbackQueue& callbackQueue = ensureCallbackQueue(element); 75 if (callbackQueue.inCreatedCallback()) { 76 // Don't move it. Authors use the createdCallback like a 77 // constructor. By not moving it, the createdCallback 78 // completes before any other callbacks are entered for this 79 // element. 80 return callbackQueue; 81 } 82 83 if (CustomElementProcessingStack::inCallbackDeliveryScope()) { 84 // The processing stack is active. 85 CustomElementProcessingStack::instance().enqueue(&callbackQueue); 86 return callbackQueue; 87 } 88 89 CustomElementMicrotaskDispatcher::instance().enqueue(&callbackQueue); 90 return callbackQueue; 91} 92 93void CustomElementScheduler::scheduleCallback(PassRefPtr<CustomElementLifecycleCallbacks> callbacks, PassRefPtrWillBeRawPtr<Element> element, CustomElementLifecycleCallbacks::CallbackType type) 94{ 95 ASSERT(type != CustomElementLifecycleCallbacks::AttributeChangedCallback); 96 97 if (!callbacks->hasCallback(type)) 98 return; 99 100 CustomElementCallbackQueue& queue = scheduleCallbackQueue(element); 101 queue.append(CustomElementCallbackInvocation::createInvocation(callbacks, type)); 102} 103 104void CustomElementScheduler::scheduleAttributeChangedCallback(PassRefPtr<CustomElementLifecycleCallbacks> callbacks, PassRefPtrWillBeRawPtr<Element> element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue) 105{ 106 if (!callbacks->hasCallback(CustomElementLifecycleCallbacks::AttributeChangedCallback)) 107 return; 108 109 CustomElementCallbackQueue& queue = scheduleCallbackQueue(element); 110 queue.append(CustomElementCallbackInvocation::createAttributeChangedInvocation(callbacks, name, oldValue, newValue)); 111} 112 113void CustomElementScheduler::resolveOrScheduleResolution(PassRefPtrWillBeRawPtr<CustomElementRegistrationContext> context, PassRefPtrWillBeRawPtr<Element> element, const CustomElementDescriptor& descriptor) 114{ 115 if (CustomElementProcessingStack::inCallbackDeliveryScope()) { 116 context->resolve(element.get(), descriptor); 117 return; 118 } 119 120 Document& document = element->document(); 121 OwnPtrWillBeRawPtr<CustomElementMicrotaskResolutionStep> step = CustomElementMicrotaskResolutionStep::create(context, element, descriptor); 122 enqueueMicrotaskStep(document, step.release()); 123} 124 125CustomElementMicrotaskImportStep* CustomElementScheduler::scheduleImport(HTMLImportChild* import) 126{ 127 ASSERT(!import->isDone()); 128 ASSERT(import->parent()); 129 130 // Ownership of the new step is transferred to the parent 131 // processing step, or the base queue. 132 OwnPtrWillBeRawPtr<CustomElementMicrotaskImportStep> step = CustomElementMicrotaskImportStep::create(import); 133 CustomElementMicrotaskImportStep* rawStep = step.get(); 134 enqueueMicrotaskStep(*(import->parent()->document()), step.release(), import->isSync()); 135 return rawStep; 136} 137 138void CustomElementScheduler::enqueueMicrotaskStep(Document& document, PassOwnPtrWillBeRawPtr<CustomElementMicrotaskStep> step, bool importIsSync) 139{ 140 Document& master = document.importsController() ? *(document.importsController()->master()) : document; 141 master.customElementMicrotaskRunQueue()->enqueue(document.importLoader(), step, importIsSync); 142} 143 144 145void CustomElementScheduler::callbackDispatcherDidFinish() 146{ 147 if (CustomElementMicrotaskDispatcher::instance().elementQueueIsEmpty()) 148 callbackQueues().clear(); 149} 150 151void CustomElementScheduler::microtaskDispatcherDidFinish() 152{ 153 ASSERT(!CustomElementProcessingStack::inCallbackDeliveryScope()); 154 callbackQueues().clear(); 155} 156 157} // namespace blink 158