PointerLockController.cpp [plain text]
#include "config.h"
#include "PointerLockController.h"
#if ENABLE(POINTER_LOCK)
#include "Chrome.h"
#include "ChromeClient.h"
#include "Element.h"
#include "Event.h"
#include "EventNames.h"
#include "Page.h"
#include "PlatformMouseEvent.h"
#include "RuntimeEnabledFeatures.h"
#include "ScriptController.h"
#include "VoidCallback.h"
namespace WebCore {
PointerLockController::PointerLockController(Page& page)
: m_page(page)
{
}
void PointerLockController::requestPointerLock(Element* target)
{
if (!target || !target->isConnected() || m_documentOfRemovedElementWhileWaitingForUnlock) {
enqueueEvent(eventNames().pointerlockerrorEvent, target);
return;
}
if (m_documentAllowedToRelockWithoutUserGesture != &target->document() && !ScriptController::processingUserGesture()) {
enqueueEvent(eventNames().pointerlockerrorEvent, target);
return;
}
if (target->document().isSandboxed(SandboxPointerLock)) {
target->document().addConsoleMessage(MessageSource::Security, MessageLevel::Error, ASCIILiteral("Blocked pointer lock on an element because the element's frame is sandboxed and the 'allow-pointer-lock' permission is not set."));
enqueueEvent(eventNames().pointerlockerrorEvent, target);
return;
}
if (m_element) {
if (&m_element->document() != &target->document()) {
enqueueEvent(eventNames().pointerlockerrorEvent, target);
return;
}
m_element = target;
enqueueEvent(eventNames().pointerlockchangeEvent, target);
} else {
m_lockPending = true;
m_element = target;
if (!m_page.chrome().client().requestPointerLock()) {
clearElement();
enqueueEvent(eventNames().pointerlockerrorEvent, target);
}
}
}
void PointerLockController::requestPointerUnlock()
{
if (!m_element)
return;
m_unlockPending = true;
m_page.chrome().client().requestPointerUnlock();
}
void PointerLockController::requestPointerUnlockAndForceCursorVisible()
{
m_documentAllowedToRelockWithoutUserGesture = nullptr;
if (!m_element)
return;
m_unlockPending = true;
m_page.chrome().client().requestPointerUnlock();
m_forceCursorVisibleUponUnlock = true;
}
void PointerLockController::elementRemoved(Element& element)
{
if (m_element == &element) {
m_documentOfRemovedElementWhileWaitingForUnlock = &m_element->document();
requestPointerUnlock();
clearElement();
}
}
void PointerLockController::documentDetached(Document& document)
{
if (m_element && &m_element->document() == &document) {
m_documentOfRemovedElementWhileWaitingForUnlock = &m_element->document();
requestPointerUnlock();
clearElement();
}
}
bool PointerLockController::isLocked() const
{
return m_element && !m_lockPending;
}
bool PointerLockController::lockPending() const
{
return m_lockPending;
}
Element* PointerLockController::element() const
{
return m_element.get();
}
void PointerLockController::didAcquirePointerLock()
{
if (!m_lockPending)
return;
ASSERT(m_element);
enqueueEvent(eventNames().pointerlockchangeEvent, m_element.get());
m_lockPending = false;
m_forceCursorVisibleUponUnlock = false;
m_documentAllowedToRelockWithoutUserGesture = &m_element->document();
}
void PointerLockController::didNotAcquirePointerLock()
{
enqueueEvent(eventNames().pointerlockerrorEvent, m_element.get());
clearElement();
m_unlockPending = false;
}
void PointerLockController::didLosePointerLock()
{
if (!m_unlockPending)
m_documentAllowedToRelockWithoutUserGesture = nullptr;
enqueueEvent(eventNames().pointerlockchangeEvent, m_element ? &m_element->document() : m_documentOfRemovedElementWhileWaitingForUnlock.get());
clearElement();
m_unlockPending = false;
m_documentOfRemovedElementWhileWaitingForUnlock = nullptr;
if (m_forceCursorVisibleUponUnlock) {
m_forceCursorVisibleUponUnlock = false;
m_page.chrome().client().setCursorHiddenUntilMouseMoves(false);
}
}
void PointerLockController::dispatchLockedMouseEvent(const PlatformMouseEvent& event, const AtomicString& eventType)
{
if (!m_element || !m_element->document().frame())
return;
m_element->dispatchMouseEvent(event, eventType, event.clickCount());
if (eventType == eventNames().mouseupEvent)
m_element->dispatchMouseEvent(event, eventNames().clickEvent, event.clickCount());
}
void PointerLockController::dispatchLockedWheelEvent(const PlatformWheelEvent& event)
{
if (!m_element || !m_element->document().frame())
return;
m_element->dispatchWheelEvent(event);
}
void PointerLockController::clearElement()
{
m_lockPending = false;
m_element = nullptr;
}
void PointerLockController::enqueueEvent(const AtomicString& type, Element* element)
{
if (element)
enqueueEvent(type, &element->document());
}
void PointerLockController::enqueueEvent(const AtomicString& type, Document* document)
{
if (document)
document->enqueueDocumentEvent(Event::create(type, true, false));
}
}
#endif // ENABLE(POINTER_LOCK)