@@ -87,7 +87,21 @@ already_AddRefed<nsFontMetrics> nsFontCache::GetMetricsFor(
87
87
}
88
88
}
89
89
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
+ }
91
105
92
106
// It's not in the cache. Get font metrics and then cache them.
93
107
// If the cache has reached its size limit, drop the older half of the
@@ -112,50 +126,6 @@ already_AddRefed<nsFontMetrics> nsFontCache::GetMetricsFor(
112
126
return fm.forget ();
113
127
}
114
128
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
-
159
129
void nsFontCache::UpdateUserFonts (gfxUserFontSet* aUserFontSet) {
160
130
MOZ_DIAGNOSTIC_ASSERT (NS_IsMainThread());
161
131
for (nsFontMetrics* fm : mFontMetrics ) {
@@ -188,7 +158,6 @@ void nsFontCache::Compact() {
188
158
NS_ADDREF (oldfm);
189
159
}
190
160
}
191
- mMissedFontFamilyNames .Clear ();
192
161
}
193
162
194
163
// Flush the aFlushCount oldest entries, or all if (aFlushCount < 0)
@@ -206,4 +175,7 @@ void nsFontCache::Flush(int32_t aFlushCount) {
206
175
NS_RELEASE (fm);
207
176
}
208
177
mFontMetrics .RemoveElementsAt (0 , n);
178
+
179
+ mLastCacheMiss = 0 ;
180
+ mCacheMisses = 0 ;
209
181
}
0 commit comments