OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 18 matching lines...) Expand all Loading... |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "core/dom/MutationObserver.h" | 32 #include "core/dom/MutationObserver.h" |
33 | 33 |
34 #include <algorithm> | 34 #include <algorithm> |
35 #include "bindings/v8/Dictionary.h" | 35 #include "bindings/v8/Dictionary.h" |
36 #include "bindings/v8/ExceptionState.h" | 36 #include "bindings/v8/ExceptionState.h" |
37 #include "core/dom/Document.h" | 37 #include "core/dom/Document.h" |
38 #include "core/dom/ExceptionCode.h" | 38 #include "core/dom/ExceptionCode.h" |
| 39 #include "core/dom/Microtask.h" |
39 #include "core/dom/MutationCallback.h" | 40 #include "core/dom/MutationCallback.h" |
40 #include "core/dom/MutationObserverRegistration.h" | 41 #include "core/dom/MutationObserverRegistration.h" |
41 #include "core/dom/MutationRecord.h" | 42 #include "core/dom/MutationRecord.h" |
42 #include "core/dom/Node.h" | 43 #include "core/dom/Node.h" |
43 #include "core/inspector/InspectorInstrumentation.h" | 44 #include "core/inspector/InspectorInstrumentation.h" |
44 #include "wtf/MainThread.h" | 45 #include "wtf/MainThread.h" |
45 | 46 |
46 namespace WebCore { | 47 namespace WebCore { |
47 | 48 |
48 static unsigned s_observerPriority = 0; | 49 static unsigned s_observerPriority = 0; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 DEFINE_STATIC_LOCAL(MutationObserverSet, activeObservers, ()); | 176 DEFINE_STATIC_LOCAL(MutationObserverSet, activeObservers, ()); |
176 return activeObservers; | 177 return activeObservers; |
177 } | 178 } |
178 | 179 |
179 static MutationObserverSet& suspendedMutationObservers() | 180 static MutationObserverSet& suspendedMutationObservers() |
180 { | 181 { |
181 DEFINE_STATIC_LOCAL(MutationObserverSet, suspendedObservers, ()); | 182 DEFINE_STATIC_LOCAL(MutationObserverSet, suspendedObservers, ()); |
182 return suspendedObservers; | 183 return suspendedObservers; |
183 } | 184 } |
184 | 185 |
| 186 static void activateObserver(PassRefPtr<MutationObserver> observer) |
| 187 { |
| 188 if (activeMutationObservers().isEmpty()) |
| 189 Microtask::enqueueMicrotask(&MutationObserver::deliverMutations); |
| 190 |
| 191 activeMutationObservers().add(observer); |
| 192 } |
| 193 |
185 void MutationObserver::enqueueMutationRecord(PassRefPtr<MutationRecord> mutation
) | 194 void MutationObserver::enqueueMutationRecord(PassRefPtr<MutationRecord> mutation
) |
186 { | 195 { |
187 ASSERT(isMainThread()); | 196 ASSERT(isMainThread()); |
188 m_records.append(mutation); | 197 m_records.append(mutation); |
189 activeMutationObservers().add(this); | 198 activateObserver(this); |
190 InspectorInstrumentation::didEnqueueMutationRecord(m_callback->executionCont
ext(), this); | 199 InspectorInstrumentation::didEnqueueMutationRecord(m_callback->executionCont
ext(), this); |
191 } | 200 } |
192 | 201 |
193 void MutationObserver::setHasTransientRegistration() | 202 void MutationObserver::setHasTransientRegistration() |
194 { | 203 { |
195 ASSERT(isMainThread()); | 204 ASSERT(isMainThread()); |
196 activeMutationObservers().add(this); | 205 activateObserver(this); |
197 } | 206 } |
198 | 207 |
199 HashSet<Node*> MutationObserver::getObservedNodes() const | 208 HashSet<Node*> MutationObserver::getObservedNodes() const |
200 { | 209 { |
201 HashSet<Node*> observedNodes; | 210 HashSet<Node*> observedNodes; |
202 for (HashSet<MutationObserverRegistration*>::const_iterator iter = m_registr
ations.begin(); iter != m_registrations.end(); ++iter) | 211 for (HashSet<MutationObserverRegistration*>::const_iterator iter = m_registr
ations.begin(); iter != m_registrations.end(); ++iter) |
203 (*iter)->addRegistrationNodesToSet(observedNodes); | 212 (*iter)->addRegistrationNodesToSet(observedNodes); |
204 return observedNodes; | 213 return observedNodes; |
205 } | 214 } |
206 | 215 |
(...skipping 20 matching lines...) Expand all Loading... |
227 return; | 236 return; |
228 | 237 |
229 Vector<RefPtr<MutationRecord> > records; | 238 Vector<RefPtr<MutationRecord> > records; |
230 records.swap(m_records); | 239 records.swap(m_records); |
231 | 240 |
232 InspectorInstrumentation::willDeliverMutationRecords(m_callback->executionCo
ntext(), this); | 241 InspectorInstrumentation::willDeliverMutationRecords(m_callback->executionCo
ntext(), this); |
233 m_callback->call(records, this); | 242 m_callback->call(records, this); |
234 InspectorInstrumentation::didDeliverMutationRecords(m_callback->executionCon
text()); | 243 InspectorInstrumentation::didDeliverMutationRecords(m_callback->executionCon
text()); |
235 } | 244 } |
236 | 245 |
237 void MutationObserver::deliverAllMutations() | 246 void MutationObserver::resumeSuspendedObservers() |
238 { | 247 { |
239 ASSERT(isMainThread()); | 248 ASSERT(isMainThread()); |
240 static bool deliveryInProgress = false; | 249 if (suspendedMutationObservers().isEmpty()) |
241 if (deliveryInProgress) | |
242 return; | 250 return; |
243 deliveryInProgress = true; | |
244 | 251 |
245 if (!suspendedMutationObservers().isEmpty()) { | 252 Vector<RefPtr<MutationObserver> > suspended; |
246 Vector<RefPtr<MutationObserver> > suspended; | 253 copyToVector(suspendedMutationObservers(), suspended); |
247 copyToVector(suspendedMutationObservers(), suspended); | 254 for (size_t i = 0; i < suspended.size(); ++i) { |
248 for (size_t i = 0; i < suspended.size(); ++i) { | 255 if (suspended[i]->canDeliver()) { |
249 if (!suspended[i]->canDeliver()) | |
250 continue; | |
251 | |
252 suspendedMutationObservers().remove(suspended[i]); | 256 suspendedMutationObservers().remove(suspended[i]); |
253 activeMutationObservers().add(suspended[i]); | 257 activateObserver(suspended[i]); |
254 } | 258 } |
255 } | 259 } |
| 260 } |
256 | 261 |
257 while (!activeMutationObservers().isEmpty()) { | 262 void MutationObserver::deliverMutations() |
258 Vector<RefPtr<MutationObserver> > observers; | 263 { |
259 copyToVector(activeMutationObservers(), observers); | 264 ASSERT(isMainThread()); |
260 activeMutationObservers().clear(); | 265 Vector<RefPtr<MutationObserver> > observers; |
261 std::sort(observers.begin(), observers.end(), ObserverLessThan()); | 266 copyToVector(activeMutationObservers(), observers); |
262 for (size_t i = 0; i < observers.size(); ++i) { | 267 activeMutationObservers().clear(); |
263 if (observers[i]->canDeliver()) | 268 std::sort(observers.begin(), observers.end(), ObserverLessThan()); |
264 observers[i]->deliver(); | 269 for (size_t i = 0; i < observers.size(); ++i) { |
265 else | 270 if (observers[i]->canDeliver()) |
266 suspendedMutationObservers().add(observers[i]); | 271 observers[i]->deliver(); |
267 } | 272 else |
| 273 suspendedMutationObservers().add(observers[i]); |
268 } | 274 } |
269 | |
270 deliveryInProgress = false; | |
271 } | 275 } |
272 | 276 |
273 } // namespace WebCore | 277 } // namespace WebCore |
OLD | NEW |