bug 812562 - click-to-play: reshow notification for blocklisted plugins r=jaws
[gecko.git] / xpcom / ds / nsObserverService.cpp
blob572b65228defcd638e2015e132320ce057297bcf
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/. */
6 #include "prlog.h"
7 #include "nsAutoPtr.h"
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
33 // the file nspr.log
34 static PRLogModuleInfo*
35 GetObserverServiceLog()
37 static PRLogModuleInfo *sLog;
38 if (!sLog)
39 sLog = PR_NewLogModule("ObserverService");
40 return sLog;
42 #define LOG(x) PR_LOG(GetObserverServiceLog(), PR_LOG_DEBUG, x)
43 #else
44 #define LOG(x)
45 #endif /* PR_LOGGING */
47 ////////////////////////////////////////////////////////////////////////////////
48 // nsObserverService Implementation
51 NS_IMPL_THREADSAFE_ISUPPORTS2(nsObserverService, nsIObserverService, nsObserverService)
53 nsObserverService::nsObserverService() :
54 mShuttingDown(false)
56 mObserverTopicTable.Init();
59 nsObserverService::~nsObserverService(void)
61 Shutdown();
64 void
65 nsObserverService::Shutdown()
67 mShuttingDown = true;
69 if (mObserverTopicTable.IsInitialized())
70 mObserverTopicTable.Clear();
73 nsresult
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; \
90 } \
91 if (mShuttingDown) { \
92 NS_ERROR("Using observer service after XPCOM shutdown!"); \
93 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; \
96 NS_IMETHODIMP
97 nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic,
98 bool ownsWeak)
100 LOG(("nsObserverService::AddObserver(%p: %s)",
101 (void*) anObserver, aTopic));
103 NS_ENSURE_VALIDCALL
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);
111 if (!observerList)
112 return NS_ERROR_OUT_OF_MEMORY;
114 return observerList->AddObserver(anObserver, ownsWeak);
117 NS_IMETHODIMP
118 nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic)
120 LOG(("nsObserverService::RemoveObserver(%p: %s)",
121 (void*) anObserver, aTopic));
122 NS_ENSURE_VALIDCALL
123 NS_ENSURE_ARG(anObserver && aTopic);
125 nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
126 if (!observerList)
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);
135 NS_IMETHODIMP
136 nsObserverService::EnumerateObservers(const char* aTopic,
137 nsISimpleEnumerator** anEnumerator)
139 NS_ENSURE_VALIDCALL
140 NS_ENSURE_ARG(aTopic && anEnumerator);
142 nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
143 if (!observerList)
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,
151 const char *aTopic,
152 const PRUnichar *someData)
154 LOG(("nsObserverService::NotifyObservers(%s)", aTopic));
156 NS_ENSURE_VALIDCALL
157 NS_ENSURE_ARG(aTopic);
159 nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
160 if (observerList)
161 observerList->NotifyObservers(aSubject, aTopic, someData);
163 #ifdef NOTIFY_GLOBAL_OBSERVERS
164 observerList = mObserverTopicTable.GetEntry("*");
165 if (observerList)
166 observerList->NotifyObservers(aSubject, aTopic, someData);
167 #endif
169 return NS_OK;
172 static PLDHashOperator
173 UnmarkGrayObserverEntry(nsObserverList* aObserverList, void* aClosure)
175 if (aObserverList) {
176 aObserverList->UnmarkGrayStrongObservers();
178 return PL_DHASH_NEXT;
181 NS_IMETHODIMP
182 nsObserverService::UnmarkGrayStrongObservers()
184 NS_ENSURE_VALIDCALL
186 mObserverTopicTable.EnumerateEntries(UnmarkGrayObserverEntry, nullptr);
188 return NS_OK;