OLD | NEW |
(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 GarbageCollectedFinalized<IntersectionObs
ervation> { |
| 19 public: |
| 20 typedef std::pair<IntersectionObserver*, Element*> HashKey; |
| 21 typedef Member<IntersectionObservation> ValueType; |
| 22 typedef WeakMember<IntersectionObservation> WeakValueType; |
| 23 typedef typename WTF::HashTraits<ValueType>::PeekInType PeekInType; |
| 24 struct ValueTypeHash { |
| 25 static unsigned hash() |
| 26 { |
| 27 return DefaultHash<HashKey>::Hash::hash(HashKey(nullptr, nullptr)); |
| 28 } |
| 29 static unsigned hash(PeekInType keyRef) |
| 30 { |
| 31 IntersectionObservation* key = keyRef; |
| 32 if (!key) |
| 33 return hash(); |
| 34 return DefaultHash<HashKey>::Hash::hash(HashKey(key->observer(), key
->target())); |
| 35 } |
| 36 static bool equal(const PeekInType& aRef, const PeekInType& bRef) |
| 37 { |
| 38 IntersectionObservation* a = aRef; |
| 39 IntersectionObservation* b = bRef; |
| 40 if (!a) |
| 41 return b; |
| 42 if (!b) |
| 43 return true; |
| 44 return a->observer() == b->observer() && a->target() == b->target(); |
| 45 } |
| 46 static const bool safeToCompareToEmptyOrDeleted = true; |
| 47 }; |
| 48 struct HashKeyTranslator { |
| 49 static unsigned hash(const HashKey& key) { return DefaultHash<HashKey>::
Hash::hash(key); } |
| 50 static bool equal(const ValueType& a, const HashKey& b) |
| 51 { |
| 52 return a && a->observer() == b.first && a->target() == b.second; |
| 53 } |
| 54 static bool equal(const WeakValueType& a, const HashKey& b) |
| 55 { |
| 56 return a && a->observer() == b.first && a->target() == b.second; |
| 57 } |
| 58 }; |
| 59 typedef HeapHashSet<ValueType, ValueTypeHash> HashSet; |
| 60 typedef HeapHashSet<WeakValueType, ValueTypeHash> WeakHashSet; |
| 61 |
| 62 template<typename HashSetType> |
| 63 static typename HashSetType::iterator find(HashSetType& hashSet, Intersectio
nObserver* observer, Element* target) |
| 64 { |
| 65 return hashSet.template find<HashKeyTranslator, HashKey>(HashKey(observe
r, target)); |
| 66 } |
| 67 |
| 68 template<typename HashSetType> |
| 69 static bool contains(HashSetType& hashSet, IntersectionObserver* observer, E
lement* target) |
| 70 { |
| 71 return hashSet.template contains<HashKeyTranslator, HashKey>(HashKey(obs
erver, target)); |
| 72 } |
| 73 |
| 74 template<typename HashSetType> |
| 75 static void remove(HashSetType& hashSet, IntersectionObserver* observer, Ele
ment* target) |
| 76 { |
| 77 hashSet.remove(hashSet.template find<HashKeyTranslator, HashKey>(HashKey
(observer, target))); |
| 78 } |
| 79 |
| 80 IntersectionObservation(IntersectionObserver&, Element*); |
| 81 |
| 82 const IntersectionObserver* observer() const { return m_observer; } |
| 83 IntersectionObserver* observer() { return m_observer; } |
| 84 const Element* target() const { return m_target.get(); } |
| 85 Element* target() { return m_target.get(); } |
| 86 bool isActive() const { return m_active; } |
| 87 void setActive(bool); |
| 88 float lastVisibleRatio() const { return m_lastVisibleRatio; } |
| 89 void setLastVisibleRatio(float ratio) { m_lastVisibleRatio = ratio; } |
| 90 |
| 91 void computeIntersectionObservations(int); |
| 92 void disconnect(); |
| 93 |
| 94 DECLARE_TRACE(); |
| 95 |
| 96 private: |
| 97 Member<IntersectionObserver> m_observer; |
| 98 WeakPtrWillBeWeakMember<Element> m_target; |
| 99 bool m_active; |
| 100 bool m_canReportRootBounds; |
| 101 float m_lastVisibleRatio; |
| 102 }; |
| 103 |
| 104 } // namespace blink { |
| 105 |
| 106 #endif // IntersectionObservation_h |
OLD | NEW |