1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* global windowTracker, EventManager, EventEmitter */
/* eslint-disable complexity */
ChromeUtils.defineESModuleGetters(this, {
LightweightThemeManager:
"resource://gre/modules/LightweightThemeManager.sys.mjs",
});
const onUpdatedEmitter = new EventEmitter();
// Represents an empty theme for convenience of use
const emptyTheme = {
details: { colors: null, images: null, properties: null },
};
let defaultTheme = emptyTheme;
// Map[windowId -> Theme instance]
let windowOverrides = new Map();
/**
* Class representing either a global theme affecting all windows or an override on a specific window.
* Any extension updating the theme with a new global theme will replace the singleton defaultTheme.
*/
class Theme {
/**
* Creates a theme instance.
*
* @param {object} options
* @param {string} options.extension Extension that created the theme.
* @param {Integer} options.windowId The windowId where the theme is applied.
* @param {object} options.details
* @param {object} options.darkDetails
* @param {object} options.experiment
* @param {object} options.startupData
*/
constructor({
extension,
details,
darkDetails,
windowId,
experiment,
startupData,
}) {
this.extension = extension;
this.details = details;
this.darkDetails = darkDetails;
this.windowId = windowId;
if (startupData && startupData.lwtData) {
Object.assign(this, startupData);
} else {
// TODO(ntim): clean this in bug 1550090
this.lwtStyles = {};
this.lwtDarkStyles = null;
if (darkDetails) {
this.lwtDarkStyles = {};
}
if (experiment) {
if (extension.canUseThemeExperiment()) {
this.lwtStyles.experimental = {
colors: {},
images: {},
properties: {},
};
if (this.lwtDarkStyles) {
this.lwtDarkStyles.experimental = {
colors: {},
images: {},
properties: {},
};
}
const { baseURI } = this.extension;
if (experiment.stylesheet) {
experiment.stylesheet = baseURI.resolve(experiment.stylesheet);
}
this.experiment = experiment;
} else {
const { logger } = this.extension;
logger.warn("This extension is not allowed to run theme experiments");
return;
}
}
}
this.load();
}
/**
* Loads a theme by reading the properties from the extension's manifest.
* This method will override any currently applied theme.
*/
load() {
if (!this.lwtData) {
this.loadDetails(this.details, this.lwtStyles);
if (this.darkDetails) {
this.loadDetails(this.darkDetails, this.lwtDarkStyles);
}
this.lwtData = {
theme: this.lwtStyles,
darkTheme: this.lwtDarkStyles,
};
if (this.experiment) {
this.lwtData.experiment = this.experiment;
}
this.extension.startupData = {
lwtData: this.lwtData,
lwtStyles: this.lwtStyles,
lwtDarkStyles: this.lwtDarkStyles,
experiment: this.experiment,
};
this.extension.saveStartupData();
}
if (this.windowId) {
this.lwtData.window = windowTracker.getWindow(
this.windowId
).docShell.outerWindowID;
windowOverrides.set(this.windowId, this);
} else {
windowOverrides.clear();
defaultTheme = this;
LightweightThemeManager.fallbackThemeData = this.lwtData;
}
onUpdatedEmitter.emit("theme-updated", this.details, this.windowId);
Services.obs.notifyObservers(
this.lwtData,
"lightweight-theme-styling-update"
);
}
/**
* @param {object} details Details
* @param {object} styles Styles object in which to store the colors.
*/
loadDetails(details, styles) {
if (details.colors) {
this.loadColors(details.colors, styles);
}
if (details.images) {
this.loadImages(details.images, styles);
}
if (details.properties) {
this.loadProperties(details.properties, styles);
}
this.loadMetadata(this.extension, styles);
}
/**
* Helper method for loading colors found in the extension's manifest.
*
* @param {object} colors Dictionary mapping color properties to values.
* @param {object} styles Styles object in which to store the colors.
*/
loadColors(colors, styles) {
for (let color of Object.keys(colors)) {
let val = colors[color];
if (!val) {
continue;
}
let cssColor = val;
if (Array.isArray(val)) {
cssColor =
"rgb" + (val.length > 3 ? "a" : "") + "(" + val.join(",") + ")";
}
switch (color) {
case "frame":
styles.accentcolor = cssColor;
break;
case "frame_inactive":
styles.accentcolorInactive = cssColor;
break;
case "tab_background_text":
styles.textcolor = cssColor;
break;
case "toolbar":
styles.toolbarColor = cssColor;
break;
case "toolbar_text":
case "bookmark_text":
styles.toolbar_text = cssColor;
break;
case "icons":
styles.icon_color = cssColor;
break;
case "icons_attention":
styles.icon_attention_color = cssColor;
break;
case "tab_background_separator":
case "tab_loading":
case "tab_text":
case "tab_line":
case "tab_selected":
case "toolbar_field":
case "toolbar_field_text":
case "toolbar_field_border":
case "toolbar_field_focus":
case "toolbar_field_text_focus":
case "toolbar_field_border_focus":
case "toolbar_top_separator":
case "toolbar_bottom_separator":
case "toolbar_vertical_separator":
case "button_background_hover":
case "button_background_active":
case "popup":
case "popup_text":
case "popup_border":
case "popup_highlight":
case "popup_highlight_text":
case "ntp_background":
case "ntp_card_background":
case "ntp_text":
case "sidebar":
case "sidebar_border":
case "sidebar_text":
case "sidebar_highlight":
case "sidebar_highlight_text":
case "toolbar_field_highlight":
case "toolbar_field_highlight_text":
styles[color] = cssColor;
break;
default:
if (
this.experiment &&
this.experiment.colors &&
color in this.experiment.colors
) {
styles.experimental.colors[color] = cssColor;
} else {
const { logger } = this.extension;
logger.warn(`Unrecognized theme property found: colors.${color}`);
}
break;
}
}
}
/**
* Helper method for loading images found in the extension's manifest.
*
* @param {object} images Dictionary mapping image properties to values.
* @param {object} styles Styles object in which to store the colors.
*/
loadImages(images, styles) {
const { baseURI, logger } = 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 => baseURI.resolve(img));
styles.additionalBackgrounds = backgroundImages;
break;
}
case "theme_frame": {
let resolvedURL = baseURI.resolve(val);
styles.headerURL = resolvedURL;
break;
}
default: {
if (
this.experiment &&
this.experiment.images &&
image in this.experiment.images
) {
styles.experimental.images[image] = baseURI.resolve(val);
} else {
logger.warn(`Unrecognized theme property found: images.${image}`);
}
break;
}
}
}
}
/**
* 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.
* @param {object} styles Styles object in which to store the colors.
*/
loadProperties(properties, styles) {
let additionalBackgroundsCount =
(styles.additionalBackgrounds && styles.additionalBackgrounds.length) ||
0;
const assertValidAdditionalBackgrounds = (property, valueCount) => {
const { logger } = this.extension;
if (!additionalBackgroundsCount) {
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) {
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];
if (!val) {
continue;
}
switch (property) {
case "additional_backgrounds_alignment": {
if (!assertValidAdditionalBackgrounds(property, val.length)) {
break;
}
styles.backgroundsAlignment = val.join(",");
break;
}
case "additional_backgrounds_tiling": {
if (!assertValidAdditionalBackgrounds(property, val.length)) {
break;
}
let tiling = [];
for (let i = 0, l = styles.additionalBackgrounds.length; i < l; ++i) {
tiling.push(val[i] || "no-repeat");
}
styles.backgroundsTiling = tiling.join(",");
break;
}
case "color_scheme":
case "content_color_scheme": {
styles[property] = val;
break;
}
default: {
if (
this.experiment &&
this.experiment.properties &&
property in this.experiment.properties
) {
styles.experimental.properties[property] = val;
} else {
const { logger } = this.extension;
logger.warn(
`Unrecognized theme property found: properties.${property}`
);
}
break;
}
}
}
}
/**
* Helper method for loading extension metadata required by downstream
* consumers.
*
* @param {object} extension Extension object.
* @param {object} styles Styles object in which to store the colors.
*/
loadMetadata(extension, styles) {
styles.id = extension.id;
styles.version = extension.version;
}
static unload(windowId) {
let lwtData = {
theme: null,
};
if (windowId) {
lwtData.window = windowTracker.getWindow(windowId).docShell.outerWindowID;
windowOverrides.delete(windowId);
} else {
windowOverrides.clear();
defaultTheme = emptyTheme;
LightweightThemeManager.fallbackThemeData = null;
}
onUpdatedEmitter.emit("theme-updated", {}, windowId);
Services.obs.notifyObservers(lwtData, "lightweight-theme-styling-update");
}
}
this.theme = class extends ExtensionAPIPersistent {
PERSISTENT_EVENTS = {
onUpdated({ fire, context }) {
let callback = (event, theme, windowId) => {
if (windowId) {
// Force access validation for incognito mode by getting the window.
if (windowTracker.getWindow(windowId, context, false)) {
fire.async({ theme, windowId });
}
} else {
fire.async({ theme });
}
};
onUpdatedEmitter.on("theme-updated", callback);
return {
unregister() {
onUpdatedEmitter.off("theme-updated", callback);
},
convert(_fire, _context) {
fire = _fire;
context = _context;
},
};
},
};
onManifestEntry() {
let { extension } = this;
let { manifest } = extension;
defaultTheme = new Theme({
extension,
details: manifest.theme,
darkDetails: manifest.dark_theme,
experiment: manifest.theme_experiment,
startupData: extension.startupData,
});
}
onShutdown(isAppShutdown) {
if (isAppShutdown) {
return;
}
let { extension } = this;
for (let [windowId, theme] of windowOverrides) {
if (theme.extension === extension) {
Theme.unload(windowId);
}
}
if (defaultTheme.extension === extension) {
Theme.unload();
}
}
getAPI(context) {
let { extension } = context;
return {
theme: {
getCurrent: windowId => {
// Take last focused window when no ID is supplied.
if (!windowId) {
windowId = windowTracker.getId(windowTracker.topWindow);
}
// Force access validation for incognito mode by getting the window.
if (!windowTracker.getWindow(windowId, context)) {
return Promise.reject(`Invalid window ID: ${windowId}`);
}
if (windowOverrides.has(windowId)) {
return Promise.resolve(windowOverrides.get(windowId).details);
}
return Promise.resolve(defaultTheme.details);
},
update: (windowId, details) => {
if (windowId) {
const browserWindow = windowTracker.getWindow(windowId, context);
if (!browserWindow) {
return Promise.reject(`Invalid window ID: ${windowId}`);
}
}
new Theme({
extension,
details,
windowId,
experiment: this.extension.manifest.theme_experiment,
});
},
reset: windowId => {
if (windowId) {
const browserWindow = windowTracker.getWindow(windowId, context);
if (!browserWindow) {
return Promise.reject(`Invalid window ID: ${windowId}`);
}
let theme = windowOverrides.get(windowId) || defaultTheme;
if (theme.extension !== extension) {
return;
}
} else if (defaultTheme.extension !== extension) {
return;
}
Theme.unload(windowId);
},
onUpdated: new EventManager({
context,
module: "theme",
event: "onUpdated",
extensionApi: this,
}).api(),
},
};
}
};
|