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 : #include "nsObserverList.h"
39 :
40 : #include "nsAutoPtr.h"
41 : #include "nsCOMArray.h"
42 : #include "nsISimpleEnumerator.h"
43 :
44 : nsresult
45 95492 : nsObserverList::AddObserver(nsIObserver* anObserver, bool ownsWeak)
46 : {
47 95492 : NS_ASSERTION(anObserver, "Null input");
48 :
49 95492 : if (!ownsWeak) {
50 37085 : ObserverRef* o = mObservers.AppendElement(anObserver);
51 37085 : if (!o)
52 0 : return NS_ERROR_OUT_OF_MEMORY;
53 :
54 37085 : return NS_OK;
55 : }
56 :
57 116814 : nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(anObserver);
58 58407 : if (!weak)
59 0 : return NS_NOINTERFACE;
60 :
61 58407 : ObserverRef *o = mObservers.AppendElement(weak);
62 58407 : if (!o)
63 0 : return NS_ERROR_OUT_OF_MEMORY;
64 :
65 58407 : return NS_OK;
66 : }
67 :
68 : nsresult
69 19527 : nsObserverList::RemoveObserver(nsIObserver* anObserver)
70 : {
71 19527 : NS_ASSERTION(anObserver, "Null input");
72 :
73 19527 : if (mObservers.RemoveElement(static_cast<nsISupports*>(anObserver)))
74 17490 : return NS_OK;
75 :
76 4074 : nsCOMPtr<nsIWeakReference> observerRef = do_GetWeakReference(anObserver);
77 2037 : if (!observerRef)
78 0 : return NS_ERROR_FAILURE;
79 :
80 2037 : if (!mObservers.RemoveElement(observerRef))
81 0 : return NS_ERROR_FAILURE;
82 :
83 2037 : return NS_OK;
84 : }
85 :
86 : nsresult
87 972 : nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator)
88 : {
89 1944 : nsRefPtr<nsObserverEnumerator> e(new nsObserverEnumerator(this));
90 972 : if (!e)
91 0 : return NS_ERROR_OUT_OF_MEMORY;
92 :
93 972 : NS_ADDREF(*anEnumerator = e);
94 972 : return NS_OK;
95 : }
96 :
97 : void
98 31251 : nsObserverList::FillObserverArray(nsCOMArray<nsIObserver> &aArray)
99 : {
100 31251 : aArray.SetCapacity(mObservers.Length());
101 :
102 62502 : nsTArray<ObserverRef> observers(mObservers);
103 :
104 89882 : for (PRInt32 i = observers.Length() - 1; i >= 0; --i) {
105 58631 : if (observers[i].isWeakRef) {
106 76426 : nsCOMPtr<nsIObserver> o(do_QueryReferent(observers[i].asWeak()));
107 38213 : if (o) {
108 38202 : aArray.AppendObject(o);
109 : }
110 : else {
111 : // the object has gone away, remove the weakref
112 11 : mObservers.RemoveElement(observers[i].asWeak());
113 : }
114 : }
115 : else {
116 20418 : aArray.AppendObject(observers[i].asObserver());
117 : }
118 : }
119 31251 : }
120 :
121 : void
122 30279 : nsObserverList::NotifyObservers(nsISupports *aSubject,
123 : const char *aTopic,
124 : const PRUnichar *someData)
125 : {
126 60558 : nsCOMArray<nsIObserver> observers;
127 30279 : FillObserverArray(observers);
128 :
129 87939 : for (PRInt32 i = 0; i < observers.Count(); ++i) {
130 57660 : observers[i]->Observe(aSubject, aTopic, someData);
131 : }
132 30279 : }
133 :
134 7776 : NS_IMPL_ISUPPORTS1(nsObserverEnumerator, nsISimpleEnumerator)
135 :
136 972 : nsObserverEnumerator::nsObserverEnumerator(nsObserverList* aObserverList)
137 972 : : mIndex(0)
138 : {
139 972 : aObserverList->FillObserverArray(mObservers);
140 972 : }
141 :
142 : NS_IMETHODIMP
143 1932 : nsObserverEnumerator::HasMoreElements(bool *aResult)
144 : {
145 1932 : *aResult = (mIndex < mObservers.Count());
146 1932 : return NS_OK;
147 : }
148 :
149 : NS_IMETHODIMP
150 960 : nsObserverEnumerator::GetNext(nsISupports* *aResult)
151 : {
152 960 : if (mIndex == mObservers.Count()) {
153 0 : NS_ERROR("Enumerating after HasMoreElements returned false.");
154 0 : return NS_ERROR_UNEXPECTED;
155 : }
156 :
157 960 : NS_ADDREF(*aResult = mObservers[mIndex]);
158 960 : ++mIndex;
159 960 : return NS_OK;
160 : }
|