SecurityLevelNotification.sys.mjs (3326B)
1 const lazy = {}; 2 3 ChromeUtils.defineESModuleGetters(lazy, { 4 BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.sys.mjs", 5 SecurityLevelPrefs: "resource://gre/modules/SecurityLevel.sys.mjs", 6 }); 7 8 ChromeUtils.defineLazyGetter(lazy, "NotificationStrings", function () { 9 return new Localization([ 10 "branding/brand.ftl", 11 "toolkit/global/base-browser.ftl", 12 ]); 13 }); 14 15 /** 16 * Interface for showing the security level restart notification on desktop. 17 */ 18 export const SecurityLevelNotification = { 19 /** 20 * Whether we have already been initialised. 21 * 22 * @type {boolean} 23 */ 24 _initialized: false, 25 26 /** 27 * Called when the UI is ready to show a notification. 28 */ 29 ready() { 30 if (this._initialized) { 31 return; 32 } 33 this._initialized = true; 34 lazy.SecurityLevelPrefs.setNotificationHandler(this); 35 }, 36 37 /** 38 * Show the restart notification, and perform the restart if the user agrees. 39 * 40 * @returns {boolean} - Whether we are restarting the browser. 41 */ 42 async tryRestartBrowser() { 43 const [titleText, bodyText, primaryButtonText, secondaryButtonText] = 44 await lazy.NotificationStrings.formatValues([ 45 { id: "security-level-restart-prompt-title" }, 46 { id: "security-level-restart-prompt-body" }, 47 { id: "restart-warning-dialog-restart-button" }, 48 { id: "security-level-restart-prompt-button-ignore" }, 49 ]); 50 const buttonFlags = 51 Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0 + 52 Services.prompt.BUTTON_POS_0_DEFAULT + 53 Services.prompt.BUTTON_DEFAULT_IS_DESTRUCTIVE + 54 Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_1; 55 56 const propBag = await Services.prompt.asyncConfirmEx( 57 lazy.BrowserWindowTracker.getTopWindow()?.browsingContext ?? null, 58 Services.prompt.MODAL_TYPE_INTERNAL_WINDOW, 59 titleText, 60 bodyText, 61 buttonFlags, 62 primaryButtonText, 63 secondaryButtonText, 64 null, 65 null, 66 null, 67 {} 68 ); 69 70 if (propBag.get("buttonNumClicked") === 0) { 71 Services.startup.quit( 72 Services.startup.eAttemptQuit | Services.startup.eRestart 73 ); 74 return true; 75 } 76 return false; 77 }, 78 79 /** 80 * Show or re-show the custom security notification. 81 * 82 * @param {Function} userDismissedCallback - The callback for when the user 83 * dismisses the notification. 84 */ 85 async showCustomWarning(userDismissedCallback) { 86 const win = lazy.BrowserWindowTracker.getTopWindow(); 87 if (!win) { 88 return; 89 } 90 const typeName = "security-level-custom"; 91 const existing = win.gNotificationBox.getNotificationWithValue(typeName); 92 if (existing) { 93 win.gNotificationBox.removeNotification(existing); 94 } 95 96 const buttons = [ 97 { 98 "l10n-id": "security-level-panel-open-settings-button", 99 callback() { 100 win.openPreferences("privacy-securitylevel"); 101 }, 102 }, 103 ]; 104 105 win.gNotificationBox.appendNotification( 106 typeName, 107 { 108 label: { "l10n-id": "security-level-summary-custom" }, 109 priority: win.gNotificationBox.PRIORITY_WARNING_HIGH, 110 eventCallback: event => { 111 if (event === "dismissed") { 112 userDismissedCallback(); 113 } 114 }, 115 }, 116 buttons 117 ); 118 }, 119 };