IPPExceptionsManager.sys.mjs (2707B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const PERM_NAME = "ipp-vpn"; 6 7 /** 8 * Manages site exceptions for IP Protection. 9 * It communicates with Services.perms to update the ipp-vpn permission type. 10 * Site exclusions are marked as permissions with DENY capabilities. 11 * 12 * While permissions related UI (eg. panels and dialogs) already handle changes to ipp-vpn, 13 * the intention of this class is to abstract methods for updating ipp-vpn as needed 14 * from other non-permissions related UI. 15 */ 16 class ExceptionsManager { 17 #inited = false; 18 19 init() { 20 if (this.#inited) { 21 return; 22 } 23 24 this.#inited = true; 25 } 26 27 uninit() { 28 if (!this.#inited) { 29 return; 30 } 31 32 this.#inited = false; 33 } 34 35 /** 36 * Adds a principal to ipp-vpn with capability DENY_ACTION 37 * for site exclusions. 38 * 39 * @param {nsIPrincipal} principal 40 * The principal that we want to add as a site exception. 41 */ 42 addExclusion(principal) { 43 Services.perms.addFromPrincipal( 44 principal, 45 PERM_NAME, 46 Ci.nsIPermissionManager.DENY_ACTION 47 ); 48 } 49 50 /** 51 * Removes an existing principal from ipp-vpn. 52 * 53 * @param {nsIPrincipal} principal 54 * The principal that we want to remove as a site exception. 55 */ 56 removeExclusion(principal) { 57 Services.perms.removeFromPrincipal(principal, PERM_NAME); 58 } 59 60 /** 61 * Returns true if the principal already exists in ipp-vpn 62 * and is registered as a permission with a DENY_ACTION 63 * capability (site exclusions). 64 * Else returns false if no such principal exists. 65 * 66 * @param {nsIPrincipal} principal 67 * The principal that we want to check is saved in ipp-vpn 68 * as a site exclusion. 69 * @returns {boolean} 70 * True if the principal exists as a site exclusion. 71 */ 72 hasExclusion(principal) { 73 let permission = this.getExceptionPermissionObject(principal); 74 return permission?.capability === Ci.nsIPermissionManager.DENY_ACTION; 75 } 76 77 /** 78 * Get the permission object for a site exception if it exists in ipp-vpn. 79 * 80 * @param {nsIPrincipal} principal 81 * The principal that we want to check is saved in ipp-vpn. 82 * 83 * @returns {nsIPermission} 84 * The permission object for a site exception, or null if unavailable. 85 */ 86 getExceptionPermissionObject(principal) { 87 let permissionObject = Services.perms.getPermissionObject( 88 principal, 89 PERM_NAME, 90 true /* exactHost */ 91 ); 92 return permissionObject; 93 } 94 } 95 96 const IPPExceptionsManager = new ExceptionsManager(); 97 export { IPPExceptionsManager };