fixes bug 219479 "further cleanup for pref backend" patch=alfredkayser@nl.ibm.com r=darin sr=alecf

This commit is contained in:
darin%meer.net 2003-09-23 17:57:22 +00:00
Родитель 7af296afeb
Коммит 9905dca6be
10 изменённых файлов: 309 добавлений и 577 удалений

Просмотреть файл

@ -533,11 +533,7 @@ NS_IMETHODIMP nsPref::UnregisterCallback( const char* domain,
void* instance_data )
//----------------------------------------------------------------------------------------
{
if (PREF_UnregisterCallback(domain, callback, instance_data) == PREF_NOERROR) {
return NS_OK;
} else {
return NS_ERROR_FAILURE;
}
return PREF_UnregisterCallback(domain, callback, instance_data);
}
/*

Просмотреть файл

@ -71,40 +71,11 @@ struct PrefCallbackData {
static NS_DEFINE_CID(kSecurityManagerCID, NS_SCRIPTSECURITYMANAGER_CID);
// Prototypes
extern PrefResult pref_UnlockPref(const char *key);
PR_STATIC_CALLBACK(PLDHashOperator)
pref_enumChild(PLDHashTable *table, PLDHashEntryHdr *heh,
PRUint32 i, void *arg);
static int PR_CALLBACK NotifyObserver(const char *newpref, void *data);
// this needs to be removed!
static nsresult _convertRes(int res)
//---------------------------------------------------------------------------
{
switch (res) {
case PREF_OUT_OF_MEMORY:
return NS_ERROR_OUT_OF_MEMORY;
case PREF_NOT_INITIALIZED:
return NS_ERROR_NOT_INITIALIZED;
case PREF_BAD_PARAMETER:
return NS_ERROR_INVALID_ARG;
case PREF_TYPE_CHANGE_ERR:
case PREF_ERROR:
case PREF_BAD_LOCKFILE:
case PREF_DEFAULT_VALUE_NOT_INITIALIZED:
return NS_ERROR_UNEXPECTED;
case PREF_VALUECHANGED:
return NS_OK;
}
NS_ASSERTION((res >= PREF_DEFAULT_VALUE_NOT_INITIALIZED) && (res <= PREF_PROFILE_UPGRADE), "you added a new error code to prefapi.h and didn't update _convertRes");
return NS_OK;
}
PR_STATIC_CALLBACK(nsresult)
NotifyObserver(const char *newpref, void *data);
/*
* Constructor/Destructor
@ -183,7 +154,7 @@ NS_IMETHODIMP nsPrefBranch::GetBoolPref(const char *aPrefName, PRBool *_retval)
rv = getValidatedPrefName(aPrefName, &pref);
if (NS_SUCCEEDED(rv)) {
rv = _convertRes(PREF_GetBoolPref(pref, _retval, mIsDefault));
rv = PREF_GetBoolPref(pref, _retval, mIsDefault);
}
return rv;
}
@ -195,11 +166,7 @@ NS_IMETHODIMP nsPrefBranch::SetBoolPref(const char *aPrefName, PRInt32 aValue)
rv = getValidatedPrefName(aPrefName, &pref);
if (NS_SUCCEEDED(rv)) {
if (PR_FALSE == mIsDefault) {
rv = _convertRes(PREF_SetBoolPref(pref, aValue));
} else {
rv = _convertRes(PREF_SetDefaultBoolPref(pref, aValue));
}
rv = PREF_SetBoolPref(pref, aValue, mIsDefault);
}
return rv;
}
@ -211,7 +178,7 @@ NS_IMETHODIMP nsPrefBranch::GetCharPref(const char *aPrefName, char **_retval)
rv = getValidatedPrefName(aPrefName, &pref);
if (NS_SUCCEEDED(rv)) {
rv = _convertRes(PREF_CopyCharPref(pref, _retval, mIsDefault));
rv = PREF_CopyCharPref(pref, _retval, mIsDefault);
}
return rv;
}
@ -224,11 +191,7 @@ NS_IMETHODIMP nsPrefBranch::SetCharPref(const char *aPrefName, const char *aValu
NS_ENSURE_ARG_POINTER(aValue);
rv = getValidatedPrefName(aPrefName, &pref);
if (NS_SUCCEEDED(rv)) {
if (PR_FALSE == mIsDefault) {
rv = _convertRes(PREF_SetCharPref(pref, aValue));
} else {
rv = _convertRes(PREF_SetDefaultCharPref(pref, aValue));
}
rv = PREF_SetCharPref(pref, aValue, mIsDefault);
}
return rv;
}
@ -240,7 +203,7 @@ NS_IMETHODIMP nsPrefBranch::GetIntPref(const char *aPrefName, PRInt32 *_retval)
rv = getValidatedPrefName(aPrefName, &pref);
if (NS_SUCCEEDED(rv)) {
rv = _convertRes(PREF_GetIntPref(pref, _retval, mIsDefault));
rv = PREF_GetIntPref(pref, _retval, mIsDefault);
}
return rv;
}
@ -252,11 +215,7 @@ NS_IMETHODIMP nsPrefBranch::SetIntPref(const char *aPrefName, PRInt32 aValue)
rv = getValidatedPrefName(aPrefName, &pref);
if (NS_SUCCEEDED(rv)) {
if (PR_FALSE == mIsDefault) {
rv = _convertRes(PREF_SetIntPref(pref, aValue));
} else {
rv = _convertRes(PREF_SetDefaultIntPref(pref, aValue));
}
rv = PREF_SetIntPref(pref, aValue, mIsDefault);
}
return rv;
}
@ -512,7 +471,7 @@ NS_IMETHODIMP nsPrefBranch::ClearUserPref(const char *aPrefName)
rv = getValidatedPrefName(aPrefName, &pref);
if (NS_SUCCEEDED(rv)) {
rv = _convertRes(PREF_ClearUserPref(pref));
rv = PREF_ClearUserPref(pref);
}
return rv;
}
@ -538,7 +497,7 @@ NS_IMETHODIMP nsPrefBranch::LockPref(const char *aPrefName)
rv = getValidatedPrefName(aPrefName, &pref);
if (NS_SUCCEEDED(rv)) {
rv = _convertRes(PREF_LockPref(pref));
rv = PREF_LockPref(pref, PR_TRUE);
}
return rv;
}
@ -564,7 +523,7 @@ NS_IMETHODIMP nsPrefBranch::UnlockPref(const char *aPrefName)
rv = getValidatedPrefName(aPrefName, &pref);
if (NS_SUCCEEDED(rv)) {
rv = _convertRes(pref_UnlockPref(pref));
rv = PREF_LockPref(pref, PR_FALSE);
}
return rv;
}
@ -582,7 +541,7 @@ NS_IMETHODIMP nsPrefBranch::DeleteBranch(const char *aStartingAt)
rv = getValidatedPrefName(aStartingAt, &pref);
if (NS_SUCCEEDED(rv)) {
rv = _convertRes(PREF_DeleteBranch(pref));
rv = PREF_DeleteBranch(pref);
}
return rv;
}
@ -731,7 +690,7 @@ NS_IMETHODIMP nsPrefBranch::RemoveObserver(const char *aDomain, nsIObserver *aOb
if (domain.Equals(aDomain)) {
// We must pass a fully qualified preference name to remove the callback
pref = getPrefName(aDomain); // aDomain == nsnull only possible failure, trapped above
rv = _convertRes(PREF_UnregisterCallback(pref, NotifyObserver, pCallback));
rv = PREF_UnregisterCallback(pref, NotifyObserver, pCallback);
if (NS_SUCCEEDED(rv)) {
// Remove this observer from our array so that nobody else can remove
// what we're trying to remove ourselves right now.
@ -758,7 +717,7 @@ NS_IMETHODIMP nsPrefBranch::Observe(nsISupports *aSubject, const char *aTopic, c
return NS_OK;
}
static int PR_CALLBACK NotifyObserver(const char *newpref, void *data)
PR_STATIC_CALLBACK(nsresult) NotifyObserver(const char *newpref, void *data)
{
PrefCallbackData *pData = (PrefCallbackData *)data;
nsPrefBranch *prefBranch = NS_STATIC_CAST(nsPrefBranch *, pData->pBranch);
@ -779,14 +738,14 @@ static int PR_CALLBACK NotifyObserver(const char *newpref, void *data)
observer = NS_STATIC_CAST(nsIObserver *, pData->pObserver);
pbi->RemoveObserver(newpref, observer);
}
return 0;
return NS_OK;
}
} else
observer = NS_STATIC_CAST(nsIObserver *, pData->pObserver);
observer->Observe(pData->pBranch, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID,
NS_ConvertASCIItoUCS2(suffix).get());
return 0;
return NS_OK;
}
@ -833,7 +792,7 @@ nsresult nsPrefBranch::GetDefaultFromPropertiesFile(const char *aPrefName, PRUni
// the default value contains a URL to a .properties file
nsXPIDLCString propertyFileURL;
rv = _convertRes(PREF_CopyCharPref(aPrefName, getter_Copies(propertyFileURL), PR_TRUE));
rv = PREF_CopyCharPref(aPrefName, getter_Copies(propertyFileURL), PR_TRUE);
if (NS_FAILED(rv))
return rv;
@ -874,22 +833,9 @@ const char *nsPrefBranch::getPrefName(const char *aPrefName)
nsresult nsPrefBranch::getValidatedPrefName(const char *aPrefName, const char **_retval)
{
static const char capabilityPrefix[] = "capability.";
const char *fullPref;
NS_ENSURE_ARG_POINTER(aPrefName);
// for speed, avoid strcpy if we can:
if (mPrefRoot.IsEmpty()) {
fullPref = aPrefName;
} else {
// isn't there a better way to do this? this is really kind of gross.
mPrefRoot.Truncate(mPrefRootLength);
// only append if anything to append
if ((nsnull != aPrefName) && (*aPrefName != '\0'))
mPrefRoot.Append(aPrefName);
fullPref = mPrefRoot.get();
}
const char *fullPref = getPrefName(aPrefName);
// now that we have the pref, check it against the ScriptSecurityManager
if ((fullPref[0] == 'c') &&
@ -898,10 +844,10 @@ nsresult nsPrefBranch::getValidatedPrefName(const char *aPrefName, const char **
nsresult rv;
nsCOMPtr<nsIScriptSecurityManager> secMan =
do_GetService(kSecurityManagerCID, &rv);
PRBool enabled;
if (NS_FAILED(rv))
return NS_ERROR_FAILURE;
PRBool enabled;
rv = secMan->IsCapabilityEnabled("CapabilityPreferencesAccess", &enabled);
if (NS_FAILED(rv) || !enabled)
return NS_ERROR_FAILURE;
@ -934,37 +880,37 @@ pref_enumChild(PLDHashTable *table, PLDHashEntryHdr *heh,
*/
NS_IMETHODIMP nsPrefBranch::SecurityGetBoolPref(const char *pref, PRBool * return_val)
{
return _convertRes(PREF_GetBoolPref(getPrefName(pref), return_val, PR_FALSE));
return PREF_GetBoolPref(getPrefName(pref), return_val, PR_FALSE);
}
NS_IMETHODIMP nsPrefBranch::SecuritySetBoolPref(const char *pref, PRBool value)
{
return _convertRes(PREF_SetBoolPref(getPrefName(pref), value));
return PREF_SetBoolPref(getPrefName(pref), value);
}
NS_IMETHODIMP nsPrefBranch::SecurityGetCharPref(const char *pref, char ** return_buf)
{
return _convertRes(PREF_CopyCharPref(getPrefName(pref), return_buf, PR_FALSE));
return PREF_CopyCharPref(getPrefName(pref), return_buf, PR_FALSE);
}
NS_IMETHODIMP nsPrefBranch::SecuritySetCharPref(const char *pref, const char* value)
{
return _convertRes(PREF_SetCharPref(getPrefName(pref), value));
return PREF_SetCharPref(getPrefName(pref), value);
}
NS_IMETHODIMP nsPrefBranch::SecurityGetIntPref(const char *pref, PRInt32 * return_val)
{
return _convertRes(PREF_GetIntPref(getPrefName(pref), return_val, PR_FALSE));
return PREF_GetIntPref(getPrefName(pref), return_val, PR_FALSE);
}
NS_IMETHODIMP nsPrefBranch::SecuritySetIntPref(const char *pref, PRInt32 value)
{
return _convertRes(PREF_SetIntPref(getPrefName(pref), value));
return PREF_SetIntPref(getPrefName(pref), value);
}
NS_IMETHODIMP nsPrefBranch::SecurityClearUserPref(const char *pref_name)
{
return _convertRes(PREF_ClearUserPref(getPrefName(pref_name)));
return PREF_ClearUserPref(getPrefName(pref_name));
}
//----------------------------------------------------------------------------

