Commit c961ef94 authored by Arthur Edelstein's avatar Arthur Edelstein Committed by Georg Koppen
Browse files

Bug 18958: Spoof screen.orientation values

Make sure that
screen.orientation.angle -> 0 and
screen.orientation.type -> "landscape-primary"

Also refactors screen.mozOrientation.
parent 738784fc
Loading
Loading
Loading
Loading
+27 −2
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
#include "mozilla/Preferences.h"

#include "mozilla/dom/Promise.h"
#include "nsContentUtils.h"

using namespace mozilla;
using namespace mozilla::dom;
@@ -403,18 +404,23 @@ ScreenOrientation::UnlockDeviceOrientation()
OrientationType
ScreenOrientation::DeviceType() const
{
  return mType;
  return ShouldResistFingerprinting() ? OrientationType::Landscape_primary
                                      : mType;
}

uint16_t
ScreenOrientation::DeviceAngle() const
{
  return mAngle;
  return ShouldResistFingerprinting() ? 0 : mAngle;
}

OrientationType
ScreenOrientation::GetType(ErrorResult& aRv) const
{
  if (ShouldResistFingerprinting()) {
    return OrientationType::Landscape_primary;
  }

  nsIDocument* doc = GetResponsibleDocument();
  if (!doc) {
    aRv.Throw(NS_ERROR_UNEXPECTED);
@@ -427,6 +433,10 @@ ScreenOrientation::GetType(ErrorResult& aRv) const
uint16_t
ScreenOrientation::GetAngle(ErrorResult& aRv) const
{
  if (ShouldResistFingerprinting()) {
    return 0;
  }

  nsIDocument* doc = GetResponsibleDocument();
  if (!doc) {
    aRv.Throw(NS_ERROR_UNEXPECTED);
@@ -489,6 +499,10 @@ ScreenOrientation::GetResponsibleDocument() const
void
ScreenOrientation::Notify(const hal::ScreenConfiguration& aConfiguration)
{
  if (ShouldResistFingerprinting()) {
    return;
  }

  nsIDocument* doc = GetResponsibleDocument();
  if (!doc) {
    return;
@@ -564,6 +578,17 @@ ScreenOrientation::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  return ScreenOrientationBinding::Wrap(aCx, this, aGivenProto);
}

bool
ScreenOrientation::ShouldResistFingerprinting() const
{
  bool resist = false;
  nsCOMPtr<nsPIDOMWindow> owner = GetOwner();
  if (owner) {
    resist = nsContentUtils::ShouldResistFingerprinting(owner->GetDocShell());
  }
  return resist;
}

NS_IMPL_ISUPPORTS(ScreenOrientation::VisibleEventListener, nsIDOMEventListener)

NS_IMETHODIMP
+2 −0
Original line number Diff line number Diff line
@@ -102,6 +102,8 @@ private:

  void DispatchChangeEvent();

  bool ShouldResistFingerprinting() const;

  LockPermission GetLockOrientationPermission(bool aCheckSandbox) const;

  // Gets the responsible document as defined in the spec.
+4 −3
Original line number Diff line number Diff line
@@ -12821,8 +12821,8 @@ void
nsGlobalWindow::EnableOrientationChangeListener()
{
  MOZ_ASSERT(IsInnerWindow());

  if (!mOrientationChangeObserver) {
  if (!nsContentUtils::ShouldResistFingerprinting(mDocShell)
      && !mOrientationChangeObserver) {
    mOrientationChangeObserver =
      new WindowOrientationObserver(this);
  }
@@ -13672,7 +13672,8 @@ nsGlobalWindow::IsModalContentWindow(JSContext* aCx, JSObject* aGlobal)
int16_t
nsGlobalWindow::Orientation() const
{
  return WindowOrientationObserver::OrientationAngle();
  return nsContentUtils::ShouldResistFingerprinting(mDocShell) ?
           0 : WindowOrientationObserver::OrientationAngle();
}
#endif

+20 −18
Original line number Diff line number Diff line
@@ -166,9 +166,6 @@ nsScreen::Orientation() const
void
nsScreen::GetMozOrientation(nsString& aOrientation) const
{
  if (ShouldResistFingerprinting()) {
    aOrientation.AssignLiteral("landscape-primary");
  } else {
  switch (mScreenOrientation->DeviceType()) {
  case OrientationType::Portrait_primary:
    aOrientation.AssignLiteral("portrait-primary");
@@ -186,7 +183,6 @@ nsScreen::GetMozOrientation(nsString& aOrientation) const
    MOZ_CRASH("Unacceptable screen orientation type.");
  }
}
}

NS_IMETHODIMP
nsScreen::GetSlowMozOrientation(nsAString& aOrientation)
@@ -236,6 +232,9 @@ bool
nsScreen::MozLockOrientation(const Sequence<nsString>& aOrientations,
                             ErrorResult& aRv)
{
  if (ShouldResistFingerprinting()) {
    return false;
  }
  ScreenOrientationInternal orientation = eScreenOrientation_None;

  for (uint32_t i = 0; i < aOrientations.Length(); ++i) {
@@ -283,6 +282,9 @@ nsScreen::MozLockOrientation(const Sequence<nsString>& aOrientations,
void
nsScreen::MozUnlockOrientation()
{
  if (ShouldResistFingerprinting()) {
    return;
  }
  UpdateDocShellOrientationLock(GetOwner(), eScreenOrientation_None);
  mScreenOrientation->UnlockDeviceOrientation();
}