Bug 1096773 part 2 - Add a KeyframeEffectReadOnly constructor that takes a TimingParams argument; r=boris
authorBrian Birtles <birtles@gmail.com>
Fri, 29 Jan 2016 12:37:52 +1100
changeset 326997 e091852619a3e746a86b7b30b545c168c507ca3a
parent 326996 b37dd661b3e0eead35f7c2e2a569d10ff1800eb9
child 326998 8d8c6966bae7bcb786e292c5a625e057d5bb8dbd
push id10184
push usermgoodwin@mozilla.com
push dateFri, 29 Jan 2016 13:53:06 +0000
reviewersboris
bugs1096773
milestone47.0a1
Bug 1096773 part 2 - Add a KeyframeEffectReadOnly constructor that takes a TimingParams argument; r=boris This will allow us to re-use the constructor from Animatable.animate() since the existing type, UnrestrictedDoubleOrKeyframeEffectOptions, is not compatible with UnrestrictedDoubleOrKeyframeAnimationOptions (introduced in the next patch in this series), as used by Animatable.animate()
dom/animation/AnimationEffectTimingReadOnly.cpp
dom/animation/AnimationEffectTimingReadOnly.h
dom/animation/KeyframeEffect.cpp
dom/animation/KeyframeEffect.h
--- a/dom/animation/AnimationEffectTimingReadOnly.cpp
+++ b/dom/animation/AnimationEffectTimingReadOnly.cpp
@@ -1,29 +1,45 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/dom/AnimationEffectTimingReadOnly.h"
+
 #include "mozilla/dom/AnimationEffectTimingReadOnlyBinding.h"
+#include "mozilla/dom/KeyframeEffectBinding.h"
 
 namespace mozilla {
 
-TimingParams&
-TimingParams::operator=(const dom::AnimationEffectTimingProperties& aRhs)
+TimingParams::TimingParams(const dom::AnimationEffectTimingProperties& aRhs)
+  : mDuration(aRhs.mDuration)
+  , mDelay(TimeDuration::FromMilliseconds(aRhs.mDelay))
+  , mIterations(aRhs.mIterations)
+  , mDirection(aRhs.mDirection)
+  , mFill(aRhs.mFill)
+{
+}
+
+TimingParams::TimingParams(double aDuration)
 {
-  mDuration = aRhs.mDuration;
-  mDelay = TimeDuration::FromMilliseconds(aRhs.mDelay);
-  mIterations = aRhs.mIterations;
-  mDirection = aRhs.mDirection;
-  mFill = aRhs.mFill;
+  mDuration.SetAsUnrestrictedDouble() = aDuration;
+}
 
-  return *this;
+/* static */ TimingParams
+TimingParams::FromOptionsUnion(
+  const dom::UnrestrictedDoubleOrKeyframeEffectOptions& aOptions)
+{
+  if (aOptions.IsUnrestrictedDouble()) {
+    return TimingParams(aOptions.GetAsUnrestrictedDouble());
+  } else {
+    MOZ_ASSERT(aOptions.IsKeyframeEffectOptions());
+    return TimingParams(aOptions.GetAsKeyframeEffectOptions());
+  }
 }
 
 bool
 TimingParams::operator==(const TimingParams& aOther) const
 {
   bool durationEqual;
   if (mDuration.IsUnrestrictedDouble()) {
     durationEqual = aOther.mDuration.IsUnrestrictedDouble() &&
--- a/dom/animation/AnimationEffectTimingReadOnly.h
+++ b/dom/animation/AnimationEffectTimingReadOnly.h
@@ -19,28 +19,39 @@
 #ifdef None
 #undef None
 #endif
 #include "mozilla/dom/AnimationEffectReadOnlyBinding.h"  // for FillMode
                                                          // and PlaybackDirection
 
 namespace mozilla {
 
+namespace dom {
+struct AnimationEffectTimingProperties;
+class UnrestrictedDoubleOrKeyframeEffectOptions;
+}
+
 struct TimingParams
 {
+  TimingParams() = default;
+  explicit TimingParams(
+    const dom::AnimationEffectTimingProperties& aTimingProperties);
+  explicit TimingParams(double aDuration);
+
+  static TimingParams FromOptionsUnion(
+    const dom::UnrestrictedDoubleOrKeyframeEffectOptions& aOptions);
+
   // The unitialized state of mDuration represents "auto".
   // Bug 1237173: We will replace this with Maybe<TimeDuration>.
   dom::OwningUnrestrictedDoubleOrString mDuration;
   TimeDuration mDelay;      // Initializes to zero
   double mIterations = 1.0; // Can be NaN, negative, +/-Infinity
   dom::PlaybackDirection mDirection = dom::PlaybackDirection::Normal;
   dom::FillMode mFill = dom::FillMode::Auto;
 
-  TimingParams& operator=(const dom::AnimationEffectTimingProperties& aRhs);
-
   bool operator==(const TimingParams& aOther) const;
   bool operator!=(const TimingParams& aOther) const
   {
     return !(*this == aOther);
   }
 };
 
 
--- a/dom/animation/KeyframeEffect.cpp
+++ b/dom/animation/KeyframeEffect.cpp
@@ -645,31 +645,16 @@ DumpAnimationProperties(nsTArray<Animati
       printf("  %f..%f: %s..%s\n", s.mFromKey, s.mToKey,
              NS_ConvertUTF16toUTF8(fromValue).get(),
              NS_ConvertUTF16toUTF8(toValue).get());
     }
   }
 }
 #endif
 
-/* static */ TimingParams
-KeyframeEffectReadOnly::ConvertKeyframeEffectOptions(
-    const UnrestrictedDoubleOrKeyframeEffectOptions& aOptions)
-{
-  TimingParams timing;
-
-  if (aOptions.IsKeyframeEffectOptions()) {
-    timing = aOptions.GetAsKeyframeEffectOptions();
-  } else {
-    timing.mDuration.SetAsUnrestrictedDouble() =
-      aOptions.GetAsUnrestrictedDouble();
-  }
-  return timing;
-}
-
 /**
  * A property and StyleAnimationValue pair.
  */
 struct KeyframeValue
 {
   nsCSSProperty mProperty;
   StyleAnimationValue mValue;
 };
@@ -1683,39 +1668,37 @@ KeyframeEffectReadOnly::BuildAnimationPr
   }
 }
 
 /* static */ already_AddRefed<KeyframeEffectReadOnly>
 KeyframeEffectReadOnly::Constructor(
     const GlobalObject& aGlobal,
     Element* aTarget,
     JS::Handle<JSObject*> aFrames,
-    const UnrestrictedDoubleOrKeyframeEffectOptions& aOptions,
+    const TimingParams& aTiming,
     ErrorResult& aRv)
 {
   if (!aTarget) {
     // We don't support null targets yet.
     aRv.Throw(NS_ERROR_DOM_ANIM_NO_TARGET_ERR);
     return nullptr;
   }
 
-  TimingParams timing = ConvertKeyframeEffectOptions(aOptions);
-
   InfallibleTArray<AnimationProperty> animationProperties;
   BuildAnimationPropertyList(aGlobal.Context(), aTarget, aFrames,
                              animationProperties, aRv);
 
   if (aRv.Failed()) {
     return nullptr;
   }
 
   RefPtr<KeyframeEffectReadOnly> effect =
     new KeyframeEffectReadOnly(aTarget->OwnerDoc(), aTarget,
                                nsCSSPseudoElements::ePseudo_NotPseudoElement,
-                               timing);
+                               aTiming);
   effect->mProperties = Move(animationProperties);
   return effect.forget();
 }
 
 void
 KeyframeEffectReadOnly::GetFrames(JSContext*& aCx,
                                   nsTArray<JSObject*>& aResult,
                                   ErrorResult& aRv)
