Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Unified Diff: third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp

Issue 1449623002: IntersectionObserver: second cut. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Added dispose() methods for expicit cleanup Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..2c665c39b49164296a50452ae21b6f3759e72fe4
--- /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 "config.h"
+#include "core/dom/IntersectionObserverController.h"
+
+#include "core/dom/Document.h"
+
+namespace blink {
+
+typedef HeapVector<Member<IntersectionObserver>> IntersectionObserverVector;
+
+IntersectionObserverController::IntersectionObserverController()
+ : m_timer(this, &IntersectionObserverController::deliverIntersectionObservations)
+{
+}
+
+void IntersectionObserverController::scheduleIntersectionObserverForDelivery(IntersectionObserver& observer)
+{
+ // TODO: use idle callback with a timeout
esprehn 2015/12/17 01:40:29 ditto for adding your name
szager1 2015/12/17 20:27:26 Done, here and elsewhere.
+ if (m_activeIntersectionObservers.isEmpty())
+ m_timer.startOneShot(0, BLINK_FROM_HERE);
+ 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: Need to define timestamp.
esprehn 2015/12/17 01:40:29 ditto
+ double timestamp = currentTime();
+ for (auto& observer : m_trackedIntersectionObservers)
+ observer->computeIntersectionObservations(timestamp);
+}
+
+void IntersectionObserverController::addTrackedObserver(IntersectionObserver& observer)
+{
+ m_trackedIntersectionObservers.add(&observer);
+}
+
+void IntersectionObserverController::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);
+}
+
+void IntersectionObserverController::dispose()
+{
+ 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 {

Powered by Google App Engine
This is Rietveld 408576698