browser/base/content/pageinfo/permissions.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/pageinfo/permissions.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,332 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Components.utils.import("resource:///modules/SitePermissions.jsm");
     1.9 +
    1.10 +const nsIQuotaManager = Components.interfaces.nsIQuotaManager;
    1.11 +
    1.12 +var gPermURI;
    1.13 +var gUsageRequest;
    1.14 +
    1.15 +var gPermissions = SitePermissions.listPermissions();
    1.16 +gPermissions.push("plugins");
    1.17 +
    1.18 +var permissionObserver = {
    1.19 +  observe: function (aSubject, aTopic, aData)
    1.20 +  {
    1.21 +    if (aTopic == "perm-changed") {
    1.22 +      var permission = aSubject.QueryInterface(Components.interfaces.nsIPermission);
    1.23 +      if (permission.host == gPermURI.host) {
    1.24 +        if (gPermissions.indexOf(permission.type) > -1)
    1.25 +          initRow(permission.type);
    1.26 +        else if (permission.type.startsWith("plugin"))
    1.27 +          setPluginsRadioState();
    1.28 +      }
    1.29 +    }
    1.30 +  }
    1.31 +};
    1.32 +
    1.33 +function onLoadPermission()
    1.34 +{
    1.35 +  var uri = gDocument.documentURIObject;
    1.36 +  var permTab = document.getElementById("permTab");
    1.37 +  if (SitePermissions.isSupportedURI(uri)) {
    1.38 +    gPermURI = uri;
    1.39 +    var hostText = document.getElementById("hostText");
    1.40 +    hostText.value = gPermURI.host;
    1.41 +
    1.42 +    for (var i of gPermissions)
    1.43 +      initRow(i);
    1.44 +    var os = Components.classes["@mozilla.org/observer-service;1"]
    1.45 +                       .getService(Components.interfaces.nsIObserverService);
    1.46 +    os.addObserver(permissionObserver, "perm-changed", false);
    1.47 +    onUnloadRegistry.push(onUnloadPermission);
    1.48 +    permTab.hidden = false;
    1.49 +  }
    1.50 +  else
    1.51 +    permTab.hidden = true;
    1.52 +}
    1.53 +
    1.54 +function onUnloadPermission()
    1.55 +{
    1.56 +  var os = Components.classes["@mozilla.org/observer-service;1"]
    1.57 +                     .getService(Components.interfaces.nsIObserverService);
    1.58 +  os.removeObserver(permissionObserver, "perm-changed");
    1.59 +
    1.60 +  if (gUsageRequest) {
    1.61 +    gUsageRequest.cancel();
    1.62 +    gUsageRequest = null;
    1.63 +  }
    1.64 +}
    1.65 +
    1.66 +function initRow(aPartId)
    1.67 +{
    1.68 +  if (aPartId == "plugins") {
    1.69 +    initPluginsRow();
    1.70 +    return;
    1.71 +  }
    1.72 +
    1.73 +  createRow(aPartId);
    1.74 +
    1.75 +  var checkbox = document.getElementById(aPartId + "Def");
    1.76 +  var command  = document.getElementById("cmd_" + aPartId + "Toggle");
    1.77 +  var perm = SitePermissions.get(gPermURI, aPartId);
    1.78 +
    1.79 +  if (perm) {
    1.80 +    checkbox.checked = false;
    1.81 +    command.removeAttribute("disabled");
    1.82 +  }
    1.83 +  else {
    1.84 +    checkbox.checked = true;
    1.85 +    command.setAttribute("disabled", "true");
    1.86 +    perm = SitePermissions.getDefault(aPartId);
    1.87 +  }
    1.88 +  setRadioState(aPartId, perm);
    1.89 +
    1.90 +  if (aPartId == "indexedDB") {
    1.91 +    initIndexedDBRow();
    1.92 +  }
    1.93 +}
    1.94 +
    1.95 +function createRow(aPartId) {
    1.96 +  let rowId = "perm-" + aPartId + "-row";
    1.97 +  if (document.getElementById(rowId))
    1.98 +    return;
    1.99 +
   1.100 +  let commandId = "cmd_" + aPartId + "Toggle";
   1.101 +  let labelId = "perm-" + aPartId + "-label";
   1.102 +  let radiogroupId = aPartId + "RadioGroup";
   1.103 +
   1.104 +  let command = document.createElement("command");
   1.105 +  command.setAttribute("id", commandId);
   1.106 +  command.setAttribute("oncommand", "onRadioClick('" + aPartId + "');");
   1.107 +  document.getElementById("pageInfoCommandSet").appendChild(command);
   1.108 +
   1.109 +  let row = document.createElement("vbox");
   1.110 +  row.setAttribute("id", rowId);
   1.111 +  row.setAttribute("class", "permission");
   1.112 +
   1.113 +  let label = document.createElement("label");
   1.114 +  label.setAttribute("id", labelId);
   1.115 +  label.setAttribute("control", radiogroupId);
   1.116 +  label.setAttribute("value", SitePermissions.getPermissionLabel(aPartId));
   1.117 +  label.setAttribute("class", "permissionLabel");
   1.118 +  row.appendChild(label);
   1.119 +
   1.120 +  let controls = document.createElement("hbox");
   1.121 +  controls.setAttribute("role", "group");
   1.122 +  controls.setAttribute("aria-labelledby", labelId);
   1.123 +
   1.124 +  let checkbox = document.createElement("checkbox");
   1.125 +  checkbox.setAttribute("id", aPartId + "Def");
   1.126 +  checkbox.setAttribute("oncommand", "onCheckboxClick('" + aPartId + "');");
   1.127 +  checkbox.setAttribute("label", gBundle.getString("permissions.useDefault"));
   1.128 +  controls.appendChild(checkbox);
   1.129 +
   1.130 +  let spacer = document.createElement("spacer");
   1.131 +  spacer.setAttribute("flex", "1");
   1.132 +  controls.appendChild(spacer);
   1.133 +
   1.134 +  let radiogroup = document.createElement("radiogroup");
   1.135 +  radiogroup.setAttribute("id", radiogroupId);
   1.136 +  radiogroup.setAttribute("orient", "horizontal");
   1.137 +  for (let state of SitePermissions.getAvailableStates(aPartId)) {
   1.138 +    let radio = document.createElement("radio");
   1.139 +    radio.setAttribute("id", aPartId + "#" + state);
   1.140 +    radio.setAttribute("label", SitePermissions.getStateLabel(aPartId, state));
   1.141 +    radio.setAttribute("command", commandId);
   1.142 +    radiogroup.appendChild(radio);
   1.143 +  }
   1.144 +  controls.appendChild(radiogroup);
   1.145 +
   1.146 +  row.appendChild(controls);
   1.147 +
   1.148 +  document.getElementById("permList").appendChild(row);
   1.149 +}
   1.150 +
   1.151 +function onCheckboxClick(aPartId)
   1.152 +{
   1.153 +  var command  = document.getElementById("cmd_" + aPartId + "Toggle");
   1.154 +  var checkbox = document.getElementById(aPartId + "Def");
   1.155 +  if (checkbox.checked) {
   1.156 +    SitePermissions.remove(gPermURI, aPartId);
   1.157 +    command.setAttribute("disabled", "true");
   1.158 +    var perm = SitePermissions.getDefault(aPartId);
   1.159 +    setRadioState(aPartId, perm);
   1.160 +  }
   1.161 +  else {
   1.162 +    onRadioClick(aPartId);
   1.163 +    command.removeAttribute("disabled");
   1.164 +  }
   1.165 +}
   1.166 +
   1.167 +function onPluginRadioClick(aEvent) {
   1.168 +  onRadioClick(aEvent.originalTarget.getAttribute("id").split('#')[0]);
   1.169 +}
   1.170 +
   1.171 +function onRadioClick(aPartId)
   1.172 +{
   1.173 +  var radioGroup = document.getElementById(aPartId + "RadioGroup");
   1.174 +  var id = radioGroup.selectedItem.id;
   1.175 +  var permission = id.split('#')[1];
   1.176 +  SitePermissions.set(gPermURI, aPartId, permission);
   1.177 +}
   1.178 +
   1.179 +function setRadioState(aPartId, aValue)
   1.180 +{
   1.181 +  var radio = document.getElementById(aPartId + "#" + aValue);
   1.182 +  radio.radioGroup.selectedItem = radio;
   1.183 +}
   1.184 +
   1.185 +function initIndexedDBRow()
   1.186 +{
   1.187 +  let row = document.getElementById("perm-indexedDB-row");
   1.188 +  let extras = document.getElementById("perm-indexedDB-extras");
   1.189 +
   1.190 +  row.appendChild(extras);
   1.191 +
   1.192 +  var quotaManager = Components.classes["@mozilla.org/dom/quota/manager;1"]
   1.193 +                               .getService(nsIQuotaManager);
   1.194 +  gUsageRequest =
   1.195 +    quotaManager.getUsageForURI(gPermURI, onIndexedDBUsageCallback);
   1.196 +
   1.197 +  var status = document.getElementById("indexedDBStatus");
   1.198 +  var button = document.getElementById("indexedDBClear");
   1.199 +
   1.200 +  status.value = "";
   1.201 +  status.setAttribute("hidden", "true");
   1.202 +  button.setAttribute("hidden", "true");
   1.203 +}
   1.204 +
   1.205 +function onIndexedDBClear()
   1.206 +{
   1.207 +  Components.classes["@mozilla.org/dom/quota/manager;1"]
   1.208 +            .getService(nsIQuotaManager)
   1.209 +            .clearStoragesForURI(gPermURI);
   1.210 +
   1.211 +  SitePermissions.remove(gPermURI, "indexedDB-unlimited");
   1.212 +  initIndexedDBRow();
   1.213 +}
   1.214 +
   1.215 +function onIndexedDBUsageCallback(uri, usage, fileUsage)
   1.216 +{
   1.217 +  if (!uri.equals(gPermURI)) {
   1.218 +    throw new Error("Callback received for bad URI: " + uri);
   1.219 +  }
   1.220 +
   1.221 +  if (usage) {
   1.222 +    if (!("DownloadUtils" in window)) {
   1.223 +      Components.utils.import("resource://gre/modules/DownloadUtils.jsm");
   1.224 +    }
   1.225 +
   1.226 +    var status = document.getElementById("indexedDBStatus");
   1.227 +    var button = document.getElementById("indexedDBClear");
   1.228 +
   1.229 +    status.value =
   1.230 +      gBundle.getFormattedString("indexedDBUsage",
   1.231 +                                 DownloadUtils.convertByteUnits(usage));
   1.232 +    status.removeAttribute("hidden");
   1.233 +    button.removeAttribute("hidden");
   1.234 +  }
   1.235 +}
   1.236 +
   1.237 +// XXX copied this from browser-plugins.js - is there a way to share?
   1.238 +function makeNicePluginName(aName) {
   1.239 +  if (aName == "Shockwave Flash")
   1.240 +    return "Adobe Flash";
   1.241 +
   1.242 +  // Clean up the plugin name by stripping off any trailing version numbers
   1.243 +  // or "plugin". EG, "Foo Bar Plugin 1.23_02" --> "Foo Bar"
   1.244 +  // Do this by first stripping the numbers, etc. off the end, and then
   1.245 +  // removing "Plugin" (and then trimming to get rid of any whitespace).
   1.246 +  // (Otherwise, something like "Java(TM) Plug-in 1.7.0_07" gets mangled)
   1.247 +  let newName = aName.replace(/[\s\d\.\-\_\(\)]+$/, "").replace(/\bplug-?in\b/i, "").trim();
   1.248 +  return newName;
   1.249 +}
   1.250 +
   1.251 +function fillInPluginPermissionTemplate(aPluginName, aPermissionString) {
   1.252 +  let permPluginTemplate = document.getElementById("permPluginTemplate").cloneNode(true);
   1.253 +  permPluginTemplate.setAttribute("permString", aPermissionString);
   1.254 +  let attrs = [
   1.255 +    [ ".permPluginTemplateLabel", "value", aPluginName ],
   1.256 +    [ ".permPluginTemplateRadioGroup", "id", aPermissionString + "RadioGroup" ],
   1.257 +    [ ".permPluginTemplateRadioDefault", "id", aPermissionString + "#0" ],
   1.258 +    [ ".permPluginTemplateRadioAsk", "id", aPermissionString + "#3" ],
   1.259 +    [ ".permPluginTemplateRadioAllow", "id", aPermissionString + "#1" ],
   1.260 +    [ ".permPluginTemplateRadioBlock", "id", aPermissionString + "#2" ]
   1.261 +  ];
   1.262 +
   1.263 +  for (let attr of attrs) {
   1.264 +    permPluginTemplate.querySelector(attr[0]).setAttribute(attr[1], attr[2]);
   1.265 +  }
   1.266 +
   1.267 +  return permPluginTemplate;
   1.268 +}
   1.269 +
   1.270 +function clearPluginPermissionTemplate() {
   1.271 +  let permPluginTemplate = document.getElementById("permPluginTemplate");
   1.272 +  permPluginTemplate.hidden = true;
   1.273 +  permPluginTemplate.removeAttribute("permString");
   1.274 +  document.querySelector(".permPluginTemplateLabel").removeAttribute("value");
   1.275 +  document.querySelector(".permPluginTemplateRadioGroup").removeAttribute("id");
   1.276 +  document.querySelector(".permPluginTemplateRadioAsk").removeAttribute("id");
   1.277 +  document.querySelector(".permPluginTemplateRadioAllow").removeAttribute("id");
   1.278 +  document.querySelector(".permPluginTemplateRadioBlock").removeAttribute("id");
   1.279 +}
   1.280 +
   1.281 +function initPluginsRow() {
   1.282 +  let vulnerableLabel = document.getElementById("browserBundle").getString("pluginActivateVulnerable.label");
   1.283 +  let pluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost);
   1.284 +
   1.285 +  let permissionMap = Map();
   1.286 +
   1.287 +  for (let plugin of pluginHost.getPluginTags()) {
   1.288 +    if (plugin.disabled) {
   1.289 +      continue;
   1.290 +    }
   1.291 +    for (let mimeType of plugin.getMimeTypes()) {
   1.292 +      let permString = pluginHost.getPermissionStringForType(mimeType);
   1.293 +      if (!permissionMap.has(permString)) {
   1.294 +        var name = makeNicePluginName(plugin.name);
   1.295 +        if (permString.startsWith("plugin-vulnerable:")) {
   1.296 +          name += " \u2014 " + vulnerableLabel;
   1.297 +        }
   1.298 +        permissionMap.set(permString, name);
   1.299 +      }
   1.300 +    }
   1.301 +  }
   1.302 +
   1.303 +  let entries = [{name: item[1], permission: item[0]} for (item of permissionMap)];
   1.304 +  entries.sort(function(a, b) {
   1.305 +    return a.name < b.name ? -1 : (a.name == b.name ? 0 : 1);
   1.306 +  });
   1.307 +
   1.308 +  let permissionEntries = [
   1.309 +    fillInPluginPermissionTemplate(p.name, p.permission) for (p of entries)
   1.310 +  ];
   1.311 +
   1.312 +  let permPluginsRow = document.getElementById("perm-plugins-row");
   1.313 +  clearPluginPermissionTemplate();
   1.314 +  if (permissionEntries.length < 1) {
   1.315 +    permPluginsRow.hidden = true;
   1.316 +    return;
   1.317 +  }
   1.318 +
   1.319 +  for (let permissionEntry of permissionEntries) {
   1.320 +    permPluginsRow.appendChild(permissionEntry);
   1.321 +  }
   1.322 +
   1.323 +  setPluginsRadioState();
   1.324 +}
   1.325 +
   1.326 +function setPluginsRadioState() {
   1.327 +  let box = document.getElementById("perm-plugins-row");
   1.328 +  for (let permissionEntry of box.childNodes) {
   1.329 +    if (permissionEntry.hasAttribute("permString")) {
   1.330 +      let permString = permissionEntry.getAttribute("permString");
   1.331 +      let permission = SitePermissions.get(gPermURI, permString);
   1.332 +      setRadioState(permString, permission);
   1.333 +    }
   1.334 +  }
   1.335 +}

mercurial