Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: Source/modules/gamepad/NavigatorGamepad.cpp

Issue 200783002: Gamepad API: add support for gamepadconnected and gamepaddisconnected events (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: layout fix: webexposed/global-constructors-listing Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/modules/gamepad/NavigatorGamepad.h ('k') | Source/modules/modules.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011, Google Inc. All rights reserved. 2 * Copyright (C) 2011, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met: 5 * modification, are permitted provided that the following conditions are met:
6 * 6 *
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23 * DAMAGE. 23 * DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include "modules/gamepad/NavigatorGamepad.h" 27 #include "modules/gamepad/NavigatorGamepad.h"
28 28
29 #include "RuntimeEnabledFeatures.h"
30 #include "core/dom/Document.h"
31 #include "core/frame/DOMWindow.h"
32 #include "core/frame/LocalFrame.h"
29 #include "core/frame/Navigator.h" 33 #include "core/frame/Navigator.h"
34 #include "core/page/Page.h"
35 #include "modules/gamepad/GamepadDispatcher.h"
36 #include "modules/gamepad/GamepadEvent.h"
30 #include "modules/gamepad/GamepadList.h" 37 #include "modules/gamepad/GamepadList.h"
31 #include "modules/gamepad/WebKitGamepadList.h" 38 #include "modules/gamepad/WebKitGamepadList.h"
32 #include "public/platform/Platform.h"
33 #include "wtf/PassOwnPtr.h"
34 39
35 namespace WebCore { 40 namespace WebCore {
36 41
37 template<typename T> 42 template<typename T>
38 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad) 43 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad)
39 { 44 {
40 gamepad.setId(webGamepad.id); 45 gamepad.setId(webGamepad.id);
41 gamepad.setIndex(index); 46 gamepad.setIndex(index);
42 gamepad.setConnected(webGamepad.connected); 47 gamepad.setConnected(webGamepad.connected);
43 gamepad.setTimestamp(webGamepad.timestamp); 48 gamepad.setTimestamp(webGamepad.timestamp);
44 gamepad.setMapping(webGamepad.mapping); 49 gamepad.setMapping(webGamepad.mapping);
45 gamepad.setAxes(webGamepad.axesLength, webGamepad.axes); 50 gamepad.setAxes(webGamepad.axesLength, webGamepad.axes);
46 gamepad.setButtons(webGamepad.buttonsLength, webGamepad.buttons); 51 gamepad.setButtons(webGamepad.buttonsLength, webGamepad.buttons);
47 } 52 }
48 53
49 template<typename GamepadType, typename ListType> 54 template<typename GamepadType, typename ListType>
50 static void sampleGamepads(ListType* into) 55 static void sampleGamepads(ListType* into)
51 { 56 {
52 blink::WebGamepads gamepads; 57 blink::WebGamepads gamepads;
53 58
54 blink::Platform::current()->sampleGamepads(gamepads); 59 GamepadDispatcher::instance().sampleGamepads(gamepads);
55 60
56 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { 61 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
57 blink::WebGamepad& webGamepad = gamepads.items[i]; 62 blink::WebGamepad& webGamepad = gamepads.items[i];
58 if (i < gamepads.length && webGamepad.connected) { 63 if (i < gamepads.length && webGamepad.connected) {
59 RefPtrWillBeRawPtr<GamepadType> gamepad = into->item(i); 64 RefPtrWillBeRawPtr<GamepadType> gamepad = into->item(i);
60 if (!gamepad) 65 if (!gamepad)
61 gamepad = GamepadType::create(); 66 gamepad = GamepadType::create();
62 sampleGamepad(i, *gamepad, webGamepad); 67 sampleGamepad(i, *gamepad, webGamepad);
63 into->set(i, gamepad); 68 into->set(i, gamepad);
64 } else { 69 } else {
65 into->set(i, nullptr); 70 into->set(i, nullptr);
66 } 71 }
67 } 72 }
68 } 73 }
69 74
70 NavigatorGamepad::NavigatorGamepad() 75 NavigatorGamepad* NavigatorGamepad::from(Document& document)
71 { 76 {
72 } 77 if (!document.frame() || !document.frame()->domWindow())
73 78 return 0;
74 NavigatorGamepad::~NavigatorGamepad() 79 Navigator& navigator = document.frame()->domWindow()->navigator();
75 { 80 return &from(navigator);
76 }
77
78 const char* NavigatorGamepad::supplementName()
79 {
80 return "NavigatorGamepad";
81 } 81 }
82 82
83 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) 83 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator)
84 { 84 {
85 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(WillBeHeapSupp lement<Navigator>::from(navigator, supplementName())); 85 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(WillBeHeapSupp lement<Navigator>::from(navigator, supplementName()));
86 if (!supplement) { 86 if (!supplement) {
87 supplement = new NavigatorGamepad(); 87 supplement = new NavigatorGamepad(*navigator.frame()->document());
88 provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement)); 88 provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement));
89 } 89 }
90 return *supplement; 90 return *supplement;
91 } 91 }
92 92
93 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) 93 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator)
94 { 94 {
95 return NavigatorGamepad::from(navigator).webkitGamepads(); 95 return NavigatorGamepad::from(navigator).webkitGamepads();
96 } 96 }
97 97
98 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator) 98 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator)
99 { 99 {
100 return NavigatorGamepad::from(navigator).gamepads(); 100 return NavigatorGamepad::from(navigator).gamepads();
101 } 101 }
102 102
103 WebKitGamepadList* NavigatorGamepad::webkitGamepads() 103 WebKitGamepadList* NavigatorGamepad::webkitGamepads()
104 { 104 {
105 startUpdating();
105 if (!m_webkitGamepads) 106 if (!m_webkitGamepads)
106 m_webkitGamepads = WebKitGamepadList::create(); 107 m_webkitGamepads = WebKitGamepadList::create();
107 sampleGamepads<WebKitGamepad>(m_webkitGamepads.get()); 108 sampleGamepads<WebKitGamepad>(m_webkitGamepads.get());
108 return m_webkitGamepads.get(); 109 return m_webkitGamepads.get();
109 } 110 }
110 111
111 GamepadList* NavigatorGamepad::gamepads() 112 GamepadList* NavigatorGamepad::gamepads()
112 { 113 {
114 startUpdating();
113 if (!m_gamepads) 115 if (!m_gamepads)
114 m_gamepads = GamepadList::create(); 116 m_gamepads = GamepadList::create();
115 sampleGamepads<Gamepad>(m_gamepads.get()); 117 sampleGamepads<Gamepad>(m_gamepads.get());
116 return m_gamepads.get(); 118 return m_gamepads.get();
117 } 119 }
118 120
119 void NavigatorGamepad::trace(Visitor* visitor) 121 void NavigatorGamepad::trace(Visitor* visitor)
120 { 122 {
121 visitor->trace(m_gamepads); 123 visitor->trace(m_gamepads);
122 visitor->trace(m_webkitGamepads); 124 visitor->trace(m_webkitGamepads);
123 } 125 }
124 126
127 void NavigatorGamepad::didConnectOrDisconnectGamepad(unsigned index, const blink ::WebGamepad& webGamepad, bool connected)
128 {
129 ASSERT(index < blink::WebGamepads::itemsLengthCap);
130 ASSERT(connected == webGamepad.connected);
131
132 // We should stop listening once we detached.
133 ASSERT(window());
134
135 // We register to the dispatcher before sampling gamepads so we need to chec k if we actually have an event listener.
136 if (!m_hasEventListener)
137 return;
138
139 if (window()->document()->activeDOMObjectsAreStopped() || window()->document ()->activeDOMObjectsAreSuspended())
140 return;
141
142 if (!m_gamepads)
143 m_gamepads = GamepadList::create();
144
145 RefPtrWillBeRawPtr<Gamepad> gamepad = m_gamepads->item(index);
146 if (!gamepad)
147 gamepad = Gamepad::create();
148 sampleGamepad(index, *gamepad, webGamepad);
149 m_gamepads->set(index, gamepad);
150
151 const AtomicString& eventName = connected ? EventTypeNames::gamepadconnected : EventTypeNames::gamepaddisconnected;
152 RefPtr<GamepadEvent> event = GamepadEvent::create(eventName, false, true, ga mepad.get());
153 window()->dispatchEvent(event);
154 }
155
156 NavigatorGamepad::NavigatorGamepad(Document& document)
157 : DOMWindowProperty(document.frame())
158 , DeviceSensorEventController(document)
159 , DOMWindowLifecycleObserver(document.frame()->domWindow())
160 {
161 }
162
163 NavigatorGamepad::~NavigatorGamepad()
164 {
165 }
166
167 const char* NavigatorGamepad::supplementName()
168 {
169 return "NavigatorGamepad";
170 }
171
172 void NavigatorGamepad::willDestroyGlobalObjectInFrame()
173 {
174 stopUpdating();
175 DOMWindowProperty::willDestroyGlobalObjectInFrame();
176 }
177
178 void NavigatorGamepad::willDetachGlobalObjectFromFrame()
179 {
180 stopUpdating();
181 DOMWindowProperty::willDetachGlobalObjectFromFrame();
182 }
183
184 void NavigatorGamepad::registerWithDispatcher()
185 {
186 GamepadDispatcher::instance().addClient(this);
187 }
188
189 void NavigatorGamepad::unregisterWithDispatcher()
190 {
191 GamepadDispatcher::instance().removeClient(this);
192 }
193
194 bool NavigatorGamepad::hasLastData()
195 {
196 // Gamepad data is polled instead of pushed.
197 return false;
198 }
199
200 PassRefPtr<Event> NavigatorGamepad::getLastEvent()
201 {
202 // This is called only when hasLastData() is true.
203 ASSERT_NOT_REACHED();
204 return nullptr;
205 }
206
207 bool NavigatorGamepad::isNullEvent(Event*)
208 {
209 // This is called only when hasLastData() is true.
210 ASSERT_NOT_REACHED();
211 return false;
212 }
213
214 void NavigatorGamepad::didAddEventListener(DOMWindow*, const AtomicString& event Type)
215 {
216 if (RuntimeEnabledFeatures::gamepadEnabled() && (eventType == EventTypeNames ::gamepadconnected || eventType == EventTypeNames::gamepaddisconnected)) {
217 if (page() && page()->visibilityState() == PageVisibilityStateVisible)
218 startUpdating();
219 m_hasEventListener = true;
220 }
221 }
222
223 void NavigatorGamepad::didRemoveEventListener(DOMWindow*, const AtomicString& ev entType)
224 {
225 if (eventType == EventTypeNames::gamepadconnected || eventType == EventTypeN ames::gamepaddisconnected)
226 m_hasEventListener = false;
227 }
228
229 void NavigatorGamepad::didRemoveAllEventListeners(DOMWindow*)
230 {
231 m_hasEventListener = false;
232 }
233
125 } // namespace WebCore 234 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/gamepad/NavigatorGamepad.h ('k') | Source/modules/modules.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698