DOMSVGLength.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_DOMSVGLENGTH_H__
  6. #define MOZILLA_DOMSVGLENGTH_H__
  7. #include "DOMSVGLengthList.h"
  8. #include "nsCycleCollectionParticipant.h"
  9. #include "nsDebug.h"
  10. #include "nsIDOMSVGLength.h"
  11. #include "nsTArray.h"
  12. #include "SVGLength.h"
  13. #include "mozilla/Attributes.h"
  14. #include "nsWrapperCache.h"
  15. class nsSVGElement;
  16. // We make DOMSVGLength a pseudo-interface to allow us to QI to it in order to
  17. // check that the objects that scripts pass to DOMSVGLengthList methods are our
  18. // *native* length objects.
  19. //
  20. // {A8468350-7F7B-4976-9A7E-3765A1DADF9A}
  21. #define MOZILLA_DOMSVGLENGTH_IID \
  22. { 0xA8468350, 0x7F7B, 0x4976, { 0x9A, 0x7E, 0x37, 0x65, 0xA1, 0xDA, 0xDF, 0x9A } }
  23. #define MOZ_SVG_LIST_INDEX_BIT_COUNT 22 // supports > 4 million list items
  24. namespace mozilla {
  25. class ErrorResult;
  26. /**
  27. * Class DOMSVGLength
  28. *
  29. * This class creates the DOM objects that wrap internal SVGLength objects that
  30. * are in an SVGLengthList. It is also used to create the objects returned by
  31. * SVGSVGElement.createSVGLength().
  32. *
  33. * For the DOM wrapper classes for non-list SVGLength, see nsSVGLength2.h.
  34. *
  35. * See the architecture comment in DOMSVGAnimatedLengthList.h.
  36. *
  37. * This class is strongly intertwined with DOMSVGAnimatedLengthList and
  38. * DOMSVGLengthList. We are a friend of DOMSVGLengthList, and are responsible
  39. * for nulling out our DOMSVGLengthList's pointer to us when we die, making it
  40. * a real weak pointer.
  41. *
  42. * When objects of this type are in a DOMSVGLengthList they belong to an
  43. * attribute. While they belong to an attribute, the objects' values come from
  44. * their corresponding internal SVGLength objects in the internal SVGLengthList
  45. * objects for the attribute. Getting and setting values of a DOMSVGLength
  46. * requires reading and writing to its internal SVGLength. However, if the
  47. * DOMSVGLength is detached from its DOMSVGLengthList then it first makes a
  48. * copy of its internal SVGLength's value and unit so that it doesn't appear to
  49. * "lose" its value from script's perspective on being removed from the list.
  50. * This means that these DOM tearoffs have space to store these values, even
  51. * though they're not used in the common case.
  52. *
  53. * Objects of this type are also used to reflect the baseVal and animVal of
  54. * a single, non-list SVGLength attribute. Getting and settings values of the
  55. * DOMSVGLength in this case requires reading and writing to the corresponding
  56. * nsSVGLength2 object.
  57. *
  58. * This class also stores its current list index, attribute enum, and whether
  59. * it belongs to a baseVal or animVal list. This is so that objects of this
  60. * type can find their corresponding internal SVGLength.
  61. *
  62. * To use these classes for <length> attributes as well as <list-of-length>
  63. * attributes, we would need to take a bit from mListIndex and use that to
  64. * indicate whether the object belongs to a list or non-list attribute, then
  65. * if-else as appropriate. The bug for doing that work is:
  66. * https://bugzilla.mozilla.org/show_bug.cgi?id=571734
  67. */
  68. class DOMSVGLength final : public nsIDOMSVGLength,
  69. public nsWrapperCache
  70. {
  71. friend class AutoChangeLengthNotifier;
  72. /**
  73. * Ctor for creating the object returned by nsSVGLength2::ToDOMBaseVal/ToDOMAnimVal
  74. */
  75. DOMSVGLength(nsSVGLength2* aVal, nsSVGElement* aSVGElement, bool aAnimVal);
  76. ~DOMSVGLength();
  77. public:
  78. NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_DOMSVGLENGTH_IID)
  79. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  80. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGLength)
  81. NS_DECL_NSIDOMSVGLENGTH
  82. /**
  83. * Generic ctor for DOMSVGLength objects that are created for an attribute.
  84. */
  85. DOMSVGLength(DOMSVGLengthList *aList,
  86. uint8_t aAttrEnum,
  87. uint32_t aListIndex,
  88. bool aIsAnimValItem);
  89. /**
  90. * Ctor for creating the objects returned by SVGSVGElement.createSVGLength(),
  91. * which do not initially belong to an attribute.
  92. */
  93. DOMSVGLength();
  94. static already_AddRefed<DOMSVGLength> GetTearOff(nsSVGLength2* aVal,
  95. nsSVGElement* aSVGElement,
  96. bool aAnimVal);
  97. /**
  98. * Create an unowned copy of a length that is owned or is reflecting a single
  99. * attribute. The caller is responsible for the first AddRef().
  100. */
  101. DOMSVGLength* Copy();
  102. bool IsInList() const {
  103. return !!mList;
  104. }
  105. /**
  106. * In future, if this class is used for non-list lengths, this will be
  107. * different to IsInList().
  108. */
  109. bool HasOwner() const {
  110. return !!mList;
  111. }
  112. /**
  113. * Returns whether this length object is reflecting a single SVG element
  114. * attribute. This includes the baseVal or animVal of SVGRectElement.x, for
  115. * example, but not an item in an SVGLengthList, such as those in the
  116. * baseVal or animVal of SVGTextElement.x.
  117. */
  118. bool IsReflectingAttribute() const {
  119. return mVal;
  120. }
  121. /**
  122. * This method is called to notify this DOM object that it is being inserted
  123. * into a list, and give it the information it needs as a result.
  124. *
  125. * This object MUST NOT already belong to a list when this method is called.
  126. * That's not to say that script can't move these DOM objects between
  127. * lists - it can - it's just that the logic to handle that (and send out
  128. * the necessary notifications) is located elsewhere (in DOMSVGLengthList).)
  129. */
  130. void InsertingIntoList(DOMSVGLengthList *aList,
  131. uint8_t aAttrEnum,
  132. uint32_t aListIndex,
  133. bool aIsAnimValItem);
  134. static uint32_t MaxListIndex() {
  135. return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT) - 1;
  136. }
  137. /// This method is called to notify this object that its list index changed.
  138. void UpdateListIndex(uint32_t aListIndex) {
  139. mListIndex = aListIndex;
  140. }
  141. /**
  142. * This method is called to notify this DOM object that it is about to be
  143. * removed from its current DOM list so that it can first make a copy of its
  144. * internal counterpart's values. (If it didn't do this, then it would
  145. * "lose" its value on being removed.)
  146. */
  147. void RemovingFromList();
  148. SVGLength ToSVGLength();
  149. // WebIDL
  150. uint16_t UnitType();
  151. float GetValue(ErrorResult& aRv);
  152. void SetValue(float aValue, ErrorResult& aRv);
  153. float ValueInSpecifiedUnits();
  154. void SetValueInSpecifiedUnits(float aValue, ErrorResult& aRv);
  155. // The XPCOM GetValueAsString is good
  156. void SetValueAsString(const nsAString& aValue, ErrorResult& aRv);
  157. void NewValueSpecifiedUnits(uint16_t aUnit, float aValue,
  158. ErrorResult& aRv);
  159. void ConvertToSpecifiedUnits(uint16_t aUnit, ErrorResult& aRv);
  160. nsISupports* GetParentObject() const {
  161. auto svgElement = mList ? Element() : mSVGElement.get();
  162. return static_cast<nsIDOMSVGElement*> (svgElement);
  163. }
  164. JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  165. private:
  166. nsSVGElement* Element() const {
  167. return mList->Element();
  168. }
  169. uint8_t AttrEnum() const {
  170. return mAttrEnum;
  171. }
  172. /**
  173. * Get the axis that this length lies along. This method must only be called
  174. * when this object is associated with an element (HasOwner() returns true).
  175. */
  176. uint8_t Axis() const {
  177. return mList->Axis();
  178. }
  179. /**
  180. * Get a reference to the internal SVGLength list item that this DOM wrapper
  181. * object currently wraps.
  182. *
  183. * To simplify the code we just have this one method for obtaining both
  184. * baseVal and animVal internal items. This means that animVal items don't
  185. * get const protection, but then our setter methods guard against changing
  186. * animVal items.
  187. */
  188. SVGLength& InternalItem();
  189. #ifdef DEBUG
  190. bool IndexIsValid();
  191. #endif
  192. /**
  193. * Clears soon-to-be-invalid weak references in external objects that were
  194. * set up during the creation of this object. This should be called during
  195. * destruction and during cycle collection.
  196. */
  197. void CleanupWeakRefs();
  198. RefPtr<DOMSVGLengthList> mList;
  199. // Bounds for the following are checked in the ctor, so be sure to update
  200. // that if you change the capacity of any of the following.
  201. uint32_t mListIndex:MOZ_SVG_LIST_INDEX_BIT_COUNT;
  202. uint32_t mAttrEnum:4; // supports up to 16 attributes
  203. uint32_t mIsAnimValItem:1;
  204. // The following members are only used when we're not in a list:
  205. uint32_t mUnit:5; // can handle 31 units (the 10 SVG 1.1 units + rem, vw, vh, wm, calc + future additions)
  206. float mValue;
  207. // The following members are only used when we have an nsSVGLength2
  208. nsSVGLength2* mVal; // kept alive because it belongs to mSVGElement
  209. RefPtr<nsSVGElement> mSVGElement;
  210. };
  211. NS_DEFINE_STATIC_IID_ACCESSOR(DOMSVGLength, MOZILLA_DOMSVGLENGTH_IID)
  212. } // namespace mozilla
  213. #undef MOZ_SVG_LIST_INDEX_BIT_COUNT
  214. #endif // MOZILLA_DOMSVGLENGTH_H__