-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gracefully handles when calling getGamepads() is not allowed... #5581
Changes from 4 commits
f7338f3
07650ee
7a6a13d
9eaeb0f
ab0065f
6a6bb02
a3f8df8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,26 +41,38 @@ module.exports.System = registerSystem('tracked-controls-webvr', { | |
* Update controller list. | ||
*/ | ||
updateControllerList: function () { | ||
var controllers = this.controllers; | ||
var gamepad; | ||
var gamepads; | ||
var i; | ||
var prevCount; | ||
try { | ||
var controllers = this.controllers; | ||
var gamepad; | ||
var gamepads; | ||
var i; | ||
var prevCount; | ||
|
||
gamepads = navigator.getGamepads && navigator.getGamepads(); | ||
if (!gamepads) { return; } | ||
gamepads = navigator.getGamepads && navigator.getGamepads(); | ||
if (!gamepads) { return; } | ||
|
||
prevCount = controllers.length; | ||
controllers.length = 0; | ||
for (i = 0; i < gamepads.length; ++i) { | ||
gamepad = gamepads[i]; | ||
if (gamepad && gamepad.pose) { | ||
controllers.push(gamepad); | ||
prevCount = controllers.length; | ||
controllers.length = 0; | ||
for (i = 0; i < gamepads.length; ++i) { | ||
gamepad = gamepads[i]; | ||
if (gamepad && gamepad.pose) { | ||
controllers.push(gamepad); | ||
} | ||
} | ||
} | ||
|
||
if (controllers.length !== prevCount) { | ||
this.el.emit('controllersupdated', undefined, false); | ||
if (controllers.length !== prevCount) { | ||
this.el.emit('controllersupdated', undefined, false); | ||
} | ||
} catch (e) { | ||
if (e.name === 'SecurityError') { | ||
if (window.self === window.top) { | ||
console.warn('A-Frame requires additional permissions to list the gamepads. The HTTP `Permissions-Policy` header must not exclude this origin in the `gamepad` directive.', e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for this message. How common is this issue? I think I've never seen a server excluding the gamepad API There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't seen this in the wild, but it would happen if an aframe app was deployed from an server with a very restrictive policy (as set in the Apache or Nginx configuration, which the a-frame app can't control). |
||
} else { | ||
console.warn('A-Frame requires additional permissions to list the gamepads. The iframe `allow` attribute must not block this origin.', e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Message needs rewording. A-Frame doesn't require additional permissions but the iframe permissions policy. Is not the solution to add gamepad to the allow attribute e.g There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. allow="gamepad" is equivalent to allow="gamepad 'self'" which is fine if the A-Frame app is at the same origin as the top-level page. If the A-Frame app is on a different origin (for example, if the aframe app is listed on itch.io but the app is served from elsewhere), you need allow="gamepad aframe-app-origin.com" or allow="gamepad *" |
||
} | ||
} else { | ||
console.error('Can\'t update controller list:', e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this error and above just warnings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the A-frame app doesn't need the non-webxr gamepads on this device, failing to get the gamepads will not keep the app from working. If the a-frame app does need gamepads on this device, not getting the gamepads is a problem that has to be fixed. Since this code can't determine which, a warning is reasonable. (However, if the error isn't caught at all, A-Frame doesn't fully initialize.) "Can't update controller list" would only occur if there was some unforeseen error and would likely be a programming error in A-Frame. |
||
} | ||
} | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this specific enough? We shouldn't check if we are in an iframe?
https://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No other code inside the try-catch can throw a SecurityError.