Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: third_party/WebKit/Source/core/dom/ElementVisibilityObserver.h

Issue 2475643004: Monitor the intersection of video and viewport. (Closed)
Patch Set: Addressed comments. Observe intersection changing. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 ViewportIntersectionObserver.
18 class VisibilityObserverBase
19 : public GarbageCollectedFinalized<VisibilityObserverBase> {
20 WTF_MAKE_NONCOPYABLE(VisibilityObserverBase);
21
22 public:
23 explicit VisibilityObserverBase(Element*);
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() final;
35 54
36 void start(); 55 void start() final;
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 // ViewportIntersectionObserver monitors the intersection between observed
50 Member<IntersectionObserver> m_intersectionObserver; 68 // element and viewport. Creating an ViewportIntersectionObserver is a no-op
51 std::unique_ptr<VisibilityCallback> m_callback; 69 // with regards to CPU cycle. The observing has be started by calling |start()|
70 // and can be stopped with |stop()|. When creating an instance, the caller will
71 // have to pass a callback taking an IntersectionObserverEntry pointer as an
72 // argument.
73 class ViewportIntersectionObserver final : public VisibilityObserverBase {
74 WTF_MAKE_NONCOPYABLE(ViewportIntersectionObserver);
75
76 public:
77 using RatioChangeCallback =
78 Function<void(IntersectionObserverEntry*), WTF::SameThreadAffinity>;
79
80 ViewportIntersectionObserver(Element*, std::unique_ptr<RatioChangeCallback>);
81 ~ViewportIntersectionObserver() final;
82
83 void start() final;
84
85 private:
86 void onVisibilityChanged(
87 const HeapVector<Member<IntersectionObserverEntry>>&);
88 std::unique_ptr<RatioChangeCallback> m_callback;
52 }; 89 };
53 90
54 } // namespace blink 91 } // namespace blink
55 92
56 #endif // ElementVisibilityObserver_h 93 #endif // ElementVisibilityObserver_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698