From c44c2cadd036ed2834f0ee8c7b2ece43a2ad0f4f Mon Sep 17 00:00:00 2001 From: Michael Layzell Date: Fri, 29 May 2015 15:34:26 -0400 Subject: [PATCH] Bug 1170200 - Part 1: Change the API for nsIPermissionManager::Remove() to accept a URI instead of a string; r=ehsan --- extensions/cookie/nsPermissionManager.cpp | 28 +++++++- .../unit/test_permmanager_removepermission.js | 67 +++++++++++++++++++ extensions/cookie/test/unit/xpcshell.ini | 1 + netwerk/base/nsIPermissionManager.idl | 20 ++++-- 4 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 extensions/cookie/test/unit/test_permmanager_removepermission.js diff --git a/extensions/cookie/nsPermissionManager.cpp b/extensions/cookie/nsPermissionManager.cpp index c0d26ab74ab..c8e3e871b55 100644 --- a/extensions/cookie/nsPermissionManager.cpp +++ b/extensions/cookie/nsPermissionManager.cpp @@ -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 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 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() { diff --git a/extensions/cookie/test/unit/test_permmanager_removepermission.js b/extensions/cookie/test/unit/test_permmanager_removepermission.js new file mode 100644 index 00000000000..e8f5817e241 --- /dev/null +++ b/extensions/cookie/test/unit/test_permmanager_removepermission.js @@ -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; + } + } + } +} diff --git a/extensions/cookie/test/unit/xpcshell.ini b/extensions/cookie/test/unit/xpcshell.ini index e9e6333d18b..902d90a992a 100644 --- a/extensions/cookie/test/unit/xpcshell.ini +++ b/extensions/cookie/test/unit/xpcshell.ini @@ -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] diff --git a/netwerk/base/nsIPermissionManager.idl b/netwerk/base/nsIPermissionManager.idl index 00fcf56faaa..28be6bd3864 100644 --- a/netwerk/base/nsIPermissionManager.idl +++ b/netwerk/base/nsIPermissionManager.idl @@ -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. */