Commit 3493d272 authored by Makoto Kato's avatar Makoto Kato
Browse files

Bug 1753574 - hal::LockOrientation can return error status. r=smaug,geckoview-reviewers,agi,calu

From https://w3c.github.io/screen-orientation/#apply-an-orientation-lock

> 7.2. Apply an orientation lock
>
> The steps to apply an orientation lock to a Document using orientation are as
> follows:
>
>  1. If the user agent does not support locking the screen orientation, return
>     a promise rejected with a "NotSupportedError" DOMException and abort
>     these steps.

So if orientation controller delegate isn't set, we should throw
`NotSupportedError`.  But, actually, we throws `AbortError`, so this isn't
correct.

To return any DOM error from platform implementation of
`screen.orientation.lock`, I would like to change return value to
`GenericPromise`'s.

Differential Revision: https://phabricator.services.mozilla.com/D137970
parent ad14d19c
Loading
Loading
Loading
Loading
+18 −17
Original line number Diff line number Diff line
@@ -152,8 +152,6 @@ ScreenOrientation::LockOrientationTask::LockOrientationTask(

ScreenOrientation::LockOrientationTask::~LockOrientationTask() = default;

using LockOrientationPromise = MozPromise<bool, bool, false>;

bool ScreenOrientation::LockOrientationTask::OrientationLockContains(
    OrientationType aOrientationType) {
  return bool(mOrientationLock & OrientationTypeToInternal(aOrientationType));
@@ -189,13 +187,14 @@ ScreenOrientation::LockOrientationTask::Run() {
  }

  mScreenOrientation->LockDeviceOrientation(mOrientationLock, mIsFullscreen)
      ->Then(GetCurrentSerialEventTarget(), __func__,
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          [self = RefPtr{this}](
                 const LockOrientationPromise::ResolveOrRejectValue& aValue) {
              const GenericNonExclusivePromise::ResolveOrRejectValue& aValue) {
            if (aValue.IsResolve()) {
              return;
            }
               self->mPromise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
            self->mPromise->MaybeReject(aValue.RejectValue());
            self->mDocument->ClearOrientationPendingPromise();
          });

@@ -352,10 +351,11 @@ already_AddRefed<Promise> ScreenOrientation::LockInternal(
#endif
}

RefPtr<LockOrientationPromise> ScreenOrientation::LockDeviceOrientation(
RefPtr<GenericNonExclusivePromise> ScreenOrientation::LockDeviceOrientation(
    hal::ScreenOrientation aOrientation, bool aIsFullscreen) {
  if (!GetOwner()) {
    return LockOrientationPromise::CreateAndReject(false, __func__);
    return GenericNonExclusivePromise::CreateAndReject(NS_ERROR_DOM_ABORT_ERR,
                                                       __func__);
  }

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

  // We are fullscreen and lock has been accepted.
@@ -377,7 +378,8 @@ RefPtr<LockOrientationPromise> ScreenOrientation::LockDeviceOrientation(
                                                 mFullscreenListener,
                                                 /* aUseCapture = */ true);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return LockOrientationPromise::CreateAndReject(false, __func__);
      return GenericNonExclusivePromise::CreateAndReject(NS_ERROR_DOM_ABORT_ERR,
                                                         __func__);
    }
  }

@@ -552,8 +554,7 @@ void ScreenOrientation::UpdateActiveOrientationLock(
    hal::LockScreenOrientation(aOrientation)
        ->Then(
            GetMainThreadSerialEventTarget(), __func__,
            [](const mozilla::MozPromise<bool, bool,
                                         false>::ResolveOrRejectValue& aValue) {
            [](const GenericNonExclusivePromise::ResolveOrRejectValue& aValue) {
              NS_WARNING_ASSERTION(aValue.IsResolve(),
                                   "hal::LockScreenOrientation failed");
            });
+1 −1
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ class ScreenOrientation final : public DOMEventTargetHelper {

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

  // This method calls in to the HAL to unlock the device and removes
+1 −1
Original line number Diff line number Diff line
@@ -378,7 +378,7 @@ void NotifyWakeLockChange(const WakeLockInformation& aInfo) {
  WakeLockObservers()->BroadcastInformation(aInfo);
}

RefPtr<mozilla::MozPromise<bool, bool, false>> LockScreenOrientation(
RefPtr<GenericNonExclusivePromise> LockScreenOrientation(
    const ScreenOrientation& aOrientation) {
  AssertMainThread();
  RETURN_PROXY_IF_SANDBOXED(LockScreenOrientation(aOrientation), nullptr);
+2 −2
Original line number Diff line number Diff line
@@ -220,8 +220,8 @@ void NotifyWakeLockChange(const hal::WakeLockInformation& aWakeLockInfo);
 * Lock the screen orientation to the specific orientation.
 * @return A promise indicating that the screen orientation has been locked.
 */
[[nodiscard]] RefPtr<mozilla::MozPromise<bool, bool, false>>
LockScreenOrientation(const hal::ScreenOrientation& aOrientation);
[[nodiscard]] RefPtr<GenericNonExclusivePromise> LockScreenOrientation(
    const hal::ScreenOrientation& aOrientation);

/**
 * Unlock the screen orientation.
+23 −9
Original line number Diff line number Diff line
@@ -101,25 +101,39 @@ static bool IsSupportedScreenOrientation(hal::ScreenOrientation aOrientation) {
  return false;
}

RefPtr<MozPromise<bool, bool, false>> LockScreenOrientation(
RefPtr<GenericNonExclusivePromise> LockScreenOrientation(
    const hal::ScreenOrientation& aOrientation) {
  using LockPromise = MozPromise<bool, bool, false>;

  if (!IsSupportedScreenOrientation(aOrientation)) {
    NS_WARNING("Unsupported screen orientation type");
    return LockPromise::CreateAndReject(false, __func__);
    return GenericNonExclusivePromise::CreateAndReject(
        NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__);
  }

  java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
  if (!runtime) {
    return LockPromise::CreateAndReject(false, __func__);
    return GenericNonExclusivePromise::CreateAndReject(NS_ERROR_DOM_ABORT_ERR,
                                                       __func__);
  }
  auto result = runtime->LockScreenOrientation(uint32_t(aOrientation));
  auto geckoResult = java::GeckoResult::LocalRef(std::move(result));
  if (!geckoResult) {
    return LockPromise::CreateAndReject(false, __func__);
  }
  return LockPromise::FromGeckoResult(geckoResult);
  return GenericNonExclusivePromise::FromGeckoResult(geckoResult)
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          [](const GenericNonExclusivePromise::ResolveOrRejectValue& aValue) {
            if (aValue.IsResolve()) {
              if (aValue.ResolveValue()) {
                return GenericNonExclusivePromise::CreateAndResolve(true,
                                                                    __func__);
              }
              // Delegated orientation controller returns failure for
              // lock.
              return GenericNonExclusivePromise::CreateAndReject(
                  NS_ERROR_DOM_ABORT_ERR, __func__);
            }
            // Browser side doesn't implement orientation delegate.
            return GenericNonExclusivePromise::CreateAndReject(
                NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__);
          });
}

void UnlockScreenOrientation() {
Loading