Bug 184657 - GFX should use the newer nsIPrefService APIs instead of nsIPref. r=roc. sr=vladimir

This commit is contained in:
Makoto Kato 2009-04-04 18:43:42 +09:00
parent e79293b2c0
commit ac4e7cb6a8
7 changed files with 96 additions and 64 deletions

View File

@ -38,7 +38,7 @@
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#include "nsIServiceManager.h" #include "nsIServiceManager.h"
#include "nsIPref.h" #include "nsIPrefService.h"
#include "nsCRT.h" #include "nsCRT.h"
#include "nsThebesDeviceContext.h" #include "nsThebesDeviceContext.h"
@ -178,8 +178,8 @@ nsThebesDeviceContext::SetDPI()
// If it's positive, we use it as the logical resolution // If it's positive, we use it as the logical resolution
nsresult rv; nsresult rv;
PRInt32 prefDPI; PRInt32 prefDPI;
nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID, &rv)); nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
if (NS_SUCCEEDED(rv) && prefs) { if (prefs) {
rv = prefs->GetIntPref("layout.css.dpi", &prefDPI); rv = prefs->GetIntPref("layout.css.dpi", &prefDPI);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
prefDPI = -1; prefDPI = -1;

View File

@ -122,6 +122,8 @@ public:
PRBool GetPrefFontEntries(const nsCString& aLangGroup, nsTArray<nsRefPtr<FontEntry> > *array); PRBool GetPrefFontEntries(const nsCString& aLangGroup, nsTArray<nsRefPtr<FontEntry> > *array);
void SetPrefFontEntries(const nsCString& aLangGroup, nsTArray<nsRefPtr<FontEntry> >& array); void SetPrefFontEntries(const nsCString& aLangGroup, nsTArray<nsRefPtr<FontEntry> >& array);
void ClearPrefFonts() { mPrefFonts.Clear(); }
typedef nsDataHashtable<nsStringHashKey, nsRefPtr<FontFamily> > FontTable; typedef nsDataHashtable<nsStringHashKey, nsRefPtr<FontFamily> > FontTable;
#ifdef MOZ_FT2_FONTS #ifdef MOZ_FT2_FONTS

View File

@ -37,7 +37,7 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#include "nsIPref.h" #include "nsIPrefService.h"
#include "nsServiceManagerUtils.h" #include "nsServiceManagerUtils.h"
#include "nsReadableUtils.h" #include "nsReadableUtils.h"
#include "nsExpirationTracker.h" #include "nsExpirationTracker.h"
@ -937,8 +937,7 @@ gfxFontGroup::ForEachFontInternal(const nsAString& aFamilies,
const PRUnichar kDoubleQuote = PRUnichar('\"'); const PRUnichar kDoubleQuote = PRUnichar('\"');
const PRUnichar kComma = PRUnichar(','); const PRUnichar kComma = PRUnichar(',');
nsCOMPtr<nsIPref> prefs; nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
prefs = do_GetService(NS_PREF_CONTRACTID);
nsPromiseFlatString families(aFamilies); nsPromiseFlatString families(aFamilies);
const PRUnichar *p, *p_end; const PRUnichar *p, *p_end;
@ -947,6 +946,7 @@ gfxFontGroup::ForEachFontInternal(const nsAString& aFamilies,
nsAutoString family; nsAutoString family;
nsCAutoString lcFamily; nsCAutoString lcFamily;
nsAutoString genericFamily; nsAutoString genericFamily;
nsXPIDLCString value;
nsCAutoString lang(aLangGroup); nsCAutoString lang(aLangGroup);
if (lang.IsEmpty()) if (lang.IsEmpty())
lang.Assign("x-unicode"); // XXX or should use "x-user-def"? 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 // prefs file always uses (must use) UTF-8 so that we can use
// |GetCharPref| and treat the result as a UTF-8 string. // |GetCharPref| and treat the result as a UTF-8 string.
nsXPIDLString value; nsresult rv = prefs->GetCharPref(prefName.get(), getter_Copies(value));
nsresult rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(value));
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
CopyASCIItoUTF16(lcFamily, genericFamily); CopyASCIItoUTF16(lcFamily, genericFamily);
family = value; CopyUTF8toUTF16(value, family);
} }
} else { } else {
generic = PR_FALSE; generic = PR_FALSE;
@ -1047,10 +1046,10 @@ gfxFontGroup::ForEachFontInternal(const nsAString& aFamilies,
prefName.Append(lcFamily); prefName.Append(lcFamily);
prefName.AppendLiteral("."); prefName.AppendLiteral(".");
prefName.Append(aLangGroup); prefName.Append(aLangGroup);
nsXPIDLString value; nsresult rv = prefs->GetCharPref(prefName.get(), getter_Copies(value));
nsresult rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(value));
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
ForEachFontInternal(value, lang, PR_FALSE, aResolveFontName, ForEachFontInternal(NS_ConvertUTF8toUTF16(value),
lang, PR_FALSE, aResolveFontName,
fc, closure); fc, closure);
} }
} }

