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