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

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

Issue 2475643004: Monitor the intersection of video and viewport. (Closed)
Patch Set: Use empty thresholds to indicate observing intersection with viewport. 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 #include "public/platform/WebRect.h"
12 13
13 namespace blink { 14 namespace blink {
14 15
15 class Element; 16 class Element;
16 17
18 // Base class for ElementVisibilityObserver and ViewportIntersectionObserver.
19 class VisibilityObserverBase
20 : public GarbageCollectedFinalized<VisibilityObserverBase> {
21 WTF_MAKE_NONCOPYABLE(VisibilityObserverBase);
22
23 public:
24 VisibilityObserverBase(Element*, const Vector<float>&);
25 virtual ~VisibilityObserverBase();
26
27 virtual void onVisibilityChanged(
28 const HeapVector<Member<IntersectionObserverEntry>>& entries) = 0;
29 void start();
30 void stop();
31
32 DECLARE_VIRTUAL_TRACE();
33
34 protected:
35 Member<Element> m_element;
36 Vector<float> m_thresholds;
37 Member<IntersectionObserver> m_intersectionObserver;
38 };
39
17 // ElementVisibilityObserver is a helper class to be used to track the 40 // ElementVisibilityObserver is a helper class to be used to track the
18 // visibility of an Element in the viewport. Creating an 41 // visibility of an Element in the viewport. Creating an
19 // ElementVisibilityObserver is a no-op with regards to CPU cycle. The observing 42 // 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()|. 43 // 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 44 // 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. 45 // a boolean as an argument. The boolean will be the new visibility state.
23 // The ElementVisibilityObserver is implemented on top of IntersectionObserver. 46 // 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 47 // It is a layer meant to simplify the usage for C++ Blink code checking for the
25 // visibility of an element. 48 // visibility of an element.
26 class CORE_EXPORT ElementVisibilityObserver final 49 class CORE_EXPORT ElementVisibilityObserver final
27 : public GarbageCollectedFinalized<ElementVisibilityObserver> { 50 : public VisibilityObserverBase {
28 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); 51 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver);
29 52
30 public: 53 public:
31 using VisibilityCallback = Function<void(bool), WTF::SameThreadAffinity>; 54 using VisibilityCallback = Function<void(bool), WTF::SameThreadAffinity>;
32 55
33 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); 56 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>);
34 virtual ~ElementVisibilityObserver(); 57 ~ElementVisibilityObserver() final;
35
36 void start();
37 void stop();
38 58
39 void deliverObservationsForTesting(); 59 void deliverObservationsForTesting();
40 60
41 DECLARE_VIRTUAL_TRACE();
42
43 private: 61 private:
44 class ElementVisibilityCallback; 62 class ElementVisibilityCallback;
45 63
46 void onVisibilityChanged( 64 void onVisibilityChanged(
47 const HeapVector<Member<IntersectionObserverEntry>>&); 65 const HeapVector<Member<IntersectionObserverEntry>>&) final;
66 std::unique_ptr<VisibilityCallback> m_callback;
67 };
48 68
49 Member<Element> m_element; 69 // ViewportIntersectionObserver monitors the intersection between observed
50 Member<IntersectionObserver> m_intersectionObserver; 70 // element and viewport. Creating an ViewportIntersectionObserver is a no-op
51 std::unique_ptr<VisibilityCallback> m_callback; 71 // with regards to CPU cycle. The observing has be started by calling |start()|
72 // and can be stopped with |stop()|. When creating an instance, the caller will
73 // have to pass a callback taking the rootRect and the intersectRect as
74 // arguments.
75 class ViewportIntersectionObserver final : public VisibilityObserverBase {
76 WTF_MAKE_NONCOPYABLE(ViewportIntersectionObserver);
77
78 public:
79 using RatioChangeCallback =
80 Function<void(const WebRect&, const WebRect&), WTF::SameThreadAffinity>;
81
82 ViewportIntersectionObserver(Element*, std::unique_ptr<RatioChangeCallback>);
83 ~ViewportIntersectionObserver() final;
84
85 private:
86 void onVisibilityChanged(
87 const HeapVector<Member<IntersectionObserverEntry>>&) final;
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