Bug 1376931 Part 3: Change Gecko media queries of resolution to compare in dppx units without unit conversion. r=heycam
authorBrad Werth <bwerth@mozilla.com>
Wed, 27 Sep 2017 16:53:27 -0700
changeset 427235 06c8706c06c9b9cafd87e3f445baee8ef2718a76
parent 427234 b04ebc4bcd3d59bee31d363ac34b55bc8e71c8f7
child 427236 914b4cd64a4a99f6dc94728020bb42246fa68445
push id97
push userfmarier@mozilla.com
push dateSat, 14 Oct 2017 01:12:59 +0000
reviewersheycam
bugs1376931
milestone58.0a1
Bug 1376931 Part 3: Change Gecko media queries of resolution to compare in dppx units without unit conversion. r=heycam MozReview-Commit-ID: DPdz1Tmv24R
layout/style/nsMediaFeatures.cpp
layout/style/nsMediaList.cpp
--- a/layout/style/nsMediaFeatures.cpp
+++ b/layout/style/nsMediaFeatures.cpp
@@ -274,26 +274,34 @@ GetMonochrome(nsPresContext* aPresContex
   // 0!
   aResult.SetIntValue(0, eCSSUnit_Integer);
 }
 
 static void
 GetResolution(nsPresContext* aPresContext, const nsMediaFeature*,
               nsCSSValue& aResult)
 {
-  float dpi = 96; // Use 96 when resisting fingerprinting.
+  // 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.
+
+  float dppx;
 
   if (!ShouldResistFingerprinting(aPresContext)) {
-    // Resolution measures device pixels per CSS (inch/cm/pixel).  We
-    // return it in device pixels per CSS inches.
-    dpi = float(nsPresContext::AppUnitsPerCSSInch()) /
-          float(aPresContext->AppUnitsPerDevPixel());
+    // Get the actual device pixel ratio, which also takes zoom into account.
+    dppx = float(nsPresContext::AppUnitsPerCSSPixel()) /
+             aPresContext->AppUnitsPerDevPixel();
+  } else {
+    // We are resisting fingerprinting, so pretend we have a device pixel ratio
+    // of 1. In that case, we simply report the zoom level.
+    dppx = aPresContext->GetDeviceFullZoom();
   }
 
-  aResult.SetFloatValue(dpi, eCSSUnit_Inch);
+  aResult.SetFloatValue(dppx, eCSSUnit_Pixel);
 }
 
 static void
 GetScan(nsPresContext* aPresContext, const nsMediaFeature*,
         nsCSSValue& aResult)
 {
   // Since Gecko doesn't support the 'tv' media type, the 'scan'
   // feature is never present.
--- a/layout/style/nsMediaList.cpp
+++ b/layout/style/nsMediaList.cpp
@@ -124,33 +124,33 @@ nsMediaExpression::Matches(nsPresContext
         NS_ASSERTION(actual.GetUnit() == eCSSUnit_Inch ||
                      actual.GetUnit() == eCSSUnit_Pixel ||
                      actual.GetUnit() == eCSSUnit_Centimeter,
                      "bad actual value");
         NS_ASSERTION(required.GetUnit() == eCSSUnit_Inch ||
                      required.GetUnit() == eCSSUnit_Pixel ||
                      required.GetUnit() == eCSSUnit_Centimeter,
                      "bad required value");
-        float actualDPI = actual.GetFloatValue();
+        float actualDPPX = actual.GetFloatValue();
         float overrideDPPX = aPresContext->GetOverrideDPPX();
 
         if (overrideDPPX > 0) {
-          actualDPI = overrideDPPX * 96.0f;
+          actualDPPX = overrideDPPX;
         } else if (actual.GetUnit() == eCSSUnit_Centimeter) {
-          actualDPI = actualDPI * 2.54f;
-        } else if (actual.GetUnit() == eCSSUnit_Pixel) {
-          actualDPI = actualDPI * 96.0f;
+          actualDPPX = actualDPPX * 2.54f / 96.0f;
+        } else if (actual.GetUnit() == eCSSUnit_Inch) {
+          actualDPPX = actualDPPX / 96.0f;
         }
-        float requiredDPI = required.GetFloatValue();
+        float requiredDPPX = required.GetFloatValue();
         if (required.GetUnit() == eCSSUnit_Centimeter) {
-          requiredDPI = requiredDPI * 2.54f;
-        } else if (required.GetUnit() == eCSSUnit_Pixel) {
-          requiredDPI = requiredDPI * 96.0f;
+          requiredDPPX = requiredDPPX * 2.54f / 96.0f;
+        } else if (required.GetUnit() == eCSSUnit_Inch) {
+          requiredDPPX = requiredDPPX / 96.0f;
         }
-        cmp = DoCompare(actualDPI, requiredDPI);
+        cmp = DoCompare(actualDPPX, requiredDPPX);
       }
       break;
     case nsMediaFeature::eEnumerated:
       {
         NS_ASSERTION(actual.GetUnit() == eCSSUnit_Enumerated,
                      "bad actual value");
         NS_ASSERTION(required.GetUnit() == eCSSUnit_Enumerated,
                      "bad required value");