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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: var PermissionsHelper = { michael@0: _permissonTypes: ["password", "geolocation", "popup", "indexedDB", michael@0: "offline-app", "desktop-notification", "plugins", "native-intent"], michael@0: _permissionStrings: { michael@0: "password": { michael@0: label: "password.savePassword", michael@0: allowed: "password.save", michael@0: denied: "password.dontSave" michael@0: }, michael@0: "geolocation": { michael@0: label: "geolocation.shareLocation", michael@0: allowed: "geolocation.allow", michael@0: denied: "geolocation.dontAllow" michael@0: }, michael@0: "popup": { michael@0: label: "blockPopups.label", michael@0: allowed: "popup.show", michael@0: denied: "popup.dontShow" michael@0: }, michael@0: "indexedDB": { michael@0: label: "offlineApps.storeOfflineData", michael@0: allowed: "offlineApps.allow", michael@0: denied: "offlineApps.dontAllow2" michael@0: }, michael@0: "offline-app": { michael@0: label: "offlineApps.storeOfflineData", michael@0: allowed: "offlineApps.allow", michael@0: denied: "offlineApps.dontAllow2" michael@0: }, michael@0: "desktop-notification": { michael@0: label: "desktopNotification.useNotifications", michael@0: allowed: "desktopNotification.allow", michael@0: denied: "desktopNotification.dontAllow" michael@0: }, michael@0: "plugins": { michael@0: label: "clickToPlayPlugins.activatePlugins", michael@0: allowed: "clickToPlayPlugins.activate", michael@0: denied: "clickToPlayPlugins.dontActivate" michael@0: }, michael@0: "native-intent": { michael@0: label: "helperapps.openWithList2", michael@0: allowed: "helperapps.always", michael@0: denied: "helperapps.never" michael@0: } michael@0: }, michael@0: michael@0: observe: function observe(aSubject, aTopic, aData) { michael@0: let uri = BrowserApp.selectedBrowser.currentURI; michael@0: michael@0: switch (aTopic) { michael@0: case "Permissions:Get": michael@0: let permissions = []; michael@0: for (let i = 0; i < this._permissonTypes.length; i++) { michael@0: let type = this._permissonTypes[i]; michael@0: let value = this.getPermission(uri, type); michael@0: michael@0: // Only add the permission if it was set by the user michael@0: if (value == Services.perms.UNKNOWN_ACTION) michael@0: continue; michael@0: michael@0: // Get the strings that correspond to the permission type michael@0: let typeStrings = this._permissionStrings[type]; michael@0: let label = Strings.browser.GetStringFromName(typeStrings["label"]); michael@0: michael@0: // Get the key to look up the appropriate string entity michael@0: let valueKey = value == Services.perms.ALLOW_ACTION ? michael@0: "allowed" : "denied"; michael@0: let valueString = Strings.browser.GetStringFromName(typeStrings[valueKey]); michael@0: michael@0: permissions.push({ michael@0: type: type, michael@0: setting: label, michael@0: value: valueString michael@0: }); michael@0: } michael@0: michael@0: // Keep track of permissions, so we know which ones to clear michael@0: this._currentPermissions = permissions; michael@0: michael@0: let host; michael@0: try { michael@0: host = uri.host; michael@0: } catch(e) { michael@0: host = uri.spec; michael@0: } michael@0: sendMessageToJava({ michael@0: type: "Permissions:Data", michael@0: host: host, michael@0: permissions: permissions michael@0: }); michael@0: break; michael@0: michael@0: case "Permissions:Clear": michael@0: // An array of the indices of the permissions we want to clear michael@0: let permissionsToClear = JSON.parse(aData); michael@0: let privacyContext = BrowserApp.selectedBrowser.docShell michael@0: .QueryInterface(Ci.nsILoadContext); michael@0: michael@0: for (let i = 0; i < permissionsToClear.length; i++) { michael@0: let indexToClear = permissionsToClear[i]; michael@0: let permissionType = this._currentPermissions[indexToClear]["type"]; michael@0: this.clearPermission(uri, permissionType, privacyContext); michael@0: } michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Gets the permission value stored for a specified permission type. michael@0: * michael@0: * @param aType michael@0: * The permission type string stored in permission manager. michael@0: * e.g. "geolocation", "indexedDB", "popup" michael@0: * michael@0: * @return A permission value defined in nsIPermissionManager. michael@0: */ michael@0: getPermission: function getPermission(aURI, aType) { michael@0: // Password saving isn't a nsIPermissionManager permission type, so handle michael@0: // it seperately. michael@0: if (aType == "password") { michael@0: // By default, login saving is enabled, so if it is disabled, the michael@0: // user selected the never remember option michael@0: if (!Services.logins.getLoginSavingEnabled(aURI.prePath)) michael@0: return Services.perms.DENY_ACTION; michael@0: michael@0: // Check to see if the user ever actually saved a login michael@0: if (Services.logins.countLogins(aURI.prePath, "", "")) michael@0: return Services.perms.ALLOW_ACTION; michael@0: michael@0: return Services.perms.UNKNOWN_ACTION; michael@0: } michael@0: michael@0: // Geolocation consumers use testExactPermission michael@0: if (aType == "geolocation") michael@0: return Services.perms.testExactPermission(aURI, aType); michael@0: michael@0: return Services.perms.testPermission(aURI, aType); michael@0: }, michael@0: michael@0: /** michael@0: * Clears a user-set permission value for the site given a permission type. michael@0: * michael@0: * @param aType michael@0: * The permission type string stored in permission manager. michael@0: * e.g. "geolocation", "indexedDB", "popup" michael@0: */ michael@0: clearPermission: function clearPermission(aURI, aType, aContext) { michael@0: // Password saving isn't a nsIPermissionManager permission type, so handle michael@0: // it seperately. michael@0: if (aType == "password") { michael@0: // Get rid of exisiting stored logings michael@0: let logins = Services.logins.findLogins({}, aURI.prePath, "", ""); michael@0: for (let i = 0; i < logins.length; i++) { michael@0: Services.logins.removeLogin(logins[i]); michael@0: } michael@0: // Re-set login saving to enabled michael@0: Services.logins.setLoginSavingEnabled(aURI.prePath, true); michael@0: } else { michael@0: Services.perms.remove(aURI.host, aType); michael@0: // Clear content prefs set in ContentPermissionPrompt.js michael@0: Cc["@mozilla.org/content-pref/service;1"] michael@0: .getService(Ci.nsIContentPrefService2) michael@0: .removeByDomainAndName(aURI.spec, aType + ".request.remember", aContext); michael@0: } michael@0: } michael@0: };