Skip to content
Snippets Groups Projects
Verified Commit 27a60cde authored by Thomas Rientjes's avatar Thomas Rientjes
Browse files

Improve preference migration logic

parent 694fa25a
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,7 @@
*/
var webextension = require('sdk/webextension');
var preferences = require('sdk/simple-prefs').prefs;
var preferences = require('sdk/simple-prefs');
var self = require('sdk/self');
var tabs = require("sdk/tabs");
......@@ -51,42 +51,56 @@ var featurelessVersions = {
// Executed as soon as the add-on is loaded.
exports.main = function (options) {
// Initialize add-on state.
interceptor.register();
loadWatcher.register();
// Display the release notes if desired.
if (preferences.prefs.showReleaseNotes) {
if (options.loadReason === 'install' || (options.loadReason === 'upgrade' && !featurelessVersions[self.version])) {
if (preferences.prefs['sdk.baseURI']) {
tabs.open(preferences.prefs['sdk.baseURI'] + 'static/release-notes.html');
}
}
}
// Initialize the embedded WebExtension.
webextension.startup().then(({ browser }) => {
browser.runtime.onConnect.addListener(port => {
webextensionPort = port;
if (port.name === 'webextension') {
webextensionPort = port;
preferences.on('', function (preferenceName) {
if (preferenceName === 'amountInjected') {
return;
}
port.postMessage({
'subject': 'update-preferences',
'content': {
[preferenceName]: preferences.prefs[preferenceName]
}
});
});
port.postMessage({
'subject': 'migrate-preferences',
'content': {
'amountInjected': preferences['amountInjected'],
'blockMissing': preferences['blockMissing'],
'domainWhitelist': preferences['domainWhitelist'],
'showReleaseNotes': preferences['showReleaseNotes']
'amountInjected': preferences.prefs['amountInjected'],
'blockMissing': preferences.prefs['blockMissing'],
'domainWhitelist': preferences.prefs['domainWhitelist'],
'showReleaseNotes': preferences.prefs['showReleaseNotes']
}
});
}
});
});
// Initialize add-on state.
interceptor.register();
loadWatcher.register();
// Display the release notes if desired.
if (preferences.showReleaseNotes) {
if (options.loadReason === 'install' || (options.loadReason === 'upgrade' && !featurelessVersions[self.version])) {
if (preferences['sdk.baseURI']) {
tabs.open(preferences['sdk.baseURI'] + 'static/release-notes.html');
}
}
}
};
// Executed as soon as the add-on is unloaded.
......
......@@ -18,7 +18,9 @@
*/
var webextensionPort = {};
var amountInjected = 0;
var amountInjected = null;
var pendingCount = 0;
/**
* Initializations
......@@ -30,31 +32,25 @@ webextensionPort = browser.runtime.connect({name: 'webextension'});
* Event Handlers
*/
// browser.storage.local.remove('amountInjected');
webextensionPort.onMessage.addListener((message) => {
if (message.subject === 'migrate-preferences') {
browser.storage.local.get(function (items) {
for (let preference of Object.keys(message.content)) {
if (items.hasOwnProperty(preference)) {
// Covers storage API failures.
if (items === null) {
return;
}
if (preference === 'amountInjected') {
amountInjected = items.amountInjected;
}
for (let preference of Object.keys(message.content)) {
} else {
// Makes sure no existing preferences are overwritten.
if (!items.hasOwnProperty(preference)) {
browser.storage.local.set({
[preference]: message.content[preference]
});
if (preference === 'amountInjected') {
amountInjected = message.content[preference];
}
}
}
});
......@@ -62,22 +58,33 @@ webextensionPort.onMessage.addListener((message) => {
if (message.subject === 'register-injection') {
if (isNaN(amountInjected)) {
if (amountInjected !== null && !isNaN(amountInjected)) {
++amountInjected;
browser.storage.local.set({amountInjected});
}
++pendingCount;
chrome.storage.local.get('amountInjected', function (items) {
if (pendingCount > 1) {
return;
}
amountInjected = items.amountInjected;
chrome.storage.local.get({
chrome.storage.local.set({
'amountInjected': ++amountInjected
});
});
// The stored amount, or zero.
amountInjected: 0
} else {
}, function (items) {
chrome.storage.local.set({
'amountInjected': ++amountInjected
});
}
// Accounts for the fact that the storage API is asynchronous.
amountInjected = (items && items.amountInjected || 0) + pendingCount;
browser.storage.local.set({amountInjected});
});
}
if (message.subject === 'update-preferences') {
chrome.storage.local.set(message.content);
}
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment