| 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 { | |
| 15 | |
| 16 bool isInRemoteFrame(const Document& document) { | |
| 17 DCHECK(document.frame()); | |
| 18 Frame* mainFrame = document.frame()->tree().top(); | |
| 19 return !mainFrame || mainFrame->isRemoteFrame(); | |
| 20 } | |
| 21 | |
| 22 } // anonymous namespace | |
| 23 | |
| 24 ElementVisibilityObserver::ElementVisibilityObserver( | 14 ElementVisibilityObserver::ElementVisibilityObserver( |
| 25 Element* element, | 15 Element* element, |
| 26 std::unique_ptr<VisibilityCallback> callback) | 16 std::unique_ptr<VisibilityCallback> callback) |
| 27 : m_element(element), m_callback(std::move(callback)) {} | 17 : m_element(element), m_callback(std::move(callback)) {} |
| 28 | 18 |
| 29 ElementVisibilityObserver::~ElementVisibilityObserver() = default; | 19 ElementVisibilityObserver::~ElementVisibilityObserver() = default; |
| 30 | 20 |
| 31 void ElementVisibilityObserver::start() { | 21 void ElementVisibilityObserver::start() { |
| 22 DCHECK(!m_intersectionObserver); |
| 23 |
| 32 ExecutionContext* context = m_element->getExecutionContext(); | 24 ExecutionContext* context = m_element->getExecutionContext(); |
| 33 DCHECK(context->isDocument()); | 25 DCHECK(context->isDocument()); |
| 34 Document& document = toDocument(*context); | 26 Document& document = toDocument(*context); |
| 35 | 27 |
| 36 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame. | |
| 37 // Remove this early return when it's fixed. See https://crbug.com/615156 | |
| 38 if (isInRemoteFrame(document)) { | |
| 39 m_element.release(); | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 DCHECK(!m_intersectionObserver); | |
| 44 m_intersectionObserver = IntersectionObserver::create( | 28 m_intersectionObserver = IntersectionObserver::create( |
| 45 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), | 29 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), |
| 46 &document, WTF::bind(&ElementVisibilityObserver::onVisibilityChanged, | 30 &document, WTF::bind(&ElementVisibilityObserver::onVisibilityChanged, |
| 47 wrapWeakPersistent(this))); | 31 wrapWeakPersistent(this))); |
| 48 DCHECK(m_intersectionObserver); | 32 DCHECK(m_intersectionObserver); |
| 33 |
| 49 m_intersectionObserver->setInitialState( | 34 m_intersectionObserver->setInitialState( |
| 50 IntersectionObserver::InitialState::kAuto); | 35 IntersectionObserver::InitialState::kAuto); |
| 51 m_intersectionObserver->observe(m_element.release()); | 36 m_intersectionObserver->observe(m_element.release()); |
| 52 } | 37 } |
| 53 | 38 |
| 54 void ElementVisibilityObserver::stop() { | 39 void ElementVisibilityObserver::stop() { |
| 55 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, | 40 DCHECK(m_intersectionObserver); |
| 56 // so |m_intersectionObserver| may be null at this point after start(). | |
| 57 // Replace this early return with DCHECK when this has been fixed. See | |
| 58 // https://crbug.com/615156 | |
| 59 if (!m_intersectionObserver) | |
| 60 return; | |
| 61 | 41 |
| 62 m_intersectionObserver->disconnect(); | 42 m_intersectionObserver->disconnect(); |
| 63 m_intersectionObserver = nullptr; | 43 m_intersectionObserver = nullptr; |
| 64 } | 44 } |
| 65 | 45 |
| 66 void ElementVisibilityObserver::deliverObservationsForTesting() { | 46 void ElementVisibilityObserver::deliverObservationsForTesting() { |
| 67 m_intersectionObserver->deliver(); | 47 m_intersectionObserver->deliver(); |
| 68 } | 48 } |
| 69 | 49 |
| 70 DEFINE_TRACE(ElementVisibilityObserver) { | 50 DEFINE_TRACE(ElementVisibilityObserver) { |
| 71 visitor->trace(m_element); | 51 visitor->trace(m_element); |
| 72 visitor->trace(m_intersectionObserver); | 52 visitor->trace(m_intersectionObserver); |
| 73 } | 53 } |
| 74 | 54 |
| 75 void ElementVisibilityObserver::onVisibilityChanged( | 55 void ElementVisibilityObserver::onVisibilityChanged( |
| 76 const HeapVector<Member<IntersectionObserverEntry>>& entries) { | 56 const HeapVector<Member<IntersectionObserverEntry>>& entries) { |
| 77 bool isVisible = entries.back()->intersectionRatio() > 0.f; | 57 bool isVisible = entries.back()->intersectionRatio() > 0.f; |
| 78 (*m_callback.get())(isVisible); | 58 (*m_callback.get())(isVisible); |
| 79 } | 59 } |
| 80 | 60 |
| 81 } // namespace blink | 61 } // namespace blink |
| OLD | NEW |