OLD | NEW |
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 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 90 |
91 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator) | 91 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator) |
92 { | 92 { |
93 return NavigatorGamepad::from(navigator).gamepads(); | 93 return NavigatorGamepad::from(navigator).gamepads(); |
94 } | 94 } |
95 | 95 |
96 GamepadList* NavigatorGamepad::gamepads() | 96 GamepadList* NavigatorGamepad::gamepads() |
97 { | 97 { |
98 if (!m_gamepads) | 98 if (!m_gamepads) |
99 m_gamepads = GamepadList::create(); | 99 m_gamepads = GamepadList::create(); |
100 if (frame() && frame()->host()) { | 100 if (startUpdatingIfAttached()) |
101 // The frame must be attached to start updating. | |
102 startUpdating(); | |
103 sampleGamepads<Gamepad>(m_gamepads.get()); | 101 sampleGamepads<Gamepad>(m_gamepads.get()); |
104 } | |
105 return m_gamepads.get(); | 102 return m_gamepads.get(); |
106 } | 103 } |
107 | 104 |
108 void NavigatorGamepad::trace(Visitor* visitor) | 105 void NavigatorGamepad::trace(Visitor* visitor) |
109 { | 106 { |
110 visitor->trace(m_gamepads); | 107 visitor->trace(m_gamepads); |
111 visitor->trace(m_pendingEvents); | 108 visitor->trace(m_pendingEvents); |
112 WillBeHeapSupplement<Navigator>::trace(visitor); | 109 WillBeHeapSupplement<Navigator>::trace(visitor); |
113 DOMWindowProperty::trace(visitor); | 110 DOMWindowProperty::trace(visitor); |
114 PlatformEventController::trace(visitor); | 111 PlatformEventController::trace(visitor); |
115 DOMWindowLifecycleObserver::trace(visitor); | 112 DOMWindowLifecycleObserver::trace(visitor); |
116 } | 113 } |
117 | 114 |
| 115 bool NavigatorGamepad::startUpdatingIfAttached() |
| 116 { |
| 117 // The frame must be attached to start updating. |
| 118 if (frame() && frame()->host()) { |
| 119 startUpdating(); |
| 120 return true; |
| 121 } |
| 122 return false; |
| 123 } |
| 124 |
118 void NavigatorGamepad::didUpdateData() | 125 void NavigatorGamepad::didUpdateData() |
119 { | 126 { |
120 // We should stop listening once we detached. | 127 // We should stop listening once we detached. |
121 ASSERT(frame()); | 128 ASSERT(frame()); |
122 ASSERT(frame()->domWindow()); | 129 ASSERT(frame()->domWindow()); |
123 | 130 |
124 // We register to the dispatcher before sampling gamepads so we need to chec
k if we actually have an event listener. | 131 // We register to the dispatcher before sampling gamepads so we need to chec
k if we actually have an event listener. |
125 if (!m_hasEventListener) | 132 if (!m_hasEventListener) |
126 return; | 133 return; |
127 | 134 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 | 214 |
208 static bool isGamepadEvent(const AtomicString& eventType) | 215 static bool isGamepadEvent(const AtomicString& eventType) |
209 { | 216 { |
210 return eventType == EventTypeNames::gamepadconnected || eventType == EventTy
peNames::gamepaddisconnected; | 217 return eventType == EventTypeNames::gamepadconnected || eventType == EventTy
peNames::gamepaddisconnected; |
211 } | 218 } |
212 | 219 |
213 void NavigatorGamepad::didAddEventListener(LocalDOMWindow*, const AtomicString&
eventType) | 220 void NavigatorGamepad::didAddEventListener(LocalDOMWindow*, const AtomicString&
eventType) |
214 { | 221 { |
215 if (isGamepadEvent(eventType)) { | 222 if (isGamepadEvent(eventType)) { |
216 if (page() && page()->visibilityState() == PageVisibilityStateVisible) | 223 if (page() && page()->visibilityState() == PageVisibilityStateVisible) |
217 startUpdating(); | 224 startUpdatingIfAttached(); |
218 m_hasEventListener = true; | 225 m_hasEventListener = true; |
219 } | 226 } |
220 } | 227 } |
221 | 228 |
222 void NavigatorGamepad::didRemoveEventListener(LocalDOMWindow* window, const Atom
icString& eventType) | 229 void NavigatorGamepad::didRemoveEventListener(LocalDOMWindow* window, const Atom
icString& eventType) |
223 { | 230 { |
224 if (isGamepadEvent(eventType) | 231 if (isGamepadEvent(eventType) |
225 && !window->hasEventListeners(EventTypeNames::gamepadconnected) | 232 && !window->hasEventListeners(EventTypeNames::gamepadconnected) |
226 && !window->hasEventListeners(EventTypeNames::gamepaddisconnected)) { | 233 && !window->hasEventListeners(EventTypeNames::gamepaddisconnected)) { |
227 didRemoveGamepadEventListeners(); | 234 didRemoveGamepadEventListeners(); |
(...skipping 10 matching lines...) Expand all Loading... |
238 m_hasEventListener = false; | 245 m_hasEventListener = false; |
239 m_dispatchOneEventRunner.stop(); | 246 m_dispatchOneEventRunner.stop(); |
240 m_pendingEvents.clear(); | 247 m_pendingEvents.clear(); |
241 } | 248 } |
242 | 249 |
243 void NavigatorGamepad::pageVisibilityChanged() | 250 void NavigatorGamepad::pageVisibilityChanged() |
244 { | 251 { |
245 // Inform the embedder whether it needs to provide gamepad data for us. | 252 // Inform the embedder whether it needs to provide gamepad data for us. |
246 bool visible = page()->visibilityState() == PageVisibilityStateVisible; | 253 bool visible = page()->visibilityState() == PageVisibilityStateVisible; |
247 if (visible && (m_hasEventListener || m_gamepads)) | 254 if (visible && (m_hasEventListener || m_gamepads)) |
248 startUpdating(); | 255 startUpdatingIfAttached(); |
249 else | 256 else |
250 stopUpdating(); | 257 stopUpdating(); |
251 | 258 |
252 if (!visible || !m_hasEventListener) | 259 if (!visible || !m_hasEventListener) |
253 return; | 260 return; |
254 | 261 |
255 // Tell the page what has changed. m_gamepads contains the state before we b
ecame hidden. | 262 // Tell the page what has changed. m_gamepads contains the state before we b
ecame hidden. |
256 // We create a new snapshot and compare them. | 263 // We create a new snapshot and compare them. |
257 GamepadList* oldGamepads = m_gamepads.release(); | 264 GamepadList* oldGamepads = m_gamepads.release(); |
258 gamepads(); | 265 gamepads(); |
(...skipping 13 matching lines...) Expand all Loading... |
272 if (connectedGamepadChanged || (!oldWasConnected && newIsConnected)) { | 279 if (connectedGamepadChanged || (!oldWasConnected && newIsConnected)) { |
273 m_pendingEvents.append(newGamepad); | 280 m_pendingEvents.append(newGamepad); |
274 } | 281 } |
275 } | 282 } |
276 | 283 |
277 if (!m_pendingEvents.isEmpty()) | 284 if (!m_pendingEvents.isEmpty()) |
278 m_dispatchOneEventRunner.runAsync(); | 285 m_dispatchOneEventRunner.runAsync(); |
279 } | 286 } |
280 | 287 |
281 } // namespace blink | 288 } // namespace blink |
OLD | NEW |