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" |
(...skipping 28 matching lines...) Expand all Loading... |
39 m_element.release(); | 39 m_element.release(); |
40 return; | 40 return; |
41 } | 41 } |
42 | 42 |
43 DCHECK(!m_intersectionObserver); | 43 DCHECK(!m_intersectionObserver); |
44 m_intersectionObserver = IntersectionObserver::create( | 44 m_intersectionObserver = IntersectionObserver::create( |
45 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), | 45 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), |
46 &document, WTF::bind(&ElementVisibilityObserver::onVisibilityChanged, | 46 &document, WTF::bind(&ElementVisibilityObserver::onVisibilityChanged, |
47 wrapWeakPersistent(this))); | 47 wrapWeakPersistent(this))); |
48 DCHECK(m_intersectionObserver); | 48 DCHECK(m_intersectionObserver); |
49 m_intersectionObserver->setInitialState( | |
50 IntersectionObserver::InitialState::kAuto); | |
51 m_intersectionObserver->observe(m_element.release()); | 49 m_intersectionObserver->observe(m_element.release()); |
52 } | 50 } |
53 | 51 |
54 void ElementVisibilityObserver::stop() { | 52 void ElementVisibilityObserver::stop() { |
55 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, | 53 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, |
56 // so |m_intersectionObserver| may be null at this point after start(). | 54 // so |m_intersectionObserver| may be null at this point after start(). |
57 // 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 |
58 // https://crbug.com/615156 | 56 // https://crbug.com/615156 |
59 if (!m_intersectionObserver) | 57 if (!m_intersectionObserver) |
60 return; | 58 return; |
61 | 59 |
62 m_intersectionObserver->disconnect(); | 60 m_intersectionObserver->disconnect(); |
63 m_intersectionObserver = nullptr; | 61 m_intersectionObserver = nullptr; |
64 } | 62 } |
65 | 63 |
66 void ElementVisibilityObserver::deliverObservationsForTesting() { | |
67 m_intersectionObserver->deliver(); | |
68 } | |
69 | |
70 DEFINE_TRACE(ElementVisibilityObserver) { | 64 DEFINE_TRACE(ElementVisibilityObserver) { |
71 visitor->trace(m_element); | 65 visitor->trace(m_element); |
72 visitor->trace(m_intersectionObserver); | 66 visitor->trace(m_intersectionObserver); |
73 } | 67 } |
74 | 68 |
75 void ElementVisibilityObserver::onVisibilityChanged( | 69 void ElementVisibilityObserver::onVisibilityChanged( |
76 const HeapVector<Member<IntersectionObserverEntry>>& entries) { | 70 const HeapVector<Member<IntersectionObserverEntry>>& entries) { |
77 bool isVisible = entries.last()->intersectionRatio() > 0.f; | 71 bool isVisible = entries.last()->intersectionRatio() > 0.f; |
78 (*m_callback.get())(isVisible); | 72 (*m_callback.get())(isVisible); |
79 } | 73 } |
80 | 74 |
81 } // namespace blink | 75 } // namespace blink |
OLD | NEW |