Commit cd8a98f0 authored by Butkovits Atila's avatar Butkovits Atila
Browse files

Backed out changeset 05897abf2c4e (bug 1697647) for causing failures at...

Backed out changeset 05897abf2c4e (bug 1697647) for causing failures at lock-sandboxed-iframe.html. CLOSED TREE
parent b53d383b
Loading
Loading
Loading
Loading
+26 −53
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/StaticPrefs_browser.h"
#include "nsContentUtils.h"

using namespace mozilla;
@@ -159,11 +158,8 @@ bool ScreenOrientation::LockOrientationTask::OrientationLockContains(

NS_IMETHODIMP
ScreenOrientation::LockOrientationTask::Run() {
  if (!mPromise) {
    return NS_OK;
  }

  // Step to lock the orientation as defined in the spec.

  if (mDocument->GetOrientationPendingPromise() != mPromise) {
    // The document's pending promise is not associated with this task
    // to lock orientation. There has since been another request to
@@ -186,29 +182,19 @@ ScreenOrientation::LockOrientationTask::Run() {
    return NS_OK;
  }

  RefPtr<MozPromise<bool, bool, false>> lockOrientationPromise =
      mScreenOrientation->LockDeviceOrientation(mOrientationLock,
                                                mIsFullscreen);
  ErrorResult rv;
  bool result = mScreenOrientation->LockDeviceOrientation(mOrientationLock,
                                                          mIsFullscreen, rv);
  if (NS_WARN_IF(rv.Failed())) {
    return rv.StealNSResult();
  }

  if (NS_WARN_IF(!lockOrientationPromise)) {
  if (NS_WARN_IF(!result)) {
    mPromise->MaybeReject(NS_ERROR_UNEXPECTED);
    mDocument->ClearOrientationPendingPromise();
    return NS_OK;
  }

  lockOrientationPromise->Then(
      GetCurrentSerialEventTarget(), __func__,
      [self = RefPtr{this}](
          const mozilla::MozPromise<bool, bool, false>::ResolveOrRejectValue&
              aValue) {
        if (aValue.IsResolve()) {
          return NS_OK;
        }
        self->mPromise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
        self->mDocument->ClearOrientationPendingPromise();
        return NS_OK;
      });

  BrowsingContext* bc = mDocument->GetBrowsingContext();
  if (OrientationLockContains(bc->GetCurrentOrientationType()) ||
      (mOrientationLock == hal::eScreenOrientation_Default &&
@@ -318,12 +304,6 @@ already_AddRefed<Promise> ScreenOrientation::LockInternal(
  p->MaybeReject(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
  return p.forget();
#else
  // Bypass locking screen orientation if preference is false
  if (!StaticPrefs::dom_screenorientation_allow_lock()) {
    p->MaybeReject(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
    return p.forget();
  }

  LockPermission perm = GetLockOrientationPermission(true);
  if (perm == LOCK_DENIED) {
    p->MaybeReject(NS_ERROR_DOM_SECURITY_ERR);
@@ -362,10 +342,11 @@ already_AddRefed<Promise> ScreenOrientation::LockInternal(
#endif
}

RefPtr<MozPromise<bool, bool, false>> ScreenOrientation::LockDeviceOrientation(
    hal::ScreenOrientation aOrientation, bool aIsFullscreen) {
bool ScreenOrientation::LockDeviceOrientation(
    hal::ScreenOrientation aOrientation, bool aIsFullscreen, ErrorResult& aRv) {
  if (!GetOwner()) {
    return MozPromise<bool, bool, false>::CreateAndReject(false, __func__);
    aRv.Throw(NS_ERROR_UNEXPECTED);
    return false;
  }

  nsCOMPtr<EventTarget> target = GetOwner()->GetDoc();
@@ -374,7 +355,11 @@ RefPtr<MozPromise<bool, bool, false>> ScreenOrientation::LockDeviceOrientation(
  // This needs to be done before LockScreenOrientation call to make sure
  // the locking can be unlocked.
  if (aIsFullscreen && !target) {
    return MozPromise<bool, bool, false>::CreateAndReject(false, __func__);
    return false;
  }

  if (NS_WARN_IF(!hal::LockScreenOrientation(aOrientation))) {
    return false;
  }

  // We are fullscreen and lock has been accepted.
@@ -383,21 +368,15 @@ RefPtr<MozPromise<bool, bool, false>> ScreenOrientation::LockDeviceOrientation(
      mFullscreenListener = new FullscreenEventListener();
    }

    nsresult rv = target->AddSystemEventListener(u"fullscreenchange"_ns,
    aRv = target->AddSystemEventListener(u"fullscreenchange"_ns,
                                         mFullscreenListener,
                                                 /* aUseCapture = */ true);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return MozPromise<bool, bool, false>::CreateAndReject(false, __func__);
    }
                                         /* useCapture = */ true);
    if (NS_WARN_IF(aRv.Failed())) {
      return false;
    }

  RefPtr<MozPromise<bool, bool, false>> halPromise =
      hal::LockScreenOrientation(aOrientation);
  if (halPromise == nullptr) {
    return MozPromise<bool, bool, false>::CreateAndReject(false, __func__);
  }

  return halPromise;
  return true;
}

void ScreenOrientation::Unlock(ErrorResult& aRv) {
@@ -542,7 +521,7 @@ void ScreenOrientation::Notify(const hal::ScreenConfiguration& aConfiguration) {
  if (doc->Hidden() && !mVisibleListener) {
    mVisibleListener = new VisibleEventListener();
    rv = doc->AddSystemEventListener(u"visibilitychange"_ns, mVisibleListener,
                                     /* aUseCapture = */ true);
                                     /* useCapture = */ true);
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "AddSystemEventListener failed");
    return;
  }
@@ -562,14 +541,8 @@ void ScreenOrientation::UpdateActiveOrientationLock(
  if (aOrientation == hal::eScreenOrientation_None) {
    hal::UnlockScreenOrientation();
  } else {
    hal::LockScreenOrientation(aOrientation)
        ->Then(
            GetMainThreadSerialEventTarget(), __func__,
            [](const mozilla::MozPromise<bool, bool,
                                         false>::ResolveOrRejectValue& aValue) {
              NS_WARNING_ASSERTION(aValue.IsResolve(),
                                   "hal::LockScreenOrientation failed");
            });
    DebugOnly<bool> ok = hal::LockScreenOrientation(aOrientation);
    NS_WARNING_ASSERTION(ok, "hal::LockScreenOrientation failed");
  }
}

+2 −3
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/ScreenOrientationBinding.h"
#include "mozilla/HalScreenConfiguration.h"
#include "mozilla/MozPromise.h"

class nsScreen;

@@ -75,8 +74,8 @@ class ScreenOrientation final

  // This method calls into the HAL to lock the device and sets
  // up listeners for full screen change.
  RefPtr<MozPromise<bool, bool, false>> LockDeviceOrientation(
      hal::ScreenOrientation aOrientation, bool aIsFullscreen);
  bool LockDeviceOrientation(hal::ScreenOrientation aOrientation,
                             bool aIsFullscreen, ErrorResult& aRv);

  // This method calls in to the HAL to unlock the device and removes
  // full screen change listener.
+73 −3
Original line number Diff line number Diff line
@@ -195,6 +195,27 @@ void nsScreen::GetMozOrientation(nsString& aOrientation,
  }
}

static void UpdateDocShellOrientationLock(nsPIDOMWindowInner* aWindow,
                                          hal::ScreenOrientation aOrientation) {
  if (!aWindow) {
    return;
  }

  nsCOMPtr<nsIDocShell> docShell = aWindow->GetDocShell();
  if (!docShell) {
    return;
  }

  RefPtr<BrowsingContext> bc = docShell->GetBrowsingContext();
  bc = bc ? bc->Top() : nullptr;
  if (!bc) {
    return;
  }

  // Setting orientation lock on a discarded context has no effect.
  Unused << bc->SetOrientationLock(aOrientation);
}

bool nsScreen::MozLockOrientation(const nsAString& aOrientation,
                                  ErrorResult& aRv) {
  nsString orientation(aOrientation);
@@ -206,13 +227,62 @@ bool nsScreen::MozLockOrientation(const nsAString& aOrientation,
  return MozLockOrientation(orientations, aRv);
}

// This function is deprecated, use ScreenOrientation API instead.
bool nsScreen::MozLockOrientation(const Sequence<nsString>& aOrientations,
                                  ErrorResult& aRv) {
  if (ShouldResistFingerprinting()) {
    return false;
  }
  hal::ScreenOrientation orientation = hal::eScreenOrientation_None;

  for (uint32_t i = 0; i < aOrientations.Length(); ++i) {
    const nsString& item = aOrientations[i];

    if (item.EqualsLiteral("portrait")) {
      orientation |= hal::eScreenOrientation_PortraitPrimary |
                     hal::eScreenOrientation_PortraitSecondary;
    } else if (item.EqualsLiteral("portrait-primary")) {
      orientation |= hal::eScreenOrientation_PortraitPrimary;
    } else if (item.EqualsLiteral("portrait-secondary")) {
      orientation |= hal::eScreenOrientation_PortraitSecondary;
    } else if (item.EqualsLiteral("landscape")) {
      orientation |= hal::eScreenOrientation_LandscapePrimary |
                     hal::eScreenOrientation_LandscapeSecondary;
    } else if (item.EqualsLiteral("landscape-primary")) {
      orientation |= hal::eScreenOrientation_LandscapePrimary;
    } else if (item.EqualsLiteral("landscape-secondary")) {
      orientation |= hal::eScreenOrientation_LandscapeSecondary;
    } else if (item.EqualsLiteral("default")) {
      orientation |= hal::eScreenOrientation_Default;
    } else {
      // If we don't recognize the token, we should just return 'false'
      // without throwing.
      return false;
    }
  }

  switch (mScreenOrientation->GetLockOrientationPermission(false)) {
    case ScreenOrientation::LOCK_DENIED:
      return false;
    case ScreenOrientation::LOCK_ALLOWED:
      UpdateDocShellOrientationLock(GetOwner(), orientation);
      return mScreenOrientation->LockDeviceOrientation(orientation, false, aRv);
    case ScreenOrientation::FULLSCREEN_LOCK_ALLOWED:
      UpdateDocShellOrientationLock(GetOwner(), orientation);
      return mScreenOrientation->LockDeviceOrientation(orientation, true, aRv);
  }

void nsScreen::MozUnlockOrientation() {}
  // This is only for compilers that don't understand that the previous switch
  // will always return.
  MOZ_CRASH("unexpected lock orientation permission value");
}

void nsScreen::MozUnlockOrientation() {
  if (ShouldResistFingerprinting()) {
    return;
  }
  UpdateDocShellOrientationLock(GetOwner(), hal::eScreenOrientation_None);
  mScreenOrientation->UnlockDeviceOrientation();
}

/* virtual */
JSObject* nsScreen::WrapObject(JSContext* aCx,
+2 −3
Original line number Diff line number Diff line
@@ -407,10 +407,9 @@ void NotifyScreenConfigurationChange(
  ScreenConfigurationObservers()->BroadcastCachedInformation();
}

RefPtr<mozilla::MozPromise<bool, bool, false>> LockScreenOrientation(
    const ScreenOrientation& aOrientation) {
bool LockScreenOrientation(const ScreenOrientation& aOrientation) {
  AssertMainThread();
  RETURN_PROXY_IF_SANDBOXED(LockScreenOrientation(aOrientation), nullptr);
  RETURN_PROXY_IF_SANDBOXED(LockScreenOrientation(aOrientation), false);
}

void UnlockScreenOrientation() {
+3 −4
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
#include "mozilla/HalWakeLockInformation.h"
#include "mozilla/HalTypes.h"
#include "mozilla/Types.h"
#include "mozilla/MozPromise.h"

/*
 * Hal.h contains the public Hal API.
@@ -233,10 +232,10 @@ void NotifyScreenConfigurationChange(

/**
 * Lock the screen orientation to the specific orientation.
 * @return A promise indicating that the screen orientation has been locked.
 * @return Whether the lock has been accepted.
 */
[[nodiscard]] RefPtr<mozilla::MozPromise<bool, bool, false>>
LockScreenOrientation(const hal::ScreenOrientation& aOrientation);
[[nodiscard]] bool LockScreenOrientation(
    const hal::ScreenOrientation& aOrientation);

/**
 * Unlock the screen orientation.
Loading