Bug 1715537 - Log a warning to the web console when a font request is blocked due to font-visibility restrictions. r=emilio

Depends on D124196

Differential Revision: https://phabricator.services.mozilla.com/D124345
This commit is contained in:
Jonathan Kew 2021-09-23 13:36:18 +00:00
Родитель 681e970a3c
Коммит 1cf6b1131c
4 изменённых файлов: 76 добавлений и 17 удалений

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

@ -833,7 +833,7 @@ class gfxFontFamily {
mCheckForFallbackFaces(false),
mCheckedForLegacyFamilyNames(false) {}
const nsCString& Name() { return mName; }
const nsCString& Name() const { return mName; }
virtual void LocalizedName(nsACString& aLocalizedName);
virtual bool HasOtherFamilyNames();

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

@ -792,6 +792,9 @@ gfxFontEntry* gfxPlatformFontList::LookupInSharedFaceNameList(
FontVisibility level =
aPresContext ? aPresContext->GetFontVisibility() : FontVisibility::User;
if (!IsVisibleToCSS(*family, level)) {
if (aPresContext) {
aPresContext->ReportBlockedFontFamily(*family);
}
return nullptr;
}
gfxFontEntry* fe = CreateFontEntry(face, family);
@ -1427,10 +1430,15 @@ bool gfxPlatformFontList::FindAndAddFamilies(
}
// Check whether the family we found is actually allowed to be looked up,
// according to current font-visibility prefs.
if (family && (IsVisibleToCSS(*family, visibilityLevel) ||
(allowHidden && family->IsHidden()))) {
aOutput->AppendElement(FamilyAndGeneric(family, aGeneric));
return true;
if (family) {
bool visible = IsVisibleToCSS(*family, visibilityLevel);
if (visible || (allowHidden && family->IsHidden())) {
aOutput->AppendElement(FamilyAndGeneric(family, aGeneric));
return true;
}
if (aPresContext) {
aPresContext->ReportBlockedFontFamily(*family);
}
}
return false;
}
@ -1438,20 +1446,33 @@ bool gfxPlatformFontList::FindAndAddFamilies(
NS_ASSERTION(mFontFamilies.Count() != 0,
"system font list was not initialized correctly");
auto isBlockedByVisibilityLevel = [=](gfxFontFamily* aFamily) -> bool {
bool visible = IsVisibleToCSS(*aFamily, visibilityLevel);
if (visible || (allowHidden && aFamily->IsHidden())) {
return false;
}
if (aPresContext) {
aPresContext->ReportBlockedFontFamily(*aFamily);
}
return true;
};
// lookup in canonical (i.e. English) family name list
gfxFontFamily* familyEntry = mFontFamilies.GetWeak(key);
if (familyEntry && !IsVisibleToCSS(*familyEntry, visibilityLevel) &&
!(allowHidden && familyEntry->IsHidden())) {
return false;
if (familyEntry) {
if (isBlockedByVisibilityLevel(familyEntry)) {
return false;
}
}
// if not found, lookup in other family names list (mostly localized names)
if (!familyEntry) {
familyEntry = mOtherFamilyNames.GetWeak(key);
}
if (familyEntry && !IsVisibleToCSS(*familyEntry, visibilityLevel) &&
!(allowHidden && familyEntry->IsHidden())) {
return false;
if (familyEntry) {
if (isBlockedByVisibilityLevel(familyEntry)) {
return false;
}
}
// if still not found and other family names not yet fully initialized,
@ -1472,9 +1493,10 @@ bool gfxPlatformFontList::FindAndAddFamilies(
}
mOtherNamesMissed->Insert(key);
}
if (familyEntry && !IsVisibleToCSS(*familyEntry, visibilityLevel) &&
!(allowHidden && familyEntry->IsHidden())) {
return false;
if (familyEntry) {
if (isBlockedByVisibilityLevel(familyEntry)) {
return false;
}
}
}
@ -1505,9 +1527,10 @@ bool gfxPlatformFontList::FindAndAddFamilies(
if (base && base->CheckForLegacyFamilyNames(this)) {
familyEntry = mOtherFamilyNames.GetWeak(key);
}
if (familyEntry && !IsVisibleToCSS(*familyEntry, visibilityLevel) &&
!(allowHidden && familyEntry->IsHidden())) {
return false;
if (familyEntry) {
if (isBlockedByVisibilityLevel(familyEntry)) {
return false;
}
}
}
}

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

@ -27,6 +27,7 @@
#include "nsCOMPtr.h"
#include "nsCSSFrameConstructor.h"
#include "nsDocShell.h"
#include "nsIConsoleService.h"
#include "nsIContentViewer.h"
#include "nsPIDOMWindow.h"
#include "mozilla/ServoStyleSet.h"
@ -835,6 +836,29 @@ bool nsPresContext::UpdateFontVisibility() {
return mFontVisibility != oldValue;
}
void nsPresContext::ReportBlockedFontFamilyName(const nsCString& aFamily,
FontVisibility aVisibility) {
if (!mBlockedFonts.EnsureInserted(aFamily)) {
return;
}
nsAutoString msg;
msg.AppendPrintf(
"Request for font \"%s\" blocked at visibility level %d (requires %d)\n",
aFamily.get(), int(GetFontVisibility()), int(aVisibility));
nsContentUtils::ReportToConsoleNonLocalized(msg, nsIScriptError::warningFlag,
"Security"_ns, mDocument);
}
void nsPresContext::ReportBlockedFontFamily(const fontlist::Family& aFamily) {
auto* fontList = gfxPlatformFontList::PlatformFontList()->SharedFontList();
const nsCString& name = aFamily.DisplayName().AsString(fontList);
ReportBlockedFontFamilyName(name, aFamily.Visibility());
}
void nsPresContext::ReportBlockedFontFamily(const gfxFontFamily& aFamily) {
ReportBlockedFontFamilyName(aFamily.Name(), aFamily.Visibility());
}
void nsPresContext::InitFontCache() {
if (!mFontCache) {
mFontCache = new nsFontCache();

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

@ -30,6 +30,7 @@
#include "nsHashKeys.h"
#include "nsRect.h"
#include "nsStringFwd.h"
#include "nsTHashSet.h"
#include "nsTHashtable.h"
#include "nsAtom.h"
#include "nsIWidgetListener.h" // for nsSizeMode
@ -53,6 +54,7 @@ class nsIFrame;
class nsFrameManager;
class nsAtom;
class nsIRunnable;
class gfxFontFamily;
class gfxFontFeatureValueSet;
class gfxUserFontEntry;
class gfxUserFontSet;
@ -164,6 +166,8 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr {
void UpdateFontCacheUserFonts(gfxUserFontSet* aUserFontSet);
FontVisibility GetFontVisibility() const { return mFontVisibility; }
void ReportBlockedFontFamily(const mozilla::fontlist::Family& aFamily);
void ReportBlockedFontFamily(const gfxFontFamily& aFamily);
/**
* Get the nsFontMetrics that describe the properties of
@ -1157,6 +1161,8 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr {
// visibile to CSS. Returns whether the current visibility value actually
// changed (in which case content should be reflowed).
bool UpdateFontVisibility();
void ReportBlockedFontFamilyName(const nsCString& aFamily,
FontVisibility aVisibility);
// IMPORTANT: The ownership implicit in the following member variables
// has been explicitly checked. If you add any members to this class,
@ -1273,6 +1279,12 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr {
nsTArray<RefPtr<mozilla::ManagedPostRefreshObserver>>
mManagedPostRefreshObservers;
// If we block the use of a font-family that is explicitly requested,
// due to font visibility settings, we log a message to the web console;
// this hash-set keeps track of names we've logged for this context, so
// that we can avoid repeatedly reporting the same font.
nsTHashSet<nsCString> mBlockedFonts;
ScrollStyles mViewportScrollStyles;
uint16_t mImageAnimationMode;