View File

@ -58,7 +58,6 @@
#include "gfxTextRunWordCache.h" #include "gfxTextRunWordCache.h"
#include "gfxUserFontSet.h" #include "gfxUserFontSet.h"
#include "nsIPref.h"
#include "nsServiceManagerUtils.h" #include "nsServiceManagerUtils.h"
#include "nsTArray.h" #include "nsTArray.h"
@ -344,45 +343,43 @@ AppendGenericFontFromPref(nsString& aFonts, const char *aLangGroup, const char *
{ {
nsresult rv; nsresult rv;
nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID)); nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (!prefs) if (!prefs)
return; return;
nsCAutoString prefName; nsCAutoString prefName;
nsXPIDLString nameValue, nameListValue; nsXPIDLCString nameValue, nameListValue;
nsXPIDLString genericName; nsCAutoString genericDotLang;
if (aGenericName) { if (aGenericName) {
genericName = NS_ConvertASCIItoUTF16(aGenericName); genericDotLang.Assign(aGenericName);
} else { } else {
prefName.AssignLiteral("font.default."); prefName.AssignLiteral("font.default.");
prefName.Append(aLangGroup); 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.AppendLiteral(".");
genericDotLang.Append(aLangGroup); genericDotLang.Append(aLangGroup);
// fetch font.name.xxx value // fetch font.name.xxx value
prefName.AssignLiteral("font.name."); prefName.AssignLiteral("font.name.");
prefName.Append(genericDotLang); prefName.Append(genericDotLang);
rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(nameValue)); rv = prefs->GetCharPref(prefName.get(), getter_Copies(nameValue));
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
if (!aFonts.IsEmpty()) if (!aFonts.IsEmpty())
aFonts.AppendLiteral(", "); aFonts.AppendLiteral(", ");
aFonts.Append(nameValue); aFonts.Append(NS_ConvertUTF8toUTF16(nameValue));
} }
// fetch font.name-list.xxx value // fetch font.name-list.xxx value
prefName.AssignLiteral("font.name-list."); prefName.AssignLiteral("font.name-list.");
prefName.Append(genericDotLang); 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 (NS_SUCCEEDED(rv) && !nameListValue.Equals(nameValue)) {
if (!aFonts.IsEmpty()) if (!aFonts.IsEmpty())
aFonts.AppendLiteral(", "); aFonts.AppendLiteral(", ");
aFonts.Append(nameListValue); aFonts.Append(NS_ConvertUTF8toUTF16(nameListValue));
} }
} }
@ -401,7 +398,7 @@ PRBool gfxPlatform::ForEachPrefFont(eFontPrefLang aLangArray[], PRUint32 aLangAr
{ {
nsresult rv; nsresult rv;
nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID)); nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (!prefs) if (!prefs)
return PR_FALSE; return PR_FALSE;
@ -412,33 +409,31 @@ PRBool gfxPlatform::ForEachPrefFont(eFontPrefLang aLangArray[], PRUint32 aLangAr
const char *langGroup = GetPrefLangName(prefLang); const char *langGroup = GetPrefLangName(prefLang);
nsCAutoString prefName; nsCAutoString prefName;
nsXPIDLString nameValue, nameListValue; nsXPIDLCString nameValue, nameListValue;
nsXPIDLString genericName;
prefName.AssignLiteral("font.default.");
prefName.Append(langGroup);
prefs->CopyUnicharPref(prefName.get(), getter_Copies(genericName));
nsCAutoString genericDotLang; nsCAutoString genericDotLang;
genericDotLang.Assign(NS_ConvertUTF16toUTF8(genericName)); prefName.AssignLiteral("font.default.");
prefName.Append(langGroup);
prefs->GetCharPref(prefName.get(), getter_Copies(genericDotLang));
genericDotLang.AppendLiteral("."); genericDotLang.AppendLiteral(".");
genericDotLang.Append(langGroup); genericDotLang.Append(langGroup);
// fetch font.name.xxx value // fetch font.name.xxx value
prefName.AssignLiteral("font.name."); prefName.AssignLiteral("font.name.");
prefName.Append(genericDotLang); prefName.Append(genericDotLang);
rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(nameValue)); rv = prefs->GetCharPref(prefName.get(), getter_Copies(nameValue));
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
if (!aCallback(prefLang, nameValue, aClosure)) if (!aCallback(prefLang, NS_ConvertUTF8toUTF16(nameValue), aClosure))
return PR_FALSE; return PR_FALSE;
} }
// fetch font.name-list.xxx value // fetch font.name-list.xxx value
prefName.AssignLiteral("font.name-list."); prefName.AssignLiteral("font.name-list.");
prefName.Append(genericDotLang); 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 (NS_SUCCEEDED(rv) && !nameListValue.Equals(nameValue)) {
if (!aCallback(prefLang, nameListValue, aClosure)) if (!aCallback(prefLang, NS_ConvertUTF8toUTF16(nameListValue), aClosure))
return PR_FALSE; return PR_FALSE;
} }
} }

