Bug 1607032: Spoof screen orientation and angle to primary values. r=tjr,geckoview-reviewers,owlish

Differential Revision: https://phabricator.services.mozilla.com/D220904
This commit is contained in:
Fatih 2024-09-09 21:57:17 +00:00
Родитель acb78fb6c9
Коммит 524e3bb0df
6 изменённых файлов: 65 добавлений и 32 удалений

Просмотреть файл

@ -626,7 +626,7 @@ void ScreenOrientation::CleanupFullscreenListener() {
OrientationType ScreenOrientation::DeviceType(CallerType aCallerType) const {
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return OrientationType::Landscape_primary;
return nsRFPService::OrientationSecondaryToPrimary(mType);
}
return mType;
}
@ -634,18 +634,13 @@ OrientationType ScreenOrientation::DeviceType(CallerType aCallerType) const {
uint16_t ScreenOrientation::DeviceAngle(CallerType aCallerType) const {
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return 0;
return nsRFPService::OrientationSecondaryToPrimary(mAngle);
}
return mAngle;
}
OrientationType ScreenOrientation::GetType(CallerType aCallerType,
ErrorResult& aRv) const {
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return OrientationType::Landscape_primary;
}
Document* doc = GetResponsibleDocument();
BrowsingContext* bc = doc ? doc->GetBrowsingContext() : nullptr;
if (!bc) {
@ -653,16 +648,16 @@ OrientationType ScreenOrientation::GetType(CallerType aCallerType,
return OrientationType::Portrait_primary;
}
return bc->GetCurrentOrientationType();
OrientationType orientation = bc->GetCurrentOrientationType();
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return nsRFPService::OrientationSecondaryToPrimary(orientation);
}
return orientation;
}
uint16_t ScreenOrientation::GetAngle(CallerType aCallerType,
ErrorResult& aRv) const {
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return 0;
}
Document* doc = GetResponsibleDocument();
BrowsingContext* bc = doc ? doc->GetBrowsingContext() : nullptr;
if (!bc) {
@ -670,7 +665,12 @@ uint16_t ScreenOrientation::GetAngle(CallerType aCallerType,
return 0;
}
return bc->GetCurrentOrientationAngle();
uint16_t angle = static_cast<uint16_t>(bc->GetCurrentOrientationAngle());
if (nsContentUtils::ShouldResistFingerprinting(
aCallerType, GetOwnerGlobal(), RFPTarget::ScreenOrientation)) {
return nsRFPService::OrientationSecondaryToPrimary(angle);
}
return angle;
}
ScreenOrientation::LockPermission

Просмотреть файл

@ -7263,11 +7263,12 @@ void nsGlobalWindowInner::InitWasOffline() { mWasOffline = NS_IsOffline(); }
int16_t nsGlobalWindowInner::Orientation(CallerType aCallerType) {
// GetOrientationAngle() returns 0, 90, 180 or 270.
// window.orientation returns -90, 0, 90 or 180.
uint16_t screenAngle = Screen()->GetOrientationAngle();
if (nsIGlobalObject::ShouldResistFingerprinting(
aCallerType, RFPTarget::ScreenOrientation)) {
return 0;
screenAngle = nsRFPService::OrientationSecondaryToPrimary(screenAngle);
}
int16_t angle = AssertedCast<int16_t>(Screen()->GetOrientationAngle());
int16_t angle = AssertedCast<int16_t>(screenAngle);
return angle <= 180 ? angle : angle - 360;
}

Просмотреть файл

@ -32,9 +32,6 @@ var test = function (isContent) {
["screen.availTop", 0],
["screen.width", "outerWidth"],
["screen.height", "outerHeight"],
["screen.orientation.type", "'landscape-primary'"],
["screen.orientation.angle", 0],
["screen.mozOrientation", "'landscape-primary'"],
["devicePixelRatio", 2],
];

Просмотреть файл

@ -79,19 +79,20 @@ void GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo) {
static bool IsSupportedScreenOrientation(hal::ScreenOrientation aOrientation) {
// The Android backend only supports these orientations.
static constexpr ScreenOrientation kSupportedOrientations[] = {
ScreenOrientation::PortraitPrimary,
ScreenOrientation::PortraitSecondary,
ScreenOrientation::PortraitPrimary | ScreenOrientation::PortraitSecondary,
ScreenOrientation::LandscapePrimary,
ScreenOrientation::LandscapeSecondary,
ScreenOrientation::LandscapePrimary |
ScreenOrientation::LandscapeSecondary,
ScreenOrientation::PortraitPrimary |
ScreenOrientation::PortraitSecondary |
ScreenOrientation::LandscapePrimary |
ScreenOrientation::LandscapeSecondary,
ScreenOrientation::Default,
static constexpr hal::ScreenOrientation kSupportedOrientations[] = {
hal::ScreenOrientation::PortraitPrimary,
hal::ScreenOrientation::PortraitSecondary,
hal::ScreenOrientation::PortraitPrimary |
hal::ScreenOrientation::PortraitSecondary,
hal::ScreenOrientation::LandscapePrimary,
hal::ScreenOrientation::LandscapeSecondary,
hal::ScreenOrientation::LandscapePrimary |
hal::ScreenOrientation::LandscapeSecondary,
hal::ScreenOrientation::PortraitPrimary |
hal::ScreenOrientation::PortraitSecondary |
hal::ScreenOrientation::LandscapePrimary |
hal::ScreenOrientation::LandscapeSecondary,
hal::ScreenOrientation::Default,
};
for (auto supportedOrientation : kSupportedOrientations) {
if (aOrientation == supportedOrientation) {

Просмотреть файл

@ -2381,3 +2381,28 @@ void nsRFPService::GetMediaDeviceGroup(nsString& aGroup,
break;
}
}
/* 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;
}
}
/* static */
uint16_t nsRFPService::OrientationSecondaryToPrimary(uint16_t aAngle) {
switch (aAngle) {
case 180:
return 0;
case 270:
return 90;
default:
return aAngle;
}
}

Просмотреть файл

@ -15,6 +15,7 @@
#include "mozilla/gfx/Types.h"
#include "mozilla/TypedEnumBits.h"
#include "mozilla/dom/MediaDeviceInfoBinding.h"
#include "mozilla/dom/ScreenOrientationBinding.h"
#include "js/RealmOptions.h"
#include "nsHashtablesFwd.h"
#include "nsICookieJarSettings.h"
@ -379,6 +380,14 @@ 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 (exactly) 180 degrees to 0 degrees, 270 degrees to 90 degrees.
static uint16_t OrientationSecondaryToPrimary(uint16_t aAngle);
private:
nsresult Init();