Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | var PermissionsHelper = { |
michael@0 | 7 | _permissonTypes: ["password", "geolocation", "popup", "indexedDB", |
michael@0 | 8 | "offline-app", "desktop-notification", "plugins", "native-intent"], |
michael@0 | 9 | _permissionStrings: { |
michael@0 | 10 | "password": { |
michael@0 | 11 | label: "password.savePassword", |
michael@0 | 12 | allowed: "password.save", |
michael@0 | 13 | denied: "password.dontSave" |
michael@0 | 14 | }, |
michael@0 | 15 | "geolocation": { |
michael@0 | 16 | label: "geolocation.shareLocation", |
michael@0 | 17 | allowed: "geolocation.allow", |
michael@0 | 18 | denied: "geolocation.dontAllow" |
michael@0 | 19 | }, |
michael@0 | 20 | "popup": { |
michael@0 | 21 | label: "blockPopups.label", |
michael@0 | 22 | allowed: "popup.show", |
michael@0 | 23 | denied: "popup.dontShow" |
michael@0 | 24 | }, |
michael@0 | 25 | "indexedDB": { |
michael@0 | 26 | label: "offlineApps.storeOfflineData", |
michael@0 | 27 | allowed: "offlineApps.allow", |
michael@0 | 28 | denied: "offlineApps.dontAllow2" |
michael@0 | 29 | }, |
michael@0 | 30 | "offline-app": { |
michael@0 | 31 | label: "offlineApps.storeOfflineData", |
michael@0 | 32 | allowed: "offlineApps.allow", |
michael@0 | 33 | denied: "offlineApps.dontAllow2" |
michael@0 | 34 | }, |
michael@0 | 35 | "desktop-notification": { |
michael@0 | 36 | label: "desktopNotification.useNotifications", |
michael@0 | 37 | allowed: "desktopNotification.allow", |
michael@0 | 38 | denied: "desktopNotification.dontAllow" |
michael@0 | 39 | }, |
michael@0 | 40 | "plugins": { |
michael@0 | 41 | label: "clickToPlayPlugins.activatePlugins", |
michael@0 | 42 | allowed: "clickToPlayPlugins.activate", |
michael@0 | 43 | denied: "clickToPlayPlugins.dontActivate" |
michael@0 | 44 | }, |
michael@0 | 45 | "native-intent": { |
michael@0 | 46 | label: "helperapps.openWithList2", |
michael@0 | 47 | allowed: "helperapps.always", |
michael@0 | 48 | denied: "helperapps.never" |
michael@0 | 49 | } |
michael@0 | 50 | }, |
michael@0 | 51 | |
michael@0 | 52 | observe: function observe(aSubject, aTopic, aData) { |
michael@0 | 53 | let uri = BrowserApp.selectedBrowser.currentURI; |
michael@0 | 54 | |
michael@0 | 55 | switch (aTopic) { |
michael@0 | 56 | case "Permissions:Get": |
michael@0 | 57 | let permissions = []; |
michael@0 | 58 | for (let i = 0; i < this._permissonTypes.length; i++) { |
michael@0 | 59 | let type = this._permissonTypes[i]; |
michael@0 | 60 | let value = this.getPermission(uri, type); |
michael@0 | 61 | |
michael@0 | 62 | // Only add the permission if it was set by the user |
michael@0 | 63 | if (value == Services.perms.UNKNOWN_ACTION) |
michael@0 | 64 | continue; |
michael@0 | 65 | |
michael@0 | 66 | // Get the strings that correspond to the permission type |
michael@0 | 67 | let typeStrings = this._permissionStrings[type]; |
michael@0 | 68 | let label = Strings.browser.GetStringFromName(typeStrings["label"]); |
michael@0 | 69 | |
michael@0 | 70 | // Get the key to look up the appropriate string entity |
michael@0 | 71 | let valueKey = value == Services.perms.ALLOW_ACTION ? |
michael@0 | 72 | "allowed" : "denied"; |
michael@0 | 73 | let valueString = Strings.browser.GetStringFromName(typeStrings[valueKey]); |
michael@0 | 74 | |
michael@0 | 75 | permissions.push({ |
michael@0 | 76 | type: type, |
michael@0 | 77 | setting: label, |
michael@0 | 78 | value: valueString |
michael@0 | 79 | }); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | // Keep track of permissions, so we know which ones to clear |
michael@0 | 83 | this._currentPermissions = permissions; |
michael@0 | 84 | |
michael@0 | 85 | let host; |
michael@0 | 86 | try { |
michael@0 | 87 | host = uri.host; |
michael@0 | 88 | } catch(e) { |
michael@0 | 89 | host = uri.spec; |
michael@0 | 90 | } |
michael@0 | 91 | sendMessageToJava({ |
michael@0 | 92 | type: "Permissions:Data", |
michael@0 | 93 | host: host, |
michael@0 | 94 | permissions: permissions |
michael@0 | 95 | }); |
michael@0 | 96 | break; |
michael@0 | 97 | |
michael@0 | 98 | case "Permissions:Clear": |
michael@0 | 99 | // An array of the indices of the permissions we want to clear |
michael@0 | 100 | let permissionsToClear = JSON.parse(aData); |
michael@0 | 101 | let privacyContext = BrowserApp.selectedBrowser.docShell |
michael@0 | 102 | .QueryInterface(Ci.nsILoadContext); |
michael@0 | 103 | |
michael@0 | 104 | for (let i = 0; i < permissionsToClear.length; i++) { |
michael@0 | 105 | let indexToClear = permissionsToClear[i]; |
michael@0 | 106 | let permissionType = this._currentPermissions[indexToClear]["type"]; |
michael@0 | 107 | this.clearPermission(uri, permissionType, privacyContext); |
michael@0 | 108 | } |
michael@0 | 109 | break; |
michael@0 | 110 | } |
michael@0 | 111 | }, |
michael@0 | 112 | |
michael@0 | 113 | /** |
michael@0 | 114 | * Gets the permission value stored for a specified permission type. |
michael@0 | 115 | * |
michael@0 | 116 | * @param aType |
michael@0 | 117 | * The permission type string stored in permission manager. |
michael@0 | 118 | * e.g. "geolocation", "indexedDB", "popup" |
michael@0 | 119 | * |
michael@0 | 120 | * @return A permission value defined in nsIPermissionManager. |
michael@0 | 121 | */ |
michael@0 | 122 | getPermission: function getPermission(aURI, aType) { |
michael@0 | 123 | // Password saving isn't a nsIPermissionManager permission type, so handle |
michael@0 | 124 | // it seperately. |
michael@0 | 125 | if (aType == "password") { |
michael@0 | 126 | // By default, login saving is enabled, so if it is disabled, the |
michael@0 | 127 | // user selected the never remember option |
michael@0 | 128 | if (!Services.logins.getLoginSavingEnabled(aURI.prePath)) |
michael@0 | 129 | return Services.perms.DENY_ACTION; |
michael@0 | 130 | |
michael@0 | 131 | // Check to see if the user ever actually saved a login |
michael@0 | 132 | if (Services.logins.countLogins(aURI.prePath, "", "")) |
michael@0 | 133 | return Services.perms.ALLOW_ACTION; |
michael@0 | 134 | |
michael@0 | 135 | return Services.perms.UNKNOWN_ACTION; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // Geolocation consumers use testExactPermission |
michael@0 | 139 | if (aType == "geolocation") |
michael@0 | 140 | return Services.perms.testExactPermission(aURI, aType); |
michael@0 | 141 | |
michael@0 | 142 | return Services.perms.testPermission(aURI, aType); |
michael@0 | 143 | }, |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * Clears a user-set permission value for the site given a permission type. |
michael@0 | 147 | * |
michael@0 | 148 | * @param aType |
michael@0 | 149 | * The permission type string stored in permission manager. |
michael@0 | 150 | * e.g. "geolocation", "indexedDB", "popup" |
michael@0 | 151 | */ |
michael@0 | 152 | clearPermission: function clearPermission(aURI, aType, aContext) { |
michael@0 | 153 | // Password saving isn't a nsIPermissionManager permission type, so handle |
michael@0 | 154 | // it seperately. |
michael@0 | 155 | if (aType == "password") { |
michael@0 | 156 | // Get rid of exisiting stored logings |
michael@0 | 157 | let logins = Services.logins.findLogins({}, aURI.prePath, "", ""); |
michael@0 | 158 | for (let i = 0; i < logins.length; i++) { |
michael@0 | 159 | Services.logins.removeLogin(logins[i]); |
michael@0 | 160 | } |
michael@0 | 161 | // Re-set login saving to enabled |
michael@0 | 162 | Services.logins.setLoginSavingEnabled(aURI.prePath, true); |
michael@0 | 163 | } else { |
michael@0 | 164 | Services.perms.remove(aURI.host, aType); |
michael@0 | 165 | // Clear content prefs set in ContentPermissionPrompt.js |
michael@0 | 166 | Cc["@mozilla.org/content-pref/service;1"] |
michael@0 | 167 | .getService(Ci.nsIContentPrefService2) |
michael@0 | 168 | .removeByDomainAndName(aURI.spec, aType + ".request.remember", aContext); |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | }; |