browser/base/content/pageinfo/permissions.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 Components.utils.import("resource:///modules/SitePermissions.jsm");
michael@0 6
michael@0 7 const nsIQuotaManager = Components.interfaces.nsIQuotaManager;
michael@0 8
michael@0 9 var gPermURI;
michael@0 10 var gUsageRequest;
michael@0 11
michael@0 12 var gPermissions = SitePermissions.listPermissions();
michael@0 13 gPermissions.push("plugins");
michael@0 14
michael@0 15 var permissionObserver = {
michael@0 16 observe: function (aSubject, aTopic, aData)
michael@0 17 {
michael@0 18 if (aTopic == "perm-changed") {
michael@0 19 var permission = aSubject.QueryInterface(Components.interfaces.nsIPermission);
michael@0 20 if (permission.host == gPermURI.host) {
michael@0 21 if (gPermissions.indexOf(permission.type) > -1)
michael@0 22 initRow(permission.type);
michael@0 23 else if (permission.type.startsWith("plugin"))
michael@0 24 setPluginsRadioState();
michael@0 25 }
michael@0 26 }
michael@0 27 }
michael@0 28 };
michael@0 29
michael@0 30 function onLoadPermission()
michael@0 31 {
michael@0 32 var uri = gDocument.documentURIObject;
michael@0 33 var permTab = document.getElementById("permTab");
michael@0 34 if (SitePermissions.isSupportedURI(uri)) {
michael@0 35 gPermURI = uri;
michael@0 36 var hostText = document.getElementById("hostText");
michael@0 37 hostText.value = gPermURI.host;
michael@0 38
michael@0 39 for (var i of gPermissions)
michael@0 40 initRow(i);
michael@0 41 var os = Components.classes["@mozilla.org/observer-service;1"]
michael@0 42 .getService(Components.interfaces.nsIObserverService);
michael@0 43 os.addObserver(permissionObserver, "perm-changed", false);
michael@0 44 onUnloadRegistry.push(onUnloadPermission);
michael@0 45 permTab.hidden = false;
michael@0 46 }
michael@0 47 else
michael@0 48 permTab.hidden = true;
michael@0 49 }
michael@0 50
michael@0 51 function onUnloadPermission()
michael@0 52 {
michael@0 53 var os = Components.classes["@mozilla.org/observer-service;1"]
michael@0 54 .getService(Components.interfaces.nsIObserverService);
michael@0 55 os.removeObserver(permissionObserver, "perm-changed");
michael@0 56
michael@0 57 if (gUsageRequest) {
michael@0 58 gUsageRequest.cancel();
michael@0 59 gUsageRequest = null;
michael@0 60 }
michael@0 61 }
michael@0 62
michael@0 63 function initRow(aPartId)
michael@0 64 {
michael@0 65 if (aPartId == "plugins") {
michael@0 66 initPluginsRow();
michael@0 67 return;
michael@0 68 }
michael@0 69
michael@0 70 createRow(aPartId);
michael@0 71
michael@0 72 var checkbox = document.getElementById(aPartId + "Def");
michael@0 73 var command = document.getElementById("cmd_" + aPartId + "Toggle");
michael@0 74 var perm = SitePermissions.get(gPermURI, aPartId);
michael@0 75
michael@0 76 if (perm) {
michael@0 77 checkbox.checked = false;
michael@0 78 command.removeAttribute("disabled");
michael@0 79 }
michael@0 80 else {
michael@0 81 checkbox.checked = true;
michael@0 82 command.setAttribute("disabled", "true");
michael@0 83 perm = SitePermissions.getDefault(aPartId);
michael@0 84 }
michael@0 85 setRadioState(aPartId, perm);
michael@0 86
michael@0 87 if (aPartId == "indexedDB") {
michael@0 88 initIndexedDBRow();
michael@0 89 }
michael@0 90 }
michael@0 91
michael@0 92 function createRow(aPartId) {
michael@0 93 let rowId = "perm-" + aPartId + "-row";
michael@0 94 if (document.getElementById(rowId))
michael@0 95 return;
michael@0 96
michael@0 97 let commandId = "cmd_" + aPartId + "Toggle";
michael@0 98 let labelId = "perm-" + aPartId + "-label";
michael@0 99 let radiogroupId = aPartId + "RadioGroup";
michael@0 100
michael@0 101 let command = document.createElement("command");
michael@0 102 command.setAttribute("id", commandId);
michael@0 103 command.setAttribute("oncommand", "onRadioClick('" + aPartId + "');");
michael@0 104 document.getElementById("pageInfoCommandSet").appendChild(command);
michael@0 105
michael@0 106 let row = document.createElement("vbox");
michael@0 107 row.setAttribute("id", rowId);
michael@0 108 row.setAttribute("class", "permission");
michael@0 109
michael@0 110 let label = document.createElement("label");
michael@0 111 label.setAttribute("id", labelId);
michael@0 112 label.setAttribute("control", radiogroupId);
michael@0 113 label.setAttribute("value", SitePermissions.getPermissionLabel(aPartId));
michael@0 114 label.setAttribute("class", "permissionLabel");
michael@0 115 row.appendChild(label);
michael@0 116
michael@0 117 let controls = document.createElement("hbox");
michael@0 118 controls.setAttribute("role", "group");
michael@0 119 controls.setAttribute("aria-labelledby", labelId);
michael@0 120
michael@0 121 let checkbox = document.createElement("checkbox");
michael@0 122 checkbox.setAttribute("id", aPartId + "Def");
michael@0 123 checkbox.setAttribute("oncommand", "onCheckboxClick('" + aPartId + "');");
michael@0 124 checkbox.setAttribute("label", gBundle.getString("permissions.useDefault"));
michael@0 125 controls.appendChild(checkbox);
michael@0 126
michael@0 127 let spacer = document.createElement("spacer");
michael@0 128 spacer.setAttribute("flex", "1");
michael@0 129 controls.appendChild(spacer);
michael@0 130
michael@0 131 let radiogroup = document.createElement("radiogroup");
michael@0 132 radiogroup.setAttribute("id", radiogroupId);
michael@0 133 radiogroup.setAttribute("orient", "horizontal");
michael@0 134 for (let state of SitePermissions.getAvailableStates(aPartId)) {
michael@0 135 let radio = document.createElement("radio");
michael@0 136 radio.setAttribute("id", aPartId + "#" + state);
michael@0 137 radio.setAttribute("label", SitePermissions.getStateLabel(aPartId, state));
michael@0 138 radio.setAttribute("command", commandId);
michael@0 139 radiogroup.appendChild(radio);
michael@0 140 }
michael@0 141 controls.appendChild(radiogroup);
michael@0 142
michael@0 143 row.appendChild(controls);
michael@0 144
michael@0 145 document.getElementById("permList").appendChild(row);
michael@0 146 }
michael@0 147
michael@0 148 function onCheckboxClick(aPartId)
michael@0 149 {
michael@0 150 var command = document.getElementById("cmd_" + aPartId + "Toggle");
michael@0 151 var checkbox = document.getElementById(aPartId + "Def");
michael@0 152 if (checkbox.checked) {
michael@0 153 SitePermissions.remove(gPermURI, aPartId);
michael@0 154 command.setAttribute("disabled", "true");
michael@0 155 var perm = SitePermissions.getDefault(aPartId);
michael@0 156 setRadioState(aPartId, perm);
michael@0 157 }
michael@0 158 else {
michael@0 159 onRadioClick(aPartId);
michael@0 160 command.removeAttribute("disabled");
michael@0 161 }
michael@0 162 }
michael@0 163
michael@0 164 function onPluginRadioClick(aEvent) {
michael@0 165 onRadioClick(aEvent.originalTarget.getAttribute("id").split('#')[0]);
michael@0 166 }
michael@0 167
michael@0 168 function onRadioClick(aPartId)
michael@0 169 {
michael@0 170 var radioGroup = document.getElementById(aPartId + "RadioGroup");
michael@0 171 var id = radioGroup.selectedItem.id;
michael@0 172 var permission = id.split('#')[1];
michael@0 173 SitePermissions.set(gPermURI, aPartId, permission);
michael@0 174 }
michael@0 175
michael@0 176 function setRadioState(aPartId, aValue)
michael@0 177 {
michael@0 178 var radio = document.getElementById(aPartId + "#" + aValue);
michael@0 179 radio.radioGroup.selectedItem = radio;
michael@0 180 }
michael@0 181
michael@0 182 function initIndexedDBRow()
michael@0 183 {
michael@0 184 let row = document.getElementById("perm-indexedDB-row");
michael@0 185 let extras = document.getElementById("perm-indexedDB-extras");
michael@0 186
michael@0 187 row.appendChild(extras);
michael@0 188
michael@0 189 var quotaManager = Components.classes["@mozilla.org/dom/quota/manager;1"]
michael@0 190 .getService(nsIQuotaManager);
michael@0 191 gUsageRequest =
michael@0 192 quotaManager.getUsageForURI(gPermURI, onIndexedDBUsageCallback);
michael@0 193
michael@0 194 var status = document.getElementById("indexedDBStatus");
michael@0 195 var button = document.getElementById("indexedDBClear");
michael@0 196
michael@0 197 status.value = "";
michael@0 198 status.setAttribute("hidden", "true");
michael@0 199 button.setAttribute("hidden", "true");
michael@0 200 }
michael@0 201
michael@0 202 function onIndexedDBClear()
michael@0 203 {
michael@0 204 Components.classes["@mozilla.org/dom/quota/manager;1"]
michael@0 205 .getService(nsIQuotaManager)
michael@0 206 .clearStoragesForURI(gPermURI);
michael@0 207
michael@0 208 SitePermissions.remove(gPermURI, "indexedDB-unlimited");
michael@0 209 initIndexedDBRow();
michael@0 210 }
michael@0 211
michael@0 212 function onIndexedDBUsageCallback(uri, usage, fileUsage)
michael@0 213 {
michael@0 214 if (!uri.equals(gPermURI)) {
michael@0 215 throw new Error("Callback received for bad URI: " + uri);
michael@0 216 }
michael@0 217
michael@0 218 if (usage) {
michael@0 219 if (!("DownloadUtils" in window)) {
michael@0 220 Components.utils.import("resource://gre/modules/DownloadUtils.jsm");
michael@0 221 }
michael@0 222
michael@0 223 var status = document.getElementById("indexedDBStatus");
michael@0 224 var button = document.getElementById("indexedDBClear");
michael@0 225
michael@0 226 status.value =
michael@0 227 gBundle.getFormattedString("indexedDBUsage",
michael@0 228 DownloadUtils.convertByteUnits(usage));
michael@0 229 status.removeAttribute("hidden");
michael@0 230 button.removeAttribute("hidden");
michael@0 231 }
michael@0 232 }
michael@0 233
michael@0 234 // XXX copied this from browser-plugins.js - is there a way to share?
michael@0 235 function makeNicePluginName(aName) {
michael@0 236 if (aName == "Shockwave Flash")
michael@0 237 return "Adobe Flash";
michael@0 238
michael@0 239 // Clean up the plugin name by stripping off any trailing version numbers
michael@0 240 // or "plugin". EG, "Foo Bar Plugin 1.23_02" --> "Foo Bar"
michael@0 241 // Do this by first stripping the numbers, etc. off the end, and then
michael@0 242 // removing "Plugin" (and then trimming to get rid of any whitespace).
michael@0 243 // (Otherwise, something like "Java(TM) Plug-in 1.7.0_07" gets mangled)
michael@0 244 let newName = aName.replace(/[\s\d\.\-\_\(\)]+$/, "").replace(/\bplug-?in\b/i, "").trim();
michael@0 245 return newName;
michael@0 246 }
michael@0 247
michael@0 248 function fillInPluginPermissionTemplate(aPluginName, aPermissionString) {
michael@0 249 let permPluginTemplate = document.getElementById("permPluginTemplate").cloneNode(true);
michael@0 250 permPluginTemplate.setAttribute("permString", aPermissionString);
michael@0 251 let attrs = [
michael@0 252 [ ".permPluginTemplateLabel", "value", aPluginName ],
michael@0 253 [ ".permPluginTemplateRadioGroup", "id", aPermissionString + "RadioGroup" ],
michael@0 254 [ ".permPluginTemplateRadioDefault", "id", aPermissionString + "#0" ],
michael@0 255 [ ".permPluginTemplateRadioAsk", "id", aPermissionString + "#3" ],
michael@0 256 [ ".permPluginTemplateRadioAllow", "id", aPermissionString + "#1" ],
michael@0 257 [ ".permPluginTemplateRadioBlock", "id", aPermissionString + "#2" ]
michael@0 258 ];
michael@0 259
michael@0 260 for (let attr of attrs) {
michael@0 261 permPluginTemplate.querySelector(attr[0]).setAttribute(attr[1], attr[2]);
michael@0 262 }
michael@0 263
michael@0 264 return permPluginTemplate;
michael@0 265 }
michael@0 266
michael@0 267 function clearPluginPermissionTemplate() {
michael@0 268 let permPluginTemplate = document.getElementById("permPluginTemplate");
michael@0 269 permPluginTemplate.hidden = true;
michael@0 270 permPluginTemplate.removeAttribute("permString");
michael@0 271 document.querySelector(".permPluginTemplateLabel").removeAttribute("value");
michael@0 272 document.querySelector(".permPluginTemplateRadioGroup").removeAttribute("id");
michael@0 273 document.querySelector(".permPluginTemplateRadioAsk").removeAttribute("id");
michael@0 274 document.querySelector(".permPluginTemplateRadioAllow").removeAttribute("id");
michael@0 275 document.querySelector(".permPluginTemplateRadioBlock").removeAttribute("id");
michael@0 276 }
michael@0 277
michael@0 278 function initPluginsRow() {
michael@0 279 let vulnerableLabel = document.getElementById("browserBundle").getString("pluginActivateVulnerable.label");
michael@0 280 let pluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost);
michael@0 281
michael@0 282 let permissionMap = Map();
michael@0 283
michael@0 284 for (let plugin of pluginHost.getPluginTags()) {
michael@0 285 if (plugin.disabled) {
michael@0 286 continue;
michael@0 287 }
michael@0 288 for (let mimeType of plugin.getMimeTypes()) {
michael@0 289 let permString = pluginHost.getPermissionStringForType(mimeType);
michael@0 290 if (!permissionMap.has(permString)) {
michael@0 291 var name = makeNicePluginName(plugin.name);
michael@0 292 if (permString.startsWith("plugin-vulnerable:")) {
michael@0 293 name += " \u2014 " + vulnerableLabel;
michael@0 294 }
michael@0 295 permissionMap.set(permString, name);
michael@0 296 }
michael@0 297 }
michael@0 298 }
michael@0 299
michael@0 300 let entries = [{name: item[1], permission: item[0]} for (item of permissionMap)];
michael@0 301 entries.sort(function(a, b) {
michael@0 302 return a.name < b.name ? -1 : (a.name == b.name ? 0 : 1);
michael@0 303 });
michael@0 304
michael@0 305 let permissionEntries = [
michael@0 306 fillInPluginPermissionTemplate(p.name, p.permission) for (p of entries)
michael@0 307 ];
michael@0 308
michael@0 309 let permPluginsRow = document.getElementById("perm-plugins-row");
michael@0 310 clearPluginPermissionTemplate();
michael@0 311 if (permissionEntries.length < 1) {
michael@0 312 permPluginsRow.hidden = true;
michael@0 313 return;
michael@0 314 }
michael@0 315
michael@0 316 for (let permissionEntry of permissionEntries) {
michael@0 317 permPluginsRow.appendChild(permissionEntry);
michael@0 318 }
michael@0 319
michael@0 320 setPluginsRadioState();
michael@0 321 }
michael@0 322
michael@0 323 function setPluginsRadioState() {
michael@0 324 let box = document.getElementById("perm-plugins-row");
michael@0 325 for (let permissionEntry of box.childNodes) {
michael@0 326 if (permissionEntry.hasAttribute("permString")) {
michael@0 327 let permString = permissionEntry.getAttribute("permString");
michael@0 328 let permission = SitePermissions.get(gPermURI, permString);
michael@0 329 setRadioState(permString, permission);
michael@0 330 }
michael@0 331 }
michael@0 332 }

mercurial