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 "PointerLockController.h"
29 #include "ChromeClient.h"
32 #include "PlatformMouseEvent.h"
33 #include "VoidCallback.h"
35 #if ENABLE(POINTER_LOCK)
39 PointerLockController::PointerLockController(Page* page)
44 PassOwnPtr<PointerLockController> PointerLockController::create(Page* page)
46 return adoptPtr(new PointerLockController(page));
49 void PointerLockController::requestPointerLock(Element* target, PassRefPtr<VoidCallback> successCallback, PassRefPtr<VoidCallback> failureCallback)
51 if (!target || !target->inDocument() || m_documentOfRemovedElementWhileWaitingForUnlock) {
52 enqueueEvent(eventNames().webkitpointerlockerrorEvent, target);
57 // FIXME: Keep enqueueEvent usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
58 enqueueEvent(eventNames().webkitpointerlockchangeEvent, target);
59 if (m_element->document() != target->document())
60 enqueueEvent(eventNames().webkitpointerlockchangeEvent, m_element.get());
62 // FIXME: Remove callback usage, keep assignment of m_element = target. (https://bugs.webkit.org/show_bug.cgi?id=84402)
63 if (m_element == target) {
65 successCallback->handleEvent();
67 didLosePointerLock(false);
70 successCallback->handleEvent();
72 } else if (m_page->chrome()->client()->requestPointerLock()) {
74 m_successCallback = successCallback;
75 m_failureCallback = failureCallback;
77 // FIXME: Keep enqueueEvent usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
78 enqueueEvent(eventNames().webkitpointerlockerrorEvent, target);
80 // FIXME: Remove callback usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
82 failureCallback->handleEvent();
86 void PointerLockController::requestPointerUnlock()
88 return m_page->chrome()->client()->requestPointerUnlock();
91 void PointerLockController::elementRemoved(Element* element)
93 if (m_element == element) {
94 m_documentOfRemovedElementWhileWaitingForUnlock = m_element->document();
95 // Set element null immediately to block any future interaction with it
96 // including mouse events received before the unlock completes.
98 requestPointerUnlock();
102 void PointerLockController::documentDetached(Document* document)
104 if (m_element && m_element->document() == document) {
106 requestPointerUnlock();
110 bool PointerLockController::isLocked()
112 return m_page->chrome()->client()->isPointerLocked();
115 Element* PointerLockController::element() const
117 return m_element.get();
120 void PointerLockController::didAcquirePointerLock()
122 // FIXME: Keep enqueueEvent usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
123 enqueueEvent(eventNames().webkitpointerlockchangeEvent, m_element.get());
125 // FIXME: Remove callback usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
126 RefPtr<Element> elementToNotify(m_element);
127 RefPtr<VoidCallback> callbackToIssue(m_successCallback);
128 m_successCallback = 0;
129 m_failureCallback = 0;
131 if (callbackToIssue && elementToNotify && elementToNotify->document()->frame())
132 callbackToIssue->handleEvent();
135 void PointerLockController::didNotAcquirePointerLock()
137 // FIXME: Keep enqueueEvent usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
138 enqueueEvent(eventNames().webkitpointerlockerrorEvent, m_element.get());
140 // FIXME: Remove callback usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
141 RefPtr<Element> elementToNotify(m_element);
142 RefPtr<VoidCallback> callbackToIssue(m_failureCallback);
144 m_successCallback = 0;
145 m_failureCallback = 0;
147 if (callbackToIssue && elementToNotify && elementToNotify->document()->frame())
148 callbackToIssue->handleEvent();
151 void PointerLockController::didLosePointerLock(bool sendChangeEvent)
153 // FIXME: Keep enqueueEvent usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
155 enqueueEvent(eventNames().webkitpointerlockchangeEvent, m_element ? m_element->document() : m_documentOfRemovedElementWhileWaitingForUnlock.get());
157 // FIXME: Remove callback usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
158 RefPtr<Element> elementToNotify(m_element);
160 m_documentOfRemovedElementWhileWaitingForUnlock = 0;
161 m_successCallback = 0;
162 m_failureCallback = 0;
163 if (elementToNotify && elementToNotify->document()->frame())
164 elementToNotify->dispatchEvent(Event::create(eventNames().webkitpointerlocklostEvent, true, false));
167 void PointerLockController::dispatchLockedMouseEvent(const PlatformMouseEvent& event, const AtomicString& eventType)
169 if (!m_element || !m_element->document()->frame())
172 m_element->dispatchMouseEvent(event, eventType, event.clickCount());
174 // Create click events
175 if (eventType == eventNames().mouseupEvent)
176 m_element->dispatchMouseEvent(event, eventNames().clickEvent, event.clickCount());
179 void PointerLockController::enqueueEvent(const AtomicString& type, Element* element)
182 enqueueEvent(type, element->document());
185 void PointerLockController::enqueueEvent(const AtomicString& type, Document* document)
188 document->enqueueDocumentEvent(Event::create(type, true, false));
191 } // namespace WebCore
193 #endif // ENABLE(POINTER_LOCK)