SVGAnimatedPathSegList.cpp 7.1 KB

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