Source code

Revision control

Other Tools

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 file,
3
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
"use strict";
6
7
var EXPORTED_SYMBOLS = ["LightweightThemeChild"];
8
9
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
10
11
/**
12
* LightweightThemeChild forwards theme data to in-content pages.
13
* It is both instantiated by the traditional Actor mechanism,
14
* and also manually within the sidebar JS global (which has no message manager).
15
* The manual instantiation is necessary due to Bug 1596852.
16
*/
17
class LightweightThemeChild extends JSWindowActorChild {
18
constructor() {
19
super();
20
Services.cpmm.sharedData.addEventListener("change", this);
21
}
22
23
didDestroy() {
24
Services.cpmm.sharedData.removeEventListener("change", this);
25
}
26
27
_getChromeOuterWindowID() {
28
if (this.docShell.messageManager) {
29
return this.docShell.messageManager.chromeOuterWindowID;
30
}
31
// We don't have a message manager, so presumable we're running in a sidebar
32
// in the parent process.
33
return this.contentWindow.top.windowUtils.outerWindowID;
34
}
35
36
/**
37
* Handles "change" events on the child sharedData map, and notifies
38
* our content page if its theme data was among the changed keys.
39
*/
40
handleEvent(event) {
41
switch (event.type) {
42
// Make sure to update the theme data on first page show.
43
case "pageshow":
44
this.update();
45
break;
46
47
case "change":
48
if (
49
event.changedKeys.includes(`theme/${this._getChromeOuterWindowID()}`)
50
) {
51
this.update();
52
}
53
break;
54
}
55
}
56
57
/**
58
* Forward the theme data to the page.
59
*/
60
update() {
61
const event = Cu.cloneInto(
62
{
63
detail: {
64
data: Services.cpmm.sharedData.get(
65
`theme/${this._getChromeOuterWindowID()}`
66
),
67
},
68
},
69
this.contentWindow
70
);
71
this.contentWindow.dispatchEvent(
72
new this.contentWindow.CustomEvent("LightweightTheme:Set", event)
73
);
74
}
75
}