From 1037f46c0db0c5b53b0760deae8d8565a762e022 Mon Sep 17 00:00:00 2001 From: "bnesse%netscape.com" Date: Tue, 3 Apr 2001 20:11:54 +0000 Subject: [PATCH] Updates to the future preferences API based on the latest round of feedback. [Not Part of the Build] --- modules/libpref/public/nsIPrefBranch.idl | 1 - .../libpref/public/nsIPrefBranchInternal.idl | 4 +- .../libpref/public/nsIPrefLocalizedString.idl | 12 +- modules/libpref/src/nsPrefBranch.cpp | 28 ++++- modules/libpref/src/nsPrefBranch.h | 9 +- modules/libpref/src/nsPrefService.cpp | 103 ++++++------------ modules/libpref/src/nsPrefService.h | 12 +- modules/libpref/src/nsPrefsFactory.cpp | 10 ++ 8 files changed, 97 insertions(+), 82 deletions(-) diff --git a/modules/libpref/public/nsIPrefBranch.idl b/modules/libpref/public/nsIPrefBranch.idl index 84867e16d096..72a4f544f50e 100644 --- a/modules/libpref/public/nsIPrefBranch.idl +++ b/modules/libpref/public/nsIPrefBranch.idl @@ -23,7 +23,6 @@ */ #include "nsISupports.idl" -#include "nsIObserver.idl" [scriptable, uuid(56c35506-f14b-11d3-99d3-ddbfac2ccf65)] interface nsIPrefBranch : nsISupports diff --git a/modules/libpref/public/nsIPrefBranchInternal.idl b/modules/libpref/public/nsIPrefBranchInternal.idl index 68baef24c5fc..9ba1b917d80c 100644 --- a/modules/libpref/public/nsIPrefBranchInternal.idl +++ b/modules/libpref/public/nsIPrefBranchInternal.idl @@ -22,11 +22,11 @@ * Brian Nesse */ -#include "nsIPrefBranch.idl" +#include "nsISupports.idl" #include "nsIObserver.idl" [scriptable, uuid(24ef9578-1dd2-11b2-b439-fcc3d2b4fa20)] -interface nsIPrefBranchInternal : nsIPrefBranch +interface nsIPrefBranchInternal : nsISupports { /* * Listeners diff --git a/modules/libpref/public/nsIPrefLocalizedString.idl b/modules/libpref/public/nsIPrefLocalizedString.idl index 36917a285b6a..9c0df7647597 100644 --- a/modules/libpref/public/nsIPrefLocalizedString.idl +++ b/modules/libpref/public/nsIPrefLocalizedString.idl @@ -43,7 +43,15 @@ interface nsIPrefLocalizedString : nsISupports %{C++ -#define NS_SUPPORTS_PREFLOCALIZEDSTRING_CONTRACTID "@mozilla.org/supports-preflocalizedstring;1" -#define NS_SUPPORTS_PREFLOCALIZEDSTRING_CLASSNAME "Supports PrefLocalizedString" +#define NS_PREFLOCALIZEDSTRING_CID \ + { /* {064d9cee-1dd2-11b2-83e3-d25ab0193c26} */ \ + 0x064d9cee, \ + 0x1dd2, \ + 0x11b2, \ + { 0x83, 0xe3, 0xd2, 0x5a, 0xb0, 0x19, 0x3c, 0x26 } \ + } + +#define NS_PREFLOCALIZEDSTRING_CONTRACTID "@mozilla.org/pref-localizedstring;1" +#define NS_PREFLOCALIZEDSTRING_CLASSNAME "Pref LocalizedString" %} diff --git a/modules/libpref/src/nsPrefBranch.cpp b/modules/libpref/src/nsPrefBranch.cpp index d0cc30e9acdc..241d53d3a80e 100644 --- a/modules/libpref/src/nsPrefBranch.cpp +++ b/modules/libpref/src/nsPrefBranch.cpp @@ -286,7 +286,7 @@ NS_IMETHODIMP nsPrefBranch::GetComplexValue(const char *aPrefName, const nsIID & } if (aType.Equals(NS_GET_IID(nsIPrefLocalizedString))) { - nsCOMPtr theString(do_CreateInstance(NS_SUPPORTS_PREFLOCALIZEDSTRING_CONTRACTID, &rv)); + nsCOMPtr theString(do_CreateInstance(NS_PREFLOCALIZEDSTRING_CONTRACTID, &rv)); if (NS_SUCCEEDED(rv)) { rv = theString->SetData(NS_ConvertASCIItoUCS2(utf8String).get()); @@ -626,9 +626,8 @@ nsresult nsPrefBranch::QueryObserver(const char *aPrefName) #pragma mark - -NS_IMPL_ISUPPORTS1(nsPrefLocalizedString, nsIPrefLocalizedString) - nsPrefLocalizedString::nsPrefLocalizedString() +: mUnicodeString(nsnull) { nsresult rv; @@ -640,3 +639,26 @@ nsPrefLocalizedString::nsPrefLocalizedString() nsPrefLocalizedString::~nsPrefLocalizedString() { } + + +/* + * nsISupports Implementation + */ + +NS_IMPL_THREADSAFE_ADDREF(nsPrefLocalizedString) +NS_IMPL_THREADSAFE_RELEASE(nsPrefLocalizedString) + +NS_INTERFACE_MAP_BEGIN(nsPrefLocalizedString) + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrefLocalizedString) + NS_INTERFACE_MAP_ENTRY(nsIPrefLocalizedString) + NS_INTERFACE_MAP_ENTRY(nsISupportsWString) +NS_INTERFACE_MAP_END + +nsresult nsPrefLocalizedString::Init() +{ + nsresult rv; + mUnicodeString = do_CreateInstance(NS_SUPPORTS_WSTRING_CONTRACTID, &rv); + + return rv; +} + diff --git a/modules/libpref/src/nsPrefBranch.h b/modules/libpref/src/nsPrefBranch.h index c6af6d237ade..0dcb5fb6d39b 100644 --- a/modules/libpref/src/nsPrefBranch.h +++ b/modules/libpref/src/nsPrefBranch.h @@ -31,7 +31,8 @@ #include "nsString.h" #include "nsVoidArray.h" -class nsPrefBranch : public nsIPrefBranchInternal +class nsPrefBranch : public nsIPrefBranch, + public nsIPrefBranchInternal { public: NS_DECL_ISUPPORTS @@ -58,7 +59,8 @@ private: }; -class nsPrefLocalizedString : public nsIPrefLocalizedString +class nsPrefLocalizedString : public nsIPrefLocalizedString, + public nsISupportsWString { public: nsPrefLocalizedString(); @@ -66,6 +68,9 @@ public: NS_DECL_ISUPPORTS NS_FORWARD_NSISUPPORTSWSTRING(mUnicodeString->) + NS_FORWARD_NSISUPPORTSPRIMITIVE(mUnicodeString->) + + nsresult Init(); private: nsCOMPtr mUnicodeString; diff --git a/modules/libpref/src/nsPrefService.cpp b/modules/libpref/src/nsPrefService.cpp index 868925f7d05b..3e53d0736198 100644 --- a/modules/libpref/src/nsPrefService.cpp +++ b/modules/libpref/src/nsPrefService.cpp @@ -36,7 +36,6 @@ class nsIFileSpec; // needed for prefapi_private_data.h inclusion // supporting lock files #include "nsDirectoryServiceDefs.h" -#include "plstr.h" #include "prmem.h" #include "prprf.h" @@ -44,7 +43,7 @@ class nsIFileSpec; // needed for prefapi_private_data.h inclusion #include "nsIJSRuntimeService.h" #include "nsAppDirectoryServiceDefs.h" -// lose these if possible (supporting _nsIFileToFileSpec) +// lose these if possible (supporting nsIFileToFileSpec) #include "nsIFileSpec.h" #include "nsFileStream.h" @@ -54,7 +53,7 @@ class nsIFileSpec; // needed for prefapi_private_data.h inclusion #define INITIAL_MAX_DEFAULT_PREF_FILES 10 // Prototypes -static nsresult _nsIFileToFileSpec(nsIFile* inFile, nsIFileSpec **aFileSpec); +static nsresult nsIFileToFileSpec(nsIFile* inFile, nsIFileSpec **aFileSpec); static PRBool verifyFileHash(char* buf, long buflen); static nsresult openPrefFile(nsIFile* aFile, PRBool aIsErrorFatal, PRBool aVerifyHash, PRBool aIsGlobalContext, PRBool aSkipFirstLine); @@ -83,16 +82,6 @@ nsPrefService::nsPrefService() nsPrefService::~nsPrefService() { PREF_Cleanup(); - - if (gLockInfoRead) { - if (gLockFileName) { - PR_DELETE(gLockFileName); - } - if (gLockVendor) { - PR_DELETE(gLockVendor); - } - gLockInfoRead = PR_FALSE; - } } @@ -108,6 +97,7 @@ NS_INTERFACE_MAP_BEGIN(nsPrefService) NS_INTERFACE_MAP_ENTRY(nsIPrefService) NS_INTERFACE_MAP_ENTRY(nsIObserver) NS_INTERFACE_MAP_ENTRY(nsIPrefBranch) + NS_INTERFACE_MAP_ENTRY(nsIPrefBranchInternal) NS_INTERFACE_MAP_END @@ -115,7 +105,7 @@ NS_INTERFACE_MAP_END * nsIPrefService Implementation */ -NS_IMETHODIMP nsPrefService::Init() +nsresult nsPrefService::Init() { nsresult rv = NS_OK; @@ -158,56 +148,7 @@ NS_IMETHODIMP nsPrefService::Observe(nsISupports *aSubject, const PRUnichar *aTo NS_IMETHODIMP nsPrefService::ReadConfigFile() { - nsresult rv = NS_OK; - PRUint32 fileNameLen; - PRUint32 vendorLen; - - gLockFileName = nsnull; - gLockVendor = nsnull; - - if (gLockInfoRead) - return rv; - - gLockInfoRead = PR_TRUE; - - rv = mRootBranch->GetCharPref("general.config.filename", &gLockFileName); - if (NS_SUCCEEDED(rv) && (gLockFileName)) { -#ifdef NS_DEBUG - printf("\ngLockFileName %s \n", gLockFileName); -#endif - rv = mRootBranch->GetCharPref("general.config.vendor", &gLockVendor); - if (NS_SUCCEEDED(rv) && (gLockVendor)) { - // if we got here, we have a lockfile pref and a vendor pref -#ifdef NS_DEBUG - printf("\ngLockVendor %s \n", gLockVendor); -#endif - fileNameLen = PL_strlen(gLockFileName); - vendorLen = PL_strlen(gLockVendor); - if (PL_strncmp(gLockFileName, gLockVendor, fileNameLen -4) != 0) { - // configuration filename and vendor name do not match - rv = NS_ERROR_FAILURE; - } else { - nsCOMPtr lockFile; - - rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR, getter_AddRefs(lockFile)); - if (NS_SUCCEEDED(rv)) { -#ifdef XP_MAC - rv = lockFile->Append("Essential Files"); -#endif - rv = lockFile->Append(gLockFileName); - rv = openPrefFile(lockFile, PR_TRUE, PR_TRUE, PR_FALSE, PR_FALSE); - } - } - } else { - // No vendor name specified, hence cannot verify it - rv = NS_ERROR_FAILURE; - } - } else { - // config file is not specified, hence no need to read it. - rv = NS_OK; - } - - return rv; + return NS_ERROR_NOT_IMPLEMENTED; } NS_IMETHODIMP nsPrefService::ReadUserPrefs(nsIFile *aFile) @@ -283,6 +224,30 @@ NS_IMETHODIMP nsPrefService::GetDefaultBranch(const char *aPrefRoot, nsIPrefBran } +#pragma mark - +// Forward these methods through the nsIPrefBranchInternal headers + +NS_IMETHODIMP nsPrefService::AddObserver(const char *aDomain, nsIObserver *aObserver) +{ + nsresult rv; + + nsCOMPtr prefBranch = do_QueryInterface(mRootBranch, &rv); + if (NS_SUCCEEDED(rv)) + rv = prefBranch->AddObserver(aDomain, aObserver); + return rv; +} + +NS_IMETHODIMP nsPrefService::RemoveObserver(const char *aDomain, nsIObserver *aObserver) +{ + nsresult rv; + + nsCOMPtr prefBranch = do_QueryInterface(mRootBranch, &rv); + if (NS_SUCCEEDED(rv)) + rv = prefBranch->RemoveObserver(aDomain, aObserver); + return rv; +} + + #pragma mark - nsresult nsPrefService::useDefaultPrefFile() @@ -336,7 +301,9 @@ static nsresult openPrefFile(nsIFile* aFile, PRBool aIsErrorFatal, PRBool aVerif nsCOMPtr fileSpec; nsresult rv; - rv = _nsIFileToFileSpec(aFile, getter_AddRefs(fileSpec)); + JS_BeginRequest(gMochaContext); + rv = nsIFileToFileSpec(aFile, getter_AddRefs(fileSpec)); + JS_EndRequest(gMochaContext); if (NS_FAILED(rv)) return rv; @@ -409,7 +376,7 @@ static nsresult savePrefFile(nsIFile* aFile) return NS_OK; // TODO: Convert the rest of this code to nsIFile and avoid this conversion to nsIFileSpec - rv = _nsIFileToFileSpec(aFile, getter_AddRefs(fileSpec)); + rv = nsIFileToFileSpec(aFile, getter_AddRefs(fileSpec)); if (NS_FAILED(rv)) return rv; @@ -446,7 +413,7 @@ static nsresult savePrefFile(nsIFile* aFile) // So discouraged is the use of nsIFileSpec, nobody wanted to have this routine be // public - It might lead to continued use of nsIFileSpec. Right now, this code has // such a need for it, here it is. Let's stop having to use it though. -static nsresult _nsIFileToFileSpec(nsIFile* inFile, nsIFileSpec **aFileSpec) +static nsresult nsIFileToFileSpec(nsIFile* inFile, nsIFileSpec **aFileSpec) //---------------------------------------------------------------------------------------- { nsresult rv; @@ -579,7 +546,7 @@ extern "C" JSBool pref_InitInitialObjects() rv = NS_GetSpecialDirectory(NS_APP_PREF_DEFAULTS_50_DIR, getter_AddRefs(aFile)); if (NS_FAILED(rv)) return JS_FALSE; - rv = _nsIFileToFileSpec(aFile, getter_AddRefs(defaultPrefDir)); + rv = nsIFileToFileSpec(aFile, getter_AddRefs(defaultPrefDir)); if (NS_FAILED(rv)) return JS_FALSE; diff --git a/modules/libpref/src/nsPrefService.h b/modules/libpref/src/nsPrefService.h index 66fa8157e38e..5364d799521a 100644 --- a/modules/libpref/src/nsPrefService.h +++ b/modules/libpref/src/nsPrefService.h @@ -22,6 +22,8 @@ */ #include "nsIPrefService.h" +#include "nsIPrefBranch.h" +#include "nsIPrefBranchInternal.h" #include "nsIObserver.h" #include "nsCOMPtr.h" @@ -29,22 +31,24 @@ class nsIFile; class nsPrefService : public nsIPrefService, public nsIObserver, - public nsIPrefBranch + public nsIPrefBranch, + public nsIPrefBranchInternal { public: NS_DECL_ISUPPORTS NS_DECL_NSIPREFSERVICE NS_FORWARD_NSIPREFBRANCH(mRootBranch->) + NS_DECL_NSIPREFBRANCHINTERNAL NS_DECL_NSIOBSERVER nsPrefService(); virtual ~nsPrefService(); - NS_METHOD Init(); + nsresult Init(); protected: - NS_METHOD useDefaultPrefFile(); - NS_METHOD useUserPrefFile(); + nsresult useDefaultPrefFile(); + nsresult useUserPrefFile(); private: nsCOMPtr mRootBranch; diff --git a/modules/libpref/src/nsPrefsFactory.cpp b/modules/libpref/src/nsPrefsFactory.cpp index 49cc5de37d46..4f98fc3cebe4 100644 --- a/modules/libpref/src/nsPrefsFactory.cpp +++ b/modules/libpref/src/nsPrefsFactory.cpp @@ -23,6 +23,7 @@ #include "nsIGenericFactory.h" #include "nsPrefService.h" +#include "nsPrefBranch.h" #include "nsIPref.h" // remove this when nsPref goes away @@ -30,6 +31,7 @@ extern "C" nsresult nsPrefConstructor(nsISupports *aOuter, REFNSIID aIID, void * NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrefService, Init) +NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrefLocalizedString, Init) // The list of components we register static nsModuleComponentInfo components[] = @@ -41,6 +43,13 @@ static nsModuleComponentInfo components[] = nsPrefServiceConstructor }, + { + NS_PREFLOCALIZEDSTRING_CLASSNAME, + NS_PREFLOCALIZEDSTRING_CID, + NS_PREFLOCALIZEDSTRING_CONTRACTID, + nsPrefLocalizedStringConstructor + }, + { // remove this when nsPref goes away NS_PREF_CLASSNAME, NS_PREF_CID, @@ -50,3 +59,4 @@ static nsModuleComponentInfo components[] = }; NS_IMPL_NSGETMODULE("nsPrefModule", components); +