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 #ifndef mozilla_dom_AnimationTimeline_h
8 #define mozilla_dom_AnimationTimeline_h
9 
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 "nsTHashtable.h"
19 
20 // GetCurrentTime is defined in winbase.h as zero argument macro forwarding to
21 // GetTickCount().
22 #ifdef GetCurrentTime
23 #undef GetCurrentTime
24 #endif
25 
26 namespace mozilla {
27 namespace dom {
28 
29 class Animation;
30 
31 class AnimationTimeline : public nsISupports, public nsWrapperCache {
32  public:
AnimationTimeline(nsIGlobalObject * aWindow)33   explicit AnimationTimeline(nsIGlobalObject* aWindow) : mWindow(aWindow) {
34     MOZ_ASSERT(mWindow);
35   }
36 
37  protected:
~AnimationTimeline()38   virtual ~AnimationTimeline() { mAnimationOrder.clear(); }
39 
40  public:
41   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AnimationTimeline)42   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AnimationTimeline)
43 
44   nsIGlobalObject* GetParentObject() const { return mWindow; }
45 
46   // AnimationTimeline methods
47   virtual Nullable<TimeDuration> GetCurrentTime() const = 0;
48 
49   // Wrapper functions for AnimationTimeline DOM methods when called from
50   // script.
GetCurrentTimeAsDouble()51   Nullable<double> GetCurrentTimeAsDouble() const {
52     return AnimationUtils::TimeDurationToDouble(GetCurrentTime());
53   }
54 
55   /**
56    * Returns true if the times returned by GetCurrentTime() are convertible
57    * to and from wallclock-based TimeStamp (e.g. from TimeStamp::Now()) values
58    * using ToTimelineTime() and ToTimeStamp().
59    *
60    * Typically this is true, but it will be false in the case when this
61    * timeline has no refresh driver or is tied to a refresh driver under test
62    * control.
63    */
64   virtual bool TracksWallclockTime() const = 0;
65 
66   /**
67    * Converts a TimeStamp to the equivalent value in timeline time.
68    * Note that when TracksWallclockTime() is false, there is no correspondence
69    * between timeline time and wallclock time. In such a case, passing a
70    * timestamp from TimeStamp::Now() to this method will not return a
71    * meaningful result.
72    */
73   virtual Nullable<TimeDuration> ToTimelineTime(
74       const TimeStamp& aTimeStamp) const = 0;
75 
76   virtual TimeStamp ToTimeStamp(const TimeDuration& aTimelineTime) const = 0;
77 
78   /**
79    * Inform this timeline that |aAnimation| which is or was observing the
80    * timeline, has been updated. This serves as both the means to associate
81    * AND disassociate animations with a timeline. The timeline itself will
82    * determine if it needs to begin, continue or stop tracking this animation.
83    */
84   virtual void NotifyAnimationUpdated(Animation& aAnimation);
85 
86   /**
87    * Returns true if any CSS animations, CSS transitions or Web animations are
88    * currently associated with this timeline.  As soon as an animation is
89    * applied to an element it is associated with the timeline even if it has a
90    * delayed start, so this includes animations that may not be active for some
91    * time.
92    */
HasAnimations()93   bool HasAnimations() const { return !mAnimations.IsEmpty(); }
94 
95   virtual void RemoveAnimation(Animation* aAnimation);
96 
97  protected:
98   nsCOMPtr<nsIGlobalObject> mWindow;
99 
100   // Animations observing this timeline
101   //
102   // We store them in (a) a hashset for quick lookup, and (b) an array
103   // to maintain a fixed sampling order.
104   //
105   // The hashset keeps a strong reference to each animation since
106   // dealing with addref/release with LinkedList is difficult.
107   typedef nsTHashtable<nsRefPtrHashKey<dom::Animation>> AnimationSet;
108   AnimationSet mAnimations;
109   LinkedList<dom::Animation> mAnimationOrder;
110 };
111 
112 }  // namespace dom
113 }  // namespace mozilla
114 
115 #endif  // mozilla_dom_AnimationTimeline_h
116