Merge "[CherryPick] Refactoring: Move the content of HTMLInputElement::subtreeHasChan...
[framework/web/webkit-efl.git] / Source / WebCore / page / PointerLockController.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24
25 #include "config.h"
26 #include "PointerLockController.h"
27
28 #include "Chrome.h"
29 #include "ChromeClient.h"
30 #include "Element.h"
31 #include "Page.h"
32 #include "PlatformMouseEvent.h"
33 #include "VoidCallback.h"
34
35 #if ENABLE(POINTER_LOCK)
36
37 namespace WebCore {
38
39 PointerLockController::PointerLockController(Page* page)
40     : m_page(page)
41 {
42 }
43
44 PassOwnPtr<PointerLockController> PointerLockController::create(Page* page)
45 {
46     return adoptPtr(new PointerLockController(page));
47 }
48
49 void PointerLockController::requestPointerLock(Element* target, PassRefPtr<VoidCallback> successCallback, PassRefPtr<VoidCallback> failureCallback)
50 {
51     if (!target || !target->inDocument() || m_documentOfRemovedElementWhileWaitingForUnlock) {
52         enqueueEvent(eventNames().webkitpointerlockerrorEvent, target);
53         return;
54     }
55
56     if (m_element) {
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());
61
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) {
64             if (successCallback)
65                 successCallback->handleEvent();
66         } else {
67             didLosePointerLock(false);
68             m_element = target;
69             if (successCallback)
70                 successCallback->handleEvent();
71         }
72     } else if (m_page->chrome()->client()->requestPointerLock()) {
73         m_element = target;
74         m_successCallback = successCallback;
75         m_failureCallback = failureCallback;
76     } else {
77         // FIXME: Keep enqueueEvent usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
78         enqueueEvent(eventNames().webkitpointerlockerrorEvent, target);
79
80         // FIXME: Remove callback usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
81         if (failureCallback)
82             failureCallback->handleEvent();
83     }
84 }
85
86 void PointerLockController::requestPointerUnlock()
87 {
88     return m_page->chrome()->client()->requestPointerUnlock();
89 }
90
91 void PointerLockController::elementRemoved(Element* element)
92 {
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.
97         m_element = 0;
98         requestPointerUnlock();
99     }
100 }
101
102 void PointerLockController::documentDetached(Document* document)
103 {
104     if (m_element && m_element->document() == document) {
105         m_element = 0;
106         requestPointerUnlock();
107     }
108 }
109
110 bool PointerLockController::isLocked()
111 {
112     return m_page->chrome()->client()->isPointerLocked();
113 }
114
115 Element* PointerLockController::element() const
116 {
117     return m_element.get();
118 }
119
120 void PointerLockController::didAcquirePointerLock()
121 {
122     // FIXME: Keep enqueueEvent usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
123     enqueueEvent(eventNames().webkitpointerlockchangeEvent, m_element.get());
124
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;
130
131     if (callbackToIssue && elementToNotify && elementToNotify->document()->frame())
132         callbackToIssue->handleEvent();
133 }
134
135 void PointerLockController::didNotAcquirePointerLock()
136 {
137     // FIXME: Keep enqueueEvent usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
138     enqueueEvent(eventNames().webkitpointerlockerrorEvent, m_element.get());
139
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);
143     m_element = 0;
144     m_successCallback = 0;
145     m_failureCallback = 0;
146
147     if (callbackToIssue && elementToNotify && elementToNotify->document()->frame())
148         callbackToIssue->handleEvent();
149 }
150
151 void PointerLockController::didLosePointerLock(bool sendChangeEvent)
152 {
153     // FIXME: Keep enqueueEvent usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
154     if (sendChangeEvent)
155         enqueueEvent(eventNames().webkitpointerlockchangeEvent, m_element ? m_element->document() : m_documentOfRemovedElementWhileWaitingForUnlock.get());
156
157     // FIXME: Remove callback usage. (https://bugs.webkit.org/show_bug.cgi?id=84402)
158     RefPtr<Element> elementToNotify(m_element);
159     m_element = 0;
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));
165 }
166
167 void PointerLockController::dispatchLockedMouseEvent(const PlatformMouseEvent& event, const AtomicString& eventType)
168 {
169     if (!m_element || !m_element->document()->frame())
170         return;
171
172     m_element->dispatchMouseEvent(event, eventType, event.clickCount());
173
174     // Create click events
175     if (eventType == eventNames().mouseupEvent)
176         m_element->dispatchMouseEvent(event, eventNames().clickEvent, event.clickCount());
177 }
178
179 void PointerLockController::enqueueEvent(const AtomicString& type, Element* element)
180 {
181     if (element)
182         enqueueEvent(type, element->document());
183 }
184
185 void PointerLockController::enqueueEvent(const AtomicString& type, Document* document)
186 {
187     if (document)
188         document->enqueueDocumentEvent(Event::create(type, true, false));
189 }
190
191 } // namespace WebCore
192
193 #endif // ENABLE(POINTER_LOCK)