From cd894417d77de0b1337ecf577ed37c2e732bf810 Mon Sep 17 00:00:00 2001 From: "shaver%mozilla.org" Date: Tue, 17 Oct 2000 17:08:00 +0000 Subject: [PATCH] #24612, make prefs observable from JS and other XPCOM bindings, r/sr=alecf git-svn-id: svn://10.0.0.236/trunk@81296 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/modules/libpref/public/nsIPref.idl | 35 ++++++++++------- mozilla/modules/libpref/src/nsPref.cpp | 44 ++++++++++++++++++++++ 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/mozilla/modules/libpref/public/nsIPref.idl b/mozilla/modules/libpref/public/nsIPref.idl index 76a35f00b01..e19ef390c95 100644 --- a/mozilla/modules/libpref/public/nsIPref.idl +++ b/mozilla/modules/libpref/public/nsIPref.idl @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file @@ -22,6 +22,7 @@ #include "nsISupports.idl" #include "nsILocalFile.idl" +#include "nsIObserver.idl" %{C++ #include "jsapi.h" @@ -76,15 +77,12 @@ interface nsIPref : nsISupports { void ReadUserJSFile(in nsIFileSpec filename); void SavePrefFileAs(in nsIFileSpec filename); - // Commenting out: #ifdef MOZ_OLD_LI_STUFF - // void ReadLIJSFile(in nsIFileSpec filename); - // void SaveLIPrefFile(in nsIFileSpec filename); - /* Getters */ long GetPrefType(in string pref); long GetIntPref(in string pref); boolean GetBoolPref(in string pref); - [noscript] void GetBinaryPref(in string pref, in voidPtr buf, inout long buf_length); + [noscript] void GetBinaryPref(in string pref, in voidPtr buf, + inout long buf_length); unsigned long GetColorPrefDWord(in string pref); /* set preferences */ @@ -92,7 +90,8 @@ interface nsIPref : nsISupports { void SetUnicharPref(in string pref, in wstring value); void SetIntPref(in string pref, in long value); void SetBoolPref(in string pref, in boolean value); - [noscript] void SetBinaryPref(in string pref, in voidPtr value, in unsigned long size); + [noscript] void SetBinaryPref(in string pref, in voidPtr value, + in unsigned long size); void ClearUserPref(in string pref_name); @@ -108,16 +107,17 @@ interface nsIPref : nsISupports { void SetDefaultUnicharPref(in string pref, in wstring value); void SetDefaultIntPref(in string pref, in long value); void SetDefaultBoolPref(in string pref, in boolean value); - [noscript] void SetDefaultBinaryPref(in string pref, in voidPtr value, in unsigned long size); + [noscript] void SetDefaultBinaryPref(in string pref, in voidPtr value, + in unsigned long size); - // these are eventually changing from Copy->get + // these are eventually changing from Copy->get string CopyCharPref(in string pref); wstring CopyUnicharPref(in string pref); [noscript] voidPtr CopyBinaryPref(in string pref, out long size); - // "localized" prefs - stored in the properties files - wstring getLocalizedUnicharPref(in string pref); - wstring getDefaultLocalizedUnicharPref(in string pref); + // "localized" prefs - stored in the properties files + wstring getLocalizedUnicharPref(in string pref); + wstring getDefaultLocalizedUnicharPref(in string pref); string CopyDefaultCharPref(in string pref); wstring CopyDefaultUnicharPref(in string pref); @@ -125,8 +125,8 @@ interface nsIPref : nsISupports { nsIFileSpec GetFilePref(in string pref); void SetFilePref(in string pref, in nsIFileSpec value, in boolean setDefault); - nsILocalFile getFileXPref(in string pref); - void setFileXPref(in string pref, in nsILocalFile value); + nsILocalFile getFileXPref(in string pref); + void setFileXPref(in string pref, in nsILocalFile value); /* pref attributes */ boolean PrefIsLocked(in string pref); @@ -141,6 +141,13 @@ interface nsIPref : nsISupports { [noscript] void UnregisterCallback(in string domain, in PrefChangedFunc callback, in voidPtr closure); + /* + * The observers have their |Observe| methods called with + * ([the observer], "nsPref:changed", [pref name]). + */ + void addObserver(in string domain, in nsIObserver observer); + void removeObserver(in string domain, in nsIObserver observer); + /* void CopyPrefsTree(in string srcRoot, in string destRoot); */ diff --git a/mozilla/modules/libpref/src/nsPref.cpp b/mozilla/modules/libpref/src/nsPref.cpp index 9dc5fccb9c1..da107d60e81 100644 --- a/mozilla/modules/libpref/src/nsPref.cpp +++ b/mozilla/modules/libpref/src/nsPref.cpp @@ -138,6 +138,8 @@ protected: static nsresult convertUTF8ToUnicode(const char *utf8String, PRUnichar **aResult); + /* used to ref-count the pref observers */ + nsSupportsHashtable mObservers; }; // class nsPref nsPref* nsPref::gInstance = NULL; @@ -360,7 +362,9 @@ nsresult nsPref::useLockPrefFile() if (NS_SUCCEEDED(rv = GetLocalizedUnicharPref("browser.startup.homepage", getter_Copies(prefVal)) && (prefVal))) { +#ifdef DEBUG_tao printf("\nStartup homepage %s \n", (const char *)NS_ConvertUCS2toUTF8(prefVal)); +#endif } if (NS_SUCCEEDED(rv = CopyCharPref("general.config.filename", @@ -484,7 +488,9 @@ nsresult nsPref::useLockPrefFile() } } GetLocalizedUnicharPref("browser.startup.homepage",getter_Copies(prefVal)); +#ifdef DEBUG_tao printf("\nStartup homepage %s \n", (const char *)NS_ConvertUCS2toUTF8(prefVal)); +#endif } return rv; } // nsPref::useLockPrefFile @@ -591,12 +597,32 @@ NS_IMETHODIMP nsPref::ResetPrefs() return rv; } // nsPref::ResetPrefs +static int PR_CALLBACK +NotifyObserver(const char *newpref, void *data) +{ + nsCOMPtr observer = NS_STATIC_CAST(nsIObserver *, data); + observer->Observe(observer, NS_LITERAL_STRING("nsPref:changed"), + NS_ConvertASCIItoUCS2(newpref)); + return 0; +} + +static PRBool PR_CALLBACK +UnregisterObservers(nsHashKey *aKey, void *aData, void *closure) +{ + nsCStringKey *stringKey = NS_REINTERPRET_CAST(nsCStringKey *, aKey); + nsCOMPtr obs = do_QueryInterface((nsISupports *)aData); + PREF_UnregisterCallback(stringKey->GetString(), NotifyObserver, obs); + return PR_TRUE; +} //---------------------------------------------------------------------------------------- NS_IMETHODIMP nsPref::ShutDown() //---------------------------------------------------------------------------------------- { + mObservers.Enumerate(UnregisterObservers, nsnull); +#ifdef DEBUG_alecf printf("PREF_Cleanup()\n"); +#endif PREF_Cleanup(); return NS_OK; } // nsPref::ShutDown @@ -1188,6 +1214,22 @@ NS_IMETHODIMP nsPref::UnregisterCallback( const char* domain, return _convertRes(PREF_UnregisterCallback(domain, callback, instance_data)); } +NS_IMETHODIMP nsPref::AddObserver(const char *domain, + nsIObserver *observer) +{ + nsCStringKey key(domain); + mObservers.Put(&key, observer); + return RegisterCallback(domain, NotifyObserver, observer); +} + +NS_IMETHODIMP nsPref::RemoveObserver(const char *domain, + nsIObserver *observer) +{ + nsCStringKey key(domain); + mObservers.Remove(&key); + return UnregisterCallback(domain, NotifyObserver, observer); +} + /* * Tree editing */ @@ -1463,11 +1505,13 @@ PRBool pref_VerifyLockFileSpec(char* buf, long buflen) (int)digest[8],(int)digest[9],(int)digest[10],(int)digest[11], (int)digest[12],(int)digest[13],(int)digest[14],(int)digest[15]); +#ifdef DEBUG_neeti printf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", (int)digest[0],(int)digest[1],(int)digest[2],(int)digest[3], (int)digest[4],(int)digest[5],(int)digest[6],(int)digest[7], (int)digest[8],(int)digest[9],(int)digest[10],(int)digest[11], (int)digest[12],(int)digest[13],(int)digest[14],(int)digest[15]); +#endif success = ( PL_strncmp((const char*) buf + 3, szHash, (PRUint32)(hash_length - 4)) == 0 );