dom/permission/PermissionSettings.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

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(s) {
michael@0 8 //dump("-*- PermissionSettings Module: " + s + "\n");
michael@0 9 }
michael@0 10
michael@0 11 const Cu = Components.utils;
michael@0 12 const Cc = Components.classes;
michael@0 13 const Ci = Components.interfaces;
michael@0 14
michael@0 15 this.EXPORTED_SYMBOLS = ["PermissionSettingsModule"];
michael@0 16
michael@0 17 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 18 Cu.import("resource://gre/modules/Services.jsm");
michael@0 19 Cu.import("resource://gre/modules/PermissionsTable.jsm");
michael@0 20
michael@0 21 XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
michael@0 22 "@mozilla.org/parentprocessmessagemanager;1",
michael@0 23 "nsIMessageListenerManager");
michael@0 24
michael@0 25 XPCOMUtils.defineLazyServiceGetter(this,
michael@0 26 "permissionManager",
michael@0 27 "@mozilla.org/permissionmanager;1",
michael@0 28 "nsIPermissionManager");
michael@0 29
michael@0 30 XPCOMUtils.defineLazyServiceGetter(this,
michael@0 31 "secMan",
michael@0 32 "@mozilla.org/scriptsecuritymanager;1",
michael@0 33 "nsIScriptSecurityManager");
michael@0 34
michael@0 35 XPCOMUtils.defineLazyServiceGetter(this,
michael@0 36 "appsService",
michael@0 37 "@mozilla.org/AppsService;1",
michael@0 38 "nsIAppsService");
michael@0 39
michael@0 40 this.PermissionSettingsModule = {
michael@0 41 init: function init() {
michael@0 42 debug("Init");
michael@0 43 ppmm.addMessageListener("PermissionSettings:AddPermission", this);
michael@0 44 Services.obs.addObserver(this, "profile-before-change", false);
michael@0 45 },
michael@0 46
michael@0 47
michael@0 48 _isChangeAllowed: function(aPrincipal, aPermName, aAction) {
michael@0 49 // Bug 812289:
michael@0 50 // Change is allowed from a child process when all of the following
michael@0 51 // conditions stand true:
michael@0 52 // * the action isn't "unknown" (so the change isn't a delete) if the app
michael@0 53 // is installed
michael@0 54 // * the permission already exists on the database
michael@0 55 // * the permission is marked as explicit on the permissions table
michael@0 56 // Note that we *have* to check the first two conditions here because
michael@0 57 // permissionManager doesn't know if it's being called as a result of
michael@0 58 // a parent process or child process request. We could check
michael@0 59 // if the permission is actually explicit (and thus modifiable) or not
michael@0 60 // on permissionManager also but we currently don't.
michael@0 61 let perm =
michael@0 62 permissionManager.testExactPermissionFromPrincipal(aPrincipal,aPermName);
michael@0 63 let isExplicit = isExplicitInPermissionsTable(aPermName, aPrincipal.appStatus);
michael@0 64
michael@0 65 return (aAction === "unknown" &&
michael@0 66 aPrincipal.appStatus === Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED) ||
michael@0 67 (aAction !== "unknown" &&
michael@0 68 (perm !== Ci.nsIPermissionManager.UNKNOWN_ACTION) &&
michael@0 69 isExplicit);
michael@0 70 },
michael@0 71
michael@0 72 addPermission: function addPermission(aData, aCallbacks) {
michael@0 73
michael@0 74 this._internalAddPermission(aData, true, aCallbacks);
michael@0 75
michael@0 76 },
michael@0 77
michael@0 78
michael@0 79 _internalAddPermission: function _internalAddPermission(aData, aAllowAllChanges, aCallbacks) {
michael@0 80 let uri = Services.io.newURI(aData.origin, null, null);
michael@0 81 let appID = appsService.getAppLocalIdByManifestURL(aData.manifestURL);
michael@0 82 let principal = secMan.getAppCodebasePrincipal(uri, appID, aData.browserFlag);
michael@0 83
michael@0 84 let action;
michael@0 85 switch (aData.value)
michael@0 86 {
michael@0 87 case "unknown":
michael@0 88 action = Ci.nsIPermissionManager.UNKNOWN_ACTION;
michael@0 89 break;
michael@0 90 case "allow":
michael@0 91 action = Ci.nsIPermissionManager.ALLOW_ACTION;
michael@0 92 break;
michael@0 93 case "deny":
michael@0 94 action = Ci.nsIPermissionManager.DENY_ACTION;
michael@0 95 break;
michael@0 96 case "prompt":
michael@0 97 action = Ci.nsIPermissionManager.PROMPT_ACTION;
michael@0 98 break;
michael@0 99 default:
michael@0 100 dump("Unsupported PermisionSettings Action: " + aData.value +"\n");
michael@0 101 action = Ci.nsIPermissionManager.UNKNOWN_ACTION;
michael@0 102 }
michael@0 103
michael@0 104 if (aAllowAllChanges ||
michael@0 105 this._isChangeAllowed(principal, aData.type, aData.value)) {
michael@0 106 debug("add: " + aData.origin + " " + appID + " " + action);
michael@0 107 permissionManager.addFromPrincipal(principal, aData.type, action);
michael@0 108 return true;
michael@0 109 } else {
michael@0 110 debug("add Failure: " + aData.origin + " " + appID + " " + action);
michael@0 111 return false; // This isn't currently used, see comment on setPermission
michael@0 112 }
michael@0 113 },
michael@0 114
michael@0 115 getPermission: function getPermission(aPermName, aManifestURL, aOrigin, aBrowserFlag) {
michael@0 116 debug("getPermission: " + aPermName + ", " + aManifestURL + ", " + aOrigin);
michael@0 117 let uri = Services.io.newURI(aOrigin, null, null);
michael@0 118 let appID = appsService.getAppLocalIdByManifestURL(aManifestURL);
michael@0 119 let principal = secMan.getAppCodebasePrincipal(uri, appID, aBrowserFlag);
michael@0 120 let result = permissionManager.testExactPermissionFromPrincipal(principal, aPermName);
michael@0 121
michael@0 122 switch (result)
michael@0 123 {
michael@0 124 case Ci.nsIPermissionManager.UNKNOWN_ACTION:
michael@0 125 return "unknown";
michael@0 126 case Ci.nsIPermissionManager.ALLOW_ACTION:
michael@0 127 return "allow";
michael@0 128 case Ci.nsIPermissionManager.DENY_ACTION:
michael@0 129 return "deny";
michael@0 130 case Ci.nsIPermissionManager.PROMPT_ACTION:
michael@0 131 return "prompt";
michael@0 132 default:
michael@0 133 dump("Unsupported PermissionSettings Action!\n");
michael@0 134 return "unknown";
michael@0 135 }
michael@0 136 },
michael@0 137
michael@0 138 removePermission: function removePermission(aPermName, aManifestURL, aOrigin, aBrowserFlag) {
michael@0 139 let data = {
michael@0 140 type: aPermName,
michael@0 141 origin: aOrigin,
michael@0 142 manifestURL: aManifestURL,
michael@0 143 value: "unknown",
michael@0 144 browserFlag: aBrowserFlag
michael@0 145 };
michael@0 146 this._internalAddPermission(data, true);
michael@0 147 },
michael@0 148
michael@0 149 observe: function observe(aSubject, aTopic, aData) {
michael@0 150 ppmm.removeMessageListener("PermissionSettings:AddPermission", this);
michael@0 151 Services.obs.removeObserver(this, "profile-before-change");
michael@0 152 ppmm = null;
michael@0 153 },
michael@0 154
michael@0 155 receiveMessage: function receiveMessage(aMessage) {
michael@0 156 debug("PermissionSettings::receiveMessage " + aMessage.name);
michael@0 157 let mm = aMessage.target;
michael@0 158 let msg = aMessage.data;
michael@0 159
michael@0 160 let result;
michael@0 161 switch (aMessage.name) {
michael@0 162 case "PermissionSettings:AddPermission":
michael@0 163 let success = false;
michael@0 164 let errorMsg =
michael@0 165 " from a content process with no 'permissions' privileges.";
michael@0 166 if (mm.assertPermission("permissions")) {
michael@0 167 success = this._internalAddPermission(msg, false);
michael@0 168 if (!success) {
michael@0 169 // Just kill the calling process
michael@0 170 mm.assertPermission("permissions-modify-implicit");
michael@0 171 errorMsg = " had an implicit permission change. Child process killed.";
michael@0 172 }
michael@0 173 }
michael@0 174
michael@0 175 if (!success) {
michael@0 176 Cu.reportError("PermissionSettings message " + msg.type + errorMsg);
michael@0 177 return null;
michael@0 178 }
michael@0 179 break;
michael@0 180 }
michael@0 181 }
michael@0 182 }
michael@0 183
michael@0 184 PermissionSettingsModule.init();

mercurial