| OLD | NEW |
| 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 IntersectionObserver_h | 5 #ifndef IntersectionObserver_h |
| 6 #define IntersectionObserver_h | 6 #define IntersectionObserver_h |
| 7 | 7 |
| 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
| 9 #include "bindings/core/v8/ScriptWrappable.h" | 9 #include "bindings/core/v8/ScriptWrappable.h" |
| 10 #include "core/dom/IntersectionObservation.h" | 10 #include "core/dom/IntersectionObservation.h" |
| 11 #include "core/dom/IntersectionObserverEntry.h" | 11 #include "core/dom/IntersectionObserverEntry.h" |
| 12 #include "platform/Length.h" | 12 #include "platform/Length.h" |
| 13 #include "platform/heap/Handle.h" | 13 #include "platform/heap/Handle.h" |
| 14 #include "wtf/HashSet.h" | 14 #include "wtf/HashSet.h" |
| 15 #include "wtf/Vector.h" | 15 #include "wtf/Vector.h" |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 | 18 |
| 19 class Document; | 19 class Document; |
| 20 class Element; | 20 class Element; |
| 21 class ExceptionState; | 21 class ExceptionState; |
| 22 class LayoutObject; | 22 class LayoutObject; |
| 23 class IntersectionObserverCallback; | 23 class IntersectionObserverCallback; |
| 24 class IntersectionObserverInit; | 24 class IntersectionObserverInit; |
| 25 | 25 |
| 26 class CORE_EXPORT IntersectionObserver final | 26 class CORE_EXPORT IntersectionObserver |
| 27 : public GarbageCollectedFinalized<IntersectionObserver>, | 27 : public GarbageCollectedFinalized<IntersectionObserver>, |
| 28 public ScriptWrappable { | 28 public ScriptWrappable { |
| 29 DEFINE_WRAPPERTYPEINFO(); | 29 DEFINE_WRAPPERTYPEINFO(); |
| 30 | 30 |
| 31 public: | 31 public: |
| 32 virtual ~IntersectionObserver(); |
| 33 |
| 32 using EventCallback = | 34 using EventCallback = |
| 33 Function<void(const HeapVector<Member<IntersectionObserverEntry>>&), | 35 Function<void(const HeapVector<Member<IntersectionObserverEntry>>&), |
| 34 WTF::SameThreadAffinity>; | 36 WTF::SameThreadAffinity>; |
| 35 | 37 |
| 36 // Defines the assumed initial state of the observed element. If the actual | 38 // Defines the assumed initial state of the observed element. If the actual |
| 37 // state is the same as the initial state, then no observation will be | 39 // state is the same as the initial state, then no observation will be |
| 38 // delivered. kAuto means the initial observation will always get sent. | 40 // delivered. kAuto means the initial observation will always get sent. |
| 39 enum class InitialState { | 41 enum class InitialState { |
| 40 // TODO(skyostil): Add support for kVisible. | 42 // TODO(skyostil): Add support for kVisible. |
| 41 kAuto, | 43 kAuto, |
| 42 kHidden, | 44 kHidden, |
| 43 }; | 45 }; |
| 44 | 46 |
| 45 static IntersectionObserver* create(const IntersectionObserverInit&, | 47 static IntersectionObserver* create(const IntersectionObserverInit&, |
| 46 IntersectionObserverCallback&, | 48 IntersectionObserverCallback&, |
| 47 ExceptionState&); | 49 ExceptionState&); |
| 50 // Observes any changes on intersection rect when |thresholds| is empty. |
| 48 static IntersectionObserver* create(const Vector<Length>& rootMargin, | 51 static IntersectionObserver* create(const Vector<Length>& rootMargin, |
| 49 const Vector<float>& thresholds, | 52 const Vector<float>& thresholds, |
| 50 Document*, | 53 Document*, |
| 51 std::unique_ptr<EventCallback>, | 54 std::unique_ptr<EventCallback>, |
| 52 ExceptionState& = ASSERT_NO_EXCEPTION); | 55 ExceptionState& = ASSERT_NO_EXCEPTION); |
| 53 static void resumeSuspendedObservers(); | 56 static void resumeSuspendedObservers(); |
| 54 | 57 |
| 55 // API methods. | 58 // API methods. |
| 56 void observe(Element*, ExceptionState& = ASSERT_NO_EXCEPTION); | 59 void observe(Element*, ExceptionState& = ASSERT_NO_EXCEPTION); |
| 57 void unobserve(Element*, ExceptionState& = ASSERT_NO_EXCEPTION); | 60 void unobserve(Element*, ExceptionState& = ASSERT_NO_EXCEPTION); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 82 } | 85 } |
| 83 | 86 |
| 84 // Set the assumed initial state of the observed element. Note that this can | 87 // Set the assumed initial state of the observed element. Note that this can |
| 85 // only be set before calling observe(). | 88 // only be set before calling observe(). |
| 86 // TODO(skyostil): Move this setting to IntersectionObserverInit once the API | 89 // TODO(skyostil): Move this setting to IntersectionObserverInit once the API |
| 87 // is finalized. | 90 // is finalized. |
| 88 void setInitialState(InitialState); | 91 void setInitialState(InitialState); |
| 89 | 92 |
| 90 DECLARE_TRACE(); | 93 DECLARE_TRACE(); |
| 91 | 94 |
| 95 protected: |
| 96 virtual IntersectionObservation* createObservation(Element&, bool); |
| 97 |
| 98 IntersectionObserver(IntersectionObserverCallback&, |
| 99 Node&, |
| 100 const Vector<Length>& rootMargin, |
| 101 const Vector<float>& thresholds); |
| 102 |
| 92 private: | 103 private: |
| 93 explicit IntersectionObserver(IntersectionObserverCallback&, | |
| 94 Node&, | |
| 95 const Vector<Length>& rootMargin, | |
| 96 const Vector<float>& thresholds); | |
| 97 void clearWeakMembers(Visitor*); | 104 void clearWeakMembers(Visitor*); |
| 98 | 105 |
| 99 Member<IntersectionObserverCallback> m_callback; | 106 Member<IntersectionObserverCallback> m_callback; |
| 100 WeakMember<Node> m_root; | 107 WeakMember<Node> m_root; |
| 101 HeapLinkedHashSet<WeakMember<IntersectionObservation>> m_observations; | 108 HeapLinkedHashSet<WeakMember<IntersectionObservation>> m_observations; |
| 102 HeapVector<Member<IntersectionObserverEntry>> m_entries; | 109 HeapVector<Member<IntersectionObserverEntry>> m_entries; |
| 103 Vector<float> m_thresholds; | 110 Vector<float> m_thresholds; |
| 104 Length m_topMargin; | 111 Length m_topMargin; |
| 105 Length m_rightMargin; | 112 Length m_rightMargin; |
| 106 Length m_bottomMargin; | 113 Length m_bottomMargin; |
| 107 Length m_leftMargin; | 114 Length m_leftMargin; |
| 108 InitialState m_initialState; | 115 InitialState m_initialState; |
| 109 }; | 116 }; |
| 110 | 117 |
| 118 class ViewportIntersectionObserver final : public IntersectionObserver { |
| 119 WTF_MAKE_NONCOPYABLE(ViewportIntersectionObserver); |
| 120 |
| 121 public: |
| 122 ViewportIntersectionObserver(IntersectionObserverCallback&, |
| 123 Node&, |
| 124 const Vector<Length>&); |
| 125 ~ViewportIntersectionObserver() final; |
| 126 |
| 127 protected: |
| 128 IntersectionObservation* createObservation(Element&, bool) final; |
| 129 }; |
| 130 |
| 111 } // namespace blink | 131 } // namespace blink |
| 112 | 132 |
| 113 #endif // IntersectionObserver_h | 133 #endif // IntersectionObserver_h |
| OLD | NEW |