Skip to content

Commit 5e7382b

Browse files
author
Butkovits Atila
committed
Backed out changeset de74f9fffac1 (bug 1936647) for causing failures at PLDHashTable.h. CLOSED TREE
1 parent 3f2f26b commit 5e7382b

File tree

2 files changed

+24
-52
lines changed

2 files changed

+24
-52
lines changed

gfx/src/nsFontCache.cpp

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,21 @@ already_AddRefed<nsFontMetrics> nsFontCache::GetMetricsFor(
8787
}
8888
}
8989

90-
DetectFontFingerprinting(aFont);
90+
if (!mReportedProbableFingerprinting) {
91+
// We try to detect font fingerprinting attempts by recognizing a large
92+
// number of cache misses in a short amount of time, which indicates the
93+
// usage of an unreasonable amount of different fonts by the web page.
94+
PRTime now = PR_Now();
95+
if (now - mLastCacheMiss > kFingerprintingTimeout) {
96+
mCacheMisses = 0;
97+
}
98+
mCacheMisses++;
99+
mLastCacheMiss = now;
100+
if (NS_IsMainThread() && mCacheMisses > kFingerprintingCacheMissThreshold) {
101+
mContext->Document()->RecordFontFingerprinting();
102+
mReportedProbableFingerprinting = true;
103+
}
104+
}
91105

92106
// It's not in the cache. Get font metrics and then cache them.
93107
// If the cache has reached its size limit, drop the older half of the
@@ -112,50 +126,6 @@ already_AddRefed<nsFontMetrics> nsFontCache::GetMetricsFor(
112126
return fm.forget();
113127
}
114128

115-
void nsFontCache::DetectFontFingerprinting(const nsFont& aFont) {
116-
// We try to detect font fingerprinting attempts by recognizing a large
117-
// number of cache misses in a short amount of time, which indicates the
118-
// usage of an unreasonable amount of different fonts by the web page.
119-
120-
if (mReportedProbableFingerprinting || aFont.family.families.list.IsEmpty()) {
121-
return;
122-
}
123-
124-
PRTime now = PR_Now();
125-
nsAutoString key;
126-
for (const auto& family : aFont.family.families.list.AsSpan()) {
127-
if (family.IsGeneric()) {
128-
continue;
129-
}
130-
key.Append(family.AsFamilyName().name.AsAtom()->GetUTF16String());
131-
}
132-
if (key.IsEmpty()) {
133-
return;
134-
}
135-
136-
mMissedFontFamilyNames.InsertOrUpdate(key, now);
137-
// Don't bother checking for fingerprinting attempts if we haven't seen
138-
// enough cache misses yet.
139-
if (mMissedFontFamilyNames.Count() <= kFingerprintingCacheMissThreshold) {
140-
return;
141-
}
142-
uint16_t fontsMissedRecently = 0;
143-
144-
for (auto iter = mMissedFontFamilyNames.Iter(); !iter.Done(); iter.Next()) {
145-
if (now - kFingerprintingLastNSec <= iter.Data()) {
146-
if (++fontsMissedRecently > kFingerprintingCacheMissThreshold) {
147-
mContext->Document()->RecordFontFingerprinting();
148-
mReportedProbableFingerprinting = true;
149-
mMissedFontFamilyNames.Clear();
150-
break;
151-
}
152-
} else {
153-
// Remove the old entries from missed cache list.
154-
iter.Remove();
155-
}
156-
}
157-
}
158-
159129
void nsFontCache::UpdateUserFonts(gfxUserFontSet* aUserFontSet) {
160130
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
161131
for (nsFontMetrics* fm : mFontMetrics) {
@@ -188,7 +158,6 @@ void nsFontCache::Compact() {
188158
NS_ADDREF(oldfm);
189159
}
190160
}
191-
mMissedFontFamilyNames.Clear();
192161
}
193162

194163
// Flush the aFlushCount oldest entries, or all if (aFlushCount < 0)
@@ -206,4 +175,7 @@ void nsFontCache::Flush(int32_t aFlushCount) {
206175
NS_RELEASE(fm);
207176
}
208177
mFontMetrics.RemoveElementsAt(0, n);
178+
179+
mLastCacheMiss = 0;
180+
mCacheMisses = 0;
209181
}

gfx/src/nsFontCache.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ class nsFontCache final : public nsIObserver {
5252
static constexpr int32_t kMaxCacheEntries = 128;
5353

5454
// Number of cache misses before we assume that a font fingerprinting attempt
55-
// is being made.
56-
static constexpr int32_t kFingerprintingCacheMissThreshold = 20;
55+
// is being made. Usually fingerprinters will lookup the same font-family
56+
// three times, as "sans-serif", "serif" and "monospace".
57+
static constexpr int32_t kFingerprintingCacheMissThreshold = 3 * 20;
5758
// We assume that fingerprinters will lookup a large number of fonts in a
5859
// short amount of time.
59-
static constexpr PRTime kFingerprintingLastNSec =
60+
static constexpr PRTime kFingerprintingTimeout =
6061
PRTime(PR_USEC_PER_SEC) * 3; // 3 seconds
6162

6263
static_assert(kFingerprintingCacheMissThreshold < kMaxCacheEntries);
@@ -92,9 +93,8 @@ class nsFontCache final : public nsIObserver {
9293
RefPtr<nsFontCache> mCache;
9394
};
9495

95-
void DetectFontFingerprinting(const nsFont& aFont);
96-
97-
nsTHashMap<nsStringHashKey, PRTime> mMissedFontFamilyNames;
96+
PRTime mLastCacheMiss = 0;
97+
uint64_t mCacheMisses = 0;
9898
bool mReportedProbableFingerprinting = false;
9999
};
100100

0 commit comments

Comments
 (0)