Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / svg / DOMSVGLength.h
blob17bcefee5b33ff59621ea457dc88b9292c1040c0
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_SVG_DOMSVGLENGTH_H_
8 #define DOM_SVG_DOMSVGLENGTH_H_
10 #include "DOMSVGLengthList.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsDebug.h"
13 #include "nsTArray.h"
14 #include "SVGLength.h"
15 #include "mozilla/Attributes.h"
16 #include "nsWrapperCache.h"
18 #define MOZ_SVG_LIST_INDEX_BIT_COUNT 22 // supports > 4 million list items
20 namespace mozilla {
22 class ErrorResult;
24 namespace dom {
25 class SVGElement;
27 /**
28 * Class DOMSVGLength
30 * This class creates the DOM objects that wrap internal SVGLength objects that
31 * are in an SVGLengthList. It is also used to create the objects returned by
32 * SVGSVGElement.createSVGLength().
34 * For the DOM wrapper classes for non-list SVGLength, see SVGAnimatedLength.h.
36 * See the architecture comment in DOMSVGAnimatedLengthList.h.
38 * This class is strongly intertwined with DOMSVGAnimatedLengthList and
39 * DOMSVGLengthList. We are a friend of DOMSVGLengthList, and are responsible
40 * for nulling out our DOMSVGLengthList's pointer to us when we die, making it
41 * a real weak pointer.
43 * When objects of this type are in a DOMSVGLengthList they belong to an
44 * attribute. While they belong to an attribute, the objects' values come from
45 * their corresponding internal SVGLength objects in the internal SVGLengthList
46 * objects for the attribute. Getting and setting values of a DOMSVGLength
47 * requires reading and writing to its internal SVGLength. However, if the
48 * DOMSVGLength is detached from its DOMSVGLengthList then it first makes a
49 * copy of its internal SVGLength's value and unit so that it doesn't appear to
50 * "lose" its value from script's perspective on being removed from the list.
51 * This means that these DOM tearoffs have space to store these values, even
52 * though they're not used in the common case.
54 * Objects of this type are also used to reflect the baseVal and animVal of
55 * a single, non-list SVGLength attribute. Getting and settings values of the
56 * DOMSVGLength in this case requires reading and writing to the corresponding
57 * SVGAnimatedLength object.
59 * This class also stores its current list index, attribute enum, and whether
60 * it belongs to a baseVal or animVal list. This is so that objects of this
61 * type can find their corresponding internal SVGLength.
63 * To use these classes for <length> attributes as well as <list-of-length>
64 * attributes, we would need to take a bit from mListIndex and use that to
65 * indicate whether the object belongs to a list or non-list attribute, then
66 * if-else as appropriate. The bug for doing that work is:
67 * https://bugzilla.mozilla.org/show_bug.cgi?id=571734
69 class DOMSVGLength final : public nsWrapperCache {
70 template <class T>
71 friend class AutoChangeLengthListNotifier;
73 /**
74 * Ctor for creating the object returned by
75 * SVGAnimatedLength::ToDOMBaseVal/ToDOMAnimVal
77 DOMSVGLength(SVGAnimatedLength* aVal, dom::SVGElement* aSVGElement,
78 bool aAnimVal);
80 ~DOMSVGLength() { CleanupWeakRefs(); }
82 public:
83 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DOMSVGLength)
84 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(DOMSVGLength)
86 /**
87 * Generic ctor for DOMSVGLength objects that are created for an attribute.
89 DOMSVGLength(DOMSVGLengthList* aList, uint8_t aAttrEnum, uint32_t aListIndex,
90 bool aIsAnimValItem);
92 /**
93 * Ctor for creating the objects returned by SVGSVGElement.createSVGLength(),
94 * which do not initially belong to an attribute.
96 DOMSVGLength();
98 static already_AddRefed<DOMSVGLength> GetTearOff(SVGAnimatedLength* aVal,
99 dom::SVGElement* aSVGElement,
100 bool aAnimVal);
103 * Create an unowned copy of a length that is owned or is reflecting a single
104 * attribute. The caller is responsible for the first AddRef().
106 DOMSVGLength* Copy();
109 * Returns true if our attribute is animating.
111 bool IsAnimating() const;
114 * In future, if this class is used for non-list lengths, this will be
115 * different to IsInList().
117 bool HasOwner() const { return !!mOwner; }
120 * This method is called to notify this DOM object that it is being inserted
121 * into a list, and give it the information it needs as a result.
123 * This object MUST NOT already belong to a list when this method is called.
124 * That's not to say that script can't move these DOM objects between
125 * lists - it can - it's just that the logic to handle that (and send out
126 * the necessary notifications) is located elsewhere (in DOMSVGLengthList).)
128 void InsertingIntoList(DOMSVGLengthList* aList, uint8_t aAttrEnum,
129 uint32_t aListIndex, bool aIsAnimValItem);
131 static uint32_t MaxListIndex() {
132 return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT) - 1;
135 /// This method is called to notify this object that its list index changed.
136 void UpdateListIndex(uint32_t aListIndex) { mListIndex = aListIndex; }
139 * This method is called to notify this DOM object that it is about to be
140 * removed from its current DOM list so that it can first make a copy of its
141 * internal counterpart's values. (If it didn't do this, then it would
142 * "lose" its value on being removed.)
144 void RemovingFromList();
146 SVGLength ToSVGLength();
148 // WebIDL
149 uint16_t UnitType();
150 float GetValue(ErrorResult& aRv);
151 void SetValue(float aUserUnitValue, ErrorResult& aRv);
152 float ValueInSpecifiedUnits();
153 void SetValueInSpecifiedUnits(float aValue, ErrorResult& aRv);
154 void GetValueAsString(nsAString& aValue);
155 void SetValueAsString(const nsAString& aValue, ErrorResult& aRv);
156 void NewValueSpecifiedUnits(uint16_t aUnit, float aValue, ErrorResult& aRv);
157 void ConvertToSpecifiedUnits(uint16_t aUnit, ErrorResult& aRv);
159 nsISupports* GetParentObject() { return mOwner; }
161 JSObject* WrapObject(JSContext* aCx,
162 JS::Handle<JSObject*> aGivenProto) override;
164 private:
165 dom::SVGElement* Element();
167 uint8_t AttrEnum() const { return mAttrEnum; }
170 * Get a reference to the internal SVGLength list item that this DOM wrapper
171 * object currently wraps.
173 * To simplify the code we just have this one method for obtaining both
174 * baseVal and animVal internal items. This means that animVal items don't
175 * get const protection, but then our setter methods guard against changing
176 * animVal items.
178 SVGLength& InternalItem();
181 * Some values have units that depend on style so we may need to flush
182 * styles to ensure we get or set the right value in pixels.
184 void FlushStyleIfNeeded();
186 #ifdef DEBUG
187 bool IndexIsValid();
188 #endif
191 * Clears soon-to-be-invalid weak references in external objects that were
192 * set up during the creation of this object. This should be called during
193 * destruction and during cycle collection.
195 void CleanupWeakRefs();
197 RefPtr<nsISupports> mOwner; // Either a DOMSVGLengthList if we're in a list,
198 // an SVGElement if we're an attribute or null
200 // Bounds for the following are checked in the ctor, so be sure to update
201 // that if you change the capacity of any of the following.
203 uint32_t mListIndex : MOZ_SVG_LIST_INDEX_BIT_COUNT;
204 uint32_t mAttrEnum : 4; // supports up to 16 attributes
205 uint32_t mIsAnimValItem : 1;
207 // The following members are only used when we're not in a list:
208 uint32_t mUnit : 5; // can handle 31 units (the 10 SVG 1.1 units + rem, vw,
209 // vh, wm, calc + future additions)
210 float mValue = 0.0f;
213 } // namespace dom
214 } // namespace mozilla
216 #undef MOZ_SVG_LIST_INDEX_BIT_COUNT
218 #endif // DOM_SVG_DOMSVGLENGTH_H_