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/. */
5 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
9 ChromeUtils.defineESModuleGetters(lazy, {
10 AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
11 BuiltInThemeConfig: "resource:///modules/BuiltInThemeConfig.sys.mjs",
14 const ColorwayL10n = new Localization(["browser/colorways.ftl"], true);
16 const kActiveThemePref = "extensions.activeThemeID";
17 const kRetainedThemesPref = "browser.theme.retainedExpiredThemes";
19 const ColorwayIntensityIdPostfixToL10nMap = [
20 ["-soft-colorway@mozilla.org", "colorway-intensity-soft"],
21 ["-balanced-colorway@mozilla.org", "colorway-intensity-balanced"],
22 ["-bold-colorway@mozilla.org", "colorway-intensity-bold"],
25 XPCOMUtils.defineLazyPreferenceGetter(
38 parsedVal = JSON.parse(val);
40 console.log(`${kRetainedThemesPref} has invalid value.`);
48 class _BuiltInThemes {
50 * The list of themes to be installed. This is exposed on the class so tests
51 * can set custom config files.
53 builtInThemeMap = lazy.BuiltInThemeConfig;
56 * @param {string} id An addon's id string.
58 * If `id` refers to a built-in theme, returns a path pointing to the
59 * theme's preview image. Null otherwise.
61 previewForBuiltInThemeId(id) {
62 let theme = this.builtInThemeMap.get(id);
64 return `${theme.path}preview.svg`;
71 * If the active theme is built-in, this function calls
72 * AddonManager.maybeInstallBuiltinAddon for that theme.
74 maybeInstallActiveBuiltInTheme() {
75 const activeThemeID = Services.prefs.getStringPref(
77 "default-theme@mozilla.org"
79 let activeBuiltInTheme = this.builtInThemeMap.get(activeThemeID);
81 if (activeBuiltInTheme) {
82 lazy.AddonManager.maybeInstallBuiltinAddon(
84 activeBuiltInTheme.version,
85 `resource://builtin-themes/${activeBuiltInTheme.path}`
91 * Ensures that all built-in themes are installed and expired themes are
94 async ensureBuiltInThemes() {
95 let installPromises = [];
96 installPromises.push(this._uninstallExpiredThemes());
98 const now = new Date();
99 for (let [id, themeInfo] of this.builtInThemeMap.entries()) {
102 lazy.retainedThemes.includes(id) ||
103 new Date(themeInfo.expiry) > now
105 installPromises.push(
106 lazy.AddonManager.maybeInstallBuiltinAddon(
115 await Promise.all(installPromises);
122 * Returns true if the theme is expired. False otherwise.
123 * @note This looks up the id in a Map rather than accessing a property on
124 * the addon itself. That makes calls to this function O(m) where m is the
125 * total number of built-in themes offered now or in the past. Since we
126 * are using a Map, calls are O(1) in the average case.
129 let themeInfo = this.builtInThemeMap.get(id);
130 return themeInfo?.expiry && new Date(themeInfo.expiry) < new Date();
137 * True if the theme with id `id` is both expired and retained. That is,
138 * the user has the ability to use it after its expiry date.
140 isRetainedExpiredTheme(id) {
141 return lazy.retainedThemes.includes(id) && this.themeIsExpired(id);
148 * True if the theme with id `id` is from the currently active theme.
153 Services.prefs.getStringPref(
155 "default-theme@mozilla.org"
161 * Uninstalls themes after they expire. If the expired theme is active, then
162 * it is not uninstalled. Instead, it is saved so that the user can use it
165 async _uninstallExpiredThemes() {
166 const activeThemeID = Services.prefs.getStringPref(
168 "default-theme@mozilla.org"
170 const now = new Date();
171 const expiredThemes = Array.from(this.builtInThemeMap.entries()).filter(
173 !!themeInfo.expiry &&
174 !lazy.retainedThemes.includes(id) &&
175 new Date(themeInfo.expiry) <= now
177 for (let [id] of expiredThemes) {
178 if (id == activeThemeID) {
179 let shouldRetain = true;
182 let addon = await lazy.AddonManager.getAddonByID(id);
184 // Only add the id to the retain themes pref if it is
185 // also a built-in themes (and don't if it was migrated
186 // xpi files installed in the user profile).
187 shouldRetain = addon.isBuiltinColorwayTheme;
191 `Failed to retrieve active theme AddonWrapper ${id}`,
197 this._retainLimitedTimeTheme(id);
201 let addon = await lazy.AddonManager.getAddonByID(id);
202 // Only uninstall the expired colorways theme if they are not
203 // migrated builtins (because on migrated to xpi files
204 // installed in the user profile they are also removed
205 // from the retainedExpiredThemes pref).
206 if (addon?.isBuiltinColorwayTheme) {
207 await addon.uninstall();
210 console.error(`Failed to uninstall expired theme ${id}`, e);
217 * Set a pref to ensure that the user can continue to use a specified theme
218 * past its expiry date.
220 * The ID of the theme to retain.
222 _retainLimitedTimeTheme(id) {
223 if (!lazy.retainedThemes.includes(id)) {
224 lazy.retainedThemes.push(id);
225 Services.prefs.setStringPref(
227 JSON.stringify(lazy.retainedThemes)
233 * Removes from the retained expired theme list colorways themes that have been
234 * migrated from the one installed in the built-in XPIProvider location
235 * to an AMO hosted xpi installed in the user profile XPIProvider location.
237 * The ID of the theme to remove from the retained themes list.
240 unretainMigratedColorwayTheme(id) {
241 if (lazy.retainedThemes.includes(id)) {
242 const retainedThemes = lazy.retainedThemes.filter(
243 retainedThemeId => retainedThemeId !== id
245 Services.prefs.setStringPref(
247 JSON.stringify(retainedThemes)
253 * Colorway collections are usually divided into and presented as "groups".
254 * A group either contains closely related colorways, e.g. stemming from the
255 * same base color but with different intensities (soft, balanced, and bold),
256 * or if the current collection doesn't have intensities, each colorway is
257 * their own group. Group name localization is optional.
259 * The ID of the colorway add-on.
261 * Localized colorway group name. null if there's no such name, in which
262 * case the caller should fall back on getting a name from the add-on API.
264 getLocalizedColorwayGroupName(colorwayId) {
265 return this._getColorwayString(colorwayId, "groupName");
270 * The ID of the colorway add-on.
272 * L10nId for intensity value of the colorway with the provided id, null if
275 getColorwayIntensityL10nId(colorwayId) {
276 const result = ColorwayIntensityIdPostfixToL10nMap.find(
277 ([postfix, l10nId]) => colorwayId.endsWith(postfix)
279 return result ? result[1] : null;
284 * The ID of the colorway add-on.
286 * Localized description of the colorway with the provided id, null if
289 getLocalizedColorwayDescription(colorwayId) {
290 return this._getColorwayString(colorwayId, "description");
293 _getColorwayString(colorwayId, stringType) {
294 let l10nId = this.builtInThemeMap.get(colorwayId)?.l10nId?.[stringType];
297 [s] = ColorwayL10n.formatMessagesSync([
303 return s?.value || null;
307 export var BuiltInThemes = new _BuiltInThemes();