Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3067 from Mardak/gh3065-backport
Browse files Browse the repository at this point in the history
chore(bootstrap): Backport Preferences -> Services.prefs change from bug 1357517
  • Loading branch information
Dan Mosedale committed Aug 2, 2017
2 parents 2ff7220 + f30b605 commit 8d800d6
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions system-addon/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.importGlobalProperties(["fetch"]);

XPCOMUtils.defineLazyModuleGetter(this, "Preferences",
"resource://gre/modules/Preferences.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Services",
"resource://gre/modules/Services.jsm");

const ACTIVITY_STREAM_ENABLED_PREF = "browser.newtabpage.activity-stream.enabled";
const BROWSER_READY_NOTIFICATION = "sessionstore-windows-restored";
const PREF_CHANGED_TOPIC = "nsPref:changed";
const REASON_SHUTDOWN_ON_PREF_CHANGE = "PREF_OFF";
const REASON_STARTUP_ON_PREF_CHANGE = "PREF_ON";
const RESOURCE_BASE = "resource://activity-stream";
Expand Down Expand Up @@ -86,10 +85,9 @@ function uninit(reason) {
/**
* onPrefChanged - handler for changes to ACTIVITY_STREAM_ENABLED_PREF
*
* @param {bool} isEnabled Determines whether Activity Stream is enabled
*/
function onPrefChanged(isEnabled) {
if (isEnabled) {
function onPrefChanged() {
if (Services.prefs.getBoolPref(ACTIVITY_STREAM_ENABLED_PREF, false)) {
init(REASON_STARTUP_ON_PREF_CHANGE);
} else {
uninit(REASON_SHUTDOWN_ON_PREF_CHANGE);
Expand All @@ -103,10 +101,10 @@ function onBrowserReady() {
waitingForBrowserReady = false;

// Listen for changes to the pref that enables Activity Stream
Preferences.observe(ACTIVITY_STREAM_ENABLED_PREF, onPrefChanged);
Services.prefs.addObserver(ACTIVITY_STREAM_ENABLED_PREF, observe); // eslint-disable-line no-use-before-define

// Only initialize if the pref is true
if (Preferences.get(ACTIVITY_STREAM_ENABLED_PREF)) {
if (Services.prefs.getBoolPref(ACTIVITY_STREAM_ENABLED_PREF, false)) {
init(startupReason);
}
}
Expand All @@ -121,6 +119,11 @@ function observe(subject, topic, data) {
// Avoid running synchronously during this event that's used for timing
Services.tm.dispatchToMainThread(() => onBrowserReady());
break;
case PREF_CHANGED_TOPIC:
if (data === ACTIVITY_STREAM_ENABLED_PREF) {
onPrefChanged();
}
break;
}
}

Expand Down Expand Up @@ -154,7 +157,7 @@ this.shutdown = function shutdown(data, reason) {
Services.obs.removeObserver(observe, BROWSER_READY_NOTIFICATION);
} else {
// Stop listening to the pref that enables Activity Stream
Preferences.ignore(ACTIVITY_STREAM_ENABLED_PREF, onPrefChanged);
Services.prefs.removeObserver(ACTIVITY_STREAM_ENABLED_PREF, observe);
}

// Unload any add-on modules that might might have been imported
Expand Down

0 comments on commit 8d800d6

Please sign in to comment.