Bug 1528712 - Remove a couple trivial dependencies on nsPresContext. r=heycam
authorEmilio Cobos Álvarez <emilio@crisal.io>
Tue, 19 Feb 2019 11:17:28 +0000
changeset 459898 533f35feb9eb271cf4e9a41854f02f1725b81131
parent 459897 1fc71d46c09a20908e6d016af6312ac1682d101f
child 459899 d15621c6f424a75f1d4be5234c420bd9ad1f7586
push id78457
push useremilio@crisal.io
push dateTue, 19 Feb 2019 13:54:20 +0000
treeherderautoland@d15621c6f424 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersheycam
bugs1528712
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1528712 - Remove a couple trivial dependencies on nsPresContext. r=heycam Differential Revision: https://phabricator.services.mozilla.com/D20141
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
layout/base/nsLayoutUtils.cpp
layout/base/nsLayoutUtils.h
layout/base/nsPresContext.cpp
layout/base/nsPresContext.h
layout/style/CSSMozDocumentRule.cpp
layout/style/CSSMozDocumentRule.h
layout/style/GeckoBindings.cpp
layout/style/GeckoBindings.h
layout/style/nsMediaFeatures.cpp
modules/libpref/init/StaticPrefList.h
modules/libpref/init/all.js
servo/components/style/gecko/media_features.rs
servo/components/style/gecko/media_queries.rs
servo/components/style/gecko/values.rs
servo/components/style/properties/cascade.rs
servo/components/style/properties/gecko.mako.rs
servo/components/style/properties/longhands/color.mako.rs
servo/components/style/properties/longhands/font.mako.rs
servo/components/style/properties/properties.mako.rs
servo/components/style/stylesheets/document_rule.rs
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -2085,17 +2085,17 @@ bool nsContentUtils::ShouldResistFingerp
 bool nsContentUtils::ShouldResistFingerprinting(nsIDocShell* aDocShell) {
   if (!aDocShell) {
     return false;
   }
   return ShouldResistFingerprinting(aDocShell->GetDocument());
 }
 
 /* static */
-bool nsContentUtils::ShouldResistFingerprinting(Document* aDoc) {
+bool nsContentUtils::ShouldResistFingerprinting(const Document* aDoc) {
   if (!aDoc) {
     return false;
   }
   bool isChrome = nsContentUtils::IsChromeDoc(aDoc);
   return !isChrome && ShouldResistFingerprinting();
 }
 
 /* static */
@@ -6510,17 +6510,18 @@ nsContentUtils::FindInternalContentViewe
     }
     return docFactory.forget();
   }
 
   return nullptr;
 }
 
 static void ReportPatternCompileFailure(nsAString& aPattern,
-                                        Document* aDocument, JSContext* cx) {
+                                        const Document* aDocument,
+                                        JSContext* cx) {
   MOZ_ASSERT(JS_IsExceptionPending(cx));
 
   JS::RootedValue exn(cx);
   if (!JS_GetPendingException(cx, &exn)) {
     return;
   }
   if (!exn.isObject()) {
     // If pending exception is not an object, it should be OOM.
@@ -6549,17 +6550,17 @@ static void ReportPatternCompileFailure(
       nsIScriptError::errorFlag, NS_LITERAL_CSTRING("DOM"), aDocument,
       nsContentUtils::eDOM_PROPERTIES, "PatternAttributeCompileFailure",
       strings, ArrayLength(strings));
   savedExc.drop();
 }
 
 // static
 bool nsContentUtils::IsPatternMatching(nsAString& aValue, nsAString& aPattern,
-                                       Document* aDocument) {
+                                       const Document* aDocument) {
   NS_ASSERTION(aDocument, "aDocument should be a valid pointer (not null)");
 
   // The fact that we're using a JS regexp under the hood should not be visible
   // to things like window onerror handlers, so we don't initialize our JSAPI
   // with the document's window (which may not exist anyway).
   AutoJSAPI jsapi;
   jsapi.Init();
   JSContext* cx = jsapi.cx();
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -302,17 +302,17 @@ class nsContentUtils {
   static bool LookupBindingMember(
       JSContext* aCx, nsIContent* aContent, JS::Handle<jsid> aId,
       JS::MutableHandle<JS::PropertyDescriptor> aDesc);
 
   // Check whether we should avoid leaking distinguishing information to JS/CSS.
   // This function can be called both in the main thread and worker threads.
   static bool ShouldResistFingerprinting();
   static bool ShouldResistFingerprinting(nsIDocShell* aDocShell);
-  static bool ShouldResistFingerprinting(Document* aDoc);
+  static bool ShouldResistFingerprinting(const Document* aDoc);
 
   // Prevent system colors from being exposed to CSS or canvas.
   static bool UseStandinsForNativeColors();
 
   // A helper function to calculate the rounded window size for fingerprinting
   // resistance. The rounded size is based on the chrome UI size and available
   // screen size. If the inputWidth/Height is greater than the available content
   // size, this will report the available content size. Otherwise, it will
@@ -2541,17 +2541,17 @@ class nsContentUtils {
    * WARNING: This method mutates aPattern and aValue!
    *
    * @param aValue    the string to check.
    * @param aPattern  the string defining the pattern.
    * @param aDocument the owner document of the element.
    * @result          whether the given string is matches the pattern.
    */
   static bool IsPatternMatching(nsAString& aValue, nsAString& aPattern,
-                                Document* aDocument);
+                                const Document* aDocument);
 
   /**
    * Calling this adds support for
    * ontouch* event handler DOM attributes.
    */
   static void InitializeTouchEventTable();
 
   /**
--- a/layout/base/nsLayoutUtils.cpp
+++ b/layout/base/nsLayoutUtils.cpp
@@ -9554,20 +9554,18 @@ already_AddRefed<nsFontMetrics> nsLayout
   params.orientation = orientation;
   params.userFontSet =
       aUseUserFontSet ? aPresContext->GetUserFontSet() : nullptr;
   params.textPerf = aPresContext->GetTextPerfMetrics();
   return aPresContext->DeviceContext()->GetMetricsFor(font, params);
 }
 
 /* static */ void nsLayoutUtils::FixupNoneGeneric(
-    nsFont* aFont, const nsPresContext* aPresContext, uint8_t aGenericFontID,
-    const nsFont* aDefaultVariableFont) {
-  bool useDocumentFonts =
-      aPresContext->GetCachedBoolPref(kPresContext_UseDocumentFonts);
+    nsFont* aFont, uint8_t aGenericFontID, const nsFont* aDefaultVariableFont) {
+  bool useDocumentFonts = StaticPrefs::browser_display_use_document_fonts();
   if (aGenericFontID == kGenericFont_NONE ||
       (!useDocumentFonts && (aGenericFontID == kGenericFont_cursive ||
                              aGenericFontID == kGenericFont_fantasy))) {
     FontFamilyType defaultGeneric =
         aDefaultVariableFont->fontlist.GetDefaultFontType();
     MOZ_ASSERT(aDefaultVariableFont->fontlist.IsEmpty() &&
                (defaultGeneric == eFamily_serif ||
                 defaultGeneric == eFamily_sans_serif));
@@ -9582,30 +9580,30 @@ already_AddRefed<nsFontMetrics> nsLayout
         }
       }
     }
   } else {
     aFont->fontlist.SetDefaultFontType(eFamily_none);
   }
 }
 
