PermissionTestUtils.sys.mjs (3839B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /* 5 * Utility module for tests to access the PermissionManager 6 * with uri or origin string parameters. 7 */ 8 9 let pm = Services.perms; 10 11 let secMan = Services.scriptSecurityManager; 12 13 /** 14 * Convert origin string or uri to principal. 15 * If passed an nsIPrincipal it will be returned without conversion. 16 * 17 * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject - Subject to convert to principal 18 * @returns {Ci.nsIPrincipal} Principal created from subject 19 */ 20 function convertToPrincipal(subject) { 21 if (subject instanceof Ci.nsIPrincipal) { 22 return subject; 23 } 24 if (typeof subject === "string") { 25 return secMan.createContentPrincipalFromOrigin(subject); 26 } 27 if (subject === null || subject instanceof Ci.nsIURI) { 28 return secMan.createContentPrincipal(subject, {}); 29 } 30 throw new Error( 31 "subject parameter must be an nsIURI an origin string or a principal." 32 ); 33 } 34 35 export let PermissionTestUtils = { 36 /** 37 * Add permission information for a given subject. 38 * Subject can be a principal, uri or origin string. 39 * 40 * @see nsIPermissionManager for documentation 41 * 42 * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject 43 * @param {*} args 44 */ 45 add(subject, ...args) { 46 return pm.addFromPrincipal(convertToPrincipal(subject), ...args); 47 }, 48 /** 49 * Add permission information for a given subject. 50 * Subject can be a principal, uri or origin string. 51 * 52 * This is a variant of add that allows specifying modification time. 53 * 54 * @see nsIPermissionManager for documentation 55 * 56 * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject 57 * @param {*} args 58 */ 59 addWithModificationTime(subject, ...args) { 60 return pm.testAddFromPrincipalByTime(convertToPrincipal(subject), ...args); 61 }, 62 /** 63 * Get all custom permissions for a given subject. 64 * Subject can be a principal, uri or origin string. 65 * 66 * @see nsIPermissionManager for documentation 67 * 68 * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject 69 * @param {*} args 70 */ 71 getAll(subject, ...args) { 72 return pm.getAllForPrincipal(convertToPrincipal(subject), ...args); 73 }, 74 /** 75 * Remove permission information for a given subject and permission type 76 * Subject can be a principal, uri or origin string. 77 * 78 * @see nsIPermissionManager for documentation 79 * 80 * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject 81 * @param {*} args 82 */ 83 remove(subject, ...args) { 84 return pm.removeFromPrincipal(convertToPrincipal(subject), ...args); 85 }, 86 /** 87 * Test whether a website has permission to perform the given action. 88 * Subject can be a principal, uri or origin string. 89 * 90 * @see nsIPermissionManager for documentation 91 * 92 * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject 93 * @param {*} args 94 */ 95 testPermission(subject, ...args) { 96 return pm.testPermissionFromPrincipal(convertToPrincipal(subject), ...args); 97 }, 98 /** 99 * Test whether a website has permission to perform the given action. 100 * Subject can be a principal, uri or origin string. 101 * 102 * @see nsIPermissionManager for documentation 103 * 104 * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject 105 * @param {*} args 106 */ 107 testExactPermission(subject, ...args) { 108 return pm.testExactPermissionFromPrincipal( 109 convertToPrincipal(subject), 110 ...args 111 ); 112 }, 113 /** 114 * Get the permission object associated with the given subject and action. 115 * Subject can be a principal, uri or origin string. 116 * 117 * @see nsIPermissionManager for documentation 118 * 119 * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject 120 * @param {*} args 121 */ 122 getPermissionObject(subject, type, exactHost = false) { 123 return pm.getPermissionObject(convertToPrincipal(subject), type, exactHost); 124 }, 125 };