From 959e868126cd2ec64ffdd6e228acfd376c073b30 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 5 Jan 2023 01:36:33 -0500 Subject: [PATCH] Read default prefs from prefs.js in a plugin's root directory The file should follow the same `pref("extensions.foo.bar", "value");` syntax as files previously in defaults/preferences/, which should no longer be used in Zotero 7. (For an extension that works in both Zotero 6 and 7, it's OK to have a file in defaults/preferences for Zotero 6 and an identical prefs.js for Zotero 7.) Files in defaults/preferences/ aren't automatically loaded when a plugin is installed/enabled but are still loaded at app startup by Mozilla code for now. Plugins shouldn't count on that continuing to be the case in Zotero 7 and should switch to prefs.js. We'll add a way for bootstrapped plugins to manually trigger reading of a prefs.js file in Zotero 6. --- chrome/content/zotero/xpcom/plugins.js | 65 +++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/xpcom/plugins.js b/chrome/content/zotero/xpcom/plugins.js index 55ce2eae18..9b2c18033e 100644 --- a/chrome/content/zotero/xpcom/plugins.js +++ b/chrome/content/zotero/xpcom/plugins.js @@ -45,6 +45,7 @@ Zotero.Plugins = new function () { var { addons } = await AddonManager.getActiveAddons(["extension"]); for (let addon of addons) { + setDefaultPrefs(addon); await _callMethod(addon, 'startup', REASONS.APP_STARTUP); } @@ -193,8 +194,64 @@ Zotero.Plugins = new function () { var addon = await AddonManager.getAddonByID(id); return AddonManager.getPreferredIconURL(addon, idealSize, Services.appShell.hiddenDOMWindow); }; - - + + + function setDefaultPrefs(addon) { + var branch = Services.prefs.getDefaultBranch(""); + var obj = { + pref(pref, value) { + switch (typeof value) { + case 'boolean': + branch.setBoolPref(pref, value); + break; + case 'string': + branch.setStringPref(pref, value); + break; + case 'number': + branch.setIntPref(pref, value); + break; + default: + Zotero.logError(`Invalid type '${typeof(value)}' for pref '${pref}'`); + } + } + }; + try { + Services.scriptloader.loadSubScript( + addon.getResourceURI("prefs.js").spec, + obj + ); + } + catch (e) { + if (!e.toString().startsWith('Error opening input stream')) { + Zotero.logError(e); + } + } + } + + + function clearDefaultPrefs(addon) { + var branch = Services.prefs.getDefaultBranch(""); + var obj = { + pref(pref, value) { + if (!branch.prefHasUserValue(pref)) { + branch.deleteBranch(pref); + } + } + }; + try { + Services.scriptloader.loadSubScript( + addon.getResourceURI("prefs.js").spec, + obj + ); + } + catch (e) { + if (!e.toString().startsWith('Error opening input stream')) { + Zotero.logError(e); + } + } + } + + /** * Add an observer to be notified of lifecycle events on all plugins. * @@ -233,6 +290,7 @@ Zotero.Plugins = new function () { return; } Zotero.debug("Installed plugin " + addon.id); + setDefaultPrefs(addon); await _callMethod(addon, 'install'); await _callMethod(addon, 'startup', REASONS.ADDON_INSTALL); }, @@ -242,6 +300,7 @@ Zotero.Plugins = new function () { return; } Zotero.debug("Enabling plugin " + addon.id); + setDefaultPrefs(addon); await _callMethod(addon, 'startup', REASONS.ADDON_ENABLE); }, @@ -251,6 +310,7 @@ Zotero.Plugins = new function () { } Zotero.debug("Disabling plugin " + addon.id); await _callMethod(addon, 'shutdown', REASONS.ADDON_DISABLE); + clearDefaultPrefs(addon); }, async onUninstalling(addon) { @@ -261,6 +321,7 @@ Zotero.Plugins = new function () { Zotero.debug("Uninstalled plugin " + addon.id); await _callMethod(addon, 'shutdown', REASONS.ADDON_UNINSTALL); await _callMethod(addon, 'uninstall'); + clearDefaultPrefs(addon); }, }; };