1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/*
* Copyright (C) 2014 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GamepadManager.h"
#if ENABLE(GAMEPAD)
#include "Document.h"
#include "EventNames.h"
#include "Gamepad.h"
#include "GamepadEvent.h"
#include "GamepadProvider.h"
#include "LocalDOMWindow.h"
#include "Logging.h"
#include "Navigator.h"
#include "NavigatorGamepad.h"
#include "PlatformGamepad.h"
#include <wtf/NeverDestroyed.h>
#if PLATFORM(VISION)
#include "Page.h"
#endif
namespace WebCore {
static NavigatorGamepad& navigatorGamepadFromDOMWindow(LocalDOMWindow& window)
{
return NavigatorGamepad::from(window.navigator());
}
GamepadManager& GamepadManager::singleton()
{
static NeverDestroyed<GamepadManager> sharedManager;
return sharedManager;
}
GamepadManager::GamepadManager()
: m_isMonitoringGamepads(false)
{
}
#if PLATFORM(VISION)
void GamepadManager::findUnquarantinedNavigatorsAndWindows(WeakHashSet<Navigator>& navigators, WeakHashSet<LocalDOMWindow, WeakPtrImplWithEventTargetData>& windows)
{
for (auto& navigator : m_navigators) {
if (!m_gamepadQuarantinedNavigators.contains(navigator))
navigators.add(navigator);
}
for (auto& window : m_domWindows) {
if (!m_gamepadQuarantinedDOMWindows.contains(window))
windows.add(window);
}
}
#endif
void GamepadManager::platformGamepadConnected(PlatformGamepad& platformGamepad, EventMakesGamepadsVisible eventVisibility)
{
if (eventVisibility == EventMakesGamepadsVisible::No)
return;
// Notify blind Navigators and Windows about all gamepads except for this one.
for (auto& gamepad : GamepadProvider::singleton().platformGamepads()) {
if (!gamepad || gamepad == &platformGamepad)
continue;
makeGamepadVisible(*gamepad, m_gamepadBlindNavigators, m_gamepadBlindDOMWindows);
}
m_gamepadBlindNavigators.clear();
m_gamepadBlindDOMWindows.clear();
#if PLATFORM(VISION)
// Notify everyone not in the quarantined list of this new gamepad.
WeakHashSet<Navigator> navigators;
WeakHashSet<LocalDOMWindow, WeakPtrImplWithEventTargetData> windows;
findUnquarantinedNavigatorsAndWindows(navigators, windows);
makeGamepadVisible(platformGamepad, navigators, windows);
#else
// Notify everyone of this new gamepad.
makeGamepadVisible(platformGamepad, m_navigators, m_domWindows);
#endif
}
void GamepadManager::platformGamepadDisconnected(PlatformGamepad& platformGamepad)
{
WeakHashSet<Navigator> notifiedNavigators;
// Handle the disconnect for all DOMWindows with event listeners and their Navigators.
for (auto& window : copyToVectorOf<WeakPtr<LocalDOMWindow, WeakPtrImplWithEventTargetData>>(m_domWindows)) {
// Event dispatch might have made this window go away.
if (!window)
continue;
// This LocalDOMWindow's Navigator might not be accessible. e.g. The LocalDOMWindow might be in the back/forward cache.
// If this happens the LocalDOMWindow will not get this gamepaddisconnected event.
Ref navigator = window->navigator();
// If this Navigator hasn't seen gamepads yet then its Window should not get the disconnect event.
if (m_gamepadBlindNavigators.contains(navigator.get()))
continue;
#if PLATFORM(VISION)
if (m_gamepadQuarantinedNavigators.contains(navigator.get()))
continue;
#endif
auto& navigatorGamepad = NavigatorGamepad::from(navigator);
Ref gamepad = navigatorGamepad.gamepadFromPlatformGamepad(platformGamepad);
navigatorGamepad.gamepadDisconnected(platformGamepad);
notifiedNavigators.add(navigator.get());
window->dispatchEvent(GamepadEvent::create(eventNames().gamepaddisconnectedEvent, gamepad.get()), window->document());
}
// Notify all the Navigators that haven't already been notified.
for (Ref navigator : m_navigators) {
if (!notifiedNavigators.contains(navigator.get()))
NavigatorGamepad::from(navigator).gamepadDisconnected(platformGamepad);
}
}
void GamepadManager::platformGamepadInputActivity(EventMakesGamepadsVisible eventVisibility)
{
if (eventVisibility == EventMakesGamepadsVisible::No)
return;
if (m_gamepadBlindNavigators.isEmptyIgnoringNullReferences() && m_gamepadBlindDOMWindows.isEmptyIgnoringNullReferences())
return;
for (auto& gamepad : GamepadProvider::singleton().platformGamepads()) {
if (gamepad)
makeGamepadVisible(*gamepad, m_gamepadBlindNavigators, m_gamepadBlindDOMWindows);
}
m_gamepadBlindNavigators.clear();
m_gamepadBlindDOMWindows.clear();
}
void GamepadManager::makeGamepadVisible(PlatformGamepad& platformGamepad, WeakHashSet<Navigator>& navigatorSet, WeakHashSet<LocalDOMWindow, WeakPtrImplWithEventTargetData>& domWindowSet)
{
LOG(Gamepad, "(%u) GamepadManager::makeGamepadVisible - New gamepad '%s' is visible", (unsigned)getpid(), platformGamepad.id().utf8().data());
if (navigatorSet.isEmptyIgnoringNullReferences() && domWindowSet.isEmptyIgnoringNullReferences())
return;
for (Ref navigator : navigatorSet)
NavigatorGamepad::from(navigator).gamepadConnected(platformGamepad);
for (auto& window : copyToVectorOf<WeakPtr<LocalDOMWindow, WeakPtrImplWithEventTargetData>>(m_domWindows)) {
// Event dispatch might have made this window go away.
if (!window)
continue;
// This LocalDOMWindow's Navigator might not be accessible. e.g. The LocalDOMWindow might be in the back/forward cache.
// If this happens the LocalDOMWindow will not get this gamepadconnected event.
// The new gamepad will still be visibile to it once it is restored from the back/forward cache.
NavigatorGamepad& navigator = navigatorGamepadFromDOMWindow(*window);
Ref gamepad = navigator.gamepadFromPlatformGamepad(platformGamepad);
RefPtr document = navigator.navigator().document();
LOG(Gamepad, "(%u) GamepadManager::makeGamepadVisible - Dispatching gamepadconnected event for gamepad '%s'", (unsigned)getpid(), platformGamepad.id().utf8().data());
UserGestureIndicator gestureIndicator(IsProcessingUserGesture::Yes, document.get());
window->dispatchEvent(GamepadEvent::create(eventNames().gamepadconnectedEvent, gamepad.get()), window->document());
}
}
void GamepadManager::registerNavigator(Navigator& navigator)
{
LOG(Gamepad, "(%u) GamepadManager registering Navigator %p", (unsigned)getpid(), &navigator);
ASSERT(!m_navigators.contains(navigator));
m_navigators.add(navigator);
#if PLATFORM(VISION)
auto page = navigator.protectedPage();
if (page && page->gamepadAccessGranted())
m_gamepadBlindNavigators.add(navigator);
else
m_gamepadQuarantinedNavigators.add(navigator);
#else
m_gamepadBlindNavigators.add(navigator);
#endif
maybeStartMonitoringGamepads();
}
void GamepadManager::unregisterNavigator(Navigator& navigator)
{
LOG(Gamepad, "(%u) GamepadManager unregistering Navigator %p", (unsigned)getpid(), &navigator);
ASSERT(m_navigators.contains(navigator));
m_navigators.remove(navigator);
m_gamepadBlindNavigators.remove(navigator);
#if PLATFORM(VISION)
m_gamepadQuarantinedNavigators.remove(navigator);
#endif
maybeStopMonitoringGamepads();
}
void GamepadManager::registerDOMWindow(LocalDOMWindow& window)
{
LOG(Gamepad, "(%u) GamepadManager registering LocalDOMWindow %p", (unsigned)getpid(), &window);
ASSERT(!m_domWindows.contains(window));
m_domWindows.add(window);
// Anytime we register a LocalDOMWindow, we should make sure its NavigatorGamepad is constructed.
// Upon construction, it will register the navigator in m_navigators.
Ref navigator = navigatorGamepadFromDOMWindow(window).navigator();
ASSERT(m_navigators.contains(navigator.get()));
// If this LocalDOMWindow's NavigatorGamepad was already registered but was still blind,
// then this LocalDOMWindow should be blind.
if (m_gamepadBlindNavigators.contains(navigator.get()))
m_gamepadBlindDOMWindows.add(window);
#if PLATFORM(VISION)
else if (m_gamepadQuarantinedNavigators.contains(navigator.get()))
m_gamepadQuarantinedDOMWindows.add(window);
#endif
maybeStartMonitoringGamepads();
}
void GamepadManager::unregisterDOMWindow(LocalDOMWindow& window)
{
LOG(Gamepad, "(%u) GamepadManager unregistering LocalDOMWindow %p", (unsigned)getpid(), &window);
ASSERT(m_domWindows.contains(window));
m_domWindows.remove(window);
m_gamepadBlindDOMWindows.remove(window);
#if PLATFORM(VISION)
m_gamepadQuarantinedDOMWindows.remove(window);
#endif
maybeStopMonitoringGamepads();
}
#if PLATFORM(VISION)
void GamepadManager::updateQuarantineStatus()
{
if (m_gamepadQuarantinedNavigators.isEmptyIgnoringNullReferences() && m_gamepadQuarantinedDOMWindows.isEmptyIgnoringNullReferences())
return;
WeakHashSet<Navigator> navigators;
WeakHashSet<LocalDOMWindow, WeakPtrImplWithEventTargetData> windows;
for (auto& navigator : m_gamepadQuarantinedNavigators) {
auto page = navigator.protectedPage();
if (page && page->gamepadAccessGranted()) {
LOG(Gamepad, "(%u) GamepadManager found navigator %p to release from quarantine", (unsigned)getpid(), &navigator);
navigators.add(navigator);
}
}
for (auto& window : m_gamepadQuarantinedDOMWindows) {
auto page = window.protectedPage();
if (page && page->gamepadAccessGranted()) {
LOG(Gamepad, "(%u) GamepadManager found window %p to release from quarantine", (unsigned)getpid(), &window);
windows.add(window);
}
}
if (navigators.isEmptyIgnoringNullReferences() && windows.isEmptyIgnoringNullReferences())
return;
for (auto& navigator : navigators) {
m_gamepadBlindNavigators.add(navigator);
m_gamepadQuarantinedNavigators.remove(navigator);
}
for (auto& window : windows) {
m_gamepadBlindDOMWindows.add(window);
m_gamepadQuarantinedDOMWindows.remove(window);
}
}
#endif // PLATFORM(VISION)
void GamepadManager::maybeStartMonitoringGamepads()
{
if (m_isMonitoringGamepads)
return;
if (!m_navigators.isEmptyIgnoringNullReferences() || !m_domWindows.isEmptyIgnoringNullReferences()) {
LOG(Gamepad, "(%u) GamepadManager has %i NavigatorGamepads and %i DOMWindows registered, is starting gamepad monitoring", (unsigned)getpid(), m_navigators.computeSize(), m_domWindows.computeSize());
m_isMonitoringGamepads = true;
GamepadProvider::singleton().startMonitoringGamepads(*this);
}
}
void GamepadManager::maybeStopMonitoringGamepads()
{
if (!m_isMonitoringGamepads)
return;
if (m_navigators.isEmptyIgnoringNullReferences() && m_domWindows.isEmptyIgnoringNullReferences()) {
LOG(Gamepad, "(%u) GamepadManager has no NavigatorGamepads or DOMWindows registered, is stopping gamepad monitoring", (unsigned)getpid());
m_isMonitoringGamepads = false;
GamepadProvider::singleton().stopMonitoringGamepads(*this);
}
}
} // namespace WebCore
#endif // ENABLE(GAMEPAD)
|