Browse Source

P4TB-201: Convert from bootstrapped legacy extension to WebExtension Experiment.

1.1.101
Jorg Knobloch 3 years ago
parent
commit
7480252199
7 changed files with 189 additions and 118 deletions
  1. +9
    -0
      chrome/background.js
  2. +0
    -111
      chrome/bootstrap.js
  3. +0
    -1
      chrome/chrome.manifest
  4. +139
    -0
      chrome/experiments.js
  5. +1
    -0
      chrome/makeXPI.bat
  6. +20
    -6
      chrome/manifest.json
  7. +20
    -0
      chrome/schema.json

+ 9
- 0
chrome/background.js View File

@ -0,0 +1,9 @@
/* globals browser */
var init = async () => {
var l10n = {};
// TODO: Fill in l10n strings.
browser.pEp4Tb.addWindowListener(JSON.stringify(l10n));
};
init();

+ 0
- 111
chrome/bootstrap.js View File

@ -1,111 +0,0 @@
/* exported install, uninstall, startup, shutdown */
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
var {ExtensionSupport} = ChromeUtils.import("resource:///modules/ExtensionSupport.jsm");
function loadStylesheets(styleSheets) {
// Load stylesheets
const styleSheetService = Cc["@mozilla.org/content/style-sheet-service;1"]
.getService(Ci.nsIStyleSheetService);
for (let i = 0, len = styleSheets.length; i < len; i++) {
const styleSheetURI = Services.io.newURI(styleSheets[i]);
styleSheetService.loadAndRegisterSheet(styleSheetURI, styleSheetService.AUTHOR_SHEET);
}
}
function install() {
console.debug("bootstrap.js: Install");
}
function setPrefs(prefBranch) {
const defaults = {
Bool: {
storeAllSecurely: true,
warnUnencrypted: false,
protectSubjects: true,
pEpSync: false,
passiveMode: false,
forceUpdate: false,
},
String: {
keyFingerprint: "",
keyLocation: "",
},
};
iterate(["Bool", "String"], defaults);
// eslint-disable-next-line no-shadow
function iterate(types, defaults) {
types.map((type) => {
const prefs = defaults[type];
if (prefs) {
Object.entries(prefs).map(([prefName, prefValue]) => {
prefBranch[`set${type}Pref`](prefName, prefValue);
});
} else {
console.log("no type", type, "in defaults", defaults);
}
});
}
}
function startup(data, reason) {
console.debug("bootstrap.js: Startup");
const styleSheets = ["chrome://pEp4Tb/content/resources/stylesheets/pEp.css"];
loadStylesheets(styleSheets);
// Define default preferences.
setPrefs(Services.prefs.getDefaultBranch("extensions.pEp."));
ExtensionSupport.registerWindowListener(data.id, {
chromeURLs: ["chrome://messenger/content/messenger.xul",
"chrome://messenger/content/messageWindow.xul",
"chrome://messenger/content/messengercompose/messengercompose.xul"],
onLoadWindow: paint,
onUnloadWindow: unpaint,
});
}
function shutdown(data, reason) {
console.debug("bootstrap.js: Shutdown");
ExtensionSupport.unregisterWindowListener(data.id);
}
function uninstall() {
console.debug("bootstrap.js: Uninstall");
}
function paint(win) {
console.debug("bootstrap.js: paint()", win.location.href);
let script;
switch (win.location.href) {
case "chrome://messenger/content/messenger.xul":
case "chrome://messenger/content/messageWindow.xul":
script = "chrome://pEp4Tb/content/pepmsghdrview.js";
break;
case "chrome://messenger/content/messengercompose/messengercompose.xul":
script = "chrome://pEp4Tb/content/pepmessengercompose.js";
break;
default:
return;
}
Services.scriptloader.loadSubScript(script, win);
}
function unpaint(win) {
console.debug("bootstrap.js: unpaint()", win.location.href);
switch (win.location.href) {
case "chrome://messenger/content/messenger.xul":
case "chrome://messenger/content/messageWindow.xul":
win.pEpHdrView.destroy();
break;
case "chrome://messenger/content/messengercompose/messengercompose.xul":
win.pEpComposer.destroy();
break;
default:
}
}

+ 0
- 1
chrome/chrome.manifest View File

@ -1 +0,0 @@
content pEp4Tb content/

+ 139
- 0
chrome/experiments.js View File

