Bug 1246320 part 1 - Add AnimationUtils::GetDocumentFromJSContext; r=bz draft
authorBrian Birtles <birtles@gmail.com>
Fri, 11 Mar 2016 17:20:17 +0900
changeset 339652 51864a04dbcef0205a4173e0c00fc088dc063a53
parent 339651 1dab54e2f0e291364005b110ceac963521c27c17
child 339653 afa5dc066f473ab18bb6b01461ab0300f0b1f205
push id12784
push userbbirtles@mozilla.com
push dateFri, 11 Mar 2016 23:06:50 +0000
reviewersbz
bugs1246320, 1245748
milestone48.0a1
Bug 1246320 part 1 - Add AnimationUtils::GetDocumentFromJSContext; r=bz Adds a utility function for getting the document on the global associated with a JSContext. We will need this in various situations where we want to use the CSS parser (which requires various bits of state we pull off a document) to parse a timing function but might not have a target element. Strictly speaking we currently always have a target element but in future we expect to support creating KeyframeEffects without an associated target element. Also, we will need this for some situations in bug 1245748 where we need to parse CSS properties on keyframe objects when we may not have a target element. MozReview-Commit-ID: Klku1LFoRGp
dom/animation/AnimationUtils.cpp
dom/animation/AnimationUtils.h
--- a/dom/animation/AnimationUtils.cpp
+++ b/dom/animation/AnimationUtils.cpp
@@ -5,20 +5,23 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "AnimationUtils.h"
 
 #include "nsCSSParser.h" // For nsCSSParser
 #include "nsDebug.h"
 #include "nsIAtom.h"
 #include "nsIContent.h"
+#include "nsIDocument.h"
+#include "nsGlobalWindow.h"
 #include "nsString.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/ComputedTimingFunction.h" // ComputedTimingFunction
 #include "mozilla/dom/Element.h" // For dom::Element
+#include "xpcpublic.h" // For xpc::CurrentWindowOrNull
 
 namespace mozilla {
 
 /* static */ void
 AnimationUtils::LogAsyncAnimationFailure(nsCString& aMessage,
                                          const nsIContent* aContent)
 {
   if (aContent) {
@@ -94,9 +97,19 @@ AnimationUtils::ParseEasing(const dom::E
       break;
     default:
       MOZ_ASSERT_UNREACHABLE("unexpected animation-timing-function unit");
       break;
   }
   return Nothing();
 }
 
+/* static */ nsIDocument*
+AnimationUtils::GetCurrentRealmDocument(JSContext* aCx)
+{
+  nsGlobalWindow* win = xpc::CurrentWindowOrNull(aCx);
+  if (!win) {
+    return nullptr;
+  }
+  return win->GetDoc();
+}
+
 } // namespace mozilla
--- a/dom/animation/AnimationUtils.h
+++ b/dom/animation/AnimationUtils.h
@@ -7,16 +7,18 @@
 #ifndef mozilla_dom_AnimationUtils_h
 #define mozilla_dom_AnimationUtils_h
 
 #include "mozilla/TimeStamp.h"
 #include "mozilla/dom/Nullable.h"
 #include "nsStringFwd.h"
 
 class nsIContent;
+class nsIDocument;
+struct JSContext;
 
 namespace mozilla {
 
 class ComputedTimingFunction;
 
 namespace dom {
 class Element;
 }
@@ -53,13 +55,19 @@ public:
 
   /**
    * Parses a CSS <single-transition-timing-function> value from
    * aEasing into a ComputedTimingFunction.  If parsing fails, Nothing() will
    * be returned.
    */
   static Maybe<ComputedTimingFunction>
   ParseEasing(const dom::Element* aTarget, const nsAString& aEasing);
+
+  /**
+   * Get the document from the JS context to use when parsing CSS properties.
+   */
+  static nsIDocument*
+  GetCurrentRealmDocument(JSContext* aCx);
 };
 
 } // namespace mozilla
 
 #endif