File: FontVisibilityProvider.cpp

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (115 lines) | stat: -rw-r--r-- 3,916 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "FontVisibilityProvider.h"

#include "gfxTypes.h"
#include "gfxFontEntry.h"
#include "gfxPlatformFontList.h"

#include "SharedFontList.h"
#include "nsPresContext.h"
#include "nsRFPService.h"

#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/ContentBlockingAllowList.h"

#include "mozilla/dom/OffscreenCanvas.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/OffscreenCanvas.h"

using namespace mozilla;

void FontVisibilityProvider::ReportBlockedFontFamily(
    const gfxFontFamily& aFamily) const {
  nsCString msg;
  FormatBlockedFontFamilyMessage(msg, aFamily.Name(), aFamily.Visibility());
  ReportBlockedFontFamily(msg);
}

void FontVisibilityProvider::ReportBlockedFontFamily(
    const fontlist::Family& aFamily) const {
  auto* fontList = gfxPlatformFontList::PlatformFontList()->SharedFontList();
  const nsCString& name = aFamily.DisplayName().AsString(fontList);
  nsCString msg;
  FormatBlockedFontFamilyMessage(msg, name, aFamily.Visibility());
  ReportBlockedFontFamily(msg);
}

void FontVisibilityProvider::FormatBlockedFontFamilyMessage(
    nsCString& aMsg, const nsCString& aFamily,
    FontVisibility aVisibility) const {
  aMsg.AppendPrintf(
      "Request for font \"%s\" blocked at visibility level %d (requires "
      "%d)\n",
      aFamily.get(), int(GetFontVisibility()), int(aVisibility));
}

FontVisibility FontVisibilityProvider::ComputeFontVisibility() const {
  /*
   * Expected behavior in order of precedence:
   *  1  If offscreen canvas, attempt to inherit the visibility
   *  2  Chrome Rules give User Level (3)
   *  3  RFP gives Highest Level (1 aka Base)
   *  4  An RFPTarget of Base gives Base Level (1)
   *  5  An RFPTarget of LangPack gives LangPack Level (2)
   *  6  The value of the Standard Font Visibility Pref
   *
   * If the ETP toggle is disabled (aka
   * ContentBlockingAllowList::Check is true), it will only override 4-6,
   * not rules 2 or 3.
   */

  // Rule 1: If the visibility should be inherited, return that value.
  if (Maybe<FontVisibility> maybeVis = MaybeInheritFontVisibility()) {
    return *maybeVis;
  }

  // Rule 2: Allow all font access for privileged contexts, including
  // chrome and devtools contexts.
  if (IsChrome()) {
    return FontVisibility::User;
  }

  // Is this a private browsing context?
  bool isPrivate = IsPrivateBrowsing();

  int32_t level;
  // Rule 4
  if (ShouldResistFingerprinting(RFPTarget::FontVisibilityBaseSystem)) {
    // Rule 3: Check RFP pref
    // This is inside Rule 4 in case this document is exempted from RFP.
    // But if it is not exempted, and RFP is enabled, we return immediately
    // to prevent the override below from occurring.
    if (nsRFPService::IsRFPPrefEnabled(isPrivate)) {
      return FontVisibility::Base;
    }

    level = int32_t(FontVisibility::Base);
  }
  // Rule 5
  else if (ShouldResistFingerprinting(RFPTarget::FontVisibilityLangPack)) {
    level = int32_t(FontVisibility::LangPack);
  }
  // Rule 6
  else {
    level = StaticPrefs::layout_css_font_visibility();
  }

  nsICookieJarSettings* cookieJarSettings = GetCookieJarSettings();

  // Override Rules 4-6 Only: Determine if the user has exempted the
  // domain from tracking protections, if so, use the default value.
  if (level != StaticPrefs::layout_css_font_visibility() &&
      ContentBlockingAllowList::Check(cookieJarSettings)) {
    level = StaticPrefs::layout_css_font_visibility();
  }

  // Clamp result to the valid range of levels.
  level = std::clamp(level, int32_t(FontVisibility::Base),
                     int32_t(FontVisibility::User));

  return FontVisibility(level);
}