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); |
49 m_intersectionObserver->observe(m_element.release()); | 51 m_intersectionObserver->observe(m_element.release()); |
50 } | 52 } |
51 | 53 |
52 void ElementVisibilityObserver::stop() { | 54 void ElementVisibilityObserver::stop() { |
53 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, | 55 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, |
54 // so |m_intersectionObserver| may be null at this point after start(). | 56 // so |m_intersectionObserver| may be null at this point after start(). |
55 // Replace this early return with DCHECK when this has been fixed. See | 57 // Replace this early return with DCHECK when this has been fixed. See |
56 // https://crbug.com/615156 | 58 // https://crbug.com/615156 |
57 if (!m_intersectionObserver) | 59 if (!m_intersectionObserver) |
58 return; | 60 return; |
59 | 61 |
60 m_intersectionObserver->disconnect(); | 62 m_intersectionObserver->disconnect(); |
61 m_intersectionObserver = nullptr; | 63 m_intersectionObserver = nullptr; |
62 } | 64 } |
63 | 65 |
| 66 void ElementVisibilityObserver::deliverObservationsForTesting() { |
| 67 m_intersectionObserver->deliver(); |
| 68 } |
| 69 |
64 DEFINE_TRACE(ElementVisibilityObserver) { | 70 DEFINE_TRACE(ElementVisibilityObserver) { |
65 visitor->trace(m_element); | 71 visitor->trace(m_element); |
66 visitor->trace(m_intersectionObserver); | 72 visitor->trace(m_intersectionObserver); |
67 } | 73 } |
68 | 74 |
69 void ElementVisibilityObserver::onVisibilityChanged( | 75 void ElementVisibilityObserver::onVisibilityChanged( |
70 const HeapVector<Member<IntersectionObserverEntry>>& entries) { | 76 const HeapVector<Member<IntersectionObserverEntry>>& entries) { |
71 bool isVisible = entries.last()->intersectionRatio() > 0.f; | 77 bool isVisible = entries.last()->intersectionRatio() > 0.f; |
72 (*m_callback.get())(isVisible); | 78 (*m_callback.get())(isVisible); |
73 } | 79 } |
74 | 80 |
75 } // namespace blink | 81 } // namespace blink |
OLD | NEW |