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

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

Issue 1449623002: IntersectionObserver: second cut. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Fix rootMargin parsing 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/IntersectionObservation.cpp
diff --git a/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp b/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..a0b336b2622f3f2ebd50b499ab66ab4b292e0dac
--- /dev/null
+++ b/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp
@@ -0,0 +1,73 @@
+// 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/IntersectionObservation.h"
+
+#include "core/dom/IntersectionObserver.h"
+#include "core/layout/LayoutObject.h"
+
+namespace blink {
+
+IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, Element* target)
dcheng 2015/12/10 00:37:09 target can't be null here, so make it a reference
szager1 2015/12/10 22:38:54 That will be in the next patch.
+ : m_observer(observer)
+ , m_target(target->createWeakPtr())
+ , m_lastVisibleRatio(0)
+{
+ setActive(true);
+ target->addIntersectionObservation(*this);
+}
+
+void IntersectionObservation::setActive(bool active)
+{
+ m_active = active;
+ if (active)
+ m_canReportRootBounds = m_target->document().frame()->securityContext()->securityOrigin()->canAccess(m_observer->root()->document().frame()->securityContext()->securityOrigin());
+}
+
+void IntersectionObservation::computeIntersectionObservations(int timestamp)
+{
+ // TODO: rootMargin is not yet supported; need to clarify how it interacts with
+ // ancestor clipping (https://github.com/slightlyoff/IntersectionObserver/issues/70)
+ Element* targetElement = target();
+ if (!targetElement || !isActive())
dcheng 2015/12/10 00:37:09 I don't see how this observation is ever cleaned u
szager1 2015/12/10 22:38:54 The target element is the only thing that holds a
+ return;
+ Element* rootElement = observer()->root();
+ LayoutObject* targetLayoutObject = targetElement->layoutObject();
+ // TODO: Support SVG
+ if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObject->isInline()))
+ return;
+
+ LayoutRect targetRect, rootRect, intersectionRect;
+ if (!targetElement->computeIntersection(rootElement, targetRect, rootRect, intersectionRect))
+ return;
+ if (!m_canReportRootBounds)
+ rootRect = LayoutRect();
+
+ float intersectionArea = intersectionRect.size().width().toFloat() * intersectionRect.size().height().toFloat();
+ float targetArea = targetRect.size().width().toFloat() * targetRect.size().height().toFloat();
+ if (!targetArea)
+ return;
+ float newVisibleRatio = intersectionArea / targetArea;
+ size_t newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleRatio);
+ size_t oldThresholdIndex = observer()->firstThresholdGreaterThan(lastVisibleRatio());
+ setLastVisibleRatio(newVisibleRatio);
+ if (oldThresholdIndex != newThresholdIndex)
+ observer()->enqueueIntersectionObserverEntry(new IntersectionObserverEntry(timestamp / 1000., pixelSnappedIntRect(targetRect), pixelSnappedIntRect(rootRect), pixelSnappedIntRect(intersectionRect), targetElement));
+}
+
+void IntersectionObservation::disconnect()
+{
+ observer()->disconnect(*this);
+ if (m_target)
+ m_target->removeIntersectionObservation(*this);
+}
+
+DEFINE_TRACE(IntersectionObservation)
+{
+ visitor->trace(m_observer);
+ visitor->trace(m_target);
+}
+
+} // namespace blink {

Powered by Google App Engine
This is Rietveld 408576698