Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / nsScreen.cpp
blobfc832fd2cfc88b6199bab2c773e5a84af6f10c6b
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 #include "nsContentUtils.h"
8 #include "nsScreen.h"
9 #include "mozilla/dom/Document.h"
10 #include "nsGlobalWindowInner.h"
11 #include "nsGlobalWindowOuter.h"
12 #include "nsIDocShell.h"
13 #include "nsPresContext.h"
14 #include "nsCOMPtr.h"
15 #include "nsIDocShellTreeItem.h"
16 #include "nsLayoutUtils.h"
17 #include "nsDeviceContext.h"
18 #include "mozilla/widget/ScreenManager.h"
20 using namespace mozilla;
21 using namespace mozilla::dom;
23 nsScreen::nsScreen(nsPIDOMWindowInner* aWindow)
24 : DOMEventTargetHelper(aWindow),
25 mScreenOrientation(new ScreenOrientation(aWindow, this)) {}
27 nsScreen::~nsScreen() = default;
29 // QueryInterface implementation for nsScreen
30 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsScreen)
31 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
33 NS_IMPL_ADDREF_INHERITED(nsScreen, DOMEventTargetHelper)
34 NS_IMPL_RELEASE_INHERITED(nsScreen, DOMEventTargetHelper)
36 NS_IMPL_CYCLE_COLLECTION_INHERITED(nsScreen, DOMEventTargetHelper,
37 mScreenOrientation)
39 int32_t nsScreen::PixelDepth() {
40 // Return 24 to prevent fingerprinting.
41 if (ShouldResistFingerprinting(RFPTarget::ScreenPixelDepth)) {
42 return 24;
44 nsDeviceContext* context = GetDeviceContext();
45 if (NS_WARN_IF(!context)) {
46 return 24;
48 return context->GetDepth();
51 nsPIDOMWindowOuter* nsScreen::GetOuter() const {
52 if (nsPIDOMWindowInner* inner = GetOwner()) {
53 return inner->GetOuterWindow();
55 return nullptr;
58 nsDeviceContext* nsScreen::GetDeviceContext() const {
59 return nsLayoutUtils::GetDeviceContextForScreenInfo(GetOuter());
62 CSSIntRect nsScreen::GetRect() {
63 // Return window inner rect to prevent fingerprinting.
64 if (ShouldResistFingerprinting(RFPTarget::ScreenRect)) {
65 return GetWindowInnerRect();
68 // Here we manipulate the value of aRect to represent the screen size,
69 // if in RDM.
70 if (nsPIDOMWindowInner* owner = GetOwner()) {
71 if (Document* doc = owner->GetExtantDoc()) {
72 Maybe<CSSIntSize> deviceSize =
73 nsGlobalWindowOuter::GetRDMDeviceSize(*doc);
74 if (deviceSize.isSome()) {
75 const CSSIntSize& size = deviceSize.value();
76 return {0, 0, size.width, size.height};
81 nsDeviceContext* context = GetDeviceContext();
82 if (NS_WARN_IF(!context)) {
83 return {};
86 nsRect r;
87 context->GetRect(r);
88 return CSSIntRect::FromAppUnitsRounded(r);
91 CSSIntRect nsScreen::GetAvailRect() {
92 // Return window inner rect to prevent fingerprinting.
93 if (ShouldResistFingerprinting(RFPTarget::ScreenAvailRect)) {
94 return GetWindowInnerRect();
97 // Here we manipulate the value of aRect to represent the screen size,
98 // if in RDM.
99 if (nsPIDOMWindowInner* owner = GetOwner()) {
100 if (Document* doc = owner->GetExtantDoc()) {
101 Maybe<CSSIntSize> deviceSize =
102 nsGlobalWindowOuter::GetRDMDeviceSize(*doc);
103 if (deviceSize.isSome()) {
104 const CSSIntSize& size = deviceSize.value();
105 return {0, 0, size.width, size.height};
110 nsDeviceContext* context = GetDeviceContext();
111 if (NS_WARN_IF(!context)) {
112 return {};
115 nsRect r;
116 context->GetClientRect(r);
117 return CSSIntRect::FromAppUnitsRounded(r);
120 uint16_t nsScreen::GetOrientationAngle() const {
121 nsDeviceContext* context = GetDeviceContext();
122 if (context) {
123 return context->GetScreenOrientationAngle();
125 RefPtr<widget::Screen> s =
126 widget::ScreenManager::GetSingleton().GetPrimaryScreen();
127 return s->GetOrientationAngle();
130 hal::ScreenOrientation nsScreen::GetOrientationType() const {
131 nsDeviceContext* context = GetDeviceContext();
132 if (context) {
133 return context->GetScreenOrientationType();
135 RefPtr<widget::Screen> s =
136 widget::ScreenManager::GetSingleton().GetPrimaryScreen();
137 return s->GetOrientationType();
140 ScreenOrientation* nsScreen::Orientation() const { return mScreenOrientation; }
142 void nsScreen::GetMozOrientation(nsString& aOrientation,
143 CallerType aCallerType) const {
144 switch (mScreenOrientation->DeviceType(aCallerType)) {
145 case OrientationType::Portrait_primary:
146 aOrientation.AssignLiteral("portrait-primary");
147 break;
148 case OrientationType::Portrait_secondary:
149 aOrientation.AssignLiteral("portrait-secondary");
150 break;
151 case OrientationType::Landscape_primary:
152 aOrientation.AssignLiteral("landscape-primary");
153 break;
154 case OrientationType::Landscape_secondary:
155 aOrientation.AssignLiteral("landscape-secondary");
156 break;
157 default:
158 MOZ_CRASH("Unacceptable screen orientation type.");
162 /* virtual */
163 JSObject* nsScreen::WrapObject(JSContext* aCx,
164 JS::Handle<JSObject*> aGivenProto) {
165 return Screen_Binding::Wrap(aCx, this, aGivenProto);
168 CSSIntRect nsScreen::GetWindowInnerRect() {
169 nsCOMPtr<nsPIDOMWindowInner> win = GetOwner();
170 if (!win) {
171 return {};
173 double width;
174 double height;
175 if (NS_FAILED(win->GetInnerWidth(&width)) ||
176 NS_FAILED(win->GetInnerHeight(&height))) {
177 return {};
179 return {0, 0, int32_t(std::round(width)), int32_t(std::round(height))};
182 bool nsScreen::ShouldResistFingerprinting(RFPTarget aTarget) const {
183 nsCOMPtr<nsPIDOMWindowInner> owner = GetOwner();
184 return owner &&
185 nsGlobalWindowInner::Cast(owner)->ShouldResistFingerprinting(aTarget);