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
|
|
|
|
|
|
|
#ifndef nsCategoryCache_h_
|
|
|
|
#define nsCategoryCache_h_
|
|
|
|
|
2011-12-18 07:47:45 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2006-02-22 17:07:21 +03:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
|
|
|
#include "nsCOMArray.h"
|
2013-10-10 16:48:03 +04:00
|
|
|
#include "nsInterfaceHashtable.h"
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
#include "nsXPCOM.h"
|
2017-05-07 05:55:00 +03:00
|
|
|
#include "MainThreadUtils.h"
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsCategoryObserver final : public nsIObserver {
|
2014-07-01 02:11:53 +04:00
|
|
|
~nsCategoryObserver();
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
public:
|
2018-07-24 03:41:06 +03:00
|
|
|
explicit nsCategoryObserver(const nsACString& aCategory);
|
2014-06-27 05:35:39 +04:00
|
|
|
|
|
|
|
void ListenerDied();
|
2017-05-07 05:55:00 +03:00
|
|
|
void SetListener(void(aCallback)(void*), void* aClosure);
|
2014-06-27 05:35:39 +04:00
|
|
|
nsInterfaceHashtable<nsCStringHashKey, nsISupports>& GetHash() {
|
|
|
|
return mHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
private:
|
|
|
|
void RemoveObservers();
|
|
|
|
|
|
|
|
nsInterfaceHashtable<nsCStringHashKey, nsISupports> mHash;
|
|
|
|
nsCString mCategory;
|
2017-05-07 05:55:00 +03:00
|
|
|
void (*mCallback)(void*);
|
|
|
|
void* mClosure;
|
2014-06-27 05:35:39 +04:00
|
|
|
bool mObserversRemoved;
|
2006-02-22 17:07:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a helper class that caches services that are registered in a certain
|
|
|
|
* category. The intended usage is that a service stores a variable of type
|
|
|
|
* nsCategoryCache<nsIFoo> in a member variable, where nsIFoo is the interface
|
|
|
|
* that these services should implement. The constructor of this class should
|
|
|
|
* then get the name of the category.
|
|
|
|
*/
|
|
|
|
template <class T>
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsCategoryCache final {
|
2014-06-27 05:35:39 +04:00
|
|
|
public:
|
|
|
|
explicit nsCategoryCache(const char* aCategory) : mCategoryName(aCategory) {
|
2017-05-07 05:55:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
~nsCategoryCache() {
|
2017-05-07 05:55:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-06-27 05:35:39 +04:00
|
|
|
if (mObserver) {
|
|
|
|
mObserver->ListenerDied();
|
2013-10-10 16:48:03 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetEntries(nsCOMArray<T>& aResult) {
|
2017-05-07 05:55:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
LazyInit();
|
|
|
|
|
|
|
|
AddEntries(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns an nsCOMArray of interface pointers to the cached
|
|
|
|
* category enries for the requested category. This was added in order to be
|
|
|
|
* used in call sites where the overhead of excessive allocations can be
|
|
|
|
* unacceptable. See bug 1360971 for an example.
|
|
|
|
*/
|
|
|
|
const nsCOMArray<T>& GetCachedEntries() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
LazyInit();
|
|
|
|
|
|
|
|
if (mCachedEntries.IsEmpty()) {
|
|
|
|
AddEntries(mCachedEntries);
|
|
|
|
}
|
|
|
|
return mCachedEntries;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void LazyInit() {
|
2014-06-27 05:35:39 +04:00
|
|
|
// Lazy initialization, so that services in this category can't
|
|
|
|
// cause reentrant getService (bug 386376)
|
|
|
|
if (!mObserver) {
|
2018-07-24 03:41:06 +03:00
|
|
|
mObserver = new nsCategoryObserver(mCategoryName);
|
2017-05-07 05:55:00 +03:00
|
|
|
mObserver->SetListener(nsCategoryCache<T>::OnCategoryChanged, this);
|
2013-10-10 16:48:03 +04:00
|
|
|
}
|
2017-05-07 05:55:00 +03:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2017-05-07 05:55:00 +03:00
|
|
|
void AddEntries(nsCOMArray<T>& aResult) {
|
2015-07-10 02:54:59 +03:00
|
|
|
for (auto iter = mObserver->GetHash().Iter(); !iter.Done(); iter.Next()) {
|
2015-07-20 15:21:28 +03:00
|
|
|
nsISupports* entry = iter.UserData();
|
2015-07-10 02:54:59 +03:00
|
|
|
nsCOMPtr<T> service = do_QueryInterface(entry);
|
|
|
|
if (service) {
|
2016-06-21 01:38:10 +03:00
|
|
|
aResult.AppendElement(service.forget());
|
2015-07-10 02:54:59 +03:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2017-05-07 05:55:00 +03:00
|
|
|
static void OnCategoryChanged(void* aClosure) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
auto self = static_cast<nsCategoryCache<T>*>(aClosure);
|
|
|
|
self->mCachedEntries.Clear();
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
private:
|
|
|
|
// Not to be implemented
|
|
|
|
nsCategoryCache(const nsCategoryCache<T>&);
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCString mCategoryName;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsCategoryObserver> mObserver;
|
2017-05-07 05:55:00 +03:00
|
|
|
nsCOMArray<T> mCachedEntries;
|
2013-10-10 16:48:03 +04:00
|
|
|
};
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
#endif
|