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 | * Pierre Phaneuf <[email protected]>
|
---|
24 | *
|
---|
25 | * Alternatively, the contents of this file may be used under the terms of
|
---|
26 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
27 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
29 | * of those above. If you wish to allow use of your version of this file only
|
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
31 | * use your version of this file under the terms of the MPL, indicate your
|
---|
32 | * decision by deleting the provisions above and replace them with the notice
|
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
34 | * the provisions above, a recipient may use your version of this file under
|
---|
35 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | #include "prlog.h"
|
---|
40 | #include "prlock.h"
|
---|
41 | #include "nsIFactory.h"
|
---|
42 | #include "nsIServiceManager.h"
|
---|
43 | #include "nsIComponentManager.h"
|
---|
44 | #include "nsIObserverService.h"
|
---|
45 | #include "nsObserverService.h"
|
---|
46 | #include "nsObserverList.h"
|
---|
47 | #include "nsHashtable.h"
|
---|
48 | #include "nsIWeakReference.h"
|
---|
49 |
|
---|
50 | #define NS_WEAK_OBSERVERS
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | #if defined(PR_LOGGING)
|
---|
55 | // Log module for nsObserverService logging...
|
---|
56 | //
|
---|
57 | // To enable logging (see prlog.h for full details):
|
---|
58 | //
|
---|
59 | // set NSPR_LOG_MODULES=ObserverService:5
|
---|
60 | // set NSPR_LOG_FILE=nspr.log
|
---|
61 | //
|
---|
62 | // this enables PR_LOG_DEBUG level information and places all output in
|
---|
63 | // the file nspr.log
|
---|
64 | PRLogModuleInfo* observerServiceLog = nsnull;
|
---|
65 | #endif /* PR_LOGGING */
|
---|
66 |
|
---|
67 | ////////////////////////////////////////////////////////////////////////////////
|
---|
68 | // nsObserverService Implementation
|
---|
69 |
|
---|
70 |
|
---|
71 | NS_IMPL_THREADSAFE_ISUPPORTS1(nsObserverService, nsIObserverService)
|
---|
72 |
|
---|
73 | nsObserverService::nsObserverService()
|
---|
74 | : mObserverTopicTable(nsnull)
|
---|
75 | {
|
---|
76 | }
|
---|
77 |
|
---|
78 | nsObserverService::~nsObserverService(void)
|
---|
79 | {
|
---|
80 | if(mObserverTopicTable)
|
---|
81 | delete mObserverTopicTable;
|
---|
82 | }
|
---|
83 |
|
---|
84 | NS_METHOD
|
---|
85 | nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
---|
86 | {
|
---|
87 | #if defined(PR_LOGGING)
|
---|
88 | if (!observerServiceLog)
|
---|
89 | observerServiceLog = PR_NewLogModule("ObserverService");
|
---|
90 | #endif
|
---|
91 |
|
---|
92 | nsresult rv;
|
---|
93 | nsObserverService* os = new nsObserverService();
|
---|
94 | if (os == nsnull)
|
---|
95 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
96 | NS_ADDREF(os);
|
---|
97 | rv = os->QueryInterface(aIID, aInstancePtr);
|
---|
98 | NS_RELEASE(os);
|
---|
99 | return rv;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static PRBool PR_CALLBACK
|
---|
103 | ReleaseObserverList(nsHashKey *aKey, void *aData, void* closure)
|
---|
104 | {
|
---|
105 | nsObserverList* observerList = NS_STATIC_CAST(nsObserverList*, aData);
|
---|
106 | delete(observerList);
|
---|
107 | return PR_TRUE;
|
---|
108 | }
|
---|
109 |
|
---|
110 | nsresult nsObserverService::GetObserverList(const char* aTopic, nsObserverList** anObserverList)
|
---|
111 | {
|
---|
112 | if (anObserverList == nsnull)
|
---|
113 | return NS_ERROR_NULL_POINTER;
|
---|
114 |
|
---|
115 | if(mObserverTopicTable == nsnull)
|
---|
116 | {
|
---|
117 | mObserverTopicTable = new nsObjectHashtable(nsnull,
|
---|
118 | nsnull, // should never be cloned
|
---|
119 | ReleaseObserverList,
|
---|
120 | nsnull,
|
---|
121 | 256,
|
---|
122 | PR_TRUE);
|
---|
123 | if (mObserverTopicTable == nsnull)
|
---|
124 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | nsCStringKey key(aTopic);
|
---|
129 |
|
---|
130 | nsObserverList *topicObservers;
|
---|
131 | topicObservers = (nsObserverList *) mObserverTopicTable->Get(&key);
|
---|
132 |
|
---|
133 | if (topicObservers)
|
---|
134 | {
|
---|
135 | *anObserverList = topicObservers;
|
---|
136 | return NS_OK;
|
---|
137 | }
|
---|
138 |
|
---|
139 | topicObservers = new nsObserverList();
|
---|
140 | if (!topicObservers)
|
---|
141 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
142 |
|
---|
143 | *anObserverList = topicObservers;
|
---|
144 | mObserverTopicTable->Put(&key, topicObservers);
|
---|
145 |
|
---|
146 | return NS_OK;
|
---|
147 | }
|
---|
148 |
|
---|
149 | NS_IMETHODIMP nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic, PRBool ownsWeak)
|
---|
150 | {
|
---|
151 | nsObserverList* anObserverList;
|
---|
152 | nsresult rv;
|
---|
153 |
|
---|
154 | if (anObserver == nsnull || aTopic == nsnull)
|
---|
155 | return NS_ERROR_NULL_POINTER;
|
---|
156 |
|
---|
157 | rv = GetObserverList(aTopic, &anObserverList);
|
---|
158 | if (NS_FAILED(rv)) return rv;
|
---|
159 |
|
---|
160 | return anObserverList->AddObserver(anObserver, ownsWeak);
|
---|
161 | }
|
---|
162 |
|
---|
163 | NS_IMETHODIMP nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic)
|
---|
164 | {
|
---|
165 | nsObserverList* anObserverList;
|
---|
166 | nsresult rv;
|
---|
167 |
|
---|
168 | if (anObserver == nsnull || aTopic == nsnull)
|
---|
169 | return NS_ERROR_NULL_POINTER;
|
---|
170 |
|
---|
171 | rv = GetObserverList(aTopic, &anObserverList);
|
---|
172 | if (NS_FAILED(rv)) return rv;
|
---|
173 |
|
---|
174 | return anObserverList->RemoveObserver(anObserver);
|
---|
175 | }
|
---|
176 |
|
---|
177 | NS_IMETHODIMP nsObserverService::EnumerateObservers(const char* aTopic, nsISimpleEnumerator** anEnumerator)
|
---|
178 | {
|
---|
179 | nsObserverList* anObserverList;
|
---|
180 | nsresult rv;
|
---|
181 |
|
---|
182 | if (anEnumerator == nsnull || aTopic == nsnull)
|
---|
183 | return NS_ERROR_NULL_POINTER;
|
---|
184 |
|
---|
185 | rv = GetObserverList(aTopic, &anObserverList);
|
---|
186 | if (NS_FAILED(rv)) return rv;
|
---|
187 |
|
---|
188 | return anObserverList->GetObserverList(anEnumerator);
|
---|
189 | }
|
---|
190 |
|
---|
191 | // Enumerate observers of aTopic and call Observe on each.
|
---|
192 | NS_IMETHODIMP nsObserverService::NotifyObservers( nsISupports *aSubject,
|
---|
193 | const char *aTopic,
|
---|
194 | const PRUnichar *someData ) {
|
---|
195 | nsresult rv = NS_OK;
|
---|
196 | nsCOMPtr<nsISimpleEnumerator> observers;
|
---|
197 | nsCOMPtr<nsISupports> observerRef;
|
---|
198 |
|
---|
199 | rv = EnumerateObservers( aTopic, getter_AddRefs(observers) );
|
---|
200 | if ( NS_FAILED( rv ) )
|
---|
201 | return rv;
|
---|
202 | PRBool loop = PR_TRUE;
|
---|
203 | while( NS_SUCCEEDED(observers->HasMoreElements(&loop)) && loop)
|
---|
204 | {
|
---|
205 | observers->GetNext(getter_AddRefs(observerRef));
|
---|
206 | nsCOMPtr<nsIObserver> observer = do_QueryInterface(observerRef);
|
---|
207 | if ( observer )
|
---|
208 | observer->Observe( aSubject, aTopic, someData );
|
---|
209 | #ifdef NS_WEAK_OBSERVERS
|
---|
210 | else
|
---|
211 | { // check for weak reference.
|
---|
212 | nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(observerRef);
|
---|
213 | if ( weakRef )
|
---|
214 | weakRef->QueryReferent(NS_GET_IID(nsIObserver), getter_AddRefs(observer));
|
---|
215 |
|
---|
216 | if ( observer )
|
---|
217 | observer->Observe( aSubject, aTopic, someData );
|
---|
218 |
|
---|
219 | PR_LOG(observerServiceLog, PR_LOG_DEBUG, ("Notification - %s\n", aTopic ? aTopic : "undefined"));
|
---|
220 |
|
---|
221 | }
|
---|
222 | #endif
|
---|
223 | }
|
---|
224 | return NS_OK;
|
---|
225 | }
|
---|
226 | ////////////////////////////////////////////////////////////////////////////////
|
---|
227 |
|
---|