| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/dom/ElementVisibilityObserver.h" | 5 #include "core/dom/ElementVisibilityObserver.h" |
| 6 | 6 |
| 7 #include "core/dom/Element.h" | 7 #include "core/dom/Element.h" |
| 8 #include "core/dom/IntersectionObserverEntry.h" | 8 #include "core/dom/IntersectionObserverEntry.h" |
| 9 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 10 #include "wtf/Functional.h" | 10 #include "wtf/Functional.h" |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 bool isInRemoteFrame(const Document& document) { | 16 bool isInRemoteFrame(const Document& document) { |
| 17 DCHECK(document.frame()); | 17 DCHECK(document.frame()); |
| 18 Frame* mainFrame = document.frame()->tree().top(); | 18 Frame* mainFrame = document.frame()->tree().top(); |
| 19 return !mainFrame || mainFrame->isRemoteFrame(); | 19 return !mainFrame || mainFrame->isRemoteFrame(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 } // anonymous namespace | 22 } // anonymous namespace |
| 23 | 23 |
| 24 ElementVisibilityObserver::ElementVisibilityObserver( | 24 VisibilityObserverBase::VisibilityObserverBase(Element* element, |
| 25 Element* element, | 25 const Vector<float>& thresholds) |
| 26 std::unique_ptr<VisibilityCallback> callback) | 26 : m_element(element), m_thresholds(thresholds) {} |
| 27 : m_element(element), m_callback(std::move(callback)) {} | |
| 28 | 27 |
| 29 ElementVisibilityObserver::~ElementVisibilityObserver() = default; | 28 VisibilityObserverBase::~VisibilityObserverBase() {} |
| 30 | 29 |
| 31 void ElementVisibilityObserver::start() { | 30 void VisibilityObserverBase::start() { |
| 32 ExecutionContext* context = m_element->getExecutionContext(); | 31 ExecutionContext* context = m_element->getExecutionContext(); |
| 33 DCHECK(context->isDocument()); | 32 DCHECK(context->isDocument()); |
| 34 Document& document = toDocument(*context); | 33 Document& document = toDocument(*context); |
| 35 | 34 |
| 36 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame. | 35 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame. |
| 37 // Remove this early return when it's fixed. See https://crbug.com/615156 | 36 // Remove this early return when it's fixed. See https://crbug.com/615156 |
| 38 if (isInRemoteFrame(document)) { | 37 if (isInRemoteFrame(document)) { |
| 39 m_element.release(); | 38 m_element.release(); |
| 40 return; | 39 return; |
| 41 } | 40 } |
| 42 | 41 |
| 43 DCHECK(!m_intersectionObserver); | 42 DCHECK(!m_intersectionObserver); |
| 44 m_intersectionObserver = IntersectionObserver::create( | 43 m_intersectionObserver = IntersectionObserver::create( |
| 45 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), | 44 Vector<Length>(), m_thresholds, &document, |
| 46 &document, WTF::bind(&ElementVisibilityObserver::onVisibilityChanged, | 45 WTF::bind(&VisibilityObserverBase::onVisibilityChanged, |
| 47 wrapWeakPersistent(this))); | 46 wrapWeakPersistent(this))); |
| 48 DCHECK(m_intersectionObserver); | 47 DCHECK(m_intersectionObserver); |
| 49 m_intersectionObserver->setInitialState( | 48 m_intersectionObserver->setInitialState( |
| 50 IntersectionObserver::InitialState::kAuto); | 49 IntersectionObserver::InitialState::kAuto); |
| 51 m_intersectionObserver->observe(m_element.release()); | 50 m_intersectionObserver->observe(m_element.release()); |
| 52 } | 51 } |
| 53 | 52 |
| 54 void ElementVisibilityObserver::stop() { | 53 void VisibilityObserverBase::stop() { |
| 55 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, | 54 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, |
| 56 // so |m_intersectionObserver| may be null at this point after start(). | 55 // so |m_intersectionObserver| may be null at this point after start(). |
| 57 // Replace this early return with DCHECK when this has been fixed. See | 56 // Replace this early return with DCHECK when this has been fixed. See |
| 58 // https://crbug.com/615156 | 57 // https://crbug.com/615156 |
| 59 if (!m_intersectionObserver) | 58 if (!m_intersectionObserver) |
| 60 return; | 59 return; |
| 61 | 60 |
| 62 m_intersectionObserver->disconnect(); | 61 m_intersectionObserver->disconnect(); |
| 63 m_intersectionObserver = nullptr; | 62 m_intersectionObserver = nullptr; |
| 64 } | 63 } |
| 65 | 64 |
| 65 DEFINE_TRACE(VisibilityObserverBase) { |
| 66 visitor->trace(m_element); |
| 67 visitor->trace(m_intersectionObserver); |
| 68 } |
| 69 |
| 70 ElementVisibilityObserver::ElementVisibilityObserver( |
| 71 Element* element, |
| 72 std::unique_ptr<VisibilityCallback> callback) |
| 73 : VisibilityObserverBase( |
| 74 element, |
| 75 Vector<float>({std::numeric_limits<float>::min()})), |
| 76 m_callback(std::move(callback)) {} |
| 77 |
| 78 ElementVisibilityObserver::~ElementVisibilityObserver() {} |
| 79 |
| 66 void ElementVisibilityObserver::deliverObservationsForTesting() { | 80 void ElementVisibilityObserver::deliverObservationsForTesting() { |
| 67 m_intersectionObserver->deliver(); | 81 m_intersectionObserver->deliver(); |
| 68 } | 82 } |
| 69 | 83 |
| 70 DEFINE_TRACE(ElementVisibilityObserver) { | |
| 71 visitor->trace(m_element); | |
| 72 visitor->trace(m_intersectionObserver); | |
| 73 } | |
| 74 | |
| 75 void ElementVisibilityObserver::onVisibilityChanged( | 84 void ElementVisibilityObserver::onVisibilityChanged( |
| 76 const HeapVector<Member<IntersectionObserverEntry>>& entries) { | 85 const HeapVector<Member<IntersectionObserverEntry>>& entries) { |
| 77 bool isVisible = entries.last()->intersectionRatio() > 0.f; | 86 bool isVisible = entries.last()->intersectionRatio() > 0.f; |
| 78 (*m_callback.get())(isVisible); | 87 (*m_callback.get())(isVisible); |
| 79 } | 88 } |
| 80 | 89 |
| 90 ElementViewportIntersectionObserver::ElementViewportIntersectionObserver( |
| 91 Element* element, |
| 92 std::unique_ptr<RatioChangeCallback> callback) |
| 93 : VisibilityObserverBase(element, Vector<float>()), |
| 94 m_callback(std::move(callback)) {} |
| 95 |
| 96 ElementViewportIntersectionObserver::~ElementViewportIntersectionObserver() {} |
| 97 |
| 98 void ElementViewportIntersectionObserver::onVisibilityChanged( |
| 99 const HeapVector<Member<IntersectionObserverEntry>>& entries) { |
| 100 IntersectionObserverEntry* entry = entries.last(); |
| 101 WebRect rootRect; |
| 102 if (entry && entry->rootBounds()) { |
| 103 rootRect = |
| 104 WebRect(entry->rootBounds()->left(), entry->rootBounds()->top(), |
| 105 entry->rootBounds()->width(), entry->rootBounds()->height()); |
| 106 } |
| 107 WebRect intersectRect; |
| 108 if (entry && entry->intersectionRect()) { |
| 109 intersectRect = WebRect(entry->intersectionRect()->left(), |
| 110 entry->intersectionRect()->top(), |
| 111 entry->intersectionRect()->width(), |
| 112 entry->intersectionRect()->height()); |
| 113 } |
| 114 |
| 115 (*m_callback.get())(rootRect, intersectRect); |
| 116 } |
| 117 |
| 81 } // namespace blink | 118 } // namespace blink |
| OLD | NEW |