ab377a3fa5e96367817a9c903a82dc660922ea24
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/page/PointerLockController.h"
27
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"
35
36 namespace WebCore {
37
38 PointerLockController::PointerLockController(Page* page)
39     : m_page(page)
40     , m_lockPending(false)
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)
50 {
51     if (!target || !target->inDocument() || m_documentOfRemovedElementWhileWaitingForUnlock) {
52         enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
53         return;
54     }
55
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);
60         return;
61     }
62
63     if (m_element) {
64         if (m_element->document() != target->document()) {
65             enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
66             return;
67         }
68         enqueueEvent(EventTypeNames::webkitpointerlockchange, target);
69         m_element = target;
70     } else if (m_page->chrome().client().requestPointerLock()) {
71         m_lockPending = true;
72         m_element = target;
73     } else {
74         enqueueEvent(EventTypeNames::webkitpointerlockerror, target);
75     }
76 }
77
78 void PointerLockController::requestPointerUnlock()
79 {
80     return m_page->chrome().client().requestPointerUnlock();
81 }
82
83 void PointerLockController::elementRemoved(Element* element)
84 {
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.
89         clearElement();
90         requestPointerUnlock();
91     }
92 }
93
94 void PointerLockController::documentDetached(Document* document)
95 {
96     if (m_element && m_element->document() == document) {
97         clearElement();
98         requestPointerUnlock();
99     }
100 }
101
102 bool PointerLockController::lockPending() const
103 {
104     return m_lockPending;
105 }
106
107 Element* PointerLockController::element() const
108 {
109     return m_element.get();
110 }
111
112 void PointerLockController::didAcquirePointerLock()
113 {
114     enqueueEvent(EventTypeNames::webkitpointerlockchange, m_element.get());
115     m_lockPending = false;
116 }
117
118 void PointerLockController::didNotAcquirePointerLock()
119 {
120     enqueueEvent(EventTypeNames::webkitpointerlockerror, m_element.get());
121     clearElement();
122 }
123
124 void PointerLockController::didLosePointerLock()
125 {
126     enqueueEvent(EventTypeNames::webkitpointerlockchange, m_element ? &m_element->document() : m_documentOfRemovedElementWhileWaitingForUnlock.get());
127     clearElement();
128     m_documentOfRemovedElementWhileWaitingForUnlock = nullptr;
129 }
130
131 void PointerLockController::dispatchLockedMouseEvent(const PlatformMouseEvent& event, const AtomicString& eventType)
132 {
133     if (!m_element || !m_element->document().frame())
134         return;
135
136     m_element->dispatchMouseEvent(event, eventType, event.clickCount());
137
138     // Create click events
139     if (eventType == EventTypeNames::mouseup)
140         m_element->dispatchMouseEvent(event, EventTypeNames::click, event.clickCount());
141 }
142
143 void PointerLockController::clearElement()
144 {
145     m_lockPending = false;
146     m_element = nullptr;
147 }
148
149 void PointerLockController::enqueueEvent(const AtomicString& type, Element* element)
150 {
151     if (element)
152         enqueueEvent(type, &element->document());
153 }
154
155 void PointerLockController::enqueueEvent(const AtomicString& type, Document* document)
156 {
157     if (document && document->domWindow())
158         document->domWindow()->enqueueDocumentEvent(Event::create(type));
159 }
160
161 } // namespace WebCore