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