Bug 1170200 - Part 1: Change the API for nsIPermissionManager::Remove() to accept a URI instead of a string; r=ehsan
authorMichael Layzell <michael@thelayzells.com>
Fri, 29 May 2015 15:34:26 -0400
changeset 251350 01c9c00474195e70321329694c3a0ff7e0585b6a
parent 251349 057f0bd4075c27aec5f5c618553730de463b436a
child 251351 d716c5d113d89994b43b3bc4dfb017043631c2d7
push id61839
push usereakhgari@mozilla.com
push dateFri, 03 Jul 2015 21:12:50 +0000
treeherdermozilla-inbound@d716c5d113d8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1170200
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1170200 - Part 1: Change the API for nsIPermissionManager::Remove() to accept a URI instead of a string; r=ehsan
extensions/cookie/nsPermissionManager.cpp
extensions/cookie/test/unit/test_permmanager_removepermission.js
extensions/cookie/test/unit/xpcshell.ini
netwerk/base/nsIPermissionManager.idl
--- a/extensions/cookie/nsPermissionManager.cpp
+++ b/extensions/cookie/nsPermissionManager.cpp
@@ -1047,21 +1047,23 @@ nsPermissionManager::AddInternal(nsIPrin
     }
     break;
   }
 
   return NS_OK;
 }
 
 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);
 }
 
 NS_IMETHODIMP
 nsPermissionManager::RemoveFromPrincipal(nsIPrincipal* aPrincipal,
                                          const char* aType)
@@ -1088,16 +1090,36 @@ nsPermissionManager::RemoveFromPrincipal
                      nsIPermissionManager::EXPIRE_NEVER,
                      0,
                      0,
                      eNotify,
                      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()
 {
   ENSURE_NOT_CHILD_PROCESS;
   return RemoveAllInternal(true);
 }
 
 NS_IMETHODIMP
 nsPermissionManager::RemoveAllSince(int64_t aSince)
new file mode 100644
--- /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;
+      }
+    }
+  }
+}
--- a/extensions/cookie/test/unit/xpcshell.ini
+++ b/extensions/cookie/test/unit/xpcshell.ini
@@ -29,8 +29,9 @@ support-files =
 skip-if = debug == true
 [test_permmanager_idn.js]
 [test_permmanager_subdomains.js]
 [test_permmanager_local_files.js]
 [test_permmanager_mailto.js]
 [test_permmanager_cleardata.js]
 [test_schema_2_migration.js]
 [test_schema_3_migration.js]
+[test_permmanager_removepermission.js]
--- a/netwerk/base/nsIPermissionManager.idl
+++ b/netwerk/base/nsIPermissionManager.idl
@@ -32,17 +32,17 @@
 
 interface nsIURI;
 interface nsIObserver;
 interface nsIPrincipal;
 interface nsIDOMWindow;
 interface nsIPermission;
 interface nsISimpleEnumerator;
 
-[scriptable, uuid(93a156f8-bcc8-4568-a214-389b073332dd)]
+[scriptable, uuid(0d1b8c65-0359-4a8c-b94d-4d3643b23e61)]
 interface nsIPermissionManager : nsISupports
 {
   /**
    * Predefined return values for the testPermission method and for
    * the permission param of the add method
    * NOTE: UNKNOWN_ACTION (0) is reserved to represent the
    * default permission when no entry is found for a host, and
    * should not be used by consumers to indicate otherwise.
@@ -101,38 +101,44 @@ interface nsIPermissionManager : nsISupp
    * granted permissions.
    */
   void addFromPrincipal(in nsIPrincipal principal, in string typed,
                         in uint32_t permission,
                         [optional] in uint32_t expireType,
                         [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);
 
   /**
    * Remove permission information for a given principal.
    * This is internally calling remove() with the host from the principal's URI.
    * Passing system principal will be a no-op because we never add them to the
    * database.
    */
   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.
    */
   void removeAll();
 
   /**
    * Clear all permission information added since the specified time.
    */
   void removeAllSince(in int64_t since);