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

Commit

Permalink
Fix #2332, fix #2333: toggle webextension on pref change
Browse files Browse the repository at this point in the history
Other changes:

* Fix #2370, unset deviceId pref set by old addon.

* Update Promise rejection / Error handling to match behavior
  documented in the addons-related Gecko code.
  • Loading branch information
ianb authored and jaredhirsch committed Mar 18, 2017
1 parent 0411d69 commit b28d687
Showing 1 changed file with 98 additions and 18 deletions.
116 changes: 98 additions & 18 deletions addon/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,118 @@
/* globals Components, AddonManager */
/* eslint-disable no-unused-vars */
/* globals AddonManager, Components, Services */

const OLD_ADDON_PREF_NAME = "extensions.jid1-NeEaf3sAHdKHPA@jetpack.deviceIdInfo";
const OLD_ADDON_ID = "jid1-NeEaf3sAHdKHPA@jetpack";
const ADDON_ID = "pageshot@mozilla.org";
const TELEMETRY_PREF = "toolkit.telemetry.enabled";
const PREF_BRANCH = "extensions.pageshot.";
const USER_DISABLE_PREF = "extensions.pageshot.disabled";
const SYSTEM_DISABLE_PREF = "extensions.pageshot.system-disabled";

const prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
Components.utils.import("resource://gre/modules/AddonManager.jsm");
const { interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/Console.jsm");
Cu.import("resource://gre/modules/Services.jsm");
const { EmbeddedExtensionManager } = Cu.import("resource://gre/modules/LegacyExtensionsUtils.jsm");

function startup(data, reason) {
data.webExtension.startup().then((api) => {
const {browser} = api;
browser.runtime.onMessage.addListener(handleMessage);
const prefs = Services.prefs;
const prefObserver = {
register: function() {
prefs.addObserver(PREF_BRANCH, this, false);
},

unregister: function() {
prefs.removeObserver(PREF_BRANCH, this);
},

observe: function(aSubject, aTopic, aData) {
// aSubject is the nsIPrefBranch we're observing (after appropriate QI)
// aData is the name of the pref that's been changed (relative to aSubject)
if (aData == USER_DISABLE_PREF || aData == SYSTEM_DISABLE_PREF) {
handleStartup();
}
}
};

function startup(data, reason) { // eslint-disable-line no-unused-vars
prefObserver.register();
handleStartup();
}

function shutdown(data, reason) { // eslint-disable-line no-unused-vars
prefObserver.unregister();
}

function install(data, reason) {} // eslint-disable-line no-unused-vars

function uninstall(data, reason) {} // eslint-disable-line no-unused-vars

function getBoolPref(pref) {
return prefs.getPrefType(pref) && prefs.getBoolPref(pref);
}

function shouldDisable() {
return getBoolPref(USER_DISABLE_PREF) || getBoolPref(SYSTEM_DISABLE_PREF);
}

function handleStartup() {
AddonManager.getAddonByID(ADDON_ID).then((addon) => {
if (addon === null) {
console.error("Unable to start WebExtension: wrapper addon not found");
// TODO: Should we send this error to Sentry? #2420
return;
}

const webExtension = EmbeddedExtensionManager.getEmbeddedExtensionFor({
id: ADDON_ID,
resourceURI: addon.getResourceURI().QueryInterface(Ci.nsIFileURL)
});

if (!shouldDisable() && !webExtension.started) {
start(webExtension);
} else if (shouldDisable()) {
stop(webExtension);
}
});
}

function start(webExtension) {
webExtension.startup().then((api) => {
api.browser.runtime.onMessage.addListener(handleMessage);
}).catch((err) => {
// The startup() promise will be rejected if the webExtension was
// already started (a harmless error), or if initializing the
// WebExtension failed and threw (an important error).
console.error(err);
if (err.message !== "This embedded extension has already been started") {
// TODO: Should we send these errors to Sentry? #2420
}
});
}

function shutdown(data, reason) {}
function install(data, reason) {}
function uninstall(data, reason) {}
function stop(webExtension) {
webExtension.shutdown().then(() => {
EmbeddedExtensionManager.untrackEmbeddedExtension(webExtension);
});
}

function handleMessage(msg, sender, sendReply) {
if (msg && msg.funcName === "getTelemetryPref") {
let enableTelemetry = prefs.getPrefType('toolkit.telemetry.enabled') && prefs.getBoolPref("toolkit.telemetry.enabled");
if (!msg) {
return;
}

if (msg.funcName === "getTelemetryPref") {
let enableTelemetry = getBoolPref(TELEMETRY_PREF);
sendReply({type: "success", value: enableTelemetry});
} else if (msg && msg.funcName === "getOldDeviceInfo") {
} else if (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") {
} else if (msg.funcName === "removeOldAddon") {
AddonManager.getAddonByID(OLD_ADDON_ID, (addon) => {
// FIXME: remove OLD_ADDON_PREF_NAME, see #2370
prefs.clearUserPref(OLD_ADDON_PREF_NAME);
if (addon) {
addon.uninstall();
}
sendReply({type: "success", value: !! addon});
sendReply({type: "success", value: !!addon});
});
}
}

0 comments on commit b28d687

Please sign in to comment.