Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "prlog.h" |
michael@0 | 7 | #include "nsAutoPtr.h" |
michael@0 | 8 | #include "nsIObserverService.h" |
michael@0 | 9 | #include "nsIObserver.h" |
michael@0 | 10 | #include "nsObserverService.h" |
michael@0 | 11 | #include "nsObserverList.h" |
michael@0 | 12 | #include "nsThreadUtils.h" |
michael@0 | 13 | #include "nsEnumeratorUtils.h" |
michael@0 | 14 | #include "mozilla/net/NeckoCommon.h" |
michael@0 | 15 | #include "mozilla/Services.h" |
michael@0 | 16 | |
michael@0 | 17 | #define NOTIFY_GLOBAL_OBSERVERS |
michael@0 | 18 | |
michael@0 | 19 | #if defined(PR_LOGGING) |
michael@0 | 20 | // Log module for nsObserverService logging... |
michael@0 | 21 | // |
michael@0 | 22 | // To enable logging (see prlog.h for full details): |
michael@0 | 23 | // |
michael@0 | 24 | // set NSPR_LOG_MODULES=ObserverService:5 |
michael@0 | 25 | // set NSPR_LOG_FILE=nspr.log |
michael@0 | 26 | // |
michael@0 | 27 | // this enables PR_LOG_DEBUG level information and places all output in |
michael@0 | 28 | // the file nspr.log |
michael@0 | 29 | static PRLogModuleInfo* |
michael@0 | 30 | GetObserverServiceLog() |
michael@0 | 31 | { |
michael@0 | 32 | static PRLogModuleInfo *sLog; |
michael@0 | 33 | if (!sLog) |
michael@0 | 34 | sLog = PR_NewLogModule("ObserverService"); |
michael@0 | 35 | return sLog; |
michael@0 | 36 | } |
michael@0 | 37 | #define LOG(x) PR_LOG(GetObserverServiceLog(), PR_LOG_DEBUG, x) |
michael@0 | 38 | #else |
michael@0 | 39 | #define LOG(x) |
michael@0 | 40 | #endif /* PR_LOGGING */ |
michael@0 | 41 | |
michael@0 | 42 | namespace mozilla { |
michael@0 | 43 | |
michael@0 | 44 | struct SuspectObserver { |
michael@0 | 45 | SuspectObserver(const char* aTopic, size_t aReferentCount) |
michael@0 | 46 | : topic(aTopic), referentCount(aReferentCount) {} |
michael@0 | 47 | const char* topic; |
michael@0 | 48 | size_t referentCount; |
michael@0 | 49 | }; |
michael@0 | 50 | |
michael@0 | 51 | struct ObserverServiceReferentCount { |
michael@0 | 52 | ObserverServiceReferentCount() |
michael@0 | 53 | : numStrong(0), numWeakAlive(0), numWeakDead(0) {} |
michael@0 | 54 | size_t numStrong; |
michael@0 | 55 | size_t numWeakAlive; |
michael@0 | 56 | size_t numWeakDead; |
michael@0 | 57 | nsTArray<SuspectObserver> suspectObservers; |
michael@0 | 58 | }; |
michael@0 | 59 | |
michael@0 | 60 | } // namespace mozilla |
michael@0 | 61 | |
michael@0 | 62 | using namespace mozilla; |
michael@0 | 63 | |
michael@0 | 64 | PLDHashOperator |
michael@0 | 65 | nsObserverService::CountReferents(nsObserverList* aObserverList, |
michael@0 | 66 | void* aClosure) |
michael@0 | 67 | { |
michael@0 | 68 | if (!aObserverList) { |
michael@0 | 69 | return PL_DHASH_NEXT; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | ObserverServiceReferentCount* referentCount = |
michael@0 | 73 | static_cast<ObserverServiceReferentCount*>(aClosure); |
michael@0 | 74 | |
michael@0 | 75 | size_t numStrong = 0; |
michael@0 | 76 | size_t numWeakAlive = 0; |
michael@0 | 77 | size_t numWeakDead = 0; |
michael@0 | 78 | |
michael@0 | 79 | nsTArray<ObserverRef>& observers = aObserverList->mObservers; |
michael@0 | 80 | for (uint32_t i = 0; i < observers.Length(); i++) { |
michael@0 | 81 | if (observers[i].isWeakRef) { |
michael@0 | 82 | nsCOMPtr<nsIObserver> observerRef( |
michael@0 | 83 | do_QueryReferent(observers[i].asWeak())); |
michael@0 | 84 | if (observerRef) { |
michael@0 | 85 | numWeakAlive++; |
michael@0 | 86 | } else { |
michael@0 | 87 | numWeakDead++; |
michael@0 | 88 | } |
michael@0 | 89 | } else { |
michael@0 | 90 | numStrong++; |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | referentCount->numStrong += numStrong; |
michael@0 | 95 | referentCount->numWeakAlive += numWeakAlive; |
michael@0 | 96 | referentCount->numWeakDead += numWeakDead; |
michael@0 | 97 | |
michael@0 | 98 | // Keep track of topics that have a suspiciously large number |
michael@0 | 99 | // of referents (symptom of leaks). |
michael@0 | 100 | size_t total = numStrong + numWeakAlive + numWeakDead; |
michael@0 | 101 | if (total > kSuspectReferentCount) { |
michael@0 | 102 | SuspectObserver suspect(aObserverList->GetKey(), total); |
michael@0 | 103 | referentCount->suspectObservers.AppendElement(suspect); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | return PL_DHASH_NEXT; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | NS_IMETHODIMP |
michael@0 | 110 | nsObserverService::CollectReports(nsIHandleReportCallback* aHandleReport, |
michael@0 | 111 | nsISupports* aData) |
michael@0 | 112 | { |
michael@0 | 113 | ObserverServiceReferentCount referentCount; |
michael@0 | 114 | mObserverTopicTable.EnumerateEntries(CountReferents, &referentCount); |
michael@0 | 115 | |
michael@0 | 116 | nsresult rv; |
michael@0 | 117 | for (uint32_t i = 0; i < referentCount.suspectObservers.Length(); i++) { |
michael@0 | 118 | SuspectObserver& suspect = referentCount.suspectObservers[i]; |
michael@0 | 119 | nsPrintfCString suspectPath("observer-service-suspect/" |
michael@0 | 120 | "referent(topic=%s)", |
michael@0 | 121 | suspect.topic); |
michael@0 | 122 | rv = aHandleReport->Callback(/* process */ EmptyCString(), |
michael@0 | 123 | suspectPath, KIND_OTHER, UNITS_COUNT, suspect.referentCount, |
michael@0 | 124 | NS_LITERAL_CSTRING("A topic with a suspiciously large number of " |
michael@0 | 125 | "referents. This may be symptomatic of a leak " |
michael@0 | 126 | "if the number of referents is high with " |
michael@0 | 127 | "respect to the number of windows."), |
michael@0 | 128 | aData); |
michael@0 | 129 | |
michael@0 | 130 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 131 | return rv; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | rv = aHandleReport->Callback(/* process */ EmptyCString(), |
michael@0 | 135 | NS_LITERAL_CSTRING("observer-service/referent/strong"), |
michael@0 | 136 | KIND_OTHER, UNITS_COUNT, referentCount.numStrong, |
michael@0 | 137 | NS_LITERAL_CSTRING("The number of strong references held by the " |
michael@0 | 138 | "observer service."), |
michael@0 | 139 | aData); |
michael@0 | 140 | |
michael@0 | 141 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 142 | return rv; |
michael@0 | 143 | |
michael@0 | 144 | rv = aHandleReport->Callback(/* process */ EmptyCString(), |
michael@0 | 145 | NS_LITERAL_CSTRING("observer-service/referent/weak/alive"), |
michael@0 | 146 | KIND_OTHER, UNITS_COUNT, referentCount.numWeakAlive, |
michael@0 | 147 | NS_LITERAL_CSTRING("The number of weak references held by the " |
michael@0 | 148 | "observer service that are still alive."), |
michael@0 | 149 | aData); |
michael@0 | 150 | |
michael@0 | 151 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 152 | return rv; |
michael@0 | 153 | |
michael@0 | 154 | rv = aHandleReport->Callback(/* process */ EmptyCString(), |
michael@0 | 155 | NS_LITERAL_CSTRING("observer-service/referent/weak/dead"), |
michael@0 | 156 | KIND_OTHER, UNITS_COUNT, referentCount.numWeakDead, |
michael@0 | 157 | NS_LITERAL_CSTRING("The number of weak references held by the " |
michael@0 | 158 | "observer service that are dead."), |
michael@0 | 159 | aData); |
michael@0 | 160 | |
michael@0 | 161 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 162 | return rv; |
michael@0 | 163 | |
michael@0 | 164 | return NS_OK; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 168 | // nsObserverService Implementation |
michael@0 | 169 | |
michael@0 | 170 | |
michael@0 | 171 | NS_IMPL_ISUPPORTS( |
michael@0 | 172 | nsObserverService, |
michael@0 | 173 | nsIObserverService, |
michael@0 | 174 | nsObserverService, |
michael@0 | 175 | nsIMemoryReporter) |
michael@0 | 176 | |
michael@0 | 177 | nsObserverService::nsObserverService() : |
michael@0 | 178 | mShuttingDown(false) |
michael@0 | 179 | { |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | nsObserverService::~nsObserverService(void) |
michael@0 | 183 | { |
michael@0 | 184 | Shutdown(); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | void |
michael@0 | 188 | nsObserverService::RegisterReporter() |
michael@0 | 189 | { |
michael@0 | 190 | RegisterWeakMemoryReporter(this); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | void |
michael@0 | 194 | nsObserverService::Shutdown() |
michael@0 | 195 | { |
michael@0 | 196 | UnregisterWeakMemoryReporter(this); |
michael@0 | 197 | |
michael@0 | 198 | mShuttingDown = true; |
michael@0 | 199 | |
michael@0 | 200 | mObserverTopicTable.Clear(); |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | nsresult |
michael@0 | 204 | nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr) |
michael@0 | 205 | { |
michael@0 | 206 | LOG(("nsObserverService::Create()")); |
michael@0 | 207 | |
michael@0 | 208 | nsRefPtr<nsObserverService> os = new nsObserverService(); |
michael@0 | 209 | |
michael@0 | 210 | if (!os) |
michael@0 | 211 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 212 | |
michael@0 | 213 | // The memory reporter can not be immediately registered here because |
michael@0 | 214 | // the nsMemoryReporterManager may attempt to get the nsObserverService |
michael@0 | 215 | // during initialization, causing a recursive GetService. |
michael@0 | 216 | nsRefPtr<nsRunnableMethod<nsObserverService> > registerRunnable = |
michael@0 | 217 | NS_NewRunnableMethod(os, &nsObserverService::RegisterReporter); |
michael@0 | 218 | NS_DispatchToCurrentThread(registerRunnable); |
michael@0 | 219 | |
michael@0 | 220 | return os->QueryInterface(aIID, aInstancePtr); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | #define NS_ENSURE_VALIDCALL \ |
michael@0 | 224 | if (!NS_IsMainThread()) { \ |
michael@0 | 225 | MOZ_CRASH("Using observer service off the main thread!"); \ |
michael@0 | 226 | return NS_ERROR_UNEXPECTED; \ |
michael@0 | 227 | } \ |
michael@0 | 228 | if (mShuttingDown) { \ |
michael@0 | 229 | NS_ERROR("Using observer service after XPCOM shutdown!"); \ |
michael@0 | 230 | return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; \ |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | NS_IMETHODIMP |
michael@0 | 234 | nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic, |
michael@0 | 235 | bool ownsWeak) |
michael@0 | 236 | { |
michael@0 | 237 | LOG(("nsObserverService::AddObserver(%p: %s)", |
michael@0 | 238 | (void*) anObserver, aTopic)); |
michael@0 | 239 | |
michael@0 | 240 | NS_ENSURE_VALIDCALL |
michael@0 | 241 | if (NS_WARN_IF(!anObserver) || NS_WARN_IF(!aTopic)) |
michael@0 | 242 | return NS_ERROR_INVALID_ARG; |
michael@0 | 243 | |
michael@0 | 244 | if (mozilla::net::IsNeckoChild() && !strncmp(aTopic, "http-on-", 8)) { |
michael@0 | 245 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | nsObserverList *observerList = mObserverTopicTable.PutEntry(aTopic); |
michael@0 | 249 | if (!observerList) |
michael@0 | 250 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 251 | |
michael@0 | 252 | return observerList->AddObserver(anObserver, ownsWeak); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | NS_IMETHODIMP |
michael@0 | 256 | nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic) |
michael@0 | 257 | { |
michael@0 | 258 | LOG(("nsObserverService::RemoveObserver(%p: %s)", |
michael@0 | 259 | (void*) anObserver, aTopic)); |
michael@0 | 260 | NS_ENSURE_VALIDCALL |
michael@0 | 261 | if (NS_WARN_IF(!anObserver) || NS_WARN_IF(!aTopic)) |
michael@0 | 262 | return NS_ERROR_INVALID_ARG; |
michael@0 | 263 | |
michael@0 | 264 | nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic); |
michael@0 | 265 | if (!observerList) |
michael@0 | 266 | return NS_ERROR_FAILURE; |
michael@0 | 267 | |
michael@0 | 268 | /* This death grip is to protect against stupid consumers who call |
michael@0 | 269 | RemoveObserver from their Destructor, see bug 485834/bug 325392. */ |
michael@0 | 270 | nsCOMPtr<nsIObserver> kungFuDeathGrip(anObserver); |
michael@0 | 271 | return observerList->RemoveObserver(anObserver); |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | NS_IMETHODIMP |
michael@0 | 275 | nsObserverService::EnumerateObservers(const char* aTopic, |
michael@0 | 276 | nsISimpleEnumerator** anEnumerator) |
michael@0 | 277 | { |
michael@0 | 278 | NS_ENSURE_VALIDCALL |
michael@0 | 279 | if (NS_WARN_IF(!anEnumerator) || NS_WARN_IF(!aTopic)) |
michael@0 | 280 | return NS_ERROR_INVALID_ARG; |
michael@0 | 281 | |
michael@0 | 282 | nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic); |
michael@0 | 283 | if (!observerList) |
michael@0 | 284 | return NS_NewEmptyEnumerator(anEnumerator); |
michael@0 | 285 | |
michael@0 | 286 | return observerList->GetObserverList(anEnumerator); |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | // Enumerate observers of aTopic and call Observe on each. |
michael@0 | 290 | NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports *aSubject, |
michael@0 | 291 | const char *aTopic, |
michael@0 | 292 | const char16_t *someData) |
michael@0 | 293 | { |
michael@0 | 294 | LOG(("nsObserverService::NotifyObservers(%s)", aTopic)); |
michael@0 | 295 | |
michael@0 | 296 | NS_ENSURE_VALIDCALL |
michael@0 | 297 | if (NS_WARN_IF(!aTopic)) |
michael@0 | 298 | return NS_ERROR_INVALID_ARG; |
michael@0 | 299 | |
michael@0 | 300 | nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic); |
michael@0 | 301 | if (observerList) |
michael@0 | 302 | observerList->NotifyObservers(aSubject, aTopic, someData); |
michael@0 | 303 | |
michael@0 | 304 | #ifdef NOTIFY_GLOBAL_OBSERVERS |
michael@0 | 305 | observerList = mObserverTopicTable.GetEntry("*"); |
michael@0 | 306 | if (observerList) |
michael@0 | 307 | observerList->NotifyObservers(aSubject, aTopic, someData); |
michael@0 | 308 | #endif |
michael@0 | 309 | |
michael@0 | 310 | return NS_OK; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | static PLDHashOperator |
michael@0 | 314 | UnmarkGrayObserverEntry(nsObserverList* aObserverList, void* aClosure) |
michael@0 | 315 | { |
michael@0 | 316 | if (aObserverList) { |
michael@0 | 317 | aObserverList->UnmarkGrayStrongObservers(); |
michael@0 | 318 | } |
michael@0 | 319 | return PL_DHASH_NEXT; |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | NS_IMETHODIMP |
michael@0 | 323 | nsObserverService::UnmarkGrayStrongObservers() |
michael@0 | 324 | { |
michael@0 | 325 | NS_ENSURE_VALIDCALL |
michael@0 | 326 | |
michael@0 | 327 | mObserverTopicTable.EnumerateEntries(UnmarkGrayObserverEntry, nullptr); |
michael@0 | 328 | |
michael@0 | 329 | return NS_OK; |
michael@0 | 330 | } |
michael@0 | 331 |