1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
---|
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.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
26 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #define NS_WEAK_OBSERVERS
|
---|
39 |
|
---|
40 | #include "nsString.h"
|
---|
41 | #include "nsAutoLock.h"
|
---|
42 | #include "nsCOMPtr.h"
|
---|
43 | #include "nsIWeakReference.h"
|
---|
44 | #include "nsEnumeratorUtils.h"
|
---|
45 | #include "nsObserverList.h"
|
---|
46 |
|
---|
47 | nsObserverList::nsObserverList()
|
---|
48 | {
|
---|
49 | MOZ_COUNT_CTOR(nsObserverList);
|
---|
50 | int vrc = RTSemFastMutexCreate(&m_hLock);
|
---|
51 | AssertRC(vrc); RT_NOREF(vrc);
|
---|
52 | }
|
---|
53 |
|
---|
54 | nsObserverList::~nsObserverList(void)
|
---|
55 | {
|
---|
56 | MOZ_COUNT_DTOR(nsObserverList);
|
---|
57 | int vrc = RTSemFastMutexDestroy(m_hLock);
|
---|
58 | AssertRC(vrc); RT_NOREF(vrc);
|
---|
59 | }
|
---|
60 |
|
---|
61 | nsresult
|
---|
62 | nsObserverList::AddObserver(nsIObserver* anObserver, PRBool ownsWeak)
|
---|
63 | {
|
---|
64 | nsresult rv;
|
---|
65 | PRBool inserted;
|
---|
66 |
|
---|
67 | NS_ENSURE_ARG(anObserver);
|
---|
68 |
|
---|
69 | nsAutoLock lock(m_hLock);
|
---|
70 |
|
---|
71 | if (!mObserverList) {
|
---|
72 | rv = NS_NewISupportsArray(getter_AddRefs(mObserverList));
|
---|
73 | if (NS_FAILED(rv)) return rv;
|
---|
74 | }
|
---|
75 |
|
---|
76 | #ifdef NS_WEAK_OBSERVERS
|
---|
77 | nsCOMPtr<nsISupports> observerRef;
|
---|
78 | if (ownsWeak) {
|
---|
79 | nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
|
---|
80 | NS_ASSERTION(weakRefFactory, "AddObserver: trying weak object that doesnt support nsIWeakReference");
|
---|
81 | if ( weakRefFactory )
|
---|
82 | observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
|
---|
83 | } else {
|
---|
84 | #if DEBUG_dougt_xxx
|
---|
85 | // if you are hitting this assertion, contact [email protected]. There may be a ownership problem caused by his checkin to freeze nsIObserver
|
---|
86 | nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
|
---|
87 | NS_ASSERTION(!weakRefFactory, "Your object supports weak references, but is being added with a strong reference");
|
---|
88 | #endif
|
---|
89 | observerRef = anObserver;
|
---|
90 | }
|
---|
91 | if (!observerRef)
|
---|
92 | return NS_ERROR_FAILURE;
|
---|
93 |
|
---|
94 | inserted = mObserverList->AppendElement(observerRef);
|
---|
95 | #else
|
---|
96 | if (*anObserver)
|
---|
97 | inserted = mObserverList->AppendElement(*anObserver);
|
---|
98 | #endif
|
---|
99 | return inserted ? NS_OK : NS_ERROR_FAILURE;
|
---|
100 | }
|
---|
101 |
|
---|
102 | nsresult
|
---|
103 | nsObserverList::RemoveObserver(nsIObserver* anObserver)
|
---|
104 | {
|
---|
105 | PRBool removed = PR_FALSE;
|
---|
106 |
|
---|
107 | NS_ENSURE_ARG(anObserver);
|
---|
108 |
|
---|
109 | nsAutoLock lock(m_hLock);
|
---|
110 |
|
---|
111 | if (!mObserverList)
|
---|
112 | return NS_ERROR_FAILURE;
|
---|
113 |
|
---|
114 | #ifdef NS_WEAK_OBSERVERS
|
---|
115 | nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
|
---|
116 | nsCOMPtr<nsISupports> observerRef;
|
---|
117 | if (weakRefFactory) {
|
---|
118 | observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
|
---|
119 | if (observerRef)
|
---|
120 | removed = mObserverList->RemoveElement(observerRef);
|
---|
121 | if (!removed)
|
---|
122 | observerRef = anObserver;
|
---|
123 | } else
|
---|
124 | observerRef = anObserver;
|
---|
125 |
|
---|
126 | if (!removed && observerRef)
|
---|
127 | removed = mObserverList->RemoveElement(observerRef);
|
---|
128 | #else
|
---|
129 | if (*anObserver)
|
---|
130 | removed = mObserverList->RemoveElement(*anObserver);
|
---|
131 | #endif
|
---|
132 | return removed ? NS_OK : NS_ERROR_FAILURE;
|
---|
133 | }
|
---|
134 |
|
---|
135 | nsresult
|
---|
136 | nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator)
|
---|
137 | {
|
---|
138 | nsAutoLock lock(m_hLock);
|
---|
139 |
|
---|
140 | ObserverListEnumerator * enumerator= new ObserverListEnumerator(mObserverList);
|
---|
141 | *anEnumerator = enumerator;
|
---|
142 | if (!enumerator)
|
---|
143 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
144 |
|
---|
145 | NS_ADDREF(enumerator);
|
---|
146 | return NS_OK;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | ObserverListEnumerator::ObserverListEnumerator(nsISupportsArray* aValueArray)
|
---|
151 | : mValueArray(aValueArray), mIndex(0)
|
---|
152 | {
|
---|
153 | if (mValueArray) {
|
---|
154 | NS_ADDREF(mValueArray);
|
---|
155 | PRUint32 total;
|
---|
156 | mValueArray->Count(&total);
|
---|
157 | mIndex = PRInt32(total);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | ObserverListEnumerator::~ObserverListEnumerator(void)
|
---|
162 | {
|
---|
163 | NS_IF_RELEASE(mValueArray);
|
---|
164 | }
|
---|
165 |
|
---|
166 | NS_IMPL_ISUPPORTS1(ObserverListEnumerator, nsISimpleEnumerator)
|
---|
167 |
|
---|
168 | NS_IMETHODIMP
|
---|
169 | ObserverListEnumerator::HasMoreElements(PRBool* aResult)
|
---|
170 | {
|
---|
171 | NS_PRECONDITION(aResult != 0, "null ptr");
|
---|
172 | if (! aResult)
|
---|
173 | return NS_ERROR_NULL_POINTER;
|
---|
174 |
|
---|
175 | if (!mValueArray) {
|
---|
176 | *aResult = PR_FALSE;
|
---|
177 | return NS_OK;
|
---|
178 | }
|
---|
179 |
|
---|
180 | *aResult = (mIndex > 0);
|
---|
181 | return NS_OK;
|
---|
182 | }
|
---|
183 |
|
---|
184 | NS_IMETHODIMP
|
---|
185 | ObserverListEnumerator::GetNext(nsISupports** aResult)
|
---|
186 | {
|
---|
187 | NS_PRECONDITION(aResult != 0, "null ptr");
|
---|
188 | if (! aResult)
|
---|
189 | return NS_ERROR_NULL_POINTER;
|
---|
190 |
|
---|
191 | if (!mValueArray) {
|
---|
192 | *aResult = nsnull;
|
---|
193 | return NS_OK;
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (mIndex <= 0 )
|
---|
197 | return NS_ERROR_UNEXPECTED;
|
---|
198 |
|
---|
199 | mValueArray->GetElementAt(--mIndex, aResult);
|
---|
200 | if (*aResult) {
|
---|
201 | nsCOMPtr<nsIWeakReference> weakRefFactory = do_QueryInterface(*aResult);
|
---|
202 | if ( weakRefFactory ) {
|
---|
203 | nsCOMPtr<nsISupports> weakref = do_QueryReferent(weakRefFactory);
|
---|
204 | NS_RELEASE(*aResult);
|
---|
205 | NS_IF_ADDREF(*aResult = weakref);
|
---|
206 |
|
---|
207 | return NS_OK;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | return NS_OK;
|
---|
212 | }
|
---|