Bug 1477708 - Make theme loading part of the Theme constructor in ext-theme.js. r=jaws
MozReview-Commit-ID: 2rZu7Iqtk7D
--- a/toolkit/components/extensions/parent/ext-theme.js
+++ b/toolkit/components/extensions/parent/ext-theme.js
@@ -31,38 +31,37 @@ let windowOverrides = new Map();
*/
class Theme {
/**
* Creates a theme instance.
*
* @param {string} extension Extension that created the theme.
* @param {Integer} windowId The windowId where the theme is applied.
*/
- constructor(extension, windowId) {
- // The base URI of the extension, used to resolve relative filepaths.
- this.baseURI = extension.baseURI;
- // Logger that will be used to show manifest warnings to the theme author.
- this.logger = extension.logger;
+ constructor({extension, details, windowId}) {
+ this.extension = extension;
+ this.details = details;
+ this.windowId = windowId;
- this.extension = extension;
- this.windowId = windowId;
this.lwtStyles = {
icons: {},
};
+
+ this.load();
}
/**
* Loads a theme by reading the properties from the extension's manifest.
* This method will override any currently applied theme.
*
* @param {Object} details Theme part of the manifest. Supported
* properties can be found in the schema under ThemeType.
*/
- load(details) {
- this.details = details;
+ load() {
+ const {details} = this;
if (details.colors) {
this.loadColors(details.colors);
}
if (details.images) {
this.loadImages(details.images);
}
@@ -169,82 +168,87 @@ class Theme {
}
/**
* Helper method for loading images found in the extension's manifest.
*
* @param {Object} images Dictionary mapping image properties to values.
*/
loadImages(images) {
+ const {baseURI} = this.extension;
+
for (let image of Object.keys(images)) {
let val = images[image];
if (!val) {
continue;
}
switch (image) {
case "additional_backgrounds": {
- let backgroundImages = val.map(img => this.baseURI.resolve(img));
+ let backgroundImages = val.map(img => baseURI.resolve(img));
this.lwtStyles.additionalBackgrounds = backgroundImages;
break;
}
case "headerURL":
case "theme_frame": {
- let resolvedURL = this.baseURI.resolve(val);
+ let resolvedURL = baseURI.resolve(val);
this.lwtStyles.headerURL = resolvedURL;
break;
}
}
}
}
/**
* Helper method for loading icons found in the extension's manifest.
*
* @param {Object} icons Dictionary mapping icon properties to extension URLs.
*/
loadIcons(icons) {
+ const {baseURI} = this.extension;
+
if (!Services.prefs.getBoolPref("extensions.webextensions.themes.icons.enabled")) {
// Return early if icons are disabled.
return;
}
for (let icon of Object.getOwnPropertyNames(icons)) {
let val = icons[icon];
// We also have to compare against the baseURI spec because
// `val` might have been resolved already. Resolving "" against
// the baseURI just produces that URI, so check for equality.
- if (!val || val == this.baseURI.spec || !ICONS.includes(icon)) {
+ if (!val || val == baseURI.spec || !ICONS.includes(icon)) {
continue;
}
let variableName = `--${icon}-icon`;
- let resolvedURL = this.baseURI.resolve(val);
+ let resolvedURL = baseURI.resolve(val);
this.lwtStyles.icons[variableName] = resolvedURL;
}
}
/**
* Helper method for preparing properties found in the extension's manifest.
* Properties are commonly used to specify more advanced behavior of colors,
* images or icons.
*
* @param {Object} properties Dictionary mapping properties to values.
*/
loadProperties(properties) {
let additionalBackgroundsCount = (this.lwtStyles.additionalBackgrounds &&
this.lwtStyles.additionalBackgrounds.length) || 0;
const assertValidAdditionalBackgrounds = (property, valueCount) => {
+ const {logger} = this.extension;
if (!additionalBackgroundsCount) {
- this.logger.warn(`The '${property}' property takes effect only when one ` +
+ logger.warn(`The '${property}' property takes effect only when one ` +
`or more additional background images are specified using the 'additional_backgrounds' property.`);
return false;
}
if (additionalBackgroundsCount !== valueCount) {
- this.logger.warn(`The amount of values specified for '${property}' ` +
+ logger.warn(`The amount of values specified for '${property}' ` +
`(${valueCount}) is not equal to the amount of additional background ` +
`images (${additionalBackgroundsCount}), which may lead to unexpected results.`);
}
return true;
};
for (let property of Object.getOwnPropertyNames(properties)) {
let val = properties[property];
@@ -306,18 +310,20 @@ class Theme {
}
}
this.theme = class extends ExtensionAPI {
onManifestEntry(entryName) {
let {extension} = this;
let {manifest} = extension;
- defaultTheme = new Theme(extension);
- defaultTheme.load(manifest.theme);
+ defaultTheme = new Theme({
+ extension,
+ details: manifest.theme,
+ });
}
onShutdown(reason) {
if (reason === "APP_SHUTDOWN") {
return;
}
let {extension} = this;
@@ -351,18 +357,21 @@ this.theme = class extends ExtensionAPI
update: (windowId, details) => {
if (windowId) {
const browserWindow = windowTracker.getWindow(windowId, context);
if (!browserWindow) {
return Promise.reject(`Invalid window ID: ${windowId}`);
}
}
- let theme = new Theme(extension, windowId);
- theme.load(details);
+ new Theme({
+ extension,
+ details,
+ windowId,
+ });
},
reset: (windowId) => {
if (windowId) {
const browserWindow = windowTracker.getWindow(windowId, context);
if (!browserWindow) {
return Promise.reject(`Invalid window ID: ${windowId}`);
}
}