VirtualBox

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

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

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

© 2023 Oracle
ContactPrivacy policyTerms of Use