Bug 1467713 - Part 2 - Reorder "permissions.js" to match "sitePermissions.js". r=jaws
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Tue, 05 Jun 2018 15:26:14 +0100
changeset 422017 702a0f9d90233a69b8761d2755ce97073a2d23f6
parent 422016 52724ae751a5e196e34cb8db096c6856ac7aee0f
child 422018 cd9e34c79fdb5bb215f7175baa04c6c54bc519a3
push id34114
push userbtara@mozilla.com
push dateSat, 09 Jun 2018 15:31:58 +0000
treeherdermozilla-central@e02a5155d815 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws
bugs1467713
milestone62.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 1467713 - Part 2 - Reorder "permissions.js" to match "sitePermissions.js". r=jaws Reordering the functions makes it easier to port the implementation across the files and merge them eventually. MozReview-Commit-ID: EkVbTA0YEQ5
browser/components/preferences/permissions.js
browser/components/preferences/permissions.xul
--- a/browser/components/preferences/permissions.js
+++ b/browser/components/preferences/permissions.js
@@ -81,161 +81,16 @@ var gPermissionManager = {
     getCellProperties(row, column) {
       if (column.element.getAttribute("id") == "siteCol")
         return "ltr";
 
       return "";
     }
   },
 
-  _getCapabilityString(aCapability) {
-    var stringKey = null;
-    switch (aCapability) {
-    case nsIPermissionManager.ALLOW_ACTION:
-      stringKey = "can";
-      break;
-    case nsIPermissionManager.DENY_ACTION:
-      stringKey = "cannot";
-      break;
-    case nsICookiePermission.ACCESS_ALLOW_FIRST_PARTY_ONLY:
-      stringKey = "canAccessFirstParty";
-      break;
-    case nsICookiePermission.ACCESS_SESSION:
-      stringKey = "canSession";
-      break;
-    }
-    return this._bundle.getString(stringKey);
-  },
-
-  addPermission(aCapability) {
-    var textbox = document.getElementById("url");
-    var input_url = textbox.value.replace(/^\s*/, ""); // trim any leading space
-    let principal;
-    try {
-      // The origin accessor on the principal object will throw if the
-      // principal doesn't have a canonical origin representation. This will
-      // help catch cases where the URI parser parsed something like
-      // `localhost:8080` as having the scheme `localhost`, rather than being
-      // an invalid URI. A canonical origin representation is required by the
-      // permission manager for storage, so this won't prevent any valid
-      // permissions from being entered by the user.
-      let uri;
-      try {
-        uri = Services.io.newURI(input_url);
-        principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
-        if (principal.origin.startsWith("moz-nullprincipal:")) {
-          throw "Null principal";
-        }
-      } catch (ex) {
-        uri = Services.io.newURI("http://" + input_url);
-        principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
-        // If we have ended up with an unknown scheme, the following will throw.
-        principal.origin;
-      }
-    } catch (ex) {
-      document.l10n.formatValues([
-        {id: "permissions-invalid-uri-title"},
-        {id: "permissions-invalid-uri-label"}
-      ]).then(([title, message]) => {
-        Services.prompt.alert(window, title, message);
-      });
-      return;
-    }
-
-    var capabilityString = this._getCapabilityString(aCapability);
-
-    // check whether the permission already exists, if not, add it
-    let permissionExists = false;
-    let capabilityExists = false;
-    for (var i = 0; i < this._permissions.length; ++i) {
-      if (this._permissions[i].principal.equals(principal)) {
-        permissionExists = true;
-        capabilityExists = this._permissions[i].capability == capabilityString;
-        if (!capabilityExists) {
-          this._permissions[i].capability = capabilityString;
-        }
-        break;
-      }
-    }
-
-    let permissionParams = {principal, type: this._type, capability: aCapability};
-    if (!permissionExists) {
-      this._permissionsToAdd.set(principal.origin, permissionParams);
-      this._addPermission(permissionParams);
-    } else if (!capabilityExists) {
-      this._permissionsToAdd.set(principal.origin, permissionParams);
-      this._handleCapabilityChange();
-    }
-
-    textbox.value = "";
-    textbox.focus();
-
-    // covers a case where the site exists already, so the buttons don't disable
-    this.onHostInput(textbox);
-
-    // enable "remove all" button as needed
-    document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
-  },
-
-  _removePermission(aPermission) {
-    this._removePermissionFromList(aPermission.principal);
-
-    // If this permission was added during this session, let's remove
-    // it from the pending adds list to prevent calls to the
-    // permission manager.
-    let isNewPermission = this._permissionsToAdd.delete(aPermission.principal.origin);
-
-    if (!isNewPermission) {
-      this._permissionsToDelete.set(aPermission.principal.origin, aPermission);
-    }
-
-  },
-
-  _handleCapabilityChange() {
-    // Re-do the sort, if the status changed from Block to Allow
-    // or vice versa, since if we're sorted on status, we may no
-    // longer be in order.
-    if (this._lastPermissionSortColumn == "statusCol") {
-      this._resortPermissions();
-    }
-    this._tree.treeBoxObject.invalidate();
-  },
-
-  _addPermission(aPermission) {
-    this._addPermissionToList(aPermission);
-    ++this._view._rowCount;
-    this._tree.treeBoxObject.rowCountChanged(this._view.rowCount - 1, 1);
-    // Re-do the sort, since we inserted this new item at the end.
-    this._resortPermissions();
-  },
-
-  _resortPermissions() {
-    gTreeUtils.sort(this._tree, this._view, this._permissions,
-                    this._lastPermissionSortColumn,
-                    this._permissionsComparator,
-                    this._lastPermissionSortColumn,
-                    !this._lastPermissionSortAscending); // keep sort direction
-  },
-
-  onHostInput(aSiteField) {
-    document.getElementById("btnSession").disabled = !aSiteField.value;
-    document.getElementById("btnBlock").disabled = !aSiteField.value;
-    document.getElementById("btnAllow").disabled = !aSiteField.value;
-  },
-
-  onWindowKeyPress(aEvent) {
-    if (aEvent.keyCode == KeyEvent.DOM_VK_ESCAPE)
-      window.close();
-  },
-
-  onHostKeyPress(aEvent) {
-    if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN)
-      document.getElementById("btnAllow").click();
-  },
-
   onLoad() {
     this._bundle = document.getElementById("bundlePreferences");
     var params = window.arguments[0];
     document.mozSubdialogReady = this.init(params);
   },
 
   async init(aParams) {
     if (this._type) {
@@ -325,21 +180,216 @@ var gPermissionManager = {
         }
         this._handleCapabilityChange();
       } else if (aData == "deleted") {
         this._removePermissionFromList(permission.principal);
       }
     }
   },
 
