Skip to content

Commit

Permalink
Backed out changeset 05897abf2c4e (bug 1697647) for causing failures …
Browse files Browse the repository at this point in the history
…at lock-sandboxed-iframe.html. CLOSED TREE
  • Loading branch information
Butkovits Atila committed Dec 4, 2021
1 parent b53d383 commit cd8a98f
Show file tree
Hide file tree
Showing 24 changed files with 132 additions and 433 deletions.
79 changes: 26 additions & 53 deletions dom/base/ScreenOrientation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 &&
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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.
Expand All @@ -383,21 +368,15 @@ RefPtr<MozPromise<bool, bool, false>> ScreenOrientation::LockDeviceOrientation(
mFullscreenListener = new FullscreenEventListener();
}

nsresult rv = target->AddSystemEventListener(u"fullscreenchange"_ns,
mFullscreenListener,
/* aUseCapture = */ true);
if (NS_WARN_IF(NS_FAILED(rv))) {
return MozPromise<bool, bool, false>::CreateAndReject(false, __func__);
aRv = target->AddSystemEventListener(u"fullscreenchange"_ns,
mFullscreenListener,
/* 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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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");
}
}

Expand Down
5 changes: 2 additions & 3 deletions dom/base/ScreenOrientation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/ScreenOrientationBinding.h"
#include "mozilla/HalScreenConfiguration.h"
#include "mozilla/MozPromise.h"

class nsScreen;

Expand Down Expand Up @@ -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.
Expand Down
76 changes: 73 additions & 3 deletions dom/base/nsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
return false;
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);
}

// 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() {}
void nsScreen::MozUnlockOrientation() {
if (ShouldResistFingerprinting()) {
return;
}
UpdateDocShellOrientationLock(GetOwner(), hal::eScreenOrientation_None);
mScreenOrientation->UnlockDeviceOrientation();
}

/* virtual */
JSObject* nsScreen::WrapObject(JSContext* aCx,
Expand Down
5 changes: 2 additions & 3 deletions hal/Hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 3 additions & 4 deletions hal/Hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
37 changes: 2 additions & 35 deletions hal/android/AndroidHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "AndroidBridge.h"
#include "mozilla/dom/network/Constants.h"
#include "mozilla/java/GeckoAppShellWrappers.h"
#include "mozilla/java/GeckoRuntimeWrappers.h"
#include "nsIScreenManager.h"
#include "nsPIDOMWindow.h"
#include "nsServiceManagerUtils.h"
Expand Down Expand Up @@ -124,40 +123,8 @@ void GetCurrentScreenConfiguration(ScreenConfiguration* aScreenConfiguration) {
orientation, angle, colorDepth, pixelDepth);
}

RefPtr<MozPromise<bool, bool, false>> LockScreenOrientation(
const hal::ScreenOrientation& aOrientation) {
// Force the default orientation to be portrait-primary.
hal::ScreenOrientation orientation =
aOrientation == eScreenOrientation_Default
? eScreenOrientation_PortraitPrimary
: aOrientation;

switch (orientation) {
// The Android backend only supports these orientations.
case eScreenOrientation_PortraitPrimary:
case eScreenOrientation_PortraitSecondary:
case eScreenOrientation_PortraitPrimary |
eScreenOrientation_PortraitSecondary:
case eScreenOrientation_LandscapePrimary:
case eScreenOrientation_LandscapeSecondary:
case eScreenOrientation_LandscapePrimary |
eScreenOrientation_LandscapeSecondary:
case eScreenOrientation_Default: {
java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
if (runtime != NULL) {
auto result = runtime->LockScreenOrientation(orientation);
auto geckoResult = java::GeckoResult::LocalRef(std::move(result));
return geckoResult
? MozPromise<bool, bool, false>::FromGeckoResult(geckoResult)
: MozPromise<bool, bool, false>::CreateAndReject(false,
__func__);
} else {
return MozPromise<bool, bool, false>::CreateAndReject(false, __func__);
}
}
default:
return nullptr;
}
bool LockScreenOrientation(const hal::ScreenOrientation& aOrientation) {
return false;
}

void UnlockScreenOrientation() {}
Expand Down
6 changes: 2 additions & 4 deletions hal/fallback/FallbackScreenConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ void GetCurrentScreenConfiguration(
fallback::GetCurrentScreenConfiguration(aScreenConfiguration);
}

RefPtr<mozilla::MozPromise<bool, bool, false>> LockScreenOrientation(
const hal::ScreenOrientation& aOrientation) {
return mozilla::MozPromise<bool, bool, false>::CreateAndReject(false,
__func__);
bool LockScreenOrientation(const hal::ScreenOrientation& aOrientation) {
return false;
}

void UnlockScreenOrientation() {}
Expand Down
2 changes: 1 addition & 1 deletion hal/sandbox/PHal.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ parent:

async EnableScreenConfigurationNotifications();
async DisableScreenConfigurationNotifications();
async LockScreenOrientation(ScreenOrientation aOrientation)
sync LockScreenOrientation(ScreenOrientation aOrientation)
returns (bool allowed);
async UnlockScreenOrientation();

Expand Down
Loading

0 comments on commit cd8a98f

Please sign in to comment.