browser/components/preferences/permissions.js

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 const nsIPermissionManager = Components.interfaces.nsIPermissionManager;
michael@0 7 const nsICookiePermission = Components.interfaces.nsICookiePermission;
michael@0 8
michael@0 9 const NOTIFICATION_FLUSH_PERMISSIONS = "flush-pending-permissions";
michael@0 10
michael@0 11 function Permission(host, rawHost, type, capability)
michael@0 12 {
michael@0 13 this.host = host;
michael@0 14 this.rawHost = rawHost;
michael@0 15 this.type = type;
michael@0 16 this.capability = capability;
michael@0 17 }
michael@0 18
michael@0 19 var gPermissionManager = {
michael@0 20 _type : "",
michael@0 21 _permissions : [],
michael@0 22 _pm : Components.classes["@mozilla.org/permissionmanager;1"]
michael@0 23 .getService(Components.interfaces.nsIPermissionManager),
michael@0 24 _bundle : null,
michael@0 25 _tree : null,
michael@0 26
michael@0 27 _view: {
michael@0 28 _rowCount: 0,
michael@0 29 get rowCount()
michael@0 30 {
michael@0 31 return this._rowCount;
michael@0 32 },
michael@0 33 getCellText: function (aRow, aColumn)
michael@0 34 {
michael@0 35 if (aColumn.id == "siteCol")
michael@0 36 return gPermissionManager._permissions[aRow].rawHost;
michael@0 37 else if (aColumn.id == "statusCol")
michael@0 38 return gPermissionManager._permissions[aRow].capability;
michael@0 39 return "";
michael@0 40 },
michael@0 41
michael@0 42 isSeparator: function(aIndex) { return false; },
michael@0 43 isSorted: function() { return false; },
michael@0 44 isContainer: function(aIndex) { return false; },
michael@0 45 setTree: function(aTree){},
michael@0 46 getImageSrc: function(aRow, aColumn) {},
michael@0 47 getProgressMode: function(aRow, aColumn) {},
michael@0 48 getCellValue: function(aRow, aColumn) {},
michael@0 49 cycleHeader: function(column) {},
michael@0 50 getRowProperties: function(row){ return ""; },
michael@0 51 getColumnProperties: function(column){ return ""; },
michael@0 52 getCellProperties: function(row,column){
michael@0 53 if (column.element.getAttribute("id") == "siteCol")
michael@0 54 return "ltr";
michael@0 55
michael@0 56 return "";
michael@0 57 }
michael@0 58 },
michael@0 59
michael@0 60 _getCapabilityString: function (aCapability)
michael@0 61 {
michael@0 62 var stringKey = null;
michael@0 63 switch (aCapability) {
michael@0 64 case nsIPermissionManager.ALLOW_ACTION:
michael@0 65 stringKey = "can";
michael@0 66 break;
michael@0 67 case nsIPermissionManager.DENY_ACTION:
michael@0 68 stringKey = "cannot";
michael@0 69 break;
michael@0 70 case nsICookiePermission.ACCESS_ALLOW_FIRST_PARTY_ONLY:
michael@0 71 stringKey = "canAccessFirstParty";
michael@0 72 break;
michael@0 73 case nsICookiePermission.ACCESS_SESSION:
michael@0 74 stringKey = "canSession";
michael@0 75 break;
michael@0 76 }
michael@0 77 return this._bundle.getString(stringKey);
michael@0 78 },
michael@0 79
michael@0 80 addPermission: function (aCapability)
michael@0 81 {
michael@0 82 var textbox = document.getElementById("url");
michael@0 83 var host = textbox.value.replace(/^\s*([-\w]*:\/+)?/, ""); // trim any leading space and scheme
michael@0 84 try {
michael@0 85 var ioService = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 86 .getService(Components.interfaces.nsIIOService);
michael@0 87 var uri = ioService.newURI("http://"+host, null, null);
michael@0 88 host = uri.host;
michael@0 89 } catch(ex) {
michael@0 90 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
michael@0 91 .getService(Components.interfaces.nsIPromptService);
michael@0 92 var message = this._bundle.getString("invalidURI");
michael@0 93 var title = this._bundle.getString("invalidURITitle");
michael@0 94 promptService.alert(window, title, message);
michael@0 95 return;
michael@0 96 }
michael@0 97
michael@0 98 var capabilityString = this._getCapabilityString(aCapability);
michael@0 99
michael@0 100 // check whether the permission already exists, if not, add it
michael@0 101 var exists = false;
michael@0 102 for (var i = 0; i < this._permissions.length; ++i) {
michael@0 103 if (this._permissions[i].rawHost == host) {
michael@0 104 // Avoid calling the permission manager if the capability settings are
michael@0 105 // the same. Otherwise allow the call to the permissions manager to
michael@0 106 // update the listbox for us.
michael@0 107 exists = this._permissions[i].capability == capabilityString;
michael@0 108 break;
michael@0 109 }
michael@0 110 }
michael@0 111
michael@0 112 if (!exists) {
michael@0 113 host = (host.charAt(0) == ".") ? host.substring(1,host.length) : host;
michael@0 114 var uri = ioService.newURI("http://" + host, null, null);
michael@0 115 this._pm.add(uri, this._type, aCapability);
michael@0 116 }
michael@0 117 textbox.value = "";
michael@0 118 textbox.focus();
michael@0 119
michael@0 120 // covers a case where the site exists already, so the buttons don't disable
michael@0 121 this.onHostInput(textbox);
michael@0 122
michael@0 123 // enable "remove all" button as needed
michael@0 124 document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
michael@0 125 },
michael@0 126
michael@0 127 onHostInput: function (aSiteField)
michael@0 128 {
michael@0 129 document.getElementById("btnSession").disabled = !aSiteField.value;
michael@0 130 document.getElementById("btnBlock").disabled = !aSiteField.value;
michael@0 131 document.getElementById("btnAllow").disabled = !aSiteField.value;
michael@0 132 },
michael@0 133
michael@0 134 onWindowKeyPress: function (aEvent)
michael@0 135 {
michael@0 136 if (aEvent.keyCode == KeyEvent.DOM_VK_ESCAPE)
michael@0 137 window.close();
michael@0 138 },
michael@0 139
michael@0 140 onHostKeyPress: function (aEvent)
michael@0 141 {
michael@0 142 if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN)
michael@0 143 document.getElementById("btnAllow").click();
michael@0 144 },
michael@0 145
michael@0 146 onLoad: function ()
michael@0 147 {
michael@0 148 this._bundle = document.getElementById("bundlePreferences");
michael@0 149 var params = window.arguments[0];
michael@0 150 this.init(params);
michael@0 151 },
michael@0 152
michael@0 153 init: function (aParams)
michael@0 154 {
michael@0 155 if (this._type) {
michael@0 156 // reusing an open dialog, clear the old observer
michael@0 157 this.uninit();
michael@0 158 }
michael@0 159
michael@0 160 this._type = aParams.permissionType;
michael@0 161 this._manageCapability = aParams.manageCapability;
michael@0 162
michael@0 163 var permissionsText = document.getElementById("permissionsText");
michael@0 164 while (permissionsText.hasChildNodes())
michael@0 165 permissionsText.removeChild(permissionsText.firstChild);
michael@0 166 permissionsText.appendChild(document.createTextNode(aParams.introText));
michael@0 167
michael@0 168 document.title = aParams.windowTitle;
michael@0 169
michael@0 170 document.getElementById("btnBlock").hidden = !aParams.blockVisible;
michael@0 171 document.getElementById("btnSession").hidden = !aParams.sessionVisible;
michael@0 172 document.getElementById("btnAllow").hidden = !aParams.allowVisible;
michael@0 173
michael@0 174 var urlFieldVisible = (aParams.blockVisible || aParams.sessionVisible || aParams.allowVisible);
michael@0 175
michael@0 176 var urlField = document.getElementById("url");
michael@0 177 urlField.value = aParams.prefilledHost;
michael@0 178 urlField.hidden = !urlFieldVisible;
michael@0 179
michael@0 180 this.onHostInput(urlField);
michael@0 181
michael@0 182 var urlLabel = document.getElementById("urlLabel");
michael@0 183 urlLabel.hidden = !urlFieldVisible;
michael@0 184
michael@0 185 var os = Components.classes["@mozilla.org/observer-service;1"]
michael@0 186 .getService(Components.interfaces.nsIObserverService);
michael@0 187 os.notifyObservers(null, NOTIFICATION_FLUSH_PERMISSIONS, this._type);
michael@0 188 os.addObserver(this, "perm-changed", false);
michael@0 189
michael@0 190 this._loadPermissions();
michael@0 191
michael@0 192 urlField.focus();
michael@0 193 },
michael@0 194
michael@0 195 uninit: function ()
michael@0 196 {
michael@0 197 var os = Components.classes["@mozilla.org/observer-service;1"]
michael@0 198 .getService(Components.interfaces.nsIObserverService);
michael@0 199 os.removeObserver(this, "perm-changed");
michael@0 200 },
michael@0 201
michael@0 202 observe: function (aSubject, aTopic, aData)
michael@0 203 {
michael@0 204 if (aTopic == "perm-changed") {
michael@0 205 var permission = aSubject.QueryInterface(Components.interfaces.nsIPermission);
michael@0 206 if (aData == "added") {
michael@0 207 this._addPermissionToList(permission);
michael@0 208 ++this._view._rowCount;
michael@0 209 this._tree.treeBoxObject.rowCountChanged(this._view.rowCount - 1, 1);
michael@0 210 // Re-do the sort, since we inserted this new item at the end.
michael@0 211 gTreeUtils.sort(this._tree, this._view, this._permissions,
michael@0 212 this._permissionsComparator,
michael@0 213 this._lastPermissionSortColumn,
michael@0 214 this._lastPermissionSortAscending);
michael@0 215 }
michael@0 216 else if (aData == "changed") {
michael@0 217 for (var i = 0; i < this._permissions.length; ++i) {
michael@0 218 if (this._permissions[i].host == permission.host) {
michael@0 219 this._permissions[i].capability = this._getCapabilityString(permission.capability);
michael@0 220 break;
michael@0 221 }
michael@0 222 }
michael@0 223 // Re-do the sort, if the status changed from Block to Allow
michael@0 224 // or vice versa, since if we're sorted on status, we may no
michael@0 225 // longer be in order.
michael@0 226 if (this._lastPermissionSortColumn.id == "statusCol") {
michael@0 227 gTreeUtils.sort(this._tree, this._view, this._permissions,
michael@0 228 this._permissionsComparator,
michael@0 229 this._lastPermissionSortColumn,
michael@0 230 this._lastPermissionSortAscending);
michael@0 231 }
michael@0 232 this._tree.treeBoxObject.invalidate();
michael@0 233 }
michael@0 234 // No UI other than this window causes this method to be sent a "deleted"
michael@0 235 // notification, so we don't need to implement it since Delete is handled
michael@0 236 // directly by the Permission Removal handlers. If that ever changes, those
michael@0 237 // implementations will have to move into here.
michael@0 238 }
michael@0 239 },
michael@0 240
michael@0 241 onPermissionSelected: function ()
michael@0 242 {
michael@0 243 var hasSelection = this._tree.view.selection.count > 0;
michael@0 244 var hasRows = this._tree.view.rowCount > 0;
michael@0 245 document.getElementById("removePermission").disabled = !hasRows || !hasSelection;
michael@0 246 document.getElementById("removeAllPermissions").disabled = !hasRows;
michael@0 247 },
michael@0 248
michael@0 249 onPermissionDeleted: function ()
michael@0 250 {
michael@0 251 if (!this._view.rowCount)
michael@0 252 return;
michael@0 253 var removedPermissions = [];
michael@0 254 gTreeUtils.deleteSelectedItems(this._tree, this._view, this._permissions, removedPermissions);
michael@0 255 for (var i = 0; i < removedPermissions.length; ++i) {
michael@0 256 var p = removedPermissions[i];
michael@0 257 this._pm.remove(p.host, p.type);
michael@0 258 }
michael@0 259 document.getElementById("removePermission").disabled = !this._permissions.length;
michael@0 260 document.getElementById("removeAllPermissions").disabled = !this._permissions.length;
michael@0 261 },
michael@0 262
michael@0 263 onAllPermissionsDeleted: function ()
michael@0 264 {
michael@0 265 if (!this._view.rowCount)
michael@0 266 return;
michael@0 267 var removedPermissions = [];
michael@0 268 gTreeUtils.deleteAll(this._tree, this._view, this._permissions, removedPermissions);
michael@0 269 for (var i = 0; i < removedPermissions.length; ++i) {
michael@0 270 var p = removedPermissions[i];
michael@0 271 this._pm.remove(p.host, p.type);
michael@0 272 }
michael@0 273 document.getElementById("removePermission").disabled = true;
michael@0 274 document.getElementById("removeAllPermissions").disabled = true;
michael@0 275 },
michael@0 276
michael@0 277 onPermissionKeyPress: function (aEvent)
michael@0 278 {
michael@0 279 if (aEvent.keyCode == 46)
michael@0 280 this.onPermissionDeleted();
michael@0 281 },
michael@0 282
michael@0 283 _lastPermissionSortColumn: "",
michael@0 284 _lastPermissionSortAscending: false,
michael@0 285 _permissionsComparator : function (a, b)
michael@0 286 {
michael@0 287 return a.toLowerCase().localeCompare(b.toLowerCase());
michael@0 288 },
michael@0 289
michael@0 290
michael@0 291 onPermissionSort: function (aColumn)
michael@0 292 {
michael@0 293 this._lastPermissionSortAscending = gTreeUtils.sort(this._tree,
michael@0 294 this._view,
michael@0 295 this._permissions,
michael@0 296 aColumn,
michael@0 297 this._permissionsComparator,
michael@0 298 this._lastPermissionSortColumn,
michael@0 299 this._lastPermissionSortAscending);
michael@0 300 this._lastPermissionSortColumn = aColumn;
michael@0 301 },
michael@0 302
michael@0 303 _loadPermissions: function ()
michael@0 304 {
michael@0 305 this._tree = document.getElementById("permissionsTree");
michael@0 306 this._permissions = [];
michael@0 307
michael@0 308 // load permissions into a table
michael@0 309 var count = 0;
michael@0 310 var enumerator = this._pm.enumerator;
michael@0 311 while (enumerator.hasMoreElements()) {
michael@0 312 var nextPermission = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission);
michael@0 313 this._addPermissionToList(nextPermission);
michael@0 314 }
michael@0 315
michael@0 316 this._view._rowCount = this._permissions.length;
michael@0 317
michael@0 318 // sort and display the table
michael@0 319 this._tree.treeBoxObject.view = this._view;
michael@0 320 this.onPermissionSort("rawHost", false);
michael@0 321
michael@0 322 // disable "remove all" button if there are none
michael@0 323 document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
michael@0 324 },
michael@0 325
michael@0 326 _addPermissionToList: function (aPermission)
michael@0 327 {
michael@0 328 if (aPermission.type == this._type &&
michael@0 329 (!this._manageCapability ||
michael@0 330 (aPermission.capability == this._manageCapability))) {
michael@0 331
michael@0 332 var host = aPermission.host;
michael@0 333 var capabilityString = this._getCapabilityString(aPermission.capability);
michael@0 334 var p = new Permission(host,
michael@0 335 (host.charAt(0) == ".") ? host.substring(1,host.length) : host,
michael@0 336 aPermission.type,
michael@0 337 capabilityString);
michael@0 338 this._permissions.push(p);
michael@0 339 }
michael@0 340 },
michael@0 341
michael@0 342 setHost: function (aHost)
michael@0 343 {
michael@0 344 document.getElementById("url").value = aHost;
michael@0 345 }
michael@0 346 };
michael@0 347
michael@0 348 function setHost(aHost)
michael@0 349 {
michael@0 350 gPermissionManager.setHost(aHost);
michael@0 351 }
michael@0 352
michael@0 353 function initWithParams(aParams)
michael@0 354 {
michael@0 355 gPermissionManager.init(aParams);
michael@0 356 }
michael@0 357

mercurial