AnimationCollection.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_AnimationCollection_h
  6. #define mozilla_AnimationCollection_h
  7. #include "mozilla/dom/Animation.h"
  8. #include "mozilla/dom/Element.h"
  9. #include "mozilla/Assertions.h"
  10. #include "mozilla/LinkedList.h"
  11. #include "mozilla/RefPtr.h"
  12. #include "nsCSSPseudoElements.h"
  13. #include "nsDOMMutationObserver.h"
  14. #include "nsTArray.h"
  15. class nsIAtom;
  16. class nsPresContext;
  17. namespace mozilla {
  18. // Traits class to define the specific atoms used when storing specializations
  19. // of AnimationCollection as a property on an Element (e.g. which atom
  20. // to use when storing an AnimationCollection<CSSAnimation> for a ::before
  21. // pseudo-element).
  22. template <class AnimationType>
  23. struct AnimationTypeTraits { };
  24. template <class AnimationType>
  25. class AnimationCollection
  26. : public LinkedListElement<AnimationCollection<AnimationType>>
  27. {
  28. typedef AnimationCollection<AnimationType> SelfType;
  29. typedef AnimationTypeTraits<AnimationType> TraitsType;
  30. AnimationCollection(dom::Element* aElement, nsIAtom* aElementProperty)
  31. : mElement(aElement)
  32. , mElementProperty(aElementProperty)
  33. , mCheckGeneration(0)
  34. #ifdef DEBUG
  35. , mCalledPropertyDtor(false)
  36. #endif
  37. {
  38. MOZ_COUNT_CTOR(AnimationCollection);
  39. }
  40. public:
  41. ~AnimationCollection()
  42. {
  43. MOZ_ASSERT(mCalledPropertyDtor,
  44. "must call destructor through element property dtor");
  45. MOZ_COUNT_DTOR(AnimationCollection);
  46. LinkedListElement<SelfType>::remove();
  47. }
  48. void Destroy()
  49. {
  50. // This will call our destructor.
  51. mElement->DeleteProperty(mElementProperty);
  52. }
  53. static void PropertyDtor(void *aObject, nsIAtom *aPropertyName,
  54. void *aPropertyValue, void *aData);
  55. // Get the collection of animations for the given |aElement| and
  56. // |aPseudoType|.
  57. static AnimationCollection<AnimationType>*
  58. GetAnimationCollection(dom::Element* aElement,
  59. CSSPseudoElementType aPseudoType);
  60. // Given the frame |aFrame| with possibly animated content, finds its
  61. // associated collection of animations. If |aFrame| is a generated content
  62. // frame, this function may examine the parent frame to search for such
  63. // animations.
  64. static AnimationCollection<AnimationType>* GetAnimationCollection(
  65. const nsIFrame* aFrame);
  66. // Get the collection of animations for the given |aElement| and
  67. // |aPseudoType| or create it if it does not already exist.
  68. //
  69. // We'll set the outparam |aCreatedCollection| to true if we have
  70. // to create the collection and we successfully do so. Otherwise,
  71. // we'll set it to false.
  72. static AnimationCollection<AnimationType>*
  73. GetOrCreateAnimationCollection(dom::Element* aElement,
  74. CSSPseudoElementType aPseudoType,
  75. bool* aCreatedCollection);
  76. bool IsForElement() const { // rather than for a pseudo-element
  77. return mElementProperty == TraitsType::ElementPropertyAtom();
  78. }
  79. bool IsForBeforePseudo() const {
  80. return mElementProperty == TraitsType::BeforePropertyAtom();
  81. }
  82. bool IsForAfterPseudo() const {
  83. return mElementProperty == TraitsType::AfterPropertyAtom();
  84. }
  85. CSSPseudoElementType PseudoElementType() const
  86. {
  87. if (IsForElement()) {
  88. return CSSPseudoElementType::NotPseudo;
  89. }
  90. if (IsForBeforePseudo()) {
  91. return CSSPseudoElementType::before;
  92. }
  93. MOZ_ASSERT(IsForAfterPseudo(),
  94. "::before & ::after should be the only pseudo-elements here");
  95. return CSSPseudoElementType::after;
  96. }
  97. static nsString PseudoTypeAsString(CSSPseudoElementType aPseudoType);
  98. dom::Element *mElement;
  99. // the atom we use in mElement's prop table (must be a static atom,
  100. // i.e., in an atom list)
  101. nsIAtom *mElementProperty;
  102. InfallibleTArray<RefPtr<AnimationType>> mAnimations;
  103. // For CSS transitions only, we record the most recent generation
  104. // for which we've done the transition update, so that we avoid doing
  105. // it more than once per style change.
  106. // (Note that we also store an animation generation on each EffectSet in
  107. // order to track when we need to update animations on layers.)
  108. uint64_t mCheckGeneration;
  109. // Update mCheckGeneration to RestyleManager's count
  110. void UpdateCheckGeneration(nsPresContext* aPresContext);
  111. private:
  112. static nsIAtom* GetPropertyAtomForPseudoType(
  113. CSSPseudoElementType aPseudoType);
  114. #ifdef DEBUG
  115. bool mCalledPropertyDtor;
  116. #endif
  117. };
  118. } // namespace mozilla
  119. #endif // mozilla_AnimationCollection_h