Bug 1272211 - Part 1: Make timeline argument of Animation constructor default to the document timeline of the active document. r=birtles, r=smaug draft
authorDaisuke Akatsuka <daisuke@mozilla-japan.org>
Mon, 30 May 2016 09:01:11 +0900
changeset 372667 4e80cd678bda83642822033ba1d4433fea40e336
parent 370474 5511d54a3f172c1d68f98cc55dce4de1d0ba1b51
child 372668 04c87f70e6bca7c68feacb3526e73c1ca4790331
push id19557
push userbmo:daisuke@mozilla-japan.org
push dateMon, 30 May 2016 00:01:25 +0000
reviewersbirtles, smaug
bugs1272211
milestone49.0a1
Bug 1272211 - Part 1: Make timeline argument of Animation constructor default to the document timeline of the active document. r=birtles, r=smaug MozReview-Commit-ID: KUsQF5bryg8
dom/animation/Animation.cpp
dom/animation/Animation.h
dom/base/Element.cpp
dom/webidl/Animation.webidl
--- a/dom/animation/Animation.cpp
+++ b/dom/animation/Animation.cpp
@@ -3,16 +3,17 @@
 /* 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 "Animation.h"
 #include "AnimationUtils.h"
 #include "mozilla/dom/AnimationBinding.h"
 #include "mozilla/dom/AnimationPlaybackEvent.h"
+#include "mozilla/dom/DocumentTimeline.h"
 #include "mozilla/AnimationTarget.h"
 #include "mozilla/AutoRestore.h"
 #include "mozilla/AsyncEventDispatcher.h" // For AsyncEventDispatcher
 #include "mozilla/Maybe.h" // For Maybe
 #include "nsAnimationManager.h" // For CSSAnimation
 #include "nsDOMMutationObserver.h" // For nsAutoAnimationMutationBatch
 #include "nsIDocument.h" // For nsIDocument
 #include "nsIPresShell.h" // For nsIPresShell
@@ -82,29 +83,42 @@ namespace {
 // ---------------------------------------------------------------------------
 //
 // Animation interface:
 //
 // ---------------------------------------------------------------------------
 /* static */ already_AddRefed<Animation>
 Animation::Constructor(const GlobalObject& aGlobal,
                        KeyframeEffectReadOnly* aEffect,
-                       AnimationTimeline* aTimeline,
+                       const Optional<AnimationTimeline*>& aTimeline,
                        ErrorResult& aRv)
 {
   nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
   RefPtr<Animation> animation = new Animation(global);
 
   if (!aEffect) {
     // Bug 1049975: We do not support null effect yet.
     aRv.Throw(NS_ERROR_DOM_ANIM_NO_EFFECT_ERR);
     return nullptr;
   }
 
-  animation->SetTimeline(aTimeline);
+  AnimationTimeline* timeline;
+  if (aTimeline.WasPassed()) {
+    timeline = aTimeline.Value();
+  } else {
+    nsIDocument* document =
+      AnimationUtils::GetCurrentRealmDocument(aGlobal.Context());
+    if (!document) {
+      aRv.Throw(NS_ERROR_FAILURE);
+      return nullptr;
+    }
+    timeline = document->Timeline();
+  }
+
+  animation->SetTimeline(timeline);
   animation->SetEffect(aEffect);
 
   return animation.forget();
 }
 
 void
 Animation::SetId(const nsAString& aId)
 {
--- a/dom/animation/Animation.h
+++ b/dom/animation/Animation.h
@@ -88,17 +88,17 @@ public:
     AutoRewind,
     Continue
   };
 
   // Animation interface methods
   static already_AddRefed<Animation>
   Constructor(const GlobalObject& aGlobal,
               KeyframeEffectReadOnly* aEffect,
-              AnimationTimeline* aTimeline,
+              const Optional<AnimationTimeline*>& aTimeline,
               ErrorResult& aRv);
   void GetId(nsAString& aResult) const { aResult = mId; }
   void SetId(const nsAString& aId);
   KeyframeEffectReadOnly* GetEffect() const { return mEffect; }
   void SetEffect(KeyframeEffectReadOnly* aEffect);
   AnimationTimeline* GetTimeline() const { return mTimeline; }
   void SetTimeline(AnimationTimeline* aTimeline);
   Nullable<TimeDuration> GetStartTime() const { return mStartTime; }
--- a/dom/base/Element.cpp
+++ b/dom/base/Element.cpp
@@ -3329,19 +3329,20 @@ Element::Animate(const Nullable<ElementO
   }
 
   RefPtr<KeyframeEffect> effect =
     KeyframeEffect::Constructor(global, aTarget, keyframes, aOptions, aError);
   if (aError.Failed()) {
     return nullptr;
   }
 
+  AnimationTimeline* timeline = referenceElement->OwnerDoc()->Timeline();
   RefPtr<Animation> animation =
     Animation::Constructor(global, effect,
-                           referenceElement->OwnerDoc()->Timeline(), aError);
+                           Optional<AnimationTimeline*>(timeline), aError);
   if (aError.Failed()) {
     return nullptr;
   }
 
   if (aOptions.IsKeyframeAnimationOptions()) {
     animation->SetId(aOptions.GetAsKeyframeAnimationOptions().mId);
   }
 
--- a/dom/webidl/Animation.webidl
+++ b/dom/webidl/Animation.webidl
@@ -9,17 +9,17 @@
  * Copyright © 2015 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C
  * liability, trademark and document use rules apply.
  */
 
 enum AnimationPlayState { "idle", "pending", "running", "paused", "finished" };
 
 [Func="nsDocument::IsElementAnimateEnabled",
  Constructor (optional KeyframeEffectReadOnly? effect = null,
-              optional AnimationTimeline? timeline = null)]
+              optional AnimationTimeline? timeline)]
 interface Animation : EventTarget {
   attribute DOMString id;
   // Bug 1049975: Make 'effect' writeable
   [Func="nsDocument::IsWebAnimationsEnabled", Pure]
   readonly attribute AnimationEffectReadOnly? effect;
   [Func="nsDocument::IsWebAnimationsEnabled"]
   readonly attribute AnimationTimeline? timeline;
   [BinaryName="startTimeAsDouble"]