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 #include "core/dom/ElementIntersectionObserverData.h" | 5 #include "core/dom/NodeIntersectionObserverData.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/Element.h" | |
9 #include "core/dom/IntersectionObservation.h" | 8 #include "core/dom/IntersectionObservation.h" |
10 #include "core/dom/IntersectionObserver.h" | 9 #include "core/dom/IntersectionObserver.h" |
11 #include "core/dom/IntersectionObserverController.h" | 10 #include "core/dom/IntersectionObserverController.h" |
12 | 11 |
13 namespace blink { | 12 namespace blink { |
14 | 13 |
15 ElementIntersectionObserverData::ElementIntersectionObserverData() { } | 14 NodeIntersectionObserverData::NodeIntersectionObserverData() { } |
16 | 15 |
17 ElementIntersectionObserverData::~ElementIntersectionObserverData() { } | 16 NodeIntersectionObserverData::~NodeIntersectionObserverData() { } |
18 | 17 |
19 bool ElementIntersectionObserverData::hasIntersectionObserver() const | 18 bool NodeIntersectionObserverData::hasIntersectionObserver() const |
20 { | 19 { |
21 return !m_intersectionObservers.isEmpty(); | 20 return !m_intersectionObservers.isEmpty(); |
22 } | 21 } |
23 | 22 |
24 bool ElementIntersectionObserverData::hasIntersectionObservation() const | 23 bool NodeIntersectionObserverData::hasIntersectionObservation() const |
25 { | 24 { |
26 return !m_intersectionObservations.isEmpty(); | 25 return !m_intersectionObservations.isEmpty(); |
27 } | 26 } |
28 | 27 |
29 IntersectionObservation* ElementIntersectionObserverData::getObservationFor(Inte
rsectionObserver& observer) | 28 IntersectionObservation* NodeIntersectionObserverData::getObservationFor(Interse
ctionObserver& observer) |
30 { | 29 { |
31 auto i = m_intersectionObservations.find(&observer); | 30 auto i = m_intersectionObservations.find(&observer); |
32 if (i == m_intersectionObservations.end()) | 31 if (i == m_intersectionObservations.end()) |
33 return nullptr; | 32 return nullptr; |
34 return i->value; | 33 return i->value; |
35 } | 34 } |
36 | 35 |
37 void ElementIntersectionObserverData::addObservation(IntersectionObservation& ob
servation) | 36 void NodeIntersectionObserverData::addObservation(IntersectionObservation& obser
vation) |
38 { | 37 { |
39 m_intersectionObservations.add(&observation.observer(), &observation); | 38 m_intersectionObservations.add(&observation.observer(), &observation); |
40 } | 39 } |
41 | 40 |
42 void ElementIntersectionObserverData::removeObservation(IntersectionObserver& ob
server) | 41 void NodeIntersectionObserverData::removeObservation(IntersectionObserver& obser
ver) |
43 { | 42 { |
44 m_intersectionObservations.remove(&observer); | 43 m_intersectionObservations.remove(&observer); |
45 } | 44 } |
46 | 45 |
47 void ElementIntersectionObserverData::activateValidIntersectionObservers(Element
& element) | 46 void NodeIntersectionObserverData::activateValidIntersectionObservers(Node& node
) |
48 { | 47 { |
49 IntersectionObserverController& controller = element.document().ensureInters
ectionObserverController(); | 48 IntersectionObserverController& controller = node.document().ensureIntersect
ionObserverController(); |
| 49 // Activate observers for which node is root. |
50 for (auto& observer : m_intersectionObservers) { | 50 for (auto& observer : m_intersectionObservers) { |
51 controller.addTrackedObserver(*observer); | 51 controller.addTrackedObserver(*observer); |
52 observer->setActive(true); | 52 observer->setActive(true); |
53 } | 53 } |
| 54 // A document can be root, but not target. |
| 55 if (node.isDocumentNode()) |
| 56 return; |
| 57 // Active observers for which node is target. |
54 for (auto& observation : m_intersectionObservations) | 58 for (auto& observation : m_intersectionObservations) |
55 observation.value->setActive(observation.key->isDescendantOfRoot(&elemen
t)); | 59 observation.value->setActive(observation.key->isDescendantOfRoot(&toElem
ent(node))); |
56 } | 60 } |
57 | 61 |
58 void ElementIntersectionObserverData::deactivateAllIntersectionObservers(Element
& element) | 62 void NodeIntersectionObserverData::deactivateAllIntersectionObservers(Node& node
) |
59 { | 63 { |
60 element.document().ensureIntersectionObserverController().removeTrackedObser
versForRoot(element); | 64 node.document().ensureIntersectionObserverController().removeTrackedObserver
sForRoot(node); |
61 for (auto& observer : m_intersectionObservers) | 65 for (auto& observer : m_intersectionObservers) |
62 observer->setActive(false); | 66 observer->setActive(false); |
63 for (auto& observation : m_intersectionObservations) | 67 for (auto& observation : m_intersectionObservations) |
64 observation.value->setActive(false); | 68 observation.value->setActive(false); |
65 } | 69 } |
66 | 70 |
67 #if !ENABLE(OILPAN) | 71 #if !ENABLE(OILPAN) |
68 void ElementIntersectionObserverData::dispose() | 72 void NodeIntersectionObserverData::dispose() |
69 { | 73 { |
70 HeapVector<Member<IntersectionObserver>> observersToDisconnect; | 74 HeapVector<Member<IntersectionObserver>> observersToDisconnect; |
71 copyToVector(m_intersectionObservers, observersToDisconnect); | 75 copyToVector(m_intersectionObservers, observersToDisconnect); |
72 for (auto& observer : observersToDisconnect) | 76 for (auto& observer : observersToDisconnect) |
73 observer->disconnect(); | 77 observer->disconnect(); |
74 ASSERT(m_intersectionObservers.isEmpty()); | 78 ASSERT(m_intersectionObservers.isEmpty()); |
75 } | 79 } |
76 #endif | 80 #endif |
77 | 81 |
78 WeakPtrWillBeRawPtr<Element> ElementIntersectionObserverData::createWeakPtr(Elem
ent* element) | 82 WeakPtrWillBeRawPtr<Node> NodeIntersectionObserverData::createWeakPtr(Node* node
) |
79 { | 83 { |
80 #if ENABLE(OILPAN) | 84 #if ENABLE(OILPAN) |
81 return element; | 85 return node; |
82 #else | 86 #else |
83 if (!m_weakPointerFactory) | 87 if (!m_weakPointerFactory) |
84 m_weakPointerFactory = adoptPtrWillBeNoop(new WeakPtrFactory<Element>(el
ement)); | 88 m_weakPointerFactory = adoptPtrWillBeNoop(new WeakPtrFactory<Node>(node)
); |
85 return m_weakPointerFactory->createWeakPtr(); | 89 WeakPtr<Node> result = m_weakPointerFactory->createWeakPtr(); |
| 90 ASSERT(result.get() == node); |
| 91 return result; |
86 #endif | 92 #endif |
87 } | 93 } |
88 | 94 |
89 DEFINE_TRACE(ElementIntersectionObserverData) | 95 DEFINE_TRACE(NodeIntersectionObserverData) |
90 { | 96 { |
91 visitor->trace(m_intersectionObservers); | 97 visitor->trace(m_intersectionObservers); |
92 visitor->trace(m_intersectionObservations); | 98 visitor->trace(m_intersectionObservations); |
93 } | 99 } |
94 | 100 |
95 } // namespace blink | 101 } // namespace blink |
OLD | NEW |