--- a/dom/animation/KeyframeEffect.h
+++ b/dom/animation/KeyframeEffect.h
@@ -186,17 +186,31 @@ public:
   }
 
   // KeyframeEffectReadOnly interface
   static already_AddRefed<KeyframeEffectReadOnly>
   Constructor(const GlobalObject& aGlobal,
               Element* aTarget,
               JS::Handle<JSObject*> aFrames,
               const UnrestrictedDoubleOrKeyframeEffectOptions& aOptions,
+              ErrorResult& aRv)
+  {
+    return Constructor(aGlobal, aTarget, aFrames,
+                       TimingParams::FromOptionsUnion(aOptions), aRv);
+  }
+
+  // More generalized version for Animatable.animate.
+  // Not exposed to content.
+  static already_AddRefed<KeyframeEffectReadOnly>
+  Constructor(const GlobalObject& aGlobal,
+              Element* aTarget,
+              JS::Handle<JSObject*> aFrames,
+              const TimingParams& aTiming,
               ErrorResult& aRv);
+
   Element* GetTarget() const {
     // Currently we never return animations from the API whose effect
     // targets a pseudo-element so this should never be called when
     // mPseudoType is not 'none' (see bug 1174575).
     MOZ_ASSERT(mPseudoType == nsCSSPseudoElements::ePseudo_NotPseudoElement,
                "Requesting the target of a KeyframeEffect that targets a"
                " pseudo-element is not yet supported.");
     return mTarget;
@@ -328,19 +342,16 @@ protected:
   // (b) It is "relevant" (i.e. yet to finish but not idle, or finished but
   //     filling forwards)
   //
   // As a result, we need to make sure this gets called whenever anything
   // changes with regards to this effects's timing including changes to the
   // owning Animation's timing.
   void UpdateTargetRegistration();
 
-  static TimingParams ConvertKeyframeEffectOptions(
-    const UnrestrictedDoubleOrKeyframeEffectOptions& aOptions);
-
   static void BuildAnimationPropertyList(
     JSContext* aCx,
     Element* aTarget,
     JS::Handle<JSObject*> aFrames,
     InfallibleTArray<AnimationProperty>& aResult,
     ErrorResult& aRv);
 
   nsCOMPtr<Element> mTarget;