diff --git a/dom/animation/ScrollTimeline.cpp b/dom/animation/ScrollTimeline.cpp index fc4561aff631..02bb8aa83f1b 100644 --- a/dom/animation/ScrollTimeline.cpp +++ b/dom/animation/ScrollTimeline.cpp @@ -61,6 +61,25 @@ ScrollTimeline::ScrollTimeline(Document* aDocument, const Scroller& aScroller, PlaybackDirection::Alternate, FillMode::Both); } +/* static */ +already_AddRefed ScrollTimeline::GetOrCreateScrollTimeline( + Document* aDocument, const Scroller& aScroller, + const StyleScrollAxis& aAxis) { + MOZ_ASSERT(aScroller); + + RefPtr timeline; + auto* set = + ScrollTimelineSet::GetOrCreateScrollTimelineSet(aScroller.mElement); + auto p = set->LookupForAdd(aAxis); + if (!p) { + timeline = new ScrollTimeline(aDocument, aScroller, aAxis); + set->Add(p, aAxis, timeline); + } else { + timeline = p->value(); + } + return timeline.forget(); +} + static StyleScrollAxis ToStyleScrollAxis( const StyleScrollDirection aDirection) { switch (aDirection) { @@ -83,6 +102,7 @@ static StyleScrollAxis ToStyleScrollAxis( return StyleScrollAxis::Block; } +/* static */ already_AddRefed ScrollTimeline::FromRule( const RawServoScrollTimelineRule& aRule, Document* aDocument, const NonOwningAnimationTarget& aTarget) { @@ -96,18 +116,8 @@ already_AddRefed ScrollTimeline::FromRule( StyleScrollAxis axis = ToStyleScrollAxis(Servo_ScrollTimelineRule_GetOrientation(&aRule)); - RefPtr timeline; auto autoScroller = Scroller::Root(aTarget.mElement->OwnerDoc()); - auto* set = - ScrollTimelineSet::GetOrCreateScrollTimelineSet(autoScroller.mElement); - auto p = set->LookupForAdd(axis); - if (!p) { - timeline = new ScrollTimeline(aDocument, autoScroller, axis); - set->Add(p, axis, timeline); - } else { - timeline = p->value(); - } - return timeline.forget(); + return GetOrCreateScrollTimeline(aDocument, autoScroller, axis); } /* static */ @@ -135,18 +145,7 @@ already_AddRefed ScrollTimeline::FromAnonymousScroll( scroller = Scroller::Nearest(curr ? curr : root); } } - - RefPtr timeline; - auto* set = - ScrollTimelineSet::GetOrCreateScrollTimelineSet(scroller.mElement); - auto p = set->LookupForAdd(aAxis); - if (!p) { - timeline = new ScrollTimeline(aDocument, scroller, aAxis); - set->Add(p, aAxis, timeline); - } else { - timeline = p->value(); - } - return timeline.forget(); + return GetOrCreateScrollTimeline(aDocument, scroller, aAxis); } /* static*/ already_AddRefed ScrollTimeline::FromNamedScroll( @@ -202,18 +201,7 @@ already_AddRefed ScrollTimeline::FromAnonymousScroll( return nullptr; } Scroller scroller = Scroller::Named(result); - - RefPtr timeline; - auto* set = - ScrollTimelineSet::GetOrCreateScrollTimelineSet(scroller.mElement); - auto p = set->LookupForAdd(axis); - if (!p) { - timeline = new ScrollTimeline(aDocument, scroller, axis); - set->Add(p, axis, timeline); - } else { - timeline = p->value(); - } - return timeline.forget(); + return GetOrCreateScrollTimeline(aDocument, scroller, axis); } Nullable ScrollTimeline::GetCurrentTimeAsDuration() const { diff --git a/dom/animation/ScrollTimeline.h b/dom/animation/ScrollTimeline.h index 5aaa19f78ca2..f71d728ae339 100644 --- a/dom/animation/ScrollTimeline.h +++ b/dom/animation/ScrollTimeline.h @@ -186,6 +186,10 @@ class ScrollTimeline final : public AnimationTimeline { ScrollTimeline(Document* aDocument, const Scroller& aScroller, StyleScrollAxis aAxis); + static already_AddRefed GetOrCreateScrollTimeline( + Document* aDocument, const Scroller& aScroller, + const StyleScrollAxis& aAxis); + // Note: This function is required to be idempotent, as it can be called from // both cycleCollection::Unlink() and ~ScrollTimeline(). When modifying this // function, be sure to preserve this property.