2015-04-09 20:25:05 +03: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/. */
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2003-03-14 08:07:24 +03:00
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK 7
|
|
|
|
|
2000-02-05 09:19:49 +03:00
|
|
|
#include "nsICategoryManager.h"
|
2002-07-18 09:09:10 +04:00
|
|
|
#include "nsCategoryManager.h"
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
#include "plarena.h"
|
|
|
|
#include "prio.h"
|
|
|
|
#include "prlock.h"
|
2000-02-05 09:19:49 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2003-06-18 15:26:27 +04:00
|
|
|
#include "nsTHashtable.h"
|
|
|
|
#include "nsClassHashtable.h"
|
2000-02-06 05:01:39 +03:00
|
|
|
#include "nsIFactory.h"
|
2003-06-18 15:26:27 +04:00
|
|
|
#include "nsIStringEnumerator.h"
|
2002-06-15 01:33:05 +04:00
|
|
|
#include "nsSupportsPrimitives.h"
|
2010-06-10 22:11:11 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2004-11-25 01:48:45 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2001-10-16 07:35:52 +04:00
|
|
|
#include "nsIObserver.h"
|
2005-06-18 01:07:47 +04:00
|
|
|
#include "nsIObserverService.h"
|
2001-10-24 13:06:49 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2003-03-14 08:07:24 +03:00
|
|
|
#include "nsCRT.h"
|
2005-06-13 22:39:19 +04:00
|
|
|
#include "nsQuickSort.h"
|
2000-02-05 09:19:49 +03:00
|
|
|
#include "nsEnumeratorUtils.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
#include "mozilla/Services.h"
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2010-07-20 17:28:45 +04:00
|
|
|
#include "ManifestParser.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2010-05-20 03:22:19 +04:00
|
|
|
|
2009-07-23 18:29:00 +04:00
|
|
|
using namespace mozilla;
|
2003-06-18 15:26:27 +04:00
|
|
|
class nsIComponentLoaderManager;
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
/*
|
|
|
|
CategoryDatabase
|
|
|
|
contains 0 or more 1-1 mappings of string to Category
|
|
|
|
each Category contains 0 or more 1-1 mappings of string keys to string values
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
In other words, the CategoryDatabase is a tree, whose root is a hashtable.
|
|
|
|
Internal nodes (or Categories) are hashtables. Leaf nodes are strings.
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
The leaf strings are allocated in an arena, because we assume they're not
|
|
|
|
going to change much ;)
|
|
|
|
*/
|
2002-10-30 03:57:40 +03:00
|
|
|
|
2003-03-14 08:07:24 +03:00
|
|
|
#define NS_CATEGORYMANAGER_ARENA_SIZE (1024 * 8)
|
|
|
|
|
|
|
|
// pulled in from nsComponentManager.cpp
|
2014-08-03 00:44:06 +04:00
|
|
|
char* ArenaStrdup(const char* aStr, PLArenaPool* aArena);
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
//
|
|
|
|
// BaseStringEnumerator is subclassed by EntryEnumerator and
|
|
|
|
// CategoryEnumerator
|
|
|
|
//
|
|
|
|
class BaseStringEnumerator
|
2014-08-03 00:44:06 +04:00
|
|
|
: public nsISimpleEnumerator
|
|
|
|
, private nsIUTF8StringEnumerator
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
NS_DECL_NSIUTF8STRINGENUMERATOR
|
|
|
|
|
|
|
|
protected:
|
2005-06-13 22:39:19 +04:00
|
|
|
// Callback function for NS_QuickSort to sort mArray
|
2014-08-03 00:44:06 +04:00
|
|
|
static int SortCallback(const void*, const void*, void*);
|
2005-06-13 22:39:19 +04:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
BaseStringEnumerator()
|
2014-08-03 00:44:06 +04:00
|
|
|
: mArray(nullptr)
|
|
|
|
, mCount(0)
|
|
|
|
, mSimpleCurItem(0)
|
|
|
|
, mStringCurItem(0)
|
|
|
|
{
|
|
|
|
}
|
2004-01-22 04:01:19 +03:00
|
|
|
|
|
|
|
// A virtual destructor is needed here because subclasses of
|
|
|
|
// BaseStringEnumerator do not implement their own Release() method.
|
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
virtual ~BaseStringEnumerator()
|
|
|
|
{
|
2014-08-08 18:04:45 +04:00
|
|
|
delete [] mArray;
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
|
|
|
|
2005-06-13 22:39:19 +04:00
|
|
|
void Sort();
|
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
const char** mArray;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCount;
|
|
|
|
uint32_t mSimpleCurItem;
|
|
|
|
uint32_t mStringCurItem;
|
2003-06-18 15:26:27 +04:00
|
|
|
};
|
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
NS_IMPL_ISUPPORTS(BaseStringEnumerator, nsISimpleEnumerator,
|
|
|
|
nsIUTF8StringEnumerator)
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2000-02-05 09:19:49 +03:00
|
|
|
NS_IMETHODIMP
|
2014-08-03 00:44:06 +04:00
|
|
|
BaseStringEnumerator::HasMoreElements(bool* aResult)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
*aResult = (mSimpleCurItem < mCount);
|
2003-06-18 15:26:27 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-08-03 00:44:06 +04:00
|
|
|
BaseStringEnumerator::GetNext(nsISupports** aResult)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (mSimpleCurItem >= mCount) {
|
2003-06-18 15:26:27 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2016-11-16 15:24:59 +03:00
|
|
|
auto* str = new nsSupportsDependentCString(mArray[mSimpleCurItem++]);
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!str) {
|
2003-06-18 15:26:27 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
*aResult = str;
|
|
|
|
NS_ADDREF(*aResult);
|
2003-06-18 15:26:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-08-03 00:44:06 +04:00
|
|
|
BaseStringEnumerator::HasMore(bool* aResult)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
*aResult = (mStringCurItem < mCount);
|
2003-06-18 15:26:27 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-08-03 00:44:06 +04:00
|
|
|
BaseStringEnumerator::GetNext(nsACString& aResult)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (mStringCurItem >= mCount) {
|
2003-06-18 15:26:27 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
aResult = nsDependentCString(mArray[mStringCurItem++]);
|
2003-06-18 15:26:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-06-13 22:39:19 +04:00
|
|
|
int
|
2014-08-03 00:44:06 +04:00
|
|
|
BaseStringEnumerator::SortCallback(const void* aE1, const void* aE2,
|
|
|
|
void* /*unused*/)
|
2005-06-13 22:39:19 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
char const* const* s1 = reinterpret_cast<char const* const*>(aE1);
|
|
|
|
char const* const* s2 = reinterpret_cast<char const* const*>(aE2);
|
2005-06-13 22:39:19 +04:00
|
|
|
|
|
|
|
return strcmp(*s1, *s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BaseStringEnumerator::Sort()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_QuickSort(mArray, mCount, sizeof(mArray[0]), SortCallback, nullptr);
|
2005-06-13 22:39:19 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// EntryEnumerator is the wrapper that allows nsICategoryManager::EnumerateCategory
|
|
|
|
//
|
|
|
|
class EntryEnumerator
|
|
|
|
: public BaseStringEnumerator
|
2000-08-10 10:19:37 +04:00
|
|
|
{
|
2003-06-18 15:26:27 +04:00
|
|
|
public:
|
|
|
|
static EntryEnumerator* Create(nsTHashtable<CategoryLeaf>& aTable);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
EntryEnumerator*
|
|
|
|
EntryEnumerator::Create(nsTHashtable<CategoryLeaf>& aTable)
|
|
|
|
{
|
2016-11-16 15:24:59 +03:00
|
|
|
auto* enumObj = new EntryEnumerator();
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!enumObj) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
|
|
|
enumObj->mArray = new char const* [aTable.Count()];
|
|
|
|
if (!enumObj->mArray) {
|
|
|
|
delete enumObj;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 02:54:58 +03:00
|
|
|
for (auto iter = aTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
CategoryLeaf* leaf = iter.Get();
|
|
|
|
if (leaf->value) {
|
|
|
|
enumObj->mArray[enumObj->mCount++] = leaf->GetKey();
|
|
|
|
}
|
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2005-06-13 22:39:19 +04:00
|
|
|
enumObj->Sort();
|
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
return enumObj;
|
2000-08-10 10:19:37 +04:00
|
|
|
}
|
|
|
|
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
//
|
|
|
|
// CategoryNode implementations
|
|
|
|
//
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
CategoryNode*
|
|
|
|
CategoryNode::Create(PLArenaPool* aArena)
|
|
|
|
{
|
2014-08-25 23:17:15 +04:00
|
|
|
return new (aArena) CategoryNode();
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
|
|
|
|
2016-11-16 15:25:31 +03:00
|
|
|
CategoryNode::~CategoryNode() = default;
|
2003-06-18 15:26:27 +04:00
|
|
|
|
|
|
|
void*
|
|
|
|
CategoryNode::operator new(size_t aSize, PLArenaPool* aArena)
|
|
|
|
{
|
|
|
|
void* p;
|
|
|
|
PL_ARENA_ALLOCATE(p, aArena, aSize);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-08-15 07:30:14 +03:00
|
|
|
nsresult
|
2003-06-18 15:26:27 +04:00
|
|
|
CategoryNode::GetLeaf(const char* aEntryName,
|
2014-08-03 00:44:06 +04:00
|
|
|
char** aResult)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2009-07-23 18:29:00 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2003-06-18 15:26:27 +04:00
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
2014-08-03 00:44:06 +04:00
|
|
|
CategoryLeaf* ent = mTable.GetEntry(aEntryName);
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
if (ent && ent->value) {
|
2014-08-03 00:44:06 +04:00
|
|
|
*aResult = NS_strdup(ent->value);
|
|
|
|
if (*aResult) {
|
2003-06-18 15:26:27 +04:00
|
|
|
rv = NS_OK;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-08-15 07:30:14 +03:00
|
|
|
nsresult
|
2003-06-18 15:26:27 +04:00
|
|
|
CategoryNode::AddLeaf(const char* aEntryName,
|
|
|
|
const char* aValue,
|
2010-06-28 05:14:54 +04:00
|
|
|
bool aReplace,
|
2014-08-03 00:44:06 +04:00
|
|
|
char** aResult,
|
2003-06-18 15:26:27 +04:00
|
|
|
PLArenaPool* aArena)
|
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (aResult) {
|
|
|
|
*aResult = nullptr;
|
|
|
|
}
|
2010-06-28 05:14:54 +04:00
|
|
|
|
2009-07-23 18:29:00 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-08-03 00:44:06 +04:00
|
|
|
CategoryLeaf* leaf = mTable.GetEntry(aEntryName);
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
if (!leaf) {
|
2003-06-18 15:26:27 +04:00
|
|
|
const char* arenaEntryName = ArenaStrdup(aEntryName, aArena);
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!arenaEntryName) {
|
2010-06-28 05:14:54 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-06-28 05:14:54 +04:00
|
|
|
|
|
|
|
leaf = mTable.PutEntry(arenaEntryName);
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!leaf) {
|
2010-06-28 05:14:54 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
if (leaf->value && !aReplace) {
|
2010-06-28 05:14:54 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-06-28 05:14:54 +04:00
|
|
|
|
|
|
|
const char* arenaValue = ArenaStrdup(aValue, aArena);
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!arenaValue) {
|
2010-06-28 05:14:54 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-06-28 05:14:54 +04:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
if (aResult && leaf->value) {
|
|
|
|
*aResult = ToNewCString(nsDependentCString(leaf->value));
|
|
|
|
if (!*aResult) {
|
2010-06-28 05:14:54 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
2010-06-28 05:14:54 +04:00
|
|
|
|
|
|
|
leaf->value = arenaValue;
|
|
|
|
return NS_OK;
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2010-06-24 22:31:18 +04:00
|
|
|
void
|
|
|
|
CategoryNode::DeleteLeaf(const char* aEntryName)
|
|
|
|
{
|
|
|
|
// we don't throw any errors, because it normally doesn't matter
|
|
|
|
// and it makes JS a lot cleaner
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// we can just remove the entire hash entry without introspection
|
|
|
|
mTable.RemoveEntry(aEntryName);
|
|
|
|
}
|
|
|
|
|
2016-08-15 07:30:14 +03:00
|
|
|
nsresult
|
2014-08-03 00:44:06 +04:00
|
|
|
CategoryNode::Enumerate(nsISimpleEnumerator** aResult)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (NS_WARN_IF(!aResult)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2009-07-23 18:29:00 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2003-06-18 15:26:27 +04:00
|
|
|
EntryEnumerator* enumObj = EntryEnumerator::Create(mTable);
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!enumObj) {
|
2003-06-18 15:26:27 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
*aResult = enumObj;
|
|
|
|
NS_ADDREF(*aResult);
|
2003-06-18 15:26:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-02-06 11:26:13 +03:00
|
|
|
|
2012-10-19 16:07:36 +04:00
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
CategoryNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf)
|
2012-10-19 16:07:36 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
// We don't measure the strings pointed to by the entries because the
|
|
|
|
// pointers are non-owning.
|
2015-07-29 11:50:52 +03:00
|
|
|
return mTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2012-10-19 16:07:36 +04:00
|
|
|
}
|
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
//
|
|
|
|
// CategoryEnumerator class
|
|
|
|
//
|
|
|
|
|
|
|
|
class CategoryEnumerator
|
|
|
|
: public BaseStringEnumerator
|
|
|
|
{
|
|
|
|
public:
|
2014-08-03 00:44:06 +04:00
|
|
|
static CategoryEnumerator* Create(nsClassHashtable<nsDepCharHashKey,
|
|
|
|
CategoryNode>& aTable);
|
2003-06-18 15:26:27 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
CategoryEnumerator*
|
2014-08-03 00:44:06 +04:00
|
|
|
CategoryEnumerator::Create(nsClassHashtable<nsDepCharHashKey, CategoryNode>&
|
|
|
|
aTable)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2016-11-16 15:24:59 +03:00
|
|
|
auto* enumObj = new CategoryEnumerator();
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!enumObj) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
|
|
|
enumObj->mArray = new const char* [aTable.Count()];
|
|
|
|
if (!enumObj->mArray) {
|
|
|
|
delete enumObj;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 02:54:59 +03:00
|
|
|
for (auto iter = aTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
// if a category has no entries, we pretend it doesn't exist
|
2015-07-20 15:21:28 +03:00
|
|
|
CategoryNode* aNode = iter.UserData();
|
2015-07-10 02:54:59 +03:00
|
|
|
if (aNode->Count()) {
|
2015-07-20 15:21:28 +03:00
|
|
|
const char* str = iter.Key();
|
2015-07-10 02:54:59 +03:00
|
|
|
enumObj->mArray[enumObj->mCount++] = str;
|
|
|
|
}
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2015-07-10 02:54:59 +03:00
|
|
|
return enumObj;
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsCategoryManager implementations
|
|
|
|
//
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsCategoryManager, nsICategoryManager, nsIMemoryReporter)
|
2012-10-19 16:07:36 +04:00
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2010-06-10 22:11:11 +04:00
|
|
|
nsCategoryManager::AddRef()
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2010-06-10 22:11:11 +04:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2010-06-10 22:11:11 +04:00
|
|
|
nsCategoryManager::Release()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
nsCategoryManager* nsCategoryManager::gCategoryManager;
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
/* static */ nsCategoryManager*
|
|
|
|
nsCategoryManager::GetSingleton()
|
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!gCategoryManager) {
|
2010-06-10 22:11:11 +04:00
|
|
|
gCategoryManager = new nsCategoryManager();
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-06-10 22:11:11 +04:00
|
|
|
return gCategoryManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
nsCategoryManager::Destroy()
|
|
|
|
{
|
2016-08-11 02:29:44 +03:00
|
|
|
// The nsMemoryReporterManager gets destroyed before the nsCategoryManager,
|
|
|
|
// so we don't need to unregister the nsCategoryManager as a memory reporter.
|
|
|
|
// In debug builds we assert that unregistering fails, as a way (imperfect
|
|
|
|
// but better than nothing) of testing the "destroyed before" part.
|
|
|
|
MOZ_ASSERT(NS_FAILED(UnregisterWeakMemoryReporter(gCategoryManager)));
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
delete gCategoryManager;
|
2012-10-19 16:07:36 +04:00
|
|
|
gCategoryManager = nullptr;
|
2010-06-10 22:11:11 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
|
|
|
nsCategoryManager::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (aOuter) {
|
2010-06-10 22:11:11 +04:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-06-10 22:11:11 +04:00
|
|
|
|
|
|
|
return GetSingleton()->QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCategoryManager::nsCategoryManager()
|
2013-12-08 10:09:10 +04:00
|
|
|
: mLock("nsCategoryManager")
|
2011-10-17 18:59:28 +04:00
|
|
|
, mSuppressNotifications(false)
|
2010-06-10 22:11:11 +04:00
|
|
|
{
|
|
|
|
PL_INIT_ARENA_POOL(&mArena, "CategoryManagerArena",
|
|
|
|
NS_CATEGORYMANAGER_ARENA_SIZE);
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
|
|
|
|
2012-10-19 16:07:36 +04:00
|
|
|
void
|
|
|
|
nsCategoryManager::InitMemoryReporter()
|
|
|
|
{
|
2016-08-11 02:29:44 +03:00
|
|
|
RegisterWeakMemoryReporter(this);
|
2012-10-19 16:07:36 +04:00
|
|
|
}
|
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
nsCategoryManager::~nsCategoryManager()
|
|
|
|
{
|
|
|
|
// the hashtable contains entries that must be deleted before the arena is
|
|
|
|
// destroyed, or else you will have PRLocks undestroyed and other Really
|
|
|
|
// Bad Stuff (TM)
|
|
|
|
mTable.Clear();
|
|
|
|
|
|
|
|
PL_FinishArenaPool(&mArena);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CategoryNode*
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCategoryManager::get_category(const char* aName)
|
|
|
|
{
|
2003-06-18 15:26:27 +04:00
|
|
|
CategoryNode* node;
|
|
|
|
if (!mTable.Get(aName, &node)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
return node;
|
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2013-12-08 10:09:10 +04:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(CategoryManagerMallocSizeOf)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::CollectReports(nsIHandleReportCallback* aHandleReport,
|
2014-05-21 10:06:54 +04:00
|
|
|
nsISupports* aData, bool aAnonymize)
|
2012-10-19 16:07:36 +04:00
|
|
|
{
|
2016-08-24 08:23:45 +03:00
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/xpcom/category-manager", KIND_HEAP, UNITS_BYTES,
|
|
|
|
SizeOfIncludingThis(CategoryManagerMallocSizeOf),
|
|
|
|
"Memory used for the XPCOM category manager.");
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-10-19 16:07:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2013-11-07 09:35:30 +04:00
|
|
|
nsCategoryManager::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
|
2012-10-19 16:07:36 +04:00
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
|
2013-02-12 20:54:38 +04:00
|
|
|
n += PL_SizeOfArenaPoolExcludingPool(&mArena, aMallocSizeOf);
|
2012-10-19 16:07:36 +04:00
|
|
|
|
2015-07-31 07:19:57 +03:00
|
|
|
n += mTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (auto iter = mTable.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
// We don't measure the key string because it's a non-owning pointer.
|
2016-09-03 01:22:09 +03:00
|
|
|
n += iter.Data()->SizeOfExcludingThis(aMallocSizeOf);
|
2015-07-31 07:19:57 +03:00
|
|
|
}
|
2012-10-19 16:07:36 +04:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-01-11 20:28:21 +04:00
|
|
|
namespace {
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class CategoryNotificationRunnable : public Runnable
|
2012-01-11 20:28:21 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CategoryNotificationRunnable(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char* aData)
|
2017-02-11 09:11:48 +03:00
|
|
|
: Runnable("CategoryNotificationRunnable")
|
|
|
|
, mSubject(aSubject)
|
2012-01-11 20:28:21 +04:00
|
|
|
, mTopic(aTopic)
|
|
|
|
, mData(aData)
|
2014-08-03 00:44:06 +04:00
|
|
|
{
|
|
|
|
}
|
2012-01-11 20:28:21 +04:00
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsISupports> mSubject;
|
|
|
|
const char* mTopic;
|
|
|
|
NS_ConvertUTF8toUTF16 mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CategoryNotificationRunnable::Run()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
2014-08-03 00:44:06 +04:00
|
|
|
if (observerService) {
|
2012-01-11 20:28:21 +04:00
|
|
|
observerService->NotifyObservers(mSubject, mTopic, mData.get());
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2012-01-11 20:28:21 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-08-03 00:44:06 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-01-11 20:28:21 +04:00
|
|
|
|
|
|
|
|
2005-06-18 01:07:47 +04:00
|
|
|
void
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCategoryManager::NotifyObservers(const char* aTopic,
|
|
|
|
const char* aCategoryName,
|
|
|
|
const char* aEntryName)
|
2005-06-18 01:07:47 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (mSuppressNotifications) {
|
2005-06-18 01:07:47 +04:00
|
|
|
return;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2005-06-18 01:07:47 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CategoryNotificationRunnable> r;
|
2005-09-19 21:13:32 +04:00
|
|
|
|
2005-06-18 01:07:47 +04:00
|
|
|
if (aEntryName) {
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCOMPtr<nsISupportsCString> entry =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
|
|
|
|
if (!entry) {
|
2005-06-18 01:07:47 +04:00
|
|
|
return;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2005-06-18 01:07:47 +04:00
|
|
|
|
|
|
|
nsresult rv = entry->SetData(nsDependentCString(aEntryName));
|
2014-08-03 00:44:06 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2005-06-18 01:07:47 +04:00
|
|
|
return;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2005-06-18 01:07:47 +04:00
|
|
|
|
2012-01-11 20:28:21 +04:00
|
|
|
r = new CategoryNotificationRunnable(entry, aTopic, aCategoryName);
|
2005-06-18 01:07:47 +04:00
|
|
|
} else {
|
2014-08-03 00:44:06 +04:00
|
|
|
r = new CategoryNotificationRunnable(NS_ISUPPORTS_CAST(nsICategoryManager*,
|
|
|
|
this),
|
|
|
|
aTopic, aCategoryName);
|
2012-01-11 20:28:21 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 19:41:16 +04:00
|
|
|
NS_DispatchToMainThread(r);
|
2005-06-18 01:07:47 +04:00
|
|
|
}
|
|
|
|
|
2000-02-05 09:19:49 +03:00
|
|
|
NS_IMETHODIMP
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCategoryManager::GetCategoryEntry(const char* aCategoryName,
|
|
|
|
const char* aEntryName,
|
|
|
|
char** aResult)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(!aCategoryName) ||
|
|
|
|
NS_WARN_IF(!aEntryName) ||
|
2014-08-03 00:44:06 +04:00
|
|
|
NS_WARN_IF(!aResult)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
nsresult status = NS_ERROR_NOT_AVAILABLE;
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2009-07-23 18:29:00 +04:00
|
|
|
CategoryNode* category;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
category = get_category(aCategoryName);
|
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
|
|
|
if (category) {
|
2014-08-03 00:44:06 +04:00
|
|
|
status = category->GetLeaf(aEntryName, aResult);
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2010-06-24 22:31:18 +04:00
|
|
|
NS_IMETHODIMP
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCategoryManager::AddCategoryEntry(const char* aCategoryName,
|
|
|
|
const char* aEntryName,
|
|
|
|
const char* aValue,
|
|
|
|
bool aPersist,
|
|
|
|
bool aReplace,
|
|
|
|
char** aResult)
|
2010-06-24 22:31:18 +04:00
|
|
|
{
|
|
|
|
if (aPersist) {
|
|
|
|
NS_ERROR("Category manager doesn't support persistence.");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
AddCategoryEntry(aCategoryName, aEntryName, aValue, aReplace, aResult);
|
2010-06-24 22:31:18 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
void
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCategoryManager::AddCategoryEntry(const char* aCategoryName,
|
|
|
|
const char* aEntryName,
|
|
|
|
const char* aValue,
|
2010-06-24 22:31:18 +04:00
|
|
|
bool aReplace,
|
|
|
|
char** aOldValue)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (aOldValue) {
|
2013-10-11 00:41:00 +04:00
|
|
|
*aOldValue = nullptr;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-06-24 22:31:18 +04:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
// Before we can insert a new entry, we'll need to
|
|
|
|
// find the |CategoryNode| to put it in...
|
2009-07-23 18:29:00 +04:00
|
|
|
CategoryNode* category;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
category = get_category(aCategoryName);
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2009-07-23 18:29:00 +04:00
|
|
|
if (!category) {
|
|
|
|
// That category doesn't exist yet; let's make it.
|
|
|
|
category = CategoryNode::Create(&mArena);
|
2014-08-03 00:44:06 +04:00
|
|
|
|
2009-07-23 18:29:00 +04:00
|
|
|
char* categoryName = ArenaStrdup(aCategoryName, &mArena);
|
|
|
|
mTable.Put(categoryName, category);
|
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!category) {
|
2010-06-10 22:11:11 +04:00
|
|
|
return;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2007-06-11 10:10:27 +04:00
|
|
|
// We will need the return value of AddLeaf even if the called doesn't want it
|
2014-08-03 00:44:06 +04:00
|
|
|
char* oldEntry = nullptr;
|
2007-06-11 10:10:27 +04:00
|
|
|
|
2005-06-18 01:07:47 +04:00
|
|
|
nsresult rv = category->AddLeaf(aEntryName,
|
|
|
|
aValue,
|
2010-06-28 05:14:54 +04:00
|
|
|
aReplace,
|
2007-06-11 10:10:27 +04:00
|
|
|
&oldEntry,
|
2005-06-18 01:07:47 +04:00
|
|
|
&mArena);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2007-06-11 10:10:27 +04:00
|
|
|
if (oldEntry) {
|
|
|
|
NotifyObservers(NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID,
|
2014-01-30 02:07:35 +04:00
|
|
|
aCategoryName, aEntryName);
|
2007-06-11 10:10:27 +04:00
|
|
|
}
|
2005-06-18 01:07:47 +04:00
|
|
|
NotifyObservers(NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID,
|
|
|
|
aCategoryName, aEntryName);
|
2007-06-11 10:10:27 +04:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
if (aOldValue) {
|
2010-06-24 22:31:18 +04:00
|
|
|
*aOldValue = oldEntry;
|
2014-08-03 00:44:06 +04:00
|
|
|
} else {
|
2015-04-01 08:29:55 +03:00
|
|
|
free(oldEntry);
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-06-24 22:31:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCategoryManager::DeleteCategoryEntry(const char* aCategoryName,
|
|
|
|
const char* aEntryName,
|
|
|
|
bool aDontPersist)
|
2010-06-24 22:31:18 +04:00
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(!aCategoryName) ||
|
2014-08-03 00:44:06 +04:00
|
|
|
NS_WARN_IF(!aEntryName)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-06-24 22:31:18 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
Note: no errors are reported since failure to delete
|
|
|
|
probably won't hurt you, and returning errors seriously
|
|
|
|
inconveniences JS clients
|
|
|
|
*/
|
|
|
|
|
|
|
|
CategoryNode* category;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
category = get_category(aCategoryName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category) {
|
|
|
|
category->DeleteLeaf(aEntryName);
|
|
|
|
|
|
|
|
NotifyObservers(NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID,
|
|
|
|
aCategoryName, aEntryName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCategoryManager::DeleteCategory(const char* aCategoryName)
|
2010-06-24 22:31:18 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (NS_WARN_IF(!aCategoryName)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-06-24 22:31:18 +04:00
|
|
|
|
|
|
|
// the categories are arena-allocated, so we don't
|
|
|
|
// actually delete them. We just remove all of the
|
|
|
|
// leaf nodes.
|
|
|
|
|
|
|
|
CategoryNode* category;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
category = get_category(aCategoryName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category) {
|
|
|
|
category->Clear();
|
|
|
|
NotifyObservers(NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID,
|
2012-07-30 18:20:58 +04:00
|
|
|
aCategoryName, nullptr);
|
2010-06-24 22:31:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCategoryManager::EnumerateCategory(const char* aCategoryName,
|
|
|
|
nsISimpleEnumerator** aResult)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(!aCategoryName) ||
|
2014-08-03 00:44:06 +04:00
|
|
|
NS_WARN_IF(!aResult)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2009-07-23 18:29:00 +04:00
|
|
|
CategoryNode* category;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
category = get_category(aCategoryName);
|
|
|
|
}
|
2014-08-03 00:44:06 +04:00
|
|
|
|
2003-06-18 15:26:27 +04:00
|
|
|
if (!category) {
|
2014-08-03 00:44:06 +04:00
|
|
|
return NS_NewEmptyEnumerator(aResult);
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
return category->Enumerate(aResult);
|
2003-06-18 15:26:27 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCategoryManager::EnumerateCategories(nsISimpleEnumerator** aResult)
|
2003-06-18 15:26:27 +04:00
|
|
|
{
|
2014-08-03 00:44:06 +04:00
|
|
|
if (NS_WARN_IF(!aResult)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2000-02-05 09:19:49 +03:00
|
|
|
|
2009-07-23 18:29:00 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2003-06-18 15:26:27 +04:00
|
|
|
CategoryEnumerator* enumObj = CategoryEnumerator::Create(mTable);
|
2000-02-05 10:42:30 +03:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!enumObj) {
|
2003-06-18 15:26:27 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2003-06-18 15:26:27 +04:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
*aResult = enumObj;
|
|
|
|
NS_ADDREF(*aResult);
|
2003-06-18 15:26:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
struct writecat_struct
|
|
|
|
{
|
2003-06-18 15:26:27 +04:00
|
|
|
PRFileDesc* fd;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool success;
|
2003-06-18 15:26:27 +04:00
|
|
|
};
|
|
|
|
|
2016-08-15 07:30:14 +03:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsCategoryManager::SuppressNotifications(bool aSuppress)
|
2005-06-18 01:07:47 +04:00
|
|
|
{
|
|
|
|
mSuppressNotifications = aSuppress;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-10-16 07:35:52 +04:00
|
|
|
/*
|
|
|
|
* CreateServicesFromCategory()
|
|
|
|
*
|
2014-08-03 00:44:06 +04:00
|
|
|
* Given a category, this convenience functions enumerates the category and
|
2001-10-16 07:35:52 +04:00
|
|
|
* creates a service of every CID or ContractID registered under the category.
|
|
|
|
* If observerTopic is non null and the service implements nsIObserver,
|
|
|
|
* this will attempt to notify the observer with the origin, observerTopic string
|
|
|
|
* as parameter.
|
|
|
|
*/
|
2011-08-18 17:46:39 +04:00
|
|
|
void
|
2014-08-03 00:44:06 +04:00
|
|
|
NS_CreateServicesFromCategory(const char* aCategory,
|
|
|
|
nsISupports* aOrigin,
|
2016-04-20 22:06:13 +03:00
|
|
|
const char* aObserverTopic,
|
|
|
|
const char16_t* aObserverData)
|
2001-10-16 07:35:52 +04:00
|
|
|
{
|
2010-07-20 17:28:45 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
nsCOMPtr<nsICategoryManager> categoryManager =
|
2010-07-20 17:28:45 +04:00
|
|
|
do_GetService("@mozilla.org/categorymanager;1");
|
2014-08-03 00:44:06 +04:00
|
|
|
if (!categoryManager) {
|
2010-07-20 17:28:45 +04:00
|
|
|
return;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-07-20 17:28:45 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
2014-08-03 00:44:06 +04:00
|
|
|
rv = categoryManager->EnumerateCategory(aCategory,
|
2010-07-20 17:28:45 +04:00
|
|
|
getter_AddRefs(enumerator));
|
2014-08-03 00:44:06 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-07-20 17:28:45 +04:00
|
|
|
return;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-07-20 17:28:45 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIUTF8StringEnumerator> senumerator =
|
|
|
|
do_QueryInterface(enumerator);
|
|
|
|
if (!senumerator) {
|
|
|
|
NS_WARNING("Category enumerator doesn't support nsIUTF8StringEnumerator.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasMore;
|
2010-07-20 17:28:45 +04:00
|
|
|
while (NS_SUCCEEDED(senumerator->HasMore(&hasMore)) && hasMore) {
|
|
|
|
// From here on just skip any error we get.
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString entryString;
|
2014-08-03 00:44:06 +04:00
|
|
|
if (NS_FAILED(senumerator->GetNext(entryString))) {
|
2010-07-20 17:28:45 +04:00
|
|
|
continue;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
|
|
|
|
2010-07-20 17:28:45 +04:00
|
|
|
nsXPIDLCString contractID;
|
2014-08-03 00:44:06 +04:00
|
|
|
rv = categoryManager->GetCategoryEntry(aCategory, entryString.get(),
|
2010-07-20 17:28:45 +04:00
|
|
|
getter_Copies(contractID));
|
2014-08-03 00:44:06 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-07-20 17:28:45 +04:00
|
|
|
continue;
|
2014-08-03 00:44:06 +04:00
|
|
|
}
|
2010-07-20 17:28:45 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> instance = do_GetService(contractID);
|
|
|
|
if (!instance) {
|
|
|
|
LogMessage("While creating services from category '%s', could not create service for entry '%s', contract ID '%s'",
|
2014-08-03 00:44:06 +04:00
|
|
|
aCategory, entryString.get(), contractID.get());
|
2010-07-20 17:28:45 +04:00
|
|
|
continue;
|
2001-10-16 07:35:52 +04:00
|
|
|
}
|
2010-07-20 17:28:45 +04:00
|
|
|
|
2014-08-03 00:44:06 +04:00
|
|
|
if (aObserverTopic) {
|
2010-07-20 17:28:45 +04:00
|
|
|
// try an observer, if it implements it.
|
|
|
|
nsCOMPtr<nsIObserver> observer = do_QueryInterface(instance);
|
2014-08-03 00:44:06 +04:00
|
|
|
if (observer) {
|
2016-04-20 22:06:13 +03:00
|
|
|
observer->Observe(aOrigin, aObserverTopic,
|
2016-07-21 08:03:25 +03:00
|
|
|
aObserverData ? aObserverData : u"");
|
2014-08-03 00:44:06 +04:00
|
|
|
} else {
|
2010-07-20 17:28:45 +04:00
|
|
|
LogMessage("While creating services from category '%s', service for entry '%s', contract ID '%s' does not implement nsIObserver.",
|
2014-08-03 00:44:06 +04:00
|
|
|
aCategory, entryString.get(), contractID.get());
|
|
|
|
}
|
2010-07-20 17:28:45 +04:00
|
|
|
}
|
|
|
|
}
|
2001-10-16 07:35:52 +04:00
|
|
|
}
|