Index: third_party/WebKit/Source/core/dom/IntersectionObservationRegistry.cpp |
diff --git a/third_party/WebKit/Source/core/dom/IntersectionObservationRegistry.cpp b/third_party/WebKit/Source/core/dom/IntersectionObservationRegistry.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aedab2765a66ccb5b1901ed10bad575d4914acd9 |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/dom/IntersectionObservationRegistry.cpp |
@@ -0,0 +1,83 @@ |
+// 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/IntersectionObservationRegistry.h" |
+ |
+#include "core/dom/Document.h" |
+ |
+namespace blink { |
+ |
+typedef WillBeHeapVector<RefPtrWillBeMember<IntersectionObserver>> IntersectionObserverVector; |
+ |
+IntersectionObservationRegistry::IntersectionObservationRegistry(Timer<Document>& timer) |
+ : m_timer(timer) |
+{ |
+} |
+ |
+IntersectionObservationRegistry::~IntersectionObservationRegistry() |
+{ |
+ resumeSuspendedIntersectionObservers(); |
+ deliverIntersectionObservations(); |
+ ASSERT(!m_suspendedIntersectionObservers.size()); |
+} |
+ |
+ |
+void IntersectionObservationRegistry::activateIntersectionObserver(IntersectionObserver& observer) |
+{ |
+ if (m_activeIntersectionObservers.isEmpty()) |
+ m_timer.startOneShot(0, BLINK_FROM_HERE); |
+ m_activeIntersectionObservers.add(&observer); |
+} |
+ |
+void IntersectionObservationRegistry::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]); |
+ activateIntersectionObserver(*suspended[i]); |
+ } |
+ } |
+} |
+ |
+void IntersectionObservationRegistry::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 IntersectionObservationRegistry::computeIntersectionObservations() |
+{ |
+ // TODO: Need to define timestamp. |
+ double timestamp = currentTime(); |
+ for (auto& observation: m_intersectionObservations) { |
+ ASSERT(observation->target()->isInTreeScope()); |
+ observation->computeIntersectionObservations(timestamp); |
+ } |
+} |
+ |
+void IntersectionObservationRegistry::addObservation(IntersectionObservation& observation) |
+{ |
+ m_intersectionObservations.add(adoptRef(&observation)); |
haraken
2015/11/16 00:41:34
adoptRefWillBeNoop ? Otherwise, it won't compile w
szager1
2015/11/19 19:15:38
Fixed in next patch (either with adoptRefWillBeNoo
|
+} |
+ |
+void IntersectionObservationRegistry::removeObservation(IntersectionObservation& observation) |
+{ |
+ m_intersectionObservations.remove(adoptRef(&observation)); |
+} |
+ |
+} // namespace blink { |