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 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | function debug(aMsg) { |
michael@0 | 8 | //dump("-*- PermissionSettings.js: " + aMsg + "\n"); |
michael@0 | 9 | } |
michael@0 | 10 | |
michael@0 | 11 | const Cc = Components.classes; |
michael@0 | 12 | const Ci = Components.interfaces; |
michael@0 | 13 | const Cu = Components.utils; |
michael@0 | 14 | |
michael@0 | 15 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 16 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 17 | Cu.import("resource://gre/modules/PermissionsTable.jsm"); |
michael@0 | 18 | |
michael@0 | 19 | var cpm = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender); |
michael@0 | 20 | |
michael@0 | 21 | // PermissionSettings |
michael@0 | 22 | |
michael@0 | 23 | const PERMISSIONSETTINGS_CONTRACTID = "@mozilla.org/permissionSettings;1"; |
michael@0 | 24 | const PERMISSIONSETTINGS_CID = Components.ID("{cd2cf7a1-f4c1-487b-8c1b-1a71c7097431}"); |
michael@0 | 25 | |
michael@0 | 26 | function PermissionSettings() |
michael@0 | 27 | { |
michael@0 | 28 | debug("Constructor"); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | XPCOMUtils.defineLazyServiceGetter(this, |
michael@0 | 32 | "permissionManager", |
michael@0 | 33 | "@mozilla.org/permissionmanager;1", |
michael@0 | 34 | "nsIPermissionManager"); |
michael@0 | 35 | |
michael@0 | 36 | XPCOMUtils.defineLazyServiceGetter(this, |
michael@0 | 37 | "secMan", |
michael@0 | 38 | "@mozilla.org/scriptsecuritymanager;1", |
michael@0 | 39 | "nsIScriptSecurityManager"); |
michael@0 | 40 | |
michael@0 | 41 | XPCOMUtils.defineLazyServiceGetter(this, |
michael@0 | 42 | "appsService", |
michael@0 | 43 | "@mozilla.org/AppsService;1", |
michael@0 | 44 | "nsIAppsService"); |
michael@0 | 45 | |
michael@0 | 46 | PermissionSettings.prototype = { |
michael@0 | 47 | get: function get(aPermName, aManifestURL, aOrigin, aBrowserFlag) { |
michael@0 | 48 | debug("Get called with: " + aPermName + ", " + aManifestURL + ", " + aOrigin + ", " + aBrowserFlag); |
michael@0 | 49 | let uri = Services.io.newURI(aOrigin, null, null); |
michael@0 | 50 | let appID = appsService.getAppLocalIdByManifestURL(aManifestURL); |
michael@0 | 51 | let principal = secMan.getAppCodebasePrincipal(uri, appID, aBrowserFlag); |
michael@0 | 52 | let result = permissionManager.testExactPermanentPermission(principal, aPermName); |
michael@0 | 53 | |
michael@0 | 54 | switch (result) |
michael@0 | 55 | { |
michael@0 | 56 | case Ci.nsIPermissionManager.UNKNOWN_ACTION: |
michael@0 | 57 | return "unknown"; |
michael@0 | 58 | case Ci.nsIPermissionManager.ALLOW_ACTION: |
michael@0 | 59 | return "allow"; |
michael@0 | 60 | case Ci.nsIPermissionManager.DENY_ACTION: |
michael@0 | 61 | return "deny"; |
michael@0 | 62 | case Ci.nsIPermissionManager.PROMPT_ACTION: |
michael@0 | 63 | return "prompt"; |
michael@0 | 64 | default: |
michael@0 | 65 | dump("Unsupported PermissionSettings Action!\n"); |
michael@0 | 66 | return "unknown"; |
michael@0 | 67 | } |
michael@0 | 68 | }, |
michael@0 | 69 | |
michael@0 | 70 | isExplicit: function isExplicit(aPermName, aManifestURL, aOrigin, |
michael@0 | 71 | aBrowserFlag) { |
michael@0 | 72 | debug("isExplicit: " + aPermName + ", " + aManifestURL + ", " + aOrigin); |
michael@0 | 73 | let uri = Services.io.newURI(aOrigin, null, null); |
michael@0 | 74 | let appID = appsService.getAppLocalIdByManifestURL(aManifestURL); |
michael@0 | 75 | let principal = secMan.getAppCodebasePrincipal(uri, appID, aBrowserFlag); |
michael@0 | 76 | |
michael@0 | 77 | return isExplicitInPermissionsTable(aPermName, principal.appStatus); |
michael@0 | 78 | }, |
michael@0 | 79 | |
michael@0 | 80 | set: function set(aPermName, aPermValue, aManifestURL, aOrigin, |
michael@0 | 81 | aBrowserFlag) { |
michael@0 | 82 | debug("Set called with: " + aPermName + ", " + aManifestURL + ", " + |
michael@0 | 83 | aOrigin + ", " + aPermValue + ", " + aBrowserFlag); |
michael@0 | 84 | let currentPermValue = this.get(aPermName, aManifestURL, aOrigin, |
michael@0 | 85 | aBrowserFlag); |
michael@0 | 86 | let action; |
michael@0 | 87 | // Check for invalid calls so that we throw an exception rather than get |
michael@0 | 88 | // killed by parent process |
michael@0 | 89 | if (currentPermValue === "unknown" || |
michael@0 | 90 | aPermValue === "unknown" || |
michael@0 | 91 | !this.isExplicit(aPermName, aManifestURL, aOrigin, aBrowserFlag)) { |
michael@0 | 92 | let errorMsg = "PermissionSettings.js: '" + aPermName + "'" + |
michael@0 | 93 | " is an implicit permission for '" + aManifestURL + |
michael@0 | 94 | "' or the permission isn't set"; |
michael@0 | 95 | Cu.reportError(errorMsg); |
michael@0 | 96 | throw new Components.Exception(errorMsg); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | cpm.sendSyncMessage("PermissionSettings:AddPermission", { |
michael@0 | 100 | type: aPermName, |
michael@0 | 101 | origin: aOrigin, |
michael@0 | 102 | manifestURL: aManifestURL, |
michael@0 | 103 | value: aPermValue, |
michael@0 | 104 | browserFlag: aBrowserFlag |
michael@0 | 105 | }); |
michael@0 | 106 | }, |
michael@0 | 107 | |
michael@0 | 108 | remove: function remove(aPermName, aManifestURL, aOrigin) { |
michael@0 | 109 | let uri = Services.io.newURI(aOrigin, null, null); |
michael@0 | 110 | let appID = appsService.getAppLocalIdByManifestURL(aManifestURL); |
michael@0 | 111 | let principal = secMan.getAppCodebasePrincipal(uri, appID, true); |
michael@0 | 112 | |
michael@0 | 113 | if (principal.appStatus !== Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED) { |
michael@0 | 114 | let errorMsg = "PermissionSettings.js: '" + aOrigin + "'" + |
michael@0 | 115 | " is installed or permission is implicit, cannot remove '" + |
michael@0 | 116 | aPermName + "'."; |
michael@0 | 117 | Cu.reportError(errorMsg); |
michael@0 | 118 | throw new Components.Exception(errorMsg); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | // PermissionSettings.jsm handles delete when value is "unknown" |
michael@0 | 122 | cpm.sendSyncMessage("PermissionSettings:AddPermission", { |
michael@0 | 123 | type: aPermName, |
michael@0 | 124 | origin: aOrigin, |
michael@0 | 125 | manifestURL: aManifestURL, |
michael@0 | 126 | value: "unknown", |
michael@0 | 127 | browserFlag: true |
michael@0 | 128 | }); |
michael@0 | 129 | }, |
michael@0 | 130 | |
michael@0 | 131 | classID : PERMISSIONSETTINGS_CID, |
michael@0 | 132 | QueryInterface : XPCOMUtils.generateQI([]) |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PermissionSettings]) |