Skip to content

Commit 9295b0a

Browse files
authored
Guard against Chrome moving Gamepad to HTTPS secure origin (#20890)
* Chrome has (apparently?) moved to requiring a secure HTTPS origin in order to access gamepad data. Therefore constrain HTML5 Gamepad API functions to return EMSCRIPTEN_RESULT_NOT_SUPPORTED when getGamepads() does not work but throws an exception. * Add Closure annotation * Use err()
1 parent 7bed0e2 commit 9295b0a

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/library_html5.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,24 +2144,38 @@ var LibraryHTML5 = {
21442144
return JSEvents.registerOrRemoveHandler(eventHandler);
21452145
},
21462146

2147+
$disableGamepadApiIfItThrows__docs: '/** @suppress {checkTypes} */',
2148+
$disableGamepadApiIfItThrows: () => {
2149+
try {
2150+
navigator.getGamepads();
2151+
} catch(e) {
2152+
#if ASSERTIONS
2153+
err(`navigator.getGamepads() exists, but failed to execute with exception ${e}. Disabling Gamepad access.`);
2154+
#endif
2155+
navigator.getGamepads = null; // Disable getGamepads() so that other functions will not attempt to use it.
2156+
return 1;
2157+
}
2158+
},
2159+
21472160
emscripten_set_gamepadconnected_callback_on_thread__proxy: 'sync',
2148-
emscripten_set_gamepadconnected_callback_on_thread__deps: ['$registerGamepadEventCallback'],
2161+
emscripten_set_gamepadconnected_callback_on_thread__deps: ['$registerGamepadEventCallback', '$disableGamepadApiIfItThrows'],
21492162
emscripten_set_gamepadconnected_callback_on_thread: (userData, useCapture, callbackfunc, targetThread) => {
2150-
if (!navigator.getGamepads && !navigator.webkitGetGamepads) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
2163+
if (!navigator.getGamepads || disableGamepadApiIfItThrows()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
21512164
return registerGamepadEventCallback({{{ cDefs.EMSCRIPTEN_EVENT_TARGET_WINDOW }}}, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_GAMEPADCONNECTED }}}, "gamepadconnected", targetThread);
21522165
},
21532166

21542167
emscripten_set_gamepaddisconnected_callback_on_thread__proxy: 'sync',
2155-
emscripten_set_gamepaddisconnected_callback_on_thread__deps: ['$registerGamepadEventCallback'],
2168+
emscripten_set_gamepaddisconnected_callback_on_thread__deps: ['$registerGamepadEventCallback', '$disableGamepadApiIfItThrows'],
21562169
emscripten_set_gamepaddisconnected_callback_on_thread: (userData, useCapture, callbackfunc, targetThread) => {
2157-
if (!navigator.getGamepads && !navigator.webkitGetGamepads) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
2170+
if (!navigator.getGamepads || disableGamepadApiIfItThrows()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
21582171
return registerGamepadEventCallback({{{ cDefs.EMSCRIPTEN_EVENT_TARGET_WINDOW }}}, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_GAMEPADDISCONNECTED }}}, "gamepaddisconnected", targetThread);
21592172
},
21602173

21612174
emscripten_sample_gamepad_data__proxy: 'sync',
2162-
emscripten_sample_gamepad_data__deps: ['$JSEvents'],
2175+
emscripten_sample_gamepad_data__deps: ['$JSEvents', '$disableGamepadApiIfItThrows'],
21632176
emscripten_sample_gamepad_data: () => {
2164-
return (JSEvents.lastGamepadState = (navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : null)))
2177+
if (!navigator.getGamepads || disableGamepadApiIfItThrows()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
2178+
return (JSEvents.lastGamepadState = navigator.getGamepads())
21652179
? {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}} : {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
21662180
},
21672181

@@ -2182,7 +2196,6 @@ var LibraryHTML5 = {
21822196
#if ASSERTIONS
21832197
if (!JSEvents.lastGamepadState) throw 'emscripten_get_gamepad_status() can only be called after having first called emscripten_sample_gamepad_data() and that function has returned EMSCRIPTEN_RESULT_SUCCESS!';
21842198
#endif
2185-
21862199
// INVALID_PARAM is returned on a Gamepad index that never was there.
21872200
if (index < 0 || index >= JSEvents.lastGamepadState.length) return {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_PARAM }}};
21882201

0 commit comments

Comments
 (0)