1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "core/dom/custom/CustomElementMicrotaskDispatcher.h"
8 #include "core/dom/Microtask.h"
9 #include "core/dom/custom/CustomElementCallbackDispatcher.h"
10 #include "core/dom/custom/CustomElementCallbackQueue.h"
11 #include "core/dom/custom/CustomElementMicrotaskImportStep.h"
12 #include "core/dom/custom/CustomElementScheduler.h"
13 #include "wtf/MainThread.h"
17 static const CustomElementCallbackQueue::ElementQueueId kMicrotaskQueueId = 0;
19 CustomElementMicrotaskDispatcher::CustomElementMicrotaskDispatcher()
20 : m_hasScheduledMicrotask(false)
25 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CustomElementMicrotaskDispatcher)
27 CustomElementMicrotaskDispatcher& CustomElementMicrotaskDispatcher::instance()
29 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<CustomElementMicrotaskDispatcher>, instance, (adoptPtrWillBeNoop(new CustomElementMicrotaskDispatcher())));
33 void CustomElementMicrotaskDispatcher::enqueue(CustomElementCallbackQueue* queue)
35 ensureMicrotaskScheduledForElementQueue();
36 queue->setOwner(kMicrotaskQueueId);
37 m_elements.append(queue);
40 void CustomElementMicrotaskDispatcher::ensureMicrotaskScheduledForElementQueue()
42 ASSERT(m_phase == Quiescent || m_phase == Resolving);
43 ensureMicrotaskScheduled();
46 void CustomElementMicrotaskDispatcher::ensureMicrotaskScheduled()
48 if (!m_hasScheduledMicrotask) {
49 Microtask::enqueueMicrotask(WTF::bind(&dispatch));
50 m_hasScheduledMicrotask = true;
54 void CustomElementMicrotaskDispatcher::dispatch()
56 instance().doDispatch();
59 void CustomElementMicrotaskDispatcher::doDispatch()
61 ASSERT(isMainThread());
63 ASSERT(m_phase == Quiescent && m_hasScheduledMicrotask);
64 m_hasScheduledMicrotask = false;
66 // Finishing microtask work deletes all
67 // CustomElementCallbackQueues. Being in a callback delivery scope
68 // implies those queues could still be in use.
69 ASSERT_WITH_SECURITY_IMPLICATION(!CustomElementCallbackDispatcher::inCallbackDeliveryScope());
73 m_phase = DispatchingCallbacks;
74 for (WillBeHeapVector<RawPtrWillBeMember<CustomElementCallbackQueue> >::iterator it = m_elements.begin(); it != m_elements.end(); ++it) {
75 // Created callback may enqueue an attached callback.
76 CustomElementCallbackDispatcher::CallbackDeliveryScope scope;
77 (*it)->processInElementQueue(kMicrotaskQueueId);
81 CustomElementScheduler::microtaskDispatcherDidFinish();
85 void CustomElementMicrotaskDispatcher::trace(Visitor* visitor)
88 visitor->trace(m_elements);