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

Unified 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: Fix rootMargin parsing Created 5 years 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 side-by-side diff with in-line comments
Download patch
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..c0c77a8b08a06114cd109bc2b1de191f6ac93e01
--- /dev/null
+++ b/third_party/WebKit/Source/core/dom/IntersectionObservation.h
@@ -0,0 +1,106 @@
+// 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/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 {
+ 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)
+ {
+ 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)
+ {
+ 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)
+ {
+ hashSet.remove(hashSet.template find<HashKeyTranslator, HashKey>(HashKey(observer, target)));
+ }
+
+ IntersectionObservation(IntersectionObserver&, Element*);
+
+ 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);
+ float lastVisibleRatio() const { return m_lastVisibleRatio; }
+ void setLastVisibleRatio(float ratio) { m_lastVisibleRatio = ratio; }
+
+ void computeIntersectionObservations(int);
+ void disconnect();
+
+ DECLARE_TRACE();
+
+private:
+ Member<IntersectionObserver> m_observer;
+ WeakPtrWillBeWeakMember<Element> m_target;
+ bool m_active;
+ bool m_canReportRootBounds;
+ float m_lastVisibleRatio;
+};
+
+} // namespace blink {
+
+#endif // IntersectionObservation_h

Powered by Google App Engine
This is Rietveld 408576698