Просмотреть файл

@ -56,6 +56,7 @@
#include "pldhash.h"
#include "prefapi.h"
#include "prefread.h"
#include "prefapi_private_data.h"
// supporting PREF_Init()
@ -70,16 +71,15 @@
// Definitions
#define INITIAL_MAX_DEFAULT_PREF_FILES 10
#define PREF_READ_BUFFER_SIZE 4096
// Prototypes
#ifdef MOZ_PROFILESHARING
static PRBool isSharingEnabled();
#endif
static nsresult openPrefFile(nsIFile* aFile, PRBool aIsErrorFatal,
PRBool aIsGlobalContext, PRBool aSkipFirstLine);
static nsresult openPrefFile(nsIFile* aFile);
static nsresult pref_InitInitialObjects(void);
// needed so we can still get the JS Runtime Service during XPCOM shutdown
static nsIJSRuntimeService* gJSRuntimeService = nsnull; // owning reference
@ -144,8 +144,11 @@ nsresult nsPrefService::Init()
nsXPIDLCString lockFileName;
nsresult rv;
if (!PREF_Init(nsnull))
return NS_ERROR_FAILURE;
rv = PREF_Init();
NS_ENSURE_SUCCESS(rv, rv);
rv = pref_InitInitialObjects();
NS_ENSURE_SUCCESS(rv, rv);
/*
* The following is a small hack which will allow us to only load the library
@ -231,10 +234,10 @@ NS_IMETHODIMP nsPrefService::ResetPrefs()
NotifyServiceObservers(NS_PREFSERVICE_RESET_TOPIC_ID);
PREF_CleanupPrefs();
if (!PREF_Init(nsnull))
return NS_ERROR_FAILURE;
nsresult rv = PREF_Init();
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
return pref_InitInitialObjects();
}
NS_IMETHODIMP nsPrefService::ResetUserPrefs()
@ -378,7 +381,7 @@ nsresult nsPrefService::UseUserPrefFile()
if (NS_SUCCEEDED(rv) && aFile) {
rv = aFile->AppendNative(NS_LITERAL_CSTRING("user.js"));
if (NS_SUCCEEDED(rv)) {
rv = openPrefFile(aFile, PR_FALSE, PR_FALSE, PR_FALSE);
rv = openPrefFile(aFile);
}
}
return rv;
@ -400,11 +403,10 @@ nsresult nsPrefService::ReadAndOwnUserPrefFile(nsIFile *aFile)
#endif
// We need to track errors in reading the shared and the
// non-shared files independently. Clear gErrorOpeningUserPrefs
// and set the appropriate member variable from it after reading.
gErrorOpeningUserPrefs = PR_FALSE;
nsresult rv = openPrefFile(mCurrentFile, PR_TRUE, PR_FALSE, PR_TRUE);
mErrorOpeningUserPrefs = gErrorOpeningUserPrefs;
// non-shared files independently.
// Set the appropriate member variable from it after reading.
nsresult rv = openPrefFile(mCurrentFile);
mErrorOpeningUserPrefs = NS_FAILED(rv);
#ifdef MOZ_PROFILESHARING
gSharedPrefHandler->ReadingUserPrefs(PR_FALSE);
@ -430,12 +432,11 @@ nsresult nsPrefService::ReadAndOwnSharedUserPrefFile(nsIFile *aFile)
#endif
// We need to track errors in reading the shared and the
// non-shared files independently. Clear gErrorOpeningUserPrefs
// and set the appropriate member variable from it after reading.
gErrorOpeningUserPrefs = PR_FALSE;
nsresult rv = openPrefFile(mCurrentSharedFile, PR_TRUE, PR_FALSE, PR_TRUE);
mErrorOpeningSharedUserPrefs = gErrorOpeningUserPrefs;
// non-shared files independently.
// Set the appropriate member variable from it after reading.
nsresult rv = openPrefFile(mCurrentSharedFile);
mErrorOpeningSharedUserPrefs = NS_FAILED(rv);
#ifdef MOZ_PROFILESHARING
gSharedPrefHandler->ReadingUserPrefs(PR_FALSE);
#endif
@ -594,14 +595,10 @@ static PRBool isSharingEnabled()
}
#endif
static nsresult openPrefFile(nsIFile* aFile, PRBool aIsErrorFatal,
PRBool aIsGlobalContext, PRBool aSkipFirstLine)
static nsresult openPrefFile(nsIFile* aFile)
{
nsCOMPtr<nsIInputStream> inStr;
char *readBuf;
PRInt64 llFileSize;
PRUint32 fileSize;
nsresult rv;
char readBuf[PREF_READ_BUFFER_SIZE];
#if MOZ_TIMELINE
{
@ -611,51 +608,24 @@ static nsresult openPrefFile(nsIFile* aFile, PRBool aIsErrorFatal,
}
#endif
rv = aFile->GetFileSize(&llFileSize);
if (NS_FAILED(rv))
return rv;
LL_L2UI(fileSize, llFileSize); // Converting 64 bit structure to unsigned int
// Now that we know the file exists, set this flag until we have
// successfully read and evaluated the prefs file. This will
// prevent us from writing an empty or partial prefs.js.
gErrorOpeningUserPrefs = aIsErrorFatal;
rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), aFile);
nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), aFile);
if (NS_FAILED(rv))
return rv;
// XXX maybe we should read the file in chunks instead??
readBuf = (char *)PR_Malloc(fileSize);
if (!readBuf)
return NS_ERROR_OUT_OF_MEMORY;
PrefParseState ps;
PREF_InitParseState(&ps, PREF_ReaderCallback, NULL);
for (;;) {
PRUint32 amtRead = 0;
rv = inStr->Read(readBuf, sizeof(readBuf), &amtRead);
if (NS_FAILED(rv) || amtRead == 0)
break;
PRUint32 amtRead = 0;
rv = inStr->Read(readBuf, fileSize, &amtRead);
NS_ASSERTION((amtRead == fileSize), "failed to read the entire prefs file!!");
if (amtRead != fileSize)
return NS_ERROR_FAILURE;
#ifdef XP_OS2 /* OS/2 workaround - our system editor adds an EOF character */
if (readBuf[amtRead - 1] == 0x1A) {
amtRead--;
}
#endif
if (NS_SUCCEEDED(rv)) {
nsCAutoString leafName;
aFile->GetNativeLeafName(leafName);
if (PREF_EvaluateConfigScript(readBuf, amtRead, leafName.get(), aIsGlobalContext, PR_TRUE,
aSkipFirstLine))
gErrorOpeningUserPrefs = PR_FALSE;
else
rv = NS_ERROR_FAILURE;
PREF_ParseBuf(&ps, readBuf, amtRead);
}
PR_Free(readBuf);
PREF_FinalizeParseState(&ps);
return rv;
}
/*
* some stuff that gets called from Pref_Init()
*/
@ -688,10 +658,10 @@ inplaceSortCallback(const void *data1, const void *data2, void *privateData)
}
//----------------------------------------------------------------------------------------
PRBool pref_InitInitialObjects()
// Initialize default preference JavaScript buffers from
// appropriate TEXT resources
//----------------------------------------------------------------------------------------
static nsresult pref_InitInitialObjects()
{
nsCOMPtr<nsIFile> aFile;
nsCOMPtr<nsIFile> defaultPrefDir;
@ -721,8 +691,7 @@ PRBool pref_InitInitialObjects()
};
rv = NS_GetSpecialDirectory(NS_APP_PREF_DEFAULTS_50_DIR, getter_AddRefs(defaultPrefDir));
if (NS_FAILED(rv))
return PR_FALSE;
NS_ENSURE_SUCCESS(rv, rv);
nsIFile **defaultPrefFiles = (nsIFile **)nsMemory::Alloc(INITIAL_MAX_DEFAULT_PREF_FILES * sizeof(nsIFile *));
int maxDefaultPrefFiles = INITIAL_MAX_DEFAULT_PREF_FILES;
@ -730,16 +699,16 @@ PRBool pref_InitInitialObjects()
// Parse all the random files that happen to be in the components directory.
nsCOMPtr<nsISimpleEnumerator> dirIterator;
rv = defaultPrefDir->GetDirectoryEntries(getter_AddRefs(dirIterator));
defaultPrefDir->GetDirectoryEntries(getter_AddRefs(dirIterator));
if (!dirIterator) {
NS_ASSERTION(NS_SUCCEEDED(rv), "ERROR: Could not make a directory iterator.");
return PR_FALSE;
NS_ERROR("ERROR: Could not make a directory iterator.");
return NS_ERROR_FAILURE;
}
dirIterator->HasMoreElements(&hasMoreElements);
if (!hasMoreElements) {
NS_ASSERTION(NS_SUCCEEDED(rv), "ERROR: Prefs directory is empty.");
return PR_FALSE;
NS_ERROR("ERROR: Prefs directory is empty.");
return NS_ERROR_FAILURE;
}
while (hasMoreElements) {
@ -772,13 +741,13 @@ PRBool pref_InitInitialObjects()
}
}
}
};
}
NS_QuickSort((void *)defaultPrefFiles, numFiles, sizeof(nsIFile *), inplaceSortCallback, nsnull);
int k;
for (k = 0; k < numFiles; k++) {
rv = openPrefFile(defaultPrefFiles[k], PR_FALSE, PR_FALSE, PR_FALSE);
rv = openPrefFile(defaultPrefFiles[k]);
NS_ASSERTION(NS_SUCCEEDED(rv), "Config file not parsed successfully");
NS_RELEASE(defaultPrefFiles[k]);
}
@ -792,11 +761,12 @@ PRBool pref_InitInitialObjects()
if (NS_SUCCEEDED(rv)) {
rv = aFile->AppendNative(nsDependentCString(specialFiles[k]));
if (NS_SUCCEEDED(rv)) {
rv = openPrefFile(aFile, PR_FALSE, PR_FALSE, PR_FALSE);
rv = openPrefFile(aFile);
NS_ASSERTION(NS_SUCCEEDED(rv), "<platform>.js was not parsed successfully");
}
}
}
return PR_TRUE;
return NS_OK;
}

