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/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.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" |
(...skipping 14 matching lines...) Expand all Loading... |
25 class CORE_EXPORT IntersectionObserver final | 25 class CORE_EXPORT IntersectionObserver final |
26 : public GarbageCollectedFinalized<IntersectionObserver>, | 26 : public GarbageCollectedFinalized<IntersectionObserver>, |
27 public ScriptWrappable { | 27 public ScriptWrappable { |
28 DEFINE_WRAPPERTYPEINFO(); | 28 DEFINE_WRAPPERTYPEINFO(); |
29 | 29 |
30 public: | 30 public: |
31 using EventCallback = | 31 using EventCallback = |
32 Function<void(const HeapVector<Member<IntersectionObserverEntry>>&), | 32 Function<void(const HeapVector<Member<IntersectionObserverEntry>>&), |
33 WTF::SameThreadAffinity>; | 33 WTF::SameThreadAffinity>; |
34 | 34 |
35 // Defines the assumed initial state of the observed element. If the actual | |
36 // state is the same as the initial state, then no observation will be | |
37 // delivered. kAuto means the initial observation will always get sent. | |
38 enum InitialState { | |
39 // TODO(skyostil): Add support for kVisible. | |
40 kAuto = 0, | |
41 kHidden = 1, | |
42 kDoNotUseMax = 2 | |
43 }; | |
44 | |
45 // InitialState is stored in a single bit in m_initialState. If adding new | |
46 // enum values, increase the size of m_initialState and update the assert. | |
47 static_assert(InitialState::kDoNotUseMax == 2, | |
48 "InitialState fits in a single bit."); | |
49 | |
50 static IntersectionObserver* create(const IntersectionObserverInit&, | 35 static IntersectionObserver* create(const IntersectionObserverInit&, |
51 IntersectionObserverCallback&, | 36 IntersectionObserverCallback&, |
52 ExceptionState&); | 37 ExceptionState&); |
53 static IntersectionObserver* create(const Vector<Length>& rootMargin, | 38 static IntersectionObserver* create(const Vector<Length>& rootMargin, |
54 const Vector<float>& thresholds, | 39 const Vector<float>& thresholds, |
55 Document*, | 40 Document*, |
56 std::unique_ptr<EventCallback>, | 41 std::unique_ptr<EventCallback>, |
57 ExceptionState& = ASSERT_NO_EXCEPTION); | 42 ExceptionState& = ASSERT_NO_EXCEPTION); |
58 static void resumeSuspendedObservers(); | 43 static void resumeSuspendedObservers(); |
59 | 44 |
(...skipping 26 matching lines...) Expand all Loading... |
86 void computeIntersectionObservations(); | 71 void computeIntersectionObservations(); |
87 void enqueueIntersectionObserverEntry(IntersectionObserverEntry&); | 72 void enqueueIntersectionObserverEntry(IntersectionObserverEntry&); |
88 unsigned firstThresholdGreaterThan(float ratio) const; | 73 unsigned firstThresholdGreaterThan(float ratio) const; |
89 void deliver(); | 74 void deliver(); |
90 bool hasEntries() const { return m_entries.size(); } | 75 bool hasEntries() const { return m_entries.size(); } |
91 const HeapLinkedHashSet<WeakMember<IntersectionObservation>>& observations() | 76 const HeapLinkedHashSet<WeakMember<IntersectionObservation>>& observations() |
92 const { | 77 const { |
93 return m_observations; | 78 return m_observations; |
94 } | 79 } |
95 | 80 |
96 // Set the assumed initial state of the observed element. Note that this can | |
97 // only be set before calling observe(). | |
98 // TODO(skyostil): Move this setting to IntersectionObserverInit once the API | |
99 // is finalized. | |
100 void setInitialState(InitialState); | |
101 | |
102 DECLARE_TRACE(); | 81 DECLARE_TRACE(); |
103 | 82 |
104 private: | 83 private: |
105 explicit IntersectionObserver(IntersectionObserverCallback&, | 84 explicit IntersectionObserver(IntersectionObserverCallback&, |
106 Element*, | 85 Element*, |
107 const Vector<Length>& rootMargin, | 86 const Vector<Length>& rootMargin, |
108 const Vector<float>& thresholds); | 87 const Vector<float>& thresholds); |
109 void clearWeakMembers(Visitor*); | 88 void clearWeakMembers(Visitor*); |
110 | 89 |
111 // Returns false if this observer has an explicit root element which has been | 90 // Returns false if this observer has an explicit root element which has been |
112 // deleted; true otherwise. | 91 // deleted; true otherwise. |
113 bool rootIsValid() const; | 92 bool rootIsValid() const; |
114 | 93 |
115 Member<IntersectionObserverCallback> m_callback; | 94 Member<IntersectionObserverCallback> m_callback; |
116 WeakMember<Element> m_root; | 95 WeakMember<Element> m_root; |
117 HeapLinkedHashSet<WeakMember<IntersectionObservation>> m_observations; | 96 HeapLinkedHashSet<WeakMember<IntersectionObservation>> m_observations; |
118 HeapVector<Member<IntersectionObserverEntry>> m_entries; | 97 HeapVector<Member<IntersectionObserverEntry>> m_entries; |
119 Vector<float> m_thresholds; | 98 Vector<float> m_thresholds; |
120 Length m_topMargin; | 99 Length m_topMargin; |
121 Length m_rightMargin; | 100 Length m_rightMargin; |
122 Length m_bottomMargin; | 101 Length m_bottomMargin; |
123 Length m_leftMargin; | 102 Length m_leftMargin; |
124 unsigned m_rootIsImplicit : 1; | 103 unsigned m_rootIsImplicit : 1; |
125 // m_initialState contains values from enum InitialState | |
126 unsigned m_initialState : 1; | |
127 }; | 104 }; |
128 | 105 |
129 } // namespace blink | 106 } // namespace blink |
130 | 107 |
131 #endif // IntersectionObserver_h | 108 #endif // IntersectionObserver_h |
OLD | NEW |