Coverage Report

Created: 2018-09-25 14:53

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