fx-compat: AddonManager.getAllAddons() now returns a promise

This commit is contained in:
Dan Stillman 2020-08-03 12:20:58 -04:00
parent 66a60eea64
commit d635fdda41
2 changed files with 113 additions and 121 deletions

View file

@ -373,118 +373,113 @@ Zotero.Prefs = new function(){
async function loadExtensionDefaults() { async function loadExtensionDefaults() {
var defaultBranch = Services.prefs.getDefaultBranch(""); var defaultBranch = Services.prefs.getDefaultBranch("");
return new Zotero.Promise(function (resolve) { Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/AddonManager.jsm");
// Lines are in format `pref("[key]", [val]);`, so define a function to be called that
// Lines are in format `pref("[key]", [val]);`, so define a function to be called that // sets the defaults
// sets the defaults function pref(key, value) {
function pref(key, value) { switch (typeof value) {
switch (typeof value) { case "boolean":
case "boolean": defaultBranch.setBoolPref(key, value);
defaultBranch.setBoolPref(key, value); break;
break; case "number":
case "number": defaultBranch.setIntPref(key, value);
defaultBranch.setIntPref(key, value); break;
break; case "string":
case "string": defaultBranch.setStringPref(key, value);
defaultBranch.setStringPref(key, value); break;
break; }
}
function readDefaults(contents) {
let re = /^\s*pref\s*\(\s*['"]([a-zA-Z0-9_\-.]+)['"]\s*,\s*["']?.*["']?\s*\)\s*;\s*$/;
let lines = contents.split(/\n/g).filter(line => re.test(line));
for (let line of lines) {
try {
eval(line);
}
catch (e) {
dump(e + "\n\n");
Components.utils.reportError(e);
} }
} }
}
function readDefaults(contents) {
let re = /^\s*pref\s*\(\s*['"]([a-zA-Z0-9_\-.]+)['"]\s*,\s*["']?.*["']?\s*\)\s*;\s*$/; let addons = await AddonManager.getAllAddons();
let lines = contents.split(/\n/g).filter(line => re.test(line)); var reusableStreamInstance = Cc['@mozilla.org/scriptableinputstream;1']
for (let line of lines) { .createInstance(Ci.nsIScriptableInputStream);
try {
eval(line); for (let addon of addons) {
} if (!addon.isActive) {
catch (e) { continue;
dump(e + "\n\n");
Components.utils.reportError(e);
}
}
} }
AddonManager.getAllAddons(async function(addons) { try {
var reusableStreamInstance = Cc['@mozilla.org/scriptableinputstream;1'] let path = OS.Path.fromFileURI(addon.getResourceURI().spec);
.createInstance(Ci.nsIScriptableInputStream);
for (let addon of addons) { // Hack to delete extensions.json for Mac users who first ran Zotero from the
if (!addon.isActive) { // disk image and ended up with invalid integration plugin paths in
continue; // extensions.json
} try {
if (Zotero.isMac && path.includes('AppTranslocation')) {
try { await OS.File.remove(
let path = OS.Path.fromFileURI(addon.getResourceURI().spec); OS.Path.join(Zotero.Profile.dir, 'extensions.json'),
{
// Hack to delete extensions.json for Mac users who first ran Zotero from the ignoreAbsent: true
// disk image and ended up with invalid integration plugin paths in
// extensions.json
try {
if (Zotero.isMac && path.includes('AppTranslocation')) {
await OS.File.remove(
OS.Path.join(Zotero.Profile.dir, 'extensions.json'),
{
ignoreAbsent: true
}
);
} }
);
}
}
catch (e) {
Zotero.logError(e);
}
// Directory
if ((await OS.File.stat(path)).isDir) {
let dir = OS.Path.join(path, 'defaults', 'preferences');
if (await OS.File.exists(dir)) {
await Zotero.File.iterateDirectory(dir, async function (entry) {
if (!entry.name.endsWith('.js')) return;
readDefaults(Zotero.File.getContents(entry.path));
});
}
}
// XPI
else {
let file = Zotero.File.pathToFile(path);
let zipReader = Components.classes["@mozilla.org/libjar/zip-reader;1"].
createInstance(Components.interfaces.nsIZipReader);
try {
try {
zipReader.open(file);
zipReader.test(null);
} }
catch (e) { catch (e) {
Zotero.logError(e); Zotero.logError(path + " is not a valid ZIP file");
continue;
} }
// Directory let entries = zipReader.findEntries('defaults/preferences/*.js');
if ((await OS.File.stat(path)).isDir) { while (entries.hasMore()) {
let dir = OS.Path.join(path, 'defaults', 'preferences'); let entryName = entries.getNext();
if (await OS.File.exists(dir)) { let entry = zipReader.getEntry(entryName);
await Zotero.File.iterateDirectory(dir, async function (entry) {
if (!entry.name.endsWith('.js')) return; if (!entry.isDirectory) {
readDefaults(Zotero.File.getContents(entry.path)); let inputStream = zipReader.getInputStream(entryName);
}); reusableStreamInstance.init(inputStream);
} readDefaults(reusableStreamInstance.read(entry.realSize));
}
// XPI
else {
let file = Zotero.File.pathToFile(path);
let zipReader = Components.classes["@mozilla.org/libjar/zip-reader;1"].
createInstance(Components.interfaces.nsIZipReader);
try {
try {
zipReader.open(file);
zipReader.test(null);
}
catch (e) {
Zotero.logError(path + " is not a valid ZIP file");
continue;
}
let entries = zipReader.findEntries('defaults/preferences/*.js');
while (entries.hasMore()) {
let entryName = entries.getNext();
let entry = zipReader.getEntry(entryName);
if (!entry.isDirectory) {
let inputStream = zipReader.getInputStream(entryName);
reusableStreamInstance.init(inputStream);
readDefaults(reusableStreamInstance.read(entry.realSize));
}
}
}
finally {
zipReader.close();
} }
} }
} }
catch (e) { finally {
Zotero.logError(e); zipReader.close();
} }
} }
}
resolve(); catch (e) {
}); Zotero.logError(e);
}); }
}
} }
this.getVirtualCollectionState = function (type) { this.getVirtualCollectionState = function (type) {

View file

@ -1309,33 +1309,30 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
/** /**
* @return {Promise<String[]>} - Promise for an array of extension names and versions * @return {Promise<String[]>} - Promise for an array of extension names and versions
*/ */
this.getInstalledExtensions = Zotero.Promise.method(function () { this.getInstalledExtensions = async function () {
var deferred = Zotero.Promise.defer(); var { AddonManager } = ChromeUtils.import("resource://gre/modules/AddonManager.jsm");
function onHaveInstalledAddons(installed) { var installed = await AddonManager.getAllAddons();
installed.sort(function(a, b) {
return ((a.appDisabled || a.userDisabled) ? 1 : 0) - installed.sort(function(a, b) {
((b.appDisabled || b.userDisabled) ? 1 : 0); return ((a.appDisabled || a.userDisabled) ? 1 : 0) -
}); ((b.appDisabled || b.userDisabled) ? 1 : 0);
var addons = []; });
for (let addon of installed) { var addons = [];
switch (addon.id) { for (let addon of installed) {
case "zotero@chnm.gmu.edu": switch (addon.id) {
case "{972ce4c6-7e08-4474-a285-3208198ce6fd}": // Default theme case "zotero@chnm.gmu.edu":
continue; case "{972ce4c6-7e08-4474-a285-3208198ce6fd}": // Default theme
} continue;
addons.push(addon.name + " (" + addon.version
+ (addon.type != 2 ? ", " + addon.type : "")
+ ((addon.appDisabled || addon.userDisabled) ? ", disabled" : "")
+ ")");
} }
deferred.resolve(addons);
addons.push(addon.name + " (" + addon.version
+ (addon.type != 2 ? ", " + addon.type : "")
+ ((addon.appDisabled || addon.userDisabled) ? ", disabled" : "")
+ ")");
} }
Components.utils.import("resource://gre/modules/AddonManager.jsm"); return addons;
AddonManager.getAllAddons(onHaveInstalledAddons); };
return deferred.promise;
});
this.getString = function (name, params, num) { this.getString = function (name, params, num) {
return Zotero.Intl.getString(...arguments); return Zotero.Intl.getString(...arguments);