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 IntersectionObserverCallback; | 22 class IntersectionObserverCallback; |
23 class IntersectionObserverInit; | 23 class IntersectionObserverInit; |
24 class LayoutObject; | |
24 | 25 |
25 class CORE_EXPORT IntersectionObserver final | 26 class CORE_EXPORT IntersectionObserver final |
26 : public GarbageCollectedFinalized<IntersectionObserver>, | 27 : public GarbageCollectedFinalized<IntersectionObserver>, |
27 public ScriptWrappable { | 28 public ScriptWrappable { |
28 DEFINE_WRAPPERTYPEINFO(); | 29 DEFINE_WRAPPERTYPEINFO(); |
29 | 30 |
30 public: | 31 public: |
31 using EventCallback = | 32 using EventCallback = |
32 Function<void(const HeapVector<Member<IntersectionObserverEntry>>&), | 33 Function<void(const HeapVector<Member<IntersectionObserverEntry>>&), |
33 WTF::SameThreadAffinity>; | 34 WTF::SameThreadAffinity>; |
34 | 35 |
35 // Defines the assumed initial state of the observed element. If the actual | 36 // 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 // 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 // delivered. kAuto means the initial observation will always get sent. |
38 enum class InitialState { | 39 enum InitialState { |
39 // TODO(skyostil): Add support for kVisible. | 40 // TODO(skyostil): Add support for kVisible. |
40 kAuto, | 41 kAuto = 0, |
41 kHidden, | 42 kHidden = 1, |
42 }; | 43 }; |
43 | 44 |
44 static IntersectionObserver* create(const IntersectionObserverInit&, | 45 static IntersectionObserver* create(const IntersectionObserverInit&, |
45 IntersectionObserverCallback&, | 46 IntersectionObserverCallback&, |
46 ExceptionState&); | 47 ExceptionState&); |
47 static IntersectionObserver* create(const Vector<Length>& rootMargin, | 48 static IntersectionObserver* create(const Vector<Length>& rootMargin, |
48 const Vector<float>& thresholds, | 49 const Vector<float>& thresholds, |
49 Document*, | 50 Document*, |
50 std::unique_ptr<EventCallback>, | 51 std::unique_ptr<EventCallback>, |
51 ExceptionState& = ASSERT_NO_EXCEPTION); | 52 ExceptionState& = ASSERT_NO_EXCEPTION); |
52 static void resumeSuspendedObservers(); | 53 static void resumeSuspendedObservers(); |
53 | 54 |
54 // API methods. | 55 // API methods. |
55 void observe(Element*, ExceptionState& = ASSERT_NO_EXCEPTION); | 56 void observe(Element*, ExceptionState& = ASSERT_NO_EXCEPTION); |
56 void unobserve(Element*, ExceptionState& = ASSERT_NO_EXCEPTION); | 57 void unobserve(Element*, ExceptionState& = ASSERT_NO_EXCEPTION); |
57 void disconnect(ExceptionState& = ASSERT_NO_EXCEPTION); | 58 void disconnect(ExceptionState& = ASSERT_NO_EXCEPTION); |
58 HeapVector<Member<IntersectionObserverEntry>> takeRecords(ExceptionState&); | 59 HeapVector<Member<IntersectionObserverEntry>> takeRecords(ExceptionState&); |
59 | 60 |
60 // API attributes. | 61 // API attributes. |
61 Element* root() const; | 62 Element* root() const { return m_root.get(); } |
62 String rootMargin() const; | 63 String rootMargin() const; |
63 const Vector<float>& thresholds() const { return m_thresholds; } | 64 const Vector<float>& thresholds() const { return m_thresholds; } |
64 | 65 |
65 Node* rootNode() const { return m_root.get(); } | 66 // An observer can either track intersections with an explicit root Element, |
67 // or with the the top-level frame's viewport (the "implicit root"). When | |
68 // tracking the implicit root, m_root will be null, but because m_root is a | |
69 // weak pointer, we cannot surmise that this observer tracks the implicit | |
70 // root just because m_root is null. Hence m_rootIsImplicit. | |
71 bool rootIsImplicit() const { return m_rootIsImplicit; } | |
72 | |
73 // This is the document which is responsible for running | |
74 // computeIntersectionObservations at frame generation time. | |
75 Document& trackingDocument() const; | |
76 | |
77 LayoutObject* rootLayoutObject() const; | |
kenrb
2016/12/09 21:01:43
This does not appear to have any callers.
szager1
2016/12/10 20:51:18
Removed.
| |
66 const Length& topMargin() const { return m_topMargin; } | 78 const Length& topMargin() const { return m_topMargin; } |
67 const Length& rightMargin() const { return m_rightMargin; } | 79 const Length& rightMargin() const { return m_rightMargin; } |
68 const Length& bottomMargin() const { return m_bottomMargin; } | 80 const Length& bottomMargin() const { return m_bottomMargin; } |
69 const Length& leftMargin() const { return m_leftMargin; } | 81 const Length& leftMargin() const { return m_leftMargin; } |
70 void computeIntersectionObservations(); | 82 void computeIntersectionObservations(); |
71 void enqueueIntersectionObserverEntry(IntersectionObserverEntry&); | 83 void enqueueIntersectionObserverEntry(IntersectionObserverEntry&); |
72 unsigned firstThresholdGreaterThan(float ratio) const; | 84 unsigned firstThresholdGreaterThan(float ratio) const; |
73 void deliver(); | 85 void deliver(); |
74 void removeObservation(IntersectionObservation&); | 86 void removeObservation(IntersectionObservation&); |
75 bool hasEntries() const { return m_entries.size(); } | 87 bool hasEntries() const { return m_entries.size(); } |
76 const HeapLinkedHashSet<WeakMember<IntersectionObservation>>& observations() | 88 const HeapLinkedHashSet<WeakMember<IntersectionObservation>>& observations() |
77 const { | 89 const { |
78 return m_observations; | 90 return m_observations; |
79 } | 91 } |
80 | 92 |
81 // Set the assumed initial state of the observed element. Note that this can | 93 // Set the assumed initial state of the observed element. Note that this can |
82 // only be set before calling observe(). | 94 // only be set before calling observe(). |
83 // TODO(skyostil): Move this setting to IntersectionObserverInit once the API | 95 // TODO(skyostil): Move this setting to IntersectionObserverInit once the API |
84 // is finalized. | 96 // is finalized. |
85 void setInitialState(InitialState); | 97 void setInitialState(InitialState); |
86 | 98 |
87 DECLARE_TRACE(); | 99 DECLARE_TRACE(); |
88 | 100 |
89 private: | 101 private: |
90 explicit IntersectionObserver(IntersectionObserverCallback&, | 102 explicit IntersectionObserver(IntersectionObserverCallback&, |
91 Node&, | 103 Element*, |
92 const Vector<Length>& rootMargin, | 104 const Vector<Length>& rootMargin, |
93 const Vector<float>& thresholds); | 105 const Vector<float>& thresholds); |
94 void clearWeakMembers(Visitor*); | 106 void clearWeakMembers(Visitor*); |
95 | 107 |
108 // Returns false if this observer has an explicit root element which has been | |
109 // deleted; true otherwise. | |
110 bool rootIsValid() const; | |
111 | |
96 Member<IntersectionObserverCallback> m_callback; | 112 Member<IntersectionObserverCallback> m_callback; |
97 WeakMember<Node> m_root; | 113 WeakMember<Element> m_root; |
98 HeapLinkedHashSet<WeakMember<IntersectionObservation>> m_observations; | 114 HeapLinkedHashSet<WeakMember<IntersectionObservation>> m_observations; |
99 HeapVector<Member<IntersectionObserverEntry>> m_entries; | 115 HeapVector<Member<IntersectionObserverEntry>> m_entries; |
100 Vector<float> m_thresholds; | 116 Vector<float> m_thresholds; |
101 Length m_topMargin; | 117 Length m_topMargin; |
102 Length m_rightMargin; | 118 Length m_rightMargin; |
103 Length m_bottomMargin; | 119 Length m_bottomMargin; |
104 Length m_leftMargin; | 120 Length m_leftMargin; |
105 InitialState m_initialState; | 121 unsigned m_rootIsImplicit : 1; |
122 unsigned m_initialState : 1; | |
106 }; | 123 }; |
107 | 124 |
108 } // namespace blink | 125 } // namespace blink |
109 | 126 |
110 #endif // IntersectionObserver_h | 127 #endif // IntersectionObserver_h |
OLD | NEW |