1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "DocumentTimeline.h"
8 #include "mozilla/dom/DocumentInlines.h"
9 #include "mozilla/dom/DocumentTimelineBinding.h"
10 #include "AnimationUtils.h"
11 #include "nsContentUtils.h"
12 #include "nsDOMMutationObserver.h"
13 #include "nsDOMNavigationTiming.h"
14 #include "nsPresContext.h"
15 #include "nsRefreshDriver.h"
16 
17 namespace mozilla::dom {
18 
19 NS_IMPL_CYCLE_COLLECTION_CLASS(DocumentTimeline)
20 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DocumentTimeline,
21                                                 AnimationTimeline)
22   tmp->UnregisterFromRefreshDriver();
23   if (tmp->isInList()) {
24     tmp->remove();
25   }
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)26   NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
27 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
28 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DocumentTimeline,
29                                                   AnimationTimeline)
30   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
32 
33 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(DocumentTimeline,
34                                                AnimationTimeline)
35 NS_IMPL_CYCLE_COLLECTION_TRACE_END
36 
37 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DocumentTimeline)
38 NS_INTERFACE_MAP_END_INHERITING(AnimationTimeline)
39 
40 NS_IMPL_ADDREF_INHERITED(DocumentTimeline, AnimationTimeline)
41 NS_IMPL_RELEASE_INHERITED(DocumentTimeline, AnimationTimeline)
42 
43 DocumentTimeline::DocumentTimeline(Document* aDocument,
44                                    const TimeDuration& aOriginTime)
45     : AnimationTimeline(aDocument->GetParentObject()),
46       mDocument(aDocument),
47       mIsObservingRefreshDriver(false),
48       mOriginTime(aOriginTime) {
49   if (mDocument) {
50     mDocument->Timelines().insertBack(this);
51   }
52 }
53 
~DocumentTimeline()54 DocumentTimeline::~DocumentTimeline() {
55   MOZ_ASSERT(!mIsObservingRefreshDriver,
56              "Timeline should have disassociated"
57              " from the refresh driver before being destroyed");
58   if (isInList()) {
59     remove();
60   }
61 }
62 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)63 JSObject* DocumentTimeline::WrapObject(JSContext* aCx,
64                                        JS::Handle<JSObject*> aGivenProto) {
65   return DocumentTimeline_Binding::Wrap(aCx, this, aGivenProto);
66 }
67 
68 /* static */
Constructor(const GlobalObject & aGlobal,const DocumentTimelineOptions & aOptions,ErrorResult & aRv)69 already_AddRefed<DocumentTimeline> DocumentTimeline::Constructor(
70     const GlobalObject& aGlobal, const DocumentTimelineOptions& aOptions,
71     ErrorResult& aRv) {
72   Document* doc = AnimationUtils::GetCurrentRealmDocument(aGlobal.Context());
73   if (!doc) {
74     aRv.Throw(NS_ERROR_FAILURE);
75     return nullptr;
76   }
77   TimeDuration originTime =
78       TimeDuration::FromMilliseconds(aOptions.mOriginTime);
79 
80   if (originTime == TimeDuration::Forever() ||
81       originTime == -TimeDuration::Forever()) {
82     aRv.ThrowTypeError<dom::MSG_TIME_VALUE_OUT_OF_RANGE>("Origin time");
83     return nullptr;
84   }
85   RefPtr<DocumentTimeline> timeline = new DocumentTimeline(doc, originTime);
86 
87   return timeline.forget();
88 }
89 
GetCurrentTimeAsDuration() const90 Nullable<TimeDuration> DocumentTimeline::GetCurrentTimeAsDuration() const {
91   return ToTimelineTime(GetCurrentTimeStamp());
92 }
93 
TracksWallclockTime() const94 bool DocumentTimeline::TracksWallclockTime() const {
95   nsRefreshDriver* refreshDriver = GetRefreshDriver();
96   return !refreshDriver || !refreshDriver->IsTestControllingRefreshesEnabled();
97 }
98 
GetCurrentTimeStamp() const99 TimeStamp DocumentTimeline::GetCurrentTimeStamp() const {
100   nsRefreshDriver* refreshDriver = GetRefreshDriver();
101   TimeStamp refreshTime =
102       refreshDriver ? refreshDriver->MostRecentRefresh() : TimeStamp();
103 
104   // Always return the same object to benefit from return-value optimization.
105   TimeStamp result =
106       !refreshTime.IsNull() ? refreshTime : mLastRefreshDriverTime;
107 
108   nsDOMNavigationTiming* timing = mDocument->GetNavigationTiming();
109   // If we don't have a refresh driver and we've never had one use the
110   // timeline's zero time.
111   // In addition, it's possible that our refresh driver's timestamp is behind
112   // from the navigation start time because the refresh driver timestamp is
113   // sent through an IPC call whereas the navigation time is set by calling
114   // TimeStamp::Now() directly. In such cases we also use the timeline's zero
115   // time.
116   if (timing &&
117       (result.IsNull() || result < timing->GetNavigationStartTimeStamp())) {
118     result = timing->GetNavigationStartTimeStamp();
119     // Also, let this time represent the current refresh time. This way
120     // we'll save it as the last refresh time and skip looking up
121     // navigation start time each time.
122     refreshTime = result;
123   }
124 
125   if (!refreshTime.IsNull()) {
126     mLastRefreshDriverTime = refreshTime;
127   }
128 
129   return result;
130 }
131 
ToTimelineTime(const TimeStamp & aTimeStamp) const132 Nullable<TimeDuration> DocumentTimeline::ToTimelineTime(
133     const TimeStamp& aTimeStamp) const {
134   Nullable<TimeDuration> result;  // Initializes to null
135   if (aTimeStamp.IsNull()) {
136     return result;
137   }
138 
139   nsDOMNavigationTiming* timing = mDocument->GetNavigationTiming();
140   if (MOZ_UNLIKELY(!timing)) {
141     return result;
142   }
143 
144   result.SetValue(aTimeStamp - timing->GetNavigationStartTimeStamp() -
145                   mOriginTime);
146   return result;
147 }
148 
NotifyAnimationUpdated(Animation & aAnimation)149 void DocumentTimeline::NotifyAnimationUpdated(Animation& aAnimation) {
150   AnimationTimeline::NotifyAnimationUpdated(aAnimation);
151 
152   if (!mIsObservingRefreshDriver) {
153     nsRefreshDriver* refreshDriver = GetRefreshDriver();
154     if (refreshDriver) {
155       MOZ_ASSERT(isInList(),
156                  "We should not register with the refresh driver if we are not"
157                  " in the document's list of timelines");
158 
159       ObserveRefreshDriver(refreshDriver);
160     }
161   }
162 }
163 
MostRecentRefreshTimeUpdated()164 void DocumentTimeline::MostRecentRefreshTimeUpdated() {
165   MOZ_ASSERT(mIsObservingRefreshDriver);
166   MOZ_ASSERT(GetRefreshDriver(),
167              "Should be able to reach refresh driver from within WillRefresh");
168 
169   nsAutoAnimationMutationBatch mb(mDocument);
170 
171   bool ticked = Tick();
172   if (!ticked) {
173     // We already assert that GetRefreshDriver() is non-null at the beginning
174     // of this function but we check it again here to be sure that ticking
175     // animations does not have any side effects that cause us to lose the
176     // connection with the refresh driver, such as triggering the destruction
177     // of mDocument's PresShell.
178     MOZ_ASSERT(GetRefreshDriver(),
179                "Refresh driver should still be valid at end of WillRefresh");
180     UnregisterFromRefreshDriver();
181   }
182 }
183 
WillRefresh(mozilla::TimeStamp aTime)184 void DocumentTimeline::WillRefresh(mozilla::TimeStamp aTime) {
185   MostRecentRefreshTimeUpdated();
186 }
187 
NotifyTimerAdjusted(TimeStamp aTime)188 void DocumentTimeline::NotifyTimerAdjusted(TimeStamp aTime) {
189   MostRecentRefreshTimeUpdated();
190 }
191 
ObserveRefreshDriver(nsRefreshDriver * aDriver)192 void DocumentTimeline::ObserveRefreshDriver(nsRefreshDriver* aDriver) {
193   MOZ_ASSERT(!mIsObservingRefreshDriver);
194   // Set the mIsObservingRefreshDriver flag before calling AddRefreshObserver
195   // since it might end up calling NotifyTimerAdjusted which calls
196   // MostRecentRefreshTimeUpdated which has an assertion for
197   // mIsObserveingRefreshDriver check.
198   mIsObservingRefreshDriver = true;
199   aDriver->AddRefreshObserver(this, FlushType::Style,
200                               "DocumentTimeline animations");
201   aDriver->AddTimerAdjustmentObserver(this);
202 }
203 
NotifyRefreshDriverCreated(nsRefreshDriver * aDriver)204 void DocumentTimeline::NotifyRefreshDriverCreated(nsRefreshDriver* aDriver) {
205   MOZ_ASSERT(!mIsObservingRefreshDriver,
206              "Timeline should not be observing the refresh driver before"
207              " it is created");
208 
209   if (!mAnimationOrder.isEmpty()) {
210     MOZ_ASSERT(isInList(),
211                "We should not register with the refresh driver if we are not"
212                " in the document's list of timelines");
213     ObserveRefreshDriver(aDriver);
214     // Although we have started observing the refresh driver, it's possible we
215     // could perform a paint before the first refresh driver tick happens.  To
216     // ensure we're in a consistent state in that case we run the first tick
217     // manually.
218     MostRecentRefreshTimeUpdated();
219   }
220 }
221 
DisconnectRefreshDriver(nsRefreshDriver * aDriver)222 void DocumentTimeline::DisconnectRefreshDriver(nsRefreshDriver* aDriver) {
223   MOZ_ASSERT(mIsObservingRefreshDriver);
224 
225   aDriver->RemoveRefreshObserver(this, FlushType::Style);
226   aDriver->RemoveTimerAdjustmentObserver(this);
227   mIsObservingRefreshDriver = false;
228 }
229 
NotifyRefreshDriverDestroying(nsRefreshDriver * aDriver)230 void DocumentTimeline::NotifyRefreshDriverDestroying(nsRefreshDriver* aDriver) {
231   if (!mIsObservingRefreshDriver) {
232     return;
233   }
234 
235   DisconnectRefreshDriver(aDriver);
236 }
237 
RemoveAnimation(Animation * aAnimation)238 void DocumentTimeline::RemoveAnimation(Animation* aAnimation) {
239   AnimationTimeline::RemoveAnimation(aAnimation);
240 
241   if (mIsObservingRefreshDriver && mAnimations.IsEmpty()) {
242     UnregisterFromRefreshDriver();
243   }
244 }
245 
ToTimeStamp(const TimeDuration & aTimeDuration) const246 TimeStamp DocumentTimeline::ToTimeStamp(
247     const TimeDuration& aTimeDuration) const {
248   TimeStamp result;
249   nsDOMNavigationTiming* timing = mDocument->GetNavigationTiming();
250   if (MOZ_UNLIKELY(!timing)) {
251     return result;
252   }
253 
254   result =
255       timing->GetNavigationStartTimeStamp() + (aTimeDuration + mOriginTime);
256   return result;
257 }
258 
GetRefreshDriver() const259 nsRefreshDriver* DocumentTimeline::GetRefreshDriver() const {
260   nsPresContext* presContext = mDocument->GetPresContext();
261   if (MOZ_UNLIKELY(!presContext)) {
262     return nullptr;
263   }
264 
265   return presContext->RefreshDriver();
266 }
267 
UnregisterFromRefreshDriver()268 void DocumentTimeline::UnregisterFromRefreshDriver() {
269   if (!mIsObservingRefreshDriver) {
270     return;
271   }
272 
273   nsRefreshDriver* refreshDriver = GetRefreshDriver();
274   if (!refreshDriver) {
275     return;
276   }
277   DisconnectRefreshDriver(refreshDriver);
278 }
279 
280 }  // namespace mozilla::dom
281