| Index: third_party/WebKit/Source/core/dom/IntersectionObserverRegistry.cpp
|
| diff --git a/third_party/WebKit/Source/core/dom/IntersectionObserverRegistry.cpp b/third_party/WebKit/Source/core/dom/IntersectionObserverRegistry.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..e59932412852e87aaf1f2a674de9a5af9e17b39d
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/core/dom/IntersectionObserverRegistry.cpp
|
| @@ -0,0 +1,89 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "config.h"
|
| +#include "core/dom/IntersectionObserverRegistry.h"
|
| +
|
| +#include "core/dom/Document.h"
|
| +
|
| +namespace blink {
|
| +
|
| +typedef HeapVector<Member<IntersectionObserver>> IntersectionObserverVector;
|
| +
|
| +IntersectionObserverRegistry::IntersectionObserverRegistry(Timer<Document>& timer)
|
| + : m_timer(timer)
|
| + , m_trackedIntersectionObservers(new IntersectionObserver::WeakHashSet())
|
| + , m_activeIntersectionObservers(new IntersectionObserver::HashSet())
|
| + , m_suspendedIntersectionObservers(new IntersectionObserver::HashSet())
|
| +{
|
| +}
|
| +
|
| +void IntersectionObserverRegistry::scheduleIntersectionObserverForDelivery(IntersectionObserver& observer)
|
| +{
|
| + if (m_activeIntersectionObservers->isEmpty())
|
| + m_timer.startOneShot(0, BLINK_FROM_HERE);
|
| + m_activeIntersectionObservers->add(&observer);
|
| +}
|
| +
|
| +void IntersectionObserverRegistry::resumeSuspendedIntersectionObservers()
|
| +{
|
| + ASSERT(isMainThread());
|
| + if (m_suspendedIntersectionObservers->isEmpty())
|
| + return;
|
| +
|
| + IntersectionObserverVector suspended;
|
| + copyToVector(*m_suspendedIntersectionObservers, suspended);
|
| + for (size_t i = 0; i < suspended.size(); ++i) {
|
| + if (!suspended[i]->shouldBeSuspended()) {
|
| + m_suspendedIntersectionObservers->remove(suspended[i]);
|
| + scheduleIntersectionObserverForDelivery(*suspended[i]);
|
| + }
|
| + }
|
| +}
|
| +
|
| +void IntersectionObserverRegistry::deliverIntersectionObservations()
|
| +{
|
| + IntersectionObserverVector observers;
|
| + copyToVector(*m_activeIntersectionObservers, observers);
|
| + m_activeIntersectionObservers->clear();
|
| + for (size_t i = 0; i < observers.size(); ++i) {
|
| + if (observers[i]->shouldBeSuspended())
|
| + m_suspendedIntersectionObservers->add(observers[i]);
|
| + else
|
| + observers[i]->deliver();
|
| + }
|
| +}
|
| +
|
| +void IntersectionObserverRegistry::computeTrackedIntersectionObservations()
|
| +{
|
| + // TODO: Need to define timestamp.
|
| + double timestamp = currentTime();
|
| + for (auto& observer: *m_trackedIntersectionObservers)
|
| + observer->computeIntersectionObservations(timestamp);
|
| +}
|
| +
|
| +void IntersectionObserverRegistry::addTrackedObserver(IntersectionObserver& observer)
|
| +{
|
| + m_trackedIntersectionObservers->add(&observer);
|
| +}
|
| +
|
| +void IntersectionObserverRegistry::removeTrackedObserversForRoot(Element* root)
|
| +{
|
| + HeapVector<Member<IntersectionObserver>> toRemove;
|
| + for (auto& observer: *m_trackedIntersectionObservers) {
|
| + if (observer->root() == root)
|
| + toRemove.append(observer);
|
| + }
|
| + for (auto& observer: toRemove)
|
| + m_trackedIntersectionObservers->remove(observer);
|
| +}
|
| +
|
| +DEFINE_TRACE(IntersectionObserverRegistry)
|
| +{
|
| + visitor->trace(m_trackedIntersectionObservers);
|
| + visitor->trace(m_activeIntersectionObservers);
|
| + visitor->trace(m_suspendedIntersectionObservers);
|
| +}
|
| +
|
| +} // namespace blink {
|
|
|