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

Implement preference migration logic

parent 682570cd
No related branches found
Tags v1.2.0-rc
No related merge requests found
......@@ -21,6 +21,8 @@ var { Class } = require('sdk/core/heritage');
var { Unknown } = require('sdk/platform/xpcom');
var { Cc, Ci, Cr } = require('chrome');
var main = require('./main');
/**
* Gets and sets add-on specific preferences.
* @var {object} simplePreferences
......@@ -116,6 +118,7 @@ var Interceptor = new Class({
//noinspection JSUnresolvedVariable
preferences.amountInjected++;
main.broadcastInjection();
},
/**
......
......@@ -17,15 +17,23 @@
* Imports
*/
var Interceptor = require('./interceptor');
var LoadWatcher = require('./load-watcher');
var webextension = require('sdk/webextension');
var preferences = require('sdk/simple-prefs').prefs;
var self = require('sdk/self');
var tabs = require("sdk/tabs");
var Interceptor = require('./interceptor');
var LoadWatcher = require('./load-watcher');
/**
* Main
* Variables
*/
var webextensionPort = null;
/**
* Initializations
*/
var interceptor = new Interceptor();
......@@ -36,13 +44,40 @@ var featurelessVersions = {
'1.3.7': true
};
/**
* Main
*/
// Executed as soon as the add-on is loaded.
exports.main = function (options) {
// Initialize the embedded WebExtension.
webextension.startup().then(({ browser }) => {
browser.runtime.onConnect.addListener(port => {
webextensionPort = port;
if (port.name === 'webextension') {
port.postMessage({
'subject': 'migrate-preferences',
'content': {
'amountInjected': preferences['amountInjected'],
'blockMissing': preferences['blockMissing'],
'domainWhitelist': preferences['domainWhitelist'],
'showReleaseNotes': preferences['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])) {
......@@ -60,3 +95,14 @@ exports.onUnload = function () {
// Clean up add-on state.
interceptor.unregister();
};
// Sends injection updates to the WebExtension.
exports.broadcastInjection = function () {
if (typeof webextensionPort === 'object') {
webextensionPort.postMessage({
'subject': 'register-injection'
});
}
};
......@@ -8,6 +8,7 @@
"homepage": "https://addons.mozilla.org/firefox/addon/decentraleyes",
"name": "decentraleyes",
"id": "jid1-BoFifL9Vbdl2zQ@jetpack",
"hasEmbeddedWebExtension": true,
"permissions": {
"multiprocess": true
},
......
/**
* Embedded WebExtension - Background Script
* Belongs to Decentraleyes.
*
* @author Thomas Rientjes
* @since 2017-08-18
* @license MPL 2.0
*
* 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/.
*/
'use strict';
/**
* Variables
*/
var webextensionPort = {};
var amountInjected = 0;
/**
* Initializations
*/
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)) {
if (preference === 'amountInjected') {
amountInjected = items.amountInjected;
}
} else {
browser.storage.local.set({
[preference]: message.content[preference]
});
if (preference === 'amountInjected') {
amountInjected = message.content[preference];
}
}
}
});
}
if (message.subject === 'register-injection') {
if (isNaN(amountInjected)) {
chrome.storage.local.get('amountInjected', function (items) {
amountInjected = items.amountInjected;
chrome.storage.local.set({
'amountInjected': ++amountInjected
});
});
} else {
chrome.storage.local.set({
'amountInjected': ++amountInjected
});
}
console.log('*** INCREMENTED');
console.log(amountInjected);
}
});
{
"manifest_version": 2,
"name": "Decentraleyes Module",
"version": "1.3.9",
"author": "Thomas Rientjes",
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"storage"
]
}
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