Bug 1474163 - Make use of sharedData for content theme data. r=mconley

MozReview-Commit-ID: Etz8huX2YCt
This commit is contained in:
Tim Nguyen
2018-07-13 16:21:34 +01:00
parent b380c472b8
commit 2a7d072a6e
9 changed files with 98 additions and 139 deletions

View File

@@ -0,0 +1,71 @@
/* 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";
ChromeUtils.import("resource://gre/modules/Services.jsm");
var EXPORTED_SYMBOLS = ["LightweightThemeChildHelper"];
/**
* LightweightThemeChildHelper forwards theme data to in-content pages.
*/
var LightweightThemeChildHelper = {
listener: null,
whitelist: [],
/**
* Listen to theme updates for the current process
* @param {Array} whitelist The pages that can receive theme updates.
*/
listen(whitelist) {
if (!this.listener) {
// Clone the whitelist to avoid leaking the global the whitelist
// originates from.
this.whitelist = new Set([...whitelist]);
this.listener = ({ changedKeys }) => {
if (changedKeys.find(change => change.startsWith("theme/"))) {
this._updateProcess(changedKeys);
}
};
Services.cpmm.sharedData.addEventListener("change", this.listener);
}
},
/**
* Update the theme data for the whole process
* @param {Array} changedKeys The sharedData keys that were changed.
*/
_updateProcess(changedKeys) {
const windowEnumerator = Services.ww.getWindowEnumerator();
while (windowEnumerator.hasMoreElements()) {
const window = windowEnumerator.getNext().QueryInterface(Ci.nsIDOMWindow);
const tabChildGlobal = window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDocShell)
.sameTypeRootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIContentFrameMessageManager);
const {chromeOuterWindowID, content} = tabChildGlobal;
if (changedKeys.includes(`theme/${chromeOuterWindowID}`) &&
content && this.whitelist.has(content.document.documentURI)) {
this.update(chromeOuterWindowID, content);
}
}
},
/**
* Forward the theme data to the page.
* @param {Object} outerWindowID The outerWindowID the parent process window has.
* @param {Object} content The receiving global
*/
update(outerWindowID, content) {
const event = Cu.cloneInto({
detail: {
data: Services.cpmm.sharedData.get(`theme/${outerWindowID}`)
},
}, content);
content.dispatchEvent(new content.CustomEvent("LightweightTheme:Set",
event));
},
};

View File

@@ -1,82 +0,0 @@
/* 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";
/**
* LightweightThemeChildListener forwards theme updates from LightweightThemeConsumer to
* the whitelisted in-content pages
*/
class LightweightThemeChildListener {
constructor() {
/**
* The pages that will receive theme updates
*/
this.whitelist = new Set([
"about:home",
"about:newtab",
"about:welcome",
]);
/**
* The last theme data received from LightweightThemeConsumer
*/
this._lastData = null;
}
/**
* Handles theme updates from the parent process
* @param {Object} message from the parent process.
*/
receiveMessage({ name, data, target }) {
if (name == "LightweightTheme:Update") {
this._lastData = data;
this._update(data, target.content);
}
}
/**
* Handles events from the content scope.
* @param {Object} event The received event.
*/
handleEvent(event) {
const content = event.originalTarget.defaultView;
if (content != content.top) {
return;
}
if (event.type == "LightweightTheme:Support") {
this._update(this._lastData, content);
}
}
/**
* Checks if a given global is allowed to receive theme updates
* @param {Object} content The global to check against.
* @returns {boolean} Whether the global is allowed to receive updates.
*/
_isContentWhitelisted(content) {
return this.whitelist.has(content.document.documentURI);
}
/**
* Forward the theme data to the page.
* @param {Object} data The theme data to forward
* @param {Object} content The receiving global
*/
_update(data, content) {
if (this._isContentWhitelisted(content)) {
const event = Cu.cloneInto({
detail: {
type: "LightweightTheme:Update",
data,
},
}, content);
content.dispatchEvent(new content.CustomEvent("LightweightTheme:Set",
event));
}
}
}
var EXPORTED_SYMBOLS = ["LightweightThemeChildListener"];

View File

@@ -64,7 +64,7 @@ with Files("ExtensionsUI.jsm"):
with Files("LaterRun.jsm"):
BUG_COMPONENT = ("Firefox", "Tours")
with Files("LightweightThemeChildListener.jsm"):
with Files("LightweightThemeChildHelper.jsm"):
BUG_COMPONENT = ("WebExtensions", "Themes")
with Files("LightWeightThemeWebInstallListener.jsm"):
@@ -153,7 +153,7 @@ EXTRA_JS_MODULES += [
'FormValidationHandler.jsm',
'HomePage.jsm',
'LaterRun.jsm',
'LightweightThemeChildListener.jsm',
'LightweightThemeChildHelper.jsm',
'LightWeightThemeWebInstallListener.jsm',
'NetErrorContent.jsm',
'OpenInTabsUtils.jsm',