LCOV - code coverage report
Current view: directory - content/svg/content/src - DOMSVGNumberList.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 179 2 1.1 %
Date: 2012-06-02 Functions: 26 2 7.7 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is Mozilla SVG Project code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is the Mozilla Foundation.
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2010
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *
      23                 :  * Alternatively, the contents of this file may be used under the terms of
      24                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      25                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      26                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      27                 :  * of those above. If you wish to allow use of your version of this file only
      28                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      29                 :  * use your version of this file under the terms of the MPL, indicate your
      30                 :  * decision by deleting the provisions above and replace them with the notice
      31                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      32                 :  * the provisions above, a recipient may use your version of this file under
      33                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      34                 :  *
      35                 :  * ***** END LICENSE BLOCK ***** */
      36                 : 
      37                 : #include "nsSVGElement.h"
      38                 : #include "DOMSVGNumberList.h"
      39                 : #include "DOMSVGNumber.h"
      40                 : #include "nsDOMError.h"
      41                 : #include "SVGAnimatedNumberList.h"
      42                 : #include "nsCOMPtr.h"
      43                 : #include "nsContentUtils.h"
      44                 : #include "dombindings.h"
      45                 : 
      46                 : // See the comment in this file's header.
      47                 : 
      48                 : namespace mozilla {
      49                 : 
      50                 : // local helper functions
      51                 : namespace {
      52                 : 
      53                 : using mozilla::DOMSVGNumber;
      54                 : 
      55               0 : void UpdateListIndicesFromIndex(nsTArray<DOMSVGNumber*>& aItemsArray,
      56                 :                                 PRUint32 aStartingIndex)
      57                 : {
      58               0 :   PRUint32 length = aItemsArray.Length();
      59                 : 
      60               0 :   for (PRUint32 i = aStartingIndex; i < length; ++i) {
      61               0 :     if (aItemsArray[i]) {
      62               0 :       aItemsArray[i]->UpdateListIndex(i);
      63                 :     }
      64                 :   }
      65               0 : }
      66                 : 
      67                 : } // namespace
      68                 : 
      69                 : // We could use NS_IMPL_CYCLE_COLLECTION_1, except that in Unlink() we need to
      70                 : // clear our DOMSVGAnimatedNumberList's weak ref to us to be safe. (The other
      71                 : // option would be to not unlink and rely on the breaking of the other edges in
      72                 : // the cycle, as NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
      73            1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGNumberList)
      74               0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGNumberList)
      75                 :   // No need to null check tmp - script/SMIL can't detach us from mAList
      76               0 :   ( tmp->IsAnimValList() ? tmp->mAList->mAnimVal : tmp->mAList->mBaseVal ) = nsnull;
      77               0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mAList)
      78               0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
      79               0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
      80               0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGNumberList)
      81               0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mAList)
      82               0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
      83               0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
      84               0 : NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGNumberList)
      85               0 :   NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
      86               0 : NS_IMPL_CYCLE_COLLECTION_TRACE_END
      87                 : 
      88               0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGNumberList)
      89               0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGNumberList)
      90                 : 
      91                 : } // namespace mozilla
      92                 : DOMCI_DATA(SVGNumberList, mozilla::DOMSVGNumberList)
      93                 : namespace mozilla {
      94                 : 
      95               0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGNumberList)
      96               0 :   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
      97               0 :   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGNumberList)
      98               0 :   NS_INTERFACE_MAP_ENTRY(nsISupports)
      99               0 :   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(SVGNumberList)
     100               0 : NS_INTERFACE_MAP_END
     101                 : 
     102                 : 
     103                 : JSObject*
     104               0 : DOMSVGNumberList::WrapObject(JSContext *cx, XPCWrappedNativeScope *scope,
     105                 :                              bool *triedToWrap)
     106                 : {
     107                 :   return mozilla::dom::binding::SVGNumberList::create(cx, scope, this,
     108               0 :                                                       triedToWrap);
     109                 : }
     110                 : 
     111                 : nsIDOMSVGNumber*
     112               0 : DOMSVGNumberList::GetItemAt(PRUint32 aIndex)
     113                 : {
     114               0 :   if (IsAnimValList()) {
     115               0 :     Element()->FlushAnimations();
     116                 :   }
     117               0 :   if (aIndex < Length()) {
     118               0 :     EnsureItemAt(aIndex);
     119               0 :     return mItems[aIndex];
     120                 :   }
     121               0 :   return nsnull;
     122                 : }
     123                 : 
     124                 : void
     125               0 : DOMSVGNumberList::InternalListLengthWillChange(PRUint32 aNewLength)
     126                 : {
     127               0 :   PRUint32 oldLength = mItems.Length();
     128                 : 
     129               0 :   if (aNewLength > DOMSVGNumber::MaxListIndex()) {
     130                 :     // It's safe to get out of sync with our internal list as long as we have
     131                 :     // FEWER items than it does.
     132               0 :     aNewLength = DOMSVGNumber::MaxListIndex();
     133                 :   }
     134                 : 
     135               0 :   nsRefPtr<DOMSVGNumberList> kungFuDeathGrip;
     136               0 :   if (aNewLength < oldLength) {
     137                 :     // RemovingFromList() might clear last reference to |this|.
     138                 :     // Retain a temporary reference to keep from dying before returning.
     139               0 :     kungFuDeathGrip = this;
     140                 :   }
     141                 : 
     142                 :   // If our length will decrease, notify the items that will be removed:
     143               0 :   for (PRUint32 i = aNewLength; i < oldLength; ++i) {
     144               0 :     if (mItems[i]) {
     145               0 :       mItems[i]->RemovingFromList();
     146                 :     }
     147                 :   }
     148                 : 
     149               0 :   if (!mItems.SetLength(aNewLength)) {
     150                 :     // We silently ignore SetLength OOM failure since being out of sync is safe
     151                 :     // so long as we have *fewer* items than our internal list.
     152               0 :     mItems.Clear();
     153                 :     return;
     154                 :   }
     155                 : 
     156                 :   // If our length has increased, null out the new pointers:
     157               0 :   for (PRUint32 i = oldLength; i < aNewLength; ++i) {
     158               0 :     mItems[i] = nsnull;
     159                 :   }
     160                 : }
     161                 : 
     162                 : SVGNumberList&
     163               0 : DOMSVGNumberList::InternalList() const
     164                 : {
     165               0 :   SVGAnimatedNumberList *alist = Element()->GetAnimatedNumberList(AttrEnum());
     166               0 :   return IsAnimValList() && alist->mAnimVal ? *alist->mAnimVal : alist->mBaseVal;
     167                 : }
     168                 : 
     169                 : // ----------------------------------------------------------------------------
     170                 : // nsIDOMSVGNumberList implementation:
     171                 : 
     172                 : NS_IMETHODIMP
     173               0 : DOMSVGNumberList::GetNumberOfItems(PRUint32 *aNumberOfItems)
     174                 : {
     175               0 :   if (IsAnimValList()) {
     176               0 :     Element()->FlushAnimations();
     177                 :   }
     178               0 :   *aNumberOfItems = Length();
     179               0 :   return NS_OK;
     180                 : }
     181                 : 
     182                 : NS_IMETHODIMP
     183               0 : DOMSVGNumberList::Clear()
     184                 : {
     185               0 :   if (IsAnimValList()) {
     186               0 :     return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
     187                 :   }
     188                 : 
     189               0 :   if (Length() > 0) {
     190               0 :     nsAttrValue emptyOrOldValue = Element()->WillChangeNumberList(AttrEnum());
     191                 :     // Notify any existing DOM items of removal *before* truncating the lists
     192                 :     // so that they can find their SVGNumber internal counterparts and copy
     193                 :     // their values. This also notifies the animVal list:
     194               0 :     mAList->InternalBaseValListWillChangeTo(SVGNumberList());
     195                 : 
     196               0 :     mItems.Clear();
     197               0 :     InternalList().Clear();
     198               0 :     Element()->DidChangeNumberList(AttrEnum(), emptyOrOldValue);
     199               0 :     if (mAList->IsAnimating()) {
     200               0 :       Element()->AnimationNeedsResample();
     201                 :     }
     202                 :   }
     203               0 :   return NS_OK;
     204                 : }
     205                 : 
     206                 : NS_IMETHODIMP
     207               0 : DOMSVGNumberList::Initialize(nsIDOMSVGNumber *newItem,
     208                 :                              nsIDOMSVGNumber **_retval)
     209                 : {
     210               0 :   *_retval = nsnull;
     211               0 :   if (IsAnimValList()) {
     212               0 :     return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
     213                 :   }
     214                 : 
     215                 :   // If newItem is already in a list we should insert a clone of newItem, and
     216                 :   // for consistency, this should happen even if *this* is the list that
     217                 :   // newItem is currently in. Note that in the case of newItem being in this
     218                 :   // list, the Clear() call before the InsertItemBefore() call would remove it
     219                 :   // from this list, and so the InsertItemBefore() call would not insert a
     220                 :   // clone of newItem, it would actually insert newItem. To prevent that from
     221                 :   // happening we have to do the clone here, if necessary.
     222                 : 
     223               0 :   nsCOMPtr<DOMSVGNumber> domItem = do_QueryInterface(newItem);
     224               0 :   if (!domItem) {
     225               0 :     return NS_ERROR_DOM_SVG_WRONG_TYPE_ERR;
     226                 :   }
     227               0 :   if (domItem->HasOwner()) {
     228               0 :     newItem = domItem->Clone();
     229                 :   }
     230                 : 
     231               0 :   Clear();
     232               0 :   return InsertItemBefore(newItem, 0, _retval);
     233                 : }
     234                 : 
     235                 : NS_IMETHODIMP
     236               0 : DOMSVGNumberList::GetItem(PRUint32 index,
     237                 :                           nsIDOMSVGNumber **_retval)
     238                 : {
     239               0 :   *_retval = GetItemAt(index);
     240               0 :   if (!*_retval) {
     241               0 :     return NS_ERROR_DOM_INDEX_SIZE_ERR;
     242                 :   }
     243               0 :   NS_ADDREF(*_retval);
     244               0 :   return NS_OK;
     245                 : }
     246                 : 
     247                 : NS_IMETHODIMP
     248               0 : DOMSVGNumberList::InsertItemBefore(nsIDOMSVGNumber *newItem,
     249                 :                                    PRUint32 index,
     250                 :                                    nsIDOMSVGNumber **_retval)
     251                 : {
     252               0 :   *_retval = nsnull;
     253               0 :   if (IsAnimValList()) {
     254               0 :     return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
     255                 :   }
     256                 : 
     257               0 :   index = NS_MIN(index, Length());
     258               0 :   if (index >= DOMSVGNumber::MaxListIndex()) {
     259               0 :     return NS_ERROR_DOM_INDEX_SIZE_ERR;
     260                 :   }
     261                 : 
     262               0 :   nsCOMPtr<DOMSVGNumber> domItem = do_QueryInterface(newItem);
     263               0 :   if (!domItem) {
     264               0 :     return NS_ERROR_DOM_SVG_WRONG_TYPE_ERR;
     265                 :   }
     266               0 :   if (domItem->HasOwner()) {
     267               0 :     domItem = domItem->Clone(); // must do this before changing anything!
     268                 :   }
     269                 : 
     270                 :   // Ensure we have enough memory so we can avoid complex error handling below:
     271               0 :   if (!mItems.SetCapacity(mItems.Length() + 1) ||
     272               0 :       !InternalList().SetCapacity(InternalList().Length() + 1)) {
     273               0 :     return NS_ERROR_OUT_OF_MEMORY;
     274                 :   }
     275                 : 
     276               0 :   nsAttrValue emptyOrOldValue = Element()->WillChangeNumberList(AttrEnum());
     277                 :   // Now that we know we're inserting, keep animVal list in sync as necessary.
     278               0 :   MaybeInsertNullInAnimValListAt(index);
     279                 : 
     280               0 :   InternalList().InsertItem(index, domItem->ToSVGNumber());
     281               0 :   mItems.InsertElementAt(index, domItem.get());
     282                 : 
     283                 :   // This MUST come after the insertion into InternalList(), or else under the
     284                 :   // insertion into InternalList() the values read from domItem would be bad
     285                 :   // data from InternalList() itself!:
     286               0 :   domItem->InsertingIntoList(this, AttrEnum(), index, IsAnimValList());
     287                 : 
     288               0 :   UpdateListIndicesFromIndex(mItems, index + 1);
     289                 : 
     290               0 :   Element()->DidChangeNumberList(AttrEnum(), emptyOrOldValue);
     291               0 :   if (mAList->IsAnimating()) {
     292               0 :     Element()->AnimationNeedsResample();
     293                 :   }
     294               0 :   *_retval = domItem.forget().get();
     295               0 :   return NS_OK;
     296                 : }
     297                 : 
     298                 : NS_IMETHODIMP
     299               0 : DOMSVGNumberList::ReplaceItem(nsIDOMSVGNumber *newItem,
     300                 :                               PRUint32 index,
     301                 :                               nsIDOMSVGNumber **_retval)
     302                 : {
     303               0 :   *_retval = nsnull;
     304               0 :   if (IsAnimValList()) {
     305               0 :     return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
     306                 :   }
     307                 : 
     308               0 :   nsCOMPtr<DOMSVGNumber> domItem = do_QueryInterface(newItem);
     309               0 :   if (!domItem) {
     310               0 :     return NS_ERROR_DOM_SVG_WRONG_TYPE_ERR;
     311                 :   }
     312               0 :   if (index >= Length()) {
     313               0 :     return NS_ERROR_DOM_INDEX_SIZE_ERR;
     314                 :   }
     315               0 :   if (domItem->HasOwner()) {
     316               0 :     domItem = domItem->Clone(); // must do this before changing anything!
     317                 :   }
     318                 : 
     319               0 :   nsAttrValue emptyOrOldValue = Element()->WillChangeNumberList(AttrEnum());
     320               0 :   if (mItems[index]) {
     321                 :     // Notify any existing DOM item of removal *before* modifying the lists so
     322                 :     // that the DOM item can copy the *old* value at its index:
     323               0 :     mItems[index]->RemovingFromList();
     324                 :   }
     325                 : 
     326               0 :   InternalList()[index] = domItem->ToSVGNumber();
     327               0 :   mItems[index] = domItem;
     328                 : 
     329                 :   // This MUST come after the ToSVGPoint() call, otherwise that call
     330                 :   // would end up reading bad data from InternalList()!
     331               0 :   domItem->InsertingIntoList(this, AttrEnum(), index, IsAnimValList());
     332                 : 
     333               0 :   Element()->DidChangeNumberList(AttrEnum(), emptyOrOldValue);
     334               0 :   if (mAList->IsAnimating()) {
     335               0 :     Element()->AnimationNeedsResample();
     336                 :   }
     337               0 :   NS_ADDREF(*_retval = domItem.get());
     338               0 :   return NS_OK;
     339                 : }
     340                 : 
     341                 : NS_IMETHODIMP
     342               0 : DOMSVGNumberList::RemoveItem(PRUint32 index,
     343                 :                              nsIDOMSVGNumber **_retval)
     344                 : {
     345               0 :   *_retval = nsnull;
     346               0 :   if (IsAnimValList()) {
     347               0 :     return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
     348                 :   }
     349                 : 
     350               0 :   if (index >= Length()) {
     351               0 :     return NS_ERROR_DOM_INDEX_SIZE_ERR;
     352                 :   }
     353                 : 
     354                 :   // Now that we know we're removing, keep animVal list in sync as necessary.
     355                 :   // Do this *before* touching InternalList() so the removed item can get its
     356                 :   // internal value.
     357               0 :   MaybeRemoveItemFromAnimValListAt(index);
     358                 : 
     359                 :   // We have to return the removed item, so make sure it exists:
     360               0 :   EnsureItemAt(index);
     361                 : 
     362               0 :   nsAttrValue emptyOrOldValue = Element()->WillChangeNumberList(AttrEnum());
     363                 :   // Notify the DOM item of removal *before* modifying the lists so that the
     364                 :   // DOM item can copy its *old* value:
     365               0 :   mItems[index]->RemovingFromList();
     366               0 :   NS_ADDREF(*_retval = mItems[index]);
     367                 : 
     368               0 :   InternalList().RemoveItem(index);
     369               0 :   mItems.RemoveElementAt(index);
     370                 : 
     371               0 :   UpdateListIndicesFromIndex(mItems, index);
     372                 : 
     373               0 :   Element()->DidChangeNumberList(AttrEnum(), emptyOrOldValue);
     374               0 :   if (mAList->IsAnimating()) {
     375               0 :     Element()->AnimationNeedsResample();
     376                 :   }
     377               0 :   return NS_OK;
     378                 : }
     379                 : 
     380                 : NS_IMETHODIMP
     381               0 : DOMSVGNumberList::AppendItem(nsIDOMSVGNumber *newItem,
     382                 :                              nsIDOMSVGNumber **_retval)
     383                 : {
     384               0 :   return InsertItemBefore(newItem, Length(), _retval);
     385                 : }
     386                 : 
     387                 : NS_IMETHODIMP
     388               0 : DOMSVGNumberList::GetLength(PRUint32 *aNumberOfItems)
     389                 : {
     390               0 :   return GetNumberOfItems(aNumberOfItems);
     391                 : }
     392                 : 
     393                 : void
     394               0 : DOMSVGNumberList::EnsureItemAt(PRUint32 aIndex)
     395                 : {
     396               0 :   if (!mItems[aIndex]) {
     397               0 :     mItems[aIndex] = new DOMSVGNumber(this, AttrEnum(), aIndex, IsAnimValList());
     398                 :   }
     399               0 : }
     400                 : 
     401                 : void
     402               0 : DOMSVGNumberList::MaybeInsertNullInAnimValListAt(PRUint32 aIndex)
     403                 : {
     404               0 :   NS_ABORT_IF_FALSE(!IsAnimValList(), "call from baseVal to animVal");
     405                 : 
     406               0 :   DOMSVGNumberList* animVal = mAList->mAnimVal;
     407                 : 
     408               0 :   if (!animVal || mAList->IsAnimating()) {
     409                 :     // No animVal list wrapper, or animVal not a clone of baseVal
     410               0 :     return;
     411                 :   }
     412                 : 
     413               0 :   NS_ABORT_IF_FALSE(animVal->mItems.Length() == mItems.Length(),
     414                 :                     "animVal list not in sync!");
     415                 : 
     416               0 :   animVal->mItems.InsertElementAt(aIndex, static_cast<DOMSVGNumber*>(nsnull));
     417                 : 
     418               0 :   UpdateListIndicesFromIndex(animVal->mItems, aIndex + 1);
     419                 : }
     420                 : 
     421                 : void
     422               0 : DOMSVGNumberList::MaybeRemoveItemFromAnimValListAt(PRUint32 aIndex)
     423                 : {
     424               0 :   NS_ABORT_IF_FALSE(!IsAnimValList(), "call from baseVal to animVal");
     425                 : 
     426                 :   // This needs to be a strong reference; otherwise, the RemovingFromList call
     427                 :   // below might drop the last reference to animVal before we're done with it.
     428               0 :   nsRefPtr<DOMSVGNumberList> animVal = mAList->mAnimVal;
     429                 : 
     430               0 :   if (!animVal || mAList->IsAnimating()) {
     431                 :     // No animVal list wrapper, or animVal not a clone of baseVal
     432                 :     return;
     433                 :   }
     434                 : 
     435               0 :   NS_ABORT_IF_FALSE(animVal->mItems.Length() == mItems.Length(),
     436                 :                     "animVal list not in sync!");
     437                 : 
     438               0 :   if (animVal->mItems[aIndex]) {
     439               0 :     animVal->mItems[aIndex]->RemovingFromList();
     440                 :   }
     441               0 :   animVal->mItems.RemoveElementAt(aIndex);
     442                 : 
     443               0 :   UpdateListIndicesFromIndex(animVal->mItems, aIndex);
     444                 : }
     445                 : 
     446            4392 : } // namespace mozilla

Generated by: LCOV version 1.7