Bug 1769628 [wpt PR 34081] - Update wpt metadata, a=testonly
[gecko.git] / dom / animation / AnimationTimeline.h
blobf2af12ca93300ae0717ef6c55a76cf6e4df19cb3
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/. */
7 #ifndef mozilla_dom_AnimationTimeline_h
8 #define mozilla_dom_AnimationTimeline_h
10 #include "nsISupports.h"
11 #include "nsWrapperCache.h"
12 #include "nsCycleCollectionParticipant.h"
13 #include "js/TypeDecls.h"
14 #include "mozilla/AnimationUtils.h"
15 #include "mozilla/Attributes.h"
16 #include "nsHashKeys.h"
17 #include "nsIGlobalObject.h"
18 #include "nsTHashSet.h"
20 namespace mozilla::dom {
22 class Animation;
23 class Document;
24 class ScrollTimeline;
26 class AnimationTimeline : public nsISupports, public nsWrapperCache {
27 public:
28 explicit AnimationTimeline(nsIGlobalObject* aWindow) : mWindow(aWindow) {
29 MOZ_ASSERT(mWindow);
32 protected:
33 virtual ~AnimationTimeline();
35 // Tick animations and may remove them from the list if we don't need to
36 // tick it. Return true if any animations need to be ticked.
37 bool Tick();
39 public:
40 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
41 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AnimationTimeline)
43 nsIGlobalObject* GetParentObject() const { return mWindow; }
45 // AnimationTimeline methods
46 virtual Nullable<TimeDuration> GetCurrentTimeAsDuration() const = 0;
48 // Wrapper functions for AnimationTimeline DOM methods when called from
49 // script.
50 Nullable<double> GetCurrentTimeAsDouble() const {
51 return AnimationUtils::TimeDurationToDouble(GetCurrentTimeAsDuration());
54 TimeStamp GetCurrentTimeAsTimeStamp() const {
55 Nullable<TimeDuration> currentTime = GetCurrentTimeAsDuration();
56 return !currentTime.IsNull() ? ToTimeStamp(currentTime.Value())
57 : TimeStamp();
60 /**
61 * Returns true if the times returned by GetCurrentTimeAsDuration() are
62 * convertible to and from wallclock-based TimeStamp (e.g. from
63 * TimeStamp::Now()) values using ToTimelineTime() and ToTimeStamp().
65 * Typically this is true, but it will be false in the case when this
66 * timeline has no refresh driver or is tied to a refresh driver under test
67 * control.
69 virtual bool TracksWallclockTime() const = 0;
71 /**
72 * Converts a TimeStamp to the equivalent value in timeline time.
73 * Note that when TracksWallclockTime() is false, there is no correspondence
74 * between timeline time and wallclock time. In such a case, passing a
75 * timestamp from TimeStamp::Now() to this method will not return a
76 * meaningful result.
78 virtual Nullable<TimeDuration> ToTimelineTime(
79 const TimeStamp& aTimeStamp) const = 0;
81 virtual TimeStamp ToTimeStamp(const TimeDuration& aTimelineTime) const = 0;
83 /**
84 * Inform this timeline that |aAnimation| which is or was observing the
85 * timeline, has been updated. This serves as both the means to associate
86 * AND disassociate animations with a timeline. The timeline itself will
87 * determine if it needs to begin, continue or stop tracking this animation.
89 virtual void NotifyAnimationUpdated(Animation& aAnimation);
91 /**
92 * Returns true if any CSS animations, CSS transitions or Web animations are
93 * currently associated with this timeline. As soon as an animation is
94 * applied to an element it is associated with the timeline even if it has a
95 * delayed start, so this includes animations that may not be active for some
96 * time.
98 bool HasAnimations() const { return !mAnimations.IsEmpty(); }
100 virtual void RemoveAnimation(Animation* aAnimation);
102 virtual Document* GetDocument() const = 0;
104 virtual bool IsMonotonicallyIncreasing() const = 0;
106 virtual bool IsScrollTimeline() const { return false; }
107 virtual const ScrollTimeline* AsScrollTimeline() const { return nullptr; }
109 protected:
110 nsCOMPtr<nsIGlobalObject> mWindow;
112 // Animations observing this timeline
114 // We store them in (a) a hashset for quick lookup, and (b) an array
115 // to maintain a fixed sampling order.
117 // The hashset keeps a strong reference to each animation since
118 // dealing with addref/release with LinkedList is difficult.
119 typedef nsTHashSet<nsRefPtrHashKey<dom::Animation>> AnimationSet;
120 AnimationSet mAnimations;
121 LinkedList<dom::Animation> mAnimationOrder;
124 } // namespace mozilla::dom
126 #endif // mozilla_dom_AnimationTimeline_h