Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Fix #2260, migrate data from old add-on
Browse files Browse the repository at this point in the history
  • Loading branch information
ianb committed Mar 13, 2017
1 parent 2868ccc commit 64213c0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
17 changes: 16 additions & 1 deletion addon/bootstrap.js
@@ -1,8 +1,12 @@
/* globals Components */
/* globals Components, AddonManager */
/* eslint-disable no-unused-vars */

const OLD_ADDON_PREF_NAME = "extensions.jid1-NeEaf3sAHdKHPA@jetpack.deviceIdInfo";
const OLD_ADDON_ID = "jid1-NeEaf3sAHdKHPA@jetpack";

const prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
Components.utils.import("resource://gre/modules/AddonManager.jsm");

function startup(data, reason) {
data.webExtension.startup().then((api) => {
Expand All @@ -19,5 +23,16 @@ function handleMessage(msg, sender, sendReply) {
if (msg && msg.funcName === "getTelemetryPref") {
let enableTelemetry = prefs.getPrefType('toolkit.telemetry.enabled') && prefs.getBoolPref("toolkit.telemetry.enabled");
sendReply({type: "success", value: enableTelemetry});
} else if (msg && msg.funcName === "getOldDeviceInfo") {
let oldDeviceInfo = prefs.prefHasUserValue(OLD_ADDON_PREF_NAME) && prefs.getCharPref(OLD_ADDON_PREF_NAME);
sendReply({type: "success", value: oldDeviceInfo || null});
} else if (msg && msg.funcName === "removeOldAddon") {
AddonManager.getAddonByID(OLD_ADDON_ID, (addon) => {
// FIXME: remove OLD_ADDON_PREF_NAME, see #2370
if (addon) {
addon.uninstall();
}
sendReply({type: "success", value: !! addon});
});
}
}
21 changes: 21 additions & 0 deletions addon/webextension/background/auth.js
Expand Up @@ -150,5 +150,26 @@ window.auth = (function () {
return registrationInfo.registered;
};

exports.setDeviceInfoFromOldAddon = function (newDeviceInfo) {
if (! (newDeviceInfo.deviceId && newDeviceInfo.secret)) {
throw new Error("Bad deviceInfo");
}
if (registrationInfo.deviceId === newDeviceInfo.deviceId &&
registrationInfo.secret === newDeviceInfo.secret) {
// Probably we already imported the information
return Promise.resolve(false);
}
let newInfo = {
deviceId: newDeviceInfo.deviceId,
secret: newDeviceInfo.secret,
deviceInfo: JSON.stringify(deviceInfo()),
registered: true
};
initialized = false;
return browser.storage.local.set({registrationInfo: newInfo}).then(() => {
return true;
});
};

return exports;
})();
17 changes: 16 additions & 1 deletion addon/webextension/background/main.js
@@ -1,5 +1,5 @@
/* globals browser, console, XMLHttpRequest, Image, document, setTimeout, navigator */
/* globals loadSelector, analytics, communication, catcher, makeUuid */
/* globals loadSelector, analytics, communication, catcher, makeUuid, auth */
window.main = (function () {
let exports = {};

Expand Down Expand Up @@ -82,5 +82,20 @@ window.main = (function () {
}
});

catcher.watchPromise(browser.runtime.sendMessage({funcName: "getOldDeviceInfo"}).then((deviceInfo) => {
deviceInfo = deviceInfo.value;
if (! deviceInfo) {
return;
}
deviceInfo = JSON.parse(deviceInfo);
if (deviceInfo && typeof deviceInfo == "object") {
return auth.setDeviceInfoFromOldAddon(deviceInfo).then((updated) => {
if (updated) {
return browser.runtime.sendMessage({funcName: "removeOldAddon"});
}
});
}
}));

return exports;
})();

0 comments on commit 64213c0

Please sign in to comment.