DOMSVGTransformList.cpp 13 KB

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