Chromium Code Reviews| Index: third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp |
| diff --git a/third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp b/third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f18b7ff425c9e9583ac744d8ba72c787429379a1 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp |
| @@ -0,0 +1,95 @@ |
| +// 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 "core/dom/IntersectionObserverController.h" |
| + |
| +#include "core/dom/Document.h" |
| + |
| +namespace blink { |
| + |
| +typedef HeapVector<Member<IntersectionObserver>> IntersectionObserverVector; |
| + |
| +IntersectionObserverController::IntersectionObserverController() |
| + : m_timer(this, &IntersectionObserverController::deliverIntersectionObservations) |
| +{ |
| +} |
| + |
| +IntersectionObserverController::~IntersectionObserverController() { } |
| + |
| +void IntersectionObserverController::scheduleIntersectionObserverForDelivery(IntersectionObserver& observer) |
| +{ |
| + // TODO(szager): use idle callback with a timeout |
| + if (m_activeIntersectionObservers.isEmpty()) |
| + m_timer.startOneShot(0, BLINK_FROM_HERE); |
|
haraken
2016/01/02 13:47:41
It's better to check m_timer.isActive() before sta
szager1
2016/01/02 19:18:34
Done.
|
| + m_activeIntersectionObservers.add(&observer); |
| +} |
| + |
| +void IntersectionObserverController::resumeSuspendedIntersectionObservers() |
| +{ |
| + ASSERT(isMainThread()); |
| + if (m_suspendedIntersectionObservers.isEmpty()) |
| + return; |
| + |
| + IntersectionObserverVector suspended; |
| + copyToVector(m_suspendedIntersectionObservers, suspended); |
| + for (auto& observer : suspended) { |
| + if (!observer->shouldBeSuspended()) { |
| + m_suspendedIntersectionObservers.remove(observer); |
| + scheduleIntersectionObserverForDelivery(*observer); |
| + } |
| + } |
| +} |
| + |
| +void IntersectionObserverController::deliverIntersectionObservations(Timer<IntersectionObserverController>*) |
| +{ |
| + IntersectionObserverVector observers; |
| + copyToVector(m_activeIntersectionObservers, observers); |
| + m_activeIntersectionObservers.clear(); |
| + for (auto& observer : observers) { |
| + if (observer->shouldBeSuspended()) |
| + m_suspendedIntersectionObservers.add(observer); |
| + else |
| + observer->deliver(); |
| + } |
| +} |
| + |
| +void IntersectionObserverController::computeTrackedIntersectionObservations() |
| +{ |
| + // TODO(szager): Need to define timestamp. |
| + double timestamp = currentTime(); |
| + for (auto& observer : m_trackedIntersectionObservers) |
| + observer->computeIntersectionObservations(timestamp); |
| +} |
| + |
| +void IntersectionObserverController::addTrackedObserver(IntersectionObserver& observer) |
| +{ |
| + m_trackedIntersectionObservers.add(&observer); |
| +} |
| + |
| +void IntersectionObserverController::removeTrackedObserversForRoot(const Element& root) |
| +{ |
| + HeapVector<Member<IntersectionObserver>> toRemove; |
| + for (auto& observer : m_trackedIntersectionObservers) { |
| + if (observer->root() == &root) |
| + toRemove.append(observer); |
| + } |
| + m_trackedIntersectionObservers.removeAll(toRemove); |
| +} |
| + |
| +void IntersectionObserverController::dispose() |
|
haraken
2016/01/02 13:47:41
This dispose() shouldn't be necessary even in non-
szager1
2016/01/02 19:18:34
Done.
|
| +{ |
| + m_timer.stop(); |
| + m_trackedIntersectionObservers.clear(); |
| + m_activeIntersectionObservers.clear(); |
| + m_suspendedIntersectionObservers.clear(); |
| +} |
| + |
| +DEFINE_TRACE(IntersectionObserverController) |
| +{ |
| + visitor->trace(m_trackedIntersectionObservers); |
| + visitor->trace(m_activeIntersectionObservers); |
| + visitor->trace(m_suspendedIntersectionObservers); |
| +} |
| + |
| +} // namespace blink |