2006-02-22 17:07:21 +03:00
|
|
|
/* vim:set st=2 sts=2 ts=2 et cin: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
#include "nsIObserverService.h"
|
2011-12-09 23:36:17 +04:00
|
|
|
#include "mozilla/Services.h"
|
2006-02-22 17:07:21 +03:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2013-10-10 16:48:03 +04:00
|
|
|
#include "nsIStringEnumerator.h"
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
#include "nsXPCOMCID.h"
|
|
|
|
|
|
|
|
#include "nsCategoryCache.h"
|
|
|
|
|
2013-10-10 16:48:03 +04:00
|
|
|
nsCategoryObserver::nsCategoryObserver(const char* aCategory)
|
|
|
|
: mCategory(aCategory)
|
|
|
|
, mObserversRemoved(false)
|
2006-02-22 17:07:21 +03:00
|
|
|
{
|
|
|
|
// First, enumerate the currently existing entries
|
|
|
|
nsCOMPtr<nsICategoryManager> catMan =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
|
|
|
|
if (!catMan)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
|
|
nsresult rv = catMan->EnumerateCategory(aCategory,
|
|
|
|
getter_AddRefs(enumerator));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
2013-10-10 16:48:03 +04:00
|
|
|
nsCOMPtr<nsIUTF8StringEnumerator> strings = do_QueryInterface(enumerator);
|
|
|
|
MOZ_ASSERT(strings);
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2013-10-10 16:48:03 +04:00
|
|
|
bool more;
|
|
|
|
while (NS_SUCCEEDED(strings->HasMore(&more)) && more) {
|
|
|
|
nsAutoCString entryName;
|
|
|
|
strings->GetNext(entryName);
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2013-10-10 16:48:03 +04:00
|
|
|
nsCString entryValue;
|
|
|
|
rv = catMan->GetCategoryEntry(aCategory,
|
|
|
|
entryName.get(),
|
|
|
|
getter_Copies(entryValue));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsISupports> service = do_GetService(entryValue.get());
|
|
|
|
if (service) {
|
|
|
|
mHash.Put(entryName, service);
|
2006-02-22 17:07:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, listen for changes
|
|
|
|
nsCOMPtr<nsIObserverService> serv =
|
2011-12-09 23:36:17 +04:00
|
|
|
mozilla::services::GetObserverService();
|
2009-11-19 18:51:00 +03:00
|
|
|
if (serv) {
|
2011-10-17 18:59:28 +04:00
|
|
|
serv->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
|
|
|
serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID, false);
|
|
|
|
serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID, false);
|
|
|
|
serv->AddObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID, false);
|
2009-11-19 18:51:00 +03:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCategoryObserver::~nsCategoryObserver() {
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsCategoryObserver, nsIObserver)
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCategoryObserver::ListenerDied() {
|
2009-11-19 16:15:05 +03:00
|
|
|
RemoveObservers();
|
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2013-10-10 16:48:03 +04:00
|
|
|
void
|
2009-11-19 16:15:05 +03:00
|
|
|
nsCategoryObserver::RemoveObservers() {
|
2010-09-08 02:22:00 +04:00
|
|
|
if (mObserversRemoved)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mObserversRemoved = true;
|
2009-11-19 16:15:05 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc =
|
2011-12-09 23:36:17 +04:00
|
|
|
mozilla::services::GetObserverService();
|
2009-11-19 16:15:05 +03:00
|
|
|
if (obsSvc) {
|
|
|
|
obsSvc->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
|
|
|
|
obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID);
|
|
|
|
obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID);
|
|
|
|
obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID);
|
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryObserver::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const PRUnichar* aData) {
|
|
|
|
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
mHash.Clear();
|
2009-11-19 16:15:05 +03:00
|
|
|
RemoveObservers();
|
|
|
|
|
2006-02-22 17:07:21 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-04-26 17:53:48 +04:00
|
|
|
if (!aData ||
|
|
|
|
!nsDependentString(aData).Equals(NS_ConvertASCIItoUTF16(mCategory)))
|
2006-02-22 17:07:21 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString str;
|
2006-02-22 17:07:21 +03:00
|
|
|
nsCOMPtr<nsISupportsCString> strWrapper(do_QueryInterface(aSubject));
|
|
|
|
if (strWrapper)
|
|
|
|
strWrapper->GetData(str);
|
|
|
|
|
|
|
|
if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID) == 0) {
|
2009-11-26 00:50:01 +03:00
|
|
|
// We may get an add notification even when we already have an entry. This
|
|
|
|
// is due to the notification happening asynchronously, so if the entry gets
|
|
|
|
// added and an nsCategoryObserver gets instantiated before events get
|
|
|
|
// processed, we'd get the notification for an existing entry.
|
|
|
|
// Do nothing in that case.
|
2013-10-10 16:48:03 +04:00
|
|
|
if (mHash.GetWeak(str))
|
2009-11-26 00:50:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
2006-02-22 17:07:21 +03:00
|
|
|
nsCOMPtr<nsICategoryManager> catMan =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
|
|
|
|
if (!catMan)
|
|
|
|
return NS_OK;
|
|
|
|
|
2007-04-26 17:53:48 +04:00
|
|
|
nsCString entryValue;
|
2006-02-22 17:07:21 +03:00
|
|
|
catMan->GetCategoryEntry(mCategory.get(),
|
|
|
|
str.get(),
|
|
|
|
getter_Copies(entryValue));
|
|
|
|
|
2013-10-10 16:48:03 +04:00
|
|
|
nsCOMPtr<nsISupports> service = do_GetService(entryValue.get());
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
mHash.Put(str, service);
|
2006-02-22 17:07:21 +03:00
|
|
|
}
|
2013-10-10 16:48:03 +04:00
|
|
|
} else if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID) == 0) {
|
|
|
|
mHash.Remove(str);
|
2006-02-22 17:07:21 +03:00
|
|
|
} else if (strcmp(aTopic, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID) == 0) {
|
|
|
|
mHash.Clear();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|