| 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 #ifndef ElementVisibilityObserver_h | 5 #ifndef ElementVisibilityObserver_h |
| 6 #define ElementVisibilityObserver_h | 6 #define ElementVisibilityObserver_h |
| 7 | 7 |
| 8 #include "core/CoreExport.h" | 8 #include "core/CoreExport.h" |
| 9 #include "core/dom/IntersectionObserver.h" | 9 #include "core/dom/IntersectionObserver.h" |
| 10 #include "platform/heap/Heap.h" | 10 #include "platform/heap/Heap.h" |
| 11 #include "platform/heap/Member.h" | 11 #include "platform/heap/Member.h" |
| 12 #include "public/platform/WebRect.h" |
| 12 | 13 |
| 13 namespace blink { | 14 namespace blink { |
| 14 | 15 |
| 15 class Element; | 16 class Element; |
| 16 | 17 |
| 18 // Base class for ElementVisibilityObserver and |
| 19 // ElementViewportIntersectionObserver. |
| 20 class CORE_EXPORT VisibilityObserverBase |
| 21 : public GarbageCollectedFinalized<VisibilityObserverBase> { |
| 22 WTF_MAKE_NONCOPYABLE(VisibilityObserverBase); |
| 23 |
| 24 public: |
| 25 VisibilityObserverBase(Element*, const Vector<float>&); |
| 26 virtual ~VisibilityObserverBase(); |
| 27 |
| 28 virtual void onVisibilityChanged( |
| 29 const HeapVector<Member<IntersectionObserverEntry>>& entries) = 0; |
| 30 void start(); |
| 31 void stop(); |
| 32 |
| 33 DECLARE_VIRTUAL_TRACE(); |
| 34 |
| 35 protected: |
| 36 Member<Element> m_element; |
| 37 Vector<float> m_thresholds; |
| 38 Member<IntersectionObserver> m_intersectionObserver; |
| 39 }; |
| 40 |
| 17 // ElementVisibilityObserver is a helper class to be used to track the | 41 // ElementVisibilityObserver is a helper class to be used to track the |
| 18 // visibility of an Element in the viewport. Creating an | 42 // visibility of an Element in the viewport. Creating an |
| 19 // ElementVisibilityObserver is a no-op with regards to CPU cycle. The observing | 43 // ElementVisibilityObserver is a no-op with regards to CPU cycle. The observing |
| 20 // has be started by calling |start()| and can be stopped with |stop()|. | 44 // has be started by calling |start()| and can be stopped with |stop()|. |
| 21 // When creating an instance, the caller will have to pass a callback taking | 45 // When creating an instance, the caller will have to pass a callback taking |
| 22 // a boolean as an argument. The boolean will be the new visibility state. | 46 // a boolean as an argument. The boolean will be the new visibility state. |
| 23 // The ElementVisibilityObserver is implemented on top of IntersectionObserver. | 47 // The ElementVisibilityObserver is implemented on top of IntersectionObserver. |
| 24 // It is a layer meant to simplify the usage for C++ Blink code checking for the | 48 // It is a layer meant to simplify the usage for C++ Blink code checking for the |
| 25 // visibility of an element. | 49 // visibility of an element. |
| 26 class CORE_EXPORT ElementVisibilityObserver final | 50 class CORE_EXPORT ElementVisibilityObserver final |
| 27 : public GarbageCollectedFinalized<ElementVisibilityObserver> { | 51 : public VisibilityObserverBase { |
| 28 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); | 52 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); |
| 29 | 53 |
| 30 public: | 54 public: |
| 31 using VisibilityCallback = Function<void(bool), WTF::SameThreadAffinity>; | 55 using VisibilityCallback = Function<void(bool), WTF::SameThreadAffinity>; |
| 32 | 56 |
| 33 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); | 57 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); |
| 34 virtual ~ElementVisibilityObserver(); | 58 ~ElementVisibilityObserver() final; |
| 35 | |
| 36 void start(); | |
| 37 void stop(); | |
| 38 | 59 |
| 39 void deliverObservationsForTesting(); | 60 void deliverObservationsForTesting(); |
| 40 | 61 |
| 41 DECLARE_VIRTUAL_TRACE(); | |
| 42 | |
| 43 private: | 62 private: |
| 44 class ElementVisibilityCallback; | 63 class ElementVisibilityCallback; |
| 45 | 64 |
| 46 void onVisibilityChanged( | 65 void onVisibilityChanged( |
| 47 const HeapVector<Member<IntersectionObserverEntry>>&); | 66 const HeapVector<Member<IntersectionObserverEntry>>&) final; |
| 67 std::unique_ptr<VisibilityCallback> m_callback; |
| 68 }; |
| 48 | 69 |
| 49 Member<Element> m_element; | 70 // ElementViewportIntersectionObserver monitors the intersection between |
| 50 Member<IntersectionObserver> m_intersectionObserver; | 71 // observed element and viewport. |
| 51 std::unique_ptr<VisibilityCallback> m_callback; | 72 // Creating an ElementViewportIntersectionObserver is a no-op with regards to |
| 73 // CPU cycle. The observing has be started by calling |start()| and can be |
| 74 // stopped with |stop()|. When creating an instance, the caller will have to |
| 75 // pass a callback taking the rootRect and the intersectRect as arguments. |
| 76 class ElementViewportIntersectionObserver final |
| 77 : public VisibilityObserverBase { |
| 78 WTF_MAKE_NONCOPYABLE(ElementViewportIntersectionObserver); |
| 79 |
| 80 public: |
| 81 using RatioChangeCallback = |
| 82 Function<void(const WebRect&, const WebRect&), WTF::SameThreadAffinity>; |
| 83 |
| 84 ElementViewportIntersectionObserver(Element*, |
| 85 std::unique_ptr<RatioChangeCallback>); |
| 86 ~ElementViewportIntersectionObserver() final; |
| 87 |
| 88 private: |
| 89 void onVisibilityChanged( |
| 90 const HeapVector<Member<IntersectionObserverEntry>>&) final; |
| 91 std::unique_ptr<RatioChangeCallback> m_callback; |
| 52 }; | 92 }; |
| 53 | 93 |
| 54 } // namespace blink | 94 } // namespace blink |
| 55 | 95 |
| 56 #endif // ElementVisibilityObserver_h | 96 #endif // ElementVisibilityObserver_h |
| OLD | NEW |