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

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

Issue 2173353002: Simplify ElementVisibilityObserver implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: simplify finalization + sync class comment Created 4 years, 4 months 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/dom/IntersectionObserver.h" 8 #include "core/CoreExport.h"
9 #include "platform/heap/Heap.h" 9 #include "platform/heap/Handle.h"
10 #include "platform/heap/Member.h"
11 10
12 namespace blink { 11 namespace blink {
13 12
14 class Element; 13 class Element;
14 class IntersectionObserver;
15 class IntersectionObserverEntry;
15 16
16 // ElementVisibilityObserver is a helper class to be used to track the 17 // ElementVisibilityObserver is a helper class to be used to track the
17 // visibility of an Element in the viewport. Creating an 18 // 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 19 // IntersectionObserver.
19 // has be started by calling |start()| and can be stopped with |stop()|. 20 //
20 // When creating an instance, the caller will have to pass a callback taking 21 // When creating an ElementVisibilityObserver instance, alongside the element
21 // a boolean as an argument. The boolean will be the new visibility state. 22 // reference, the caller will have to supply an object reference implementing
22 // The ElementVisibilityObserver is implemented on top of IntersectionObserver. 23 // 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 24 // method will be invoked when the element changes visibility state,
24 // visibility of an element. 25 // the boolean argument indicating which.
25 class ElementVisibilityObserver final : public GarbageCollectedFinalized<Element VisibilityObserver> { 26 class ElementVisibilityObserver final : public GarbageCollected<ElementVisibilit yObserver> {
26 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver); 27 WTF_MAKE_NONCOPYABLE(ElementVisibilityObserver);
27 public: 28 public:
28 using VisibilityCallback = Function<void(bool), WTF::SameThreadAffinity>; 29 class CORE_EXPORT Client : public GarbageCollectedMixin {
30 public:
31 virtual void onVisibilityChanged(bool isVisible) = 0;
32 };
29 33
30 ElementVisibilityObserver(Element*, std::unique_ptr<VisibilityCallback>); 34 static ElementVisibilityObserver* create(Element*, Client*);
31 virtual ~ElementVisibilityObserver();
32 35
33 void start();
34 void stop(); 36 void stop();
35 37
36 DECLARE_VIRTUAL_TRACE(); 38 DECLARE_TRACE();
37 39
38 private: 40 private:
39 class ElementVisibilityCallback; 41 explicit ElementVisibilityObserver(Client*);
42
43 void start(Element*);
40 44
41 void onVisibilityChanged(const HeapVector<Member<IntersectionObserverEntry>> &); 45 void onVisibilityChanged(const HeapVector<Member<IntersectionObserverEntry>> &);
42 46
43 Member<Element> m_element; 47 Member<Client> m_client;
44 Member<IntersectionObserver> m_intersectionObserver; 48 Member<IntersectionObserver> m_intersectionObserver;
45 std::unique_ptr<VisibilityCallback> m_callback;
46 }; 49 };
47 50
48 } // namespace blink 51 } // namespace blink
49 52
50 #endif // ElementVisibilityObserver_h 53 #endif // ElementVisibilityObserver_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698