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/IntersectionObserverController.h" | 5 #include "core/dom/IntersectionObserverController.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 | 8 |
9 namespace blink { | 9 namespace blink { |
10 | 10 |
11 typedef HeapVector<Member<IntersectionObserver>> IntersectionObserverVector; | 11 typedef HeapVector<Member<IntersectionObserver>> IntersectionObserverVector; |
12 | 12 |
13 IntersectionObserverController::IntersectionObserverController() | 13 IntersectionObserverController* IntersectionObserverController::create(Document*
document) |
14 : m_timer(this, &IntersectionObserverController::deliverIntersectionObservat
ions) | 14 { |
| 15 IntersectionObserverController* result = new IntersectionObserverController(
document); |
| 16 result->suspendIfNeeded(); |
| 17 return result; |
| 18 } |
| 19 |
| 20 IntersectionObserverController::IntersectionObserverController(Document* documen
t) |
| 21 : ActiveDOMObject(document) |
| 22 , m_timer(this, &IntersectionObserverController::deliverIntersectionObservat
ions) |
| 23 , m_timerFiredWhileSuspended(false) |
15 { | 24 { |
16 } | 25 } |
17 | 26 |
18 IntersectionObserverController::~IntersectionObserverController() { } | 27 IntersectionObserverController::~IntersectionObserverController() { } |
19 | 28 |
20 void IntersectionObserverController::scheduleIntersectionObserverForDelivery(Int
ersectionObserver& observer) | 29 void IntersectionObserverController::scheduleIntersectionObserverForDelivery(Int
ersectionObserver& observer) |
21 { | 30 { |
22 // TODO(szager): use idle callback with a timeout | 31 // TODO(szager): use idle callback with a timeout |
23 if (!m_timer.isActive()) | 32 if (!m_timer.isActive()) |
24 m_timer.startOneShot(0, BLINK_FROM_HERE); | 33 m_timer.startOneShot(0, BLINK_FROM_HERE); |
25 m_pendingIntersectionObservers.add(&observer); | 34 m_pendingIntersectionObservers.add(&observer); |
26 } | 35 } |
27 | 36 |
| 37 void IntersectionObserverController::resume() |
| 38 { |
| 39 // If the timer fired while DOM objects were suspended, notifications might
be late, so deliver |
| 40 // them right away (rather than waiting for m_timer to fire again). |
| 41 if (m_timerFiredWhileSuspended) { |
| 42 m_timerFiredWhileSuspended = false; |
| 43 deliverIntersectionObservations(nullptr); |
| 44 } |
| 45 } |
| 46 |
28 void IntersectionObserverController::deliverIntersectionObservations(Timer<Inter
sectionObserverController>*) | 47 void IntersectionObserverController::deliverIntersectionObservations(Timer<Inter
sectionObserverController>*) |
29 { | 48 { |
| 49 if (executionContext()->activeDOMObjectsAreSuspended()) { |
| 50 m_timerFiredWhileSuspended = true; |
| 51 return; |
| 52 } |
30 IntersectionObserverVector observers; | 53 IntersectionObserverVector observers; |
31 copyToVector(m_pendingIntersectionObservers, observers); | 54 copyToVector(m_pendingIntersectionObservers, observers); |
32 m_pendingIntersectionObservers.clear(); | 55 m_pendingIntersectionObservers.clear(); |
33 for (auto& observer : observers) | 56 for (auto& observer : observers) |
34 observer->deliver(); | 57 observer->deliver(); |
35 } | 58 } |
36 | 59 |
37 void IntersectionObserverController::computeTrackedIntersectionObservations() | 60 void IntersectionObserverController::computeTrackedIntersectionObservations() |
38 { | 61 { |
39 // TODO(szager): Need to define timestamp. | 62 // TODO(szager): Need to define timestamp. |
40 double timestamp = currentTime(); | 63 double timestamp = currentTime(); |
41 for (auto& observer : m_trackedIntersectionObservers) | 64 for (auto& observer : m_trackedIntersectionObservers) { |
42 observer->computeIntersectionObservations(timestamp); | 65 observer->computeIntersectionObservations(timestamp); |
| 66 if (observer->hasEntries()) |
| 67 scheduleIntersectionObserverForDelivery(*observer); |
| 68 } |
43 } | 69 } |
44 | 70 |
45 void IntersectionObserverController::addTrackedObserver(IntersectionObserver& ob
server) | 71 void IntersectionObserverController::addTrackedObserver(IntersectionObserver& ob
server) |
46 { | 72 { |
47 m_trackedIntersectionObservers.add(&observer); | 73 m_trackedIntersectionObservers.add(&observer); |
48 } | 74 } |
49 | 75 |
50 void IntersectionObserverController::removeTrackedObserversForRoot(const Element
& root) | 76 void IntersectionObserverController::removeTrackedObserversForRoot(const Element
& root) |
51 { | 77 { |
52 HeapVector<Member<IntersectionObserver>> toRemove; | 78 HeapVector<Member<IntersectionObserver>> toRemove; |
53 for (auto& observer : m_trackedIntersectionObservers) { | 79 for (auto& observer : m_trackedIntersectionObservers) { |
54 if (observer->root() == &root) | 80 if (observer->root() == &root) |
55 toRemove.append(observer); | 81 toRemove.append(observer); |
56 } | 82 } |
57 m_trackedIntersectionObservers.removeAll(toRemove); | 83 m_trackedIntersectionObservers.removeAll(toRemove); |
58 } | 84 } |
59 | 85 |
60 DEFINE_TRACE(IntersectionObserverController) | 86 DEFINE_TRACE(IntersectionObserverController) |
61 { | 87 { |
| 88 ActiveDOMObject::trace(visitor); |
62 visitor->trace(m_trackedIntersectionObservers); | 89 visitor->trace(m_trackedIntersectionObservers); |
63 visitor->trace(m_pendingIntersectionObservers); | 90 visitor->trace(m_pendingIntersectionObservers); |
64 } | 91 } |
65 | 92 |
66 } // namespace blink | 93 } // namespace blink |
OLD | NEW |