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 "Page.h"
#include "PlatformMouseEvent.h"
#include "VoidCallback.h"
namespace WebCore {
PointerLockController::PointerLockController(Page& page)
: m_page(page)
, m_lockPending(false)
{
}
void PointerLockController::requestPointerLock(Element* target)
{
if (!target || !target->inDocument() || m_documentOfRemovedElementWhileWaitingForUnlock) {
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;
}
enqueueEvent(eventNames().pointerlockchangeEvent, target);
m_element = target;
} else if (m_page.chrome().client().requestPointerLock()) {
m_lockPending = true;
m_element = target;
} else
enqueueEvent(eventNames().pointerlockerrorEvent, target);
}
void PointerLockController::requestPointerUnlock()
{
return m_page.chrome().client().requestPointerUnlock();
}
void PointerLockController::elementRemoved(Element* element)
{
if (m_element == element) {
m_documentOfRemovedElementWhileWaitingForUnlock = &m_element->document();
clearElement();
requestPointerUnlock();
}
}
void PointerLockController::documentDetached(Document* document)
{
if (m_element && &m_element->document() == document) {
clearElement();
requestPointerUnlock();
}
}
bool PointerLockController::lockPending() const
{
return m_lockPending;
}
Element* PointerLockController::element() const
{
return m_element.get();
}
void PointerLockController::didAcquirePointerLock()
{
enqueueEvent(eventNames().pointerlockchangeEvent, m_element.get());
m_lockPending = false;
}
void PointerLockController::didNotAcquirePointerLock()
{
enqueueEvent(eventNames().pointerlockerrorEvent, m_element.get());
clearElement();
}
void PointerLockController::didLosePointerLock()
{
enqueueEvent(eventNames().pointerlockchangeEvent, m_element ? &m_element->document() : m_documentOfRemovedElementWhileWaitingForUnlock.get());
clearElement();
m_documentOfRemovedElementWhileWaitingForUnlock = nullptr;
}
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::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)