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