2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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)
|
2017-05-07 05:55:00 +03:00
|
|
|
, mCallback(nullptr)
|
|
|
|
, mClosure(nullptr)
|
2013-10-10 16:48:03 +04:00
|
|
|
, mObserversRemoved(false)
|
2006-02-22 17:07:21 +03:00
|
|
|
{
|
2017-05-07 05:55:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2006-02-22 17:07:21 +03:00
|
|
|
// First, enumerate the currently existing entries
|
|
|
|
nsCOMPtr<nsICategoryManager> catMan =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!catMan) {
|
2006-02-22 17:07:21 +03:00
|
|
|
return;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
|
|
nsresult rv = catMan->EnumerateCategory(aCategory,
|
|
|
|
getter_AddRefs(enumerator));
|
2014-06-27 05:35:39 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2006-02-22 17:07:21 +03:00
|
|
|
return;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
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,
|
2014-06-27 05:35:39 +04:00
|
|
|
entryName.get(),
|
|
|
|
getter_Copies(entryValue));
|
2013-10-10 16:48:03 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsISupports> service = do_GetService(entryValue.get());
|
|
|
|
if (service) {
|
2014-06-27 05:35:39 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-16 15:25:31 +03:00
|
|
|
nsCategoryObserver::~nsCategoryObserver() = default;
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsCategoryObserver, nsIObserver)
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCategoryObserver::ListenerDied()
|
|
|
|
{
|
2017-05-07 05:55:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2009-11-19 16:15:05 +03:00
|
|
|
RemoveObservers();
|
2017-05-07 05:55:00 +03:00
|
|
|
mCallback = nullptr;
|
|
|
|
mClosure = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCategoryObserver::SetListener(void(aCallback)(void*), void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mCallback = aCallback;
|
|
|
|
mClosure = aClosure;
|
2009-11-19 16:15:05 +03:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2013-10-10 16:48:03 +04:00
|
|
|
void
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCategoryObserver::RemoveObservers()
|
|
|
|
{
|
2017-05-07 05:55:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (mObserversRemoved) {
|
2010-09-08 02:22:00 +04:00
|
|
|
return;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2010-09-08 02:22:00 +04:00
|
|
|
|
2017-05-07 05:55:00 +03:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback(mClosure);
|
|
|
|
}
|
|
|
|
|
2010-09-08 02:22:00 +04:00
|
|
|
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,
|
2014-06-27 05:35:39 +04:00
|
|
|
const char16_t* aData)
|
|
|
|
{
|
2017-05-07 05:55:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2006-02-22 17:07:21 +03:00
|
|
|
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 ||
|
2014-06-27 05:35:39 +04:00
|
|
|
!nsDependentString(aData).Equals(NS_ConvertASCIItoUTF16(mCategory))) {
|
2006-02-22 17:07:21 +03:00
|
|
|
return NS_OK;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString str;
|
2006-02-22 17:07:21 +03:00
|
|
|
nsCOMPtr<nsISupportsCString> strWrapper(do_QueryInterface(aSubject));
|
2014-06-27 05:35:39 +04:00
|
|
|
if (strWrapper) {
|
2006-02-22 17:07:21 +03:00
|
|
|
strWrapper->GetData(str);
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
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.
|
2014-06-27 05:35:39 +04:00
|
|
|
if (mHash.GetWeak(str)) {
|
2009-11-26 00:50:01 +03:00
|
|
|
return NS_OK;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2009-11-26 00:50:01 +03:00
|
|
|
|
2006-02-22 17:07:21 +03:00
|
|
|
nsCOMPtr<nsICategoryManager> catMan =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!catMan) {
|
2006-02-22 17:07:21 +03:00
|
|
|
return NS_OK;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
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
|
|
|
}
|
2017-05-07 05:55:00 +03:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback(mClosure);
|
|
|
|
}
|
2013-10-10 16:48:03 +04:00
|
|
|
} else if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID) == 0) {
|
|
|
|
mHash.Remove(str);
|
2017-05-07 05:55:00 +03:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback(mClosure);
|
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
} else if (strcmp(aTopic, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID) == 0) {
|
|
|
|
mHash.Clear();
|
2017-05-07 05:55:00 +03:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback(mClosure);
|
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|