OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "core/dom/custom/CustomElementMicrotaskDispatcher.h" | |
6 | |
7 #include "bindings/core/v8/Microtask.h" | |
8 #include "core/dom/custom/CustomElementCallbackQueue.h" | |
9 #include "core/dom/custom/CustomElementMicrotaskImportStep.h" | |
10 #include "core/dom/custom/CustomElementProcessingStack.h" | |
11 #include "core/dom/custom/CustomElementScheduler.h" | |
12 | |
13 namespace blink { | |
14 | |
15 static const CustomElementCallbackQueue::ElementQueueId kMicrotaskQueueId = 0; | |
16 | |
17 CustomElementMicrotaskDispatcher::CustomElementMicrotaskDispatcher() | |
18 : m_hasScheduledMicrotask(false) | |
19 , m_phase(Quiescent) | |
20 { | |
21 } | |
22 | |
23 CustomElementMicrotaskDispatcher& CustomElementMicrotaskDispatcher::instance() | |
24 { | |
25 DEFINE_STATIC_LOCAL(CustomElementMicrotaskDispatcher, instance, (new CustomE
lementMicrotaskDispatcher)); | |
26 return instance; | |
27 } | |
28 | |
29 void CustomElementMicrotaskDispatcher::enqueue(CustomElementCallbackQueue* queue
) | |
30 { | |
31 ensureMicrotaskScheduledForElementQueue(); | |
32 queue->setOwner(kMicrotaskQueueId); | |
33 m_elements.append(queue); | |
34 } | |
35 | |
36 void CustomElementMicrotaskDispatcher::ensureMicrotaskScheduledForElementQueue() | |
37 { | |
38 DCHECK(m_phase == Quiescent || m_phase == Resolving); | |
39 ensureMicrotaskScheduled(); | |
40 } | |
41 | |
42 void CustomElementMicrotaskDispatcher::ensureMicrotaskScheduled() | |
43 { | |
44 if (!m_hasScheduledMicrotask) { | |
45 Microtask::enqueueMicrotask(WTF::bind(&dispatch)); | |
46 m_hasScheduledMicrotask = true; | |
47 } | |
48 } | |
49 | |
50 void CustomElementMicrotaskDispatcher::dispatch() | |
51 { | |
52 instance().doDispatch(); | |
53 } | |
54 | |
55 void CustomElementMicrotaskDispatcher::doDispatch() | |
56 { | |
57 DCHECK(isMainThread()); | |
58 | |
59 DCHECK(m_phase == Quiescent); | |
60 DCHECK(m_hasScheduledMicrotask); | |
61 m_hasScheduledMicrotask = false; | |
62 | |
63 // Finishing microtask work deletes all | |
64 // CustomElementCallbackQueues. Being in a callback delivery scope | |
65 // implies those queues could still be in use. | |
66 ASSERT_WITH_SECURITY_IMPLICATION(!CustomElementProcessingStack::inCallbackDe
liveryScope()); | |
67 | |
68 m_phase = Resolving; | |
69 | |
70 m_phase = DispatchingCallbacks; | |
71 for (const auto& element : m_elements) { | |
72 // Created callback may enqueue an attached callback. | |
73 CustomElementProcessingStack::CallbackDeliveryScope scope; | |
74 element->processInElementQueue(kMicrotaskQueueId); | |
75 } | |
76 | |
77 m_elements.clear(); | |
78 CustomElementScheduler::microtaskDispatcherDidFinish(); | |
79 m_phase = Quiescent; | |
80 } | |
81 | |
82 DEFINE_TRACE(CustomElementMicrotaskDispatcher) | |
83 { | |
84 visitor->trace(m_elements); | |
85 } | |
86 | |
87 } // namespace blink | |
OLD | NEW |