Skip to content
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

Closed

Conversation

DougReeder
Copy link
Contributor

… from the context (such as an IFRAME), by the Document's Permission Policy, by catching the DOMException.

When AFrame is run in an IFRAME without the needed permission, the call to Navigator.getGamepads() throws a DOMException which prevents AFrame from loading properly.

…alling getGamepads() from the context (such as an IFRAME).
@dmarcos
Copy link
Member

dmarcos commented Sep 28, 2024

warn message is not super useful. Can we just detect we're in an iframe environment and show a more descriptive message?

@DougReeder
Copy link
Contributor Author

If the IFRAME is something like <iframe src="https://other.com/game" allow="gamepad"></iframe>, it will have permission to get gamepads. There are other permissions an IFRAME might or might not have, such as accelerometer, fullscreen, gyroscope, and xr-spatial-tracking. Permissions-Policy These permissions can also be controlled for the top-level context (the page). So it's not clear to me that whether or not we're in an IFRAME is useful info.

What error message would you consider useful?

@dmarcos
Copy link
Member

dmarcos commented Sep 29, 2024

What error message would you consider useful?

Not sure. Something that helps the dev understand what's going on and how to fix it. e.g:

You are running A-Frame inside an iframe that requires additional permissions to access some APIs . In order to access the gamepads API you need to add `gamepads` to the allow attribute

Message in PR not super useful can't update controller list

@dmarcos
Copy link
Member

dmarcos commented Sep 29, 2024

left a comment in PR

@@ -64,7 +64,11 @@ module.exports.System = registerSystem('tracked-controls-webvr', {
this.el.emit('controllersupdated', undefined, false);
}
} catch (e) {
console.warn('A-Frame requires additional permissions to list the gamepads. If this is running in an IFRAME, you need to add `gamepads` to the `allow` attribute. If this is running as the top-level page, the HTTP `Permissions-Policy` header must not exclude this origin in the `gamepad` directive.', e);
if (e.name === 'SecurityError') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

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);
} else {
console.warn('A-Frame requires additional permissions to list the gamepads. The iframe `allow` attribute must not block this origin.', e);
Copy link
Member

Choose a reason for hiding this comment

The 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 allow="gamepad"? allow doesn't bock origins afaik?

Copy link
Contributor Author

@DougReeder DougReeder Oct 1, 2024

Choose a reason for hiding this comment

The 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 *"

} 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);
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

@DougReeder DougReeder Oct 1, 2024

Choose a reason for hiding this comment

The 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).

console.warn('A-Frame requires additional permissions to list the gamepads. The iframe `allow` attribute must not block this origin.', e);
}
} else {
console.error('Can\'t update controller list:', e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this error and above just warnings?

Copy link
Contributor Author

@DougReeder DougReeder Oct 1, 2024

Choose a reason for hiding this comment

The 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.

if (window.self === window.top) {
console.warn('The HTTP `Permissions-Policy` header must not block this origin in the `gamepad` directive, to allow A-Frame to list the gamepads.', e);
} else {
console.warn('The iframe `allow` attribute must not block the origin of this A-Frame app, to allow A-Frame to list the gamepads.', e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is still not clear. I wouldn't know what to do and I'm well versed in web dev. What about?

It looks like your A-Frame experience is running inside an iframe. In order to access the gamepads API you need to add allow="gamepad" to your iframe attributes

} catch (e) {
if (e.name === 'SecurityError') {
if (window.self === window.top) {
console.warn('The HTTP `Permissions-Policy` header must not block this origin in the `gamepad` directive, to allow A-Frame to list the gamepads.', e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this. Seems pretty rare. Can add later if necessary

@dmarcos
Copy link
Member

dmarcos commented Oct 2, 2024

What is the general context of this error? Are you still using the webvr code path?

@DougReeder
Copy link
Contributor Author

See this example of the current code failing to load properly

examples/test/iframe/index.html in the PR has been modified to show the PR code gracefully handling the lack of gamepad permission.

@dmarcos
Copy link
Member

dmarcos commented Oct 14, 2024

Two thoughts here

  1. Don't think we should check for gamePads if the scene is not in VR mode. I would add a guard here to check for this.el.is('vr-mode') here https://github.com/aframevr/aframe/blob/master/src/systems/tracked-controls-webvr.js#L50

It should get rid of the error message

  1. For next version we should deprecate the WebVR path and all of this will be a non-issue. There's a WIP PR open Remove WebVR support and polyfill #5451

@DougReeder
Copy link
Contributor Author

Adding that check does allow A-Frame to start up, but as soon as the user enters vr-mode, the Uncaught SecurityError prevents tick() from running normally and rendering doesn't happen, unless we catch the SecurityError.

@dmarcos
Copy link
Member

dmarcos commented Oct 15, 2024

I merged a PR to remove WebVR support. This should no longer be an issue. Can you give master builds a try and let me know?

https://cdn.jsdelivr.net/gh/aframevr/aframe@ce9fd2321c97daf01bf9f098ad8b625c1603bc63/dist/aframe-master.min.js

Thanks!

@DougReeder
Copy link
Contributor Author

With that version of A-Frame, I do not observe the issue, either on startup or in the tick() handler.

@dmarcos
Copy link
Member

dmarcos commented Oct 21, 2024

Thanks for the PR, the back and forth and verifying that master is now working. Now that we have deprecated WebVR we no longer need this PR

@dmarcos dmarcos closed this Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants