This source file includes following definitions.
- m_lockPending
- create
- requestPointerLock
- requestPointerUnlock
- elementRemoved
- documentDetached
- lockPending
- element
- didAcquirePointerLock
- didNotAcquirePointerLock
- didLosePointerLock
- dispatchLockedMouseEvent
- clearElement
- enqueueEvent
- enqueueEvent
#include "config.h"
#include "core/page/PointerLockController.h"
#include "core/dom/Element.h"
#include "core/events/Event.h"
#include "core/frame/DOMWindow.h"
#include "core/page/Chrome.h"
#include "core/page/ChromeClient.h"
#include "core/page/Page.h"
#include "platform/PlatformMouseEvent.h"
namespace WebCore {
PointerLockController::PointerLockController(Page* page)
: m_page(page)
, m_lockPending(false)
{
}
PassOwnPtr<PointerLockController> PointerLockController::create(Page* page)
{
return adoptPtr(new PointerLockController(page));
}
void PointerLockController::requestPointerLock(Element* target)
{
if (!target || !target->inDocument() || m_documentOfRemovedElementWhileWaitingForUnlock) {
enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
return;
}
if (target->document().isSandboxed(SandboxPointerLock)) {
target->document().addConsoleMessage(SecurityMessageSource, ErrorMessageLevel, "Blocked pointer lock on an element because the element's frame is sandboxed and the 'allow-pointer-lock' permission is not set.");
enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
return;
}
if (m_element) {
if (m_element->document() != target->document()) {
enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
return;
}
enqueueEvent(EventTypeNames::webkitpointerlockchange, target);
m_element = target;
} else if (m_page->chrome().client().requestPointerLock()) {
m_lockPending = true;
m_element = target;
} else {
enqueueEvent(EventTypeNames::webkitpointerlockerror, 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(EventTypeNames::webkitpointerlockchange, m_element.get());
m_lockPending = false;
}
void PointerLockController::didNotAcquirePointerLock()
{
enqueueEvent(EventTypeNames::webkitpointerlockerror, m_element.get());
clearElement();
}
void PointerLockController::didLosePointerLock()
{
enqueueEvent(EventTypeNames::webkitpointerlockchange, 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 == EventTypeNames::mouseup)
m_element->dispatchMouseEvent(event, EventTypeNames::click, 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->domWindow())
document->domWindow()->enqueueDocumentEvent(Event::create(type));
}
}