michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource:///modules/SitePermissions.jsm"); michael@0: michael@0: const nsIQuotaManager = Components.interfaces.nsIQuotaManager; michael@0: michael@0: var gPermURI; michael@0: var gUsageRequest; michael@0: michael@0: var gPermissions = SitePermissions.listPermissions(); michael@0: gPermissions.push("plugins"); michael@0: michael@0: var permissionObserver = { michael@0: observe: function (aSubject, aTopic, aData) michael@0: { michael@0: if (aTopic == "perm-changed") { michael@0: var permission = aSubject.QueryInterface(Components.interfaces.nsIPermission); michael@0: if (permission.host == gPermURI.host) { michael@0: if (gPermissions.indexOf(permission.type) > -1) michael@0: initRow(permission.type); michael@0: else if (permission.type.startsWith("plugin")) michael@0: setPluginsRadioState(); michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: function onLoadPermission() michael@0: { michael@0: var uri = gDocument.documentURIObject; michael@0: var permTab = document.getElementById("permTab"); michael@0: if (SitePermissions.isSupportedURI(uri)) { michael@0: gPermURI = uri; michael@0: var hostText = document.getElementById("hostText"); michael@0: hostText.value = gPermURI.host; michael@0: michael@0: for (var i of gPermissions) michael@0: initRow(i); michael@0: var os = Components.classes["@mozilla.org/observer-service;1"] michael@0: .getService(Components.interfaces.nsIObserverService); michael@0: os.addObserver(permissionObserver, "perm-changed", false); michael@0: onUnloadRegistry.push(onUnloadPermission); michael@0: permTab.hidden = false; michael@0: } michael@0: else michael@0: permTab.hidden = true; michael@0: } michael@0: michael@0: function onUnloadPermission() michael@0: { michael@0: var os = Components.classes["@mozilla.org/observer-service;1"] michael@0: .getService(Components.interfaces.nsIObserverService); michael@0: os.removeObserver(permissionObserver, "perm-changed"); michael@0: michael@0: if (gUsageRequest) { michael@0: gUsageRequest.cancel(); michael@0: gUsageRequest = null; michael@0: } michael@0: } michael@0: michael@0: function initRow(aPartId) michael@0: { michael@0: if (aPartId == "plugins") { michael@0: initPluginsRow(); michael@0: return; michael@0: } michael@0: michael@0: createRow(aPartId); michael@0: michael@0: var checkbox = document.getElementById(aPartId + "Def"); michael@0: var command = document.getElementById("cmd_" + aPartId + "Toggle"); michael@0: var perm = SitePermissions.get(gPermURI, aPartId); michael@0: michael@0: if (perm) { michael@0: checkbox.checked = false; michael@0: command.removeAttribute("disabled"); michael@0: } michael@0: else { michael@0: checkbox.checked = true; michael@0: command.setAttribute("disabled", "true"); michael@0: perm = SitePermissions.getDefault(aPartId); michael@0: } michael@0: setRadioState(aPartId, perm); michael@0: michael@0: if (aPartId == "indexedDB") { michael@0: initIndexedDBRow(); michael@0: } michael@0: } michael@0: michael@0: function createRow(aPartId) { michael@0: let rowId = "perm-" + aPartId + "-row"; michael@0: if (document.getElementById(rowId)) michael@0: return; michael@0: michael@0: let commandId = "cmd_" + aPartId + "Toggle"; michael@0: let labelId = "perm-" + aPartId + "-label"; michael@0: let radiogroupId = aPartId + "RadioGroup"; michael@0: michael@0: let command = document.createElement("command"); michael@0: command.setAttribute("id", commandId); michael@0: command.setAttribute("oncommand", "onRadioClick('" + aPartId + "');"); michael@0: document.getElementById("pageInfoCommandSet").appendChild(command); michael@0: michael@0: let row = document.createElement("vbox"); michael@0: row.setAttribute("id", rowId); michael@0: row.setAttribute("class", "permission"); michael@0: michael@0: let label = document.createElement("label"); michael@0: label.setAttribute("id", labelId); michael@0: label.setAttribute("control", radiogroupId); michael@0: label.setAttribute("value", SitePermissions.getPermissionLabel(aPartId)); michael@0: label.setAttribute("class", "permissionLabel"); michael@0: row.appendChild(label); michael@0: michael@0: let controls = document.createElement("hbox"); michael@0: controls.setAttribute("role", "group"); michael@0: controls.setAttribute("aria-labelledby", labelId); michael@0: michael@0: let checkbox = document.createElement("checkbox"); michael@0: checkbox.setAttribute("id", aPartId + "Def"); michael@0: checkbox.setAttribute("oncommand", "onCheckboxClick('" + aPartId + "');"); michael@0: checkbox.setAttribute("label", gBundle.getString("permissions.useDefault")); michael@0: controls.appendChild(checkbox); michael@0: michael@0: let spacer = document.createElement("spacer"); michael@0: spacer.setAttribute("flex", "1"); michael@0: controls.appendChild(spacer); michael@0: michael@0: let radiogroup = document.createElement("radiogroup"); michael@0: radiogroup.setAttribute("id", radiogroupId); michael@0: radiogroup.setAttribute("orient", "horizontal"); michael@0: for (let state of SitePermissions.getAvailableStates(aPartId)) { michael@0: let radio = document.createElement("radio"); michael@0: radio.setAttribute("id", aPartId + "#" + state); michael@0: radio.setAttribute("label", SitePermissions.getStateLabel(aPartId, state)); michael@0: radio.setAttribute("command", commandId); michael@0: radiogroup.appendChild(radio); michael@0: } michael@0: controls.appendChild(radiogroup); michael@0: michael@0: row.appendChild(controls); michael@0: michael@0: document.getElementById("permList").appendChild(row); michael@0: } michael@0: michael@0: function onCheckboxClick(aPartId) michael@0: { michael@0: var command = document.getElementById("cmd_" + aPartId + "Toggle"); michael@0: var checkbox = document.getElementById(aPartId + "Def"); michael@0: if (checkbox.checked) { michael@0: SitePermissions.remove(gPermURI, aPartId); michael@0: command.setAttribute("disabled", "true"); michael@0: var perm = SitePermissions.getDefault(aPartId); michael@0: setRadioState(aPartId, perm); michael@0: } michael@0: else { michael@0: onRadioClick(aPartId); michael@0: command.removeAttribute("disabled"); michael@0: } michael@0: } michael@0: michael@0: function onPluginRadioClick(aEvent) { michael@0: onRadioClick(aEvent.originalTarget.getAttribute("id").split('#')[0]); michael@0: } michael@0: michael@0: function onRadioClick(aPartId) michael@0: { michael@0: var radioGroup = document.getElementById(aPartId + "RadioGroup"); michael@0: var id = radioGroup.selectedItem.id; michael@0: var permission = id.split('#')[1]; michael@0: SitePermissions.set(gPermURI, aPartId, permission); michael@0: } michael@0: michael@0: function setRadioState(aPartId, aValue) michael@0: { michael@0: var radio = document.getElementById(aPartId + "#" + aValue); michael@0: radio.radioGroup.selectedItem = radio; michael@0: } michael@0: michael@0: function initIndexedDBRow() michael@0: { michael@0: let row = document.getElementById("perm-indexedDB-row"); michael@0: let extras = document.getElementById("perm-indexedDB-extras"); michael@0: michael@0: row.appendChild(extras); michael@0: michael@0: var quotaManager = Components.classes["@mozilla.org/dom/quota/manager;1"] michael@0: .getService(nsIQuotaManager); michael@0: gUsageRequest = michael@0: quotaManager.getUsageForURI(gPermURI, onIndexedDBUsageCallback); michael@0: michael@0: var status = document.getElementById("indexedDBStatus"); michael@0: var button = document.getElementById("indexedDBClear"); michael@0: michael@0: status.value = ""; michael@0: status.setAttribute("hidden", "true"); michael@0: button.setAttribute("hidden", "true"); michael@0: } michael@0: michael@0: function onIndexedDBClear() michael@0: { michael@0: Components.classes["@mozilla.org/dom/quota/manager;1"] michael@0: .getService(nsIQuotaManager) michael@0: .clearStoragesForURI(gPermURI); michael@0: michael@0: SitePermissions.remove(gPermURI, "indexedDB-unlimited"); michael@0: initIndexedDBRow(); michael@0: } michael@0: michael@0: function onIndexedDBUsageCallback(uri, usage, fileUsage) michael@0: { michael@0: if (!uri.equals(gPermURI)) { michael@0: throw new Error("Callback received for bad URI: " + uri); michael@0: } michael@0: michael@0: if (usage) { michael@0: if (!("DownloadUtils" in window)) { michael@0: Components.utils.import("resource://gre/modules/DownloadUtils.jsm"); michael@0: } michael@0: michael@0: var status = document.getElementById("indexedDBStatus"); michael@0: var button = document.getElementById("indexedDBClear"); michael@0: michael@0: status.value = michael@0: gBundle.getFormattedString("indexedDBUsage", michael@0: DownloadUtils.convertByteUnits(usage)); michael@0: status.removeAttribute("hidden"); michael@0: button.removeAttribute("hidden"); michael@0: } michael@0: } michael@0: michael@0: // XXX copied this from browser-plugins.js - is there a way to share? michael@0: function makeNicePluginName(aName) { michael@0: if (aName == "Shockwave Flash") michael@0: return "Adobe Flash"; michael@0: michael@0: // Clean up the plugin name by stripping off any trailing version numbers michael@0: // or "plugin". EG, "Foo Bar Plugin 1.23_02" --> "Foo Bar" michael@0: // Do this by first stripping the numbers, etc. off the end, and then michael@0: // removing "Plugin" (and then trimming to get rid of any whitespace). michael@0: // (Otherwise, something like "Java(TM) Plug-in 1.7.0_07" gets mangled) michael@0: let newName = aName.replace(/[\s\d\.\-\_\(\)]+$/, "").replace(/\bplug-?in\b/i, "").trim(); michael@0: return newName; michael@0: } michael@0: michael@0: function fillInPluginPermissionTemplate(aPluginName, aPermissionString) { michael@0: let permPluginTemplate = document.getElementById("permPluginTemplate").cloneNode(true); michael@0: permPluginTemplate.setAttribute("permString", aPermissionString); michael@0: let attrs = [ michael@0: [ ".permPluginTemplateLabel", "value", aPluginName ], michael@0: [ ".permPluginTemplateRadioGroup", "id", aPermissionString + "RadioGroup" ], michael@0: [ ".permPluginTemplateRadioDefault", "id", aPermissionString + "#0" ], michael@0: [ ".permPluginTemplateRadioAsk", "id", aPermissionString + "#3" ], michael@0: [ ".permPluginTemplateRadioAllow", "id", aPermissionString + "#1" ], michael@0: [ ".permPluginTemplateRadioBlock", "id", aPermissionString + "#2" ] michael@0: ]; michael@0: michael@0: for (let attr of attrs) { michael@0: permPluginTemplate.querySelector(attr[0]).setAttribute(attr[1], attr[2]); michael@0: } michael@0: michael@0: return permPluginTemplate; michael@0: } michael@0: michael@0: function clearPluginPermissionTemplate() { michael@0: let permPluginTemplate = document.getElementById("permPluginTemplate"); michael@0: permPluginTemplate.hidden = true; michael@0: permPluginTemplate.removeAttribute("permString"); michael@0: document.querySelector(".permPluginTemplateLabel").removeAttribute("value"); michael@0: document.querySelector(".permPluginTemplateRadioGroup").removeAttribute("id"); michael@0: document.querySelector(".permPluginTemplateRadioAsk").removeAttribute("id"); michael@0: document.querySelector(".permPluginTemplateRadioAllow").removeAttribute("id"); michael@0: document.querySelector(".permPluginTemplateRadioBlock").removeAttribute("id"); michael@0: } michael@0: michael@0: function initPluginsRow() { michael@0: let vulnerableLabel = document.getElementById("browserBundle").getString("pluginActivateVulnerable.label"); michael@0: let pluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost); michael@0: michael@0: let permissionMap = Map(); michael@0: michael@0: for (let plugin of pluginHost.getPluginTags()) { michael@0: if (plugin.disabled) { michael@0: continue; michael@0: } michael@0: for (let mimeType of plugin.getMimeTypes()) { michael@0: let permString = pluginHost.getPermissionStringForType(mimeType); michael@0: if (!permissionMap.has(permString)) { michael@0: var name = makeNicePluginName(plugin.name); michael@0: if (permString.startsWith("plugin-vulnerable:")) { michael@0: name += " \u2014 " + vulnerableLabel; michael@0: } michael@0: permissionMap.set(permString, name); michael@0: } michael@0: } michael@0: } michael@0: michael@0: let entries = [{name: item[1], permission: item[0]} for (item of permissionMap)]; michael@0: entries.sort(function(a, b) { michael@0: return a.name < b.name ? -1 : (a.name == b.name ? 0 : 1); michael@0: }); michael@0: michael@0: let permissionEntries = [ michael@0: fillInPluginPermissionTemplate(p.name, p.permission) for (p of entries) michael@0: ]; michael@0: michael@0: let permPluginsRow = document.getElementById("perm-plugins-row"); michael@0: clearPluginPermissionTemplate(); michael@0: if (permissionEntries.length < 1) { michael@0: permPluginsRow.hidden = true; michael@0: return; michael@0: } michael@0: michael@0: for (let permissionEntry of permissionEntries) { michael@0: permPluginsRow.appendChild(permissionEntry); michael@0: } michael@0: michael@0: setPluginsRadioState(); michael@0: } michael@0: michael@0: function setPluginsRadioState() { michael@0: let box = document.getElementById("perm-plugins-row"); michael@0: for (let permissionEntry of box.childNodes) { michael@0: if (permissionEntry.hasAttribute("permString")) { michael@0: let permString = permissionEntry.getAttribute("permString"); michael@0: let permission = SitePermissions.get(gPermURI, permString); michael@0: setRadioState(permString, permission); michael@0: } michael@0: } michael@0: }