2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "core/page/PointerLockController.h"
28 #include "core/dom/Element.h"
29 #include "core/events/Event.h"
30 #include "core/frame/DOMWindow.h"
31 #include "core/page/Chrome.h"
32 #include "core/page/ChromeClient.h"
33 #include "core/page/Page.h"
34 #include "platform/PlatformMouseEvent.h"
38 PointerLockController::PointerLockController(Page* page)
40 , m_lockPending(false)
44 PassOwnPtr<PointerLockController> PointerLockController::create(Page* page)
46 return adoptPtr(new PointerLockController(page));
49 void PointerLockController::requestPointerLock(Element* target)
51 if (!target || !target->inDocument() || m_documentOfRemovedElementWhileWaitingForUnlock) {
52 enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
56 if (target->document().isSandboxed(SandboxPointerLock)) {
57 // FIXME: This message should be moved off the console once a solution to https://bugs.webkit.org/show_bug.cgi?id=103274 exists.
58 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.");
59 enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
64 if (m_element->document() != target->document()) {
65 enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
68 enqueueEvent(EventTypeNames::webkitpointerlockchange, target);
70 } else if (m_page->chrome().client().requestPointerLock()) {
74 enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
78 void PointerLockController::requestPointerUnlock()
80 return m_page->chrome().client().requestPointerUnlock();
83 void PointerLockController::elementRemoved(Element* element)
85 if (m_element == element) {
86 m_documentOfRemovedElementWhileWaitingForUnlock = &m_element->document();
87 // Set element null immediately to block any future interaction with it
88 // including mouse events received before the unlock completes.
90 requestPointerUnlock();
94 void PointerLockController::documentDetached(Document* document)
96 if (m_element && m_element->document() == document) {
98 requestPointerUnlock();
102 bool PointerLockController::lockPending() const
104 return m_lockPending;
107 Element* PointerLockController::element() const
109 return m_element.get();
112 void PointerLockController::didAcquirePointerLock()
114 enqueueEvent(EventTypeNames::webkitpointerlockchange, m_element.get());
115 m_lockPending = false;
118 void PointerLockController::didNotAcquirePointerLock()
120 enqueueEvent(EventTypeNames::webkitpointerlockerror, m_element.get());
124 void PointerLockController::didLosePointerLock()
126 enqueueEvent(EventTypeNames::webkitpointerlockchange, m_element ? &m_element->document() : m_documentOfRemovedElementWhileWaitingForUnlock.get());
128 m_documentOfRemovedElementWhileWaitingForUnlock = nullptr;
131 void PointerLockController::dispatchLockedMouseEvent(const PlatformMouseEvent& event, const AtomicString& eventType)
133 if (!m_element || !m_element->document().frame())
136 m_element->dispatchMouseEvent(event, eventType, event.clickCount());
138 // Create click events
139 if (eventType == EventTypeNames::mouseup)
140 m_element->dispatchMouseEvent(event, EventTypeNames::click, event.clickCount());
143 void PointerLockController::clearElement()
145 m_lockPending = false;
149 void PointerLockController::enqueueEvent(const AtomicString& type, Element* element)
152 enqueueEvent(type, &element->document());
155 void PointerLockController::enqueueEvent(const AtomicString& type, Document* document)
157 if (document && document->domWindow())
158 document->domWindow()->enqueueDocumentEvent(Event::create(type));
161 } // namespace WebCore