From 6011221d0246ba587b412a8bd2047b53a067a542 Mon Sep 17 00:00:00 2001 From: papush Date: Sun, 29 Aug 2021 16:02:25 +0200 Subject: clean up the preference migration code --- chrome/js/redirector.js | 80 +++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 39 deletions(-) (limited to 'chrome/js') diff --git a/chrome/js/redirector.js b/chrome/js/redirector.js index b62c3da..11905a9 100644 --- a/chrome/js/redirector.js +++ b/chrome/js/redirector.js @@ -105,47 +105,49 @@ Redirector = { return file; }, - handleUpgrades : function() { - AddonManager.getAddonByID("url-rewriter@papush", (addon) => { - currentVersion = '' + addon.version; - this._list = []; - - if (this._prefs.version == currentVersion) { - this.debug("No version change, prefs migration skipped"); - return; + // Return a list of Redirects read from a string. + // + // The string is expected to be in the format redirector used in + // older versions where it was stored as a user pref. + parseRedirectsString : function(data) { + let list = []; + for each (redirectString in data.split(':::')) { + if (!redirectString || !redirectString.split) { + this.debug('Invalid old redirect: ' + redirectString); + continue; } - this.debug("Migrating prefs"); - //Here update checks are handled - - try { - var branch = PrefService.getBranch("extensions.redirector."); - var data = branch.getCharPref("redirects"); - } catch(e) { - this._prefs.version = currentVersion; - return; + let parts = redirectString.split(',,,'); + if (parts.length < 5) { + throw Error("Invalid serialized redirect, too few fields: " + redirectString); } - var arr; - this._list = []; - if (data != '') { - for each (redirectString in data.split(':::')) { - if (!redirectString || !redirectString.split) { - continue; - this.debug('Invalid old redirect: ' + redirectString); - } - var parts = redirectString.split(',,,'); - if (parts.length < 5) { - throw Error("Invalid serialized redirect, too few fields: " + redirectString); - } - var redirect = new Redirect(); - redirect._init.apply(redirect, parts); - this._list.push(redirect); - } - this.save(); - this._list = []; //Let the real loading start this properly - } - branch.deleteBranch('redirects'); - this._prefs.version = currentVersion; - }); + let redirect = new Redirect(); + redirect._init.apply(redirect, parts); + list.push(redirect); + } + return list; + }, + + handleUpgrades : function() { + let branch, data; + try { + branch = PrefService.getBranch("extensions.redirector."); + data = branch.getCharPref("redirects"); + } catch (e) { + this.debug("No legacy redirect string preference found"); + return; + } + this.debug("Legacy redirect string preference found, migrating"); + oldRedirects = this.parseRedirectsString(data); + if (!oldRedirects) { + throw Error("Failed to migrate redirects, this shouldn't happen, send hatemail to papush@deepzero.net"); + } + let redirectsFile = this._getRedirectsFile(); + if (redirectsFile.exists()) { + this.importRedirects(redirectsFile); + } + this._list = this._list.concat(oldRedirects); + this.save(); + branch.deleteBranch('redirects'); }, importRedirects : function(file) { -- cgit v1.2.3-70-g09d2