BuiltInThemes.sys.mjs (2074B)
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 lazy = {}; 6 7 ChromeUtils.defineESModuleGetters(lazy, { 8 AddonManager: "resource://gre/modules/AddonManager.sys.mjs", 9 BuiltInThemeConfig: "resource:///modules/BuiltInThemeConfig.sys.mjs", 10 }); 11 12 const kActiveThemePref = "extensions.activeThemeID"; 13 14 class _BuiltInThemes { 15 /** 16 * The list of themes to be installed. This is exposed on the class so tests 17 * can set custom config files. 18 */ 19 builtInThemeMap = lazy.BuiltInThemeConfig; 20 21 /** 22 * @param {string} id An addon's id string. 23 * @returns {string} 24 * If `id` refers to a built-in theme, returns a path pointing to the 25 * theme's preview image. Null otherwise. 26 */ 27 previewForBuiltInThemeId(id) { 28 let theme = this.builtInThemeMap.get(id); 29 if (theme) { 30 return `${theme.path}preview.svg`; 31 } 32 33 return null; 34 } 35 36 /** 37 * If the active theme is built-in, this function calls 38 * AddonManager.maybeInstallBuiltinAddon for that theme. 39 */ 40 maybeInstallActiveBuiltInTheme() { 41 const activeThemeID = Services.prefs.getStringPref( 42 kActiveThemePref, 43 "default-theme@mozilla.org" 44 ); 45 let activeBuiltInTheme = this.builtInThemeMap.get(activeThemeID); 46 47 if (activeBuiltInTheme) { 48 lazy.AddonManager.maybeInstallBuiltinAddon( 49 activeThemeID, 50 activeBuiltInTheme.version, 51 activeBuiltInTheme.path 52 ); 53 } 54 } 55 56 /** 57 * Ensures that all built-in themes are installed and expired themes are 58 * uninstalled. 59 */ 60 async ensureBuiltInThemes() { 61 let installPromises = []; 62 63 for (let [id, themeInfo] of this.builtInThemeMap.entries()) { 64 installPromises.push( 65 lazy.AddonManager.maybeInstallBuiltinAddon( 66 id, 67 themeInfo.version, 68 themeInfo.path 69 ) 70 ); 71 } 72 73 await Promise.all(installPromises); 74 } 75 } 76 77 export var BuiltInThemes = new _BuiltInThemes();