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/TimeStamp.h"
11 #include "mozilla/dom/Nullable.h"
12 #include "nsRFPService.h"
13 #include "nsStringFwd.h"
14 
15 class nsIContent;
16 class nsIFrame;
17 struct JSContext;
18 
19 namespace mozilla {
20 
21 enum class PseudoStyleType : uint8_t;
22 class ComputedTimingFunction;
23 class EffectSet;
24 
25 namespace dom {
26 class Document;
27 class Element;
28 }  // namespace dom
29 
30 class AnimationUtils {
31  public:
32   using Document = dom::Document;
33 
TimeDurationToDouble(const dom::Nullable<TimeDuration> & aTime)34   static dom::Nullable<double> TimeDurationToDouble(
35       const dom::Nullable<TimeDuration>& aTime) {
36     dom::Nullable<double> result;
37 
38     if (!aTime.IsNull()) {
39       // 0 is an inappropriate mixin for this this area; however CSS Animations
40       // needs to have it's Time Reduction Logic refactored, so it's currently
41       // only clamping for RFP mode. RFP mode gives a much lower time precision,
42       // so we accept the security leak here for now
43       result.SetValue(nsRFPService::ReduceTimePrecisionAsMSecsRFPOnly(
44           aTime.Value().ToMilliseconds(), 0));
45     }
46 
47     return result;
48   }
49 
DoubleToTimeDuration(const dom::Nullable<double> & aTime)50   static dom::Nullable<TimeDuration> DoubleToTimeDuration(
51       const dom::Nullable<double>& aTime) {
52     dom::Nullable<TimeDuration> result;
53 
54     if (!aTime.IsNull()) {
55       result.SetValue(TimeDuration::FromMilliseconds(aTime.Value()));
56     }
57 
58     return result;
59   }
60 
61   static void LogAsyncAnimationFailure(nsCString& aMessage,
62                                        const nsIContent* aContent = nullptr);
63 
64   /**
65    * Get the document from the JS context to use when parsing CSS properties.
66    */
67   static Document* GetCurrentRealmDocument(JSContext* aCx);
68 
69   /**
70    * Get the document from the global object, or nullptr if the document has
71    * no window, to use when constructing DOM object without entering the
72    * target window's compartment (see KeyframeEffect constructor).
73    */
74   static Document* GetDocumentFromGlobal(JSObject* aGlobalObject);
75 
76   /**
77    * Returns true if the given frame has an animated scale.
78    */
79   static bool FrameHasAnimatedScale(const nsIFrame* aFrame);
80 
81   /**
82    * Returns true if the given (pseudo-)element has any transitions that are
83    * current (playing or waiting to play) or in effect (e.g. filling forwards).
84    */
85   static bool HasCurrentTransitions(const dom::Element* aElement,
86                                     PseudoStyleType aPseudoType);
87 };
88 
89 }  // namespace mozilla
90 
91 #endif
92