Bug 1170200 - Part 1: Change the API for nsIPermissionManager::Remove() to accept a URI instead of a string; r=ehsan

This commit is contained in:
Michael Layzell 2015-07-03 14:17:01 -04:00 committed by Ehsan Akhgari
parent 0df76506da
commit eb908c6aa5
4 changed files with 106 additions and 10 deletions

View File

@ -1052,11 +1052,13 @@ nsPermissionManager::AddInternal(nsIPrincipal* aPrincipal,
}
NS_IMETHODIMP
nsPermissionManager::Remove(const nsACString &aHost,
const char *aType)
nsPermissionManager::Remove(nsIURI* aURI,
const char* aType)
{
NS_ENSURE_ARG_POINTER(aURI);
nsCOMPtr<nsIPrincipal> principal;
nsresult rv = GetPrincipal(aHost, getter_AddRefs(principal));
nsresult rv = GetPrincipal(aURI, getter_AddRefs(principal));
NS_ENSURE_SUCCESS(rv, rv);
return RemoveFromPrincipal(principal, aType);
@ -1092,6 +1094,26 @@ nsPermissionManager::RemoveFromPrincipal(nsIPrincipal* aPrincipal,
eWriteToDB);
}
NS_IMETHODIMP
nsPermissionManager::RemovePermission(nsIPermission* aPerm)
{
nsAutoCString host;
nsresult rv = aPerm->GetHost(host);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPrincipal> principal;
rv = GetPrincipal(host, getter_AddRefs(principal));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString type;
rv = aPerm->GetType(type);
NS_ENSURE_SUCCESS(rv, rv);
// Permissions are uniquely identified by their principal and type.
// We remove the permission using these two pieces of data.
return RemoveFromPrincipal(principal, type.get());
}
NS_IMETHODIMP
nsPermissionManager::RemoveAll()
{

View File

@ -0,0 +1,67 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
function run_test() {
// initialize the permission manager service
let pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
do_check_eq(perm_count(), 0);
// add some permissions
let uri = NetUtil.newURI("http://amazon.com:8080/foobarbaz", null, null);
let uri2 = NetUtil.newURI("http://google.com:2048/quxx", null, null);
pm.add(uri, "apple", 0);
pm.add(uri, "apple", 3);
pm.add(uri, "pear", 3);
pm.add(uri, "pear", 1);
pm.add(uri, "cucumber", 1);
pm.add(uri, "cucumber", 1);
pm.add(uri, "cucumber", 1);
pm.add(uri2, "apple", 2);
pm.add(uri2, "pear", 0);
pm.add(uri2, "pear", 2);
// Make sure that removePermission doesn't remove more than one permission each time
do_check_eq(perm_count(), 5);
remove_one_by_type("apple");
do_check_eq(perm_count(), 4);
remove_one_by_type("apple");
do_check_eq(perm_count(), 3);
remove_one_by_type("pear");
do_check_eq(perm_count(), 2);
remove_one_by_type("cucumber");
do_check_eq(perm_count(), 1);
remove_one_by_type("pear");
do_check_eq(perm_count(), 0);
function perm_count() {
let enumerator = pm.enumerator;
let count = 0;
while (enumerator.hasMoreElements()) {
count++;
enumerator.getNext();
}
return count;
}
function remove_one_by_type(type) {
let enumerator = pm.enumerator;
while (enumerator.hasMoreElements()) {
let it = enumerator.getNext().QueryInterface(Ci.nsIPermission);
if (it.type == type) {
pm.removePermission(it);
break;
}
}
}
}

View File

@ -34,3 +34,4 @@ skip-if = debug == true
[test_permmanager_cleardata.js]
[test_schema_2_migration.js]
[test_schema_3_migration.js]
[test_permmanager_removepermission.js]

View File

@ -37,7 +37,7 @@ interface nsIDOMWindow;
interface nsIPermission;
interface nsISimpleEnumerator;
[scriptable, uuid(93a156f8-bcc8-4568-a214-389b073332dd)]
[scriptable, uuid(0d1b8c65-0359-4a8c-b94d-4d3643b23e61)]
interface nsIPermissionManager : nsISupports
{
/**
@ -106,17 +106,16 @@ interface nsIPermissionManager : nsISupports
[optional] in int64_t expireTime);
/**
* Remove permission information for a given host string and permission type.
* The host string represents the exact entry in the permission list (such as
* obtained from the enumerator), not a URI which that permission might apply
* to.
* Remove permission information for a given URI and permission type. This will
* remove the permission for the entire host described by the uri, acting as the
* opposite operation to the add() method.
*
* @param host the host to remove the permission for
* @param uri the uri to remove the permission for
* @param type a case-sensitive ASCII string, identifying the consumer.
* The type must have been previously registered using the
* add() method.
*/
void remove(in AUTF8String host,
void remove(in nsIURI uri,
in string type);
/**
@ -127,6 +126,13 @@ interface nsIPermissionManager : nsISupports
*/
void removeFromPrincipal(in nsIPrincipal principal, in string type);
/**
* Remove the given permission from the permission manager.
*
* @param perm a permission obtained from the permission manager.
*/
void removePermission(in nsIPermission perm);
/**
* Clear permission information for all websites.
*/