diff --git a/dom/base/Document.cpp b/dom/base/Document.cpp index 3ed1b3a5b312..965bd7f9078c 100644 --- a/dom/base/Document.cpp +++ b/dom/base/Document.cpp @@ -16422,47 +16422,33 @@ WindowContext* Document::GetWindowContextForPageUseCounters() const { return wc; } -void Document::UpdateIntersectionObservations(TimeStamp aNowTime) { - if (mIntersectionObservers.IsEmpty()) { - return; - } - - DOMHighResTimeStamp time = 0; - if (nsPIDOMWindowInner* win = GetInnerWindow()) { - if (Performance* perf = win->GetPerformance()) { - time = perf->TimeStampToDOMHighResForRendering(aNowTime); +void Document::UpdateIntersections(TimeStamp aNowTime) { + if (!mIntersectionObservers.IsEmpty()) { + DOMHighResTimeStamp time = 0; + if (nsPIDOMWindowInner* win = GetInnerWindow()) { + if (Performance* perf = win->GetPerformance()) { + time = perf->TimeStampToDOMHighResForRendering(aNowTime); + } } - } - - const auto observers = ToTArray>>( - mIntersectionObservers); - for (const auto& observer : observers) { - if (observer) { + for (DOMIntersectionObserver* observer : mIntersectionObservers) { observer->Update(*this, time); } + Dispatch(NewRunnableMethod("Document::NotifyIntersectionObservers", this, + &Document::NotifyIntersectionObservers)); } -} - -void Document::ScheduleIntersectionObserverNotification() { - if (mIntersectionObservers.IsEmpty()) { - return; - } - MOZ_RELEASE_ASSERT(NS_IsMainThread()); - nsCOMPtr notification = - NewRunnableMethod("Document::NotifyIntersectionObservers", this, - &Document::NotifyIntersectionObservers); - Dispatch(notification.forget()); + EnumerateSubDocuments([aNowTime](Document& aDoc) { + aDoc.UpdateIntersections(aNowTime); + return CallState::Continue; + }); } void Document::NotifyIntersectionObservers() { const auto observers = ToTArray>>( mIntersectionObservers); for (const auto& observer : observers) { - if (observer) { - // MOZ_KnownLive because the 'observers' array guarantees to keep it - // alive. - MOZ_KnownLive(observer)->Notify(); - } + // MOZ_KnownLive because the 'observers' array guarantees to keep it + // alive. + MOZ_KnownLive(observer)->Notify(); } } diff --git a/dom/base/Document.h b/dom/base/Document.h index 0b0d0ca3d0fd..e919f19be08d 100644 --- a/dom/base/Document.h +++ b/dom/base/Document.h @@ -3709,8 +3709,9 @@ class Document : public nsINode, return !mIntersectionObservers.IsEmpty(); } - void UpdateIntersectionObservations(TimeStamp aNowTime); - void ScheduleIntersectionObserverNotification(); + // Update intersection observers in this document and all + // same-process subdocuments. + void UpdateIntersections(TimeStamp aNowTime); MOZ_CAN_RUN_SCRIPT void NotifyIntersectionObservers(); DOMIntersectionObserver* GetLazyLoadObserver() { return mLazyLoadObserver; } diff --git a/layout/base/nsRefreshDriver.cpp b/layout/base/nsRefreshDriver.cpp index 7885d6b8dbe3..0ec39783c265 100644 --- a/layout/base/nsRefreshDriver.cpp +++ b/layout/base/nsRefreshDriver.cpp @@ -2228,23 +2228,7 @@ void nsRefreshDriver::RunFullscreenSteps() { void nsRefreshDriver::UpdateIntersectionObservations(TimeStamp aNowTime) { AUTO_PROFILER_LABEL_RELEVANT_FOR_JS("Compute intersections", LAYOUT); - - AutoTArray, 32> documents; - - if (mPresContext->Document()->HasIntersectionObservers()) { - documents.AppendElement(mPresContext->Document()); - } - - mPresContext->Document()->CollectDescendantDocuments( - documents, [](const Document* document) -> bool { - return document->HasIntersectionObservers(); - }); - - for (const auto& doc : documents) { - doc->UpdateIntersectionObservations(aNowTime); - doc->ScheduleIntersectionObserverNotification(); - } - + mPresContext->Document()->UpdateIntersections(aNowTime); mNeedToUpdateIntersectionObservations = false; }