diff --git a/lib/main.js b/lib/main.js index f1153784b7aac7fbd03934cb0365cf49c00a7f4f..234053d9ffc8830a70886de5d1ad0659f8da0e30 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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. diff --git a/webextension/background.js b/webextension/background.js index ce7f07371646c3de38742b7de699508f9a46b689..023e5874b66ef17ec3cbfe8376842e88497fa069 100644 --- a/webextension/background.js +++ b/webextension/background.js @@ -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); } });