1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsIFactory.h"
9 #include "nsIServiceManager.h"
10 #include "nsIComponentManager.h"
11 #include "nsIObserverService.h"
12 #include "nsIObserver.h"
13 #include "nsISimpleEnumerator.h"
14 #include "nsObserverService.h"
15 #include "nsObserverList.h"
16 #include "nsHashtable.h"
17 #include "nsThreadUtils.h"
18 #include "nsIWeakReference.h"
19 #include "nsEnumeratorUtils.h"
20 #include "mozilla/net/NeckoCommon.h"
22 #define NOTIFY_GLOBAL_OBSERVERS
24 #if defined(PR_LOGGING)
25 // Log module for nsObserverService logging...
27 // To enable logging (see prlog.h for full details):
29 // set NSPR_LOG_MODULES=ObserverService:5
30 // set NSPR_LOG_FILE=nspr.log
32 // this enables PR_LOG_DEBUG level information and places all output in
34 static PRLogModuleInfo
*
35 GetObserverServiceLog()
37 static PRLogModuleInfo
*sLog
;
39 sLog
= PR_NewLogModule("ObserverService");
42 #define LOG(x) PR_LOG(GetObserverServiceLog(), PR_LOG_DEBUG, x)
45 #endif /* PR_LOGGING */
47 ////////////////////////////////////////////////////////////////////////////////
48 // nsObserverService Implementation
51 NS_IMPL_THREADSAFE_ISUPPORTS2(nsObserverService
, nsIObserverService
, nsObserverService
)
53 nsObserverService::nsObserverService() :
56 mObserverTopicTable
.Init();
59 nsObserverService::~nsObserverService(void)
65 nsObserverService::Shutdown()
69 if (mObserverTopicTable
.IsInitialized())
70 mObserverTopicTable
.Clear();
74 nsObserverService::Create(nsISupports
* outer
, const nsIID
& aIID
, void* *aInstancePtr
)
76 LOG(("nsObserverService::Create()"));
78 nsRefPtr
<nsObserverService
> os
= new nsObserverService();
80 if (!os
|| !os
->mObserverTopicTable
.IsInitialized())
81 return NS_ERROR_OUT_OF_MEMORY
;
83 return os
->QueryInterface(aIID
, aInstancePtr
);
86 #define NS_ENSURE_VALIDCALL \
87 if (!NS_IsMainThread()) { \
88 NS_ERROR("Using observer service off the main thread!"); \
89 return NS_ERROR_UNEXPECTED; \
91 if (mShuttingDown) { \
92 NS_ERROR("Using observer service after XPCOM shutdown!"); \
93 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; \
97 nsObserverService::AddObserver(nsIObserver
* anObserver
, const char* aTopic
,
100 LOG(("nsObserverService::AddObserver(%p: %s)",
101 (void*) anObserver
, aTopic
));
104 NS_ENSURE_ARG(anObserver
&& aTopic
);
106 if (mozilla::net::IsNeckoChild() && !strncmp(aTopic
, "http-on-", 8)) {
107 return NS_ERROR_NOT_IMPLEMENTED
;
110 nsObserverList
*observerList
= mObserverTopicTable
.PutEntry(aTopic
);
112 return NS_ERROR_OUT_OF_MEMORY
;
114 return observerList
->AddObserver(anObserver
, ownsWeak
);
118 nsObserverService::RemoveObserver(nsIObserver
* anObserver
, const char* aTopic
)
120 LOG(("nsObserverService::RemoveObserver(%p: %s)",
121 (void*) anObserver
, aTopic
));
123 NS_ENSURE_ARG(anObserver
&& aTopic
);
125 nsObserverList
*observerList
= mObserverTopicTable
.GetEntry(aTopic
);
127 return NS_ERROR_FAILURE
;
129 /* This death grip is to protect against stupid consumers who call
130 RemoveObserver from their Destructor, see bug 485834/bug 325392. */
131 nsCOMPtr
<nsIObserver
> kungFuDeathGrip(anObserver
);
132 return observerList
->RemoveObserver(anObserver
);
136 nsObserverService::EnumerateObservers(const char* aTopic
,
137 nsISimpleEnumerator
** anEnumerator
)
140 NS_ENSURE_ARG(aTopic
&& anEnumerator
);
142 nsObserverList
*observerList
= mObserverTopicTable
.GetEntry(aTopic
);
144 return NS_NewEmptyEnumerator(anEnumerator
);
146 return observerList
->GetObserverList(anEnumerator
);
149 // Enumerate observers of aTopic and call Observe on each.
150 NS_IMETHODIMP
nsObserverService::NotifyObservers(nsISupports
*aSubject
,
152 const PRUnichar
*someData
)
154 LOG(("nsObserverService::NotifyObservers(%s)", aTopic
));
157 NS_ENSURE_ARG(aTopic
);
159 nsObserverList
*observerList
= mObserverTopicTable
.GetEntry(aTopic
);
161 observerList
->NotifyObservers(aSubject
, aTopic
, someData
);
163 #ifdef NOTIFY_GLOBAL_OBSERVERS
164 observerList
= mObserverTopicTable
.GetEntry("*");
166 observerList
->NotifyObservers(aSubject
, aTopic
, someData
);
172 static PLDHashOperator
173 UnmarkGrayObserverEntry(nsObserverList
* aObserverList
, void* aClosure
)
176 aObserverList
->UnmarkGrayStrongObservers();
178 return PL_DHASH_NEXT
;
182 nsObserverService::UnmarkGrayStrongObservers()
186 mObserverTopicTable
.EnumerateEntries(UnmarkGrayObserverEntry
, nullptr);