View File

@ -213,6 +213,8 @@ public:
PRBool GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName); PRBool GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName);
void UpdateFontList() { InitFontList(); } void UpdateFontList() { InitFontList(); }
void ClearPrefFonts() { mPrefFonts.Clear(); }
void GetFontFamilyList(nsTArray<nsRefPtr<MacOSFamilyEntry> >& aFamilyArray); void GetFontFamilyList(nsTArray<nsRefPtr<MacOSFamilyEntry> >& aFamilyArray);
MacOSFontEntry* FindFontForChar(const PRUint32 aCh, gfxFont *aPrevFont); MacOSFontEntry* FindFontForChar(const PRUint32 aCh, gfxFont *aPrevFont);
@ -272,7 +274,6 @@ private:
void GenerateFontListKey(const nsAString& aKeyName, nsAString& aResult); void GenerateFontListKey(const nsAString& aKeyName, nsAString& aResult);
static void ATSNotification(ATSFontNotificationInfoRef aInfo, void* aUserArg); static void ATSNotification(ATSFontNotificationInfoRef aInfo, void* aUserArg);
static int PrefChangedCallback(const char *aPrefName, void *closure);
static PLDHashOperator static PLDHashOperator
HashEnumFuncForFamilies(nsStringHashKey::KeyType aKey, HashEnumFuncForFamilies(nsStringHashKey::KeyType aKey,

View File

@ -47,7 +47,8 @@
#include "gfxAtsuiFonts.h" #include "gfxAtsuiFonts.h"
#include "gfxUserFontSet.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 "nsServiceManagerUtils.h"
#include "nsTArray.h" #include "nsTArray.h"
@ -106,6 +107,26 @@ static NSString* GetNSStringForString(const nsAString& aSrc)
static PRLogModuleInfo *gFontInfoLog = PR_NewLogModule("fontInfoLog"); 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 void
gfxQuartzFontCache::GenerateFontListKey(const nsAString& aKeyName, nsAString& aResult) gfxQuartzFontCache::GenerateFontListKey(const nsAString& aKeyName, nsAString& aResult)
{ {
@ -695,11 +716,17 @@ gfxQuartzFontCache::gfxQuartzFontCache()
(void*)this, nsnull); (void*)this, nsnull);
// pref changes notification setup // pref changes notification setup
nsCOMPtr<nsIPref> pref = do_GetService(NS_PREF_CONTRACTID); gfxQuartzFontCachePrefObserver *observer = new gfxQuartzFontCachePrefObserver();
pref->RegisterCallback("font.", PrefChangedCallback, this); if (observer) {
pref->RegisterCallback("font.name-list.", PrefChangedCallback, this); nsCOMPtr<nsIPrefBranch2> pref = do_GetService(NS_PREFSERVICE_CONTRACTID);
pref->RegisterCallback("intl.accept_languages", PrefChangedCallback, this); // hmmmm... 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; const PRUint32 kNonNormalTraits = NSItalicFontMask | NSBoldFontMask | NSNarrowFontMask | NSExpandedFontMask | NSCondensedFontMask | NSCompressedFontMask;
@ -1088,16 +1115,6 @@ gfxQuartzFontCache::ATSNotification(ATSFontNotificationInfoRef aInfo,
qfc->UpdateFontList(); 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<gfxQuartzFontCache *>(closure);
qfc->mPrefFonts.Clear();
return 0;
}
MacOSFontEntry* MacOSFontEntry*
gfxQuartzFontCache::GetDefaultFont(const gfxFontStyle* aStyle, PRBool& aNeedsBold) gfxQuartzFontCache::GetDefaultFont(const gfxFontStyle* aStyle, PRBool& aNeedsBold)
{ {

View File

@ -45,7 +45,8 @@
#include "nsUnicharUtils.h" #include "nsUnicharUtils.h"
#include "nsIPref.h" #include "nsIPrefService.h"
#include "nsIPrefBranch2.h"
#include "nsServiceManagerUtils.h" #include "nsServiceManagerUtils.h"
#include "nsTArray.h" #include "nsTArray.h"
@ -89,14 +90,24 @@ BuildKeyNameFromFontName(nsAString &aName)
ToLowerCase(aName); ToLowerCase(aName);
} }
int class gfxWindowsPlatformPrefObserver : public nsIObserver {
gfxWindowsPlatform::PrefChangedCallback(const char *aPrefName, void *closure) 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 // 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. // but it probably isn't that big a deal.
gfxWindowsPlatform *plat = static_cast<gfxWindowsPlatform *>(closure); gfxWindowsPlatform::GetPlatform()->ClearPrefFonts();
plat->mPrefFonts.Clear(); return NS_OK;
return 0;
} }
gfxWindowsPlatform::gfxWindowsPlatform() gfxWindowsPlatform::gfxWindowsPlatform()
@ -115,11 +126,18 @@ gfxWindowsPlatform::gfxWindowsPlatform()
UpdateFontList(); UpdateFontList();
nsCOMPtr<nsIPref> pref = do_GetService(NS_PREF_CONTRACTID); gfxWindowsPlatformPrefObserver *observer = new gfxWindowsPlatformPrefObserver();
pref->RegisterCallback("font.", PrefChangedCallback, this); if (observer) {
pref->RegisterCallback("font.name-list.", PrefChangedCallback, this); nsCOMPtr<nsIPrefBranch2> pref = do_GetService(NS_PREFSERVICE_CONTRACTID);
pref->RegisterCallback("intl.accept_languages", PrefChangedCallback, this); if (pref) {
// don't bother unregistering. We'll get shutdown after the pref service 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() gfxWindowsPlatform::~gfxWindowsPlatform()