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 { ActorChild } = ChromeUtils.import(
11
);
12
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
13
14
/**
15
* LightweightThemeChild forwards theme data to in-content pages.
16
* It is both instantiated by the traditional Actor mechanism,
17
* and also manually within the sidebar JS global (which has no message manager)
18
*/
19
class LightweightThemeChild extends ActorChild {
20
constructor(dispatcher) {
21
if (dispatcher.mm) {
22
// This is being instantiated by the Actor mechanism.
23
super(dispatcher);
24
} else {
25
// Manually instantiated by the sidebar.
26
let fakeDispatcher = {
27
mm: dispatcher,
28
window: dispatcher.content,
29
addEventListener: dispatcher.content.addEventListener,
30
};
31
super(fakeDispatcher);
32
}
33
34
this.init();
35
}
36
37
/**
38
* Initializes the actor for the current page, sending it any existing
39
* theme data, and adding shared data change listeners so it can
40
* notify the page of future updates.
41
*
42
* This is called when the actor is constructed, and any time
43
* ActorManagerChild receives a pageshow event for the page we're
44
* attached to.
45
*/
46
init() {
47
Services.cpmm.sharedData.addEventListener("change", this);
48
this.update(this.mm.chromeOuterWindowID, this.content);
49
}
50
51
/**
52
* Cleans up any global listeners registered by the actor.
53
*
54
* This is called by ActorManagerChild any time it receives a pagehide
55
* event for the page we're attached to.
56
*/
57
cleanup() {
58
Services.cpmm.sharedData.removeEventListener("change", this);
59
}
60
61
/**
62
* Handles "change" events on the child sharedData map, and notifies
63
* our content page if its theme data was among the changed keys.
64
*/
65
handleEvent(event) {
66
if (event.type === "change") {
67
if (event.changedKeys.includes(`theme/${this.mm.chromeOuterWindowID}`)) {
68
this.update(this.mm.chromeOuterWindowID, this.content);
69
}
70
}
71
}
72
73
/**
74
* Forward the theme data to the page.
75
* @param {Object} outerWindowID The outerWindowID the parent process window has.
76
* @param {Object} content The receiving global
77
*/
78
update(outerWindowID, content) {
79
const event = Cu.cloneInto(
80
{
81
detail: {
82
data: Services.cpmm.sharedData.get(`theme/${outerWindowID}`),
83
},
84
},
85
content
86
);
87
content.dispatchEvent(
88
new content.CustomEvent("LightweightTheme:Set", event)
89
);
90
}
91
}