OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "core/dom/custom/CustomElementMicrotaskDispatcher.h" | 6 #include "core/dom/custom/CustomElementMicrotaskDispatcher.h" |
7 | 7 |
8 #include "core/dom/Microtask.h" | 8 #include "core/dom/Microtask.h" |
| 9 #include "core/dom/custom/CustomElementAsyncImportMicrotaskQueue.h" |
9 #include "core/dom/custom/CustomElementCallbackDispatcher.h" | 10 #include "core/dom/custom/CustomElementCallbackDispatcher.h" |
10 #include "core/dom/custom/CustomElementCallbackQueue.h" | 11 #include "core/dom/custom/CustomElementCallbackQueue.h" |
11 #include "core/dom/custom/CustomElementMicrotaskImportStep.h" | 12 #include "core/dom/custom/CustomElementMicrotaskImportStep.h" |
12 #include "core/dom/custom/CustomElementMicrotaskQueue.h" | 13 #include "core/dom/custom/CustomElementMicrotaskQueue.h" |
13 #include "core/dom/custom/CustomElementScheduler.h" | 14 #include "core/dom/custom/CustomElementScheduler.h" |
14 #include "core/html/imports/HTMLImportLoader.h" | 15 #include "core/html/imports/HTMLImportLoader.h" |
15 #include "wtf/MainThread.h" | 16 #include "wtf/MainThread.h" |
16 | 17 |
17 namespace WebCore { | 18 namespace WebCore { |
18 | 19 |
19 static const CustomElementCallbackQueue::ElementQueueId kMicrotaskQueueId = 0; | 20 static const CustomElementCallbackQueue::ElementQueueId kMicrotaskQueueId = 0; |
20 | 21 |
21 CustomElementMicrotaskDispatcher::CustomElementMicrotaskDispatcher() | 22 CustomElementMicrotaskDispatcher::CustomElementMicrotaskDispatcher() |
22 : m_hasScheduledMicrotask(false) | 23 : m_hasScheduledMicrotask(false) |
23 , m_phase(Quiescent) | 24 , m_phase(Quiescent) |
24 , m_resolutionAndImports(CustomElementMicrotaskQueue::create()) | 25 , m_resolutionAndImports(CustomElementMicrotaskQueue::create()) |
| 26 , m_asyncImports(CustomElementAsyncImportMicrotaskQueue::create()) |
25 { | 27 { |
26 } | 28 } |
27 | 29 |
28 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CustomElementMicrotaskDispatcher) | 30 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CustomElementMicrotaskDispatcher) |
29 | 31 |
30 CustomElementMicrotaskDispatcher& CustomElementMicrotaskDispatcher::instance() | 32 CustomElementMicrotaskDispatcher& CustomElementMicrotaskDispatcher::instance() |
31 { | 33 { |
32 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<CustomElementMicrotaskDispatcher>
, instance, (adoptPtrWillBeNoop(new CustomElementMicrotaskDispatcher()))); | 34 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<CustomElementMicrotaskDispatcher>
, instance, (adoptPtrWillBeNoop(new CustomElementMicrotaskDispatcher()))); |
33 return *instance; | 35 return *instance; |
34 } | 36 } |
35 | 37 |
36 void CustomElementMicrotaskDispatcher::enqueue(HTMLImportLoader* importLoader, P
assOwnPtrWillBeRawPtr<CustomElementMicrotaskStep> step) | 38 void CustomElementMicrotaskDispatcher::enqueue(HTMLImportLoader* parentLoader, P
assOwnPtrWillBeRawPtr<CustomElementMicrotaskStep> step) |
37 { | 39 { |
38 ASSERT(m_phase == Quiescent || m_phase == DispatchingCallbacks); | 40 ensureMicrotaskScheduledForMicrotaskSteps(); |
39 ensureMicrotaskScheduled(); | 41 if (parentLoader) |
40 if (importLoader) | 42 parentLoader->microtaskQueue()->enqueue(step); |
41 importLoader->microtaskQueue()->enqueue(step); | |
42 else | 43 else |
43 m_resolutionAndImports->enqueue(step); | 44 m_resolutionAndImports->enqueue(step); |
44 } | 45 } |
45 | 46 |
| 47 void CustomElementMicrotaskDispatcher::enqueue(HTMLImportLoader* parentLoader, P
assOwnPtr<CustomElementMicrotaskImportStep> step, bool importIsSync) |
| 48 { |
| 49 ensureMicrotaskScheduledForMicrotaskSteps(); |
| 50 if (importIsSync) |
| 51 enqueue(parentLoader, PassOwnPtr<CustomElementMicrotaskStep>(step)); |
| 52 else |
| 53 m_asyncImports->enqueue(step); |
| 54 } |
| 55 |
46 void CustomElementMicrotaskDispatcher::enqueue(CustomElementCallbackQueue* queue
) | 56 void CustomElementMicrotaskDispatcher::enqueue(CustomElementCallbackQueue* queue
) |
47 { | 57 { |
48 ASSERT(m_phase == Quiescent || m_phase == Resolving); | 58 ensureMicrotaskScheduledForElementQueue(); |
49 ensureMicrotaskScheduled(); | |
50 queue->setOwner(kMicrotaskQueueId); | 59 queue->setOwner(kMicrotaskQueueId); |
51 m_elements.append(queue); | 60 m_elements.append(queue); |
52 } | 61 } |
53 | 62 |
54 void CustomElementMicrotaskDispatcher::importDidFinish(CustomElementMicrotaskImp
ortStep* step) | 63 void CustomElementMicrotaskDispatcher::importDidFinish(CustomElementMicrotaskImp
ortStep* step) |
55 { | 64 { |
| 65 ensureMicrotaskScheduledForMicrotaskSteps(); |
| 66 } |
| 67 |
| 68 void CustomElementMicrotaskDispatcher::ensureMicrotaskScheduledForMicrotaskSteps
() |
| 69 { |
56 ASSERT(m_phase == Quiescent || m_phase == DispatchingCallbacks); | 70 ASSERT(m_phase == Quiescent || m_phase == DispatchingCallbacks); |
57 ensureMicrotaskScheduled(); | 71 ensureMicrotaskScheduled(); |
58 } | 72 } |
59 | 73 |
| 74 void CustomElementMicrotaskDispatcher::ensureMicrotaskScheduledForElementQueue() |
| 75 { |
| 76 ASSERT(m_phase == Quiescent || m_phase == Resolving); |
| 77 ensureMicrotaskScheduled(); |
| 78 } |
| 79 |
60 void CustomElementMicrotaskDispatcher::ensureMicrotaskScheduled() | 80 void CustomElementMicrotaskDispatcher::ensureMicrotaskScheduled() |
61 { | 81 { |
62 if (!m_hasScheduledMicrotask) { | 82 if (!m_hasScheduledMicrotask) { |
63 Microtask::enqueueMicrotask(WTF::bind(&dispatch)); | 83 Microtask::enqueueMicrotask(WTF::bind(&dispatch)); |
64 m_hasScheduledMicrotask = true; | 84 m_hasScheduledMicrotask = true; |
65 } | 85 } |
66 } | 86 } |
67 | 87 |
68 void CustomElementMicrotaskDispatcher::dispatch() | 88 void CustomElementMicrotaskDispatcher::dispatch() |
69 { | 89 { |
70 instance().doDispatch(); | 90 instance().doDispatch(); |
71 } | 91 } |
72 | 92 |
73 void CustomElementMicrotaskDispatcher::doDispatch() | 93 void CustomElementMicrotaskDispatcher::doDispatch() |
74 { | 94 { |
75 ASSERT(isMainThread()); | 95 ASSERT(isMainThread()); |
76 | 96 |
77 ASSERT(m_phase == Quiescent && m_hasScheduledMicrotask); | 97 ASSERT(m_phase == Quiescent && m_hasScheduledMicrotask); |
78 m_hasScheduledMicrotask = false; | 98 m_hasScheduledMicrotask = false; |
79 | 99 |
80 // Finishing microtask work deletes all | 100 // Finishing microtask work deletes all |
81 // CustomElementCallbackQueues. Being in a callback delivery scope | 101 // CustomElementCallbackQueues. Being in a callback delivery scope |
82 // implies those queues could still be in use. | 102 // implies those queues could still be in use. |
83 ASSERT_WITH_SECURITY_IMPLICATION(!CustomElementCallbackDispatcher::inCallbac
kDeliveryScope()); | 103 ASSERT_WITH_SECURITY_IMPLICATION(!CustomElementCallbackDispatcher::inCallbac
kDeliveryScope()); |
84 | 104 |
85 m_phase = Resolving; | 105 m_phase = Resolving; |
86 m_resolutionAndImports->dispatch(); | 106 m_resolutionAndImports->dispatch(); |
| 107 if (m_resolutionAndImports->isEmpty()) |
| 108 m_asyncImports->dispatch(); |
87 | 109 |
88 m_phase = DispatchingCallbacks; | 110 m_phase = DispatchingCallbacks; |
89 for (WillBeHeapVector<RawPtrWillBeMember<CustomElementCallbackQueue> >::iter
ator it = m_elements.begin(); it != m_elements.end(); ++it) { | 111 for (WillBeHeapVector<RawPtrWillBeMember<CustomElementCallbackQueue> >::iter
ator it = m_elements.begin(); it != m_elements.end(); ++it) { |
90 // Created callback may enqueue an attached callback. | 112 // Created callback may enqueue an attached callback. |
91 CustomElementCallbackDispatcher::CallbackDeliveryScope scope; | 113 CustomElementCallbackDispatcher::CallbackDeliveryScope scope; |
92 (*it)->processInElementQueue(kMicrotaskQueueId); | 114 (*it)->processInElementQueue(kMicrotaskQueueId); |
93 } | 115 } |
94 | 116 |
95 m_elements.clear(); | 117 m_elements.clear(); |
96 CustomElementScheduler::microtaskDispatcherDidFinish(); | 118 CustomElementScheduler::microtaskDispatcherDidFinish(); |
97 m_phase = Quiescent; | 119 m_phase = Quiescent; |
98 } | 120 } |
99 | 121 |
100 void CustomElementMicrotaskDispatcher::trace(Visitor* visitor) | 122 void CustomElementMicrotaskDispatcher::trace(Visitor* visitor) |
101 { | 123 { |
102 visitor->trace(m_resolutionAndImports); | 124 visitor->trace(m_resolutionAndImports); |
103 #if ENABLE(OILPAN) | 125 #if ENABLE(OILPAN) |
104 visitor->trace(m_elements); | 126 visitor->trace(m_elements); |
105 #endif | 127 #endif |
106 } | 128 } |
107 | 129 |
108 #if !defined(NDEBUG) | 130 #if !defined(NDEBUG) |
109 void CustomElementMicrotaskDispatcher::show() | 131 void CustomElementMicrotaskDispatcher::show() |
110 { | 132 { |
111 fprintf(stderr, "Dispatcher:\n"); | 133 fprintf(stderr, "Dispatcher:\n"); |
112 m_resolutionAndImports->show(1); | 134 fprintf(stderr, " Sync:\n"); |
| 135 m_resolutionAndImports->show(3); |
| 136 fprintf(stderr, " Async:\n"); |
| 137 m_asyncImports->show(3); |
| 138 |
113 } | 139 } |
114 #endif | 140 #endif |
115 | 141 |
116 } // namespace WebCore | 142 } // namespace WebCore |
117 | 143 |
118 #if !defined(NDEBUG) | 144 #if !defined(NDEBUG) |
119 void showCEMD() | 145 void showCEMD() |
120 { | 146 { |
121 WebCore::CustomElementMicrotaskDispatcher::instance().show(); | 147 WebCore::CustomElementMicrotaskDispatcher::instance().show(); |
122 } | 148 } |
123 #endif | 149 #endif |
OLD | NEW |