VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsObserverList.cpp@ 101917

Last change on this file since 101917 was 101917, checked in by vboxsync, 13 months ago

libs/xpcom: Convert nsObserverList.{cpp,h} to use an IPRT fast mutex, bugref:10545

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
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 "pratom.h"
41#include "nsString.h"
42#include "nsAutoLock.h"
43#include "nsCOMPtr.h"
44#include "nsIWeakReference.h"
45#include "nsEnumeratorUtils.h"
46#include "nsObserverList.h"
47
48nsObserverList::nsObserverList()
49{
50 MOZ_COUNT_CTOR(nsObserverList);
51 int vrc = RTSemFastMutexCreate(&m_hLock);
52 AssertRC(vrc); RT_NOREF(vrc);
53}
54
55nsObserverList::~nsObserverList(void)
56{
57 MOZ_COUNT_DTOR(nsObserverList);
58 int vrc = RTSemFastMutexDestroy(m_hLock);
59 AssertRC(vrc); RT_NOREF(vrc);
60}
61
62nsresult
63nsObserverList::AddObserver(nsIObserver* anObserver, PRBool ownsWeak)
64{
65 nsresult rv;
66 PRBool inserted;
67
68 NS_ENSURE_ARG(anObserver);
69
70 nsAutoLock lock(m_hLock);
71
72 if (!mObserverList) {
73 rv = NS_NewISupportsArray(getter_AddRefs(mObserverList));
74 if (NS_FAILED(rv)) return rv;
75 }
76
77#ifdef NS_WEAK_OBSERVERS
78 nsCOMPtr<nsISupports> observerRef;
79 if (ownsWeak) {
80 nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
81 NS_ASSERTION(weakRefFactory, "AddObserver: trying weak object that doesnt support nsIWeakReference");
82 if ( weakRefFactory )
83 observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
84 } else {
85#if DEBUG_dougt_xxx
86 // if you are hitting this assertion, contact [email protected]. There may be a ownership problem caused by his checkin to freeze nsIObserver
87 nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
88 NS_ASSERTION(!weakRefFactory, "Your object supports weak references, but is being added with a strong reference");
89#endif
90 observerRef = anObserver;
91 }
92 if (!observerRef)
93 return NS_ERROR_FAILURE;
94
95 inserted = mObserverList->AppendElement(observerRef);
96#else
97 if (*anObserver)
98 inserted = mObserverList->AppendElement(*anObserver);
99#endif
100 return inserted ? NS_OK : NS_ERROR_FAILURE;
101}
102
103nsresult
104nsObserverList::RemoveObserver(nsIObserver* anObserver)
105{
106 PRBool removed = PR_FALSE;
107
108 NS_ENSURE_ARG(anObserver);
109
110 nsAutoLock lock(m_hLock);
111
112 if (!mObserverList)
113 return NS_ERROR_FAILURE;
114
115#ifdef NS_WEAK_OBSERVERS
116 nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
117 nsCOMPtr<nsISupports> observerRef;
118 if (weakRefFactory) {
119 observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
120 if (observerRef)
121 removed = mObserverList->RemoveElement(observerRef);
122 if (!removed)
123 observerRef = anObserver;
124 } else
125 observerRef = anObserver;
126
127 if (!removed && observerRef)
128 removed = mObserverList->RemoveElement(observerRef);
129#else
130 if (*anObserver)
131 removed = mObserverList->RemoveElement(*anObserver);
132#endif
133 return removed ? NS_OK : NS_ERROR_FAILURE;
134}
135
136nsresult
137nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator)
138{
139 nsAutoLock lock(m_hLock);
140
141 ObserverListEnumerator * enumerator= new ObserverListEnumerator(mObserverList);
142 *anEnumerator = enumerator;
143 if (!enumerator)
144 return NS_ERROR_OUT_OF_MEMORY;
145
146 NS_ADDREF(enumerator);
147 return NS_OK;
148}
149
150
151ObserverListEnumerator::ObserverListEnumerator(nsISupportsArray* aValueArray)
152 : mValueArray(aValueArray), mIndex(0)
153{
154 if (mValueArray) {
155 NS_ADDREF(mValueArray);
156 PRUint32 total;
157 mValueArray->Count(&total);
158 mIndex = PRInt32(total);
159 }
160}
161
162ObserverListEnumerator::~ObserverListEnumerator(void)
163{
164 NS_IF_RELEASE(mValueArray);
165}
166
167NS_IMPL_ISUPPORTS1(ObserverListEnumerator, nsISimpleEnumerator)
168
169NS_IMETHODIMP
170ObserverListEnumerator::HasMoreElements(PRBool* aResult)
171{
172 NS_PRECONDITION(aResult != 0, "null ptr");
173 if (! aResult)
174 return NS_ERROR_NULL_POINTER;
175
176 if (!mValueArray) {
177 *aResult = PR_FALSE;
178 return NS_OK;
179 }
180
181 *aResult = (mIndex > 0);
182 return NS_OK;
183}
184
185NS_IMETHODIMP
186ObserverListEnumerator::GetNext(nsISupports** aResult)
187{
188 NS_PRECONDITION(aResult != 0, "null ptr");
189 if (! aResult)
190 return NS_ERROR_NULL_POINTER;
191
192 if (!mValueArray) {
193 *aResult = nsnull;
194 return NS_OK;
195 }
196
197 if (mIndex <= 0 )
198 return NS_ERROR_UNEXPECTED;
199
200 mValueArray->GetElementAt(--mIndex, aResult);
201 if (*aResult) {
202 nsCOMPtr<nsIWeakReference> weakRefFactory = do_QueryInterface(*aResult);
203 if ( weakRefFactory ) {
204 nsCOMPtr<nsISupports> weakref = do_QueryReferent(weakRefFactory);
205 NS_RELEASE(*aResult);
206 NS_IF_ADDREF(*aResult = weakref);
207
208 return NS_OK;
209 }
210 }
211
212 return NS_OK;
213}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette