Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "core/dom/IntersectionObservation.h" | |
| 7 | |
| 8 #include "core/dom/IntersectionObserver.h" | |
| 9 #include "core/layout/LayoutObject.h" | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 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.
| |
| 14 : m_observer(observer) | |
| 15 , m_target(target->createWeakPtr()) | |
| 16 , m_lastVisibleRatio(0) | |
| 17 { | |
| 18 setActive(true); | |
| 19 target->addIntersectionObservation(*this); | |
| 20 } | |
| 21 | |
| 22 void IntersectionObservation::setActive(bool active) | |
| 23 { | |
| 24 m_active = active; | |
| 25 if (active) | |
| 26 m_canReportRootBounds = m_target->document().frame()->securityContext()- >securityOrigin()->canAccess(m_observer->root()->document().frame()->securityCon text()->securityOrigin()); | |
| 27 } | |
| 28 | |
| 29 void IntersectionObservation::computeIntersectionObservations(int timestamp) | |
| 30 { | |
| 31 // TODO: rootMargin is not yet supported; need to clarify how it interacts w ith | |
| 32 // ancestor clipping (https://github.com/slightlyoff/IntersectionObserver/is sues/70) | |
| 33 Element* targetElement = target(); | |
| 34 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
| |
| 35 return; | |
| 36 Element* rootElement = observer()->root(); | |
| 37 LayoutObject* targetLayoutObject = targetElement->layoutObject(); | |
| 38 // TODO: Support SVG | |
| 39 if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObj ect->isInline())) | |
| 40 return; | |
| 41 | |
| 42 LayoutRect targetRect, rootRect, intersectionRect; | |
| 43 if (!targetElement->computeIntersection(rootElement, targetRect, rootRect, i ntersectionRect)) | |
| 44 return; | |
| 45 if (!m_canReportRootBounds) | |
| 46 rootRect = LayoutRect(); | |
| 47 | |
| 48 float intersectionArea = intersectionRect.size().width().toFloat() * interse ctionRect.size().height().toFloat(); | |
| 49 float targetArea = targetRect.size().width().toFloat() * targetRect.size().h eight().toFloat(); | |
| 50 if (!targetArea) | |
| 51 return; | |
| 52 float newVisibleRatio = intersectionArea / targetArea; | |
| 53 size_t newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleR atio); | |
| 54 size_t oldThresholdIndex = observer()->firstThresholdGreaterThan(lastVisible Ratio()); | |
| 55 setLastVisibleRatio(newVisibleRatio); | |
| 56 if (oldThresholdIndex != newThresholdIndex) | |
| 57 observer()->enqueueIntersectionObserverEntry(new IntersectionObserverEnt ry(timestamp / 1000., pixelSnappedIntRect(targetRect), pixelSnappedIntRect(rootR ect), pixelSnappedIntRect(intersectionRect), targetElement)); | |
| 58 } | |
| 59 | |
| 60 void IntersectionObservation::disconnect() | |
| 61 { | |
| 62 observer()->disconnect(*this); | |
| 63 if (m_target) | |
| 64 m_target->removeIntersectionObservation(*this); | |
| 65 } | |
| 66 | |
| 67 DEFINE_TRACE(IntersectionObservation) | |
| 68 { | |
| 69 visitor->trace(m_observer); | |
| 70 visitor->trace(m_target); | |
| 71 } | |
| 72 | |
| 73 } // namespace blink { | |
| OLD | NEW |