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 bool rootIsImplicit() const { return m_rootIsImplicit; } |
skobes
2016/12/08 01:08:56
Add comments explaining what this means.
szager1
2016/12/08 19:39:58
Done.
| |
67 Document& trackingDocument() const; | |
skobes
2016/12/08 01:08:56
The observer does the tracking, right? So this sh
szager1
2016/12/08 19:39:58
I added a comment to clarify this: "this is the do
| |
68 LayoutObject* rootLayoutObject() const; | |
66 const Length& topMargin() const { return m_topMargin; } | 69 const Length& topMargin() const { return m_topMargin; } |
67 const Length& rightMargin() const { return m_rightMargin; } | 70 const Length& rightMargin() const { return m_rightMargin; } |
68 const Length& bottomMargin() const { return m_bottomMargin; } | 71 const Length& bottomMargin() const { return m_bottomMargin; } |
69 const Length& leftMargin() const { return m_leftMargin; } | 72 const Length& leftMargin() const { return m_leftMargin; } |
70 void computeIntersectionObservations(); | 73 void computeIntersectionObservations(); |
71 void enqueueIntersectionObserverEntry(IntersectionObserverEntry&); | 74 void enqueueIntersectionObserverEntry(IntersectionObserverEntry&); |
72 unsigned firstThresholdGreaterThan(float ratio) const; | 75 unsigned firstThresholdGreaterThan(float ratio) const; |
73 void deliver(); | 76 void deliver(); |
74 void removeObservation(IntersectionObservation&); | 77 void removeObservation(IntersectionObservation&); |
75 bool hasEntries() const { return m_entries.size(); } | 78 bool hasEntries() const { return m_entries.size(); } |
76 const HeapLinkedHashSet<WeakMember<IntersectionObservation>>& observations() | 79 const HeapLinkedHashSet<WeakMember<IntersectionObservation>>& observations() |
77 const { | 80 const { |
78 return m_observations; | 81 return m_observations; |
79 } | 82 } |
80 | 83 |
81 // Set the assumed initial state of the observed element. Note that this can | 84 // Set the assumed initial state of the observed element. Note that this can |
82 // only be set before calling observe(). | 85 // only be set before calling observe(). |
83 // TODO(skyostil): Move this setting to IntersectionObserverInit once the API | 86 // TODO(skyostil): Move this setting to IntersectionObserverInit once the API |
84 // is finalized. | 87 // is finalized. |
85 void setInitialState(InitialState); | 88 void setInitialState(InitialState); |
86 | 89 |
87 DECLARE_TRACE(); | 90 DECLARE_TRACE(); |
88 | 91 |
89 private: | 92 private: |
90 explicit IntersectionObserver(IntersectionObserverCallback&, | 93 explicit IntersectionObserver(IntersectionObserverCallback&, |
91 Node&, | 94 Element*, |
92 const Vector<Length>& rootMargin, | 95 const Vector<Length>& rootMargin, |
93 const Vector<float>& thresholds); | 96 const Vector<float>& thresholds); |
94 void clearWeakMembers(Visitor*); | 97 void clearWeakMembers(Visitor*); |
95 | 98 |
99 // Did the explicit root Element get deleted? | |
skobes
2016/12/08 01:08:56
The answer to this question is the inverse of the
szager1
2016/12/08 19:39:58
Done.
| |
100 bool rootIsValid() const; | |
101 | |
96 Member<IntersectionObserverCallback> m_callback; | 102 Member<IntersectionObserverCallback> m_callback; |
97 WeakMember<Node> m_root; | 103 WeakMember<Element> m_root; |
skobes
2016/12/08 01:08:56
What happens if we want to track a Document in an
szager1
2016/12/08 19:39:58
The only choices are to track an Element, or to tr
skobes
2016/12/08 20:02:43
So which element is m_root set to if we want to tr
szager1
2016/12/08 20:12:11
If you read through the code paths that get to the
| |
98 HeapLinkedHashSet<WeakMember<IntersectionObservation>> m_observations; | 104 HeapLinkedHashSet<WeakMember<IntersectionObservation>> m_observations; |
99 HeapVector<Member<IntersectionObserverEntry>> m_entries; | 105 HeapVector<Member<IntersectionObserverEntry>> m_entries; |
100 Vector<float> m_thresholds; | 106 Vector<float> m_thresholds; |
101 Length m_topMargin; | 107 Length m_topMargin; |
102 Length m_rightMargin; | 108 Length m_rightMargin; |
103 Length m_bottomMargin; | 109 Length m_bottomMargin; |
104 Length m_leftMargin; | 110 Length m_leftMargin; |
105 InitialState m_initialState; | 111 unsigned m_rootIsImplicit : 1; |
112 unsigned m_initialState : 1; | |
skobes
2016/12/08 01:08:56
Why is the type not InitialState?
szager1
2016/12/08 19:39:58
Just for efficient struct packing. I think the en
skobes
2016/12/08 20:02:43
C++ allows enum bitfields, so you should be able t
szager1
2016/12/08 20:12:11
That gives a presubmit error:
$ git cl uplodad
Ru
skobes
2016/12/08 21:12:21
"InitialiState" looks like a typo.
szager1
2016/12/08 21:30:58
Same error.
| |
106 }; | 113 }; |
107 | 114 |
108 } // namespace blink | 115 } // namespace blink |
109 | 116 |
110 #endif // IntersectionObserver_h | 117 #endif // IntersectionObserver_h |
OLD | NEW |