DOMSVGAnimatedNumberList.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_DOMSVGANIMATEDNUMBERLIST_H__
  6. #define MOZILLA_DOMSVGANIMATEDNUMBERLIST_H__
  7. #include "nsCOMPtr.h"
  8. #include "nsCycleCollectionParticipant.h"
  9. #include "nsSVGElement.h"
  10. #include "nsWrapperCache.h"
  11. #include "mozilla/Attributes.h"
  12. namespace mozilla {
  13. class DOMSVGNumberList;
  14. class SVGAnimatedNumberList;
  15. class SVGNumberList;
  16. /**
  17. * Class DOMSVGAnimatedNumberList
  18. *
  19. * This class is used to create the DOM tearoff objects that wrap internal
  20. * SVGAnimatedNumberList objects.
  21. *
  22. * See the architecture comment in DOMSVGAnimatedLengthList.h (that's
  23. * LENGTH list). The comment for that class largly applies to this one too
  24. * and will go a long way to helping you understand the architecture here.
  25. *
  26. * This class is strongly intertwined with DOMSVGNumberList and DOMSVGNumber.
  27. * Our DOMSVGNumberList base and anim vals are friends and take care of nulling
  28. * out our pointers to them when they die (making our pointers to them true
  29. * weak refs).
  30. */
  31. class DOMSVGAnimatedNumberList final : public nsISupports,
  32. public nsWrapperCache
  33. {
  34. friend class DOMSVGNumberList;
  35. public:
  36. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  37. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGAnimatedNumberList)
  38. /**
  39. * Factory method to create and return a DOMSVGAnimatedNumberList wrapper
  40. * for a given internal SVGAnimatedNumberList object. The factory takes care
  41. * of caching the object that it returns so that the same object can be
  42. * returned for the given SVGAnimatedNumberList each time it is requested.
  43. * The cached object is only removed from the cache when it is destroyed due
  44. * to there being no more references to it or to any of its descendant
  45. * objects. If that happens, any subsequent call requesting the DOM wrapper
  46. * for the SVGAnimatedNumberList will naturally result in a new
  47. * DOMSVGAnimatedNumberList being returned.
  48. */
  49. static already_AddRefed<DOMSVGAnimatedNumberList>
  50. GetDOMWrapper(SVGAnimatedNumberList *aList,
  51. nsSVGElement *aElement,
  52. uint8_t aAttrEnum);
  53. /**
  54. * This method returns the DOMSVGAnimatedNumberList wrapper for an internal
  55. * SVGAnimatedNumberList object if it currently has a wrapper. If it does
  56. * not, then nullptr is returned.
  57. */
  58. static DOMSVGAnimatedNumberList*
  59. GetDOMWrapperIfExists(SVGAnimatedNumberList *aList);
  60. /**
  61. * Called by internal code to notify us when we need to sync the length of
  62. * our baseVal DOM list with its internal list. This is called just prior to
  63. * the length of the internal baseVal list being changed so that any DOM list
  64. * items that need to be removed from the DOM list can first get their values
  65. * from their internal counterpart.
  66. *
  67. * The only time this method could fail is on OOM when trying to increase the
  68. * length of the DOM list. If that happens then this method simply clears the
  69. * list and returns. Callers just proceed as normal, and we simply accept
  70. * that the DOM list will be empty (until successfully set to a new value).
  71. */
  72. void InternalBaseValListWillChangeTo(const SVGNumberList& aNewValue);
  73. void InternalAnimValListWillChangeTo(const SVGNumberList& aNewValue);
  74. /**
  75. * Returns true if our attribute is animating (in which case our animVal is
  76. * not simply a mirror of our baseVal).
  77. */
  78. bool IsAnimating() const;
  79. // WebIDL
  80. nsSVGElement* GetParentObject() const { return mElement; }
  81. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  82. // These aren't weak refs because mBaseVal and mAnimVal are weak
  83. already_AddRefed<DOMSVGNumberList> BaseVal();
  84. already_AddRefed<DOMSVGNumberList> AnimVal();
  85. private:
  86. /**
  87. * Only our static GetDOMWrapper() factory method may create objects of our
  88. * type.
  89. */
  90. DOMSVGAnimatedNumberList(nsSVGElement *aElement, uint8_t aAttrEnum)
  91. : mBaseVal(nullptr)
  92. , mAnimVal(nullptr)
  93. , mElement(aElement)
  94. , mAttrEnum(aAttrEnum)
  95. {
  96. }
  97. ~DOMSVGAnimatedNumberList();
  98. /// Get a reference to this DOM wrapper object's internal counterpart.
  99. SVGAnimatedNumberList& InternalAList();
  100. const SVGAnimatedNumberList& InternalAList() const;
  101. // Weak refs to our DOMSVGNumberList baseVal/animVal objects. These objects
  102. // are friends and take care of clearing these pointers when they die, making
  103. // these true weak references.
  104. DOMSVGNumberList *mBaseVal;
  105. DOMSVGNumberList *mAnimVal;
  106. // Strong ref to our element to keep it alive. We hold this not only for
  107. // ourself, but also for our base/animVal and all of their items.
  108. RefPtr<nsSVGElement> mElement;
  109. uint8_t mAttrEnum;
  110. };
  111. } // namespace mozilla
  112. #endif // MOZILLA_DOMSVGANIMATEDNUMBERLIST_H__