gecko-dev/layout/style/AnimationCollection.cpp
Brian Birtles 065446fdcd Bug 1239945 part 7 - Move GetAnimationCollection to AnimationCollection; r=dholbert
By moving GetAnimationCollection to AnimationCollection itself, we can remove
a bunch of virtual methods on the animation managers, simplify call sites,
and provide better type safety by ensuring a correspondence between element
property names and concrete animation types.

One change in behavior, however, is that in doing this we can no longer
add any newly-created AnimationCollection to the corresponding manager's linked
list of collections inside GetAnimationCollection. Instead we take a bool
outparam to indicate if a new collection was created and leave managing the
linked list to the manager. This is just a temporary measure, however, since
by the end of this patch series will will eliminate this linked list altogether
along with this flag.

MozReview-Commit-ID: 1jsc4QcmVDg
2016-03-09 12:55:39 +09:00

153 lines
4.9 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/AnimationCollection.h"
#include "mozilla/RestyleManagerHandle.h"
#include "mozilla/RestyleManagerHandleInlines.h"
#include "nsAnimationManager.h" // For dom::CSSAnimation
#include "nsPresContext.h"
#include "nsTransitionManager.h" // For dom::CSSTransition
namespace mozilla {
template <class AnimationType>
/* static */ void
AnimationCollection<AnimationType>::PropertyDtor(void* aObject,
nsIAtom* aPropertyName,
void* aPropertyValue,
void* aData)
{
AnimationCollection* collection =
static_cast<AnimationCollection*>(aPropertyValue);
#ifdef DEBUG
MOZ_ASSERT(!collection->mCalledPropertyDtor, "can't call dtor twice");
collection->mCalledPropertyDtor = true;
#endif
{
nsAutoAnimationMutationBatch mb(collection->mElement->OwnerDoc());
for (size_t animIdx = collection->mAnimations.Length(); animIdx-- != 0; ) {
collection->mAnimations[animIdx]->CancelFromStyle();
}
}
delete collection;
}
template <class AnimationType>
/* static */ AnimationCollection<AnimationType>*
AnimationCollection<AnimationType>::GetAnimationCollection(
dom::Element *aElement,
CSSPseudoElementType aPseudoType,
bool aCreateIfNeeded,
bool* aCreatedCollection)
{
if (aCreatedCollection) {
*aCreatedCollection = false;
}
if (!aCreateIfNeeded && !aElement->MayHaveAnimations()) {
// Early return for the most common case.
return nullptr;
}
nsIAtom *propName;
if (aPseudoType == CSSPseudoElementType::NotPseudo) {
propName = TraitsType::ElementPropertyAtom();
} else if (aPseudoType == CSSPseudoElementType::before) {
propName = TraitsType::BeforePropertyAtom();
} else if (aPseudoType == CSSPseudoElementType::after) {
propName = TraitsType::AfterPropertyAtom();
} else {
NS_ASSERTION(!aCreateIfNeeded,
"should never try to create transitions for pseudo "
"other than :before or :after");
return nullptr;
}
auto collection = static_cast<AnimationCollection<AnimationType>*>(
aElement->GetProperty(propName));
if (!collection && aCreateIfNeeded) {
// FIXME: Consider arena-allocating?
collection = new AnimationCollection<AnimationType>(aElement, propName);
nsresult rv =
aElement->SetProperty(propName, collection,
&AnimationCollection<AnimationType>::PropertyDtor,
false);
if (NS_FAILED(rv)) {
NS_WARNING("SetProperty failed");
// The collection must be destroyed via PropertyDtor, otherwise
// mCalledPropertyDtor assertion is triggered in destructor.
AnimationCollection<AnimationType>::PropertyDtor(aElement, propName,
collection, nullptr);
return nullptr;
}
if (aCreatedCollection) {
*aCreatedCollection = true;
}
aElement->SetMayHaveAnimations();
}
return collection;
}
template <class AnimationType>
/* static */ AnimationCollection<AnimationType>*
AnimationCollection<AnimationType>::GetAnimationCollection(
const nsIFrame* aFrame)
{
Maybe<Pair<dom::Element*, CSSPseudoElementType>> pseudoElement =
EffectCompositor::GetAnimationElementAndPseudoForFrame(aFrame);
if (!pseudoElement) {
return nullptr;
}
if (!pseudoElement->first()->MayHaveAnimations()) {
return nullptr;
}
return GetAnimationCollection(pseudoElement->first(),
pseudoElement->second(),
false /* aCreateIfNeeded */);
}
template <class AnimationType>
/* static */ nsString
AnimationCollection<AnimationType>::PseudoTypeAsString(
CSSPseudoElementType aPseudoType)
{
switch (aPseudoType) {
case CSSPseudoElementType::before:
return NS_LITERAL_STRING("::before");
case CSSPseudoElementType::after:
return NS_LITERAL_STRING("::after");
default:
MOZ_ASSERT(aPseudoType == CSSPseudoElementType::NotPseudo,
"Unexpected pseudo type");
return EmptyString();
}
}
template <class AnimationType>
void
AnimationCollection<AnimationType>::UpdateCheckGeneration(
nsPresContext* aPresContext)
{
if (aPresContext->RestyleManager()->IsServo()) {
// stylo: ServoRestyleManager does not support animations yet.
return;
}
mCheckGeneration =
aPresContext->RestyleManager()->AsGecko()->GetAnimationGeneration();
}
// Explicit class instantiations
template class AnimationCollection<dom::CSSAnimation>;
template class AnimationCollection<dom::CSSTransition>;
} // namespace mozilla