AnimationUtils.h (5043B)
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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_AnimationUtils_h 8 #define mozilla_dom_AnimationUtils_h 9 10 #include "mozilla/PseudoStyleType.h" 11 #include "mozilla/TimeStamp.h" 12 #include "mozilla/dom/Nullable.h" 13 #include "nsRFPService.h" 14 #include "nsStringFwd.h" 15 16 class nsIContent; 17 class nsIFrame; 18 struct JSContext; 19 20 namespace mozilla { 21 22 class EffectSet; 23 24 namespace dom { 25 class Document; 26 class Element; 27 } // namespace dom 28 29 class AnimationUtils { 30 public: 31 using Document = dom::Document; 32 33 static dom::Nullable<double> TimeDurationToDouble( 34 const dom::Nullable<TimeDuration>& aTime, RTPCallerType aRTPCallerType) { 35 dom::Nullable<double> result; 36 37 if (!aTime.IsNull()) { 38 // 0 is an inappropriate mixin for this this area; however CSS Animations 39 // needs to have it's Time Reduction Logic refactored, so it's currently 40 // only clamping for RFP mode. RFP mode gives a much lower time precision, 41 // so we accept the security leak here for now 42 result.SetValue(nsRFPService::ReduceTimePrecisionAsMSecsRFPOnly( 43 aTime.Value().ToMilliseconds(), 0, aRTPCallerType)); 44 } 45 46 return result; 47 } 48 49 static dom::Nullable<TimeDuration> DoubleToTimeDuration( 50 const dom::Nullable<double>& aTime) { 51 dom::Nullable<TimeDuration> result; 52 53 if (!aTime.IsNull()) { 54 result.SetValue(TimeDuration::FromMilliseconds(aTime.Value())); 55 } 56 57 return result; 58 } 59 60 static void LogAsyncAnimationFailure(nsCString& aMessage, 61 const nsIContent* aContent = nullptr); 62 63 /** 64 * Get the document from the JS context to use when parsing CSS properties. 65 */ 66 static Document* GetCurrentRealmDocument(JSContext* aCx); 67 68 /** 69 * Get the document from the global object, or nullptr if the document has 70 * no window, to use when constructing DOM object without entering the 71 * target window's compartment (see KeyframeEffect constructor). 72 */ 73 static Document* GetDocumentFromGlobal(JSObject* aGlobalObject); 74 75 /** 76 * Returns true if the given frame has an animated scale. 77 */ 78 static bool FrameHasAnimatedScale(const nsIFrame* aFrame); 79 80 /** 81 * Returns true if the given (pseudo-)element has any transitions that are 82 * current (playing or waiting to play) or in effect (e.g. filling forwards). 83 */ 84 static bool HasCurrentTransitions(const dom::Element* aElement, 85 const PseudoStyleRequest& aPseudoRequest = 86 PseudoStyleRequest::NotPseudo()); 87 88 static bool StoresAnimationsInParent(PseudoStyleType aType) { 89 return aType == PseudoStyleType::before || 90 aType == PseudoStyleType::after || 91 aType == PseudoStyleType::marker || 92 aType == PseudoStyleType::backdrop; 93 } 94 95 /** 96 * Returns true if this pseudo style type is supported by animations. 97 * Note: This doesn't include PseudoStyleType::NotPseudo. 98 */ 99 static bool IsSupportedPseudoForAnimations(PseudoStyleType aType) { 100 // FIXME: Bug 1615469: Support first-line and first-letter for Animation. 101 return PseudoStyle::IsViewTransitionPseudoElement(aType) || 102 StoresAnimationsInParent(aType); 103 } 104 static bool IsSupportedPseudoForAnimations( 105 const PseudoStyleRequest& aRequest) { 106 return IsSupportedPseudoForAnimations(aRequest.mType); 107 } 108 109 /** 110 * Returns true if the difference between |aFirst| and |aSecond| is within 111 * the animation time tolerance (i.e. 1 microsecond). 112 */ 113 static bool IsWithinAnimationTimeTolerance(const TimeDuration& aFirst, 114 const TimeDuration& aSecond) { 115 if (aFirst == TimeDuration::Forever() || 116 aSecond == TimeDuration::Forever()) { 117 return aFirst == aSecond; 118 } 119 120 TimeDuration diff = aFirst >= aSecond ? aFirst - aSecond : aSecond - aFirst; 121 return diff <= TimeDuration::FromMicroseconds(1); 122 } 123 124 // Returns the pair of |Element, PseudoStyleRequest| from an element which 125 // could be an element or a pseudo element (i.e. an element used for restyling 126 // and DOM tree.). 127 // 128 // Animation module usually uses a pair of (Element*, PseudoStyleRequest) to 129 // represent the animation target. 130 // Note that we sepatate the originating element and PseudoStyleRequest in 131 // Animation code, but store the animations on "::before", "::after", and 132 // "::marker" in the originating element. For view-transition pseudo-elements 133 // and others, we store their KeyframeEffect, timelines, animations, and 134 // transitions in the pseudo-element themself. So use this function carefully. 135 static std::pair<const dom::Element*, PseudoStyleRequest> 136 GetElementPseudoPair(const dom::Element* aElementOrPseudo); 137 }; 138 139 } // namespace mozilla 140 141 #endif