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