1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* the features that media queries can test */
10 #include "nsStyleConsts.h"
11 #include "nsPresContext.h"
12 #include "nsCSSProps.h"
13 #include "nsCSSValue.h"
14 #include "mozilla/LookAndFeel.h"
15 #include "nsDeviceContext.h"
16 #include "nsIBaseWindow.h"
17 #include "nsIDocShell.h"
18 #include "nsIPrintSettings.h"
19 #include "mozilla/dom/Document.h"
20 #include "mozilla/dom/DocumentInlines.h"
21 #include "mozilla/dom/BrowsingContextBinding.h"
22 #include "mozilla/dom/ScreenBinding.h"
23 #include "nsIWidget.h"
24 #include "nsContentUtils.h"
25 #include "mozilla/RelativeLuminanceUtils.h"
26 #include "mozilla/StaticPrefs_browser.h"
27 #include "mozilla/StyleSheet.h"
28 #include "mozilla/StyleSheetInlines.h"
29 #include "mozilla/GeckoBindings.h"
30 #include "PreferenceSheet.h"
31 #include "nsGlobalWindowOuter.h"
33 # include "mozilla/WindowsVersion.h"
36 using namespace mozilla
;
37 using mozilla::dom::DisplayMode
;
38 using mozilla::dom::Document
;
40 // A helper for four features below
41 static nsSize
GetSize(const Document
& aDocument
) {
42 nsPresContext
* pc
= aDocument
.GetPresContext();
44 // Per spec, return a 0x0 viewport if we're not being rendered. See:
46 // * https://github.com/w3c/csswg-drafts/issues/571
47 // * https://github.com/whatwg/html/issues/1813
53 if (pc
->IsRootPaginatedDocument()) {
54 // We want the page size, including unprintable areas and margins.
56 // FIXME(emilio, bug 1414600): Not quite!
57 return pc
->GetPageSize();
60 return pc
->GetVisibleArea().Size();
63 // A helper for three features below.
64 static nsSize
GetDeviceSize(const Document
& aDocument
) {
65 if (aDocument
.ShouldResistFingerprinting(RFPTarget::CSSDeviceSize
)) {
66 return GetSize(aDocument
);
69 // Media queries in documents in an RDM pane should use the simulated
71 Maybe
<CSSIntSize
> deviceSize
=
72 nsGlobalWindowOuter::GetRDMDeviceSize(aDocument
);
73 if (deviceSize
.isSome()) {
74 return CSSPixel::ToAppUnits(deviceSize
.value());
77 nsPresContext
* pc
= aDocument
.GetPresContext();
78 // NOTE(emilio): We should probably figure out how to return an appropriate
79 // device size here, though in a multi-screen world that makes no sense
85 if (pc
->IsRootPaginatedDocument()) {
86 // We want the page size, including unprintable areas and margins.
87 // XXX The spec actually says we want the "page sheet size", but
88 // how is that different?
89 return pc
->GetPageSize();
93 pc
->DeviceContext()->GetDeviceSurfaceDimensions(size
.width
, size
.height
);
97 bool Gecko_MediaFeatures_IsResourceDocument(const Document
* aDocument
) {
98 return aDocument
->IsResourceDoc();
101 bool Gecko_MediaFeatures_ShouldAvoidNativeTheme(const Document
* aDocument
) {
102 return aDocument
->ShouldAvoidNativeTheme();
105 bool Gecko_MediaFeatures_UseOverlayScrollbars(const Document
* aDocument
) {
106 nsPresContext
* pc
= aDocument
->GetPresContext();
107 return pc
&& pc
->UseOverlayScrollbars();
110 static nsDeviceContext
* GetDeviceContextFor(const Document
* aDocument
) {
111 nsPresContext
* pc
= aDocument
->GetPresContext();
116 // It would be nice to call nsLayoutUtils::GetDeviceContextForScreenInfo here,
117 // except for two things: (1) it can flush, and flushing is bad here, and (2)
118 // it doesn't really get us consistency in multi-monitor situations *anyway*.
119 return pc
->DeviceContext();
122 void Gecko_MediaFeatures_GetDeviceSize(const Document
* aDocument
,
123 nscoord
* aWidth
, nscoord
* aHeight
) {
124 nsSize size
= GetDeviceSize(*aDocument
);
125 *aWidth
= size
.width
;
126 *aHeight
= size
.height
;
129 int32_t Gecko_MediaFeatures_GetMonochromeBitsPerPixel(
130 const Document
* aDocument
) {
131 // The default bits per pixel for a monochrome device. We could propagate this
132 // further to nsIPrintSettings, but Gecko doesn't actually know this value
133 // from the hardware, so it seems silly to do so.
134 static constexpr int32_t kDefaultMonochromeBpp
= 8;
136 nsPresContext
* pc
= aDocument
->GetPresContext();
140 nsIPrintSettings
* ps
= pc
->GetPrintSettings();
145 ps
->GetPrintInColor(&color
);
146 return color
? 0 : kDefaultMonochromeBpp
;
149 dom::ScreenColorGamut
Gecko_MediaFeatures_ColorGamut(
150 const Document
* aDocument
) {
151 auto colorGamut
= dom::ScreenColorGamut::Srgb
;
152 if (!aDocument
->ShouldResistFingerprinting(RFPTarget::CSSColorInfo
)) {
153 if (auto* dx
= GetDeviceContextFor(aDocument
)) {
154 colorGamut
= dx
->GetColorGamut();
160 int32_t Gecko_MediaFeatures_GetColorDepth(const Document
* aDocument
) {
161 if (Gecko_MediaFeatures_GetMonochromeBitsPerPixel(aDocument
) != 0) {
162 // If we're a monochrome device, then the color depth is zero.
166 // Use depth of 24 when resisting fingerprinting, or when we're not being
170 if (!aDocument
->ShouldResistFingerprinting(RFPTarget::CSSColorInfo
)) {
171 if (nsDeviceContext
* dx
= GetDeviceContextFor(aDocument
)) {
172 depth
= dx
->GetDepth();
176 // The spec says to use bits *per color component*, so divide by 3,
177 // and round down, since the spec says to use the smallest when the
178 // color components differ.
182 float Gecko_MediaFeatures_GetResolution(const Document
* aDocument
) {
183 // We're returning resolution in terms of device pixels per css pixel, since
184 // that is the preferred unit for media queries of resolution. This avoids
185 // introducing precision error from conversion to and from less-used
186 // physical units like inches.
187 nsPresContext
* pc
= aDocument
->GetPresContext();
192 if (pc
->GetOverrideDPPX() > 0.) {
193 return pc
->GetOverrideDPPX();
196 if (aDocument
->ShouldResistFingerprinting(RFPTarget::CSSResolution
)) {
197 return pc
->DeviceContext()->GetFullZoom();
199 // Get the actual device pixel ratio, which also takes zoom into account.
200 return float(AppUnitsPerCSSPixel()) /
201 pc
->DeviceContext()->AppUnitsPerDevPixel();
204 static const Document
* TopDocument(const Document
* aDocument
) {
205 const Document
* current
= aDocument
;
206 while (const Document
* parent
= current
->GetInProcessParentDocument()) {
212 StyleDisplayMode
Gecko_MediaFeatures_GetDisplayMode(const Document
* aDocument
) {
213 const Document
* rootDocument
= TopDocument(aDocument
);
215 nsCOMPtr
<nsISupports
> container
= rootDocument
->GetContainer();
216 if (nsCOMPtr
<nsIBaseWindow
> baseWindow
= do_QueryInterface(container
)) {
217 nsCOMPtr
<nsIWidget
> mainWidget
;
218 baseWindow
->GetMainWidget(getter_AddRefs(mainWidget
));
219 if (mainWidget
&& mainWidget
->SizeMode() == nsSizeMode_Fullscreen
) {
220 return StyleDisplayMode::Fullscreen
;
224 static_assert(static_cast<int32_t>(DisplayMode::Browser
) ==
225 static_cast<int32_t>(StyleDisplayMode::Browser
) &&
226 static_cast<int32_t>(DisplayMode::Minimal_ui
) ==
227 static_cast<int32_t>(StyleDisplayMode::MinimalUi
) &&
228 static_cast<int32_t>(DisplayMode::Standalone
) ==
229 static_cast<int32_t>(StyleDisplayMode::Standalone
) &&
230 static_cast<int32_t>(DisplayMode::Fullscreen
) ==
231 static_cast<int32_t>(StyleDisplayMode::Fullscreen
),
232 "DisplayMode must mach nsStyleConsts.h");
234 dom::BrowsingContext
* browsingContext
= aDocument
->GetBrowsingContext();
235 if (!browsingContext
) {
236 return StyleDisplayMode::Browser
;
238 return static_cast<StyleDisplayMode
>(browsingContext
->DisplayMode());
241 bool Gecko_MediaFeatures_MatchesPlatform(StylePlatform aPlatform
) {
244 case StylePlatform::Windows
:
246 #elif defined(ANDROID)
247 case StylePlatform::Android
:
249 #elif defined(MOZ_WIDGET_GTK)
250 case StylePlatform::Linux
:
252 #elif defined(XP_MACOSX)
253 case StylePlatform::Macos
:
256 # error "Unknown platform?"
263 bool Gecko_MediaFeatures_PrefersReducedMotion(const Document
* aDocument
) {
264 if (aDocument
->ShouldResistFingerprinting(
265 RFPTarget::CSSPrefersReducedMotion
)) {
268 return LookAndFeel::GetInt(LookAndFeel::IntID::PrefersReducedMotion
, 0) == 1;
271 bool Gecko_MediaFeatures_PrefersReducedTransparency(const Document
* aDocument
) {
272 if (aDocument
->ShouldResistFingerprinting(
273 RFPTarget::CSSPrefersReducedTransparency
)) {
276 return LookAndFeel::GetInt(LookAndFeel::IntID::PrefersReducedTransparency
,
280 StylePrefersColorScheme
Gecko_MediaFeatures_PrefersColorScheme(
281 const Document
* aDocument
, bool aUseContent
) {
282 auto scheme
= aUseContent
? LookAndFeel::PreferredColorSchemeForContent()
283 : aDocument
->PreferredColorScheme();
284 return scheme
== ColorScheme::Dark
? StylePrefersColorScheme::Dark
285 : StylePrefersColorScheme::Light
;
288 // Neither Linux, Windows, nor Mac have a way to indicate that low contrast is
289 // preferred so we use the presence of an accessibility theme or forced colors
291 StylePrefersContrast
Gecko_MediaFeatures_PrefersContrast(
292 const Document
* aDocument
) {
293 if (aDocument
->ShouldResistFingerprinting(RFPTarget::CSSPrefersContrast
)) {
294 return StylePrefersContrast::NoPreference
;
296 const auto& prefs
= PreferenceSheet::PrefsFor(*aDocument
);
297 if (!prefs
.mUseAccessibilityTheme
&& prefs
.mUseDocumentColors
) {
298 return StylePrefersContrast::NoPreference
;
300 const auto& colors
= prefs
.ColorsFor(ColorScheme::Light
);
301 float ratio
= RelativeLuminanceUtils::ContrastRatio(colors
.mDefaultBackground
,
303 // https://www.w3.org/TR/WCAG21/#contrast-minimum
305 return StylePrefersContrast::Less
;
307 // https://www.w3.org/TR/WCAG21/#contrast-enhanced
309 return StylePrefersContrast::More
;
311 return StylePrefersContrast::Custom
;
314 bool Gecko_MediaFeatures_InvertedColors(const Document
* aDocument
) {
315 if (aDocument
->ShouldResistFingerprinting(RFPTarget::CSSInvertedColors
)) {
318 return LookAndFeel::GetInt(LookAndFeel::IntID::InvertedColors
, 0) == 1;
321 StyleScripting
Gecko_MediaFeatures_Scripting(const Document
* aDocument
) {
322 const auto* doc
= aDocument
;
323 if (aDocument
->IsStaticDocument()) {
324 doc
= aDocument
->GetOriginalDocument();
327 return doc
->IsScriptEnabled() ? StyleScripting::Enabled
328 : StyleScripting::None
;
331 StyleDynamicRange
Gecko_MediaFeatures_DynamicRange(const Document
* aDocument
) {
332 // Bug 1759772: Once HDR color is available, update each platform
333 // LookAndFeel implementation to return StyleDynamicRange::High when
335 return StyleDynamicRange::Standard
;
338 StyleDynamicRange
Gecko_MediaFeatures_VideoDynamicRange(
339 const Document
* aDocument
) {
340 if (aDocument
->ShouldResistFingerprinting(RFPTarget::CSSVideoDynamicRange
)) {
341 return StyleDynamicRange::Standard
;
343 // video-dynamic-range: high has 3 requirements:
344 // 1) high peak brightness
345 // 2) high contrast ratio
346 // 3) color depth > 24
347 // We check the color depth requirement before asking the LookAndFeel
348 // if it is HDR capable.
349 if (nsDeviceContext
* dx
= GetDeviceContextFor(aDocument
)) {
350 if (dx
->GetDepth() > 24 &&
351 LookAndFeel::GetInt(LookAndFeel::IntID::VideoDynamicRange
)) {
352 return StyleDynamicRange::High
;
356 return StyleDynamicRange::Standard
;
359 static PointerCapabilities
GetPointerCapabilities(const Document
* aDocument
,
360 LookAndFeel::IntID aID
) {
361 MOZ_ASSERT(aID
== LookAndFeel::IntID::PrimaryPointerCapabilities
||
362 aID
== LookAndFeel::IntID::AllPointerCapabilities
);
363 MOZ_ASSERT(aDocument
);
365 if (dom::BrowsingContext
* bc
= aDocument
->GetBrowsingContext()) {
366 // The touch-events-override happens only for the Responsive Design Mode so
367 // that we don't need to care about ResistFingerprinting.
368 if (bc
->TouchEventsOverride() == dom::TouchEventsOverride::Enabled
) {
369 return PointerCapabilities::Coarse
;
373 // The default value for Desktop is mouse-type pointer, and for Android
375 const PointerCapabilities kDefaultCapabilities
=
377 PointerCapabilities::Coarse
;
379 PointerCapabilities::Fine
| PointerCapabilities::Hover
;
381 if (aDocument
->ShouldResistFingerprinting(
382 RFPTarget::CSSPointerCapabilities
)) {
383 return kDefaultCapabilities
;
387 nsresult rv
= LookAndFeel::GetInt(aID
, &intValue
);
389 return kDefaultCapabilities
;
392 return static_cast<PointerCapabilities
>(intValue
);
395 PointerCapabilities
Gecko_MediaFeatures_PrimaryPointerCapabilities(
396 const Document
* aDocument
) {
397 return GetPointerCapabilities(aDocument
,
398 LookAndFeel::IntID::PrimaryPointerCapabilities
);
401 PointerCapabilities
Gecko_MediaFeatures_AllPointerCapabilities(
402 const Document
* aDocument
) {
403 return GetPointerCapabilities(aDocument
,
404 LookAndFeel::IntID::AllPointerCapabilities
);