Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/svg/SVGAnimatedPointList.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 "SVGAnimatedPointList.h"
8
9
#include "DOMSVGPointList.h"
10
#include "mozilla/Move.h"
11
#include "nsSVGElement.h"
12
#include "nsSVGAttrTearoffTable.h"
13
#include "nsSMILValue.h"
14
#include "SVGPointListSMILType.h"
15
16
// See the comments in this file's header!
17
18
namespace mozilla {
19
20
nsresult
21
SVGAnimatedPointList::SetBaseValueString(const nsAString& aValue)
22
0
{
23
0
  SVGPointList newBaseValue;
24
0
25
0
  // The spec says that the point data is parsed and accepted up to the first
26
0
  // error encountered, so we don't return early if an error occurs. However,
27
0
  // we do want to throw any error code from setAttribute if there's a problem.
28
0
29
0
  nsresult rv = newBaseValue.SetValueFromString(aValue);
30
0
31
0
  // We must send these notifications *before* changing mBaseVal! Our baseVal's
32
0
  // DOM wrapper list may have to remove DOM items from itself, and any removed
33
0
  // DOM items need to copy their internal counterpart's values *before* we
34
0
  // change them. See the comments in
35
0
  // DOMSVGPointList::InternalListWillChangeTo().
36
0
37
0
  DOMSVGPointList *baseValWrapper =
38
0
    DOMSVGPointList::GetDOMWrapperIfExists(GetBaseValKey());
39
0
  if (baseValWrapper) {
40
0
    baseValWrapper->InternalListWillChangeTo(newBaseValue);
41
0
  }
42
0
43
0
  DOMSVGPointList* animValWrapper = nullptr;
44
0
  if (!IsAnimating()) {  // DOM anim val wraps our base val too!
45
0
    animValWrapper = DOMSVGPointList::GetDOMWrapperIfExists(GetAnimValKey());
46
0
    if (animValWrapper) {
47
0
      animValWrapper->InternalListWillChangeTo(newBaseValue);
48
0
    }
49
0
  }
50
0
51
0
  // Only now may we modify mBaseVal!
52
0
53
0
  // We don't need to call DidChange* here - we're only called by
54
0
  // nsSVGElement::ParseAttribute under Element::SetAttr,
55
0
  // which takes care of notifying.
56
0
57
0
  nsresult rv2 = mBaseVal.CopyFrom(newBaseValue);
58
0
  if (NS_FAILED(rv2)) {
59
0
    // Attempting to increase mBaseVal's length failed (mBaseVal is left
60
0
    // unmodified). We MUST keep any DOM wrappers in sync:
61
0
    if (baseValWrapper) {
62
0
      baseValWrapper->InternalListWillChangeTo(mBaseVal);
63
0
    }
64
0
    if (animValWrapper) {
65
0
      animValWrapper->InternalListWillChangeTo(mBaseVal);
66
0
    }
67
0
    return rv2;
68
0
  }
69
0
  return rv;
70
0
}
71
72
void
73
SVGAnimatedPointList::ClearBaseValue()
74
0
{
75
0
  // We must send these notifications *before* changing mBaseVal! (See above.)
76
0
77
0
  DOMSVGPointList *baseValWrapper =
78
0
    DOMSVGPointList::GetDOMWrapperIfExists(GetBaseValKey());
79
0
  if (baseValWrapper) {
80
0
    baseValWrapper->InternalListWillChangeTo(SVGPointList());
81
0
  }
82
0
83
0
  if (!IsAnimating()) { // DOM anim val wraps our base val too!
84
0
    DOMSVGPointList *animValWrapper =
85
0
      DOMSVGPointList::GetDOMWrapperIfExists(GetAnimValKey());
86
0
    if (animValWrapper) {
87
0
      animValWrapper->InternalListWillChangeTo(SVGPointList());
88
0
    }
89
0
  }
90
0
91
0
  mBaseVal.Clear();
92
0
  // Caller notifies
93
0
}
94
95
nsresult
96
SVGAnimatedPointList::SetAnimValue(const SVGPointList& aNewAnimValue,
97
                                   nsSVGElement *aElement)
98
0
{
99
0
  // Note that a new animation may totally change the number of items in the
100
0
  // animVal list, either replacing what was essentially a mirror of the
101
0
  // baseVal list, or else replacing and overriding an existing animation.
102
0
  // It is not possible for us to reliably distinguish between calls to this
103
0
  // method that are setting a new sample for an existing animation (in which
104
0
  // case our list length isn't changing and we wouldn't need to notify our DOM
105
0
  // wrapper to keep its length in sync), and calls to this method that are
106
0
  // setting the first sample of a new animation that will override the base
107
0
  // value/an existing animation (in which case our length may be changing and
108
0
  // our DOM wrapper may need to be notified). Happily though, it's cheap to
109
0
  // just blindly notify our animVal's DOM wrapper of our new value each time
110
0
  // this method is called, so that's what we do.
111
0
112
0
  // We must send this notification *before* changing mAnimVal! (See above.)
113
0
114
0
  DOMSVGPointList *domWrapper =
115
0
    DOMSVGPointList::GetDOMWrapperIfExists(GetAnimValKey());
116
0
  if (domWrapper) {
117
0
    domWrapper->InternalListWillChangeTo(aNewAnimValue);
118
0
  }
119
0
  if (!mAnimVal) {
120
0
    mAnimVal = new SVGPointList();
121
0
  }
122
0
  nsresult rv = mAnimVal->CopyFrom(aNewAnimValue);
123
0
  if (NS_FAILED(rv)) {
124
0
    // OOM. We clear the animation and, importantly, ClearAnimValue() ensures
125
0
    // that mAnimVal's DOM wrapper (if any) is kept in sync!
126
0
    ClearAnimValue(aElement);
127
0
    return rv;
128
0
  }
129
0
  aElement->DidAnimatePointList();
130
0
  return NS_OK;
131
0
}
132
133
void
134
SVGAnimatedPointList::ClearAnimValue(nsSVGElement *aElement)
135
0
{
136
0
  // We must send these notifications *before* changing mAnimVal! (See above.)
137
0
138
0
  DOMSVGPointList *domWrapper =
139
0
    DOMSVGPointList::GetDOMWrapperIfExists(GetAnimValKey());
140
0
  if (domWrapper) {
141
0
    // When all animation ends, animVal simply mirrors baseVal, which may have
142
0
    // a different number of items to the last active animated value.
143
0
    //
144
0
    domWrapper->InternalListWillChangeTo(mBaseVal);
145
0
  }
146
0
  mAnimVal = nullptr;
147
0
  aElement->DidAnimatePointList();
148
0
}
149
150
UniquePtr<nsISMILAttr>
151
SVGAnimatedPointList::ToSMILAttr(nsSVGElement *aElement)
152
0
{
153
0
  return MakeUnique<SMILAnimatedPointList>(this, aElement);
154
0
}
155
156
nsresult
157
SVGAnimatedPointList::
158
  SMILAnimatedPointList::ValueFromString(const nsAString& aStr,
159
                               const dom::SVGAnimationElement* /*aSrcElement*/,
160
                               nsSMILValue& aValue,
161
                               bool& aPreventCachingOfSandwich) const
162
0
{
163
0
  nsSMILValue val(&SVGPointListSMILType::sSingleton);
164
0
  SVGPointListAndInfo *list = static_cast<SVGPointListAndInfo*>(val.mU.mPtr);
165
0
  nsresult rv = list->SetValueFromString(aStr);
166
0
  if (NS_SUCCEEDED(rv)) {
167
0
    list->SetInfo(mElement);
168
0
    aValue = std::move(val);
169
0
  }
170
0
  aPreventCachingOfSandwich = false;
171
0
  return rv;
172
0
}
173
174
nsSMILValue
175
SVGAnimatedPointList::SMILAnimatedPointList::GetBaseValue() const
176
0
{
177
0
  // To benefit from Return Value Optimization and avoid copy constructor calls
178
0
  // due to our use of return-by-value, we must return the exact same object
179
0
  // from ALL return points. This function must only return THIS variable:
180
0
  nsSMILValue val;
181
0
182
0
  nsSMILValue tmp(&SVGPointListSMILType::sSingleton);
183
0
  SVGPointListAndInfo *list = static_cast<SVGPointListAndInfo*>(tmp.mU.mPtr);
184
0
  nsresult rv = list->CopyFrom(mVal->mBaseVal);
185
0
  if (NS_SUCCEEDED(rv)) {
186
0
    list->SetInfo(mElement);
187
0
    Swap(val, tmp);
188
0
  }
189
0
  return val;
190
0
}
191
192
nsresult
193
SVGAnimatedPointList::SMILAnimatedPointList::SetAnimValue(const nsSMILValue& aValue)
194
0
{
195
0
  NS_ASSERTION(aValue.mType == &SVGPointListSMILType::sSingleton,
196
0
               "Unexpected type to assign animated value");
197
0
  if (aValue.mType == &SVGPointListSMILType::sSingleton) {
198
0
    mVal->SetAnimValue(*static_cast<SVGPointListAndInfo*>(aValue.mU.mPtr),
199
0
                       mElement);
200
0
  }
201
0
  return NS_OK;
202
0
}
203
204
void
205
SVGAnimatedPointList::SMILAnimatedPointList::ClearAnimValue()
206
0
{
207
0
  if (mVal->mAnimVal) {
208
0
    mVal->ClearAnimValue(mElement);
209
0
  }
210
0
}
211
212
} // namespace mozilla