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