aboutsummaryrefslogtreecommitdiff
path: root/chrome/js
diff options
context:
space:
mode:
authorpapush2021-08-29 16:02:25 +0200
committerpapush2021-08-29 16:02:25 +0200
commit6011221d0246ba587b412a8bd2047b53a067a542 (patch)
tree5b8c8374e3c7860a423ddc0e92a5b6a12005eeb6 /chrome/js
parent3a55cc0eded7e63eef252ad6e21afd1b1899d2e1 (diff)
clean up the preference migration code
Diffstat (limited to 'chrome/js')
-rw-r--r--chrome/js/redirector.js80
1 files changed, 41 insertions, 39 deletions
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) {