Bug 1235352 - Add JavaScript module for checking and requesting runtime permissions. r=nalexander

--HG--
extra : commitid : A7kDUJG7nyx
extra : rebase_source : d8e60fad79250d33ff668806175c746cbed67d34
This commit is contained in:
Sebastian Kaspari 2016-01-11 12:13:41 +01:00
parent 5de57b6ff3
commit 71a2884ca4
6 changed files with 139 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import org.mozilla.gecko.menu.MenuPanel;
import org.mozilla.gecko.mozglue.ContextUtils;
import org.mozilla.gecko.mozglue.ContextUtils.SafeIntent;
import org.mozilla.gecko.mozglue.GeckoLoader;
import org.mozilla.gecko.permissions.Permissions;
import org.mozilla.gecko.preferences.ClearOnShutdownPref;
import org.mozilla.gecko.preferences.GeckoPreferences;
import org.mozilla.gecko.prompts.PromptService;
@ -663,6 +664,26 @@ public abstract class GeckoApp
UpdateServiceHelper.downloadUpdate(this);
} else if ("Update:Install".equals(event)) {
UpdateServiceHelper.applyUpdate(this);
} else if ("RuntimePermissions:Prompt".equals(event)) {
String[] permissions = message.getStringArray("permissions");
if (callback == null || permissions == null) {
return;
}
Permissions.from(this)
.withPermissions(permissions)
.andFallback(new Runnable() {
@Override
public void run() {
callback.sendSuccess(false);
}
})
.run(new Runnable() {
@Override
public void run() {
callback.sendSuccess(true);
}
});
}
}
@ -1262,6 +1283,7 @@ public abstract class GeckoApp
"Locale:Set",
"Permissions:Data",
"PrivateBrowsing:Data",
"RuntimePermissions:Prompt",
"Session:StatePurged",
"Share:Text",
"Snackbar:Show",
@ -2087,6 +2109,7 @@ public abstract class GeckoApp
"Locale:Set",
"Permissions:Data",
"PrivateBrowsing:Data",
"RuntimePermissions:Prompt",
"Session:StatePurged",
"Share:Text",
"Snackbar:Show",
@ -2452,6 +2475,11 @@ public abstract class GeckoApp
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Permissions.onRequestPermissionsResult(this, permissions, grantResults);
}
@Override
public AbsoluteLayout getPluginContainer() { return mPluginContainer; }

View File

@ -0,0 +1,41 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
this.EXPORTED_SYMBOLS = ["RuntimePermissions"];
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Messaging", "resource://gre/modules/Messaging.jsm");
// See: http://developer.android.com/reference/android/Manifest.permission.html
const CAMERA = "android.permission.CAMERA";
const WRITE_EXTERNAL_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE";
const RECORD_AUDIO = "android.permission.RECORD_AUDIO";
var RuntimePermissions = {
CAMERA: CAMERA,
RECORD_AUDIO: RECORD_AUDIO,
WRITE_EXTERNAL_STORAGE: WRITE_EXTERNAL_STORAGE,
/**
* Check whether the permissions have been granted or not. If needed prompt the user to accept the permissions.
*
* @returns A promise resolving to true if all the permissions have been granted or false if any of the
* permissions have been denied.
*/
waitForPermissions: function(permission) {
let permissions = [].concat(permission);
let msg = {
type: 'RuntimePermissions:Prompt',
permissions: permissions
};
return Messaging.sendRequestForResult(msg);
}
};

View File

@ -25,6 +25,7 @@ EXTRA_JS_MODULES += [
'OrderedBroadcast.jsm',
'PageActions.jsm',
'Prompt.jsm',
'RuntimePermissions.jsm',
'Sanitizer.jsm',
'SharedPreferences.jsm',
'Snackbars.jsm',

View File

@ -106,6 +106,7 @@ skip-if = android_version == "18"
skip-if = android_version == "10" || android_version == "18"
[src/org/mozilla/gecko/tests/testReadingListCache.java]
[src/org/mozilla/gecko/tests/testRestrictions.java]
[src/org/mozilla/gecko/tests/testRuntimePermissionsAPI.java]
[src/org/mozilla/gecko/tests/testSnackbarAPI.java]
[src/org/mozilla/gecko/tests/testTrackingProtection.java]
# disabled on 4.3, bug 1158363

View File

@ -0,0 +1,48 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.tests;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.util.EventCallback;
import org.mozilla.gecko.util.NativeEventListener;
import org.mozilla.gecko.util.NativeJSObject;
import static org.mozilla.gecko.tests.helpers.AssertionHelper.fFail;
public class testRuntimePermissionsAPI extends JavascriptTest implements NativeEventListener {
public testRuntimePermissionsAPI() {
super("testRuntimePermissionsAPI.js");
}
@Override
public void setUp() throws Exception {
super.setUp();
EventDispatcher.getInstance().registerGeckoThreadListener(this, "RuntimePermissions:Prompt");
}
@Override
public void tearDown() throws Exception {
super.tearDown();
EventDispatcher.getInstance().unregisterGeckoThreadListener(this, "RuntimePermissions:Prompt");
}
@Override
public void handleMessage(String event, NativeJSObject message, EventCallback callback) {
mAsserter.is(event, "RuntimePermissions:Prompt", "Received RuntimePermissions:Prompt event");
try {
String[] permissions = message.getStringArray("permissions");
mAsserter.is(3, permissions.length, "Received three permissions");
mAsserter.is("android.permission.CAMERA", permissions[0], "Received CAMERA permission");
mAsserter.is("android.permission.WRITE_EXTERNAL_STORAGE", permissions[1], "Received WRITE_EXTERNAL_STORAGE permission");
mAsserter.is("android.permission.RECORD_AUDIO", permissions[2], "Received RECORD_AUDIO permission");
} catch (Exception e) {
fFail("Event does not contain expected data: " + e.getMessage());
}
}
}

View File

@ -0,0 +1,20 @@
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "RuntimePermissions", "resource://gre/modules/RuntimePermissions.jsm");
add_task(function* test_snackbar_api() {
RuntimePermissions.waitForPermissions([
RuntimePermissions.CAMERA,
RuntimePermissions.RECORD_AUDIO,
RuntimePermissions.WRITE_EXTERNAL_STORAGE
]);
});
run_next_test();