Index: third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp |
diff --git a/third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp b/third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp |
index 055b3c64c4bf36f518927d131f177cd7b6840ed4..5b71c685889d2d4d00020ff01b8f133dbd7a66c0 100644 |
--- a/third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp |
+++ b/third_party/WebKit/Source/core/dom/IntersectionObserverController.cpp |
@@ -5,6 +5,7 @@ |
#include "core/dom/IntersectionObserverController.h" |
#include "core/dom/Document.h" |
+#include "core/dom/IdleRequestOptions.h" |
namespace blink { |
@@ -19,8 +20,8 @@ IntersectionObserverController* IntersectionObserverController::create(Document* |
IntersectionObserverController::IntersectionObserverController(Document* document) |
: ActiveDOMObject(document) |
- , m_timer(this, &IntersectionObserverController::deliverIntersectionObservations) |
- , m_timerFiredWhileSuspended(false) |
+ , m_callbackIsScheduled(false) |
+ , m_callbackFiredWhileSuspended(false) |
{ |
} |
@@ -28,28 +29,39 @@ IntersectionObserverController::~IntersectionObserverController() { } |
void IntersectionObserverController::scheduleIntersectionObserverForDelivery(IntersectionObserver& observer) |
{ |
- // TODO(szager): use idle callback with a timeout. Until we do that, there is no |
- // reliable way to write a test for takeRecords, because it's impossible to guarantee |
- // that javascript will get a chance to run before the timer fires. |
- if (!m_timer.isActive()) |
- m_timer.startOneShot(0, BLINK_FROM_HERE); |
m_pendingIntersectionObservers.add(&observer); |
+ if (m_callbackIsScheduled) |
+ return; |
+ Document* document = toDocument(getExecutionContext()); |
+ if (!document) |
+ return; |
+ m_callbackIsScheduled = true; |
+ IdleRequestOptions options; |
+ // The IntersectionObserver spec mandates that notifications be sent within 100ms. |
+ options.setTimeout(100); |
+ document->requestIdleCallback(this, options); |
} |
void IntersectionObserverController::resume() |
{ |
- // If the timer fired while DOM objects were suspended, notifications might be late, so deliver |
- // them right away (rather than waiting for m_timer to fire again). |
- if (m_timerFiredWhileSuspended) { |
- m_timerFiredWhileSuspended = false; |
- deliverIntersectionObservations(nullptr); |
+ // If the callback fired while DOM objects were suspended, notifications might be late, so deliver |
+ // them right away (rather than waiting to fire again). |
+ if (m_callbackFiredWhileSuspended) { |
+ m_callbackFiredWhileSuspended = false; |
+ deliverIntersectionObservations(); |
} |
} |
-void IntersectionObserverController::deliverIntersectionObservations(Timer<IntersectionObserverController>*) |
+void IntersectionObserverController::handleEvent(IdleDeadline*) |
+{ |
+ m_callbackIsScheduled = false; |
+ deliverIntersectionObservations(); |
+} |
+ |
+void IntersectionObserverController::deliverIntersectionObservations() |
{ |
if (getExecutionContext()->activeDOMObjectsAreSuspended()) { |
- m_timerFiredWhileSuspended = true; |
+ m_callbackFiredWhileSuspended = true; |
return; |
} |
IntersectionObserverVector observers; |
@@ -88,6 +100,7 @@ DEFINE_TRACE(IntersectionObserverController) |
visitor->trace(m_trackedIntersectionObservers); |
visitor->trace(m_pendingIntersectionObservers); |
ActiveDOMObject::trace(visitor); |
+ IdleRequestCallback::trace(visitor); |
} |
} // namespace blink |