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