SVGAnimatedPointList.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "SVGAnimatedPointList.h"
  6. #include "DOMSVGPointList.h"
  7. #include "mozilla/Move.h"
  8. #include "nsSVGElement.h"
  9. #include "nsSVGAttrTearoffTable.h"
  10. #include "nsSMILValue.h"
  11. #include "SVGPointListSMILType.h"
  12. // See the comments in this file's header!
  13. namespace mozilla {
  14. nsresult
  15. SVGAnimatedPointList::SetBaseValueString(const nsAString& aValue)
  16. {
  17. SVGPointList newBaseValue;
  18. // The spec says that the point 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. // DOMSVGPointList::InternalListWillChangeTo().
  27. DOMSVGPointList *baseValWrapper =
  28. DOMSVGPointList::GetDOMWrapperIfExists(GetBaseValKey());
  29. if (baseValWrapper) {
  30. baseValWrapper->InternalListWillChangeTo(newBaseValue);
  31. }
  32. DOMSVGPointList* animValWrapper = nullptr;
  33. if (!IsAnimating()) { // DOM anim val wraps our base val too!
  34. animValWrapper = DOMSVGPointList::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. SVGAnimatedPointList::ClearBaseValue()
  59. {
  60. // We must send these notifications *before* changing mBaseVal! (See above.)
  61. DOMSVGPointList *baseValWrapper =
  62. DOMSVGPointList::GetDOMWrapperIfExists(GetBaseValKey());
  63. if (baseValWrapper) {
  64. baseValWrapper->InternalListWillChangeTo(SVGPointList());
  65. }
  66. if (!IsAnimating()) { // DOM anim val wraps our base val too!
  67. DOMSVGPointList *animValWrapper =
  68. DOMSVGPointList::GetDOMWrapperIfExists(GetAnimValKey());
  69. if (animValWrapper) {
  70. animValWrapper->InternalListWillChangeTo(SVGPointList());
  71. }
  72. }
  73. mBaseVal.Clear();
  74. // Caller notifies
  75. }
  76. nsresult
  77. SVGAnimatedPointList::SetAnimValue(const SVGPointList& 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. // It is not possible for us to reliably distinguish between calls to this
  84. // method that are setting a new sample for an existing animation (in which
  85. // case our list length isn't changing and we wouldn't need to notify our DOM
  86. // wrapper to keep its length in sync), and calls to this method that are
  87. // setting the first sample of a new animation that will override the base
  88. // value/an existing animation (in which case our length may be changing and
  89. // our DOM wrapper may need to be notified). Happily though, it's cheap to
  90. // just blindly notify our animVal's DOM wrapper of our new value each time
  91. // this method is called, so that's what we do.
  92. // We must send this notification *before* changing mAnimVal! (See above.)
  93. DOMSVGPointList *domWrapper =
  94. DOMSVGPointList::GetDOMWrapperIfExists(GetAnimValKey());
  95. if (domWrapper) {
  96. domWrapper->InternalListWillChangeTo(aNewAnimValue);
  97. }
  98. if (!mAnimVal) {
  99. mAnimVal = new SVGPointList();
  100. }
  101. nsresult rv = mAnimVal->CopyFrom(aNewAnimValue);
  102. if (NS_FAILED(rv)) {
  103. // OOM. We clear the animation and, importantly, ClearAnimValue() ensures
  104. // that mAnimVal's DOM wrapper (if any) is kept in sync!
  105. ClearAnimValue(aElement);
  106. return rv;
  107. }
  108. aElement->DidAnimatePointList();
  109. return NS_OK;
  110. }
  111. void
  112. SVGAnimatedPointList::ClearAnimValue(nsSVGElement *aElement)
  113. {
  114. // We must send these notifications *before* changing mAnimVal! (See above.)
  115. DOMSVGPointList *domWrapper =
  116. DOMSVGPointList::GetDOMWrapperIfExists(GetAnimValKey());
  117. if (domWrapper) {
  118. // When all animation ends, animVal simply mirrors baseVal, which may have
  119. // a different number of items to the last active animated value.
  120. //
  121. domWrapper->InternalListWillChangeTo(mBaseVal);
  122. }
  123. mAnimVal = nullptr;
  124. aElement->DidAnimatePointList();
  125. }
  126. nsISMILAttr*
  127. SVGAnimatedPointList::ToSMILAttr(nsSVGElement *aElement)
  128. {
  129. return new SMILAnimatedPointList(this, aElement);
  130. }
  131. nsresult
  132. SVGAnimatedPointList::
  133. SMILAnimatedPointList::ValueFromString(const nsAString& aStr,
  134. const dom::SVGAnimationElement* /*aSrcElement*/,
  135. nsSMILValue& aValue,
  136. bool& aPreventCachingOfSandwich) const
  137. {
  138. nsSMILValue val(&SVGPointListSMILType::sSingleton);
  139. SVGPointListAndInfo *list = static_cast<SVGPointListAndInfo*>(val.mU.mPtr);
  140. nsresult rv = list->SetValueFromString(aStr);
  141. if (NS_SUCCEEDED(rv)) {
  142. list->SetInfo(mElement);
  143. aValue = Move(val);
  144. }
  145. aPreventCachingOfSandwich = false;
  146. return rv;
  147. }
  148. nsSMILValue
  149. SVGAnimatedPointList::SMILAnimatedPointList::GetBaseValue() const
  150. {
  151. // To benefit from Return Value Optimization and avoid copy constructor calls
  152. // due to our use of return-by-value, we must return the exact same object
  153. // from ALL return points. This function must only return THIS variable:
  154. nsSMILValue val;
  155. nsSMILValue tmp(&SVGPointListSMILType::sSingleton);
  156. SVGPointListAndInfo *list = static_cast<SVGPointListAndInfo*>(tmp.mU.mPtr);
  157. nsresult rv = list->CopyFrom(mVal->mBaseVal);
  158. if (NS_SUCCEEDED(rv)) {
  159. list->SetInfo(mElement);
  160. Swap(val, tmp);
  161. }
  162. return val;
  163. }
  164. nsresult
  165. SVGAnimatedPointList::SMILAnimatedPointList::SetAnimValue(const nsSMILValue& aValue)
  166. {
  167. NS_ASSERTION(aValue.mType == &SVGPointListSMILType::sSingleton,
  168. "Unexpected type to assign animated value");
  169. if (aValue.mType == &SVGPointListSMILType::sSingleton) {
  170. mVal->SetAnimValue(*static_cast<SVGPointListAndInfo*>(aValue.mU.mPtr),
  171. mElement);
  172. }
  173. return NS_OK;
  174. }
  175. void
  176. SVGAnimatedPointList::SMILAnimatedPointList::ClearAnimValue()
  177. {
  178. if (mVal->mAnimVal) {
  179. mVal->ClearAnimValue(mElement);
  180. }
  181. }
  182. } // namespace mozilla