Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/svg/DOMSVGLengthList.cpp
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
#include "nsSVGElement.h"
8
#include "DOMSVGLengthList.h"
9
#include "DOMSVGLength.h"
10
#include "nsError.h"
11
#include "SVGAnimatedLengthList.h"
12
#include "nsCOMPtr.h"
13
#include "mozilla/dom/SVGLengthListBinding.h"
14
#include <algorithm>
15
16
// See the comment in this file's header.
17
18
// local helper functions
19
namespace {
20
21
using mozilla::DOMSVGLength;
22
23
void UpdateListIndicesFromIndex(FallibleTArray<DOMSVGLength*>& aItemsArray,
24
                                uint32_t aStartingIndex)
25
0
{
26
0
  uint32_t length = aItemsArray.Length();
27
0
28
0
  for (uint32_t i = aStartingIndex; i < length; ++i) {
29
0
    if (aItemsArray[i]) {
30
0
      aItemsArray[i]->UpdateListIndex(i);
31
0
    }
32
0
  }
33
0
}
34
35
} // namespace
36
37
namespace mozilla {
38
39
// We could use NS_IMPL_CYCLE_COLLECTION(, except that in Unlink() we need to
40
// clear our DOMSVGAnimatedLengthList's weak ref to us to be safe. (The other
41
// option would be to not unlink and rely on the breaking of the other edges in
42
// the cycle, as NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
43
NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGLengthList)
44
45
0
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGLengthList)
46
0
  if (tmp->mAList) {
47
0
    if (tmp->IsAnimValList()) {
48
0
      tmp->mAList->mAnimVal = nullptr;
49
0
    } else {
50
0
      tmp->mAList->mBaseVal = nullptr;
51
0
    }
52
0
    NS_IMPL_CYCLE_COLLECTION_UNLINK(mAList)
53
0
  }
54
0
  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
55
0
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
56
0
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGLengthList)
57
0
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAList)
58
0
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
59
0
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGLengthList)
60
0
  NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
61
0
NS_IMPL_CYCLE_COLLECTION_TRACE_END
62
63
NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGLengthList)
64
NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGLengthList)
65
66
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGLengthList)
67
0
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
68
0
  NS_INTERFACE_MAP_ENTRY(nsISupports)
