Bug 1918202: Spoof orientation based on screen size. r=tjr

Differential Revision: https://phabricator.services.mozilla.com/D221863
This commit is contained in:
Fatih 2024-09-11 19:29:17 +00:00
parent dbb4ae6b7d
commit 58b66d38ca
4 changed files with 51 additions and 28 deletions

View File

@ -626,7 +626,13 @@ void ScreenOrientation::CleanupFullscreenListener() {
OrientationType ScreenOrientation::DeviceType(CallerType aCallerType) const {
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return nsRFPService::OrientationSecondaryToPrimary(mType);
Document* doc = GetResponsibleDocument();
BrowsingContext* bc = doc ? doc->GetBrowsingContext() : nullptr;
if (!bc) {
return nsRFPService::GetDefaultOrientationType();
}
CSSIntSize size = bc->GetTopInnerSizeForRFP();
return nsRFPService::ViewportSizeToOrientationType(size.width, size.height);
}
return mType;
}
@ -634,7 +640,13 @@ OrientationType ScreenOrientation::DeviceType(CallerType aCallerType) const {
uint16_t ScreenOrientation::DeviceAngle(CallerType aCallerType) const {
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return nsRFPService::OrientationSecondaryToPrimary(mAngle);
Document* doc = GetResponsibleDocument();
BrowsingContext* bc = doc ? doc->GetBrowsingContext() : nullptr;
if (!bc) {
return 0;
}
CSSIntSize size = bc->GetTopInnerSizeForRFP();
return nsRFPService::ViewportSizeToAngle(size.width, size.height);
}
return mAngle;
}
@ -651,7 +663,8 @@ OrientationType ScreenOrientation::GetType(CallerType aCallerType,
OrientationType orientation = bc->GetCurrentOrientationType();
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return nsRFPService::OrientationSecondaryToPrimary(orientation);
CSSIntSize size = bc->GetTopInnerSizeForRFP();
return nsRFPService::ViewportSizeToOrientationType(size.width, size.height);
}
return orientation;
}
@ -668,7 +681,8 @@ uint16_t ScreenOrientation::GetAngle(CallerType aCallerType,
uint16_t angle = static_cast<uint16_t>(bc->GetCurrentOrientationAngle());
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return nsRFPService::OrientationSecondaryToPrimary(angle);
CSSIntSize size = bc->GetTopInnerSizeForRFP();
return nsRFPService::ViewportSizeToAngle(size.width, size.height);
}
return angle;
}

View File

@ -7274,7 +7274,8 @@ int16_t nsGlobalWindowInner::Orientation(CallerType aCallerType) {
uint16_t screenAngle = Screen()->GetOrientationAngle();
if (nsIGlobalObject::ShouldResistFingerprinting(
aCallerType, RFPTarget::ScreenOrientation)) {
screenAngle = nsRFPService::OrientationSecondaryToPrimary(screenAngle);
CSSIntSize size = mBrowsingContext->GetTopInnerSizeForRFP();
screenAngle = nsRFPService::ViewportSizeToAngle(size.width, size.height);
}
int16_t angle = AssertedCast<int16_t>(screenAngle);
return angle <= 180 ? angle : angle - 360;

View File

@ -2389,26 +2389,32 @@ void nsRFPService::GetMediaDeviceGroup(nsString& aGroup,
}
/* static */
dom::OrientationType nsRFPService::OrientationSecondaryToPrimary(
dom::OrientationType aOrientation) {
switch (aOrientation) {
case dom::OrientationType::Landscape_secondary:
return dom::OrientationType::Landscape_primary;
case dom::OrientationType::Portrait_secondary:
return dom::OrientationType::Portrait_primary;
default:
return aOrientation;
uint16_t nsRFPService::ViewportSizeToAngle(int32_t aWidth, int32_t aHeight) {
#ifdef MOZ_WIDGET_ANDROID
bool neutral = aHeight >= aWidth;
#else
bool neutral = aWidth >= aHeight;
#endif
if (neutral) {
return 0;
}
return 90;
}
/* static */
uint16_t nsRFPService::OrientationSecondaryToPrimary(uint16_t aAngle) {
switch (aAngle) {
case 180:
return 0;
case 270:
return 90;
default:
return aAngle;
dom::OrientationType nsRFPService::ViewportSizeToOrientationType(
int32_t aWidth, int32_t aHeight) {
if (aWidth >= aHeight) {
return dom::OrientationType::Landscape_primary;
}
return dom::OrientationType::Portrait_primary;
}
/* static */
dom::OrientationType nsRFPService::GetDefaultOrientationType() {
#ifdef MOZ_WIDGET_ANDROID
return dom::OrientationType::Portrait_primary;
#else
return dom::OrientationType::Landscape_primary;
#endif
}

View File

@ -384,13 +384,15 @@ class nsRFPService final : public nsIObserver, public nsIRFPService {
static void GetMediaDeviceGroup(nsString& aGroup,
mozilla::dom::MediaDeviceKind aKind);
// Converts any OrientationType::SOMETHING_secondary to
// OrientationType::SOMETHING_primary
static mozilla::dom::OrientationType OrientationSecondaryToPrimary(
mozilla::dom::OrientationType aOrientation);
// Converts the viewport size to the angle.
static uint16_t ViewportSizeToAngle(int32_t aWidth, int32_t aHeight);
// Converts (exactly) 180 degrees to 0 degrees, 270 degrees to 90 degrees.
static uint16_t OrientationSecondaryToPrimary(uint16_t aAngle);
// Converts the viewport size to the orientation type.
static dom::OrientationType ViewportSizeToOrientationType(int32_t aWidth,
int32_t aHeight);
// Returns the default orientation type for the given platform.
static dom::OrientationType GetDefaultOrientationType();
private:
nsresult Init();