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 #include "AnimationUtils.h"
8 
9 #include "mozilla/dom/Animation.h"
10 #include "mozilla/dom/Document.h"
11 #include "mozilla/dom/KeyframeEffect.h"
12 #include "mozilla/EffectSet.h"
13 #include "nsDebug.h"
14 #include "nsAtom.h"
15 #include "nsIContent.h"
16 #include "nsGlobalWindow.h"
17 #include "nsString.h"
18 #include "xpcpublic.h"  // For xpc::NativeGlobal
19 
20 using namespace mozilla::dom;
21 
22 namespace mozilla {
23 
24 /* static */
LogAsyncAnimationFailure(nsCString & aMessage,const nsIContent * aContent)25 void AnimationUtils::LogAsyncAnimationFailure(nsCString& aMessage,
26                                               const nsIContent* aContent) {
27   if (aContent) {
28     aMessage.AppendLiteral(" [");
29     aMessage.Append(nsAtomCString(aContent->NodeInfo()->NameAtom()));
30 
31     nsAtom* id = aContent->GetID();
32     if (id) {
33       aMessage.AppendLiteral(" with id '");
34       aMessage.Append(nsAtomCString(aContent->GetID()));
35       aMessage.Append('\'');
36     }
37     aMessage.Append(']');
38   }
39   aMessage.Append('\n');
40   printf_stderr("%s", aMessage.get());
41 }
42 
43 /* static */
GetCurrentRealmDocument(JSContext * aCx)44 Document* AnimationUtils::GetCurrentRealmDocument(JSContext* aCx) {
45   nsGlobalWindowInner* win = xpc::CurrentWindowOrNull(aCx);
46   if (!win) {
47     return nullptr;
48   }
49   return win->GetDoc();
50 }
51 
52 /* static */
GetDocumentFromGlobal(JSObject * aGlobalObject)53 Document* AnimationUtils::GetDocumentFromGlobal(JSObject* aGlobalObject) {
54   nsGlobalWindowInner* win = xpc::WindowOrNull(aGlobalObject);
55   if (!win) {
56     return nullptr;
57   }
58   return win->GetDoc();
59 }
60 
61 /* static */
FrameHasAnimatedScale(const nsIFrame * aFrame)62 bool AnimationUtils::FrameHasAnimatedScale(const nsIFrame* aFrame) {
63   EffectSet* effectSet = EffectSet::GetEffectSetForFrame(
64       aFrame, nsCSSPropertyIDSet::TransformLikeProperties());
65   if (!effectSet) {
66     return false;
67   }
68 
69   for (const dom::KeyframeEffect* effect : *effectSet) {
70     if (effect->ContainsAnimatedScale(aFrame)) {
71       return true;
72     }
73   }
74 
75   return false;
76 }
77 
78 /* static */
HasCurrentTransitions(const Element * aElement,PseudoStyleType aPseudoType)79 bool AnimationUtils::HasCurrentTransitions(const Element* aElement,
80                                            PseudoStyleType aPseudoType) {
81   MOZ_ASSERT(aElement);
82 
83   EffectSet* effectSet = EffectSet::GetEffectSet(aElement, aPseudoType);
84   if (!effectSet) {
85     return false;
86   }
87 
88   for (const dom::KeyframeEffect* effect : *effectSet) {
89     // If |effect| is current, it must have an associated Animation
90     // so we don't need to null-check the result of GetAnimation().
91     if (effect->IsCurrent() && effect->GetAnimation()->AsCSSTransition()) {
92       return true;
93     }
94   }
95 
96   return false;
97 }
98 
99 }  // namespace mozilla
100