gecko-dev/browser/extensions/activity-stream/bootstrap.js
k88hudson aa6c7a6981 Bug 1368775 - Add search suggestions, pref configuration and new telemetry to Activity Stream r=ursula
MozReview-Commit-ID: 6ZZxDp04oPX

--HG--
extra : rebase_source : 8f58895a8fc1b2999426da02918c8638bd789203
2017-05-30 14:06:48 -04:00

163 lines
5.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/. */
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.importGlobalProperties(["fetch"]);
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
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 = "browser-ui-startup-complete";
const REASON_SHUTDOWN_ON_PREF_CHANGE = "PREF_OFF";
const REASON_STARTUP_ON_PREF_CHANGE = "PREF_ON";
const RESOURCE_BASE = "resource://activity-stream";
const ACTIVITY_STREAM_OPTIONS = {newTabURL: "about:newtab"};
let activityStream;
let modulesToUnload = new Set();
let startupData;
let startupReason;
let waitingForBrowserReady = true;
// Lazily load ActivityStream then find related modules to unload
XPCOMUtils.defineLazyModuleGetter(this, "ActivityStream",
"resource://activity-stream/lib/ActivityStream.jsm", null, null, () => {
// Helper to fetch a resource directory listing and call back with each item
const processListing = async(uri, cb) => (await (await fetch(uri)).text())
.split("\n").slice(2).forEach(line => cb(line.split(" ").slice(1)));
// Look for modules one level deeper than the top resource URI
processListing(RESOURCE_BASE, ([directory, , , type]) => {
if (type === "DIRECTORY") {
// Look into this directory for .jsm files
const subDir = `${RESOURCE_BASE}/${directory}`;
processListing(subDir, ([name]) => {
if (name && name.search(/\.jsm$/) !== -1) {
modulesToUnload.add(`${subDir}/${name}`);
}
});
}
});
});
/**
* 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);
}
}
/**
* 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);
}
}
/**
* onBrowserReady - Continues startup of the add-on after browser is ready.
*/
function onBrowserReady() {
waitingForBrowserReady = false;
// 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(startupReason);
}
}
/**
* observe - nsIObserver callback to handle various browser notifications.
*/
function observe(subject, topic, data) {
switch (topic) {
case BROWSER_READY_NOTIFICATION:
Services.obs.removeObserver(observe, BROWSER_READY_NOTIFICATION);
onBrowserReady();
break;
}
}
// 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;
startupReason = reason;
// Only start Activity Stream up when the browser UI is ready
if (Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup).startingUp) {
Services.obs.addObserver(observe, BROWSER_READY_NOTIFICATION);
} else {
// Handle manual install or automatic install after manual uninstall
onBrowserReady();
}
};
this.shutdown = function shutdown(data, reason) {
// Uninitialize Activity Stream
startupData = null;
startupReason = null;
uninit(reason);
// Stop waiting for browser to be ready
if (waitingForBrowserReady) {
Services.obs.removeObserver(observe, BROWSER_READY_NOTIFICATION);
} else {
// Stop listening to the pref that enables Activity Stream
Preferences.ignore(ACTIVITY_STREAM_ENABLED_PREF, onPrefChanged);
}
// Unload any add-on modules that might might have been imported
modulesToUnload.forEach(Cu.unload);
};
this.uninstall = function uninstall(data, reason) {
if (activityStream) {
activityStream.uninstall(reason);
activityStream = null;
}
};