-/* static */ void nsLayoutUtils::ApplyMinFontSize(
-    nsStyleFont* aFont, const nsPresContext* aPresContext,
-    nscoord aMinFontSize) {
+/* static */ void nsLayoutUtils::ApplyMinFontSize(nsStyleFont* aFont,
+                                                  const Document* aDocument,
+                                                  nscoord aMinFontSize) {
   nscoord fontSize = aFont->mSize;
 
   // enforce the user' specified minimum font-size on the value that we expose
   // (but don't change font-size:0, since that would unhide hidden text)
   if (fontSize > 0) {
     if (aMinFontSize < 0) {
       aMinFontSize = 0;
     } else {
       aMinFontSize = (aMinFontSize * aFont->mMinFontSizeRatio) / 100;
     }
-    if (fontSize < aMinFontSize && !aPresContext->IsChrome()) {
+    if (fontSize < aMinFontSize && !nsContentUtils::IsChromeDoc(aDocument)) {
       // override the minimum font-size constraint
       fontSize = aMinFontSize;
     }
   }
   aFont->mFont.size = fontSize;
 }
 
 /* static */ void nsLayoutUtils::ComputeSystemFont(
--- a/layout/base/nsLayoutUtils.h
+++ b/layout/base/nsLayoutUtils.h
@@ -2941,26 +2941,25 @@ class nsLayoutUtils {
   static already_AddRefed<nsFontMetrics> GetMetricsFor(
       nsPresContext* aPresContext, bool aIsVertical,
       const nsStyleFont* aStyleFont, nscoord aFontSize, bool aUseUserFontSet);
 
   /**
    * Appropriately add the correct font if we are using DocumentFonts or
    * overriding for XUL
    */
-  static void FixupNoneGeneric(nsFont* aFont, const nsPresContext* aPresContext,
-                               uint8_t aGenericFontID,
+  static void FixupNoneGeneric(nsFont* aFont, uint8_t aGenericFontID,
                                const nsFont* aDefaultVariableFont);
 
   /**
    * For an nsStyleFont with mSize set, apply minimum font size constraints
    * from preferences, as well as -moz-min-font-size-ratio.
    */
   static void ApplyMinFontSize(nsStyleFont* aFont,
-                               const nsPresContext* aPresContext,
+                               const mozilla::dom::Document*,
                                nscoord aMinFontSize);
 
   static void ComputeSystemFont(nsFont* aSystemFont,
                                 mozilla::LookAndFeel::FontID aFontID,
                                 const nsFont* aDefaultVariableFont);
 
   static void ComputeFontFeatures(const nsCSSValuePairList* aFeaturesList,
                                   nsTArray<gfxFontFeature>& aFeatureSettings);
--- a/layout/base/nsPresContext.cpp
+++ b/layout/base/nsPresContext.cpp
@@ -189,17 +189,16 @@ nsPresContext::nsPresContext(dom::Docume
       mInterruptChecksToSkip(0),
       mElementsRestyled(0),
       mFramesConstructed(0),
       mFramesReflowed(0),
       mInteractionTimeEnabled(true),
       mHasPendingInterrupt(false),
       mPendingInterruptFromTest(false),
       mInterruptsEnabled(false),
-      mUseDocumentFonts(true),
       mUseDocumentColors(true),
       mUnderlineLinks(true),
       mSendAfterPaintToContent(false),
       mUseFocusColors(false),
       mFocusRingOnAnything(false),
       mFocusRingStyle(false),
       mDrawImageBackground(true),  // always draw the background
       mDrawColorBackground(true),
@@ -513,20 +512,16 @@ void nsPresContext::GetUserPreferences()
   mFocusRingOnAnything = Preferences::GetBool(
       "browser.display.focus_ring_on_anything", mFocusRingOnAnything);
 
   mFocusRingStyle =
       Preferences::GetInt("browser.display.focus_ring_style", mFocusRingStyle);
 
   mBodyTextColor = mDefaultColor;
 
-  // * use fonts?
-  mUseDocumentFonts =
-      Preferences::GetInt("browser.display.use_document_fonts") != 0;
-
   mPrefScrollbarSide = Preferences::GetInt("layout.scrollbar.side");
 
   Document()->ResetLangPrefs();
 
   // * image animation
   nsAutoCString animatePref;
   Preferences::GetCString("image.animation_mode", animatePref);
   if (animatePref.EqualsLiteral("normal"))
--- a/layout/base/nsPresContext.h
+++ b/layout/base/nsPresContext.h
@@ -82,20 +82,20 @@ class LayerManager;
 }  // namespace layers
 namespace dom {
 class Document;
 class Element;
 }  // namespace dom
 }  // namespace mozilla
 
 // supported values for cached bool types
-enum nsPresContext_CachedBoolPrefType {
-  kPresContext_UseDocumentFonts = 1,
-  kPresContext_UnderlineLinks
-};
+//
+// FIXME(emilio): We have StaticPrefs now, probably all of these should be
+// migrated.
+enum nsPresContext_CachedBoolPrefType { kPresContext_UnderlineLinks = 1 };
 
 // supported values for cached integer pref types
 enum nsPresContext_CachedIntPrefType {
   kPresContext_ScrollbarSide = 1,
   kPresContext_BidiDirection
 };
 
 // IDs for the default variable and fixed fonts (not to be changed, see
@@ -360,18 +360,16 @@ class nsPresContext : public nsISupports
   void StopEmulatingMedium();
 
   /** Get a cached boolean pref, by its type */
   // *  - initially created for bugs 31816, 20760, 22963
   bool GetCachedBoolPref(nsPresContext_CachedBoolPrefType aPrefType) const {
     // If called with a constant parameter, the compiler should optimize
     // this switch statement away.
     switch (aPrefType) {
-      case kPresContext_UseDocumentFonts:
-        return mUseDocumentFonts;
       case kPresContext_UnderlineLinks:
         return mUnderlineLinks;
       default:
         NS_ERROR("Invalid arg passed to GetCachedBoolPref");
     }
 
     return false;
   }
@@ -1278,17 +1276,16 @@ class nsPresContext : public nsISupports
   bool mInteractionTimeEnabled;
 
   // last time we did a full style flush
   mozilla::TimeStamp mLastStyleUpdateForAllAnimations;
 
   unsigned mHasPendingInterrupt : 1;
   unsigned mPendingInterruptFromTest : 1;
   unsigned mInterruptsEnabled : 1;
-  unsigned mUseDocumentFonts : 1;
   unsigned mUseDocumentColors : 1;
   unsigned mUnderlineLinks : 1;
   unsigned mSendAfterPaintToContent : 1;
   unsigned mUseFocusColors : 1;
   unsigned mFocusRingOnAnything : 1;
   unsigned mFocusRingStyle : 1;
   unsigned mDrawImageBackground : 1;
   unsigned mDrawColorBackground : 1;
--- a/layout/style/CSSMozDocumentRule.cpp
+++ b/layout/style/CSSMozDocumentRule.cpp
@@ -14,17 +14,17 @@ namespace dom {
 
 using namespace mozilla::css;
 
 /* virtual */ JSObject* CSSMozDocumentRule::WrapObject(
     JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
   return CSSMozDocumentRule_Binding::Wrap(aCx, this, aGivenProto);
 }
 
-bool CSSMozDocumentRule::Match(Document* aDoc, nsIURI* aDocURI,
+bool CSSMozDocumentRule::Match(const Document* aDoc, nsIURI* aDocURI,
                                const nsACString& aDocURISpec,
                                const nsACString& aPattern,
                                DocumentMatchingFunction aMatchingFunction) {
   switch (aMatchingFunction) {
     case DocumentMatchingFunction::MediaDocument: {
       auto kind = aDoc->MediaDocumentKind();
       if (aPattern.EqualsLiteral("all")) {
         return kind != Document::MediaDocumentKind::NotMedia;
--- a/layout/style/CSSMozDocumentRule.h
+++ b/layout/style/CSSMozDocumentRule.h
@@ -17,17 +17,17 @@ namespace dom {
 class CSSMozDocumentRule final : public css::ConditionRule {
  public:
   CSSMozDocumentRule(RefPtr<RawServoMozDocumentRule> aRawRule,
                      StyleSheet* aSheet, css::Rule* aParentRule, uint32_t aLine,
                      uint32_t aColumn);
 
   NS_DECL_ISUPPORTS_INHERITED
 
-  static bool Match(Document* aDoc, nsIURI* aDocURI,
+  static bool Match(const Document*, nsIURI* aDocURI,
                     const nsACString& aDocURISpec, const nsACString& aPattern,
                     css::DocumentMatchingFunction);
 
 #ifdef DEBUG
   void List(FILE* out = stdout, int32_t aIndent = 0) const final;
 #endif
 
   RawServoMozDocumentRule* Raw() const { return mRawRule; }
--- a/layout/style/GeckoBindings.cpp
+++ b/layout/style/GeckoBindings.cpp
@@ -96,24 +96,24 @@ SERVO_ARC_TYPE(ComputedStyle, ComputedSt
 #undef SERVO_ARC_TYPE
 
 // Definitions of the global traversal stats.
 bool ServoTraversalStatistics::sActive = false;
 ServoTraversalStatistics ServoTraversalStatistics::sSingleton;
 
 static RWLock* sServoFFILock = nullptr;
 
-static const nsFont* ThreadSafeGetDefaultFontHelper(
-    const nsPresContext* aPresContext, nsAtom* aLanguage, uint8_t aGenericId) {
+static const nsFont* ThreadSafeGetDefaultFontHelper(const Document& aDocument,
+                                                    nsAtom* aLanguage,
+                                                    uint8_t aGenericId) {
   bool needsCache = false;
   const nsFont* retval;
 
   auto GetDefaultFont = [&](bool* aNeedsToCache) {
-    auto* prefs =
-        aPresContext->Document()->GetFontPrefsForLang(aLanguage, aNeedsToCache);
+    auto* prefs = aDocument.GetFontPrefsForLang(aLanguage, aNeedsToCache);
     return prefs ? prefs->GetDefaultFont(aGenericId) : nullptr;
   };
 
   {
     AutoReadLock guard(*sServoFFILock);
     retval = GetDefaultFont(&needsCache);
   }
   if (!needsCache) {
@@ -696,19 +696,18 @@ void Gecko_FillAllImageLayers(nsStyleIma
   aLayers->FillAllLayers(aMaxLen);
 }
 
 bool Gecko_IsDocumentBody(RawGeckoElementBorrowed aElement) {
   Document* doc = aElement->GetUncomposedDoc();
   return doc && doc->GetBodyElement() == aElement;
 }
 
-nscolor Gecko_GetLookAndFeelSystemColor(
-    int32_t aId, RawGeckoPresContextBorrowed aPresContext) {
-  bool useStandinsForNativeColors = aPresContext && !aPresContext->IsChrome();
+nscolor Gecko_GetLookAndFeelSystemColor(int32_t aId, const Document* aDoc) {
+  bool useStandinsForNativeColors = !nsContentUtils::IsChromeDoc(aDoc);
   nscolor result;
   LookAndFeel::ColorID colorId = static_cast<LookAndFeel::ColorID>(aId);
   AutoWriteLock guard(*sServoFFILock);
   LookAndFeel::GetColor(colorId, useStandinsForNativeColors, &result);
   return result;
 }
 
 bool Gecko_MatchLang(RawGeckoElementBorrowed aElement, nsAtom* aOverrideLang,
@@ -999,27 +998,25 @@ size_t Gecko_SharedFontList_SizeOfInclud
 NS_IMPL_THREADSAFE_FFI_REFCOUNTING(mozilla::SharedFontList, SharedFontList);
 
 void Gecko_CopyFontFamilyFrom(nsFont* dst, const nsFont* src) {
   dst->fontlist = src->fontlist;
 }
 
 void Gecko_nsFont_InitSystem(nsFont* aDest, int32_t aFontId,
                              const nsStyleFont* aFont,
-                             RawGeckoPresContextBorrowed aPresContext) {
+                             const Document* aDocument) {
   const nsFont* defaultVariableFont = ThreadSafeGetDefaultFontHelper(
-      aPresContext, aFont->mLanguage, kPresContext_DefaultVariableFont_ID);
+      *aDocument, aFont->mLanguage, kPresContext_DefaultVariableFont_ID);
 
   // We have passed uninitialized memory to this function,
   // initialize it. We can't simply return an nsFont because then
   // we need to know its size beforehand. Servo cannot initialize nsFont
   // itself, so this will do.
-  nsFont* system = new (aDest) nsFont(*defaultVariableFont);
-
-  MOZ_RELEASE_ASSERT(system);
+  new (aDest) nsFont(*defaultVariableFont);
 
   *aDest = *defaultVariableFont;
   LookAndFeel::FontID fontID = static_cast<LookAndFeel::FontID>(aFontId);
 
   AutoWriteLock guard(*sServoFFILock);
   nsLayoutUtils::ComputeSystemFont(aDest, fontID, defaultVariableFont);
 }
 
@@ -1103,18 +1100,17 @@ void Gecko_AppendAlternateValues(nsFont*
 }
 
 void Gecko_CopyAlternateValuesFrom(nsFont* aDest, const nsFont* aSrc) {
   aDest->alternateValues.Clear();
   aDest->alternateValues.AppendElements(aSrc->alternateValues);
   aDest->featureValueLookup = aSrc->featureValueLookup;
 }
 
-void Gecko_SetCounterStyleToName(CounterStylePtr* aPtr, nsAtom* aName,
-                                 RawGeckoPresContextBorrowed aPresContext) {
+void Gecko_SetCounterStyleToName(CounterStylePtr* aPtr, nsAtom* aName) {
   RefPtr<nsAtom> name = already_AddRefed<nsAtom>(aName);
   *aPtr = name.forget();
 }
 
 void Gecko_SetCounterStyleToSymbols(CounterStylePtr* aPtr, uint8_t aSymbolsType,
                                     nsACString const* const* aSymbols,
                                     uint32_t aSymbolsCount) {
   nsTArray<nsString> symbols(aSymbolsCount);
@@ -1893,63 +1889,63 @@ void Gecko_nsStyleFont_SetLang(nsStyleFo
   aFont->mExplicitLanguage = true;
 }
 
 void Gecko_nsStyleFont_CopyLangFrom(nsStyleFont* aFont,
                                     const nsStyleFont* aSource) {
   aFont->mLanguage = aSource->mLanguage;
 }
 
-void Gecko_nsStyleFont_FixupNoneGeneric(
-    nsStyleFont* aFont, RawGeckoPresContextBorrowed aPresContext) {
+void Gecko_nsStyleFont_FixupNoneGeneric(nsStyleFont* aFont,
+                                        const Document* aDocument) {
   const nsFont* defaultVariableFont = ThreadSafeGetDefaultFontHelper(
-      aPresContext, aFont->mLanguage, kPresContext_DefaultVariableFont_ID);
-  nsLayoutUtils::FixupNoneGeneric(&aFont->mFont, aPresContext,
-                                  aFont->mGenericID, defaultVariableFont);
+      *aDocument, aFont->mLanguage, kPresContext_DefaultVariableFont_ID);
+  nsLayoutUtils::FixupNoneGeneric(&aFont->mFont, aFont->mGenericID,
+                                  defaultVariableFont);
 }
 
-void Gecko_nsStyleFont_PrefillDefaultForGeneric(
-    nsStyleFont* aFont, RawGeckoPresContextBorrowed aPresContext,
-    uint8_t aGenericId) {
-  const nsFont* defaultFont = ThreadSafeGetDefaultFontHelper(
-      aPresContext, aFont->mLanguage, aGenericId);
+void Gecko_nsStyleFont_PrefillDefaultForGeneric(nsStyleFont* aFont,
+                                                const Document* aDocument,
+                                                uint8_t aGenericId) {
+  const nsFont* defaultFont =
+      ThreadSafeGetDefaultFontHelper(*aDocument, aFont->mLanguage, aGenericId);
   // In case of just the language changing, the parent could have had no
   // generic, which Gecko just does regular cascading with. Do the same. This
   // can only happen in the case where the language changed but the family did
   // not
   if (aGenericId != kGenericFont_NONE) {
     aFont->mFont.fontlist = defaultFont->fontlist;
   } else {
     aFont->mFont.fontlist.SetDefaultFontType(
         defaultFont->fontlist.GetDefaultFontType());
   }
 }
 
-void Gecko_nsStyleFont_FixupMinFontSize(
-    nsStyleFont* aFont, RawGeckoPresContextBorrowed aPresContext) {
+void Gecko_nsStyleFont_FixupMinFontSize(nsStyleFont* aFont,
+                                        const Document* aDocument) {
   nscoord minFontSize;
   bool needsCache = false;
 
   auto MinFontSize = [&](bool* aNeedsToCache) {
-    auto* prefs = aPresContext->Document()->GetFontPrefsForLang(
-        aFont->mLanguage, aNeedsToCache);
+    auto* prefs =
+        aDocument->GetFontPrefsForLang(aFont->mLanguage, aNeedsToCache);
     return prefs ? prefs->mMinimumFontSize : 0;
   };
 
   {
     AutoReadLock guard(*sServoFFILock);
     minFontSize = MinFontSize(&needsCache);
   }
 
   if (needsCache) {
     AutoWriteLock guard(*sServoFFILock);
     minFontSize = MinFontSize(nullptr);
   }
 
-  nsLayoutUtils::ApplyMinFontSize(aFont, aPresContext, minFontSize);
+  nsLayoutUtils::ApplyMinFontSize(aFont, aDocument, minFontSize);
 }
 
 void FontSizePrefs::CopyFrom(const LangGroupFontPrefs& prefs) {
   mDefaultVariableSize = prefs.mDefaultVariableFont.size;
   mDefaultFixedSize = prefs.mDefaultFixedFont.size;
   mDefaultSerifSize = prefs.mDefaultSerifFont.size;
   mDefaultSansSerifSize = prefs.mDefaultSansSerifFont.size;
   mDefaultMonospaceSize = prefs.mDefaultMonospaceFont.size;
@@ -2044,22 +2040,16 @@ GeckoFontMetrics Gecko_GetFontMetrics(Ra
   gfxFloat zeroWidth = fm->GetThebesFontGroup()
                            ->GetFirstValidFont()
                            ->GetMetrics(fm->Orientation())
                            .zeroOrAveCharWidth;
   ret.mChSize = NS_round(aPresContext->AppUnitsPerDevPixel() * zeroWidth);
   return ret;
 }
 
-int32_t Gecko_GetAppUnitsPerPhysicalInch(
-    RawGeckoPresContextBorrowed aPresContext) {
-  nsPresContext* presContext = const_cast<nsPresContext*>(aPresContext);
-  return presContext->DeviceContext()->AppUnitsPerPhysicalInch();
-}
-
 NS_IMPL_THREADSAFE_FFI_REFCOUNTING(SheetLoadDataHolder, SheetLoadDataHolder);
 
 void Gecko_StyleSheet_FinishAsyncParse(
     SheetLoadDataHolder* aData, RawServoStyleSheetContentsStrong aSheetContents,
     StyleUseCountersOwned aUseCounters) {
   UniquePtr<StyleUseCounters> useCounters(aUseCounters);
   RefPtr<SheetLoadDataHolder> loadData = aData;
   RefPtr<RawServoStyleSheetContents> sheetContents = aSheetContents.Consume();
@@ -2178,19 +2168,19 @@ void Gecko_AddPropertyToSet(nsCSSPropert
                             nsCSSPropertyID aProperty) {
   aPropertySet->AddProperty(aProperty);
 }
 
 NS_IMPL_THREADSAFE_FFI_REFCOUNTING(nsCSSValueSharedList, CSSValueSharedList);
 
 #define STYLE_STRUCT(name)                                             \
                                                                        \
-  void Gecko_Construct_Default_nsStyle##name(                          \
-      nsStyle##name* ptr, const nsPresContext* pres_context) {         \
-    new (ptr) nsStyle##name(*pres_context->Document());                \
+  void Gecko_Construct_Default_nsStyle##name(nsStyle##name* ptr,       \
+                                             const Document* doc) {    \
+    new (ptr) nsStyle##name(*doc);                                     \
   }                                                                    \
                                                                        \
   void Gecko_CopyConstruct_nsStyle##name(nsStyle##name* ptr,           \
                                          const nsStyle##name* other) { \
     new (ptr) nsStyle##name(*other);                                   \
   }                                                                    \
                                                                        \
   void Gecko_Destroy_nsStyle##name(nsStyle##name* ptr) {               \
@@ -2199,30 +2189,29 @@ NS_IMPL_THREADSAFE_FFI_REFCOUNTING(nsCSS
 
 void Gecko_RegisterProfilerThread(const char* name) {
   PROFILER_REGISTER_THREAD(name);
 }
 
 void Gecko_UnregisterProfilerThread() { PROFILER_UNREGISTER_THREAD(); }
 
 bool Gecko_DocumentRule_UseForPresentation(
-    RawGeckoPresContextBorrowed aPresContext, const nsACString* aPattern,
+    const Document* aDocument, const nsACString* aPattern,
     css::DocumentMatchingFunction aMatchingFunction) {
   MOZ_ASSERT(NS_IsMainThread());
 
-  Document* doc = aPresContext->Document();
-  nsIURI* docURI = doc->GetDocumentURI();
+  nsIURI* docURI = aDocument->GetDocumentURI();
   nsAutoCString docURISpec;
   if (docURI) {
     // If GetSpec fails (due to OOM) just skip these URI-specific CSS rules.
     nsresult rv = docURI->GetSpec(docURISpec);
     NS_ENSURE_SUCCESS(rv, false);
   }
 
-  return CSSMozDocumentRule::Match(doc, docURI, docURISpec, *aPattern,
+  return CSSMozDocumentRule::Match(aDocument, docURI, docURISpec, *aPattern,
                                    aMatchingFunction);
 }
 
 void Gecko_SetJemallocThreadLocalArena(bool enabled) {
 #if defined(MOZ_MEMORY)
   jemalloc_thread_local_arena(enabled);
 #endif
 }
--- a/layout/style/GeckoBindings.h
+++ b/layout/style/GeckoBindings.h
@@ -272,17 +272,17 @@ size_t Gecko_SharedFontList_SizeOfInclud
     mozilla::SharedFontList* fontlist);
 
 NS_DECL_THREADSAFE_FFI_REFCOUNTING(mozilla::SharedFontList, SharedFontList);
 
 // will not run destructors on dst, give it uninitialized memory
 // font_id is LookAndFeel::FontID
 void Gecko_nsFont_InitSystem(nsFont* dst, int32_t font_id,
                              const nsStyleFont* font,
-                             RawGeckoPresContextBorrowed pres_context);
+                             const mozilla::dom::Document*);
 
 void Gecko_nsFont_Destroy(nsFont* dst);
 
 // The gfxFontFeatureValueSet returned from this function has zero reference.
 gfxFontFeatureValueSet* Gecko_ConstructFontFeatureValueSet();
 
 nsTArray<unsigned int>* Gecko_AppendFeatureValueHashEntry(
     gfxFontFeatureValueSet* value_set, nsAtom* family, uint32_t alternate,
@@ -307,18 +307,17 @@ void Gecko_SetImageOrientation(nsStyleVi
 
 void Gecko_SetImageOrientationAsFromImage(nsStyleVisibility* aVisibility);
 
 void Gecko_CopyImageOrientationFrom(nsStyleVisibility* aDst,
                                     const nsStyleVisibility* aSrc);
 
 // Counter style.
 // This function takes an already addrefed nsAtom
-void Gecko_SetCounterStyleToName(mozilla::CounterStylePtr* ptr, nsAtom* name,
-                                 RawGeckoPresContextBorrowed pres_context);
+void Gecko_SetCounterStyleToName(mozilla::CounterStylePtr* ptr, nsAtom* name);
 
 void Gecko_SetCounterStyleToSymbols(mozilla::CounterStylePtr* ptr,
                                     uint8_t symbols_type,
                                     nsACString const* const* symbols,
                                     uint32_t symbols_count);
 
 void Gecko_SetCounterStyleToString(mozilla::CounterStylePtr* ptr,
                                    const nsACString* symbol);
@@ -665,25 +664,25 @@ float Gecko_FontWeight_ToFloat(mozilla::
 
 void Gecko_FontWeight_SetFloat(mozilla::FontWeight* aWeight, float aFloatValue);
 
 void Gecko_nsStyleFont_SetLang(nsStyleFont* font, nsAtom* atom);
 
 void Gecko_nsStyleFont_CopyLangFrom(nsStyleFont* aFont,
                                     const nsStyleFont* aSource);
 
-void Gecko_nsStyleFont_FixupNoneGeneric(
-    nsStyleFont* font, RawGeckoPresContextBorrowed pres_context);
+void Gecko_nsStyleFont_FixupNoneGeneric(nsStyleFont* font,
+                                        const mozilla::dom::Document*);
 
-void Gecko_nsStyleFont_PrefillDefaultForGeneric(
-    nsStyleFont* font, RawGeckoPresContextBorrowed pres_context,
-    uint8_t generic_id);
+void Gecko_nsStyleFont_PrefillDefaultForGeneric(nsStyleFont* font,
+                                                const mozilla::dom::Document*,
+                                                uint8_t generic_id);
 
-void Gecko_nsStyleFont_FixupMinFontSize(
-    nsStyleFont* font, RawGeckoPresContextBorrowed pres_context);
+void Gecko_nsStyleFont_FixupMinFontSize(nsStyleFont* font,
+                                        const mozilla::dom::Document*);
 
 mozilla::FontSizePrefs Gecko_GetBaseSize(nsAtom* lang);
 
 // XBL related functions.
 RawGeckoElementBorrowedOrNull Gecko_GetBindingParent(
     RawGeckoElementBorrowed aElement);
 
 RawServoAuthorStylesBorrowedOrNull Gecko_XBLBinding_GetRawServoStyles(
@@ -696,51 +695,48 @@ struct GeckoFontMetrics {
   nscoord mXSize;
 };
 
 GeckoFontMetrics Gecko_GetFontMetrics(RawGeckoPresContextBorrowed pres_context,
                                       bool is_vertical, const nsStyleFont* font,
                                       nscoord font_size,
                                       bool use_user_font_set);
 
-int32_t Gecko_GetAppUnitsPerPhysicalInch(
-    RawGeckoPresContextBorrowed pres_context);
-
 mozilla::StyleSheet* Gecko_StyleSheet_Clone(
     const mozilla::StyleSheet* aSheet,
     const mozilla::StyleSheet* aNewParentSheet);
 
 void Gecko_StyleSheet_AddRef(const mozilla::StyleSheet* aSheet);
 void Gecko_StyleSheet_Release(const mozilla::StyleSheet* aSheet);
 nsCSSKeyword Gecko_LookupCSSKeyword(const uint8_t* string, uint32_t len);
 const char* Gecko_CSSKeywordString(nsCSSKeyword keyword, uint32_t* len);
 bool Gecko_IsDocumentBody(RawGeckoElementBorrowed element);
 
 // We use an int32_t here instead of a LookAndFeel::ColorID
 // because forward-declaring a nested enum/struct is impossible
-nscolor Gecko_GetLookAndFeelSystemColor(
-    int32_t color_id, RawGeckoPresContextBorrowed pres_context);
+nscolor Gecko_GetLookAndFeelSystemColor(int32_t color_id,
+                                        const mozilla::dom::Document*);
 
 void Gecko_AddPropertyToSet(nsCSSPropertyIDSetBorrowedMut, nsCSSPropertyID);
 
 // Style-struct management.
-#define STYLE_STRUCT(name)                                            \
-  void Gecko_Construct_Default_nsStyle##name(                         \
-      nsStyle##name* ptr, RawGeckoPresContextBorrowed pres_context);  \
-  void Gecko_CopyConstruct_nsStyle##name(nsStyle##name* ptr,          \
-                                         const nsStyle##name* other); \
+#define STYLE_STRUCT(name)                                                   \
+  void Gecko_Construct_Default_nsStyle##name(nsStyle##name* ptr,             \
+                                             const mozilla::dom::Document*); \
+  void Gecko_CopyConstruct_nsStyle##name(nsStyle##name* ptr,                 \
+                                         const nsStyle##name* other);        \
   void Gecko_Destroy_nsStyle##name(nsStyle##name* ptr);
 #include "nsStyleStructList.h"
 #undef STYLE_STRUCT
 
 void Gecko_RegisterProfilerThread(const char* name);
 void Gecko_UnregisterProfilerThread();
 
 bool Gecko_DocumentRule_UseForPresentation(
-    RawGeckoPresContextBorrowed, const nsACString* aPattern,
+    const mozilla::dom::Document*, const nsACString* aPattern,
     mozilla::css::DocumentMatchingFunction);
 
 // Allocator hinting.
 void Gecko_SetJemallocThreadLocalArena(bool enabled);
 void Gecko_AddBufferToCrashReport(const void* addr, size_t len);
 void Gecko_AnnotateCrashReport(uint32_t key, const char* value_str);
 
 // Pseudo-element flags.
@@ -784,38 +780,39 @@ bool Gecko_IsInServoTraversal();
 
 // Returns true if we're currently on the main thread.
 bool Gecko_IsMainThread();
 
 // Media feature helpers.
 //
 // Defined in nsMediaFeatures.cpp.
 mozilla::StyleDisplayMode Gecko_MediaFeatures_GetDisplayMode(
-    mozilla::dom::Document*);
+    const mozilla::dom::Document*);
 
-uint32_t Gecko_MediaFeatures_GetColorDepth(mozilla::dom::Document*);
+uint32_t Gecko_MediaFeatures_GetColorDepth(const mozilla::dom::Document*);
 
-void Gecko_MediaFeatures_GetDeviceSize(mozilla::dom::Document*, nscoord* width,
-                                       nscoord* height);
+void Gecko_MediaFeatures_GetDeviceSize(const mozilla::dom::Document*,
+                                       nscoord* width, nscoord* height);
 
-float Gecko_MediaFeatures_GetResolution(mozilla::dom::Document*);
-bool Gecko_MediaFeatures_PrefersReducedMotion(mozilla::dom::Document*);
+float Gecko_MediaFeatures_GetResolution(const mozilla::dom::Document*);
+bool Gecko_MediaFeatures_PrefersReducedMotion(const mozilla::dom::Document*);
 mozilla::StylePrefersColorScheme Gecko_MediaFeatures_PrefersColorScheme(
-    mozilla::dom::Document*);
+    const mozilla::dom::Document*);
 
 mozilla::PointerCapabilities Gecko_MediaFeatures_PrimaryPointerCapabilities(
-    mozilla::dom::Document*);
+    const mozilla::dom::Document*);
 
 mozilla::PointerCapabilities Gecko_MediaFeatures_AllPointerCapabilities(
-    mozilla::dom::Document*);
+    const mozilla::dom::Document*);
 
-float Gecko_MediaFeatures_GetDevicePixelRatio(mozilla::dom::Document*);
+float Gecko_MediaFeatures_GetDevicePixelRatio(const mozilla::dom::Document*);
 
-bool Gecko_MediaFeatures_HasSystemMetric(mozilla::dom::Document*,
+bool Gecko_MediaFeatures_HasSystemMetric(const mozilla::dom::Document*,
                                          nsAtom* metric,
                                          bool is_accessible_from_content);
 
-bool Gecko_MediaFeatures_IsResourceDocument(mozilla::dom::Document*);
-nsAtom* Gecko_MediaFeatures_GetOperatingSystemVersion(mozilla::dom::Document*);
+bool Gecko_MediaFeatures_IsResourceDocument(const mozilla::dom::Document*);
+nsAtom* Gecko_MediaFeatures_GetOperatingSystemVersion(
+    const mozilla::dom::Document*);
 
 }  // extern "C"
 
 #endif  // mozilla_GeckoBindings_h
--- a/layout/style/nsMediaFeatures.cpp
+++ b/layout/style/nsMediaFeatures.cpp
@@ -20,16 +20,17 @@
 #include "mozilla/dom/Document.h"
 #include "nsIWidget.h"
 #include "nsContentUtils.h"
 #include "mozilla/StyleSheet.h"
 #include "mozilla/StyleSheetInlines.h"
 #include "mozilla/GeckoBindings.h"
 
 using namespace mozilla;
+using mozilla::dom::Document;
 
 static nsTArray<const nsStaticAtom*>* sSystemMetrics = nullptr;
 
 #ifdef XP_WIN
 struct OperatingSystemVersionInfo {
   LookAndFeel::OperatingSystemVersion mId;
   nsStaticAtom* const mName;
 };
@@ -37,17 +38,17 @@ struct OperatingSystemVersionInfo {
 // Os version identities used in the -moz-os-version media query.
 const OperatingSystemVersionInfo kOsVersionStrings[] = {
     {LookAndFeel::eOperatingSystemVersion_Windows7, nsGkAtoms::windows_win7},
     {LookAndFeel::eOperatingSystemVersion_Windows8, nsGkAtoms::windows_win8},
     {LookAndFeel::eOperatingSystemVersion_Windows10, nsGkAtoms::windows_win10}};
 #endif
 
 // A helper for four features below
-static nsSize GetSize(Document* aDocument) {
+static nsSize GetSize(const Document* aDocument) {
   nsPresContext* pc = aDocument->GetPresContext();
 
   // Per spec, return a 0x0 viewport if we're not being rendered. See:
   //
   //  * https://github.com/w3c/csswg-drafts/issues/571
   //  * https://github.com/whatwg/html/issues/1813
   //
   if (!pc) {
@@ -59,26 +60,26 @@ static nsSize GetSize(Document* aDocumen
     //
     // FIXME(emilio, bug 1414600): Not quite!
     return pc->GetPageSize();
   }
 
   return pc->GetVisibleArea().Size();
 }
 
-static bool IsDeviceSizePageSize(Document* aDocument) {
+static bool IsDeviceSizePageSize(const Document* aDocument) {
   nsIDocShell* docShell = aDocument->GetDocShell();
   if (!docShell) {
     return false;
   }
   return docShell->GetDeviceSizeIsPageSize();
 }
 
 // A helper for three features below.
-static nsSize GetDeviceSize(Document* aDocument) {
+static nsSize GetDeviceSize(const Document* aDocument) {
   if (nsContentUtils::ShouldResistFingerprinting(aDocument) ||
       IsDeviceSizePageSize(aDocument)) {
     return GetSize(aDocument);
   }
 
   nsPresContext* pc = aDocument->GetPresContext();
   // NOTE(emilio): We should probably figure out how to return an appropriate
   // device size here, though in a multi-screen world that makes no sense
@@ -94,40 +95,40 @@ static nsSize GetDeviceSize(Document* aD
     return pc->GetPageSize();
   }
 
   nsSize size;
   pc->DeviceContext()->GetDeviceSurfaceDimensions(size.width, size.height);
   return size;
 }
 
-bool Gecko_MediaFeatures_IsResourceDocument(Document* aDocument) {
+bool Gecko_MediaFeatures_IsResourceDocument(const Document* aDocument) {
   return aDocument->IsResourceDoc();
 }
 
-static nsDeviceContext* GetDeviceContextFor(Document* aDocument) {
+static nsDeviceContext* GetDeviceContextFor(const Document* aDocument) {
   nsPresContext* pc = aDocument->GetPresContext();
   if (!pc) {
     return nullptr;
   }
 
   // It would be nice to call nsLayoutUtils::GetDeviceContextForScreenInfo here,
   // except for two things:  (1) it can flush, and flushing is bad here, and (2)
   // it doesn't really get us consistency in multi-monitor situations *anyway*.
   return pc->DeviceContext();
 }
 
-void Gecko_MediaFeatures_GetDeviceSize(Document* aDocument, nscoord* aWidth,
-                                       nscoord* aHeight) {
+void Gecko_MediaFeatures_GetDeviceSize(const Document* aDocument,
+                                       nscoord* aWidth, nscoord* aHeight) {
   nsSize size = GetDeviceSize(aDocument);
   *aWidth = size.width;
   *aHeight = size.height;
 }
 
-uint32_t Gecko_MediaFeatures_GetColorDepth(Document* aDocument) {
+uint32_t Gecko_MediaFeatures_GetColorDepth(const Document* aDocument) {
   // Use depth of 24 when resisting fingerprinting, or when we're not being
   // rendered.
   uint32_t depth = 24;
 
   if (!nsContentUtils::ShouldResistFingerprinting(aDocument)) {
     if (nsDeviceContext* dx = GetDeviceContextFor(aDocument)) {
       // FIXME: On a monochrome device, return 0!
       dx->GetDepth(depth);
@@ -135,17 +136,17 @@ uint32_t Gecko_MediaFeatures_GetColorDep
   }
 
   // The spec says to use bits *per color component*, so divide by 3,
   // and round down, since the spec says to use the smallest when the
   // color components differ.
   return depth / 3;
 }
 
-float Gecko_MediaFeatures_GetResolution(Document* aDocument) {
+float Gecko_MediaFeatures_GetResolution(const Document* aDocument) {
   // We're returning resolution in terms of device pixels per css pixel, since
   // that is the preferred unit for media queries of resolution. This avoids
   // introducing precision error from conversion to and from less-used
   // physical units like inches.
   nsPresContext* pc = aDocument->GetPresContext();
   if (!pc) {
     return 1.;
   }
@@ -157,26 +158,26 @@ float Gecko_MediaFeatures_GetResolution(
   if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
     return pc->DeviceContext()->GetFullZoom();
   }
   // Get the actual device pixel ratio, which also takes zoom into account.
   return float(AppUnitsPerCSSPixel()) /
          pc->DeviceContext()->AppUnitsPerDevPixel();
 }
 
-static Document* TopDocument(Document* aDocument) {
-  Document* current = aDocument;
-  while (Document* parent = current->GetParentDocument()) {
+static const Document* TopDocument(const Document* aDocument) {
+  const Document* current = aDocument;
+  while (const Document* parent = current->GetParentDocument()) {
     current = parent;
   }
   return current;
 }
 
-StyleDisplayMode Gecko_MediaFeatures_GetDisplayMode(Document* aDocument) {
-  Document* rootDocument = TopDocument(aDocument);
+StyleDisplayMode Gecko_MediaFeatures_GetDisplayMode(const Document* aDocument) {
+  const Document* rootDocument = TopDocument(aDocument);
 
   nsCOMPtr<nsISupports> container = rootDocument->GetContainer();
   if (nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(container)) {
     nsCOMPtr<nsIWidget> mainWidget;
     baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
     if (mainWidget && mainWidget->SizeMode() == nsSizeMode_Fullscreen) {
       return StyleDisplayMode::Fullscreen;
     }
@@ -194,28 +195,29 @@ StyleDisplayMode Gecko_MediaFeatures_Get
 
   nsIDocShell* docShell = rootDocument->GetDocShell();
   if (!docShell) {
     return StyleDisplayMode::Browser;
   }
   return static_cast<StyleDisplayMode>(docShell->GetDisplayMode());
 }
 
-bool Gecko_MediaFeatures_HasSystemMetric(Document* aDocument, nsAtom* aMetric,
+bool Gecko_MediaFeatures_HasSystemMetric(const Document* aDocument,
+                                         nsAtom* aMetric,
                                          bool aIsAccessibleFromContent) {
   if (aIsAccessibleFromContent &&
       nsContentUtils::ShouldResistFingerprinting(aDocument)) {
     return false;
   }
 
   nsMediaFeatures::InitSystemMetrics();
   return sSystemMetrics->IndexOf(aMetric) != sSystemMetrics->NoIndex;
 }
 
-nsAtom* Gecko_MediaFeatures_GetOperatingSystemVersion(Document* aDocument) {
+nsAtom* Gecko_MediaFeatures_GetOperatingSystemVersion(const Document* aDocument) {
   if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
     return nullptr;
   }
 
 #ifdef XP_WIN
   int32_t metricResult;
   if (NS_SUCCEEDED(LookAndFeel::GetInt(
           LookAndFeel::eIntID_OperatingSystemVersionIdentifier,
@@ -226,25 +228,25 @@ nsAtom* Gecko_MediaFeatures_GetOperating
       }
     }
   }
 #endif
 
   return nullptr;
 }
 
-bool Gecko_MediaFeatures_PrefersReducedMotion(Document* aDocument) {
+bool Gecko_MediaFeatures_PrefersReducedMotion(const Document* aDocument) {
   if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
     return false;
   }
   return LookAndFeel::GetInt(LookAndFeel::eIntID_PrefersReducedMotion, 0) == 1;
 }
 
 StylePrefersColorScheme Gecko_MediaFeatures_PrefersColorScheme(
-    Document* aDocument) {
+    const Document* aDocument) {
   if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
     return StylePrefersColorScheme::Light;
   }
   if (nsPresContext* pc = aDocument->GetPresContext()) {
     if (pc->IsPrintingOrPrintPreview()) {
       return StylePrefersColorScheme::Light;
     }
   }
@@ -259,17 +261,17 @@ StylePrefersColorScheme Gecko_MediaFeatu
       return StylePrefersColorScheme::NoPreference;
     default:
       // This only occurs if the user has set the ui.systemUsesDarkTheme pref to
       // an invalid value.
       return StylePrefersColorScheme::Light;
   }
 }
 
-static PointerCapabilities GetPointerCapabilities(Document* aDocument,
+static PointerCapabilities GetPointerCapabilities(const Document* aDocument,
                                                   LookAndFeel::IntID aID) {
   MOZ_ASSERT(aID == LookAndFeel::eIntID_PrimaryPointerCapabilities ||
              aID == LookAndFeel::eIntID_AllPointerCapabilities);
   MOZ_ASSERT(aDocument);
 
   if (nsIDocShell* docShell = aDocument->GetDocShell()) {
     // The touch-events-override happens only for the Responsive Design Mode so
     // that we don't need to care about ResistFingerprinting.
@@ -292,23 +294,23 @@ static PointerCapabilities GetPointerCap
   if (NS_FAILED(rv)) {
     return kDefaultCapabilities;
   }
 
   return static_cast<PointerCapabilities>(intValue);
 }
 
 PointerCapabilities Gecko_MediaFeatures_PrimaryPointerCapabilities(
-    Document* aDocument) {
+    const Document* aDocument) {
   return GetPointerCapabilities(aDocument,
                                 LookAndFeel::eIntID_PrimaryPointerCapabilities);
 }
 
 PointerCapabilities Gecko_MediaFeatures_AllPointerCapabilities(
-    Document* aDocument) {
+    const Document* aDocument) {
   return GetPointerCapabilities(aDocument,
                                 LookAndFeel::eIntID_AllPointerCapabilities);
 }
 
 /* static */ void nsMediaFeatures::InitSystemMetrics() {
   if (sSystemMetrics) return;
 
   MOZ_ASSERT(NS_IsMainThread());
--- a/modules/libpref/init/StaticPrefList.h
+++ b/modules/libpref/init/StaticPrefList.h
@@ -543,16 +543,24 @@ VARCACHE_PREF(
    full_screen_api_unprefix_enabled,
   bool, true
 )
 
 //---------------------------------------------------------------------------
 // Graphics prefs
 //---------------------------------------------------------------------------
 
+// In theory: 0 = never, 1 = quick, 2 = always, though we always just use it as
+// a bool!
+VARCACHE_PREF(
+  "browser.display.use_document_fonts",
+   browser_display_use_document_fonts,
+  RelaxedAtomicInt32, 1
+)
+
 VARCACHE_PREF(
   "gfx.font_rendering.opentype_svg.enabled",
    gfx_font_rendering_opentype_svg_enabled,
   bool, true
 )
 
 VARCACHE_PREF(
   "gfx.offscreencanvas.enabled",
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -288,17 +288,16 @@ pref("ui.context_menus.after_mouseup", f
 pref("ui.menu.incremental_search.timeout", 1000);
 // If true, all popups won't hide automatically on blur
 pref("ui.popup.disable_autohide", false);
 
 #ifdef XP_MACOSX
 pref("ui.touchbar.layout", "Back,Reload,OpenOrFocus,AddBookmark,NewTab,Share");
 #endif
 
-pref("browser.display.use_document_fonts",  1);  // 0 = never, 1 = quick, 2 = always
 // 0 = default: always, except in high contrast mode
 // 1 = always
 // 2 = never
 pref("browser.display.document_color_use", 0);
 pref("browser.display.use_system_colors",   false);
 pref("browser.display.foreground_color",    "#000000");
 pref("browser.display.background_color",    "#FFFFFF");
 pref("browser.display.force_inline_alttext", false); // true = force ALT text for missing images to be layed out inline
--- a/servo/components/style/gecko/media_features.rs
+++ b/servo/components/style/gecko/media_features.rs
@@ -456,17 +456,17 @@ fn eval_any_hover(device: &Device, query
     eval_hover_capabilities(query_value, all_pointer_capabilities(device))
 }
 
 fn eval_moz_is_glyph(
     device: &Device,
     query_value: Option<bool>,
     _: Option<RangeOrOperator>,
 ) -> bool {
-    let is_glyph = unsafe { (*device.document()).mIsSVGGlyphsDocument() };
+    let is_glyph = device.document().mIsSVGGlyphsDocument();
     query_value.map_or(is_glyph, |v| v == is_glyph)
 }
 
 fn eval_moz_is_resource_document(
     device: &Device,
     query_value: Option<bool>,
     _: Option<RangeOrOperator>,
 ) -> bool {
--- a/servo/components/style/gecko/media_queries.rs
+++ b/servo/components/style/gecko/media_queries.rs
@@ -80,17 +80,17 @@ unsafe impl Sync for Device {}
 unsafe impl Send for Device {}
 
 impl Device {
     /// Trivially constructs a new `Device`.
     pub fn new(pres_context: RawGeckoPresContextBorrowed) -> Self {
         assert!(!pres_context.is_null());
         Device {
             pres_context,
-            default_values: ComputedValues::default_values(unsafe { &*pres_context }),
+            default_values: ComputedValues::default_values(unsafe { &*(*pres_context).mDocument.mRawPtr }),
             // FIXME(bz): Seems dubious?
             root_font_size: AtomicIsize::new(FontSize::medium().size().0 as isize),
             body_text_color: AtomicUsize::new(unsafe { &*pres_context }.mDefaultColor as usize),
             used_root_font_size: AtomicBool::new(false),
             used_viewport_size: AtomicBool::new(false),
             environment: CssEnvironment,
         }
     }
@@ -157,23 +157,23 @@ impl Device {
     /// Gets the pres context associated with this document.
     #[inline]
     pub fn pres_context(&self) -> &nsPresContext {
         unsafe { &*self.pres_context }
     }
 
     /// Gets the document pointer.
     #[inline]
-    pub fn document(&self) -> *mut structs::Document {
-        self.pres_context().mDocument.mRawPtr
+    pub fn document(&self) -> &structs::Document {
+        unsafe { &*self.pres_context().mDocument.mRawPtr }
     }
 
     /// Recreates the default computed values.
     pub fn reset_computed_values(&mut self) {
-        self.default_values = ComputedValues::default_values(self.pres_context());
+        self.default_values = ComputedValues::default_values(self.document());
     }
 
     /// Rebuild all the cached data.
     pub fn rebuild_cached_data(&mut self) {
         self.reset_computed_values();
         self.used_root_font_size.store(false, Ordering::Relaxed);
         self.used_viewport_size.store(false, Ordering::Relaxed);
     }
--- a/servo/components/style/gecko/values.rs
+++ b/servo/components/style/gecko/values.rs
@@ -5,17 +5,16 @@
 #![allow(unsafe_code)]
 
 //! Different kind of helpers to interact with Gecko values.
 
 use crate::counter_style::{Symbol, Symbols};
 use crate::gecko_bindings::structs::{nsStyleCoord, CounterStylePtr};
 use crate::gecko_bindings::structs::{StyleGridTrackBreadth, StyleShapeRadius};
 use crate::gecko_bindings::sugar::ns_style_coord::{CoordData, CoordDataMut, CoordDataValue};
-use crate::media_queries::Device;
 use crate::values::computed::basic_shape::ShapeRadius as ComputedShapeRadius;
 use crate::values::computed::{Angle, Length, LengthPercentage};
 use crate::values::computed::{Number, NumberOrPercentage, Percentage};
 use crate::values::generics::basic_shape::ShapeRadius;
 use crate::values::generics::box_::Perspective;
 use crate::values::generics::gecko::ScrollSnapPoint;
 use crate::values::generics::grid::{TrackBreadth, TrackKeyword};
 use crate::values::generics::length::LengthPercentageOrAuto;
@@ -382,26 +381,25 @@ pub fn round_border_to_device_pixels(wid
             au_per_device_px,
             Au(width.0 / au_per_device_px.0 * au_per_device_px.0),
         )
     }
 }
 
 impl CounterStyleOrNone {
     /// Convert this counter style to a Gecko CounterStylePtr.
-    pub fn to_gecko_value(self, gecko_value: &mut CounterStylePtr, device: &Device) {
+    pub fn to_gecko_value(self, gecko_value: &mut CounterStylePtr) {
         use crate::gecko_bindings::bindings::Gecko_SetCounterStyleToName as set_name;
         use crate::gecko_bindings::bindings::Gecko_SetCounterStyleToSymbols as set_symbols;
-        let pres_context = device.pres_context();
         match self {
             CounterStyleOrNone::None => unsafe {
-                set_name(gecko_value, atom!("none").into_addrefed(), pres_context);
+                set_name(gecko_value, atom!("none").into_addrefed());
             },
             CounterStyleOrNone::Name(name) => unsafe {
-                set_name(gecko_value, name.0.into_addrefed(), pres_context);
+                set_name(gecko_value, name.0.into_addrefed());
             },
             CounterStyleOrNone::Symbols(symbols_type, symbols) => {
                 let symbols: Vec<_> = symbols
                     .0
                     .iter()
                     .map(|symbol| match *symbol {
                         Symbol::String(ref s) => nsCStr::from(s),
                         Symbol::Ident(_) => unreachable!("Should not have identifier in symbols()"),
--- a/servo/components/style/properties/cascade.rs
+++ b/servo/components/style/properties/cascade.rs
@@ -740,23 +740,23 @@ impl<'a, 'b: 'a> Cascade<'a, 'b> {
                             // not skip cascading then.
                             _skip_font_family = true;
                         }
                     }
                 }
 
                 // FIXME(emilio): Why both setting the generic and passing it
                 // down?
-                let pres_context = self.context.builder.device.pres_context();
+                let doc = self.context.builder.device.document();
                 let gecko_font = self.context.builder.mutate_font().gecko_mut();
                 gecko_font.mGenericID = generic;
                 unsafe {
                     crate::gecko_bindings::bindings::Gecko_nsStyleFont_PrefillDefaultForGeneric(
                         gecko_font,
-                        pres_context,
+                        doc,
                         generic,
                     );
                 }
             }
         }
 
         // It is important that font-size is computed before the late
         // properties (for em units), but after font-family (for the
--- a/servo/components/style/properties/gecko.mako.rs
+++ b/servo/components/style/properties/gecko.mako.rs
@@ -30,17 +30,16 @@ use crate::gecko_bindings::bindings::Gec
 use crate::gecko_bindings::bindings::Gecko_SetCursorImageValue;
 use crate::gecko_bindings::bindings::Gecko_NewCSSShadowArray;
 use crate::gecko_bindings::bindings::Gecko_nsStyleFont_SetLang;
 use crate::gecko_bindings::bindings::Gecko_nsStyleFont_CopyLangFrom;
 use crate::gecko_bindings::bindings::Gecko_SetListStyleImageNone;
 use crate::gecko_bindings::bindings::Gecko_SetListStyleImageImageValue;
 use crate::gecko_bindings::bindings::Gecko_SetNullImageValue;
 use crate::gecko_bindings::bindings::{Gecko_ResetFilters, Gecko_CopyFiltersFrom};
-use crate::gecko_bindings::bindings::RawGeckoPresContextBorrowed;
 use crate::gecko_bindings::structs;
 use crate::gecko_bindings::structs::nsCSSPropertyID;
 use crate::gecko_bindings::structs::mozilla::PseudoStyleType;
 use crate::gecko_bindings::sugar::ns_style_coord::{CoordDataValue, CoordData, CoordDataMut};
 use crate::gecko_bindings::sugar::refptr::RefPtr;
 use crate::gecko::values::convert_nscolor_to_rgba;
 use crate::gecko::values::convert_rgba_to_nscolor;
 use crate::gecko::values::GeckoStyleCoordConvertible;
@@ -97,25 +96,25 @@ impl ComputedValues {
             rules,
             visited_style,
             % for style_struct in data.style_structs:
             ${style_struct.ident},
             % endfor
         ).to_outer(pseudo)
     }
 
-    pub fn default_values(pres_context: RawGeckoPresContextBorrowed) -> Arc<Self> {
+    pub fn default_values(doc: &structs::Document) -> Arc<Self> {
         ComputedValuesInner::new(
             /* custom_properties = */ None,
             /* writing_mode = */ WritingMode::empty(), // FIXME(bz): This seems dubious
             ComputedValueFlags::empty(),
             /* rules = */ None,
             /* visited_style = */ None,
             % for style_struct in data.style_structs:
-            style_structs::${style_struct.name}::default(pres_context),
+            style_structs::${style_struct.name}::default(doc),
             % endfor
         ).to_outer(None)
     }
 
     #[inline]
     pub fn pseudo(&self) -> Option<PseudoElement> {
         if self.0.mPseudoType == PseudoStyleType::NotPseudo {
             return None;
@@ -1242,21 +1241,23 @@ pub fn clone_transform_from_list(
 
 <%def name="impl_logical(name, **kwargs)">
     ${helpers.logical_setter(name)}
 </%def>
 
 <%def name="impl_style_struct(style_struct)">
 impl ${style_struct.gecko_struct_name} {
     #[allow(dead_code, unused_variables)]
-    pub fn default(pres_context: RawGeckoPresContextBorrowed) -> Arc<Self> {
+    pub fn default(document: &structs::Document) -> Arc<Self> {
         let mut result = Arc::new(${style_struct.gecko_struct_name} { gecko: unsafe { zeroed() } });
         unsafe {
-            Gecko_Construct_Default_${style_struct.gecko_ffi_name}(&mut Arc::get_mut(&mut result).unwrap().gecko,
-                                                                   pres_context);
+            Gecko_Construct_Default_${style_struct.gecko_ffi_name}(
+                &mut Arc::get_mut(&mut result).unwrap().gecko,
+                document,
+            );
         }
         result
     }
     pub fn get_gecko(&self) -> &${style_struct.gecko_ffi_name} {
         &self.gecko
     }
 }
 impl Drop for ${style_struct.gecko_struct_name} {
@@ -2194,17 +2195,17 @@ fn static_assert() {
     // Negative numbers are invalid at parse time, but <integer> is still an
     // i32.
     <% impl_font_settings("font_feature_settings", "gfxFontFeature", "FeatureTagValue", "i32", "u32") %>
     <% impl_font_settings("font_variation_settings", "gfxFontVariation", "VariationValue", "f32", "f32") %>
 
     pub fn fixup_none_generic(&mut self, device: &Device) {
         self.gecko.mFont.systemFont = false;
         unsafe {
-            bindings::Gecko_nsStyleFont_FixupNoneGeneric(&mut self.gecko, device.pres_context())
+            bindings::Gecko_nsStyleFont_FixupNoneGeneric(&mut self.gecko, device.document())
         }
     }
 
     pub fn fixup_system(&mut self, default_font_type: structs::FontFamilyType) {
         self.gecko.mFont.systemFont = true;
         self.gecko.mGenericID = structs::kGenericFont_NONE;
         self.gecko.mFont.fontlist.mDefaultFontType = default_font_type;
     }
@@ -2310,17 +2311,17 @@ fn static_assert() {
         } else {
             self.gecko.mSize = v.size().0;
             self.fixup_font_min_size(device);
             Some(Au(parent.gecko.mScriptUnconstrainedSize).into())
         }
     }
 
     pub fn fixup_font_min_size(&mut self, device: &Device) {
-        unsafe { bindings::Gecko_nsStyleFont_FixupMinFontSize(&mut self.gecko, device.pres_context()) }
+        unsafe { bindings::Gecko_nsStyleFont_FixupMinFontSize(&mut self.gecko, device.document()) }
     }
 
     pub fn apply_unconstrained_font_size(&mut self, v: NonNegativeLength) {
         self.gecko.mScriptUnconstrainedSize = v.0.to_i32_au();
     }
 
     /// Calculates the constrained and unconstrained font sizes to be inherited
     /// from the parent.
@@ -2632,19 +2633,21 @@ fn static_assert() {
     #[allow(non_snake_case)]
     pub fn clone__x_text_zoom(&self) -> longhands::_x_text_zoom::computed_value::T {
         longhands::_x_text_zoom::computed_value::T(self.gecko.mAllowZoom)
     }
 
     ${impl_simple("_moz_script_level", "mScriptLevel")}
     <% impl_simple_type_with_conversion("font_language_override", "mFont.languageOverride") %>
 
-    pub fn set_font_variant_alternates(&mut self,
-                                       v: values::computed::font::FontVariantAlternates,
-                                       device: &Device) {
+    pub fn set_font_variant_alternates(
+        &mut self,
+        v: values::computed::font::FontVariantAlternates,
+        device: &Device,
+    ) {
         use crate::gecko_bindings::bindings::{Gecko_ClearAlternateValues, Gecko_AppendAlternateValues};
         use crate::gecko_bindings::bindings::Gecko_nsFont_ResetFontFeatureValuesLookup;
         use crate::gecko_bindings::bindings::Gecko_nsFont_SetFontFeatureValuesLookup;
         % for value in "normal swash stylistic ornaments annotation styleset character_variant historical".split():
             use crate::gecko_bindings::structs::NS_FONT_VARIANT_ALTERNATES_${value.upper()};
         % endfor
         use crate::values::specified::font::VariantAlternates;
 
@@ -3948,22 +3951,22 @@ fn static_assert() {
         }
 
         unsafe {
             let ref gecko_image_request = *self.gecko.mListStyleImage.mRawPtr;
             UrlOrNone::Url(ComputedImageUrl::from_image_request(gecko_image_request))
         }
     }
 
-    pub fn set_list_style_type(&mut self, v: longhands::list_style_type::computed_value::T, device: &Device) {
+    pub fn set_list_style_type(&mut self, v: longhands::list_style_type::computed_value::T) {
         use crate::gecko_bindings::bindings::Gecko_SetCounterStyleToString;
         use nsstring::{nsACString, nsCStr};
         use self::longhands::list_style_type::computed_value::T;
         match v {
-            T::CounterStyle(s) => s.to_gecko_value(&mut self.gecko.mCounterStyle, device),
+            T::CounterStyle(s) => s.to_gecko_value(&mut self.gecko.mCounterStyle),
             T::String(s) => unsafe {
                 Gecko_SetCounterStyleToString(&mut self.gecko.mCounterStyle,
                                               &nsCStr::from(&s) as &nsACString)
             }
         }
     }
 
     pub fn copy_list_style_type_from(&mut self, other: &Self) {
@@ -5195,17 +5198,17 @@ clip-path
 </%self:impl_trait>
 
 <%self:impl_trait style_struct_name="Counters"
                   skip_longhands="content counter-increment counter-reset">
     pub fn ineffective_content_property(&self) -> bool {
         self.gecko.mContents.is_empty()
     }
 
-    pub fn set_content(&mut self, v: longhands::content::computed_value::T, device: &Device) {
+    pub fn set_content(&mut self, v: longhands::content::computed_value::T) {
         use crate::values::CustomIdent;
         use crate::values::generics::counters::{Content, ContentItem};
         use crate::values::generics::CounterStyleOrNone;
         use crate::gecko_bindings::structs::nsStyleContentData;
         use crate::gecko_bindings::structs::nsStyleContentAttr;
         use crate::gecko_bindings::structs::StyleContentType;
         use crate::gecko_bindings::bindings::Gecko_ClearAndResizeStyleContents;
 
@@ -5220,28 +5223,27 @@ clip-path
         }
 
         fn set_counter_function(
             data: &mut nsStyleContentData,
             content_type: StyleContentType,
             name: &CustomIdent,
             sep: &str,
             style: CounterStyleOrNone,
-            device: &Device,
         ) {
             debug_assert!(content_type == StyleContentType::Counter ||
                           content_type == StyleContentType::Counters);
             let counter_func = unsafe {
                 bindings::Gecko_SetCounterFunction(data, content_type).as_mut().unwrap()
             };
             counter_func.mIdent.assign(name.0.as_slice());
             if content_type == StyleContentType::Counters {
                 counter_func.mSeparator.assign_str(sep);
             }
-            style.to_gecko_value(&mut counter_func.mCounterStyle, device);
+            style.to_gecko_value(&mut counter_func.mCounterStyle);
         }
 
         match v {
             Content::None |
             Content::Normal => {
                 // Ensure destructors run, otherwise we could leak.
                 if !self.gecko.mContents.is_empty() {
                     unsafe {
@@ -5306,27 +5308,25 @@ clip-path
                             => self.gecko.mContents[i].mType = StyleContentType::NoCloseQuote,
                         ContentItem::Counter(ref name, ref style) => {
                             set_counter_function(
                                 &mut self.gecko.mContents[i],
                                 StyleContentType::Counter,
                                 &name,
                                 "",
                                 style.clone(),
-                                device,
                             );
                         }
                         ContentItem::Counters(ref name, ref sep, ref style) => {
                             set_counter_function(
                                 &mut self.gecko.mContents[i],
                                 StyleContentType::Counters,
                                 &name,
                                 &sep,
                                 style.clone(),
-                                device,
                             );
                         }
                         ContentItem::Url(ref url) => {
                             unsafe {
                                 bindings::Gecko_SetContentDataImageValue(
                                     &mut self.gecko.mContents[i],
                                     url.url_value_ptr(),
                                 )
--- a/servo/components/style/properties/longhands/color.mako.rs
+++ b/servo/components/style/properties/longhands/color.mako.rs
@@ -93,17 +93,17 @@ pub mod system_colors {
     impl ToComputedValue for SystemColor {
         type ComputedValue = u32; // nscolor
 
         #[inline]
         fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue {
             unsafe {
                 Gecko_GetLookAndFeelSystemColor(
                     *self as i32,
-                    cx.device().pres_context(),
+                    cx.device().document(),
                 )
             }
         }
 
         #[inline]
         fn from_computed_value(_: &Self::ComputedValue) -> Self {
             unreachable!()
         }
--- a/servo/components/style/properties/longhands/font.mako.rs
+++ b/servo/components/style/properties/longhands/font.mako.rs
@@ -391,17 +391,17 @@ https://drafts.csswg.org/css-fonts-4/#lo
                 };
 
                 let mut system: nsFont = unsafe { mem::uninitialized() };
                 unsafe {
                     bindings::Gecko_nsFont_InitSystem(
                         &mut system,
                         id as i32,
                         cx.style().get_font().gecko(),
-                        cx.device().pres_context()
+                        cx.device().document()
                     )
                 }
                 let font_weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight);
                 let font_stretch = FontStretch(NonNegative(Percentage(unsafe {
                     bindings::Gecko_FontStretch_ToFloat(system.stretch)
                 })));
                 let font_style = FontStyle::from_gecko(system.style);
                 let ret = ComputedSystemFont {
--- a/servo/components/style/properties/properties.mako.rs
+++ b/servo/components/style/properties/properties.mako.rs
@@ -3502,17 +3502,17 @@ impl<'a> StyleBuilder<'a> {
     pub fn set_${property.ident}(
         &mut self,
         value: longhands::${property.ident}::computed_value::T
     ) {
         % if not property.style_struct.inherited:
         self.modified_reset = true;
         % endif
 
-        <% props_need_device = ["content", "list_style_type", "font_variant_alternates"] %>
+        <% props_need_device = ["font_variant_alternates"] %>
         self.${property.style_struct.ident}.mutate()
             .set_${property.ident}(
                 value,
                 % if property.logical:
                 self.writing_mode,
                 % elif product == "gecko" and property.ident in props_need_device:
                 self.device,
                 % endif
--- a/servo/components/style/stylesheets/document_rule.rs
+++ b/servo/components/style/stylesheets/document_rule.rs
@@ -193,17 +193,17 @@ impl DocumentMatchingFunction {
             DocumentMatchingFunction::Regexp(ref pat) => pat,
             DocumentMatchingFunction::MediaDocument(kind) => match kind {
                 MediaDocumentKind::All => "all",
                 MediaDocumentKind::Image => "image",
                 MediaDocumentKind::Plugin => "plugin",
                 MediaDocumentKind::Video => "video",
             },
         });
-        unsafe { Gecko_DocumentRule_UseForPresentation(device.pres_context(), &*pattern, func) }
+        unsafe { Gecko_DocumentRule_UseForPresentation(device.document(), &*pattern, func) }
     }
 
     #[cfg(not(feature = "gecko"))]
     /// Evaluate a URL matching function.
     pub fn evaluate(&self, _: &Device) -> bool {
         false
     }
 }