Try to fix intermittent refcount assertions in the presence of more than one thread...
[mozilla-central.git] / xpcom / ds / nsObserverService.cpp
bloba3eb38e62b2b817bf52cb2d0eb5a2616583fec77
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
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/
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.
15 * The Original Code is mozilla.org code.
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.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
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.
37 * ***** END LICENSE BLOCK ***** */
39 #include "prlog.h"
40 #include "nsAutoPtr.h"
41 #include "nsIFactory.h"
42 #include "nsIServiceManager.h"
43 #include "nsIComponentManager.h"
44 #include "nsIObserverService.h"
45 #include "nsIObserver.h"
46 #include "nsISimpleEnumerator.h"
47 #include "nsObserverService.h"
48 #include "nsObserverList.h"
49 #include "nsHashtable.h"
50 #include "nsThreadUtils.h"
51 #include "nsIWeakReference.h"
52 #include "nsEnumeratorUtils.h"
54 #define NOTIFY_GLOBAL_OBSERVERS
56 #if defined(PR_LOGGING)
57 // Log module for nsObserverService logging...
59 // To enable logging (see prlog.h for full details):
61 // set NSPR_LOG_MODULES=ObserverService:5
62 // set NSPR_LOG_FILE=nspr.log
64 // this enables PR_LOG_DEBUG level information and places all output in
65 // the file nspr.log
66 PRLogModuleInfo* observerServiceLog = PR_NewLogModule("ObserverService");
67 #define LOG(x) PR_LOG(observerServiceLog, PR_LOG_DEBUG, x)
68 #else
69 #define LOG(x)
70 #endif /* PR_LOGGING */
72 ////////////////////////////////////////////////////////////////////////////////
73 // nsObserverService Implementation
76 NS_IMPL_THREADSAFE_ISUPPORTS2(nsObserverService, nsIObserverService, nsObserverService)
78 nsObserverService::nsObserverService() :
79 mShuttingDown(PR_FALSE)
81 mObserverTopicTable.Init();
84 nsObserverService::~nsObserverService(void)
86 Shutdown();
89 void
90 nsObserverService::Shutdown()
92 mShuttingDown = PR_TRUE;
94 if (mObserverTopicTable.IsInitialized())
95 mObserverTopicTable.Clear();
98 nsresult
99 nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
101 LOG(("nsObserverService::Create()"));
103 nsRefPtr<nsObserverService> os = new nsObserverService();
105 if (!os || !os->mObserverTopicTable.IsInitialized())
106 return NS_ERROR_OUT_OF_MEMORY;
108 return os->QueryInterface(aIID, aInstancePtr);
111 #define NS_ENSURE_VALIDCALL \
112 if (!NS_IsMainThread()) { \
113 NS_ERROR("Using observer service off the main thread!"); \
114 return NS_ERROR_UNEXPECTED; \
116 if (mShuttingDown) { \
117 NS_ERROR("Using observer service after XPCOM shutdown!"); \
118 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; \
121 NS_IMETHODIMP
122 nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic,
123 PRBool ownsWeak)
125 LOG(("nsObserverService::AddObserver(%p: %s)",
126 (void*) anObserver, aTopic));
128 NS_ENSURE_VALIDCALL
129 NS_ENSURE_ARG(anObserver && aTopic);
131 nsObserverList *observerList = mObserverTopicTable.PutEntry(aTopic);
132 if (!observerList)
133 return NS_ERROR_OUT_OF_MEMORY;
135 return observerList->AddObserver(anObserver, ownsWeak);
138 NS_IMETHODIMP
139 nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic)
141 LOG(("nsObserverService::RemoveObserver(%p: %s)",
142 (void*) anObserver, aTopic));
143 NS_ENSURE_VALIDCALL
144 NS_ENSURE_ARG(anObserver && aTopic);
146 nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
147 if (!observerList)
148 return NS_ERROR_FAILURE;
150 /* This death grip is to protect against stupid consumers who call
151 RemoveObserver from their Destructor, see bug 485834/bug 325392. */
152 nsCOMPtr<nsIObserver> kungFuDeathGrip(anObserver);
153 return observerList->RemoveObserver(anObserver);
156 NS_IMETHODIMP
157 nsObserverService::EnumerateObservers(const char* aTopic,
158 nsISimpleEnumerator** anEnumerator)
160 NS_ENSURE_VALIDCALL
161 NS_ENSURE_ARG(aTopic && anEnumerator);
163 nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
164 if (!observerList)
165 return NS_NewEmptyEnumerator(anEnumerator);
167 return observerList->GetObserverList(anEnumerator);
170 // Enumerate observers of aTopic and call Observe on each.
171 NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports *aSubject,
172 const char *aTopic,
173 const PRUnichar *someData)
175 LOG(("nsObserverService::NotifyObservers(%s)", aTopic));
177 NS_ENSURE_VALIDCALL
178 NS_ENSURE_ARG(aTopic);
180 nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
181 if (observerList)
182 observerList->NotifyObservers(aSubject, aTopic, someData);
184 #ifdef NOTIFY_GLOBAL_OBSERVERS
185 observerList = mObserverTopicTable.GetEntry("*");
186 if (observerList)
187 observerList->NotifyObservers(aSubject, aTopic, someData);
188 #endif
190 return NS_OK;