]> Pileus Git - ~andy/sunrise/blob - www-client/torbrowser/files/0013-Limit-the-number-of-fonts-per-document.patch
games-rpg/sumwars: correctly respect GAMES_* variables
[~andy/sunrise] / www-client / torbrowser / files / 0013-Limit-the-number-of-fonts-per-document.patch
1 From 4ed1e1c2372216cfcf377b369a9d44bb9f60ee14 Mon Sep 17 00:00:00 2001
2 From: Mike Perry <mikeperry-git@torproject.org>
3 Date: Wed, 1 Feb 2012 16:01:21 -0800
4 Subject: [PATCH 13/13] Limit the number of fonts per document.
5
6 We create two prefs:
7 browser.display.max_font_count and browser.display.max_font_attempts.
8 max_font_count sets a limit on the number of fonts actually used in the
9 document, and max_font_attempts sets a limit on the total number of CSS
10 queries that a document is allowed to perform.
11
12 Once either limit is reached, the browser behaves as if
13 browser.display.use_document_fonts was set to 0 for subsequent font queries.
14
15 If a pref is not set or is negative, that limit does not apply.
16
17 This is done to address:
18 https://www.torproject.org/projects/torbrowser/design/#fingerprinting-linkability
19 ---
20  layout/base/nsPresContext.cpp |  100 +++++++++++++++++++++++++++++++++++++++++
21  layout/base/nsPresContext.h   |    9 ++++
22  layout/style/nsRuleNode.cpp   |   13 ++++-
23  3 files changed, 119 insertions(+), 3 deletions(-)
24
25 diff --git a/layout/base/nsPresContext.cpp b/layout/base/nsPresContext.cpp
26 index e1587db..9690d9c 100644
27 --- a/layout/base/nsPresContext.cpp
28 +++ b/layout/base/nsPresContext.cpp
29 @@ -98,6 +98,8 @@
30  #include "FrameLayerBuilder.h"
31  #include "nsDOMMediaQueryList.h"
32  #include "nsSMILAnimationController.h"
33 +#include "nsString.h"
34 +#include "nsUnicharUtils.h"
35  
36  #ifdef IBMBIDI
37  #include "nsBidiPresUtils.h"
38 @@ -706,6 +708,10 @@ nsPresContext::GetUserPreferences()
39    // * use fonts?
40    mUseDocumentFonts =
41      Preferences::GetInt("browser.display.use_document_fonts") != 0;
42 +  mMaxFonts =
43 +    Preferences::GetInt("browser.display.max_font_count", -1);
44 +  mMaxFontAttempts =
45 +    Preferences::GetInt("browser.display.max_font_attempts", -1);
46  
47    // * replace backslashes with Yen signs? (bug 245770)
48    mEnableJapaneseTransform =
49 @@ -1300,6 +1306,100 @@ nsPresContext::GetDefaultFont(PRUint8 aFontID) const
50    return font;
51  }
52  
53 +PRBool
54 +nsPresContext::FontUseCountReached(const nsFont &font) {
55 +  if (mMaxFonts < 0) {
56 +    return PR_FALSE;
57 +  }
58 +
59 +  for (PRUint32 i = 0; i < mFontsUsed.Length(); i++) {
60 +    if (mFontsUsed[i].name.Equals(font.name,
61 +                                  nsCaseInsensitiveStringComparator())
62 +        // XXX: Style is sometimes filled with garbage??
63 +        /*&& mFontsUsed[i].style == font.style*/) {
64 +      // seen it before: OK
65 +      return PR_FALSE;
66 +    }
67 +  }
68 +
69 +  if (mFontsUsed.Length() >= mMaxFonts) {
70 +    return PR_TRUE;
71 +  }
72 +
73 +  return PR_FALSE;
74 +}
75 +
76 +PRBool
77 +nsPresContext::FontAttemptCountReached(const nsFont &font) {
78 +  if (mMaxFontAttempts < 0) {
79 +    return PR_FALSE;
80 +  }
81 +
82 +  for (PRUint32 i = 0; i < mFontsTried.Length(); i++) {
83 +    if (mFontsTried[i].name.Equals(font.name,
84 +                                  nsCaseInsensitiveStringComparator())
85 +        // XXX: Style is sometimes filled with garbage??
86 +        /*&& mFontsTried[i].style == font.style*/) {
87 +      // seen it before: OK
88 +      return PR_FALSE;
89 +    }
90 +  }
91 +
92 +  if (mFontsTried.Length() >= mMaxFontAttempts) {
93 +    return PR_TRUE;
94 +  }
95 +
96 +  return PR_FALSE;
97 +}
98 +
99 +void
100 +nsPresContext::AddFontUse(const nsFont &font) {
101 +  if (mMaxFonts < 0) {
102 +    return;
103 +  }
104 +
105 +  for (PRUint32 i = 0; i < mFontsUsed.Length(); i++) {
106 +    if (mFontsUsed[i].name.Equals(font.name,
107 +                                  nsCaseInsensitiveStringComparator())
108 +        // XXX: Style is sometimes filled with garbage??
109 +        /*&& mFontsUsed[i].style == font.style*/) {
110 +      // seen it before: OK
111 +      return;
112 +    }
113 +  }
114 +
115 +  if (mFontsUsed.Length() >= mMaxFonts) {
116 +    return;
117 +  }
118 +   
119 +  mFontsUsed.AppendElement(font);
120 +  return;
121 +}
122 +
123 +void
124 +nsPresContext::AddFontAttempt(const nsFont &font) {
125 +  if (mMaxFontAttempts < 0) {
126 +    return;
127 +  }
128 +
129 +  for (PRUint32 i = 0; i < mFontsTried.Length(); i++) {
130 +    if (mFontsTried[i].name.Equals(font.name,
131 +                                  nsCaseInsensitiveStringComparator())
132 +        // XXX: Style is sometimes filled with garbage??
133 +        /*&& mFontsTried[i].style == font.style*/) {
134 +      // seen it before: OK
135 +      return;
136 +    }
137 +  }
138 +
139 +  if (mFontsTried.Length() >= mMaxFontAttempts) {
140 +    return;
141 +  }
142 +   
143 +  mFontsTried.AppendElement(font);
144 +  return;
145 +}
146 +
147  void
148  nsPresContext::SetFullZoom(float aZoom)
149  {
150 diff --git a/layout/base/nsPresContext.h b/layout/base/nsPresContext.h
151 index ecd01d8..552a69a 100644
152 --- a/layout/base/nsPresContext.h
153 +++ b/layout/base/nsPresContext.h
154 @@ -548,6 +548,13 @@ public:
155      }
156    }
157  
158 +  nsTArray<nsFont> mFontsUsed; // currently for font-count limiting only
159 +  nsTArray<nsFont> mFontsTried; // currently for font-count limiting only
160 +  void AddFontUse(const nsFont &font);
161 +  void AddFontAttempt(const nsFont &font);
162 +  PRBool FontUseCountReached(const nsFont &font);
163 +  PRBool FontAttemptCountReached(const nsFont &font);
164 +
165    PRInt32 MinFontSize() const {
166      return NS_MAX(mMinFontSize, mMinimumFontSizePref);
167    }
168 @@ -1117,6 +1124,8 @@ protected:
169    PRUint32              mInterruptChecksToSkip;
170  
171    mozilla::TimeStamp    mReflowStartTime;
172 +  PRInt32               mMaxFontAttempts;
173 +  PRInt32               mMaxFonts;
174  
175    unsigned              mHasPendingInterrupt : 1;
176    unsigned              mInterruptsEnabled : 1;
177 diff --git a/layout/style/nsRuleNode.cpp b/layout/style/nsRuleNode.cpp
178 index 27336bf..827585a 100644
179 --- a/layout/style/nsRuleNode.cpp
180 +++ b/layout/style/nsRuleNode.cpp
181 @@ -3091,6 +3091,7 @@ nsRuleNode::ComputeFontData(void* aStartStruct,
182  
183    // See if there is a minimum font-size constraint to honor
184    nscoord minimumFontSize = mPresContext->MinFontSize();
185 +  PRBool isXUL = PR_FALSE;
186  
187    if (minimumFontSize < 0)
188      minimumFontSize = 0;
189 @@ -3102,10 +3103,10 @@ nsRuleNode::ComputeFontData(void* aStartStruct,
190    // We only need to know this to determine if we have to use the
191    // document fonts (overriding the useDocumentFonts flag), or to
192    // determine if we have to override the minimum font-size constraint.
193 -  if ((!useDocumentFonts || minimumFontSize > 0) && mPresContext->IsChrome()) {
194 +  if (mPresContext->IsChrome()) {
195      // if we are not using document fonts, but this is a XUL document,
196      // then we use the document fonts anyway
197 -    useDocumentFonts = true;
198 +    isXUL = PR_TRUE;
199      minimumFontSize = 0;
200    }
201  
202 @@ -3120,9 +3121,13 @@ nsRuleNode::ComputeFontData(void* aStartStruct,
203      // generic?
204      nsFont::GetGenericID(font->mFont.name, &generic);
205  
206 +    mPresContext->AddFontAttempt(font->mFont);
207 +
208      // If we aren't allowed to use document fonts, then we are only entitled
209      // to use the user's default variable-width font and fixed-width font
210 -    if (!useDocumentFonts) {
211 +    if (!isXUL && (!useDocumentFonts ||
212 +                    mPresContext->FontAttemptCountReached(font->mFont) ||
213 +                    mPresContext->FontUseCountReached(font->mFont))) {
214        // Extract the generic from the specified font family...
215        nsAutoString genericName;
216        if (!font->mFont.EnumerateFamilies(ExtractGeneric, &genericName)) {
217 @@ -3158,6 +3163,8 @@ nsRuleNode::ComputeFontData(void* aStartStruct,
218                                 minimumFontSize, font);
219    }
220  
221 +  if (font->mGenericID == kGenericFont_NONE)
222 +    mPresContext->AddFontUse(font->mFont);
223    COMPUTE_END_INHERITED(Font, font)
224  }
225  
226 -- 
227 1.7.5.4
228