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

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

Issue 1449623002: IntersectionObserver: second cut. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Clarify the tear-down path for observers and observations. Created 5 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "platform/heap/Handle.h"
6 #include "wtf/HashSet.h"
7 #include "wtf/RefPtr.h"
8 #include "wtf/WeakPtr.h"
9
10 #ifndef IntersectionObservation_h
11 #define IntersectionObservation_h
12
13 namespace blink {
14
15 class Element;
16 class IntersectionObserver;
17
18 class IntersectionObservation : public RefCountedWillBeGarbageCollectedFinalized <IntersectionObservation> {
haraken 2015/11/16 00:41:34 You don't need to use WillBe types for newly intro
szager1 2015/11/17 01:50:03 If I understand you correctly, I can use regular o
19 public:
20 typedef RefPtrWillBeRawPtr<IntersectionObservation> RefPtrType;
21 typedef std::pair<IntersectionObserver*, Element*> HashKey;
22 typedef WTF::RefPtrValuePeeker<IntersectionObservation> PeekInType;
23 struct RefPtrHash {
24 static unsigned hash(PeekInType keyRef)
25 {
26 IntersectionObservation* key = keyRef;
27 if (!key)
28 return DefaultHash<HashKey>::Hash::hash(HashKey(nullptr, nullptr ));
eae 2015/11/16 23:34:44 A separate hash method that takes no type would ma
szager1 2015/11/19 19:15:38 Do you mean: static unsigned hash(PeekInType keyR
29 return DefaultHash<HashKey>::Hash::hash(HashKey(key->observer(), key ->target()));
30 }
31 static bool equal(PeekInType aRef, PeekInType bRef)
eae 2015/11/16 23:34:44 You might want to use const here.
szager1 2015/11/19 19:15:38 Done in next patch.
32 {
33 IntersectionObservation* a = aRef;
34 IntersectionObservation* b = bRef;
35 if (!a)
36 return b;
37 if (!b)
38 return true;
39 return a->observer() == b->observer() && a->target() == b->target();
40 }
41 static const bool safeToCompareToEmptyOrDeleted = true;
42 };
43 struct HashKeyTranslator {
44 static unsigned hash(const HashKey& key) { return DefaultHash<HashKey>:: Hash::hash(key); }
45 static bool equal(const RefPtrType& a, const HashKey& b)
46 {
47 return a->observer() == b.first && a->target() == b.second;
48 }
49 };
50 typedef WillBeHeapHashSet<RefPtrType, RefPtrHash> HashSet;
51
52 static HashSet::iterator find(HashSet& hashSet, IntersectionObserver* observ er, Element* target)
53 {
54 return hashSet.find<HashKeyTranslator, HashKey>(HashKey(observer, target ));
55 }
56 static bool contains(HashSet& hashSet, IntersectionObserver* observer, Eleme nt* target)
57 {
58 return hashSet.contains<HashKeyTranslator, HashKey>(HashKey(observer, ta rget));
59 }
60 static void remove(HashSet& hashSet, IntersectionObserver* observer, Element * target)
61 {
62 hashSet.remove(hashSet.find<HashKeyTranslator, HashKey>(HashKey(observer , target)));
63 }
64
65 static PassRefPtrWillBeRawPtr<IntersectionObservation> create(IntersectionOb server&, WeakPtrWillBeRawPtr<Element>, bool active = true);
66
67 const IntersectionObserver* observer() const;
68 IntersectionObserver* observer();
69 const Element* target() const { return m_target.get(); }
70 Element* target() { return m_target.get(); }
71 bool isActive() const { return m_active; }
72 void setActive(bool);
73 float lastVisibleRatio() const { return m_lastVisibleRatio; }
74 void setLastVisibleRatio(float ratio) { m_lastVisibleRatio = ratio; }
75
76 void computeIntersectionObservations(double);
77 void disconnect();
78
79 DECLARE_TRACE();
80 private:
81 IntersectionObservation(IntersectionObserver&, WeakPtrWillBeRawPtr<Element>, bool);
82
83 RefPtrWillBeMember<IntersectionObserver> m_observer;
84 WeakPtrWillBeWeakMember<Element> m_target;
85 bool m_active;
86 float m_lastVisibleRatio;
87 };
88
89 } // namespace blink {
90
91 #endif // IntersectionObservation_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698