-  onPermissionSelect() {
-    var hasSelection = this._tree.view.selection.count > 0;
-    var hasRows = this._tree.view.rowCount > 0;
-    document.getElementById("removePermission").disabled = !hasRows || !hasSelection;
-    document.getElementById("removeAllPermissions").disabled = !hasRows;
+  _resortPermissions() {
+    gTreeUtils.sort(this._tree, this._view, this._permissions,
+                    this._lastPermissionSortColumn,
+                    this._permissionsComparator,
+                    this._lastPermissionSortColumn,
+                    !this._lastPermissionSortAscending); // keep sort direction
+  },
+
+  _handleCapabilityChange() {
+    // Re-do the sort, if the status changed from Block to Allow
+    // or vice versa, since if we're sorted on status, we may no
+    // longer be in order.
+    if (this._lastPermissionSortColumn == "statusCol") {
+      this._resortPermissions();
+    }
+    this._tree.treeBoxObject.invalidate();
+  },
+
+  _getCapabilityString(aCapability) {
+    var stringKey = null;
+    switch (aCapability) {
+    case nsIPermissionManager.ALLOW_ACTION:
+      stringKey = "can";
+      break;
+    case nsIPermissionManager.DENY_ACTION:
+      stringKey = "cannot";
+      break;
+    case nsICookiePermission.ACCESS_ALLOW_FIRST_PARTY_ONLY:
+      stringKey = "canAccessFirstParty";
+      break;
+    case nsICookiePermission.ACCESS_SESSION:
+      stringKey = "canSession";
+      break;
+    }
+    return this._bundle.getString(stringKey);
+  },
+
+  _addPermission(aPermission) {
+    this._addPermissionToList(aPermission);
+    ++this._view._rowCount;
+    this._tree.treeBoxObject.rowCountChanged(this._view.rowCount - 1, 1);
+    // Re-do the sort, since we inserted this new item at the end.
+    this._resortPermissions();
+  },
+
+  _addPermissionToList(aPermission) {
+    if (aPermission.type == this._type &&
+        (!this._manageCapability ||
+         (aPermission.capability == this._manageCapability))) {
+
+      var principal = aPermission.principal;
+      var capabilityString = this._getCapabilityString(aPermission.capability);
+      var p = new Permission(principal,
+                             aPermission.type,
+                             capabilityString);
+      this._permissions.push(p);
+    }
+  },
+
+  addPermission(aCapability) {
+    var textbox = document.getElementById("url");
+    var input_url = textbox.value.replace(/^\s*/, ""); // trim any leading space
+    let principal;
+    try {
+      // The origin accessor on the principal object will throw if the
+      // principal doesn't have a canonical origin representation. This will
+      // help catch cases where the URI parser parsed something like
+      // `localhost:8080` as having the scheme `localhost`, rather than being
+      // an invalid URI. A canonical origin representation is required by the
+      // permission manager for storage, so this won't prevent any valid
+      // permissions from being entered by the user.
+      let uri;
+      try {
+        uri = Services.io.newURI(input_url);
+        principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
+        if (principal.origin.startsWith("moz-nullprincipal:")) {
+          throw "Null principal";
+        }
+      } catch (ex) {
+        uri = Services.io.newURI("http://" + input_url);
+        principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
+        // If we have ended up with an unknown scheme, the following will throw.
+        principal.origin;
+      }
+    } catch (ex) {
+      document.l10n.formatValues([
+        {id: "permissions-invalid-uri-title"},
+        {id: "permissions-invalid-uri-label"}
+      ]).then(([title, message]) => {
+        Services.prompt.alert(window, title, message);
+      });
+      return;
+    }
+
+    var capabilityString = this._getCapabilityString(aCapability);
+
+    // check whether the permission already exists, if not, add it
+    let permissionExists = false;
+    let capabilityExists = false;
+    for (var i = 0; i < this._permissions.length; ++i) {
+      if (this._permissions[i].principal.equals(principal)) {
+        permissionExists = true;
+        capabilityExists = this._permissions[i].capability == capabilityString;
+        if (!capabilityExists) {
+          this._permissions[i].capability = capabilityString;
+        }
+        break;
+      }
+    }
+
+    let permissionParams = {principal, type: this._type, capability: aCapability};
+    if (!permissionExists) {
+      this._permissionsToAdd.set(principal.origin, permissionParams);
+      this._addPermission(permissionParams);
+    } else if (!capabilityExists) {
+      this._permissionsToAdd.set(principal.origin, permissionParams);
+      this._handleCapabilityChange();
+    }
+
+    textbox.value = "";
+    textbox.focus();
+
+    // covers a case where the site exists already, so the buttons don't disable
+    this.onHostInput(textbox);
+
+    // enable "remove all" button as needed
+    document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
+  },
+
+  _removePermission(aPermission) {
+    this._removePermissionFromList(aPermission.principal);
+
+    // If this permission was added during this session, let's remove
+    // it from the pending adds list to prevent calls to the
+    // permission manager.
+    let isNewPermission = this._permissionsToAdd.delete(aPermission.principal.origin);
+
+    if (!isNewPermission) {
+      this._permissionsToDelete.set(aPermission.principal.origin, aPermission);
+    }
+
+  },
+
+  _removePermissionFromList(aPrincipal) {
+    for (let i = 0; i < this._permissions.length; ++i) {
+      if (this._permissions[i].principal.equals(aPrincipal)) {
+        this._permissions.splice(i, 1);
+        this._view._rowCount--;
+        this._tree.treeBoxObject.rowCountChanged(this._view.rowCount - 1, -1);
+        this._tree.treeBoxObject.invalidate();
+        break;
+      }
+    }
+  },
+
+  _loadPermissions() {
+    this._tree = document.getElementById("permissionsTree");
+    this._permissions = [];
+
+    // load permissions into a table
+    var enumerator = Services.perms.enumerator;
+    while (enumerator.hasMoreElements()) {
+      var nextPermission = enumerator.getNext().QueryInterface(Ci.nsIPermission);
+      this._addPermissionToList(nextPermission);
+    }
+
+    this._view._rowCount = this._permissions.length;
+
+    // sort and display the table
+    this._tree.view = this._view;
+    this.onPermissionSort("origin");
+
+    // disable "remove all" button if there are none
+    document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
+  },
+
+  onWindowKeyPress(aEvent) {
+    if (aEvent.keyCode == KeyEvent.DOM_VK_ESCAPE)
+      window.close();
+  },
+
+  onPermissionKeyPress(aEvent) {
+    if (aEvent.keyCode == KeyEvent.DOM_VK_DELETE) {
+      this.onPermissionDelete();
+    } else if (AppConstants.platform == "macosx" &&
+               aEvent.keyCode == KeyEvent.DOM_VK_BACK_SPACE) {
+      this.onPermissionDelete();
+      aEvent.preventDefault();
+    }
+  },
+
+  onHostKeyPress(aEvent) {
+    if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN)
+      document.getElementById("btnAllow").click();
+  },
+
+  onHostInput(aSiteField) {
+    document.getElementById("btnSession").disabled = !aSiteField.value;
+    document.getElementById("btnBlock").disabled = !aSiteField.value;
+    document.getElementById("btnAllow").disabled = !aSiteField.value;
   },
 
   onPermissionDelete() {
     if (!this._view.rowCount)
       return;
     var removedPermissions = [];
     gTreeUtils.deleteSelectedItems(this._tree, this._view, this._permissions, removedPermissions);
     for (var i = 0; i < removedPermissions.length; ++i) {
@@ -358,47 +408,21 @@ var gPermissionManager = {
     for (var i = 0; i < removedPermissions.length; ++i) {
       var p = removedPermissions[i];
       this._removePermission(p);
     }
     document.getElementById("removePermission").disabled = true;
     document.getElementById("removeAllPermissions").disabled = true;
   },
 
-  onPermissionKeyPress(aEvent) {
-    if (aEvent.keyCode == KeyEvent.DOM_VK_DELETE) {
-      this.onPermissionDelete();
-    } else if (AppConstants.platform == "macosx" &&
-               aEvent.keyCode == KeyEvent.DOM_VK_BACK_SPACE) {
-      this.onPermissionDelete();
-      aEvent.preventDefault();
-    }
-  },
-
-  _lastPermissionSortColumn: "",
-  _lastPermissionSortAscending: false,
-  _permissionsComparator(a, b) {
-    return a.toLowerCase().localeCompare(b.toLowerCase());
-  },
-
-
-  onPermissionSort(aColumn) {
-    this._lastPermissionSortAscending = gTreeUtils.sort(this._tree,
-                                                        this._view,
-                                                        this._permissions,
-                                                        aColumn,
-                                                        this._permissionsComparator,
-                                                        this._lastPermissionSortColumn,
-                                                        this._lastPermissionSortAscending);
-    this._lastPermissionSortColumn = aColumn;
-    let sortDirection = this._lastPermissionSortAscending ? "descending" : "ascending";
-    let cols = document.querySelectorAll("treecol");
-    cols.forEach(c => c.removeAttribute("sortDirection"));
-    let column = document.querySelector(`treecol[data-field-name=${aColumn}]`);
-    column.setAttribute("sortDirection", sortDirection);
+  onPermissionSelect() {
+    var hasSelection = this._tree.view.selection.count > 0;
+    var hasRows = this._tree.view.rowCount > 0;
+    document.getElementById("removePermission").disabled = !hasRows || !hasSelection;
+    document.getElementById("removeAllPermissions").disabled = !hasRows;
   },
 
   onApplyChanges() {
     // Stop observing permission changes since we are about
     // to write out the pending adds/deletes and don't need
     // to update the UI
     this.uninit();
 
@@ -408,61 +432,36 @@ var gPermissionManager = {
 
     for (let p of this._permissionsToDelete.values()) {
       Services.perms.removeFromPrincipal(p.principal, p.type);
     }
 
     window.close();
   },
 
-  _loadPermissions() {
-    this._tree = document.getElementById("permissionsTree");
-    this._permissions = [];
-
-    // load permissions into a table
-    var enumerator = Services.perms.enumerator;
-    while (enumerator.hasMoreElements()) {
-      var nextPermission = enumerator.getNext().QueryInterface(Ci.nsIPermission);
-      this._addPermissionToList(nextPermission);
-    }
-
-    this._view._rowCount = this._permissions.length;
-
-    // sort and display the table
-    this._tree.view = this._view;
-    this.onPermissionSort("origin");
-
-    // disable "remove all" button if there are none
-    document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
+  _lastPermissionSortColumn: "",
+  _lastPermissionSortAscending: false,
+  _permissionsComparator(a, b) {
+    return a.toLowerCase().localeCompare(b.toLowerCase());
   },
 
-  _addPermissionToList(aPermission) {
-    if (aPermission.type == this._type &&
-        (!this._manageCapability ||
-         (aPermission.capability == this._manageCapability))) {
-
-      var principal = aPermission.principal;
-      var capabilityString = this._getCapabilityString(aPermission.capability);
-      var p = new Permission(principal,
-                             aPermission.type,
-                             capabilityString);
-      this._permissions.push(p);
-    }
-  },
-
-  _removePermissionFromList(aPrincipal) {
-    for (let i = 0; i < this._permissions.length; ++i) {
-      if (this._permissions[i].principal.equals(aPrincipal)) {
-        this._permissions.splice(i, 1);
-        this._view._rowCount--;
-        this._tree.treeBoxObject.rowCountChanged(this._view.rowCount - 1, -1);
-        this._tree.treeBoxObject.invalidate();
-        break;
-      }
-    }
+  onPermissionSort(aColumn) {
+    this._lastPermissionSortAscending = gTreeUtils.sort(this._tree,
+                                                        this._view,
+                                                        this._permissions,
+                                                        aColumn,
+                                                        this._permissionsComparator,
+                                                        this._lastPermissionSortColumn,
+                                                        this._lastPermissionSortAscending);
+    this._lastPermissionSortColumn = aColumn;
+    let sortDirection = this._lastPermissionSortAscending ? "descending" : "ascending";
+    let cols = document.querySelectorAll("treecol");
+    cols.forEach(c => c.removeAttribute("sortDirection"));
+    let column = document.querySelector(`treecol[data-field-name=${aColumn}]`);
+    column.setAttribute("sortDirection", sortDirection);
   },
 
   setOrigin(aOrigin) {
     document.getElementById("url").value = aOrigin;
   }
 };
 
 function setOrigin(aOrigin) {
--- a/browser/components/preferences/permissions.xul
+++ b/browser/components/preferences/permissions.xul
@@ -4,19 +4,19 @@
    - 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/. -->
 
 <?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
 <?xml-stylesheet href="chrome://browser/skin/preferences/preferences.css" type="text/css"?>
 
 <window id="PermissionsDialog" class="windowDialog"
         windowtype="Browser:Permissions"
+        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
         data-l10n-id="permissions-window"
         data-l10n-attrs="title, style"
-        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
         onload="gPermissionManager.onLoad();"
         onunload="gPermissionManager.uninit();"
         persist="screenX screenY width height"
         onkeypress="gPermissionManager.onWindowKeyPress(event);">
 
   <link rel="localization" href="browser/preferences/permissions.ftl"/>
   <script type="application/javascript" src="chrome://global/content/l10n.js"></script>