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 : #ifndef MOZILLA_DOMSVGNUMBERLIST_H__
38 : #define MOZILLA_DOMSVGNUMBERLIST_H__
39 :
40 : #include "DOMSVGAnimatedNumberList.h"
41 : #include "nsAutoPtr.h"
42 : #include "nsCycleCollectionParticipant.h"
43 : #include "nsDebug.h"
44 : #include "nsIDOMSVGNumberList.h"
45 : #include "nsTArray.h"
46 : #include "SVGNumberList.h"
47 :
48 : class nsSVGElement;
49 :
50 : namespace mozilla {
51 :
52 : class DOMSVGNumber;
53 :
54 : /**
55 : * Class DOMSVGNumberList
56 : *
57 : * This class is used to create the DOM tearoff objects that wrap internal
58 : * SVGNumberList objects.
59 : *
60 : * See the architecture comment in DOMSVGAnimatedNumberList.h.
61 : *
62 : * This class is strongly intertwined with DOMSVGAnimatedNumberList and
63 : * DOMSVGNumber. We are a friend of DOMSVGAnimatedNumberList, and are
64 : * responsible for nulling out our DOMSVGAnimatedNumberList's pointer to us
65 : * when we die, essentially making its pointer to us a weak pointer. Similarly,
66 : * our DOMSVGNumber items are friends of us and responsible for nulling out our
67 : * pointers to them.
68 : *
69 : * Our DOM items are created lazily on demand as and when script requests them.
70 : */
71 : class DOMSVGNumberList : public nsIDOMSVGNumberList,
72 : public nsWrapperCache
73 : {
74 : friend class DOMSVGNumber;
75 :
76 : public:
77 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
78 1464 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGNumberList)
79 : NS_DECL_NSIDOMSVGNUMBERLIST
80 :
81 0 : DOMSVGNumberList(DOMSVGAnimatedNumberList *aAList,
82 : const SVGNumberList &aInternalList)
83 0 : : mAList(aAList)
84 : {
85 0 : SetIsProxy();
86 :
87 : // aInternalList must be passed in explicitly because we can't use
88 : // InternalList() here. (Because it depends on IsAnimValList, which depends
89 : // on this object having been assigned to aAList's mBaseVal or mAnimVal,
90 : // which hasn't happend yet.)
91 :
92 0 : InternalListLengthWillChange(aInternalList.Length()); // Sync mItems
93 0 : }
94 :
95 0 : ~DOMSVGNumberList() {
96 : // Our mAList's weak ref to us must be nulled out when we die. If GC has
97 : // unlinked us using the cycle collector code, then that has already
98 : // happened, and mAList is null.
99 0 : if (mAList) {
100 0 : ( IsAnimValList() ? mAList->mAnimVal : mAList->mBaseVal ) = nsnull;
101 : }
102 0 : }
103 :
104 : virtual JSObject* WrapObject(JSContext *cx, XPCWrappedNativeScope *scope,
105 : bool *triedToWrap);
106 :
107 0 : nsISupports* GetParentObject()
108 : {
109 0 : return static_cast<nsIContent*>(Element());
110 : }
111 :
112 : /**
113 : * This will normally be the same as InternalList().Length(), except if we've
114 : * hit OOM in which case our length will be zero.
115 : */
116 0 : PRUint32 Length() const {
117 0 : NS_ABORT_IF_FALSE(mItems.Length() == 0 ||
118 : mItems.Length() == InternalList().Length(),
119 : "DOM wrapper's list length is out of sync");
120 0 : return mItems.Length();
121 : }
122 :
123 : /// Called to notify us to syncronize our length and detach excess items.
124 : void InternalListLengthWillChange(PRUint32 aNewLength);
125 :
126 : private:
127 :
128 0 : nsSVGElement* Element() const {
129 0 : return mAList->mElement;
130 : }
131 :
132 0 : PRUint8 AttrEnum() const {
133 0 : return mAList->mAttrEnum;
134 : }
135 :
136 : /// Used to determine if this list is the baseVal or animVal list.
137 0 : bool IsAnimValList() const {
138 0 : NS_ABORT_IF_FALSE(this == mAList->mBaseVal || this == mAList->mAnimVal,
139 : "Calling IsAnimValList() too early?!");
140 0 : return this == mAList->mAnimVal;
141 : }
142 :
143 : /**
144 : * Get a reference to this object's corresponding internal SVGNumberList.
145 : *
146 : * To simplify the code we just have this one method for obtaining both
147 : * baseVal and animVal internal lists. This means that animVal lists don't
148 : * get const protection, but our setter methods guard against changing
149 : * animVal lists.
150 : */
151 : SVGNumberList& InternalList() const;
152 :
153 : /// Creates a DOMSVGNumber for aIndex, if it doesn't already exist.
154 : void EnsureItemAt(PRUint32 aIndex);
155 :
156 : void MaybeInsertNullInAnimValListAt(PRUint32 aIndex);
157 : void MaybeRemoveItemFromAnimValListAt(PRUint32 aIndex);
158 :
159 : // Weak refs to our DOMSVGNumber items. The items are friends and take care
160 : // of clearing our pointer to them when they die.
161 : nsTArray<DOMSVGNumber*> mItems;
162 :
163 : nsRefPtr<DOMSVGAnimatedNumberList> mAList;
164 : };
165 :
166 : } // namespace mozilla
167 :
168 : #endif // MOZILLA_DOMSVGNUMBERLIST_H__
|