cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Philipp Kursawe <>
Subject Why doesn't `cordova.requestPermission` have an `callbackContext` arg?
Date Tue, 28 Jun 2016 08:29:26 GMT
I wonder about this API design decision.

The current API forces us to save the callback context in some state
variable to have access to it
in the plugins `onRequestPermissionResult` like this:

private void requestPermissionAction(CallbackContext callbackContext,
JSONArray permission) {
  this.permissionsCallback = callbackContext;
  cordova.requestPermission(this, REQUEST_CODE_ENABLE_PERMISSION,

public void onRequestPermissionResult(int requestCode, String[]
permissions, int[] grantResults) {
  if (this.permissionsCallback == null) {

  if (permissions != null && permissions.length > 0) {
  } else {
    this.permissionsCallback.error("No permission");
  this.permissionsCallback = null;

This works but limits the concurrent calls to the plugins
`requestPermissionAction` to one caller from the script at a time (that
means until the success or error callbacks are called in the script).
Otherwise the `this.permissionCallback` would be set again for a second
call and when the first calls `onRequestPermissionResult` is called it will
actually call the handlers of the second `requestPermissionAction`

Wouldn't this API signature make more sense?

cordova.requestPermission(CordovaPlugin plugin, int requestCode, String
permission, CallbackContext callbackContext);

public void onRequestPermissionResult(int requestCode, String[]
permissions, int[] grantResults, CallbackContext callbackContext) {
  if (permissions != null && permissions.length > 0) {
  } else {
    permissionsCallback.error("No permission");

And while we are at: Change `permission[]` and `grantResults[]` to an
object `results[permission] = grantResult`?

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message