2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsErrorService.h"
|
2013-09-19 11:29:31 -07:00
|
|
|
#include "nsCRTGlue.h"
|
2013-08-05 08:16:53 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void*
|
2007-03-22 10:30:00 -07:00
|
|
|
CloneCString(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
2009-03-25 06:19:08 -07:00
|
|
|
return NS_strdup((const char*)aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
DeleteCString(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
2009-03-25 06:19:08 -07:00
|
|
|
NS_Free(aData);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsInt2StrHashtable::nsInt2StrHashtable()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mHashtable(CloneCString, nullptr, DeleteCString, nullptr, 16)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsInt2StrHashtable::Put(uint32_t key, const char* aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-03-25 06:19:08 -07:00
|
|
|
char* value = NS_strdup(aData);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (value == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
nsPRUint32Key k(key);
|
|
|
|
char* oldValue = (char*)mHashtable.Put(&k, value);
|
|
|
|
if (oldValue)
|
2009-04-11 09:33:52 -07:00
|
|
|
NS_Free(oldValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2012-08-22 08:56:38 -07:00
|
|
|
nsInt2StrHashtable::Get(uint32_t key)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsPRUint32Key k(key);
|
|
|
|
const char* value = (const char*)mHashtable.Get(&k);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (value == nullptr)
|
|
|
|
return nullptr;
|
2009-03-25 06:19:08 -07:00
|
|
|
return NS_strdup(value);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsInt2StrHashtable::Remove(uint32_t key)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsPRUint32Key k(key);
|
|
|
|
char* oldValue = (char*)mHashtable.Remove(&k);
|
|
|
|
if (oldValue)
|
2009-04-11 09:33:52 -07:00
|
|
|
NS_Free(oldValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsErrorService, nsIErrorService)
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsErrorService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(outer))
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2013-08-05 08:16:53 -07:00
|
|
|
nsRefPtr<nsErrorService> serv = new nsErrorService();
|
|
|
|
return serv->QueryInterface(aIID, aInstancePtr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsErrorService::RegisterErrorStringBundle(int16_t errorModule, const char *stringBundleURL)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mErrorStringBundleURLMap.Put(errorModule, stringBundleURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsErrorService::UnregisterErrorStringBundle(int16_t errorModule)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mErrorStringBundleURLMap.Remove(errorModule);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsErrorService::GetErrorStringBundle(int16_t errorModule, char **result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
char* value = mErrorStringBundleURLMap.Get(errorModule);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (value == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
*result = value;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsErrorService::RegisterErrorStringBundleKey(nsresult error, const char *stringBundleKey)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
return mErrorStringBundleKeyMap.Put(static_cast<uint32_t>(error),
|
2012-08-09 01:36:40 -07:00
|
|
|
stringBundleKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsErrorService::UnregisterErrorStringBundleKey(nsresult error)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
return mErrorStringBundleKeyMap.Remove(static_cast<uint32_t>(error));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsErrorService::GetErrorStringBundleKey(nsresult error, char **result)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
char* value = mErrorStringBundleKeyMap.Get(static_cast<uint32_t>(error));
|
2012-07-30 07:20:58 -07:00
|
|
|
if (value == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
*result = value;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|