From ac4e7cb6a8e809a121b05cb2e53c1a5285dcd908 Mon Sep 17 00:00:00 2001 From: Makoto Kato Date: Sat, 4 Apr 2009 18:43:42 +0900 Subject: [PATCH] Bug 184657 - GFX should use the newer nsIPrefService APIs instead of nsIPref. r=roc. sr=vladimir --- gfx/src/thebes/nsThebesDeviceContext.cpp | 6 +-- gfx/thebes/public/gfxWindowsPlatform.h | 2 + gfx/thebes/src/gfxFont.cpp | 17 ++++---- gfx/thebes/src/gfxPlatform.cpp | 43 +++++++++------------ gfx/thebes/src/gfxQuartzFontCache.h | 3 +- gfx/thebes/src/gfxQuartzFontCache.mm | 49 ++++++++++++++++-------- gfx/thebes/src/gfxWindowsPlatform.cpp | 40 +++++++++++++------ 7 files changed, 96 insertions(+), 64 deletions(-) diff --git a/gfx/src/thebes/nsThebesDeviceContext.cpp b/gfx/src/thebes/nsThebesDeviceContext.cpp index f2b7eec31a3..853630c3af3 100644 --- a/gfx/src/thebes/nsThebesDeviceContext.cpp +++ b/gfx/src/thebes/nsThebesDeviceContext.cpp @@ -38,7 +38,7 @@ * ***** END LICENSE BLOCK ***** */ #include "nsIServiceManager.h" -#include "nsIPref.h" +#include "nsIPrefService.h" #include "nsCRT.h" #include "nsThebesDeviceContext.h" @@ -178,8 +178,8 @@ nsThebesDeviceContext::SetDPI() // If it's positive, we use it as the logical resolution nsresult rv; PRInt32 prefDPI; - nsCOMPtr prefs(do_GetService(NS_PREF_CONTRACTID, &rv)); - if (NS_SUCCEEDED(rv) && prefs) { + nsCOMPtr prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); + if (prefs) { rv = prefs->GetIntPref("layout.css.dpi", &prefDPI); if (NS_FAILED(rv)) { prefDPI = -1; diff --git a/gfx/thebes/public/gfxWindowsPlatform.h b/gfx/thebes/public/gfxWindowsPlatform.h index 11503cebe3e..1261effefd4 100644 --- a/gfx/thebes/public/gfxWindowsPlatform.h +++ b/gfx/thebes/public/gfxWindowsPlatform.h @@ -122,6 +122,8 @@ public: PRBool GetPrefFontEntries(const nsCString& aLangGroup, nsTArray > *array); void SetPrefFontEntries(const nsCString& aLangGroup, nsTArray >& array); + void ClearPrefFonts() { mPrefFonts.Clear(); } + typedef nsDataHashtable > FontTable; #ifdef MOZ_FT2_FONTS diff --git a/gfx/thebes/src/gfxFont.cpp b/gfx/thebes/src/gfxFont.cpp index 14924ee239d..7d4c24eaab6 100644 --- a/gfx/thebes/src/gfxFont.cpp +++ b/gfx/thebes/src/gfxFont.cpp @@ -37,7 +37,7 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsIPref.h" +#include "nsIPrefService.h" #include "nsServiceManagerUtils.h" #include "nsReadableUtils.h" #include "nsExpirationTracker.h" @@ -937,8 +937,7 @@ gfxFontGroup::ForEachFontInternal(const nsAString& aFamilies, const PRUnichar kDoubleQuote = PRUnichar('\"'); const PRUnichar kComma = PRUnichar(','); - nsCOMPtr prefs; - prefs = do_GetService(NS_PREF_CONTRACTID); + nsCOMPtr prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); nsPromiseFlatString families(aFamilies); const PRUnichar *p, *p_end; @@ -947,6 +946,7 @@ gfxFontGroup::ForEachFontInternal(const nsAString& aFamilies, nsAutoString family; nsCAutoString lcFamily; nsAutoString genericFamily; + nsXPIDLCString value; nsCAutoString lang(aLangGroup); if (lang.IsEmpty()) lang.Assign("x-unicode"); // XXX or should use "x-user-def"? @@ -1003,11 +1003,10 @@ gfxFontGroup::ForEachFontInternal(const nsAString& aFamilies, // prefs file always uses (must use) UTF-8 so that we can use // |GetCharPref| and treat the result as a UTF-8 string. - nsXPIDLString value; - nsresult rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(value)); + nsresult rv = prefs->GetCharPref(prefName.get(), getter_Copies(value)); if (NS_SUCCEEDED(rv)) { CopyASCIItoUTF16(lcFamily, genericFamily); - family = value; + CopyUTF8toUTF16(value, family); } } else { generic = PR_FALSE; @@ -1047,10 +1046,10 @@ gfxFontGroup::ForEachFontInternal(const nsAString& aFamilies, prefName.Append(lcFamily); prefName.AppendLiteral("."); prefName.Append(aLangGroup); - nsXPIDLString value; - nsresult rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(value)); + nsresult rv = prefs->GetCharPref(prefName.get(), getter_Copies(value)); if (NS_SUCCEEDED(rv)) { - ForEachFontInternal(value, lang, PR_FALSE, aResolveFontName, + ForEachFontInternal(NS_ConvertUTF8toUTF16(value), + lang, PR_FALSE, aResolveFontName, fc, closure); } } diff --git a/gfx/thebes/src/gfxPlatform.cpp b/gfx/thebes/src/gfxPlatform.cpp index 7c3d6638f1e..7c52f53f056 100644 --- a/gfx/thebes/src/gfxPlatform.cpp +++ b/gfx/thebes/src/gfxPlatform.cpp @@ -58,7 +58,6 @@ #include "gfxTextRunWordCache.h" #include "gfxUserFontSet.h" -#include "nsIPref.h" #include "nsServiceManagerUtils.h" #include "nsTArray.h" @@ -344,45 +343,43 @@ AppendGenericFontFromPref(nsString& aFonts, const char *aLangGroup, const char * { nsresult rv; - nsCOMPtr prefs(do_GetService(NS_PREF_CONTRACTID)); + nsCOMPtr prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); if (!prefs) return; nsCAutoString prefName; - nsXPIDLString nameValue, nameListValue; + nsXPIDLCString nameValue, nameListValue; - nsXPIDLString genericName; + nsCAutoString genericDotLang; if (aGenericName) { - genericName = NS_ConvertASCIItoUTF16(aGenericName); + genericDotLang.Assign(aGenericName); } else { prefName.AssignLiteral("font.default."); prefName.Append(aLangGroup); - prefs->CopyUnicharPref(prefName.get(), getter_Copies(genericName)); + prefs->GetCharPref(prefName.get(), getter_Copies(genericDotLang)); } - nsCAutoString genericDotLang; - genericDotLang.Assign(NS_ConvertUTF16toUTF8(genericName)); genericDotLang.AppendLiteral("."); genericDotLang.Append(aLangGroup); // fetch font.name.xxx value prefName.AssignLiteral("font.name."); prefName.Append(genericDotLang); - rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(nameValue)); + rv = prefs->GetCharPref(prefName.get(), getter_Copies(nameValue)); if (NS_SUCCEEDED(rv)) { if (!aFonts.IsEmpty()) aFonts.AppendLiteral(", "); - aFonts.Append(nameValue); + aFonts.Append(NS_ConvertUTF8toUTF16(nameValue)); } // fetch font.name-list.xxx value prefName.AssignLiteral("font.name-list."); prefName.Append(genericDotLang); - rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(nameListValue)); + rv = prefs->GetCharPref(prefName.get(), getter_Copies(nameListValue)); if (NS_SUCCEEDED(rv) && !nameListValue.Equals(nameValue)) { if (!aFonts.IsEmpty()) aFonts.AppendLiteral(", "); - aFonts.Append(nameListValue); + aFonts.Append(NS_ConvertUTF8toUTF16(nameListValue)); } } @@ -401,7 +398,7 @@ PRBool gfxPlatform::ForEachPrefFont(eFontPrefLang aLangArray[], PRUint32 aLangAr { nsresult rv; - nsCOMPtr prefs(do_GetService(NS_PREF_CONTRACTID)); + nsCOMPtr prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); if (!prefs) return PR_FALSE; @@ -412,33 +409,31 @@ PRBool gfxPlatform::ForEachPrefFont(eFontPrefLang aLangArray[], PRUint32 aLangAr const char *langGroup = GetPrefLangName(prefLang); nsCAutoString prefName; - nsXPIDLString nameValue, nameListValue; - - nsXPIDLString genericName; - prefName.AssignLiteral("font.default."); - prefName.Append(langGroup); - prefs->CopyUnicharPref(prefName.get(), getter_Copies(genericName)); + nsXPIDLCString nameValue, nameListValue; nsCAutoString genericDotLang; - genericDotLang.Assign(NS_ConvertUTF16toUTF8(genericName)); + prefName.AssignLiteral("font.default."); + prefName.Append(langGroup); + prefs->GetCharPref(prefName.get(), getter_Copies(genericDotLang)); + genericDotLang.AppendLiteral("."); genericDotLang.Append(langGroup); // fetch font.name.xxx value prefName.AssignLiteral("font.name."); prefName.Append(genericDotLang); - rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(nameValue)); + rv = prefs->GetCharPref(prefName.get(), getter_Copies(nameValue)); if (NS_SUCCEEDED(rv)) { - if (!aCallback(prefLang, nameValue, aClosure)) + if (!aCallback(prefLang, NS_ConvertUTF8toUTF16(nameValue), aClosure)) return PR_FALSE; } // fetch font.name-list.xxx value prefName.AssignLiteral("font.name-list."); prefName.Append(genericDotLang); - rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(nameListValue)); + rv = prefs->GetCharPref(prefName.get(), getter_Copies(nameListValue)); if (NS_SUCCEEDED(rv) && !nameListValue.Equals(nameValue)) { - if (!aCallback(prefLang, nameListValue, aClosure)) + if (!aCallback(prefLang, NS_ConvertUTF8toUTF16(nameListValue), aClosure)) return PR_FALSE; } } diff --git a/gfx/thebes/src/gfxQuartzFontCache.h b/gfx/thebes/src/gfxQuartzFontCache.h index 749e2aa234c..17e5a08216f 100644 --- a/gfx/thebes/src/gfxQuartzFontCache.h +++ b/gfx/thebes/src/gfxQuartzFontCache.h @@ -213,6 +213,8 @@ public: PRBool GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName); void UpdateFontList() { InitFontList(); } + void ClearPrefFonts() { mPrefFonts.Clear(); } + void GetFontFamilyList(nsTArray >& aFamilyArray); MacOSFontEntry* FindFontForChar(const PRUint32 aCh, gfxFont *aPrevFont); @@ -272,7 +274,6 @@ private: void GenerateFontListKey(const nsAString& aKeyName, nsAString& aResult); static void ATSNotification(ATSFontNotificationInfoRef aInfo, void* aUserArg); - static int PrefChangedCallback(const char *aPrefName, void *closure); static PLDHashOperator HashEnumFuncForFamilies(nsStringHashKey::KeyType aKey, diff --git a/gfx/thebes/src/gfxQuartzFontCache.mm b/gfx/thebes/src/gfxQuartzFontCache.mm index 8287c6dd8be..b410134600c 100644 --- a/gfx/thebes/src/gfxQuartzFontCache.mm +++ b/gfx/thebes/src/gfxQuartzFontCache.mm @@ -47,7 +47,8 @@ #include "gfxAtsuiFonts.h" #include "gfxUserFontSet.h" -#include "nsIPref.h" // for pref changes callback notification +#include "nsIPrefService.h" +#include "nsIPrefBranch2.h" // for pref changes callback notification #include "nsServiceManagerUtils.h" #include "nsTArray.h" @@ -106,6 +107,26 @@ static NSString* GetNSStringForString(const nsAString& aSrc) static PRLogModuleInfo *gFontInfoLog = PR_NewLogModule("fontInfoLog"); +class gfxQuartzFontCachePrefObserver : public nsIObserver { +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIOBSERVER +}; + +NS_IMPL_ISUPPORTS1(gfxQuartzFontCachePrefObserver, nsIObserver) + +NS_IMETHODIMP +gfxQuartzFontCachePrefObserver::Observe(nsISupports *aSubject, + const char *aTopic, + const PRUnichar *aData) +{ + NS_ASSERTION(!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID), "invalid topic"); + // XXX this could be made to only clear out the cache for the prefs that were changed + // but it probably isn't that big a deal. + gfxQuartzFontCache::SharedFontCache()->ClearPrefFonts(); + return NS_OK; +} + void gfxQuartzFontCache::GenerateFontListKey(const nsAString& aKeyName, nsAString& aResult) { @@ -695,11 +716,17 @@ gfxQuartzFontCache::gfxQuartzFontCache() (void*)this, nsnull); // pref changes notification setup - nsCOMPtr pref = do_GetService(NS_PREF_CONTRACTID); - pref->RegisterCallback("font.", PrefChangedCallback, this); - pref->RegisterCallback("font.name-list.", PrefChangedCallback, this); - pref->RegisterCallback("intl.accept_languages", PrefChangedCallback, this); // hmmmm... - + gfxQuartzFontCachePrefObserver *observer = new gfxQuartzFontCachePrefObserver(); + if (observer) { + nsCOMPtr pref = do_GetService(NS_PREFSERVICE_CONTRACTID); + if (pref) { + pref->AddObserver("font.", observer, PR_FALSE); + pref->AddObserver("font.name-list.", observer, PR_FALSE); + pref->AddObserver("intl.accept_languages", observer, PR_FALSE); // hmmmm... + } else { + delete observer; + } + } } const PRUint32 kNonNormalTraits = NSItalicFontMask | NSBoldFontMask | NSNarrowFontMask | NSExpandedFontMask | NSCondensedFontMask | NSCompressedFontMask; @@ -1088,16 +1115,6 @@ gfxQuartzFontCache::ATSNotification(ATSFontNotificationInfoRef aInfo, qfc->UpdateFontList(); } -int PR_CALLBACK -gfxQuartzFontCache::PrefChangedCallback(const char *aPrefName, void *closure) -{ - // XXX this could be made to only clear out the cache for the prefs that were changed - // but it probably isn't that big a deal. - gfxQuartzFontCache *qfc = static_cast(closure); - qfc->mPrefFonts.Clear(); - return 0; -} - MacOSFontEntry* gfxQuartzFontCache::GetDefaultFont(const gfxFontStyle* aStyle, PRBool& aNeedsBold) { diff --git a/gfx/thebes/src/gfxWindowsPlatform.cpp b/gfx/thebes/src/gfxWindowsPlatform.cpp index 2291fb3a45d..1b3e2d5dc7f 100644 --- a/gfx/thebes/src/gfxWindowsPlatform.cpp +++ b/gfx/thebes/src/gfxWindowsPlatform.cpp @@ -45,7 +45,8 @@ #include "nsUnicharUtils.h" -#include "nsIPref.h" +#include "nsIPrefService.h" +#include "nsIPrefBranch2.h" #include "nsServiceManagerUtils.h" #include "nsTArray.h" @@ -89,14 +90,24 @@ BuildKeyNameFromFontName(nsAString &aName) ToLowerCase(aName); } -int -gfxWindowsPlatform::PrefChangedCallback(const char *aPrefName, void *closure) +class gfxWindowsPlatformPrefObserver : public nsIObserver { +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIOBSERVER +}; + +NS_IMPL_ISUPPORTS1(gfxWindowsPlatformPrefObserver, nsIObserver) + +NS_IMETHODIMP +gfxWindowsPlatformPrefObserver::Observe(nsISupports *aSubject, + const char *aTopic, + const PRUnichar *aData) { + NS_ASSERTION(!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID), "invalid topic"); // XXX this could be made to only clear out the cache for the prefs that were changed // but it probably isn't that big a deal. - gfxWindowsPlatform *plat = static_cast(closure); - plat->mPrefFonts.Clear(); - return 0; + gfxWindowsPlatform::GetPlatform()->ClearPrefFonts(); + return NS_OK; } gfxWindowsPlatform::gfxWindowsPlatform() @@ -115,11 +126,18 @@ gfxWindowsPlatform::gfxWindowsPlatform() UpdateFontList(); - nsCOMPtr pref = do_GetService(NS_PREF_CONTRACTID); - pref->RegisterCallback("font.", PrefChangedCallback, this); - pref->RegisterCallback("font.name-list.", PrefChangedCallback, this); - pref->RegisterCallback("intl.accept_languages", PrefChangedCallback, this); - // don't bother unregistering. We'll get shutdown after the pref service + gfxWindowsPlatformPrefObserver *observer = new gfxWindowsPlatformPrefObserver(); + if (observer) { + nsCOMPtr pref = do_GetService(NS_PREFSERVICE_CONTRACTID); + if (pref) { + pref->AddObserver("font.", observer, PR_FALSE); + pref->AddObserver("font.name-list.", observer, PR_FALSE); + pref->AddObserver("intl.accept_languages", observer, PR_FALSE); + // don't bother unregistering. We'll get shutdown after the pref service + } else { + delete observer; + } + } } gfxWindowsPlatform::~gfxWindowsPlatform()