1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 12:50:36 +01:00

Separated settings migration

For #166
This commit is contained in:
kkapsner 2018-09-14 16:29:30 +02:00
parent caa8db93c7
commit 9d5e8b71ed
4 changed files with 152 additions and 114 deletions

View File

@ -35,6 +35,21 @@ const require = function(){
}
throw new ReferenceError("Unable to get non relative module " + module + "!");
}
require.register = function(module, scope = {}){
if (!require.exists(module)){
const scopeName = getScopeName(module);
scope[scopeName] = scope;
return scope;
}
else {
require("./logging").error("Module", module, "already registered.");
}
};
require.exists = function(module){
return scope.hasOwnProperty(getScopeName(module));
};
var events = {};
require.on = function(module, callback){

View File

@ -565,93 +565,6 @@
});
}
const settingsMigration = {
validVersions: [undefined, 0.1, 0.2, 0.3, 0.4],
transitions: {
"": function(oldStorage){
return {
storageVersion: 0.2
};
},
0.1: function(oldStorage){
var newStorage = {
storageVersion: 0.2
};
if (oldStorage.hasOwnProperty("askOnlyOnce")){
newStorage.askOnlyOnce = oldStorage.askOnlyOnce? "individual": "no";
}
return newStorage;
},
0.2: function(oldStorage){
var newStorage = {
storageVersion: 0.3,
urlSettings: (
oldStorage.urlSettings &&
Array.isArray(oldStorage.urlSettings)
)? oldStorage.urlSettings: []
};
var urlSettings = {};
(oldStorage.blackList || "").split(",")
.map(function(url){return url.trim();})
.filter(function(url){return !!url;})
.forEach(function(url){
var entry = urlSettings[url];
if (!entry){
entry = {url, blockMode: "block"};
urlSettings[url] = entry;
newStorage.urlSettings.push(entry);
}
});
(oldStorage.whiteList || "").split(",")
.map(function(url){return url.trim();})
.filter(function(url){return !!url;})
.forEach(function(url){
var entry = urlSettings[url];
if (!entry){
entry = {url, blockMode: "allow"};
urlSettings[url] = entry;
newStorage.urlSettings.push(entry);
}
});
(oldStorage.ignoreList || "").split(",")
.map(function(url){return url.trim();})
.filter(function(url){return !!url;})
.forEach(function(url){
var entry = urlSettings[url];
if (!entry){
entry = {url, showNotifications: false};
urlSettings[url] = entry;
newStorage.urlSettings.push(entry);
}
else {
entry.showNotifications = false;
}
});
["whiteList", "blackList", "ignoreList"].forEach(function(list){
if (oldStorage.hasOwnProperty(list)){
newStorage[list] = "";
}
});
return newStorage;
},
0.3: function(oldStorage){
var newStorage = {
storageVersion: 0.4
};
if (oldStorage.hasOwnProperty("apiWhiteList")){
const protectedAPIFeatures = {};
Object.keys(oldStorage.apiWhiteList).forEach(function(key){
protectedAPIFeatures[key] = !oldStorage.apiWhiteList[key];
});
newStorage.protectedAPIFeatures = protectedAPIFeatures;
}
return newStorage;
},
}
};
logging.verbose("loading settings");
let initialized = false;
@ -662,33 +575,9 @@
}
initialized = true;
logging.message("settings loaded");
if (!storage.storageVersion){
logging.message("No storage version found. Initializing storage.");
browser.storage.local.remove(Object.keys(storage));
storage = settingsMigration.transitions[""]({});
browser.storage.local.set(storage);
}
else if (storage.storageVersion !== settings.storageVersion){
var toChange = {};
while (storage.storageVersion !== settings.storageVersion){
logging.message("Old storage found (",
storage.storageVersion, "expected", settings.storageVersion,
")");
if (settingsMigration.transitions[storage.storageVersion]){
var changes = settingsMigration.transitions[storage.storageVersion](storage);
Object.entries(changes).forEach(function(entry){
const [name, value] = entry;
toChange[name] = value;
storage[name] = value;
});
}
else {
logging.warning("Unable to migrate storage.");
break;
}
}
logging.notice("Changed settings:", toChange);
browser.storage.local.set(toChange);
if (require.exists("./settingsMigration")){
const settingsMigration = require("./settingsMigration");
settingsMigration.check(storage, {settings, logging, changeValue, urlContainer});
}
var delayedChange = [];
Object.entries(storage).forEach(function(entry){

133
lib/settingsMigration.js Normal file
View File

@ -0,0 +1,133 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
"use strict";
var scope;
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
window.scope.settingsMigration = {};
scope = window.scope.settingsMigration;
}
scope.validVersions = [undefined, 0.1, 0.2, 0.3, 0.4];
scope.transitions = {
"": function(oldStorage){
return {
storageVersion: 0.2
};
},
0.1: function(oldStorage){
var newStorage = {
storageVersion: 0.2
};
if (oldStorage.hasOwnProperty("askOnlyOnce")){
newStorage.askOnlyOnce = oldStorage.askOnlyOnce? "individual": "no";
}
return newStorage;
},
0.2: function(oldStorage){
var newStorage = {
storageVersion: 0.3,
urlSettings: (
oldStorage.urlSettings &&
Array.isArray(oldStorage.urlSettings)
)? oldStorage.urlSettings: []
};
var urlSettings = {};
(oldStorage.blackList || "").split(",")
.map(function(url){return url.trim();})
.filter(function(url){return !!url;})
.forEach(function(url){
var entry = urlSettings[url];
if (!entry){
entry = {url, blockMode: "block"};
urlSettings[url] = entry;
newStorage.urlSettings.push(entry);
}
});
(oldStorage.whiteList || "").split(",")
.map(function(url){return url.trim();})
.filter(function(url){return !!url;})
.forEach(function(url){
var entry = urlSettings[url];
if (!entry){
entry = {url, blockMode: "allow"};
urlSettings[url] = entry;
newStorage.urlSettings.push(entry);
}
});
(oldStorage.ignoreList || "").split(",")
.map(function(url){return url.trim();})
.filter(function(url){return !!url;})
.forEach(function(url){
var entry = urlSettings[url];
if (!entry){
entry = {url, showNotifications: false};
urlSettings[url] = entry;
newStorage.urlSettings.push(entry);
}
else {
entry.showNotifications = false;
}
});
["whiteList", "blackList", "ignoreList"].forEach(function(list){
if (oldStorage.hasOwnProperty(list)){
newStorage[list] = "";
}
});
return newStorage;
},
0.3: function(oldStorage){
var newStorage = {
storageVersion: 0.4
};
if (oldStorage.hasOwnProperty("apiWhiteList")){
const protectedAPIFeatures = {};
Object.keys(oldStorage.apiWhiteList).forEach(function(key){
protectedAPIFeatures[key] = !oldStorage.apiWhiteList[key];
});
newStorage.protectedAPIFeatures = protectedAPIFeatures;
}
return newStorage;
},
};
scope.check = function(storage, {settings, logging, changeValue, urlContainer}){
if (!storage.storageVersion){
logging.message("No storage version found. Initializing storage.");
browser.storage.local.remove(Object.keys(storage));
storage = scope.transitions[""]({});
browser.storage.local.set(storage);
}
else if (storage.storageVersion !== settings.storageVersion){
var toChange = {};
while (storage.storageVersion !== settings.storageVersion){
logging.message("Old storage found (",
storage.storageVersion, "expected", settings.storageVersion,
")");
if (scope.transitions[storage.storageVersion]){
var changes = scope.transitions[storage.storageVersion](storage);
Object.entries(changes).forEach(function(entry){
const [name, value] = entry;
toChange[name] = value;
storage[name] = value;
});
}
else {
logging.warning("Unable to migrate storage.");
break;
}
}
logging.notice("Changed settings:", toChange);
browser.storage.local.set(toChange);
}
};
}());

View File

@ -11,6 +11,7 @@
"scripts": [
"lib/require.js",
"lib/settingDefinitions.js",
"lib/settingsMigration.js",
"lib/settings.js",
"lib/logging.js",
"lib/lists.js",