gecko-dev/browser/extensions/activity-stream/bootstrap.js
k88hudson b61a06ef8e Bug 1344319 - Create Activity Stream browser extension. r=rhelmer
MozReview-Commit-ID: EUthIoHKlOm

--HG--
extra : rebase_source : c2f81b795b79eae4b2dca6526f5000181d3fa2c5
2017-03-09 14:56:28 -05:00

94 lines
3 KiB
JavaScript

/* 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/. */
/* globals Components, XPCOMUtils, Preferences, ActivityStream */
"use strict";
const {utils: Cu} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Preferences.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ActivityStream",
"resource://activity-stream/lib/ActivityStream.jsm");
const ACTIVITY_STREAM_ENABLED_PREF = "browser.newtabpage.activity-stream.enabled";
const REASON_STARTUP_ON_PREF_CHANGE = "PREF_ON";
const REASON_SHUTDOWN_ON_PREF_CHANGE = "PREF_OFF";
const ACTIVITY_STREAM_OPTIONS = {newTabURL: "about:newtab"};
let activityStream;
let startupData;
/**
* init - Initializes an instance of ActivityStream. This could be called by
* the startup() function exposed by bootstrap.js, or it could be called
* when ACTIVITY_STREAM_ENABLED_PREF is changed from false to true.
*
* @param {string} reason - Reason for initialization. Could be install, upgrade, or PREF_ON
*/
function init(reason) {
// Don't re-initialize
if (activityStream && activityStream.initialized) {
return;
}
const options = Object.assign({}, startupData || {}, ACTIVITY_STREAM_OPTIONS);
activityStream = new ActivityStream(options);
activityStream.init(reason);
}
/**
* uninit - Uninitializes the activityStream instance, if it exsits.This could be
* called by the shutdown() function exposed by bootstrap.js, or it could
* be called when ACTIVITY_STREAM_ENABLED_PREF is changed from true to false.
*
* @param {type} reason Reason for uninitialization. Could be uninstall, upgrade, or PREF_OFF
*/
function uninit(reason) {
if (activityStream) {
activityStream.uninit(reason);
activityStream = null;
}
}
/**
* onPrefChanged - handler for changes to ACTIVITY_STREAM_ENABLED_PREF
*
* @param {bool} isEnabled Determines whether Activity Stream is enabled
*/
function onPrefChanged(isEnabled) {
if (isEnabled) {
init(REASON_STARTUP_ON_PREF_CHANGE);
} else {
uninit(REASON_SHUTDOWN_ON_PREF_CHANGE);
}
}
// The functions below are required by bootstrap.js
this.install = function install(data, reason) {};
this.startup = function startup(data, reason) {
// Cache startup data which contains stuff like the version number, etc.
// so we can use it when we init
startupData = data;
// Listen for changes to the pref that enables Activity Stream
Preferences.observe(ACTIVITY_STREAM_ENABLED_PREF, onPrefChanged);
// Only initialize if the pref is true
if (Preferences.get(ACTIVITY_STREAM_ENABLED_PREF)) {
init(reason);
}
};
this.shutdown = function shutdown(data, reason) {
// Uninitialize Activity Stream
startupData = null;
uninit(reason);
// Stop listening to the pref that enables Activity Stream
Preferences.ignore(ACTIVITY_STREAM_ENABLED_PREF, onPrefChanged);
};
this.uninstall = function uninstall(data, reason) {};