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