DOMSVGAnimatedLengthList.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_DOMSVGANIMATEDLENGTHLIST_H__
  6. #define MOZILLA_DOMSVGANIMATEDLENGTHLIST_H__
  7. #include "nsCOMPtr.h"
  8. #include "nsCycleCollectionParticipant.h"
  9. #include "nsSVGElement.h"
  10. #include "mozilla/Attributes.h"
  11. namespace mozilla {
  12. class SVGAnimatedLengthList;
  13. class SVGLengthList;
  14. class DOMSVGLengthList;
  15. /**
  16. * Class DOMSVGAnimatedLengthList
  17. *
  18. * This class is used to create the DOM tearoff objects that wrap internal
  19. * SVGAnimatedLengthList objects. We have this internal-DOM split because DOM
  20. * classes are relatively heavy-weight objects with non-optimal interfaces for
  21. * internal code, and they're relatively infrequently used. Having separate
  22. * internal and DOM classes does add complexity - especially for lists where
  23. * the internal list and DOM lists (and their items) need to be kept in sync -
  24. * but it keeps the internal classes light and fast, and in 99% of cases
  25. * they're all that's used. DOM wrappers are only instantiated when script
  26. * demands it.
  27. *
  28. * Ownership model:
  29. *
  30. * The diagram below shows the ownership model between the various DOM objects
  31. * in the tree of DOM objects that correspond to an SVG length list attribute.
  32. * The angled brackets ">" and "<" denote a reference from one object to
  33. * another, where the "!" character denotes a strong reference, and the "~"
  34. * character denotes a weak reference.
  35. *
  36. * .----<!----. .----<!----. .----<!----.
  37. * | | | | | |
  38. * element ~> DOMSVGAnimatedLengthList ~> DOMSVGLengthList ~> DOMSVGLength
  39. *
  40. * Rationale:
  41. *
  42. * The following three paragraphs explain the main three requirements that must
  43. * be met by any design. These are followed by an explanation of the rationale
  44. * behind our particular design.
  45. *
  46. * 1: DOMSVGAnimatedLengthList, DOMSVGLengthLists and DOMSVGLength get to their
  47. * internal counterparts via their element, and they use their element to send
  48. * out appropriate notifications when they change. Because of this, having
  49. * their element disappear out from under them would be very bad. To keep their
  50. * element alive at least as long as themselves, each of these classes must
  51. * contain a _strong_ reference (directly or indirectly) to their element.
  52. *
  53. * 2: Another central requirement of any design is the SVG specification's
  54. * requirement that script must always be given the exact same objects each
  55. * time it accesses a given object in a DOM object tree corresponding to an SVG
  56. * length list attribute. In practice "always" actually means "whenever script
  57. * has kept a references to a DOM object it previously accessed", since a
  58. * script will only be able to detect any difference in object identity if it
  59. * has a previous reference to compare against.
  60. *
  61. * 3: The wiggle room in the "same object" requirement leads us to a third
  62. * (self imposed) requirement: if script no longer has a reference to a given
  63. * DOM object from an object tree corresponding to an SVG length list
  64. * attribute, and if that object doesn't currently have any descendants, then
  65. * that object should be released to free up memory.
  66. *
  67. * To help in understanding our current design, consider this BROKEN design:
  68. *
  69. * .-------------------------------<!-------------------------.
  70. * |--------------------<!----------------. |
  71. * |----<!----. | |
  72. * | | | |
  73. * element ~> DOMSVGAnimatedLengthList !> DOMSVGLengthList !> DOMSVGLength
  74. *
  75. * Having all the objects keep a reference directly to their element like this
  76. * would reduce the number of dereferences that they need to make to get their
  77. * internal counterpart. Hovewer, this design does not meet the "same object"
  78. * requirement of the SVG specification. If script keeps a reference to a
  79. * DOMSVGLength or DOMSVGLengthList object, but not to that object's
  80. * DOMSVGAnimatedLengthList, then the DOMSVGAnimatedLengthList may be garbage
  81. * collected. We'd then have no way to return the same DOMSVGLength /
  82. * DOMSVGLengthList object that the script has a reference to if the script
  83. * went looking for it via the DOMSVGAnimatedLengthList property on the
  84. * element - we'd end up creating a fresh DOMSVGAnimatedLengthList, with no
  85. * knowlegde of the existing DOMSVGLengthList or DOMSVGLength object.
  86. *
  87. * The way we solve this problem is by making sure that parent objects cannot
  88. * die until all their children are dead by having child objects hold a strong
  89. * reference to their parent object. Note that this design means that the child
  90. * objects hold a strong reference to their element too, albeit indirectly via
  91. * the strong reference to their parent object:
  92. *
  93. * .----<!----. .----<!----. .----<!----.
  94. * | | | | | |
  95. * element ~> DOMSVGAnimatedLengthList ~> DOMSVGLengthList ~> DOMSVGLength
  96. *
  97. * One drawback of this design is that objects must look up their parent
  98. * chain to find their element, but that overhead is relatively small.
  99. */
  100. class DOMSVGAnimatedLengthList final : public nsWrapperCache
  101. {
  102. friend class DOMSVGLengthList;
  103. public:
  104. NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DOMSVGAnimatedLengthList)
  105. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(DOMSVGAnimatedLengthList)
  106. /**
  107. * Factory method to create and return a DOMSVGAnimatedLengthList wrapper
  108. * for a given internal SVGAnimatedLengthList object. The factory takes care
  109. * of caching the object that it returns so that the same object can be
  110. * returned for the given SVGAnimatedLengthList each time it is requested.
  111. * The cached object is only removed from the cache when it is destroyed due
  112. * to there being no more references to it or to any of its descendant
  113. * objects. If that happens, any subsequent call requesting the DOM wrapper
  114. * for the SVGAnimatedLengthList will naturally result in a new
  115. * DOMSVGAnimatedLengthList being returned.
  116. */
  117. static already_AddRefed<DOMSVGAnimatedLengthList>
  118. GetDOMWrapper(SVGAnimatedLengthList *aList,
  119. nsSVGElement *aElement,
  120. uint8_t aAttrEnum,
  121. uint8_t aAxis);
  122. /**
  123. * This method returns the DOMSVGAnimatedLengthList wrapper for an internal
  124. * SVGAnimatedLengthList object if it currently has a wrapper. If it does
  125. * not, then nullptr is returned.
  126. */
  127. static DOMSVGAnimatedLengthList*
  128. GetDOMWrapperIfExists(SVGAnimatedLengthList *aList);
  129. /**
  130. * Called by internal code to notify us when we need to sync the length of
  131. * our baseVal DOM list with its internal list. This is called just prior to
  132. * the length of the internal baseVal list being changed so that any DOM list
  133. * items that need to be removed from the DOM list can first get their values
  134. * from their internal counterpart.
  135. *
  136. * The only time this method could fail is on OOM when trying to increase the
  137. * length of the DOM list. If that happens then this method simply clears the
  138. * list and returns. Callers just proceed as normal, and we simply accept
  139. * that the DOM list will be empty (until successfully set to a new value).
  140. */
  141. void InternalBaseValListWillChangeTo(const SVGLengthList& aNewValue);
  142. void InternalAnimValListWillChangeTo(const SVGLengthList& aNewValue);
  143. /**
  144. * Returns true if our attribute is animating (in which case our animVal is
  145. * not simply a mirror of our baseVal).
  146. */
  147. bool IsAnimating() const;
  148. // WebIDL
  149. nsSVGElement* GetParentObject() const { return mElement; }
  150. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  151. // These aren't weak refs because mBaseVal and mAnimVal are weak
  152. already_AddRefed<DOMSVGLengthList> BaseVal();
  153. already_AddRefed<DOMSVGLengthList> AnimVal();
  154. private:
  155. /**
  156. * Only our static GetDOMWrapper() factory method may create objects of our
  157. * type.
  158. */
  159. DOMSVGAnimatedLengthList(nsSVGElement *aElement, uint8_t aAttrEnum, uint8_t aAxis)
  160. : mBaseVal(nullptr)
  161. , mAnimVal(nullptr)
  162. , mElement(aElement)
  163. , mAttrEnum(aAttrEnum)
  164. , mAxis(aAxis)
  165. {
  166. }
  167. ~DOMSVGAnimatedLengthList();
  168. /// Get a reference to this DOM wrapper object's internal counterpart.
  169. SVGAnimatedLengthList& InternalAList();
  170. const SVGAnimatedLengthList& InternalAList() const;
  171. // Weak refs to our DOMSVGLengthList baseVal/animVal objects. These objects
  172. // are friends and take care of clearing these pointers when they die, making
  173. // these true weak references.
  174. DOMSVGLengthList *mBaseVal;
  175. DOMSVGLengthList *mAnimVal;
  176. // Strong ref to our element to keep it alive. We hold this not only for
  177. // ourself, but also for our base/animVal and all of their items.
  178. RefPtr<nsSVGElement> mElement;
  179. uint8_t mAttrEnum;
  180. uint8_t mAxis;
  181. };
  182. } // namespace mozilla
  183. #endif // MOZILLA_DOMSVGANIMATEDLENGTHLIST_H__