@ -0,0 +1,139 @@
/* global ExtensionCommon */
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
var {ExtensionSupport} = ChromeUtils.import("resource:///modules/ExtensionSupport.jsm");
const EXTENSION_NAME = "pEp4Tb@pEp.security";
function loadStylesheets(styleSheets) {
// Load stylesheets
const styleSheetService = Cc["@mozilla.org/content/style-sheet-service;1"]
.getService(Ci.nsIStyleSheetService);
for (let i = 0, len = styleSheets.length; i < len; i++) {
const styleSheetURI = Services.io.newURI(styleSheets[i]);
styleSheetService.loadAndRegisterSheet(styleSheetURI, styleSheetService.AUTHOR_SHEET);
}
}
function setPrefs(prefBranch) {
const defaults = {
Bool: {
storeAllSecurely: true,
warnUnencrypted: false,
protectSubjects: true,
pEpSync: false,
passiveMode: false,
forceUpdate: false,
},
String: {
keyFingerprint: "",
keyLocation: "",
},
};
iterate(["Bool", "String"], defaults);
// eslint-disable-next-line no-shadow
function iterate(types, defaults) {
types.map((type) => {
const prefs = defaults[type];
if (prefs) {
Object.entries(prefs).map(([prefName, prefValue]) => {
prefBranch[`set${type}Pref`](prefName, prefValue);
});
} else {
console.log("no type", type, "in defaults", defaults);
}
});
}
}
// Implements the functions defined in the experiments section of schema.json.
// eslint-disable-next-line vars-on-top
var pEp4Tb = class extends ExtensionCommon.ExtensionAPI {
// Idea from https://searchfox.org/mozilla-central/rev/72e3388f74458d369af4f6cdbaeaacb719523b8c/browser/extensions/formautofill/api.js#95
// Investigation by Mark Banner.
onStartup() {
const aomStartup = Cc["@mozilla.org/addons/addon-manager-startup;1"]
.getService(Ci.amIAddonManagerStartup);
const manifestURI = Services.io.newURI("manifest.json", null, this.extension.rootURI);
this.chromeHandle = aomStartup.registerChrome(manifestURI, [["content", "pEp4Tb", "content/"]]);
}
onShutdown(isAppShutdown) {
if (isAppShutdown) return;
this.chromeHandle.destruct();
this.chromeHandle = null;
}
getAPI(context) {
return {
pEp4Tb: {
addWindowListener(l10n) {
console.debug("experiments.js: Startup");
const styleSheets = ["chrome://pEp4Tb/content/resources/stylesheets/pEp.css"];
loadStylesheets(styleSheets);
// Define default preferences.
setPrefs(Services.prefs.getDefaultBranch("extensions.pEp."));
ExtensionSupport.registerWindowListener(EXTENSION_NAME, {
chromeURLs: ["chrome://messenger/content/messenger.xul",
"chrome://messenger/content/messageWindow.xul",
"chrome://messenger/content/messengercompose/messengercompose.xul",
"chrome://messenger/content/messenger.xhtml",
"chrome://messenger/content/messageWindow.xhtml",
"chrome://messenger/content/messengercompose/messengercompose.xhtml"],
onLoadWindow: paint,
onUnloadWindow: unpaint,
});
},
},
};
}
close() {
console.debug("experiments.js: close");
ExtensionSupport.unregisterWindowListener(EXTENSION_NAME);
}
};
function paint(win) {
console.debug("experiments.js: paint()", win.location.href);
let script;
switch (win.location.href) {
case "chrome://messenger/content/messenger.xul":
case "chrome://messenger/content/messageWindow.xul":
case "chrome://messenger/content/messenger.xhtml":
case "chrome://messenger/content/messageWindow.xhtml":
script = "chrome://pEp4Tb/content/pepmsghdrview.js";
break;
case "chrome://messenger/content/messengercompose/messengercompose.xul":
case "chrome://messenger/content/messengercompose/messengercompose.xhtml":
script = "chrome://pEp4Tb/content/pepmessengercompose.js";
break;
default:
return;
}
Services.scriptloader.loadSubScript(script, win);
}
function unpaint(win) {
console.debug("experiments.js: unpaint()", win.location.href);
switch (win.location.href) {
case "chrome://messenger/content/messenger.xul":
case "chrome://messenger/content/messageWindow.xul":
case "chrome://messenger/content/messenger.xhtml":
case "chrome://messenger/content/messageWindow.xhtml":
win.pEpHdrView.destroy();
break;
case "chrome://messenger/content/messengercompose/messengercompose.xul":
case "chrome://messenger/content/messengercompose/messengercompose.xhtml":
win.pEpComposer.destroy();
break;
default:
}
}

+ 1
- 0
chrome/makeXPI.bat View File

@ -3,4 +3,5 @@ zip -r pEp4Tb@pEp.security.xpi . -x makeXPI.bat
REM Jörg's personal use:
copy pEp4Tb@pEp.security.xpi C:\Users\jorgk\AppData\Roaming\Thunderbird\Profiles\qciyaj3h.off42-peptest.ch\extensions\pEp4Tb@pEp.security.xpi
copy pEp4Tb@pEp.security.xpi "C:\Users\jorgk\AppData\Roaming\Thunderbird\Profiles\lhqs21u5.off42-peptest.ch for 75\extensions\pEp4Tb@pEp.security.xpi"
copy pEp4Tb@pEp.security.xpi P:\MAIL-THUNDERBIRD\extensions\pEp4Tb@pEp.security.xpi

+ 20
- 6
chrome/manifest.json View File

@ -9,8 +9,7 @@
"applications": {
"gecko": {
"id": "pEp4Tb@pEp.security",
"strict_min_version": "68.0a1",
"strict_max_version": "68.*"
"strict_min_version": "68.0a1"
}
},
@ -27,10 +26,25 @@
},
"legacy": {
"type": "bootstrap",
"options": {
"page": "chrome://pEp4Tb/content/options.xhtml"
"experiment_apis": {
"pEp4Tb": {
"schema": "schema.json",
"parent": {
"scopes": [
"addon_parent"
],
"paths": [
["pEp4Tb"]
],
"script": "experiments.js",
"events": ["startup"]
}
}
},
"background": {
"scripts": [
"background.js"
]
}
}

+ 20
- 0
chrome/schema.json View File

@ -0,0 +1,20 @@
[
{
"namespace": "pEp4Tb",
"functions": [
{
"name": "addWindowListener",
"type": "function",
"description": "Adds a listener to all opening 3pane, stand-alone or compose windows",
"async": false,
"parameters": [
{
"name": "l10n",
"type": "string",
"description": "Stringified L10N object containing the strings"
}
]
}
]
}
]

Loading…
Cancel
Save