OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "config.h" |
| 6 #include "core/dom/IntersectionObserverRegistry.h" |
| 7 |
| 8 #include "core/dom/Document.h" |
| 9 |
| 10 namespace blink { |
| 11 |
| 12 typedef HeapVector<Member<IntersectionObserver>> IntersectionObserverVector; |
| 13 |
| 14 IntersectionObserverRegistry::IntersectionObserverRegistry(Timer<Document>& time
r) |
| 15 : m_timer(timer) |
| 16 , m_trackedIntersectionObservers(new IntersectionObserver::WeakHashSet()) |
| 17 , m_activeIntersectionObservers(new IntersectionObserver::HashSet()) |
| 18 , m_suspendedIntersectionObservers(new IntersectionObserver::HashSet()) |
| 19 { |
| 20 } |
| 21 |
| 22 void IntersectionObserverRegistry::scheduleIntersectionObserverForDelivery(Inter
sectionObserver& observer) |
| 23 { |
| 24 if (m_activeIntersectionObservers->isEmpty()) |
| 25 m_timer.startOneShot(0, BLINK_FROM_HERE); |
| 26 m_activeIntersectionObservers->add(&observer); |
| 27 } |
| 28 |
| 29 void IntersectionObserverRegistry::resumeSuspendedIntersectionObservers() |
| 30 { |
| 31 ASSERT(isMainThread()); |
| 32 if (m_suspendedIntersectionObservers->isEmpty()) |
| 33 return; |
| 34 |
| 35 IntersectionObserverVector suspended; |
| 36 copyToVector(*m_suspendedIntersectionObservers, suspended); |
| 37 for (size_t i = 0; i < suspended.size(); ++i) { |
| 38 if (!suspended[i]->shouldBeSuspended()) { |
| 39 m_suspendedIntersectionObservers->remove(suspended[i]); |
| 40 scheduleIntersectionObserverForDelivery(*suspended[i]); |
| 41 } |
| 42 } |
| 43 } |
| 44 |
| 45 void IntersectionObserverRegistry::deliverIntersectionObservations() |
| 46 { |
| 47 IntersectionObserverVector observers; |
| 48 copyToVector(*m_activeIntersectionObservers, observers); |
| 49 m_activeIntersectionObservers->clear(); |
| 50 for (size_t i = 0; i < observers.size(); ++i) { |
| 51 if (observers[i]->shouldBeSuspended()) |
| 52 m_suspendedIntersectionObservers->add(observers[i]); |
| 53 else |
| 54 observers[i]->deliver(); |
| 55 } |
| 56 } |
| 57 |
| 58 void IntersectionObserverRegistry::computeTrackedIntersectionObservations() |
| 59 { |
| 60 // TODO: Need to define timestamp. |
| 61 double timestamp = currentTime(); |
| 62 for (auto& observer: *m_trackedIntersectionObservers) |
| 63 observer->computeIntersectionObservations(timestamp); |
| 64 } |
| 65 |
| 66 void IntersectionObserverRegistry::addTrackedObserver(IntersectionObserver& obse
rver) |
| 67 { |
| 68 m_trackedIntersectionObservers->add(&observer); |
| 69 } |
| 70 |
| 71 void IntersectionObserverRegistry::removeTrackedObserversForRoot(Element* root) |
| 72 { |
| 73 HeapVector<Member<IntersectionObserver>> toRemove; |
| 74 for (auto& observer: *m_trackedIntersectionObservers) { |
| 75 if (observer->root() == root) |
| 76 toRemove.append(observer); |
| 77 } |
| 78 for (auto& observer: toRemove) |
| 79 m_trackedIntersectionObservers->remove(observer); |
| 80 } |
| 81 |
| 82 DEFINE_TRACE(IntersectionObserverRegistry) |
| 83 { |
| 84 visitor->trace(m_trackedIntersectionObservers); |
| 85 visitor->trace(m_activeIntersectionObservers); |
| 86 visitor->trace(m_suspendedIntersectionObservers); |
| 87 } |
| 88 |
| 89 } // namespace blink { |
OLD | NEW |