Просмотреть файл

@ -153,17 +153,15 @@ nsresult nsSharedPrefHandler::OnSavePrefs()
return NS_OK;
}
nsresult nsSharedPrefHandler::OnPrefChanged(PrefAction action,
nsresult nsSharedPrefHandler::OnPrefChanged(PRBool defaultPref,
PrefHashEntry* pref,
PrefValue newValue)
{
if (!mSessionActive)
return NS_OK;
if (action != PREF_SETUSER)
return NS_OK;
if (!IsPrefShared(pref->key))
return NS_OK;
if (mReadingUserPrefs || mProcessingTransaction)
if (!mSessionActive
|| defaultPref
|| !IsPrefShared(pref->key)
|| mReadingUserPrefs
|| mProcessingTransaction)
return NS_OK;
nsresult rv = EnsureTransactionService();
@ -173,7 +171,7 @@ nsresult nsSharedPrefHandler::OnPrefChanged(PrefAction action,
ipcMessageWriter outMsg(256);
outMsg.PutInt32(kCurrentPrefsTransactionDataVersion);
outMsg.PutInt32(action);
outMsg.PutInt32(defaultPref); // XXX: Is not used?
outMsg.PutInt32(prefNameLen + 1);
outMsg.PutBytes(pref->key, prefNameLen + 1);
@ -296,7 +294,7 @@ NS_IMETHODIMP nsSharedPrefHandler::OnTransactionAvailable(PRUint32 aQueueID, con
dataVersion = inMsg.GetInt32();
NS_ENSURE_TRUE(dataVersion == kCurrentPrefsTransactionDataVersion, NS_ERROR_INVALID_ARG);
prefAction = inMsg.GetInt32();
prefAction = inMsg.GetInt32(); //XXX: Is not used?
dataLen = inMsg.GetInt32(); // includes terminating null
stringStart = (const char *)inMsg.GetPtr();
nsDependentCString prefNameStr(stringStart);

Просмотреть файл

@ -71,7 +71,7 @@ public:
nsresult OnSavePrefs();
nsresult OnPrefChanged(PrefAction action,
nsresult OnPrefChanged(PRBool defaultPref,
PrefHashEntry* pref,
PrefValue newValue);

Просмотреть файл

@ -14,7 +14,7 @@
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
@ -22,7 +22,7 @@
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
@ -106,24 +106,22 @@ matchPrefEntry(PLDHashTable*, const PLDHashEntryHdr* entry,
{
const PrefHashEntry *prefEntry =
NS_STATIC_CAST(const PrefHashEntry*,entry);
if (prefEntry->key == key) return PR_TRUE;
if (!prefEntry->key || !key) return PR_FALSE;
const char *otherKey = NS_REINTERPRET_CAST(const char*, key);
return (strcmp(prefEntry->key, otherKey) == 0);
}
PRBool gErrorOpeningUserPrefs = PR_FALSE;
PLDHashTable gHashTable = { nsnull };
static PLArenaPool gPrefNameArena;
PRBool gDirty = PR_FALSE;
static struct CallbackNode* gCallbacks = NULL;
static PRBool gCallbacksEnabled = PR_FALSE;
static PRBool gCallbacksEnabled = PR_TRUE;
static PRBool gIsAnyPrefLocked = PR_FALSE;
static char * gSavedLine = NULL;
static PLDHashTableOps pref_HashTableOps = {
@ -137,7 +135,7 @@ static PLDHashTableOps pref_HashTableOps = {
PL_DHashFinalizeStub,
nsnull,
};
// PR_ALIGN_OF_WORD is only defined on some platforms. ALIGN_OF_WORD has
// already been defined to PR_ALIGN_OF_WORD everywhere
#ifndef PR_ALIGN_OF_WORD
@ -169,11 +167,9 @@ static char *ArenaStrDup(const char* str, PLArenaPool* aArena)
/*---------------------------------------------------------------------------*/
#define PREF_IS_LOCKED(pref) ((pref)->flags & PREF_LOCKED)
#define PREF_IS_CONFIG(pref) ((pref)->flags & PREF_CONFIG)
#define PREF_HAS_USER_VALUE(pref) ((pref)->flags & PREF_USERSET)
#define PREF_TYPE(pref) (PrefType)((pref)->flags & PREF_VALUETYPE_MASK)
static void pref_ReaderCallback(void *closure, const char *pref, PrefValue, PrefType, PrefAction);
static PRBool pref_ValueChanged(PrefValue oldValue, PrefValue newValue, PrefType type);
/* -- Privates */
@ -185,29 +181,26 @@ struct CallbackNode {
};
/* -- Prototypes */
static PrefResult pref_DoCallback(const char* changed_pref);
static nsresult pref_DoCallback(const char* changed_pref);
static PrefResult pref_HashPref(const char *key, PrefValue value, PrefType type, PrefAction action);
static nsresult pref_HashPref(const char *key, PrefValue value, PrefType type, PRBool defaultPref);
static inline PrefHashEntry* pref_HashTableLookup(const void *key);
PRBool PREF_Init(const char *filename)
nsresult PREF_Init()
{
if (!gHashTable.ops) {
if (!PL_DHashTableInit(&gHashTable, &pref_HashTableOps, nsnull,
sizeof(PrefHashEntry), 1024))
sizeof(PrefHashEntry), 1024)) {
gHashTable.ops = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
PL_INIT_ARENA_POOL(&gPrefNameArena, "PrefNameArena",
PREFNAME_ARENA_SIZE);
}
PRBool ok = pref_InitInitialObjects();
if (!ok)
gErrorOpeningUserPrefs = PR_TRUE;
return ok;
return NS_OK;
}
/* Frees the callback list. */
@ -215,7 +208,7 @@ void PREF_Cleanup()
{
struct CallbackNode* node = gCallbacks;
struct CallbackNode* next_node;
while (node)
{
next_node = node->next;
@ -236,27 +229,6 @@ void PREF_CleanupPrefs()
gHashTable.ops = nsnull;
PL_FinishArenaPool(&gPrefNameArena);
}
if (gSavedLine)
free(gSavedLine);
gSavedLine = NULL;
}
/* This is more recent than the below 3 routines which should be obsoleted */
PRBool
PREF_EvaluateConfigScript(const char * js_buffer, size_t length,
const char* filename, PRBool bGlobalContext, PRBool bCallbacks,
PRBool skipFirstLine)
{
gCallbacksEnabled = bCallbacks;
PrefParseState ps;
PREF_InitParseState(&ps, pref_ReaderCallback, NULL);
PREF_ParseBuf(&ps, js_buffer, length);
PREF_FinalizeParseState(&ps);
gCallbacksEnabled = PR_TRUE;
return PR_TRUE;
}
// note that this appends to aResult, and does not assign!
@ -309,63 +281,33 @@ static void str_escape(const char * original, nsAFlatCString& aResult)
/*
** External calls
*/
PrefResult
PREF_SetCharPref(const char *pref_name, const char *value)
nsresult
PREF_SetCharPref(const char *pref_name, const char *value, PRBool set_default)
{
PrefValue pref;
pref.stringVal = (char*) value;
return pref_HashPref(pref_name, pref, PREF_STRING, PREF_SETUSER);
return pref_HashPref(pref_name, pref, PREF_STRING, set_default);
}
PrefResult
PREF_SetIntPref(const char *pref_name, PRInt32 value)
nsresult
PREF_SetIntPref(const char *pref_name, PRInt32 value, PRBool set_default)
{
PrefValue pref;
pref.intVal = value;
return pref_HashPref(pref_name, pref, PREF_INT, PREF_SETUSER);
return pref_HashPref(pref_name, pref, PREF_INT, set_default);
}
PrefResult
PREF_SetBoolPref(const char *pref_name, PRBool value)
nsresult
PREF_SetBoolPref(const char *pref_name, PRBool value, PRBool set_default)
{
PrefValue pref;
pref.boolVal = value;
return pref_HashPref(pref_name, pref, PREF_BOOL, PREF_SETUSER);
return pref_HashPref(pref_name, pref, PREF_BOOL, set_default);
}
/*
** DEFAULT VERSIONS: Call internal with (set_default == PR_TRUE)
*/
PrefResult
PREF_SetDefaultCharPref(const char *pref_name,const char *value)
{
PrefValue pref;
pref.stringVal = (char*) value;
return pref_HashPref(pref_name, pref, PREF_STRING, PREF_SETDEFAULT);
}
PrefResult
PREF_SetDefaultIntPref(const char *pref_name,PRInt32 value)
{
PrefValue pref;
pref.intVal = value;
return pref_HashPref(pref_name, pref, PREF_INT, PREF_SETDEFAULT);
}
PrefResult
PREF_SetDefaultBoolPref(const char *pref_name,PRBool value)
{
PrefValue pref;
pref.boolVal = value;
return pref_HashPref(pref_name, pref, PREF_BOOL, PREF_SETDEFAULT);
}
PLDHashOperator
pref_savePref(PLDHashTable *table, PLDHashEntryHdr *heh, PRUint32 i, void *arg)
@ -382,9 +324,9 @@ pref_savePref(PLDHashTable *table, PLDHashEntryHdr *heh, PRUint32 i, void *arg)
// where we're getting our pref from
PrefValue* sourcePref;
if (PREF_HAS_USER_VALUE(pref) &&
pref_ValueChanged(pref->defaultPref,
pref->userPref,
if (PREF_HAS_USER_VALUE(pref) &&
pref_ValueChanged(pref->defaultPref,
pref->userPref,
(PrefType) PREF_TYPE(pref)))
sourcePref = &pref->userPref;
else if (PREF_IS_LOCKED(pref))
@ -447,30 +389,25 @@ pref_CompareStrings(const void *v1, const void *v2, void *unused)
PRBool PREF_HasUserPref(const char *pref_name)
{
PrefHashEntry *pref;
if (!gHashTable.ops)
return PR_FALSE;
pref = pref_HashTableLookup(pref_name);
PrefHashEntry *pref = pref_HashTableLookup(pref_name);
if (!pref) return PR_FALSE;
/* convert PREF_HAS_USER_VALUE to bool */
return (PREF_HAS_USER_VALUE(pref) != 0);
}
PrefResult PREF_GetCharPref(const char *pref_name, char * return_buffer, int * length, PRBool get_default)
nsresult PREF_GetCharPref(const char *pref_name, char * return_buffer, int * length, PRBool get_default)
{
PrefResult result = PREF_ERROR;
char* stringVal;
PrefHashEntry* pref;
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
return NS_ERROR_NOT_INITIALIZED;
pref = pref_HashTableLookup(pref_name);
nsresult rv = NS_ERROR_UNEXPECTED;
char* stringVal;
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
if (pref)
{
@ -478,7 +415,7 @@ PrefResult PREF_GetCharPref(const char *pref_name, char * return_buffer, int * l
stringVal = pref->defaultPref.stringVal;
else
stringVal = pref->userPref.stringVal;
if (stringVal)
{
if (*length <= 0)
@ -488,24 +425,22 @@ PrefResult PREF_GetCharPref(const char *pref_name, char * return_buffer, int * l
PL_strncpy(return_buffer, stringVal, PR_MIN((size_t)*length - 1, PL_strlen(stringVal) + 1));
return_buffer[*length - 1] = '\0';
}
result = PREF_OK;
rv = NS_OK;
}
}
return result;
return rv;
}
PrefResult
nsresult
PREF_CopyCharPref(const char *pref_name, char ** return_buffer, PRBool get_default)
{
PrefResult result = PREF_ERROR;
char* stringVal;
PrefHashEntry* pref;
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
return NS_ERROR_NOT_INITIALIZED;
pref = pref_HashTableLookup(pref_name);
nsresult rv = NS_ERROR_UNEXPECTED;
char* stringVal;
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
if (pref && (pref->flags & PREF_STRING))
{
@ -513,50 +448,46 @@ PREF_CopyCharPref(const char *pref_name, char ** return_buffer, PRBool get_defau
stringVal = pref->defaultPref.stringVal;
else
stringVal = pref->userPref.stringVal;
if (stringVal) {
*return_buffer = PL_strdup(stringVal);
result = PREF_OK;
rv = NS_OK;
}
}
return result;
return rv;
}
PrefResult PREF_GetIntPref(const char *pref_name,PRInt32 * return_int, PRBool get_default)
nsresult PREF_GetIntPref(const char *pref_name,PRInt32 * return_int, PRBool get_default)
{
PrefResult result = PREF_ERROR;
PrefHashEntry* pref;
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
return NS_ERROR_NOT_INITIALIZED;
pref = pref_HashTableLookup(pref_name);
nsresult rv = NS_ERROR_UNEXPECTED;
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
if (pref && (pref->flags & PREF_INT))
{
if (get_default || PREF_IS_LOCKED(pref) || !PREF_HAS_USER_VALUE(pref))
{
PRInt32 tempInt = pref->defaultPref.intVal;
/* check to see if we even had a default */
if (tempInt == ((PRInt32) BOGUS_DEFAULT_INT_PREF_VALUE))
return PREF_DEFAULT_VALUE_NOT_INITIALIZED;
if (tempInt != ((PRInt32) BOGUS_DEFAULT_INT_PREF_VALUE))
return NS_ERROR_UNEXPECTED;
*return_int = tempInt;
}
else
*return_int = pref->userPref.intVal;
result = PREF_OK;
rv = NS_OK;
}
return result;
return rv;
}
PrefResult PREF_GetBoolPref(const char *pref_name, PRBool * return_value, PRBool get_default)
nsresult PREF_GetBoolPref(const char *pref_name, PRBool * return_value, PRBool get_default)
{
PrefResult result = PREF_ERROR;
PrefHashEntry* pref;
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
return NS_ERROR_NOT_INITIALIZED;
pref = pref_HashTableLookup(pref_name);
nsresult rv = NS_ERROR_UNEXPECTED;
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
//NS_ASSERTION(pref, pref_name);
if (pref && (pref->flags & PREF_BOOL))
{
@ -564,15 +495,17 @@ PrefResult PREF_GetBoolPref(const char *pref_name, PRBool * return_value, PRBool
{
PRBool tempBool = pref->defaultPref.boolVal;
/* check to see if we even had a default */
if (tempBool == ((PRBool) BOGUS_DEFAULT_BOOL_PREF_VALUE))
return PREF_DEFAULT_VALUE_NOT_INITIALIZED;
*return_value = tempBool;
if (tempBool != ((PRBool) BOGUS_DEFAULT_BOOL_PREF_VALUE)) {
*return_value = tempBool;
rv = NS_OK;
}
}
else
else {
*return_value = pref->userPref.boolVal;
result = PREF_OK;
rv = NS_OK;
}
}
return result;
return rv;
}
/* Delete a branch. Used for deleting mime types */
@ -582,28 +515,28 @@ pref_DeleteItem(PLDHashTable *table, PLDHashEntryHdr *heh, PRUint32 i, void *arg
PrefHashEntry* he = NS_STATIC_CAST(PrefHashEntry*,heh);
const char *to_delete = (const char *) arg;
int len = PL_strlen(to_delete);
/* note if we're deleting "ldap" then we want to delete "ldap.xxx"
and "ldap" (if such a leaf node exists) but not "ldap_1.xxx" */
if (to_delete && (PL_strncmp(he->key, to_delete, (PRUint32) len) == 0 ||
(len-1 == (int)PL_strlen(he->key) && PL_strncmp(he->key, to_delete, (PRUint32)(len-1)) == 0)))
return PL_DHASH_REMOVE;
return PL_DHASH_NEXT;
}
PrefResult
nsresult
PREF_DeleteBranch(const char *branch_name)
{
int len = (int)PL_strlen(branch_name);
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
return NS_ERROR_NOT_INITIALIZED;
/* The following check insures that if the branch name already has a "."
* at the end, we don't end up with a "..". This fixes an incompatibility
* between nsIPref, which needs the period added, and nsIPrefBranch which
* does not. When nsIPref goes away this function should be fixed to
* does not. When nsIPref goes away this function should be fixed to
* never add the period at all.
*/
nsCAutoString branch_dot(branch_name);
@ -613,29 +546,27 @@ PREF_DeleteBranch(const char *branch_name)
PL_DHashTableEnumerate(&gHashTable, pref_DeleteItem,
(void*) branch_dot.get());
gDirty = PR_TRUE;
return PREF_NOERROR;
return NS_OK;
}
PrefResult
nsresult
PREF_ClearUserPref(const char *pref_name)
{
PrefResult success = PREF_ERROR;
PrefHashEntry* pref;
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
return NS_ERROR_NOT_INITIALIZED;
pref = pref_HashTableLookup(pref_name);
nsresult rv = NS_ERROR_UNEXPECTED;
PrefHashEntry* pref = pref_HashTableLookup(pref_name);
if (pref && PREF_HAS_USER_VALUE(pref))
{
pref->flags &= ~PREF_USERSET;
if (gCallbacksEnabled)
pref_DoCallback(pref_name);
success = PREF_OK;
gDirty = PR_TRUE;
rv = NS_OK;
}
return success;
return rv;
}
PR_STATIC_CALLBACK(PLDHashOperator)
@ -660,49 +591,46 @@ pref_ClearUserPref(PLDHashTable *table, PLDHashEntryHdr *he, PRUint32,
return PL_DHASH_NEXT;
}
PrefResult
nsresult
PREF_ClearAllUserPrefs()
{
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
return NS_ERROR_NOT_INITIALIZED;
PL_DHashTableEnumerate(&gHashTable, pref_ClearUserPref, nsnull);
gDirty = PR_TRUE;
return PREF_OK;
return NS_OK;
}
PrefResult pref_UnlockPref(const char *key)
nsresult PREF_LockPref(const char *key, PRBool lockit)
{
PrefHashEntry* pref;
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
return NS_ERROR_NOT_INITIALIZED;
pref = pref_HashTableLookup(key);
PrefHashEntry* pref = pref_HashTableLookup(key);
if (!pref)
return PREF_DOES_NOT_EXIST;
return NS_ERROR_UNEXPECTED;
if (PREF_IS_LOCKED(pref))
{
pref->flags &= ~PREF_LOCKED;
if (gCallbacksEnabled)
pref_DoCallback(key);
if (lockit) {
if (!PREF_IS_LOCKED(pref))
{
pref->flags |= PREF_LOCKED;
gIsAnyPrefLocked = PR_TRUE;
if (gCallbacksEnabled)
pref_DoCallback(key);
}
}
return PREF_OK;
}
PrefResult PREF_LockPref(const char *key)
{
PrefHashEntry* pref;
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
pref = pref_HashTableLookup(key);
if (!pref)
return PREF_DOES_NOT_EXIST;
return pref_HashPref(key, pref->defaultPref, (PrefType)pref->flags, PREF_LOCK);
else
{
if (PREF_IS_LOCKED(pref))
{
pref->flags &= ~PREF_LOCKED;
if (gCallbacksEnabled)
pref_DoCallback(key);
}
}
return NS_OK;
}
/*
@ -732,7 +660,7 @@ static void pref_SetValue(PrefValue* oldValue, PrefValue newValue, PrefType type
PR_FREEIF(oldValue->stringVal);
oldValue->stringVal = newValue.stringVal ? PL_strdup(newValue.stringVal) : NULL;
break;
default:
*oldValue = newValue;
}
@ -743,35 +671,32 @@ static inline PrefHashEntry* pref_HashTableLookup(const void *key)
{
PrefHashEntry* result =
NS_STATIC_CAST(PrefHashEntry*, PL_DHashTableOperate(&gHashTable, key, PL_DHASH_LOOKUP));
if (PL_DHASH_ENTRY_IS_FREE(result))
return nsnull;
return result;
}
PrefResult pref_HashPref(const char *key, PrefValue value, PrefType type, PrefAction action)
nsresult pref_HashPref(const char *key, PrefValue value, PrefType type, PRBool set_default)
{
PrefHashEntry* pref;
PrefResult result = PREF_OK;
if (!gHashTable.ops)
return PREF_NOT_INITIALIZED;
pref = NS_STATIC_CAST(PrefHashEntry*, PL_DHashTableOperate(&gHashTable, key, PL_DHASH_ADD));
return NS_ERROR_OUT_OF_MEMORY;
PrefHashEntry* pref = NS_STATIC_CAST(PrefHashEntry*, PL_DHashTableOperate(&gHashTable, key, PL_DHASH_ADD));
if (!pref)
return PREF_OUT_OF_MEMORY;
return NS_ERROR_OUT_OF_MEMORY;
// new entry, better intialize
if (!pref->key) {
// initialize the pref entry
pref->flags = type;
pref->key = ArenaStrDup(key, &gPrefNameArena);
pref->defaultPref.intVal = 0;
pref->userPref.intVal = 0;
/* ugly hack -- define it to a default that no pref will ever
default to this should really get fixed right by some out
of band data
@ -785,77 +710,60 @@ PrefResult pref_HashPref(const char *key, PrefValue value, PrefType type, PrefAc
(type & PREF_VALUETYPE_MASK))
{
NS_WARNING(nsPrintfCString(192, "Trying to set pref %s to with the wrong type!", key).get());
return PREF_TYPE_CHANGE_ERR;
return NS_ERROR_UNEXPECTED;
}
switch (action)
PRBool valueChanged = PR_FALSE;
if (set_default)
{
case PREF_SETDEFAULT:
case PREF_SETCONFIG:
if (!PREF_IS_LOCKED(pref))
{ /* ?? change of semantics? */
if (pref_ValueChanged(pref->defaultPref, value, type))
{
pref_SetValue(&pref->defaultPref, value, type);
if (!PREF_HAS_USER_VALUE(pref))
result = PREF_VALUECHANGED;
}
}
if (action == PREF_SETCONFIG)
pref->flags |= PREF_CONFIG;
break;
case PREF_SETUSER:
/* If setting to the default value, then un-set the user value.
Otherwise, set the user value only if it has changed */
if ( !pref_ValueChanged(pref->defaultPref, value, type) )
{
if (PREF_HAS_USER_VALUE(pref))
{
pref->flags &= ~PREF_USERSET;
if (!PREF_IS_LOCKED(pref))
result = PREF_VALUECHANGED;
}
}
else if ( !PREF_HAS_USER_VALUE(pref) ||
pref_ValueChanged(pref->userPref, value, type) )
{
pref_SetValue(&pref->userPref, value, type);
pref->flags |= PREF_USERSET;
if (!PREF_IS_LOCKED(pref))
result = PREF_VALUECHANGED;
}
break;
case PREF_LOCK:
if (!PREF_IS_LOCKED(pref))
{ /* ?? change of semantics? */
if (pref_ValueChanged(pref->defaultPref, value, type))
{
pref_SetValue(&pref->defaultPref, value, type);
result = PREF_VALUECHANGED;
if (!PREF_HAS_USER_VALUE(pref))
valueChanged = PR_TRUE;
}
else if (!PREF_IS_LOCKED(pref))
}
}
else
{
/* If new value is same as the default value, then un-set the user value.
Otherwise, set the user value only if it has changed */
if ( !pref_ValueChanged(pref->defaultPref, value, type) )
{
if (PREF_HAS_USER_VALUE(pref))
{
result = PREF_VALUECHANGED;
pref->flags &= ~PREF_USERSET;
if (!PREF_IS_LOCKED(pref))
valueChanged = PR_TRUE;
}
pref->flags |= PREF_LOCKED;
gIsAnyPrefLocked = PR_TRUE;
break;
}
else if ( !PREF_HAS_USER_VALUE(pref) ||
pref_ValueChanged(pref->userPref, value, type) )
{
pref_SetValue(&pref->userPref, value, type);
pref->flags |= PREF_USERSET;
if (!PREF_IS_LOCKED(pref))
valueChanged = PR_TRUE;
}
}
if (result == PREF_VALUECHANGED) {
nsresult rv = NS_OK;
if (valueChanged) {
gDirty = PR_TRUE;
if (gCallbacksEnabled) {
PrefResult result2 = pref_DoCallback(key);
if (result2 < 0)
result = result2;
nsresult rv2 = pref_DoCallback(key);
if (NS_FAILED(rv2))
rv = rv2;
}
#ifdef MOZ_PROFILESHARING
if (gSharedPrefHandler)
gSharedPrefHandler->OnPrefChanged(action, pref, value);
gSharedPrefHandler->OnPrefChanged(set_default, pref, value);
#endif
}
return result;
return rv;
}
PrefType
@ -888,7 +796,7 @@ PREF_PrefIsLocked(const char *pref_name)
if (pref && PREF_IS_LOCKED(pref))
result = PR_TRUE;
}
return result;
}
@ -911,15 +819,15 @@ PREF_RegisterCallback(const char *pref_node,
}
/* Deletes a node from the callback list. */
PrefResult
nsresult
PREF_UnregisterCallback(const char *pref_node,
PrefChangedFunc callback,
void * instance_data)
{
PrefResult result = PREF_ERROR;
nsresult rv = NS_ERROR_FAILURE;
struct CallbackNode* node = gCallbacks;
struct CallbackNode* prev_node = NULL;
while (node != NULL)
{
if ( strcmp(node->domain, pref_node) == 0 &&
@ -934,7 +842,7 @@ PREF_UnregisterCallback(const char *pref_node,
PR_Free(node->domain);
PR_Free(node);
node = next_node;
result = PREF_NOERROR;
rv = NS_OK;
}
else
{
@ -942,30 +850,30 @@ PREF_UnregisterCallback(const char *pref_node,
node = node->next;
}
}
return result;
return rv;
}
static PrefResult pref_DoCallback(const char* changed_pref)
static nsresult pref_DoCallback(const char* changed_pref)
{
PrefResult result = PREF_OK;
nsresult rv = NS_OK;
struct CallbackNode* node;
for (node = gCallbacks; node != NULL; node = node->next)
{
if ( PL_strncmp(changed_pref, node->domain, PL_strlen(node->domain)) == 0 )
{
int result2 = (*node->func) (changed_pref, node->data);
if (result2 != 0)
result = (PrefResult)result2;
nsresult rv2 = (*node->func) (changed_pref, node->data);
if (NS_FAILED(rv2))
rv = rv2;
}
}
return result;
return rv;
}
static void pref_ReaderCallback(void *closure,
const char *pref,
PrefValue value,
PrefType type,
PrefAction action)
void PREF_ReaderCallback(void *closure,
const char *pref,
PrefValue value,
PrefType type,
PRBool isDefault)
{
pref_HashPref(pref, value, type, action);
pref_HashPref(pref, value, type, isDefault);
}

Просмотреть файл

@ -41,18 +41,9 @@
#ifndef PREFAPI_H
#define PREFAPI_H
#include "prtypes.h"
#include "nscore.h"
#include "pldhash.h"
#if defined(VMS)
/* Deal with case naming conflicts */
#define pref_CopyCharPref prefl_CopyCharPref
#define pref_GetBoolPref prefl_GetBoolPref
#define pref_GetCharPref prefl_GetCharPref
#define pref_GetIntPref prefl_GetIntPref
#define pref_LockPref prefl_LockPref
#endif /* VMS */
NSPR_BEGIN_EXTERN_C
typedef union
@ -70,102 +61,20 @@ struct PrefHashEntry : PLDHashEntryHdr
PRUint8 flags;
};
/* Error numbers between -100 and -999 are reserved for individual stores */
/* Error numbers less than -1000 are reserved for the profile manager */
/* IMPORTANT: if you add error codes to this, make sure you update convertRes() in
* nsPref.cpp, otherwise the error won't get turned into the right nsresult value
*/
typedef enum {
PREF_DEFAULT_VALUE_NOT_INITIALIZED = -13,
PREF_BAD_PASSWORD = -12,
PREF_CANT_DELETE_NET_PROFILE = -11,
PREF_NETPROFILE_DIR_EXISTS = -10,
PREF_PROFILE_DIR_EXISTS = -9,
PREF_PROFILE_EXISTS = -8,
PREF_BAD_PARAMETER = -7,
PREF_DOES_NOT_EXIST = -6,
PREF_OUT_OF_MEMORY = -5,
PREF_TYPE_CHANGE_ERR = -4,
PREF_NOT_INITIALIZED = -3,
PREF_BAD_LOCKFILE = -2,
PREF_ERROR = -1,
PREF_NOERROR = 0,
PREF_OK = 0, /* same as PREF_NOERROR */
PREF_VALUECHANGED = 1,
PREF_PROFILE_UPGRADE = 2
} PrefResult;
/*
// <font color=blue>
// The Init function sets up of the JavaScript preference context and creates
// the basic preference and pref_array objects:
// It also takes the filename of the preference file to use. This allows the
// module to create its own pref file if that is so desired. If a filename isn't
// passed, the pref module will either use the current name and file (if known) or
// will call (FE_GetPrefFileName() ?) to have a module prompt the user to determine
// which user it is (on a mulit-user system). In general, the FE should pass the
// filename and the sub-modules should pass NULL
//
// At the moment, the Init function is defining 3 JavaScript functions that can be
// called in arbitrary JavaScript (like that passed to the EvaluateJS function below).
// This may increase in the future...
//
// pref(pref_name,default_value) : Setup the initial preference storage and default
// user_pref(pref_name, user_value) : Set a user preference
// lock_pref(pref_name) : Lock a preference (prevent it from being modifyed by user)
// The Init function initializes the preference context and creates
// the preference hashtable.
// </font>
*/
PRBool
PREF_Init(const char *filename);
PrefResult
PREF_LockPref(const char *key);
nsresult PREF_Init();
/*
// Cleanup should be called at program exit to free the
// list of registered callbacks.
*/
void
PREF_Cleanup();
void
PREF_CleanupPrefs();
PRBool
PREF_EvaluateConfigScript(const char * js_buffer, size_t length,
const char* filename, PRBool bGlobalContext, PRBool bCallbacks,
PRBool skipFirstLine);
/*
// This routine is newer than the above which are not being called externally,
// as far as I know. The following is used from mkautocf to evaluate a
// config URL file with callbacks.
// <font color=blue>
// Pass an arbitrary JS buffer to be evaluated in the Preference context.
// On startup modules will want to set up their preferences with reasonable
// defaults. For example netlib might call with a buffer of:
//
// pref("network.proxy.http_host", "");
// pref("network.proxy.http_port". 0);
// ...etc...
//
// This routine generates callbacks to functions registered with
// PREF_RegisterCallback() for any user values that have changed.
// </font>
*/
/*
// <font color=blue>
// Actions that can be passed to various functions to perform an operation on a pref key
// </font>
*/
typedef enum { PREF_SETDEFAULT, PREF_SETUSER,
PREF_LOCK, PREF_SETCONFIG} PrefAction;
void PREF_Cleanup();
void PREF_CleanupPrefs();
/*
// <font color=blue>
@ -186,28 +95,21 @@ typedef enum { PREF_INVALID = 0,
// Note that this will cause the preference to be saved to the file if
// it is different from the default. In other words, these are used
// to set the _user_ preferences.
//
// If set_default is set to PR_TRUE however, it sets the default value.
// This will only affect the program behavior if the user does not have a value
// saved over it for the particular preference. In addition, these will never
// be saved out to disk.
//
// Each set returns PREF_VALUECHANGED if the user value changed
// (triggering a callback), or PREF_NOERROR if the value was unchanged.
// </font>
*/
PrefResult PREF_SetCharPref(const char *pref,const char* value);
PrefResult PREF_SetIntPref(const char *pref,PRInt32 value);
PrefResult PREF_SetBoolPref(const char *pref,PRBool value);
nsresult PREF_SetCharPref(const char *pref,const char* value, PRBool set_default = PR_FALSE);
nsresult PREF_SetIntPref(const char *pref,PRInt32 value, PRBool set_default = PR_FALSE);
nsresult PREF_SetBoolPref(const char *pref,PRBool value, PRBool set_default = PR_FALSE);
/*
// <font color=blue>
// Set the default for various types of preferences. These functions take a dotted
// notation of the preference name (e.g. "browser.startup.homepage")
// This will only affect the program behavior if the user does not have a value
// saved over it for the particular preference. In addition, these will never
// be saved out to disk.
// </font>
*/
PrefResult PREF_SetDefaultCharPref(const char *pref,const char* value);
PrefResult PREF_SetDefaultIntPref(const char *pref,PRInt32 value);
PrefResult PREF_SetDefaultBoolPref(const char *pref,PRBool value);
PRBool PREF_HasUserPref(const char* pref_name);
PRBool PREF_HasUserPref(const char* pref_name);
/*
// <font color=blue>
@ -222,9 +124,9 @@ PRBool PREF_HasUserPref(const char* pref_name);
// (if it is reasonably possible)
// </font>
*/
PrefResult PREF_GetIntPref(const char *pref,
PRInt32 * return_int, PRBool isDefault);
PrefResult PREF_GetBoolPref(const char *pref, PRBool * return_val, PRBool isDefault);
nsresult PREF_GetIntPref(const char *pref,
PRInt32 * return_int, PRBool get_default);
nsresult PREF_GetBoolPref(const char *pref, PRBool * return_val, PRBool get_default);
/*
// <font color=blue>
// These functions are similar to the above "Get" version with the significant
@ -232,7 +134,7 @@ PrefResult PREF_GetBoolPref(const char *pref, PRBool * return_val, PRBool isDefa
// the caller will need to be responsible for freeing it...
// </font>
*/
PrefResult PREF_CopyCharPref(const char *pref, char ** return_buf, PRBool isDefault);
nsresult PREF_CopyCharPref(const char *pref, char ** return_buf, PRBool get_default);
/*
// <font color=blue>
// PRBool function that returns whether or not the preference is locked and therefore
@ -241,29 +143,37 @@ PrefResult PREF_CopyCharPref(const char *pref, char ** return_buf, PRBool isDefa
*/
PRBool PREF_PrefIsLocked(const char *pref_name);
/*
// <font color=blue>
// Function that sets whether or not the preference is locked and therefore
// cannot be changed.
// </font>
*/
nsresult PREF_LockPref(const char *key, PRBool lockIt);
PrefType PREF_GetPrefType(const char *pref_name);
/*
* Delete a branch of the tree
*/
PrefResult PREF_DeleteBranch(const char *branch_name);
nsresult PREF_DeleteBranch(const char *branch_name);
/*
* Clears the given pref (reverts it to its default value)
*/
PrefResult PREF_ClearUserPref(const char *pref_name);
nsresult PREF_ClearUserPref(const char *pref_name);
/*
* Clears all user prefs
*/
PrefResult PREF_ClearAllUserPrefs();
nsresult PREF_ClearAllUserPrefs();
/*
// <font color=blue>
// The callback function will get passed the pref_node which triggered the call
// and the void * instance_data which was passed to the register callback function.
// Return a non-zero result (a PrefResult enumerated value) to pass an error up to the caller.
// Return a non-zero result (nsresult) to pass an error up to the caller.
// </font>
*/
/* Temporarily conditionally compile PrefChangedFunc typedef.
@ -273,7 +183,7 @@ PrefResult PREF_ClearAllUserPrefs();
** compilers were having problems with multiple definitions.
*/
#ifndef have_PrefChangedFunc_typedef
typedef int (*PrefChangedFunc) (const char *, void *);
typedef nsresult (*PrefChangedFunc) (const char *, void *);
#define have_PrefChangedFunc_typedef
#endif
@ -287,8 +197,17 @@ typedef int (*PrefChangedFunc) (const char *, void *);
*/
void PREF_RegisterCallback( const char* domain,
PrefChangedFunc callback, void* instance_data );
PrefResult PREF_UnregisterCallback( const char* domain,
nsresult PREF_UnregisterCallback( const char* domain,
PrefChangedFunc callback, void* instance_data );
/*
* Used by nsPrefService as the callback function of the 'pref' parser
*/
void PREF_ReaderCallback( void *closure,
const char *pref,
PrefValue value,
PrefType type,
PRBool isDefault);
NSPR_END_EXTERN_C
#endif

Просмотреть файл

@ -37,7 +37,6 @@
/* Data shared between prefapi.c and nsPref.cpp */
extern PRBool gErrorOpeningUserPrefs;
extern PLDHashTable gHashTable;
extern PRBool gDirty;
@ -49,7 +48,7 @@ struct pref_saveArgs {
pref_SaveTypes saveTypes;
};
PLDHashOperator PR_CALLBACK pref_savePref(PLDHashTable*, PLDHashEntryHdr *, PRUint32, void *arg);
PLDHashOperator
pref_savePref(PLDHashTable *table, PLDHashEntryHdr *heh, PRUint32 i, void *arg);
int PR_CALLBACK pref_CompareStrings(const void *v1, const void *v2, void* unused);
extern PRBool pref_InitInitialObjects(void);

Просмотреть файл

@ -134,13 +134,9 @@ pref_GrowBuf(PrefParseState *ps)
static PRBool
pref_DoCallback(PrefParseState *ps)
{
PrefType type;
PrefAction action;
PrefValue value;
type = ps->vtype;
action = ps->fuser ? PREF_SETUSER : PREF_SETDEFAULT;
switch (type) {
switch (ps->vtype) {
case PREF_STRING:
value.stringVal = ps->vb;
break;
@ -157,7 +153,7 @@ pref_DoCallback(PrefParseState *ps)
default:
break;
}
(*ps->reader)(ps->closure, ps->lb, value, type, action);
(*ps->reader)(ps->closure, ps->lb, value, ps->vtype, ps->fdefault);
return PR_TRUE;
}
@ -214,7 +210,7 @@ PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen)
ps->lbcur = ps->lb;
ps->vb = NULL;
ps->vtype = PREF_INVALID;
ps->fuser = PR_FALSE;
ps->fdefault = PR_FALSE;
}
switch (c) {
case '/': /* begin comment block or line? */
@ -253,7 +249,7 @@ PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen)
/* name parsing */
case PREF_PARSE_UNTIL_NAME:
if (c == '\"') {
ps->fuser = (ps->smatch == kUserPref);
ps->fdefault = (ps->smatch == kPref);
state = PREF_PARSE_NAME;
}
else if (c == '/') { /* allow embedded comment */
@ -468,7 +464,7 @@ PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen)
/* need to handle mac, unix, or dos line endings.
* PREF_PARSE_INIT will eat the next \n in case
* we have \r\n. */
if (c == '\r' || c == '\n') {
if (c == '\r' || c == '\n' || c == 0x1A) {
state = ps->nextstate;
ps->nextstate = PREF_PARSE_INIT; /* reset next state */
}

Просмотреть файл

@ -55,14 +55,14 @@ NSPR_BEGIN_EXTERN_C
* preference value
* @param type
* preference type (PREF_STRING, PREF_INT, or PREF_BOOL)
* @param action
* preference action (PREF_SETDEFAULT or PREF_SETUSER)
* @param defPref
* preference type (PR_TRUE: default, PR_FALSE: user preference)
*/
typedef void (*PrefReader)(void *closure,
const char *pref,
PrefValue val,
PrefType type,
PrefAction action);
PRBool defPref);
/* structure fields are private */
typedef struct PrefParseState {
@ -77,7 +77,7 @@ typedef struct PrefParseState {
char *lbend; /* line buffer end */
char *vb; /* value buffer (ptr into lb) */
PrefType vtype; /* PREF_STRING,INT,BOOL */
PRBool fuser; /* PR_TRUE if user_pref */
PRBool fdefault; /* PR_TRUE if (default) pref */
} PrefParseState;
/**