Chromium Code Reviews| Index: third_party/WebKit/Source/core/dom/IntersectionObservation.h |
| diff --git a/third_party/WebKit/Source/core/dom/IntersectionObservation.h b/third_party/WebKit/Source/core/dom/IntersectionObservation.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..43dae6337116a6792d648d0909bfc33f090db37f |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/dom/IntersectionObservation.h |
| @@ -0,0 +1,110 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "platform/geometry/LayoutRect.h" |
| +#include "platform/heap/Handle.h" |
| +#include "wtf/HashSet.h" |
| +#include "wtf/RefPtr.h" |
| +#include "wtf/WeakPtr.h" |
| + |
| +#ifndef IntersectionObservation_h |
| +#define IntersectionObservation_h |
| + |
| +namespace blink { |
| + |
| +class Element; |
| +class IntersectionObserver; |
| + |
| +class IntersectionObservation : public GarbageCollectedFinalized<IntersectionObservation> { |
| +public: |
| + typedef std::pair<IntersectionObserver*, Element*> HashKey; |
| + typedef Member<IntersectionObservation> ValueType; |
| + typedef WeakMember<IntersectionObservation> WeakValueType; |
| + typedef typename WTF::HashTraits<ValueType>::PeekInType PeekInType; |
| + struct ValueTypeHash { |
|
esprehn
2015/12/12 00:14:14
hmm, we've never had to do this before for a norma
szager1
2015/12/16 19:15:35
Removed the custom hash_traits.
|
| + static unsigned hash() |
| + { |
| + return DefaultHash<HashKey>::Hash::hash(HashKey(nullptr, nullptr)); |
| + } |
| + static unsigned hash(PeekInType keyRef) |
| + { |
| + IntersectionObservation* key = keyRef; |
| + if (!key) |
| + return hash(); |
| + return DefaultHash<HashKey>::Hash::hash(HashKey(key->observer(), key->target())); |
| + } |
| + static bool equal(const PeekInType& aRef, const PeekInType& bRef) |
| + { |
| + IntersectionObservation* a = aRef; |
| + IntersectionObservation* b = bRef; |
| + if (!a) |
| + return b; |
| + if (!b) |
| + return true; |
| + return a->observer() == b->observer() && a->target() == b->target(); |
| + } |
| + static const bool safeToCompareToEmptyOrDeleted = true; |
| + }; |
| + struct HashKeyTranslator { |
| + static unsigned hash(const HashKey& key) { return DefaultHash<HashKey>::Hash::hash(key); } |
| + static bool equal(const ValueType& a, const HashKey& b) |
| + { |
|
esprehn
2015/12/12 00:14:14
yeah I'd really rather we didn't do this
szager1
2015/12/16 19:15:35
Done.
|
| + return a && a->observer() == b.first && a->target() == b.second; |
| + } |
| + static bool equal(const WeakValueType& a, const HashKey& b) |
| + { |
| + return a && a->observer() == b.first && a->target() == b.second; |
| + } |
| + }; |
| + typedef HeapHashSet<ValueType, ValueTypeHash> HashSet; |
| + typedef HeapHashSet<WeakValueType, ValueTypeHash> WeakHashSet; |
| + |
| + template<typename HashSetType> |
| + static typename HashSetType::iterator find(HashSetType& hashSet, IntersectionObserver* observer, Element* target) |
|
esprehn
2015/12/12 00:14:14
remove this, no templates or statics like this, ad
szager1
2015/12/16 19:15:35
Done.
|
| + { |
| + return hashSet.template find<HashKeyTranslator, HashKey>(HashKey(observer, target)); |
| + } |
| + |
| + template<typename HashSetType> |
| + static bool contains(HashSetType& hashSet, IntersectionObserver* observer, Element* target) |
| + { |
| + return hashSet.template contains<HashKeyTranslator, HashKey>(HashKey(observer, target)); |
| + } |
| + |
| + template<typename HashSetType> |
| + static void remove(HashSetType& hashSet, IntersectionObserver* observer, Element* target) |
|
esprehn
2015/12/12 00:14:14
ditto for all of these
szager1
2015/12/16 19:15:35
Done.
|
| + { |
| + hashSet.remove(hashSet.template find<HashKeyTranslator, HashKey>(HashKey(observer, target))); |
| + } |
| + |
| + IntersectionObservation(IntersectionObserver&, Element*, bool); |
|
esprehn
2015/12/12 00:14:14
bool needs an argument name, why not an enum? We d
szager1
2015/12/16 19:15:35
The bool is canReportRootBounds, is there some oth
|
| + |
| + const IntersectionObserver* observer() const { return m_observer; } |
| + IntersectionObserver* observer() { return m_observer; } |
| + const Element* target() const { return m_target.get(); } |
| + Element* target() { return m_target.get(); } |
| + bool isActive() const { return m_active; } |
| + void setActive(bool active) { m_active = active; } |
| + float lastVisibleRatio() const { return m_lastVisibleRatio; } |
| + void setLastVisibleRatio(float ratio) { m_lastVisibleRatio = ratio; } |
| + bool canReportRootBounds() const { return m_canReportRootBounds; } |
| + |
| + void computeIntersectionObservations(int); |
|
esprehn
2015/12/12 00:14:14
int needs an argument name, primitive arguments al
szager1
2015/12/16 19:15:35
Done.
|
| + void disconnect(); |
| + |
| + DECLARE_TRACE(); |
| + |
| +private: |
| + bool computeGeometry(LayoutRect& targetRect, LayoutRect& rootRect, LayoutRect& intersectionRect); |
| + |
| + Member<IntersectionObserver> m_observer; |
| + WeakPtrWillBeWeakMember<Element> m_target; |
| + bool m_active; |
| + bool m_canReportRootBounds; |
|
esprehn
2015/12/12 00:14:14
I'd swap these with the float, and make them both
ojan
2015/12/12 01:06:28
Bikeshed nit: We typically call this m_shouldRepor
szager1
2015/12/16 19:15:35
Done.
szager1
2015/12/16 19:15:35
Done.
|
| + float m_lastVisibleRatio; |
| +}; |
| + |
| +} // namespace blink { |
| + |
| +#endif // IntersectionObservation_h |