Float07 opened a new issue, #1888:
URL: https://github.com/apache/cordova-android/issues/1888

   # Bug Report
   
   ## Problem
   
   ### What is expected to happen?
   Requesting permission to use media resources from the WebView (with JS code) 
should throw an error if the native permission for that resource isn't granted.
   
   For example, if I call:
   
   ```js
   navigator.mediaDevices.getUserMedia({video: true})
        .then(() => console.log('success!'))
        .catch(() => console.log('error!'));
   ```
   
   I expected `error!` to be logged if the app doesn't have native permission 
to use the camera.
   
   ### What does actually happen?
   The expected behavior happens the first time. But every request after that 
results in an infinite Promise that never resolves:
   
   <img width="812" height="327" alt="Image" 
src="https://github.com/user-attachments/assets/3d39af91-6c27-4952-82ec-da36e57dcafa";
 />
   
   Even after native permission is granted, these promises are stuck like that. 
The only way to fix it is restarting the WebView somehow (for example, by 
restarting the app)
   
   ## Information
   I did some research to try and fix it, and it led me to this code:
   
   ```java
       @Override
       public void onPermissionRequest(final PermissionRequest request) {
           LOG.d(LOG_TAG, "onPermissionRequest: " + 
Arrays.toString(request.getResources()));
           request.grant(request.getResources());
       }
   ```
   
   Here's the link to the commit that added it: 
https://github.com/apache/cordova-android/commit/eb70f05168dcf3a07d8716c772db3a3c3c3a59f8
   
   And here's the PR that the commit above closes: 
https://github.com/apache/cordova-android/pull/178
   
   That code will **always** grant the permission to the WebView, without 
checking if the app even has that permission. If the native permission is not 
granted, an error is thrown, which would be fine by itself, but the code also 
breaks, and future requests for permissions coming from the WebView won't even 
trigger the `onPermissionRequest` callback, which I believe simply stops 
working after the first error.
   
   This probably would work fine for versions before Android 6, when 
permissions were granted when the app is being installed (at least I assume so).
   
   There are ways to get around that. For example, by always checking the 
permission with native  code before using JS code to request permission. But 
that's not ideal, and leads to more complex code that can break more easily.
   
   Also, I tried overriding the `onPermissionRequest` callback with my own 
code. This works, but is very unreliable, as Cordova sometimes overrides my 
override. Still, this helped me be more confident that the issue is indeed 
related to this code.
   
   ### Command or Code
   Simply run this in the WebView console **before** native permission is 
granted:
   
   ```js
   navigator.mediaDevices.getUserMedia({video: true})
        .then(() => console.log('success!'))
        .catch(() => console.log('error!'));
   ```
   
   
   ### Environment, Platform, Device
   I tested and successfully reproduced this issue on:
   - Pixel 7 Pro (Android 13) virtual device, running on Android Studio on 
Ubuntu
   - Galaxy A01 (Android 6) physical device
   
   
   
   ### Version information
   I'm using the [Meteor](https://www.meteor.com/) framework. More 
specifically, [email protected]
   
   
   ## Checklist
   <!-- Please check the boxes by putting an x in the [ ] like so: [x] -->
   
   - [x] I searched for existing GitHub issues
   - [x] I updated all Cordova tooling to most recent version
   - [x] I included all the necessary information above
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to