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 MutationObserverSet& active = activeMutationObservers(); | |
adamk
2014/01/29 22:58:24
Nit: No need to save this into a local, you can ju
rafaelw
2014/01/29 23:18:52
Done.
| |
189 if (active.isEmpty()) | |
190 Microtask::enqueueMicrotask(&MutationObserver::deliverMutations); | |
191 | |
192 activeMutationObservers().add(observer); | |
193 } | |
194 | |
185 void MutationObserver::enqueueMutationRecord(PassRefPtr<MutationRecord> mutation ) | 195 void MutationObserver::enqueueMutationRecord(PassRefPtr<MutationRecord> mutation ) |
186 { | 196 { |
187 ASSERT(isMainThread()); | 197 ASSERT(isMainThread()); |
188 m_records.append(mutation); | 198 m_records.append(mutation); |
189 activeMutationObservers().add(this); | 199 activateObserver(this); |
190 InspectorInstrumentation::didEnqueueMutationRecord(m_callback->executionCont ext(), this); | 200 InspectorInstrumentation::didEnqueueMutationRecord(m_callback->executionCont ext(), this); |
191 } | 201 } |
192 | 202 |
193 void MutationObserver::setHasTransientRegistration() | 203 void MutationObserver::setHasTransientRegistration() |
194 { | 204 { |
195 ASSERT(isMainThread()); | 205 ASSERT(isMainThread()); |
196 activeMutationObservers().add(this); | 206 activateObserver(this); |
197 } | 207 } |
198 | 208 |
199 HashSet<Node*> MutationObserver::getObservedNodes() const | 209 HashSet<Node*> MutationObserver::getObservedNodes() const |
200 { | 210 { |
201 HashSet<Node*> observedNodes; | 211 HashSet<Node*> observedNodes; |
202 for (HashSet<MutationObserverRegistration*>::const_iterator iter = m_registr ations.begin(); iter != m_registrations.end(); ++iter) | 212 for (HashSet<MutationObserverRegistration*>::const_iterator iter = m_registr ations.begin(); iter != m_registrations.end(); ++iter) |
203 (*iter)->addRegistrationNodesToSet(observedNodes); | 213 (*iter)->addRegistrationNodesToSet(observedNodes); |
204 return observedNodes; | 214 return observedNodes; |
205 } | 215 } |
206 | 216 |
(...skipping 20 matching lines...) Expand all Loading... | |
227 return; | 237 return; |
228 | 238 |
229 Vector<RefPtr<MutationRecord> > records; | 239 Vector<RefPtr<MutationRecord> > records; |
230 records.swap(m_records); | 240 records.swap(m_records); |
231 | 241 |
232 InspectorInstrumentation::willDeliverMutationRecords(m_callback->executionCo ntext(), this); | 242 InspectorInstrumentation::willDeliverMutationRecords(m_callback->executionCo ntext(), this); |
233 m_callback->call(records, this); | 243 m_callback->call(records, this); |
234 InspectorInstrumentation::didDeliverMutationRecords(m_callback->executionCon text()); | 244 InspectorInstrumentation::didDeliverMutationRecords(m_callback->executionCon text()); |
235 } | 245 } |
236 | 246 |
237 void MutationObserver::deliverAllMutations() | 247 void MutationObserver::deliverMutations() |
238 { | 248 { |
239 ASSERT(isMainThread()); | 249 ASSERT(isMainThread()); |
240 static bool deliveryInProgress = false; | 250 static bool deliveryInProgress = false; |
adamk
2014/01/29 22:58:24
Can you remove this bool while you're at it?
rafaelw
2014/01/29 23:18:52
Sorry. Yes. Had this is my prototype, but failed t
| |
241 if (deliveryInProgress) | 251 if (deliveryInProgress) |
242 return; | 252 return; |
243 deliveryInProgress = true; | 253 deliveryInProgress = true; |
244 | 254 |
245 if (!suspendedMutationObservers().isEmpty()) { | 255 if (!suspendedMutationObservers().isEmpty()) { |
246 Vector<RefPtr<MutationObserver> > suspended; | 256 Vector<RefPtr<MutationObserver> > suspended; |
247 copyToVector(suspendedMutationObservers(), suspended); | 257 copyToVector(suspendedMutationObservers(), suspended); |
248 for (size_t i = 0; i < suspended.size(); ++i) { | 258 for (size_t i = 0; i < suspended.size(); ++i) { |
249 if (!suspended[i]->canDeliver()) | 259 if (!suspended[i]->canDeliver()) |
250 continue; | 260 continue; |
251 | 261 |
252 suspendedMutationObservers().remove(suspended[i]); | 262 suspendedMutationObservers().remove(suspended[i]); |
253 activeMutationObservers().add(suspended[i]); | 263 activateObserver(suspended[i]); |
254 } | 264 } |
255 } | 265 } |
256 | 266 |
257 while (!activeMutationObservers().isEmpty()) { | 267 Vector<RefPtr<MutationObserver> > observers; |
258 Vector<RefPtr<MutationObserver> > observers; | 268 copyToVector(activeMutationObservers(), observers); |
259 copyToVector(activeMutationObservers(), observers); | 269 activeMutationObservers().clear(); |
260 activeMutationObservers().clear(); | 270 std::sort(observers.begin(), observers.end(), ObserverLessThan()); |
261 std::sort(observers.begin(), observers.end(), ObserverLessThan()); | 271 for (size_t i = 0; i < observers.size(); ++i) { |
262 for (size_t i = 0; i < observers.size(); ++i) { | 272 if (observers[i]->canDeliver()) |
263 if (observers[i]->canDeliver()) | 273 observers[i]->deliver(); |
264 observers[i]->deliver(); | 274 else |
265 else | 275 suspendedMutationObservers().add(observers[i]); |
266 suspendedMutationObservers().add(observers[i]); | |
267 } | |
268 } | 276 } |
269 | 277 |
270 deliveryInProgress = false; | 278 deliveryInProgress = false; |
271 } | 279 } |
272 | 280 |
273 } // namespace WebCore | 281 } // namespace WebCore |
OLD | NEW |