diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index ebb4aa1e5b71..e44b3edf61e2 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -12314,6 +12314,65 @@ private: bool mUserInputOrChromeCaller; }; +static bool +ShouldLockPointer(Element* aElement, Element* aCurrentLock, + bool aNoFocusCheck = false) +{ + // Check if pointer lock pref is enabled + if (!Preferences::GetBool("full-screen-api.pointer-lock.enabled")) { + NS_WARNING("ShouldLockPointer(): Pointer Lock pref not enabled"); + return false; + } + + nsCOMPtr ownerDoc = aElement->OwnerDoc(); + if (aCurrentLock && aCurrentLock->OwnerDoc() != ownerDoc) { + NS_WARNING("ShouldLockPointer(): Existing pointer lock element in a different document"); + return false; + } + + if (!aElement->IsInUncomposedDoc()) { + NS_WARNING("ShouldLockPointer(): Element without Document"); + return false; + } + + if (ownerDoc->GetSandboxFlags() & SANDBOXED_POINTER_LOCK) { + NS_WARNING("ShouldLockPointer(): Document is sandboxed and doesn't allow pointer-lock"); + return false; + } + + // Check if the element is in a document with a docshell. + if (!ownerDoc->GetContainer()) { + return false; + } + nsCOMPtr ownerWindow = ownerDoc->GetWindow(); + if (!ownerWindow) { + return false; + } + nsCOMPtr ownerInnerWindow = ownerDoc->GetInnerWindow(); + if (!ownerInnerWindow) { + return false; + } + if (ownerWindow->GetCurrentInnerWindow() != ownerInnerWindow) { + return false; + } + + nsCOMPtr top = ownerWindow->GetScriptableTop(); + if (!top || !top->GetExtantDoc() || top->GetExtantDoc()->Hidden()) { + NS_WARNING("ShouldLockPointer(): Top document isn't visible."); + return false; + } + + if (!aNoFocusCheck) { + mozilla::ErrorResult rv; + if (!top->GetExtantDoc()->HasFocus(rv)) { + NS_WARNING("ShouldLockPointer(): Top document isn't focused."); + return false; + } + } + + return true; +} + NS_IMETHODIMP PointerLockRequest::Run() { @@ -12333,7 +12392,7 @@ PointerLockRequest::Run() } // Note, we must bypass focus change, so pass true as the last parameter! - if (!d->ShouldLockPointer(e, pointerLockedElement, true)) { + if (!ShouldLockPointer(e, pointerLockedElement, true)) { DispatchPointerLockError(d); return NS_OK; } @@ -12389,65 +12448,6 @@ nsDocument::RequestPointerLock(Element* aElement) userInputOrChromeCaller)); } -bool -nsDocument::ShouldLockPointer(Element* aElement, Element* aCurrentLock, - bool aNoFocusCheck) -{ - // Check if pointer lock pref is enabled - if (!Preferences::GetBool("full-screen-api.pointer-lock.enabled")) { - NS_WARNING("ShouldLockPointer(): Pointer Lock pref not enabled"); - return false; - } - - if (aCurrentLock && aCurrentLock->OwnerDoc() != aElement->OwnerDoc()) { - NS_WARNING("ShouldLockPointer(): Existing pointer lock element in a different document"); - return false; - } - - if (!aElement->IsInUncomposedDoc()) { - NS_WARNING("ShouldLockPointer(): Element without Document"); - return false; - } - - if (mSandboxFlags & SANDBOXED_POINTER_LOCK) { - NS_WARNING("ShouldLockPointer(): Document is sandboxed and doesn't allow pointer-lock"); - return false; - } - - // Check if the element is in a document with a docshell. - nsCOMPtr ownerDoc = aElement->OwnerDoc(); - if (!ownerDoc->GetContainer()) { - return false; - } - nsCOMPtr ownerWindow = ownerDoc->GetWindow(); - if (!ownerWindow) { - return false; - } - nsCOMPtr ownerInnerWindow = ownerDoc->GetInnerWindow(); - if (!ownerInnerWindow) { - return false; - } - if (ownerWindow->GetCurrentInnerWindow() != ownerInnerWindow) { - return false; - } - - nsCOMPtr top = ownerWindow->GetScriptableTop(); - if (!top || !top->GetExtantDoc() || top->GetExtantDoc()->Hidden()) { - NS_WARNING("ShouldLockPointer(): Top document isn't visible."); - return false; - } - - if (!aNoFocusCheck) { - mozilla::ErrorResult rv; - if (!top->GetExtantDoc()->HasFocus(rv)) { - NS_WARNING("ShouldLockPointer(): Top document isn't focused."); - return false; - } - } - - return true; -} - bool nsDocument::SetPointerLock(Element* aElement, int aCursorStyle) { diff --git a/dom/base/nsDocument.h b/dom/base/nsDocument.h index 01ac0e58289c..3853f9350c0b 100644 --- a/dom/base/nsDocument.h +++ b/dom/base/nsDocument.h @@ -1268,8 +1268,6 @@ public: Element* GetFullscreenElement() override; void RequestPointerLock(Element* aElement) override; - bool ShouldLockPointer(Element* aElement, Element* aCurrentLock, - bool aNoFocusCheck = false); bool SetPointerLock(Element* aElement, int aCursorStyle); static void UnlockPointer(nsIDocument* aDoc = nullptr);