69
0
NS_INTERFACE_MAP_END
70
71
JSObject*
72
DOMSVGLengthList::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto)
73
0
{
74
0
  return mozilla::dom::SVGLengthList_Binding::Wrap(cx, this, aGivenProto);
75
0
}
76
77
//----------------------------------------------------------------------
78
// Helper class: AutoChangeLengthListNotifier
79
// Stack-based helper class to pair calls to WillChangeLengthList and
80
// DidChangeLengthList.
81
class MOZ_RAII AutoChangeLengthListNotifier
82
{
83
public:
84
  explicit AutoChangeLengthListNotifier(DOMSVGLengthList* aLengthList MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
85
    : mLengthList(aLengthList)
86
0
  {
87
0
    MOZ_GUARD_OBJECT_NOTIFIER_INIT;
88
0
    MOZ_ASSERT(mLengthList, "Expecting non-null lengthList");
89
0
    mEmptyOrOldValue =
90
0
      mLengthList->Element()->WillChangeLengthList(mLengthList->AttrEnum());
91
0
  }
92
93
  ~AutoChangeLengthListNotifier()
94
0
  {
95
0
    mLengthList->Element()->DidChangeLengthList(mLengthList->AttrEnum(),
96
0
                                                mEmptyOrOldValue);
97
0
    if (mLengthList->IsAnimating()) {
98
0
      mLengthList->Element()->AnimationNeedsResample();
99
0
    }
100
0
  }
101
102
private:
103
  DOMSVGLengthList* const mLengthList;
104
  nsAttrValue       mEmptyOrOldValue;
105
  MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
106
};
107
108
void
109
DOMSVGLengthList::InternalListLengthWillChange(uint32_t aNewLength)
110
0
{
111
0
  uint32_t oldLength = mItems.Length();
112
0
113
0
  if (aNewLength > DOMSVGLength::MaxListIndex()) {
114
0
    // It's safe to get out of sync with our internal list as long as we have
115
0
    // FEWER items than it does.
116
0
    aNewLength = DOMSVGLength::MaxListIndex();
117
0
  }
118
0
119
0
  RefPtr<DOMSVGLengthList> kungFuDeathGrip;
120
0
  if (aNewLength < oldLength) {
121
0
    // RemovingFromList() might clear last reference to |this|.
122
0
    // Retain a temporary reference to keep from dying before returning.
123
0
    kungFuDeathGrip = this;
124
0
  }
125
0
126
0
  // If our length will decrease, notify the items that will be removed:
127
0
  for (uint32_t i = aNewLength; i < oldLength; ++i) {
128
0
    if (mItems[i]) {
129
0
      mItems[i]->RemovingFromList();
130
0
    }
131
0
  }
132
0
133
0
  if (!mItems.SetLength(aNewLength, fallible)) {
134
0
    // We silently ignore SetLength OOM failure since being out of sync is safe
135
0
    // so long as we have *fewer* items than our internal list.
136
0
    mItems.Clear();
137
0
    return;
138
0
  }
139
0
140
0
  // If our length has increased, null out the new pointers:
141
0
  for (uint32_t i = oldLength; i < aNewLength; ++i) {
142
0
    mItems[i] = nullptr;
143
0
  }
144
0
}
145
146
SVGLengthList&
147
DOMSVGLengthList::InternalList() const
148
0
{
149
0
  SVGAnimatedLengthList *alist = Element()->GetAnimatedLengthList(AttrEnum());
150
0
  return IsAnimValList() && alist->mAnimVal ? *alist->mAnimVal : alist->mBaseVal;
151
0
}
152
153
// ----------------------------------------------------------------------------
154
155
void
156
DOMSVGLengthList::Clear(ErrorResult& aError)
157
0
{
158
0
  if (IsAnimValList()) {
159
0
    aError.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
160
0
    return;
161
0
  }
162
0
163
0
  if (LengthNoFlush() > 0) {
164
0
    AutoChangeLengthListNotifier notifier(this);
165
0
    // Notify any existing DOM items of removal *before* truncating the lists
166
0
    // so that they can find their SVGLength internal counterparts and copy
167
0
    // their values. This also notifies the animVal list:
168
0
    mAList->InternalBaseValListWillChangeTo(SVGLengthList());
169
0
170
0
    mItems.Clear();
171
0
    InternalList().Clear();
172
0
  }
173
0
}
174
175
already_AddRefed<DOMSVGLength>
176
DOMSVGLengthList::Initialize(DOMSVGLength& newItem,
177
                             ErrorResult& error)
178
0
{
179
0
  if (IsAnimValList()) {
180
0
    error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
181
0
    return nullptr;
182
0
  }
183
0
184
0
  // If newItem already has an owner or is reflecting an attribute, we should
185
0
  // insert a clone of newItem, and for consistency, this should happen even if
186
0
  // *this* is the list that newItem is currently in. Note that in the case of
187
0
  // newItem being in this list, the Clear() call before the InsertItemBefore()
188
0
  // call would remove it from this list, and so the InsertItemBefore() call
189
0
  // would not insert a clone of newItem, it would actually insert newItem. To
190
0
  // prevent that from happening we have to do the clone here, if necessary.
191
0
192
0
  RefPtr<DOMSVGLength> domItem = &newItem;
193
0
  if (!domItem) {
194
0
    error.Throw(NS_ERROR_DOM_SVG_WRONG_TYPE_ERR);
195
0
    return nullptr;
196
0
  }
197
0
  if (domItem->HasOwner() || domItem->IsReflectingAttribute()) {
198
0
    domItem = domItem->Copy();
199
0
  }
200
0
201
0
  ErrorResult rv;
202
0
  Clear(rv);
203
0
  MOZ_ASSERT(!rv.Failed());
204
0
  return InsertItemBefore(*domItem, 0, error);
205
0
}
206
207
already_AddRefed<DOMSVGLength>
208
DOMSVGLengthList::GetItem(uint32_t index, ErrorResult& error)
209
0
{
210
0
  bool found;
211
0
  RefPtr<DOMSVGLength> item = IndexedGetter(index, found, error);
212
0
  if (!found) {
213
0
    error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
214
0
  }
215
0
  return item.forget();
216
0
}
217
218
already_AddRefed<DOMSVGLength>
219
DOMSVGLengthList::IndexedGetter(uint32_t index, bool& found, ErrorResult& error)
220
0
{
221
0
  if (IsAnimValList()) {
222
0
    Element()->FlushAnimations();
223
0
  }
224
0
  found = index < LengthNoFlush();
225
0
  if (found) {
226
0
    return GetItemAt(index);
227
0
  }
228
0
  return nullptr;
229
0
}
230
231
already_AddRefed<DOMSVGLength>
232
DOMSVGLengthList::InsertItemBefore(DOMSVGLength& newItem,
233
                                   uint32_t index,
234
                                   ErrorResult& error)
235
0
{
236
0
  if (IsAnimValList()) {
237
0
    error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
238
0
    return nullptr;
239
0
  }
240
0
241
0
  index = std::min(index, LengthNoFlush());
242
0
  if (index >= DOMSVGLength::MaxListIndex()) {
243
0
    error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
244
0
    return nullptr;
245
0
  }
246
0
247
0
  RefPtr<DOMSVGLength> domItem = &newItem;
248
0
  if (!domItem) {
249
0
    error.Throw(NS_ERROR_DOM_SVG_WRONG_TYPE_ERR);
250
0
    return nullptr;
251
0
  }
252
0
  if (domItem->HasOwner() || domItem->IsReflectingAttribute()) {
253
0
    domItem = domItem->Copy(); // must do this before changing anything!
254
0
  }
255
0
256
0
  // Ensure we have enough memory so we can avoid complex error handling below:
257
0
  if (!mItems.SetCapacity(mItems.Length() + 1, fallible) ||
258
0
      !InternalList().SetCapacity(InternalList().Length() + 1)) {
259
0
    error.Throw(NS_ERROR_OUT_OF_MEMORY);
260
0
    return nullptr;
261
0
  }
262
0
  if (AnimListMirrorsBaseList()) {
263
0
    if (!mAList->mAnimVal->mItems.SetCapacity(
264
0
          mAList->mAnimVal->mItems.Length() + 1, fallible)) {
265
0
      error.Throw(NS_ERROR_OUT_OF_MEMORY);
266
0
      return nullptr;
267
0
    }
268
0
  }
269
0
270
0
  AutoChangeLengthListNotifier notifier(this);
271
0
  // Now that we know we're inserting, keep animVal list in sync as necessary.
272
0
  MaybeInsertNullInAnimValListAt(index);
273
0
274
0
  InternalList().InsertItem(index, domItem->ToSVGLength());
275
0
  MOZ_ALWAYS_TRUE(mItems.InsertElementAt(index, domItem.get(), fallible));
276
0
277
0
  // This MUST come after the insertion into InternalList(), or else under the
278
0
  // insertion into InternalList() the values read from domItem would be bad
279
0
  // data from InternalList() itself!:
280
0
  domItem->InsertingIntoList(this, AttrEnum(), index, IsAnimValList());
281
0
282
0
  UpdateListIndicesFromIndex(mItems, index + 1);
283
0
284
0
  return domItem.forget();
285
0
}
286
287
already_AddRefed<DOMSVGLength>
288
DOMSVGLengthList::ReplaceItem(DOMSVGLength& newItem,
289
                              uint32_t index,
290
                              ErrorResult& error)
291
0
{
292
0
  if (IsAnimValList()) {
293
0
    error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
294
0
    return nullptr;
295
0
  }
296
0
297
0
  RefPtr<DOMSVGLength> domItem = &newItem;
298
0
  if (!domItem) {
299
0
    error.Throw(NS_ERROR_DOM_SVG_WRONG_TYPE_ERR);
300
0
    return nullptr;
301
0
  }
302
0
  if (index >= LengthNoFlush()) {
303
0
    error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
304
0
    return nullptr;
305
0
  }
306
0
  if (domItem->HasOwner() || domItem->IsReflectingAttribute()) {
307
0
    domItem = domItem->Copy(); // must do this before changing anything!
308
0
  }
309
0
310
0
  AutoChangeLengthListNotifier notifier(this);
311
0
  if (mItems[index]) {
312
0
    // Notify any existing DOM item of removal *before* modifying the lists so
313
0
    // that the DOM item can copy the *old* value at its index:
314
0
    mItems[index]->RemovingFromList();
315
0
  }
316
0
317
0
  InternalList()[index] = domItem->ToSVGLength();
318
0
  mItems[index] = domItem;
319
0
320
0
  // This MUST come after the ToSVGPoint() call, otherwise that call
321
0
  // would end up reading bad data from InternalList()!
322
0
  domItem->InsertingIntoList(this, AttrEnum(), index, IsAnimValList());
323
0
324
0
  return domItem.forget();
325
0
}
326
327
already_AddRefed<DOMSVGLength>
328
DOMSVGLengthList::RemoveItem(uint32_t index,
329
                             ErrorResult& error)
330
0
{
331
0
  if (IsAnimValList()) {
332
0
    error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
333
0
    return nullptr;
334
0
  }
335
0
336
0
  if (index >= LengthNoFlush()) {
337
0
    error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
338
0
    return nullptr;
339
0
  }
340
0
341
0
  AutoChangeLengthListNotifier notifier(this);
342
0
  // Now that we know we're removing, keep animVal list in sync as necessary.
343
0
  // Do this *before* touching InternalList() so the removed item can get its
344
0
  // internal value.
345
0
  MaybeRemoveItemFromAnimValListAt(index);
346
0
347
0
  // We have to return the removed item, so get it, creating it if necessary:
348
0
  nsCOMPtr<DOMSVGLength> result = GetItemAt(index);
349
0
350
0
  // Notify the DOM item of removal *before* modifying the lists so that the
351
0
  // DOM item can copy its *old* value:
352
0
  mItems[index]->RemovingFromList();
353
0
354
0
  InternalList().RemoveItem(index);
355
0
  mItems.RemoveElementAt(index);
356
0
357
0
  UpdateListIndicesFromIndex(mItems, index);
358
0
359
0
  return result.forget();
360
0
}
361
362
already_AddRefed<DOMSVGLength>
363
DOMSVGLengthList::GetItemAt(uint32_t aIndex)
364
0
{
365
0
  MOZ_ASSERT(aIndex < mItems.Length());
366
0
367
0
  if (!mItems[aIndex]) {
368
0
    mItems[aIndex] = new DOMSVGLength(this, AttrEnum(), aIndex, IsAnimValList());
369
0
  }
370
0
  RefPtr<DOMSVGLength> result = mItems[aIndex];
371
0
  return result.forget();
372
0
}
373
374
void
375
DOMSVGLengthList::MaybeInsertNullInAnimValListAt(uint32_t aIndex)
376
0
{
377
0
  MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
378
0
379
0
  if (!AnimListMirrorsBaseList()) {
380
0
    return;
381
0
  }
382
0
383
0
  DOMSVGLengthList* animVal = mAList->mAnimVal;
384
0
385
0
  MOZ_ASSERT(animVal, "AnimListMirrorsBaseList() promised a non-null animVal");
386
0
  MOZ_ASSERT(animVal->mItems.Length() == mItems.Length(),
387
0
             "animVal list not in sync!");
388
0
  MOZ_ALWAYS_TRUE(animVal->mItems.InsertElementAt(aIndex, nullptr, fallible));
389
0
390
0
  UpdateListIndicesFromIndex(animVal->mItems, aIndex + 1);
391
0
}
392
393
void
394
DOMSVGLengthList::MaybeRemoveItemFromAnimValListAt(uint32_t aIndex)
395
0
{
396
0
  MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
397
0
398
0
  if (!AnimListMirrorsBaseList()) {
399
0
    return;
400
0
  }
401
0
402
0
  // This needs to be a strong reference; otherwise, the RemovingFromList call
403
0
  // below might drop the last reference to animVal before we're done with it.
404
0
  RefPtr<DOMSVGLengthList> animVal = mAList->mAnimVal;
405
0
406
0
  MOZ_ASSERT(animVal, "AnimListMirrorsBaseList() promised a non-null animVal");
407
0
  MOZ_ASSERT(animVal->mItems.Length() == mItems.Length(),
408
0
             "animVal list not in sync!");
409
0
410
0
  if (animVal->mItems[aIndex]) {
411
0
    animVal->mItems[aIndex]->RemovingFromList();
412
0
  }
413
0
  animVal->mItems.RemoveElementAt(aIndex);
414
0
415
0
  UpdateListIndicesFromIndex(animVal->mItems, aIndex);
416
0
}
417
418
} // namespace mozilla