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"
9 #include "mozilla/dom/Document.h"
10 #include "nsGlobalWindowInner.h"
11 #include "nsGlobalWindowOuter.h"
12 #include "nsIDocShell.h"
13 #include "nsPresContext.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
,
39 int32_t nsScreen::PixelDepth() {
40 // Return 24 to prevent fingerprinting.
41 if (ShouldResistFingerprinting(RFPTarget::ScreenPixelDepth
)) {
44 nsDeviceContext
* context
= GetDeviceContext();
45 if (NS_WARN_IF(!context
)) {
48 return context
->GetDepth();
51 nsPIDOMWindowOuter
* nsScreen::GetOuter() const {
52 if (nsPIDOMWindowInner
* inner
= GetOwnerWindow()) {
53 return inner
->GetOuterWindow();
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 GetTopWindowInnerRectForRFP();
68 // Here we manipulate the value of aRect to represent the screen size,
70 if (nsPIDOMWindowInner
* owner
= GetOwnerWindow()) {
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
)) {
85 return CSSIntRect::FromAppUnitsRounded(context
->GetRect());
88 CSSIntRect
nsScreen::GetAvailRect() {
89 // Return window inner rect to prevent fingerprinting.
90 if (ShouldResistFingerprinting(RFPTarget::ScreenAvailRect
)) {
91 return GetTopWindowInnerRectForRFP();
94 // Here we manipulate the value of aRect to represent the screen size,
96 if (nsPIDOMWindowInner
* owner
= GetOwnerWindow()) {
97 if (Document
* doc
= owner
->GetExtantDoc()) {
98 Maybe
<CSSIntSize
> deviceSize
=
99 nsGlobalWindowOuter::GetRDMDeviceSize(*doc
);
100 if (deviceSize
.isSome()) {
101 const CSSIntSize
& size
= deviceSize
.value();
102 return {0, 0, size
.width
, size
.height
};
107 nsDeviceContext
* context
= GetDeviceContext();
108 if (NS_WARN_IF(!context
)) {
111 return CSSIntRect::FromAppUnitsRounded(context
->GetClientRect());
114 uint16_t nsScreen::GetOrientationAngle() const {
115 nsDeviceContext
* context
= GetDeviceContext();
117 return context
->GetScreenOrientationAngle();
119 RefPtr
<widget::Screen
> s
=
120 widget::ScreenManager::GetSingleton().GetPrimaryScreen();
121 return s
->GetOrientationAngle();
124 hal::ScreenOrientation
nsScreen::GetOrientationType() const {
125 nsDeviceContext
* context
= GetDeviceContext();
127 return context
->GetScreenOrientationType();
129 RefPtr
<widget::Screen
> s
=
130 widget::ScreenManager::GetSingleton().GetPrimaryScreen();
131 return s
->GetOrientationType();
134 ScreenOrientation
* nsScreen::Orientation() const { return mScreenOrientation
; }
136 void nsScreen::GetMozOrientation(nsString
& aOrientation
,
137 CallerType aCallerType
) const {
138 switch (mScreenOrientation
->DeviceType(aCallerType
)) {
139 case OrientationType::Portrait_primary
:
140 aOrientation
.AssignLiteral("portrait-primary");
142 case OrientationType::Portrait_secondary
:
143 aOrientation
.AssignLiteral("portrait-secondary");
145 case OrientationType::Landscape_primary
:
146 aOrientation
.AssignLiteral("landscape-primary");
148 case OrientationType::Landscape_secondary
:
149 aOrientation
.AssignLiteral("landscape-secondary");
152 MOZ_CRASH("Unacceptable screen orientation type.");
157 JSObject
* nsScreen::WrapObject(JSContext
* aCx
,
158 JS::Handle
<JSObject
*> aGivenProto
) {
159 return Screen_Binding::Wrap(aCx
, this, aGivenProto
);
162 CSSIntRect
nsScreen::GetTopWindowInnerRectForRFP() {
163 if (nsPIDOMWindowInner
* inner
= GetOwnerWindow()) {
164 if (BrowsingContext
* bc
= inner
->GetBrowsingContext()) {
165 CSSIntSize size
= bc
->Top()->GetTopInnerSizeForRFP();
166 return {0, 0, size
.width
, size
.height
};
172 bool nsScreen::ShouldResistFingerprinting(RFPTarget aTarget
) const {
173 nsGlobalWindowInner
* owner
= GetOwnerWindow();
174 return owner
&& owner
->ShouldResistFingerprinting(aTarget
);