Chromium Code Reviews| 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 #include "core/dom/IdleRequestOptions.h" | |
| 8 | 9 |
| 9 namespace blink { | 10 namespace blink { |
| 10 | 11 |
| 11 typedef HeapVector<Member<IntersectionObserver>> IntersectionObserverVector; | 12 typedef HeapVector<Member<IntersectionObserver>> IntersectionObserverVector; |
| 12 | 13 |
| 13 IntersectionObserverController* IntersectionObserverController::create(Document* document) | 14 IntersectionObserverController* IntersectionObserverController::create(Document* document) |
| 14 { | 15 { |
| 15 IntersectionObserverController* result = new IntersectionObserverController( document); | 16 IntersectionObserverController* result = new IntersectionObserverController( document); |
| 16 result->suspendIfNeeded(); | 17 result->suspendIfNeeded(); |
| 17 return result; | 18 return result; |
| 18 } | 19 } |
| 19 | 20 |
| 20 IntersectionObserverController::IntersectionObserverController(Document* documen t) | 21 IntersectionObserverController::IntersectionObserverController(Document* documen t) |
| 21 : ActiveDOMObject(document) | 22 : ActiveDOMObject(document) |
| 22 , m_timer(this, &IntersectionObserverController::deliverIntersectionObservat ions) | 23 , m_timerIsScheduled(false) |
|
Sami
2016/03/08 17:32:42
nit: s/timer/callback/ (here and below)?
szager1
2016/03/08 18:51:32
Done.
| |
| 23 , m_timerFiredWhileSuspended(false) | 24 , m_timerFiredWhileSuspended(false) |
| 24 { | 25 { |
| 25 } | 26 } |
| 26 | 27 |
| 27 IntersectionObserverController::~IntersectionObserverController() { } | 28 IntersectionObserverController::~IntersectionObserverController() { } |
| 28 | 29 |
| 29 void IntersectionObserverController::scheduleIntersectionObserverForDelivery(Int ersectionObserver& observer) | 30 void IntersectionObserverController::scheduleIntersectionObserverForDelivery(Int ersectionObserver& observer) |
| 30 { | 31 { |
| 31 // TODO(szager): use idle callback with a timeout. Until we do that, there is no | 32 // TODO(szager): use idle callback with a timeout. Until we do that, there is no |
|
Sami
2016/03/08 17:32:42
Did you mean to remove this TODO?
szager1
2016/03/08 18:51:32
Done.
| |
| 32 // reliable way to write a test for takeRecords, because it's impossible to guarantee | 33 // reliable way to write a test for takeRecords, because it's impossible to guarantee |
| 33 // that javascript will get a chance to run before the timer fires. | 34 // that javascript will get a chance to run before the timer fires. |
| 34 if (!m_timer.isActive()) | |
| 35 m_timer.startOneShot(0, BLINK_FROM_HERE); | |
| 36 m_pendingIntersectionObservers.add(&observer); | 35 m_pendingIntersectionObservers.add(&observer); |
| 36 if (m_timerIsScheduled) | |
| 37 return; | |
| 38 Document* document = toDocument(executionContext()); | |
| 39 if (!document) | |
| 40 return; | |
| 41 m_timerIsScheduled = true; | |
| 42 IdleRequestOptions options; | |
| 43 options.setTimeout(100); | |
|
haraken
2016/03/08 03:44:03
We want to remove hard-coded timers in the future
szager1
2016/03/08 18:51:32
100ms is required by the IntersectionObserver spec
haraken
2016/03/08 23:05:58
Then maybe worth adding a comment and mention that
szager1
2016/03/08 23:12:02
Done.
| |
| 44 document->requestIdleCallback(this, options); | |
| 37 } | 45 } |
| 38 | 46 |
| 39 void IntersectionObserverController::resume() | 47 void IntersectionObserverController::resume() |
| 40 { | 48 { |
| 41 // If the timer fired while DOM objects were suspended, notifications might be late, so deliver | 49 // If the timer fired while DOM objects were suspended, notifications might be late, so deliver |
| 42 // them right away (rather than waiting for m_timer to fire again). | 50 // them right away (rather than waiting to fire again). |
| 43 if (m_timerFiredWhileSuspended) { | 51 if (m_timerFiredWhileSuspended) { |
| 44 m_timerFiredWhileSuspended = false; | 52 m_timerFiredWhileSuspended = false; |
| 45 deliverIntersectionObservations(nullptr); | 53 deliverIntersectionObservations(); |
| 46 } | 54 } |
| 47 } | 55 } |
| 48 | 56 |
| 49 void IntersectionObserverController::deliverIntersectionObservations(Timer<Inter sectionObserverController>*) | 57 void IntersectionObserverController::handleEvent(IdleDeadline*) |
| 58 { | |
| 59 m_timerIsScheduled = false; | |
| 60 deliverIntersectionObservations(); | |
| 61 } | |
| 62 | |
| 63 void IntersectionObserverController::deliverIntersectionObservations() | |
| 50 { | 64 { |
| 51 if (executionContext()->activeDOMObjectsAreSuspended()) { | 65 if (executionContext()->activeDOMObjectsAreSuspended()) { |
| 52 m_timerFiredWhileSuspended = true; | 66 m_timerFiredWhileSuspended = true; |
| 53 return; | 67 return; |
| 54 } | 68 } |
| 55 IntersectionObserverVector observers; | 69 IntersectionObserverVector observers; |
| 56 copyToVector(m_pendingIntersectionObservers, observers); | 70 copyToVector(m_pendingIntersectionObservers, observers); |
| 57 m_pendingIntersectionObservers.clear(); | 71 m_pendingIntersectionObservers.clear(); |
| 58 for (auto& observer : observers) | 72 for (auto& observer : observers) |
| 59 observer->deliver(); | 73 observer->deliver(); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 81 toRemove.append(observer); | 95 toRemove.append(observer); |
| 82 } | 96 } |
| 83 m_trackedIntersectionObservers.removeAll(toRemove); | 97 m_trackedIntersectionObservers.removeAll(toRemove); |
| 84 } | 98 } |
| 85 | 99 |
| 86 DEFINE_TRACE(IntersectionObserverController) | 100 DEFINE_TRACE(IntersectionObserverController) |
| 87 { | 101 { |
| 88 visitor->trace(m_trackedIntersectionObservers); | 102 visitor->trace(m_trackedIntersectionObservers); |
| 89 visitor->trace(m_pendingIntersectionObservers); | 103 visitor->trace(m_pendingIntersectionObservers); |
| 90 ActiveDOMObject::trace(visitor); | 104 ActiveDOMObject::trace(visitor); |
| 105 IdleRequestCallback::trace(visitor); | |
| 91 } | 106 } |
| 92 | 107 |
| 93 } // namespace blink | 108 } // namespace blink |
| OLD | NEW |