| 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/dom/IntersectionObserver.h" | 9 #include "core/dom/IntersectionObserver.h" |
| 9 #include "platform/heap/Heap.h" | 10 #include "platform/heap/Heap.h" |
| 10 #include "platform/heap/Member.h" | 11 #include "platform/heap/Member.h" |
| 11 | 12 |
| 12 namespace blink { | 13 namespace blink { |
| 13 | 14 |
| 14 class Element; | 15 class Element; |
| 15 | 16 |
| 16 // ElementVisibilityObserver is a helper class to be used to track the | 17 // ElementVisibilityObserver is a helper class to be used to track the |
| 17 // visibility of an Element in the viewport. Creating an | 18 // visibility of an Element in the viewport. Creating an |
| 18 // ElementVisibilityObserver is a no-op with regards to CPU cycle. The observing | 19 // ElementVisibilityObserver is a no-op with regards to CPU cycle. The observing |
| 19 // has be started by calling |start()| and can be stopped with |stop()|. | 20 // has be started by calling |start()| and can be stopped with |stop()|. |
| 20 // When creating an instance, the caller will have to pass a callback taking | 21 // When creating an instance, the caller will have to pass a callback taking |
| 21 // a boolean as an argument. The boolean will be the new visibility state. | 22 // a boolean as an argument. The boolean will be the new visibility state. |
| 22 // The ElementVisibilityObserver is implemented on top of IntersectionObserver. | 23 // The ElementVisibilityObserver is implemented on top of IntersectionObserver. |
| 23 // It is a layer meant to simplify the usage for C++ Blink code checking for the | 24 // It is a layer meant to simplify the usage for C++ Blink code checking for the |
| 24 // visibility of an element. | 25 // visibility of an element. |
| 25 class ElementVisibilityObserver final | 26 class CORE_EXPORT ElementVisibilityObserver final |
| 26 : public GarbageCollectedFinalized<ElementVisibilityObserver> { | 27 : public GarbageCollectedFinalized<ElementVisibilityObserver> { |
| 27 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); | 28 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); |
| 28 | 29 |
| 29 public: | 30 public: |
| 30 using VisibilityCallback = Function<void(bool), WTF::SameThreadAffinity>; | 31 using VisibilityCallback = Function<void(bool), WTF::SameThreadAffinity>; |
| 31 | 32 |
| 32 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); | 33 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); |
| 33 virtual ~ElementVisibilityObserver(); | 34 virtual ~ElementVisibilityObserver(); |
| 34 | 35 |
| 35 void start(); | 36 void start(); |
| 36 void stop(); | 37 void stop(); |
| 37 | 38 |
| 38 DECLARE_VIRTUAL_TRACE(); | 39 DECLARE_VIRTUAL_TRACE(); |
| 39 | 40 |
| 40 private: | 41 private: |
| 41 class ElementVisibilityCallback; | 42 class ElementVisibilityCallback; |
| 42 | 43 |
| 43 void onVisibilityChanged( | 44 void onVisibilityChanged( |
| 44 const HeapVector<Member<IntersectionObserverEntry>>&); | 45 const HeapVector<Member<IntersectionObserverEntry>>&); |
| 45 | 46 |
| 46 Member<Element> m_element; | 47 Member<Element> m_element; |
| 47 Member<IntersectionObserver> m_intersectionObserver; | 48 Member<IntersectionObserver> m_intersectionObserver; |
| 48 std::unique_ptr<VisibilityCallback> m_callback; | 49 std::unique_ptr<VisibilityCallback> m_callback; |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 } // namespace blink | 52 } // namespace blink |
| 52 | 53 |
| 53 #endif // ElementVisibilityObserver_h | 54 #endif // ElementVisibilityObserver_h |
| OLD | NEW |