Mirror of roytam1's UXP fork just in case Moonchild and Tobin decide to go after him
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

7880 lines
270 KiB

/* 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 Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
this.EXPORTED_SYMBOLS = ["XPIProvider"];
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/AddonManager.jsm");
Components.utils.import("resource://gre/modules/Preferences.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AddonRepository",
"resource://gre/modules/addons/AddonRepository.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ChromeManifestParser",
"resource://gre/modules/ChromeManifestParser.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeManager",
"resource://gre/modules/LightweightThemeManager.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
"resource://gre/modules/FileUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ZipUtils",
"resource://gre/modules/ZipUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PermissionsUtils",
"resource://gre/modules/PermissionsUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/Promise.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS",
"resource://gre/modules/osfile.jsm");
#ifdef MOZ_DEVTOOLS
XPCOMUtils.defineLazyModuleGetter(this, "BrowserToolboxProcess",
"resource://devtools/client/framework/ToolboxProcess.jsm");
#endif
XPCOMUtils.defineLazyModuleGetter(this, "ConsoleAPI",
"resource://gre/modules/Console.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "Blocklist",
"@mozilla.org/extensions/blocklist;1",
Ci.nsIBlocklistService);
XPCOMUtils.defineLazyServiceGetter(this,
"ChromeRegistry",
"@mozilla.org/chrome/chrome-registry;1",
"nsIChromeRegistry");
XPCOMUtils.defineLazyServiceGetter(this,
"ResProtocolHandler",
"@mozilla.org/network/protocol;1?name=resource",
"nsIResProtocolHandler");
XPCOMUtils.defineLazyServiceGetter(this,
"AddonPathService",
"@mozilla.org/addon-path-service;1",
"amIAddonPathService");
const nsIFile = Components.Constructor("@mozilla.org/file/local;1", "nsIFile",
"initWithPath");
const PREF_DB_SCHEMA = "extensions.databaseSchema";
const PREF_INSTALL_CACHE = "extensions.installCache";
const PREF_XPI_STATE = "extensions.xpiState";
const PREF_BOOTSTRAP_ADDONS = "extensions.bootstrappedAddons";
const PREF_PENDING_OPERATIONS = "extensions.pendingOperations";
const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
const PREF_SELECTED_LOCALE = "general.useragent.locale";
const PREF_EM_DSS_ENABLED = "extensions.dss.enabled";
const PREF_DSS_SWITCHPENDING = "extensions.dss.switchPending";
const PREF_DSS_SKIN_TO_SELECT = "extensions.lastSelectedSkin";
const PREF_GENERAL_SKINS_SELECTEDSKIN = "general.skins.selectedSkin";
const PREF_EM_UPDATE_URL = "extensions.update.url";
const PREF_EM_UPDATE_BACKGROUND_URL = "extensions.update.background.url";
const PREF_EM_ENABLED_ADDONS = "extensions.enabledAddons";
const PREF_EM_EXTENSION_FORMAT = "extensions.";
const PREF_EM_ENABLED_SCOPES = "extensions.enabledScopes";
const PREF_EM_AUTO_DISABLED_SCOPES = "extensions.autoDisableScopes";
const PREF_EM_SHOW_MISMATCH_UI = "extensions.showMismatchUI";
const PREF_XPI_ENABLED = "xpinstall.enabled";
const PREF_XPI_WHITELIST_REQUIRED = "xpinstall.whitelist.required";
const PREF_XPI_DIRECT_WHITELISTED = "xpinstall.whitelist.directRequest";
const PREF_XPI_FILE_WHITELISTED = "xpinstall.whitelist.fileRequest";
const PREF_XPI_PERMISSIONS_BRANCH = "xpinstall.";
const PREF_XPI_UNPACK = "extensions.alwaysUnpack";
const PREF_INSTALL_REQUIREBUILTINCERTS = "extensions.install.requireBuiltInCerts";
const PREF_INSTALL_REQUIRESECUREORIGIN = "extensions.install.requireSecureOrigin";
const PREF_INSTALL_DISTRO_ADDONS = "extensions.installDistroAddons";
const PREF_BRANCH_INSTALLED_ADDON = "extensions.installedDistroAddon.";
const PREF_SHOWN_SELECTION_UI = "extensions.shownSelectionUI";
const PREF_INTERPOSITION_ENABLED = "extensions.interposition.enabled";
const PREF_EM_MIN_COMPAT_APP_VERSION = "extensions.minCompatibleAppVersion";
const PREF_EM_MIN_COMPAT_PLATFORM_VERSION = "extensions.minCompatiblePlatformVersion";
const URI_EXTENSION_SELECT_DIALOG = "chrome://mozapps/content/extensions/selectAddons.xul";
const URI_EXTENSION_UPDATE_DIALOG = "chrome://mozapps/content/extensions/update.xul";
const URI_EXTENSION_STRINGS = "chrome://mozapps/locale/extensions/extensions.properties";
const STRING_TYPE_NAME = "type.%ID%.name";
const DIR_EXTENSIONS = "extensions";
const DIR_STAGE = "staged";
const DIR_XPI_STAGE = "staged-xpis";
const DIR_TRASH = "trash";
const FILE_DATABASE = "extensions.json";
const FILE_OLD_CACHE = "extensions.cache";
const FILE_INSTALL_MANIFEST = "install.rdf";
#ifndef MOZ_JETPACK
const FILE_JETPACK_MANIFEST_1 = "harness-options.json";
const FILE_JETPACK_MANIFEST_2 = "package.json";
#endif
const FILE_WEBEXT_MANIFEST = "manifest.json";
const FILE_XPI_ADDONS_LIST = "extensions.ini";
const KEY_PROFILEDIR = "ProfD";
const KEY_APPDIR = "XCurProcD";
const KEY_TEMPDIR = "TmpD";
const KEY_APP_DISTRIBUTION = "XREAppDist";
const KEY_APP_PROFILE = "app-profile";
const KEY_APP_GLOBAL = "app-global";
const KEY_APP_SYSTEM_LOCAL = "app-system-local";
const KEY_APP_SYSTEM_SHARE = "app-system-share";
const KEY_APP_SYSTEM_USER = "app-system-user";
const NOTIFICATION_FLUSH_PERMISSIONS = "flush-pending-permissions";
const XPI_PERMISSION = "install";
const RDFURI_INSTALL_MANIFEST_ROOT = "urn:mozilla:install-manifest";
const PREFIX_NS_EM = "http://www.mozilla.org/2004/em-rdf#";
const TOOLKIT_ID = "toolkit@mozilla.org";
#ifdef MOZ_PHOENIX_EXTENSIONS
const FIREFOX_ID = "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}"
const FIREFOX_APPCOMPATVERSION = "56.9"
#endif
// The value for this is in Makefile.in
#expand const DB_SCHEMA = __MOZ_EXTENSIONS_DB_SCHEMA__;
XPCOMUtils.defineConstant(this, "DB_SCHEMA", DB_SCHEMA);
#ifdef MOZ_DEVTOOLS
const NOTIFICATION_TOOLBOXPROCESS_LOADED = "ToolboxProcessLoaded";
#endif
// Properties that exist in the install manifest
const PROP_METADATA = ["id", "version", "type", "internalName", "updateURL",
"updateKey", "optionsURL", "optionsType", "aboutURL",
"iconURL", "icon64URL"];
const PROP_LOCALE_SINGLE = ["name", "description", "creator", "homepageURL"];
const PROP_LOCALE_MULTI = ["developers", "translators", "contributors"];
const PROP_TARGETAPP = ["id", "minVersion", "maxVersion"];
// Properties that should be migrated where possible from an old database. These
// shouldn't include properties that can be read directly from install.rdf files
// or calculated
const DB_MIGRATE_METADATA= ["installDate", "userDisabled", "softDisabled",
"sourceURI", "applyBackgroundUpdates",
"releaseNotesURI", "foreignInstall", "syncGUID"];
// Properties to cache and reload when an addon installation is pending
const PENDING_INSTALL_METADATA =
["syncGUID", "targetApplications", "userDisabled", "softDisabled",
"existingAddonID", "sourceURI", "releaseNotesURI", "installDate",
"updateDate", "applyBackgroundUpdates", "compatibilityOverrides"];
// Note: When adding/changing/removing items here, remember to change the
// DB schema version to ensure changes are picked up ASAP.
const STATIC_BLOCKLIST_PATTERNS = [
{ creator: "Mozilla Corp.",
level: Blocklist.STATE_BLOCKED,
blockID: "i162" },
{ creator: "Mozilla.org",
level: Blocklist.STATE_BLOCKED,
blockID: "i162" }
];
const BOOTSTRAP_REASONS = {
APP_STARTUP : 1,
APP_SHUTDOWN : 2,
ADDON_ENABLE : 3,
ADDON_DISABLE : 4,
ADDON_INSTALL : 5,
ADDON_UNINSTALL : 6,
ADDON_UPGRADE : 7,
ADDON_DOWNGRADE : 8
};
// Map new string type identifiers to old style nsIUpdateItem types
const TYPES = {
extension: 2,
theme: 4,
locale: 8,
multipackage: 32,
dictionary: 64,
experiment: 128,
};
const RESTARTLESS_TYPES = new Set([
"dictionary",
"experiment",
"locale",
]);
// Keep track of where we are in startup for telemetry
// event happened during XPIDatabase.startup()
const XPI_STARTING = "XPIStarting";
// event happened after startup() but before the final-ui-startup event
const XPI_BEFORE_UI_STARTUP = "BeforeFinalUIStartup";
// event happened after final-ui-startup
const XPI_AFTER_UI_STARTUP = "AfterFinalUIStartup";
const COMPATIBLE_BY_DEFAULT_TYPES = {
extension: true,
dictionary: true
};
const MSG_JAR_FLUSH = "AddonJarFlush";
const MSG_MESSAGE_MANAGER_CACHES_FLUSH = "AddonMessageManagerCachesFlush";
var gGlobalScope = this;
/**
* Valid IDs fit this pattern.
*/
var gIDTest = /^(\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}|[a-z0-9-\._]*\@[a-z0-9-\._]+)$/i;
Cu.import("resource://gre/modules/Log.jsm");
const LOGGER_ID = "addons.xpi";
// Create a new logger for use by all objects in this Addons XPI Provider module
// (Requires AddonManager.jsm)
var logger = Log.repository.getLogger(LOGGER_ID);
const LAZY_OBJECTS = ["XPIDatabase"];
var gLazyObjectsLoaded = false;
function loadLazyObjects() {
let scope = {};
scope.AddonInternal = AddonInternal;
scope.XPIProvider = XPIProvider;
scope.XPIStates = XPIStates;
Services.scriptloader.loadSubScript("resource://gre/modules/addons/XPIProviderUtils.js",
scope);
for (let name of LAZY_OBJECTS) {
delete gGlobalScope[name];
gGlobalScope[name] = scope[name];
}
gLazyObjectsLoaded = true;
return scope;
}
for (let name of LAZY_OBJECTS) {
Object.defineProperty(gGlobalScope, name, {
get: function lazyObjectGetter() {
let objs = loadLazyObjects();
return objs[name];
},
configurable: true
});
}
function findMatchingStaticBlocklistItem(aAddon) {
for (let item of STATIC_BLOCKLIST_PATTERNS) {
if ("creator" in item && typeof item.creator == "string") {
if ((aAddon.defaultLocale && aAddon.defaultLocale.creator == item.creator) ||
(aAddon.selectedLocale && aAddon.selectedLocale.creator == item.creator)) {
return item;
}
}
}
return null;
}
/**
* Sets permissions on a file
*
* @param aFile
* The file or directory to operate on.
* @param aPermissions
* The permisions to set
*/
function setFilePermissions(aFile, aPermissions) {
try {
aFile.permissions = aPermissions;
}
catch (e) {
logger.warn("Failed to set permissions " + aPermissions.toString(8) + " on " +
aFile.path, e);
}
}
/**
* A safe way to install a file or the contents of a directory to a new
* directory. The file or directory is moved or copied recursively and if
* anything fails an attempt is made to rollback the entire operation. The
* operation may also be rolled back to its original state after it has
* completed by calling the rollback method.
*
* Operations can be chained. Calling move or copy multiple times will remember
* the whole set and if one fails all of the operations will be rolled back.
*/
function SafeInstallOperation() {
this._installedFiles = [];
this._createdDirs = [];
}
SafeInstallOperation.prototype = {
_installedFiles: null,
_createdDirs: null,
_installFile: function SIO_installFile(aFile, aTargetDirectory, aCopy) {
let oldFile = aCopy ? null : aFile.clone();
let newFile = aFile.clone();
try {
if (aCopy)
newFile.copyTo(aTargetDirectory, null);
else
newFile.moveTo(aTargetDirectory, null);
}
catch (e) {
logger.error("Failed to " + (aCopy ? "copy" : "move") + " file " + aFile.path +
" to " + aTargetDirectory.path, e);
throw e;
}
this._installedFiles.push({ oldFile: oldFile, newFile: newFile });
},
_installDirectory: function SIO_installDirectory(aDirectory, aTargetDirectory, aCopy) {
let newDir = aTargetDirectory.clone();
newDir.append(aDirectory.leafName);
try {
newDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
}
catch (e) {
logger.error("Failed to create directory " + newDir.path, e);
throw e;
}
this._createdDirs.push(newDir);
// Use a snapshot of the directory contents to avoid possible issues with
// iterating over a directory while removing files from it (the YAFFS2
// embedded filesystem has this issue, see bug 772238), and to remove
// normal files before their resource forks on OSX (see bug 733436).
let entries = getDirectoryEntries(aDirectory, true);
entries.forEach(function(aEntry) {
try {
this._installDirEntry(aEntry, newDir, aCopy);
}
catch (e) {
logger.error("Failed to " + (aCopy ? "copy" : "move") + " entry " +
aEntry.path, e);
throw e;
}
}, this);
// If this is only a copy operation then there is nothing else to do
if (aCopy)
return;
// The directory should be empty by this point. If it isn't this will throw
// and all of the operations will be rolled back
try {
setFilePermissions(aDirectory, FileUtils.PERMS_DIRECTORY);
aDirectory.remove(false);
}
catch (e) {
logger.error("Failed to remove directory " + aDirectory.path, e);
throw e;
}
// Note we put the directory move in after all the file moves so the
// directory is recreated before all the files are moved back
this._installedFiles.push({ oldFile: aDirectory, newFile: newDir });
},
_installDirEntry: function SIO_installDirEntry(aDirEntry, aTargetDirectory, aCopy) {
let isDir = null;
try {
isDir = aDirEntry.isDirectory();
}
catch (e) {
// If the file has already gone away then don't worry about it, this can
// happen on OSX where the resource fork is automatically moved with the
// data fork for the file. See bug 733436.
if (e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)
return;
logger.error("Failure " + (aCopy ? "copying" : "moving") + " " + aDirEntry.path +
" to " + aTargetDirectory.path);
throw e;
}
try {
if (isDir)
this._installDirectory(aDirEntry, aTargetDirectory, aCopy);
else
this._installFile(aDirEntry, aTargetDirectory, aCopy);
}
catch (e) {
logger.error("Failure " + (aCopy ? "copying" : "moving") + " " + aDirEntry.path +
" to " + aTargetDirectory.path);
throw e;
}
},
/**
* Moves a file or directory into a new directory. If an error occurs then all
* files that have been moved will be moved back to their original location.
*
* @param aFile
* The file or directory to be moved.
* @param aTargetDirectory
* The directory to move into, this is expected to be an empty
* directory.
*/
moveUnder: function SIO_move(aFile, aTargetDirectory) {
try {
this._installDirEntry(aFile, aTargetDirectory, false);
}
catch (e) {
this.rollback();
throw e;
}
},
/**
* Renames a file to a new location. If an error occurs then all
* files that have been moved will be moved back to their original location.
*
* @param aOldLocation
* The old location of the file.
* @param aNewLocation
* The new location of the file.
*/
moveTo: function(aOldLocation, aNewLocation) {
try {
let oldFile = aOldLocation.clone(), newFile = aNewLocation.clone();
oldFile.moveTo(newFile.parent, newFile.leafName);
this._installedFiles.push({ oldFile: oldFile, newFile: newFile, isMoveTo: true});
}
catch(e) {
this.rollback();
throw e;
}
},
/**
* Copies a file or directory into a new directory. If an error occurs then
* all new files that have been created will be removed.
*
* @param aFile
* The file or directory to be copied.
* @param aTargetDirectory
* The directory to copy into, this is expected to be an empty
* directory.
*/
copy: function SIO_copy(aFile, aTargetDirectory) {
try {
this._installDirEntry(aFile, aTargetDirectory, true);
}
catch (e) {
this.rollback();
throw e;
}
},
/**
* Rolls back all the moves that this operation performed. If an exception
* occurs here then both old and new directories are left in an indeterminate
* state
*/
rollback: function SIO_rollback() {
while (this._installedFiles.length > 0) {
let move = this._installedFiles.pop();
if (move.isMoveTo) {
move.newFile.moveTo(oldDir.parent, oldDir.leafName);
}
else if (move.newFile.isDirectory()) {
let oldDir = move.oldFile.parent.clone();
oldDir.append(move.oldFile.leafName);
oldDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
}
else if (!move.oldFile) {
// No old file means this was a copied file
move.newFile.remove(true);
}
else {
move.newFile.moveTo(move.oldFile.parent, null);
}
}
while (this._createdDirs.length > 0)
recursiveRemove(this._createdDirs.pop());
}
};
/**
* Gets the currently selected locale for display.
* @return the selected locale or "en-US" if none is selected
*/
function getLocale() {
if (Preferences.get(PREF_MATCH_OS_LOCALE, false))
return Services.locale.getLocaleComponentForUserAgent();
try {
let locale = Preferences.get(PREF_SELECTED_LOCALE, null, Ci.nsIPrefLocalizedString);
if (locale)
return locale;
}
catch (e) {}
return Preferences.get(PREF_SELECTED_LOCALE, "en-US");
}
/**
* Selects the closest matching locale from a list of locales.
*
* @param aLocales
* An array of locales
* @return the best match for the currently selected locale
*/
function findClosestLocale(aLocales) {
let appLocale = getLocale();
// Holds the best matching localized resource
var bestmatch = null;
// The number of locale parts it matched with
var bestmatchcount = 0;
// The number of locale parts in the match
var bestpartcount = 0;
var matchLocales = [appLocale.toLowerCase()];
/* If the current locale is English then it will find a match if there is
a valid match for en-US so no point searching that locale too. */
if (matchLocales[0].substring(0, 3) != "en-")
matchLocales.push("en-us");
for each (var locale in matchLocales) {
var lparts = locale.split("-");
for each (var localized in aLocales) {
for each (let found in localized.locales) {
found = found.toLowerCase();
// Exact match is returned immediately
if (locale == found)
return localized;
var fparts = found.split("-");
/* If we have found a possible match and this one isn't any longer
then we dont need to check further. */
if (bestmatch && fparts.length < bestmatchcount)
continue;
// Count the number of parts that match
var maxmatchcount = Math.min(fparts.length, lparts.length);
var matchcount = 0;
while (matchcount < maxmatchcount &&
fparts[matchcount] == lparts[matchcount])
matchcount++;
/* If we matched more than the last best match or matched the same and
this locale is less specific than the last best match. */
if (matchcount > bestmatchcount ||
(matchcount == bestmatchcount && fparts.length < bestpartcount)) {
bestmatch = localized;
bestmatchcount = matchcount;
bestpartcount = fparts.length;
}
}
}
// If we found a valid match for this locale return it
if (bestmatch)
return bestmatch;
}
return null;
}
/**
* Sets the userDisabled and softDisabled properties of an add-on based on what
* values those properties had for a previous instance of the add-on. The
* previous instance may be a previous install or in the case of an application
* version change the same add-on.
*
* NOTE: this may modify aNewAddon in place; callers should save the database if
* necessary
*
* @param aOldAddon
* The previous instance of the add-on
* @param aNewAddon
* The new instance of the add-on
* @param aAppVersion
* The optional application version to use when checking the blocklist
* or undefined to use the current application
* @param aPlatformVersion
* The optional platform version to use when checking the blocklist or
* undefined to use the current platform
*/
function applyBlocklistChanges(aOldAddon, aNewAddon, aOldAppVersion,
aOldPlatformVersion) {
// Copy the properties by default
aNewAddon.userDisabled = aOldAddon.userDisabled;
aNewAddon.softDisabled = aOldAddon.softDisabled;
let oldBlocklistState = Blocklist.getAddonBlocklistState(createWrapper(aOldAddon),
aOldAppVersion,
aOldPlatformVersion);
let newBlocklistState = Blocklist.getAddonBlocklistState(createWrapper(aNewAddon));
// If the blocklist state hasn't changed then the properties don't need to
// change
if (newBlocklistState == oldBlocklistState)
return;
if (newBlocklistState == Blocklist.STATE_SOFTBLOCKED) {
if (aNewAddon.type != "theme") {
// The add-on has become softblocked, set softDisabled if it isn't already
// userDisabled
aNewAddon.softDisabled = !aNewAddon.userDisabled;
}
else {
// Themes just get userDisabled to switch back to the default theme
aNewAddon.userDisabled = true;
}
}
else {
// If the new add-on is not softblocked then it cannot be softDisabled
aNewAddon.softDisabled = false;
}
}
/**
* Calculates whether an add-on should be appDisabled or not.
*
* @param aAddon
* The add-on to check
* @return true if the add-on should not be appDisabled
*/
function isUsableAddon(aAddon) {
// Hack to ensure the default theme is always usable
if (aAddon.type == "theme" && aAddon.internalName == XPIProvider.defaultSkin)
return true;
if (aAddon.blocklistState == Blocklist.STATE_BLOCKED)
return false;
if (AddonManager.checkUpdateSecurity && !aAddon.providesUpdatesSecurely)
return false;
if (!aAddon.isPlatformCompatible)
return false;
if (AddonManager.checkCompatibility) {
if (!aAddon.isCompatible)
return false;
}
else {
if (!aAddon.matchingTargetApplication)
return false;
}
return true;
}
XPCOMUtils.defineLazyServiceGetter(this, "gRDF", "@mozilla.org/rdf/rdf-service;1",
Ci.nsIRDFService);
function EM_R(aProperty) {
return gRDF.GetResource(PREFIX_NS_EM + aProperty);
}
function createAddonDetails(id, aAddon) {
return {
id: id || aAddon.id,
type: aAddon.type,
version: aAddon.version
};
}
/**
* Converts an RDF literal, resource or integer into a string.
*
* @param aLiteral
* The RDF object to convert
* @return a string if the object could be converted or null
*/
function getRDFValue(aLiteral) {
if (aLiteral instanceof Ci.nsIRDFLiteral)
return aLiteral.Value;
if (aLiteral instanceof Ci.nsIRDFResource)
return aLiteral.Value;
if (aLiteral instanceof Ci.nsIRDFInt)
return aLiteral.Value;
return null;
}
/**
* Gets an RDF property as a string
*
* @param aDs
* The RDF datasource to read the property from
* @param aResource
* The RDF resource to read the property from
* @param aProperty
* The property to read
* @return a string if the property existed or null
*/
function getRDFProperty(aDs, aResource, aProperty) {
return getRDFValue(aDs.GetTarget(aResource, EM_R(aProperty), true));
}
/**
* Reads an AddonInternal object from an RDF stream.
*
* @param aUri
* The URI that the manifest is being read from
* @param aStream
* An open stream to read the RDF from
* @return an AddonInternal object
* @throws if the install manifest in the RDF stream is corrupt or could not
* be read
*/
function loadManifestFromRDF(aUri, aStream) {
function getPropertyArray(aDs, aSource, aProperty) {
let values = [];
let targets = aDs.GetTargets(aSource, EM_R(aProperty), true);
while (targets.hasMoreElements())
values.push(getRDFValue(targets.getNext()));
return values;
}
/**
* Reads locale properties from either the main install manifest root or
* an em:localized section in the install manifest.
*
* @param aDs
* The nsIRDFDatasource to read from
* @param aSource
* The nsIRDFResource to read the properties from
* @param isDefault
* True if the locale is to be read from the main install manifest
* root
* @param aSeenLocales
* An array of locale names already seen for this install manifest.
* Any locale names seen as a part of this function will be added to
* this array
* @return an object containing the locale properties
*/
function readLocale(aDs, aSource, isDefault, aSeenLocales) {
let locale = { };
if (!isDefault) {
locale.locales = [];
let targets = ds.GetTargets(aSource, EM_R("locale"), true);
while (targets.hasMoreElements()) {
let localeName = getRDFValue(targets.getNext());
if (!localeName) {
logger.warn("Ignoring empty locale in localized properties");
continue;
}
if (aSeenLocales.indexOf(localeName) != -1) {
logger.warn("Ignoring duplicate locale in localized properties");
continue;
}
aSeenLocales.push(localeName);
locale.locales.push(localeName);
}
if (locale.locales.length == 0) {
logger.warn("Ignoring localized properties with no listed locales");
return null;
}
}
PROP_LOCALE_SINGLE.forEach(function(aProp) {
locale[aProp] = getRDFProperty(aDs, aSource, aProp);
});
PROP_LOCALE_MULTI.forEach(function(aProp) {
// Don't store empty arrays
let props = getPropertyArray(aDs, aSource,
aProp.substring(0, aProp.length - 1));
if (props.length > 0)
locale[aProp] = props;
});
return locale;
}
let rdfParser = Cc["@mozilla.org/rdf/xml-parser;1"].
createInstance(Ci.nsIRDFXMLParser)
let ds = Cc["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"].
createInstance(Ci.nsIRDFDataSource);
let listener = rdfParser.parseAsync(ds, aUri);
let channel = Cc["@mozilla.org/network/input-stream-channel;1"].
createInstance(Ci.nsIInputStreamChannel);
channel.setURI(aUri);
channel.contentStream = aStream;
channel.QueryInterface(Ci.nsIChannel);
channel.contentType = "text/xml";
listener.onStartRequest(channel, null);
try {
let pos = 0;
let count = aStream.available();
while (count > 0) {
listener.onDataAvailable(channel, null, aStream, pos, count);
pos += count;
count = aStream.available();
}
listener.onStopRequest(channel, null, Components.results.NS_OK);
}
catch (e) {
listener.onStopRequest(channel, null, e.result);
throw e;
}
let root = gRDF.GetResource(RDFURI_INSTALL_MANIFEST_ROOT);
let addon = new AddonInternal();
PROP_METADATA.forEach(function(aProp) {
addon[aProp] = getRDFProperty(ds, root, aProp);
});
addon.unpack = getRDFProperty(ds, root, "unpack") == "true";
if (!addon.type) {
addon.type = addon.internalName ? "theme" : "extension";
}
else {
let type = addon.type;
addon.type = null;
for (let name in TYPES) {
if (TYPES[name] == type) {
addon.type = name;
break;
}
}
}
if (!(addon.type in TYPES))
throw new Error("Install manifest specifies unknown type: " + addon.type);
if (addon.type != "multipackage") {
if (!addon.id)
throw new Error("No ID in install manifest");
if (!gIDTest.test(addon.id))
throw new Error("Illegal add-on ID " + addon.id);
if (!addon.version)
throw new Error("No version in install manifest");
}
addon.strictCompatibility = !(addon.type in COMPATIBLE_BY_DEFAULT_TYPES) ||
getRDFProperty(ds, root, "strictCompatibility") == "true";
// Only read these properties for extensions.
if (addon.type == "extension") {
addon.bootstrap = getRDFProperty(ds, root, "bootstrap") == "true";
addon.multiprocessCompatible = getRDFProperty(ds, root, "multiprocessCompatible") == "true";
if (addon.optionsType &&
addon.optionsType != AddonManager.OPTIONS_TYPE_DIALOG &&
addon.optionsType != AddonManager.OPTIONS_TYPE_INLINE &&
addon.optionsType != AddonManager.OPTIONS_TYPE_TAB &&
addon.optionsType != AddonManager.OPTIONS_TYPE_INLINE_INFO) {
throw new Error("Install manifest specifies unknown type: " + addon.optionsType);
}
}
else {
// Some add-on types are always restartless.
if (RESTARTLESS_TYPES.has(addon.type)) {
addon.bootstrap = true;
}
// Only extensions are allowed to provide an optionsURL, optionsType or aboutURL. For
// all other types they are silently ignored
addon.optionsURL = null;
addon.optionsType = null;
addon.aboutURL = null;
if (addon.type == "theme") {
if (!addon.internalName)
throw new Error("Themes must include an internalName property");
addon.skinnable = getRDFProperty(ds, root, "skinnable") == "true";
}
}
addon.defaultLocale = readLocale(ds, root, true);
let seenLocales = [];
addon.locales = [];
let targets = ds.GetTargets(root, EM_R("localized"), true);
while (targets.hasMoreElements()) {
let target = targets.getNext().QueryInterface(Ci.nsIRDFResource);
let locale = readLocale(ds, target, false, seenLocales);
if (locale)
addon.locales.push(locale);
}
let seenApplications = [];
addon.targetApplications = [];
targets = ds.GetTargets(root, EM_R("targetApplication"), true);
while (targets.hasMoreElements()) {
let target = targets.getNext().QueryInterface(Ci.nsIRDFResource);
let targetAppInfo = {};
PROP_TARGETAPP.forEach(function(aProp) {
targetAppInfo[aProp] = getRDFProperty(ds, target, aProp);
});
if (!targetAppInfo.id || !targetAppInfo.minVersion ||
!targetAppInfo.maxVersion) {
logger.warn("Ignoring invalid targetApplication entry in install manifest");
continue;
}
if (seenApplications.indexOf(targetAppInfo.id) != -1) {
logger.warn("Ignoring duplicate targetApplication entry for " + targetAppInfo.id +
" in install manifest");
continue;
}
seenApplications.push(targetAppInfo.id);
addon.targetApplications.push(targetAppInfo);
}
// Note that we don't need to check for duplicate targetPlatform entries since
// the RDF service coalesces them for us.
let targetPlatforms = getPropertyArray(ds, root, "targetPlatform");
addon.targetPlatforms = [];
targetPlatforms.forEach(function(aPlatform) {
let platform = {
os: null,
abi: null
};
let pos = aPlatform.indexOf("_");
if (pos != -1) {
platform.os = aPlatform.substring(0, pos);
platform.abi = aPlatform.substring(pos + 1);
}
else {
platform.os = aPlatform;
}
addon.targetPlatforms.push(platform);
});
// A theme's userDisabled value is true if the theme is not the selected skin
// or if there is an active lightweight theme. We ignore whether softblocking
// is in effect since it would change the active theme.
if (addon.type == "theme") {
addon.userDisabled = !!LightweightThemeManager.currentTheme ||
addon.internalName != XPIProvider.selectedSkin;
}
// Experiments are disabled by default. It is up to the Experiments Manager
// to enable them (it drives installation).
else if (addon.type == "experiment") {
addon.userDisabled = true;
}
else {
addon.userDisabled = false;
addon.softDisabled = addon.blocklistState == Blocklist.STATE_SOFTBLOCKED;
}
addon.applyBackgroundUpdates = AddonManager.AUTOUPDATE_DEFAULT;
// Experiments are managed and updated through an external "experiments
// manager." So disable some built-in mechanisms.
if (addon.type == "experiment") {
addon.applyBackgroundUpdates = AddonManager.AUTOUPDATE_DISABLE;
addon.updateURL = null;
addon.updateKey = null;
addon.targetApplications = [];
addon.targetPlatforms = [];
}
// Load the storage service before NSS (nsIRandomGenerator),
// to avoid a SQLite initialization error (bug 717904).
let storage = Services.storage;
// Generate random GUID used for Sync.
// This was lifted from util.js:makeGUID() from services-sync.
let guid = Cc["@mozilla.org/uuid-generator;1"]
.getService(Ci.nsIUUIDGenerator)
.generateUUID().toString();
addon.syncGUID = guid;
return addon;
}
/**
* Loads an AddonInternal object from an add-on extracted in a directory.
*
* @param aDir
* The nsIFile directory holding the add-on
* @return an AddonInternal object
* @throws if the directory does not contain a valid install manifest
*/
function loadManifestFromDir(aDir) {
function getFileSize(aFile) {
if (aFile.isSymlink())
return 0;
if (!aFile.isDirectory())
return aFile.fileSize;
let size = 0;
let entries = aFile.directoryEntries.QueryInterface(Ci.nsIDirectoryEnumerator);
let entry;
while ((entry = entries.nextFile))
size += getFileSize(entry);
entries.close();
return size;
}
let file = aDir.clone();
file.append(FILE_INSTALL_MANIFEST);
if (!file.exists() || !file.isFile())
throw new Error("Directory " + aDir.path + " does not contain a valid " +
"install manifest");
let fis = Cc["@mozilla.org/network/file-input-stream;1"].
createInstance(Ci.nsIFileInputStream);
fis.init(file, -1, -1, false);
let bis = Cc["@mozilla.org/network/buffered-input-stream;1"].
createInstance(Ci.nsIBufferedInputStream);
bis.init(fis, 4096);
try {
let addon = loadManifestFromRDF(Services.io.newFileURI(file), bis);
addon._sourceBundle = aDir.clone();
addon.size = getFileSize(aDir);
file = aDir.clone();
file.append("chrome.manifest");
let chromeManifest = ChromeManifestParser.parseSync(Services.io.newFileURI(file));
addon.hasBinaryComponents = ChromeManifestParser.hasType(chromeManifest,
"binary-component");
addon.appDisabled = !isUsableAddon(addon);
return addon;
}
finally {
bis.close();
fis.close();
}
}
/**
* Loads an AddonInternal object from an nsIZipReader for an add-on.
*
* @param aZipReader
* An open nsIZipReader for the add-on's files
* @return an AddonInternal object
* @throws if the XPI file does not contain a valid install manifest.
* Throws with |webext:true| if a WebExtension manifest was found
* to distinguish between WebExtensions and corrupt files.
* Throws with |jetpacksdk:true| if a Jetpack files were found
* if Jetpack its self isn't built.
*/
function loadManifestFromZipReader(aZipReader) {
// If WebExtension but not install.rdf throw an error
if (aZipReader.hasEntry(FILE_WEBEXT_MANIFEST)) {
if (!aZipReader.hasEntry(FILE_INSTALL_MANIFEST)) {
throw {
name: "UnsupportedExtension",
message: Services.appinfo.name + " does not support WebExtensions",
webext: true
};
}
}
#ifndef MOZ_JETPACK
// If Jetpack is not built throw an error
if (aZipReader.hasEntry(FILE_JETPACK_MANIFEST_1) ||
aZipReader.hasEntry(FILE_JETPACK_MANIFEST_2)) {
throw {
name: "UnsupportedExtension",
message: Services.appinfo.name + " does not support Jetpack Extensions",
jetpacksdk: true
};
}
#endif
// Attempt to open install.rdf else throw normally
let zis = aZipReader.getInputStream(FILE_INSTALL_MANIFEST);
// Create a buffered input stream for install.rdf
let bis = Cc["@mozilla.org/network/buffered-input-stream;1"].
createInstance(Ci.nsIBufferedInputStream);
bis.init(zis, 4096);
try {
let uri = buildJarURI(aZipReader.file, FILE_INSTALL_MANIFEST);
let addon = loadManifestFromRDF(uri, bis);
addon._sourceBundle = aZipReader.file;
addon.size = 0;
let entries = aZipReader.findEntries(null);
while (entries.hasMore())
addon.size += aZipReader.getEntry(entries.getNext()).realSize;
// Binary components can only be loaded from unpacked addons.
if (addon.unpack) {
uri = buildJarURI(aZipReader.file, "chrome.manifest");
let chromeManifest = ChromeManifestParser.parseSync(uri);
addon.hasBinaryComponents = ChromeManifestParser.hasType(chromeManifest,
"binary-component");
} else {
addon.hasBinaryComponents = false;
}
addon.appDisabled = !isUsableAddon(addon);
return addon;
}
finally {
// Close the buffered input stream
bis.close();
// Close the input stream to install.rdf
zis.close();
}
}
/**
* Loads an AddonInternal object from an add-on in an XPI file.
*
* @param aXPIFile
* An nsIFile pointing to the add-on's XPI file
* @return an AddonInternal object
* @throws if the XPI file does not contain a valid install manifest
*/
function loadManifestFromZipFile(aXPIFile) {
let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].
createInstance(Ci.nsIZipReader);
try {
zipReader.open(aXPIFile);
return loadManifestFromZipReader(zipReader);
}
finally {
zipReader.close();
}
}
function loadManifestFromFile(aFile) {
if (aFile.isFile())
return loadManifestFromZipFile(aFile);
else
return loadManifestFromDir(aFile);
}
/**
* Gets an nsIURI for a file within another file, either a directory or an XPI
* file. If aFile is a directory then this will return a file: URI, if it is an
* XPI file then it will return a jar: URI.
*
* @param aFile
* The file containing the resources, must be either a directory or an
* XPI file
* @param aPath
* The path to find the resource at, "/" separated. If aPath is empty
* then the uri to the root of the contained files will be returned
* @return an nsIURI pointing at the resource
*/
function getURIForResourceInFile(aFile, aPath) {
if (aFile.isDirectory()) {
let resource = aFile.clone();
if (aPath) {
aPath.split("/").forEach(function(aPart) {
resource.append(aPart);
});
}
return NetUtil.newURI(resource);
}
return buildJarURI(aFile, aPath);
}
/**
* Creates a jar: URI for a file inside a ZIP file.
*
* @param aJarfile
* The ZIP file as an nsIFile
* @param aPath
* The path inside the ZIP file
* @return an nsIURI for the file
*/
function buildJarURI(aJarfile, aPath) {
let uri = Services.io.newFileURI(aJarfile);
uri = "jar:" + uri.spec + "!/" + aPath;
return NetUtil.newURI(uri);
}
/**
* Sends local and remote notifications to flush a JAR file cache entry
*
* @param aJarFile
* The ZIP/XPI/JAR file as a nsIFile
*/
function flushJarCache(aJarFile) {
Services.obs.notifyObservers(aJarFile, "flush-cache-entry", null);
Services.mm.broadcastAsyncMessage(MSG_JAR_FLUSH, aJarFile.path);
}
function flushChromeCaches() {
// Init this, so it will get the notification.
Services.obs.notifyObservers(null, "startupcache-invalidate", null);
// Flush message manager cached scripts
Services.obs.notifyObservers(null, "message-manager-flush-caches", null);
// Also dispatch this event to child processes
Services.mm.broadcastAsyncMessage(MSG_MESSAGE_MANAGER_CACHES_FLUSH, null);
}
/**
* Creates and returns a new unique temporary file. The caller should delete
* the file when it is no longer needed.
*
* @return an nsIFile that points to a randomly named, initially empty file in
* the OS temporary files directory
*/
function getTemporaryFile() {
let file = FileUtils.getDir(KEY_TEMPDIR, []);
let random = Math.random().toString(36).replace(/0./, '').substr(-3);
file.append("tmp-" + random + ".xpi");
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
return file;
}
/**
* Verifies that a zip file's contents are all signed by the same principal.
* Directory entries and anything in the META-INF directory are not checked.
*
* @param aZip
* A nsIZipReader to check
* @param aCertificate
* The nsIX509Cert to compare against
* @return true if all the contents that should be signed were signed by the
* principal
*/
function verifyZipSigning(aZip, aCertificate) {
var count = 0;
var entries = aZip.findEntries(null);
while (entries.hasMore()) {
var entry = entries.getNext();
// Nothing in META-INF is in the manifest.
if (entry.substr(0, 9) == "META-INF/")
continue;
// Directory entries aren't in the manifest.
if (entry.substr(-1) == "/")
continue;
count++;
var entryCertificate = aZip.getSigningCert(entry);
if (!entryCertificate || !aCertificate.equals(entryCertificate)) {
return false;
}
}
return aZip.manifestEntriesCount == count;
}
/**
* Replaces %...% strings in an addon url (update and updateInfo) with
* appropriate values.
*
* @param aAddon
* The AddonInternal representing the add-on
* @param aUri
* The uri to escape
* @param aUpdateType
* An optional number representing the type of update, only applicable
* when creating a url for retrieving an update manifest
* @param aAppVersion
* The optional application version to use for %APP_VERSION%
* @return the appropriately escaped uri.
*/
function escapeAddonURI(aAddon, aUri, aUpdateType, aAppVersion)
{
let uri = AddonManager.escapeAddonURI(aAddon, aUri, aAppVersion);
// If there is an updateType then replace the UPDATE_TYPE string
if (aUpdateType)
uri = uri.replace(/%UPDATE_TYPE%/g, aUpdateType);
// If this add-on has compatibility information for either the current
// application or toolkit then replace the ITEM_MAXAPPVERSION with the
// maxVersion
let app = aAddon.matchingTargetApplication;
if (app)
var maxVersion = app.maxVersion;
else
maxVersion = "";
uri = uri.replace(/%ITEM_MAXAPPVERSION%/g, maxVersion);
let compatMode = "normal";
if (!AddonManager.checkCompatibility)
compatMode = "ignore";
else if (AddonManager.strictCompatibility)
compatMode = "strict";
uri = uri.replace(/%COMPATIBILITY_MODE%/g, compatMode);
return uri;
}
function removeAsync(aFile) {
return Task.spawn(function () {
let info = null;
try {
info = yield OS.File.stat(aFile.path);
if (info.isDir)
yield OS.File.removeDir(aFile.path);
else
yield OS.File.remove(aFile.path);
}
catch (e if e instanceof OS.File.Error && e.becauseNoSuchFile) {
// The file has already gone away
return;
}
});
}
/**
* Recursively removes a directory or file fixing permissions when necessary.
*
* @param aFile
* The nsIFile to remove
*/
function recursiveRemove(aFile) {
let isDir = null;
try {
isDir = aFile.isDirectory();
}
catch (e) {
// If the file has already gone away then don't worry about it, this can
// happen on OSX where the resource fork is automatically moved with the
// data fork for the file. See bug 733436.
if (e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)
return;
if (e.result == Cr.NS_ERROR_FILE_NOT_FOUND)
return;
throw e;
}
setFilePermissions(aFile, isDir ? FileUtils.PERMS_DIRECTORY
: FileUtils.PERMS_FILE);
try {
aFile.remove(true);
return;
}
catch (e) {
if (!aFile.isDirectory()) {
logger.error("Failed to remove file " + aFile.path, e);
throw e;
}
}
// Use a snapshot of the directory contents to avoid possible issues with
// iterating over a directory while removing files from it (the YAFFS2
// embedded filesystem has this issue, see bug 772238), and to remove
// normal files before their resource forks on OSX (see bug 733436).
let entries = getDirectoryEntries(aFile, true);
entries.forEach(recursiveRemove);
try {
aFile.remove(true);
}
catch (e) {
logger.error("Failed to remove empty directory " + aFile.path, e);
throw e;
}
}
/**
* Returns the timestamp and leaf file name of the most recently modified
* entry in a directory,
* or simply the file's own timestamp if it is not a directory.
* Also returns the total number of items (directories and files) visited in the scan
*
* @param aFile
* A non-null nsIFile object
* @return [File Name, Epoch time, items visited], as described above.
*/
function recursiveLastModifiedTime(aFile) {
try {
let modTime = aFile.lastModifiedTime;
let fileName = aFile.leafName;
if (aFile.isFile())
return [fileName, modTime, 1];
if (aFile.isDirectory()) {
let entries = aFile.directoryEntries.QueryInterface(Ci.nsIDirectoryEnumerator);
let entry;
let totalItems = 1;
while ((entry = entries.nextFile)) {
let [subName, subTime, items] = recursiveLastModifiedTime(entry);
totalItems += items;
if (subTime > modTime) {
modTime = subTime;
fileName = subName;
}
}
entries.close();
return [fileName, modTime, totalItems];
}
}
catch (e) {
logger.warn("Problem getting last modified time for " + aFile.path, e);
}
// If the file is something else, just ignore it.
return ["", 0, 0];
}
/**
* Gets a snapshot of directory entries.
*
* @param aDir
* Directory to look at
* @param aSortEntries
* True to sort entries by filename
* @return An array of nsIFile, or an empty array if aDir is not a readable directory
*/
function getDirectoryEntries(aDir, aSortEntries) {
let dirEnum;
try {
dirEnum = aDir.directoryEntries.QueryInterface(Ci.nsIDirectoryEnumerator);
let entries = [];
while (dirEnum.hasMoreElements())
entries.push(dirEnum.nextFile);
if (aSortEntries) {
entries.sort(function sortDirEntries(a, b) {
return a.path > b.path ? -1 : 1;
});
}
return entries
}
catch (e) {
logger.warn("Can't iterate directory " + aDir.path, e);
return [];
}
finally {
if (dirEnum) {
dirEnum.close();
}
}
}
/**
* Wraps a function in an exception handler to protect against exceptions inside callbacks
* @param aFunction function(args...)
* @return function(args...), a function that takes the same arguments as aFunction
* and returns the same result unless aFunction throws, in which case it logs
* a warning and returns undefined.
*/
function makeSafe(aFunction) {
return function(...aArgs) {
try {
return aFunction(...aArgs);
}
catch(ex) {
logger.warn("XPIProvider callback failed", ex);
}
return undefined;
}
}
/**
* The on-disk state of an individual XPI, created from an Object
* as stored in the 'extensions.xpiState' pref.
*/
function XPIState(saved) {
for (let [short, long] of XPIState.prototype.fields) {
if (short in saved) {
this[long] = saved[short];
}
}
}
XPIState.prototype = {
fields: [['d', 'descriptor'],
['e', 'enabled'],
['v', 'version'],
['st', 'scanTime'],
['mt', 'manifestTime']],
/**
* Return the last modified time, based on enabled/disabled
*/
get mtime() {
if (!this.enabled && ('manifestTime' in this) && this.manifestTime > this.scanTime) {
return this.manifestTime;
}
return this.scanTime;
},
toJSON() {
let json = {};
for (let [short, long] of XPIState.prototype.fields) {
if (long in this) {
json[short] = this[long];
}
}
return json;
},
/**
* Update the last modified time for an add-on on disk.
* @param aFile: nsIFile path of the add-on.
* @param aId: The add-on ID.
* @return True if the time stamp has changed.
*/
getModTime(aFile, aId) {
let changed = false;
let scanStarted = Cu.now();
// For an unknown or enabled add-on, we do a full recursive scan.
if (!('scanTime' in this) || this.enabled) {
logger.debug('getModTime: Recursive scan of ' + aId);
let [modFile, modTime, items] = recursiveLastModifiedTime(aFile);
XPIProvider._mostRecentlyModifiedFile[aId] = modFile;
if (modTime != this.scanTime) {
this.scanTime = modTime;
changed = true;
}
}
// if the add-on is disabled, modified time is the install.rdf time, if any.
// If {path}/install.rdf doesn't exist, we assume this is a packed .xpi and use
// the time stamp of {path}
try {
// Get the install.rdf update time, if any.
// XXX This will eventually also need to check for package.json or whatever
// the new manifest is named.
let maniFile = aFile.clone();
maniFile.append(FILE_INSTALL_MANIFEST);
if (!(aId in XPIProvider._mostRecentlyModifiedFile)) {
XPIProvider._mostRecentlyModifiedFile[aId] = maniFile.leafName;
}
let maniTime = maniFile.lastModifiedTime;
if (maniTime != this.manifestTime) {
this.manifestTime = maniTime;
changed = true;
}
} catch (e) {
// No manifest
delete this.manifestTime;
try {
let dtime = aFile.lastModifiedTime;
if (dtime != this.scanTime) {
changed = true;
this.scanTime = dtime;
}
} catch (e) {
logger.warn("Can't get modified time of ${file}: ${e}", {file: aFile.path, e: e});
changed = true;
this.scanTime = 0;
}
}
return changed;
},
/**
* Update the XPIState to match an XPIDatabase entry; if 'enabled' is changed to true,
* update the last-modified time. This should probably be made async, but for now we
* don't want to maintain parallel sync and async versions of the scan.
* Caller is responsible for doing XPIStates.save() if necessary.
* @param aDBAddon The DBAddonInternal for this add-on.
* @param aUpdated The add-on was updated, so we must record new modified time.
*/
syncWithDB(aDBAddon, aUpdated = false) {
logger.debug("Updating XPIState for " + JSON.stringify(aDBAddon));
// If the add-on changes from disabled to enabled, we should re-check the modified time.
// If this is a newly found add-on, it won't have an 'enabled' field but we
// did a full recursive scan in that case, so we don't need to do it again.
// We don't use aDBAddon.active here because it's not updated until after restart.
let mustGetMod = (aDBAddon.visible && !aDBAddon.disabled && !this.enabled);
this.enabled = (aDBAddon.visible && !aDBAddon.disabled);
this.version = aDBAddon.version;
// XXX Eventually also copy bootstrap, etc.
if (aUpdated || mustGetMod) {
this.getModTime(new nsIFile(this.descriptor), aDBAddon.id);
if (this.scanTime != aDBAddon.updateDate) {
aDBAddon.updateDate = this.scanTime;
XPIDatabase.saveChanges();
}
}
},
};
// Constructor for an ES6 Map that knows how to convert itself into a
// regular object for toJSON().
function SerializableMap() {
let m = new Map();
m.toJSON = function() {
let out = {}
for (let [key, val] of m) {
out[key] = val;
}
return out;
};
return m;
}
/**
* Keeps track of the state of XPI add-ons on the file system.
*/
this.XPIStates = {
// Map(location name -> Map(add-on ID -> XPIState))
db: null,
get size() {
if (!this.db) {
return 0;
}
let count = 0;
for (let location of this.db.values()) {
count += location.size;
}
return count;
},
/**
* Load extension state data from preferences.
*/
loadExtensionState() {
let state = {};
// Clear out old directory state cache.
Preferences.reset(PREF_INSTALL_CACHE);
let cache = Preferences.get(PREF_XPI_STATE, "{}");
try {
state = JSON.parse(cache);
} catch (e) {
logger.warn("Error parsing extensions.xpiState ${state}: ${error}",
{state: cache, error: e});
}
logger.debug("Loaded add-on state from prefs: ${}", state);
return state;
},
/**
* Walk through all install locations, highest priority first,
* comparing the on-disk state of extensions to what is stored in prefs.
* @return true if anything has changed.
*/
getInstallState() {
let oldState = this.loadExtensionState();
let changed = false;
this.db = new SerializableMap();
for (let location of XPIProvider.installLocations) {
// The list of add-on like file/directory names in the install location.
let addons = location.addonLocations;
// The results of scanning this location.
let foundAddons = new SerializableMap();
// What our old state thinks should be in this location.
let locState = {};
if (location.name in oldState) {
locState = oldState[location.name];
// We've seen this location.
delete oldState[location.name];
}
for (let file of addons) {
let id = location.getIDForLocation(file);
if (!(id in locState)) {
logger.debug("New add-on ${id} in ${location}", {id: id, location: location.name});
let xpiState = new XPIState({d: file.persistentDescriptor});
changed = xpiState.getModTime(file, id) || changed;
foundAddons.set(id, xpiState);
} else {
let xpiState = new XPIState(locState[id]);
// We found this add-on in the file system
delete locState[id];
changed = xpiState.getModTime(file, id) || changed;
if (file.persistentDescriptor != xpiState.descriptor) {
xpiState.descriptor = file.persistentDescriptor;
changed = true;
}
if (changed) {
logger.debug("Changed add-on ${id} in ${location}", {id: id, location: location.name});
}
foundAddons.set(id, xpiState);
}
}
// Anything left behind in oldState was removed from the file system.
for (let id in locState) {
changed = true;
break;
}
// If we found anything, add this location to our database.
if (foundAddons.size != 0) {
this.db.set(location.name, foundAddons);
}
}
// If there's anything left in oldState, an install location that held add-ons
// was removed from the browser configuration.
for (let location in oldState) {
changed = true;
break;
}
logger.debug("getInstallState changed: ${rv}, state: ${state}",
{rv: changed, state: this.db});
return changed;
},
/**
* Get the Map of XPI states for a particular location.
* @param aLocation The name of the install location.
* @return Map (id -> XPIState) or null if there are no add-ons in the location.
*/
getLocation(aLocation) {
return this.db.get(aLocation);
},
/**
* Get the XPI state for a specific add-on in a location.
* If the state is not in our cache, return null.
* @param aLocation The name of the location where the add-on is installed.
* @param aId The add-on ID
* @return The XPIState entry for the add-on, or null.
*/
getAddon(aLocation, aId) {
let location = this.db.get(aLocation);
if (!location) {
return null;
}
return location.get(aId);
},
/**
* Find the highest priority location of an add-on by ID and return the
* location and the XPIState.
* @param aId The add-on ID
* @return [locationName, XPIState] if the add-on is found, [undefined, undefined]
* if the add-on is not found.
*/
findAddon(aId) {
// Fortunately the Map iterator returns in order of insertion, which is
// also our highest -> lowest priority order.
for (let [name, location] of this.db) {
if (location.has(aId)) {
return [name, location.get(aId)];
}
}
return [undefined, undefined];
},
/**
* Add a new XPIState for an add-on and synchronize it with the DBAddonInternal.
* @param aAddon DBAddonInternal for the new add-on.
*/
addAddon(aAddon) {
let location = this.db.get(aAddon.location);
if (!location) {
// First add-on in this location.
location = new SerializableMap();
this.db.set(aAddon.location, location);
}
logger.debug("XPIStates adding add-on ${id} in ${location}: ${descriptor}", aAddon);
let xpiState = new XPIState({d: aAddon.descriptor});
location.set(aAddon.id, xpiState);
xpiState.syncWithDB(aAddon, true);
},
/**
* Save the current state of installed add-ons.
* XXX this *totally* should be a .json file using DeferredSave...
*/
save() {
let cache = JSON.stringify(this.db);
Services.prefs.setCharPref(PREF_XPI_STATE, cache);
},
/**
* Remove the XPIState for an add-on and save the new state.
* @param aLocation The name of the add-on location.
* @param aId The ID of the add-on.
*/
removeAddon(aLocation, aId) {
logger.debug("Removing XPIState for " + aLocation + ":" + aId);
let location = this.db.get(aLocation);
if (!location) {
return;
}
location.delete(aId);
if (location.size == 0) {
this.db.delete(aLocation);
}
this.save();
},
};
this.XPIProvider = {
get name() "XPIProvider",
// An array of known install locations
installLocations: null,
// A dictionary of known install locations by name
installLocationsByName: null,
// An array of currently active AddonInstalls
installs: null,
// The default skin for the application
defaultSkin: "classic/1.0",
// The current skin used by the application
currentSkin: null,
// The selected skin to be used by the application when it is restarted. This
// will be the same as currentSkin when it is the skin to be used when the
// application is restarted
selectedSkin: null,
// The value of the minCompatibleAppVersion preference
minCompatibleAppVersion: null,
// The value of the minCompatiblePlatformVersion preference
minCompatiblePlatformVersion: null,
// A dictionary of the file descriptors for bootstrappable add-ons by ID
bootstrappedAddons: {},
// A dictionary of JS scopes of loaded bootstrappable add-ons by ID
bootstrapScopes: {},
// True if the platform could have activated extensions
extensionsActive: false,
// True if all of the add-ons found during startup were installed in the
// application install location
allAppGlobal: true,
// A string listing the enabled add-ons for annotating crash reports
enabledAddons: null,
// Keep track of startup phases for telemetry
runPhase: XPI_STARTING,
// Keep track of the newest file in each add-on, in case we want to
// report it to telemetry.
_mostRecentlyModifiedFile: {},
// Per-addon telemetry information
_telemetryDetails: {},
// Experiments are disabled by default. Track ones that are locally enabled.
_enabledExperiments: null,
// A Map from an add-on install to its ID
_addonFileMap: new Map(),
#ifdef MOZ_DEVTOOLS
// Flag to know if ToolboxProcess.jsm has already been loaded by someone or not
_toolboxProcessLoaded: false,
#endif
// Have we started shutting down bootstrap add-ons?
_closing: false,
// Keep track of in-progress operations that support cancel()
_inProgress: [],
doing: function XPI_doing(aCancellable) {
this._inProgress.push(aCancellable);
},
done: function XPI_done(aCancellable) {
let i = this._inProgress.indexOf(aCancellable);
if (i != -1) {
this._inProgress.splice(i, 1);
return true;
}
return false;
},
cancelAll: function XPI_cancelAll() {
// Cancelling one may alter _inProgress, so don't use a simple iterator
while (this._inProgress.length > 0) {
let c = this._inProgress.shift();
try {
c.cancel();
}
catch (e) {
logger.warn("Cancel failed", e);
}
}
},
/**
* Adds or updates a URI mapping for an Addon.id.
*
* Mappings should not be removed at any point. This is so that the mappings
* will be still valid after an add-on gets disabled or uninstalled, as
* consumers may still have URIs of (leaked) resources they want to map.
*/
_addURIMapping: function XPI__addURIMapping(aID, aFile) {
logger.info("Mapping " + aID + " to " + aFile.path);
this._addonFileMap.set(aID, aFile.path);
AddonPathService.insertPath(aFile.path, aID);
},
/**
* Resolve a URI back to physical file.
*
* Of course, this works only for URIs pointing to local resources.
*
* @param aURI
* URI to resolve
* @return
* resolved nsIFileURL
*/
_resolveURIToFile: function XPI__resolveURIToFile(aURI) {
switch (aURI.scheme) {
case "jar":
case "file":
if (aURI instanceof Ci.nsIJARURI) {
return this._resolveURIToFile(aURI.JARFile);
}
return aURI;
case "chrome":
aURI = ChromeRegistry.convertChromeURL(aURI);
return this._resolveURIToFile(aURI);
case "resource":
aURI = Services.io.newURI(ResProtocolHandler.resolveURI(aURI), null,
null);
return this._resolveURIToFile(aURI);
case "view-source":
aURI = Services.io.newURI(aURI.path, null, null);
return this._resolveURIToFile(aURI);
case "about":
if (aURI.spec == "about:blank") {
// Do not attempt to map about:blank
return null;
}
let chan;
try {
chan = NetUtil.newChannel({
uri: aURI,
loadUsingSystemPrincipal: true
});
}
catch (ex) {
return null;
}
// Avoid looping
if (chan.URI.equals(aURI)) {
return null;
}
// We want to clone the channel URI to avoid accidentially keeping
// unnecessary references to the channel or implementation details
// around.
return this._resolveURIToFile(chan.URI.clone());
default:
return null;
}
},
/**
* Starts the XPI provider initializes the install locations and prefs.
*
* @param aAppChanged
* A tri-state value. Undefined means the current profile was created
* for this session, true means the profile already existed but was
* last used with an application with a different version number,
* false means that the profile was last used by this version of the
* application.
* @param aOldAppVersion
* The version of the application last run with this profile or null
* if it is a new profile or the version is unknown
* @param aOldPlatformVersion
* The version of the platform last run with this profile or null
* if it is a new profile or the version is unknown
*/
startup: function XPI_startup(aAppChanged, aOldAppVersion, aOldPlatformVersion) {
function addDirectoryInstallLocation(aName, aKey, aPaths, aScope, aLocked) {
try {
var dir = FileUtils.getDir(aKey, aPaths);
}
catch (e) {
// Some directories aren't defined on some platforms, ignore them
logger.debug("Skipping unavailable install location " + aName);
return;
}
try {
var location = new DirectoryInstallLocation(aName, dir, aScope, aLocked);
}
catch (e) {
logger.warn("Failed to add directory install location " + aName, e);
return;
}
XPIProvider.installLocations.push(location);
XPIProvider.installLocationsByName[location.name] = location;
}
function addRegistryInstallLocation(aName, aRootkey, aScope) {
try {
var location = new WinRegInstallLocation(aName, aRootkey, aScope);
}
catch (e) {
logger.warn("Failed to add registry install location " + aName, e);
return;
}
XPIProvider.installLocations.push(location);
XPIProvider.installLocationsByName[location.name] = location;
}
try {
AddonManagerPrivate.recordTimestamp("XPI_startup_begin");
logger.debug("startup");
this.runPhase = XPI_STARTING;
this.installs = [];
this.installLocations = [];
this.installLocationsByName = {};
// Hook for tests to detect when saving database at shutdown time fails
this._shutdownError = null;
// Clear this at startup for xpcshell test restarts
this._telemetryDetails = {};
// Clear the set of enabled experiments (experiments disabled by default).
this._enabledExperiments = new Set();
let hasRegistry = ("nsIWindowsRegKey" in Ci);
let enabledScopes = Preferences.get(PREF_EM_ENABLED_SCOPES,
AddonManager.SCOPE_ALL);
// These must be in order of priority, highest to lowest,
// for processFileChanges etc. to work
// The profile location is always enabled
addDirectoryInstallLocation(KEY_APP_PROFILE, KEY_PROFILEDIR,
[DIR_EXTENSIONS],
AddonManager.SCOPE_PROFILE, false);
if (enabledScopes & AddonManager.SCOPE_USER) {
addDirectoryInstallLocation(KEY_APP_SYSTEM_USER, "XREUSysExt",
[Services.appinfo.ID],
AddonManager.SCOPE_USER, true);
if (hasRegistry) {
addRegistryInstallLocation("winreg-app-user",
Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
AddonManager.SCOPE_USER);
}
}
if (enabledScopes & AddonManager.SCOPE_APPLICATION) {
addDirectoryInstallLocation(KEY_APP_GLOBAL, KEY_APPDIR,
[DIR_EXTENSIONS],
AddonManager.SCOPE_APPLICATION, true);
}
if (enabledScopes & AddonManager.SCOPE_SYSTEM) {
addDirectoryInstallLocation(KEY_APP_SYSTEM_SHARE, "XRESysSExtPD",
[Services.appinfo.ID],
AddonManager.SCOPE_SYSTEM, true);
addDirectoryInstallLocation(KEY_APP_SYSTEM_LOCAL, "XRESysLExtPD",
[Services.appinfo.ID],
AddonManager.SCOPE_SYSTEM, true);
if (hasRegistry) {
addRegistryInstallLocation("winreg-app-global",
Ci.nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE,
AddonManager.SCOPE_SYSTEM);
}
}
let defaultPrefs = new Preferences({ defaultBranch: true });
this.defaultSkin = defaultPrefs.get(PREF_GENERAL_SKINS_SELECTEDSKIN,
"classic/1.0");
this.currentSkin = Preferences.get(PREF_GENERAL_SKINS_SELECTEDSKIN,
this.defaultSkin);
this.selectedSkin = this.currentSkin;
this.applyThemeChange();
this.minCompatibleAppVersion = Preferences.get(PREF_EM_MIN_COMPAT_APP_VERSION,
null);
this.minCompatiblePlatformVersion = Preferences.get(PREF_EM_MIN_COMPAT_PLATFORM_VERSION,
null);
this.enabledAddons = "";
Services.prefs.addObserver(PREF_EM_MIN_COMPAT_APP_VERSION, this, false);
Services.prefs.addObserver(PREF_EM_MIN_COMPAT_PLATFORM_VERSION, this, false);
Services.obs.addObserver(this, NOTIFICATION_FLUSH_PERMISSIONS, false);
#ifdef MOZ_DEVTOOLS
if (Cu.isModuleLoaded("resource://devtools/client/framework/ToolboxProcess.jsm")) {
// If BrowserToolboxProcess is already loaded, set the boolean to true
// and do whatever is needed
this._toolboxProcessLoaded = true;
BrowserToolboxProcess.on("connectionchange",
this.onDebugConnectionChange.bind(this));
}
else {
// Else, wait for it to load
Services.obs.addObserver(this, NOTIFICATION_TOOLBOXPROCESS_LOADED, false);
}
#endif
let flushCaches = this.checkForChanges(aAppChanged, aOldAppVersion,
aOldPlatformVersion);
// Changes to installed extensions may have changed which theme is selected
this.applyThemeChange();
AddonManagerPrivate.markProviderSafe(this);
if (aAppChanged === undefined) {
// For new profiles we will never need to show the add-on selection UI
Services.prefs.setBoolPref(PREF_SHOWN_SELECTION_UI, true);
}
else if (aAppChanged && !this.allAppGlobal &&
Preferences.get(PREF_EM_SHOW_MISMATCH_UI, true)) {
if (!Preferences.get(PREF_SHOWN_SELECTION_UI, false)) {
// Flip a flag to indicate that we interrupted startup with an interactive prompt
Services.startup.interrupted = true;
// This *must* be modal as it has to block startup.
var features = "chrome,centerscreen,dialog,titlebar,modal";
Services.ww.openWindow(null, URI_EXTENSION_SELECT_DIALOG, "", features, null);
Services.prefs.setBoolPref(PREF_SHOWN_SELECTION_UI, true);
// Ensure any changes to the add-ons list are flushed to disk
Services.prefs.setBoolPref(PREF_PENDING_OPERATIONS,
!XPIDatabase.writeAddonsList());
}
else {
let addonsToUpdate = this.shouldForceUpdateCheck(aAppChanged);
if (addonsToUpdate) {
this.showUpgradeUI(addonsToUpdate);
flushCaches = true;
}
}
}
if (flushCaches) {
Services.obs.notifyObservers(null, "startupcache-invalidate", null);
// UI displayed early in startup (like the compatibility UI) may have
// caused us to cache parts of the skin or locale in memory. These must
// be flushed to allow extension provided skins and locales to take full
// effect
Services.obs.notifyObservers(null, "chrome-flush-skin-caches", null);
Services.obs.notifyObservers(null, "chrome-flush-caches", null);
}
this.enabledAddons = Preferences.get(PREF_EM_ENABLED_ADDONS, "");
if ("nsICrashReporter" in Ci &&
Services.appinfo instanceof Ci.nsICrashReporter) {
// Annotate the crash report with relevant add-on information.
try {
Services.appinfo.annotateCrashReport("Theme", this.currentSkin);
} catch (e) { }
try {
Services.appinfo.annotateCrashReport("EMCheckCompatibility",
AddonManager.checkCompatibility);
} catch (e) { }
this.addAddonsToCrashReporter();
}
try {
AddonManagerPrivate.recordTimestamp("XPI_bootstrap_addons_begin");
for (let id in this.bootstrappedAddons) {
try {
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
file.persistentDescriptor = this.bootstrappedAddons[id].descriptor;
let reason = BOOTSTRAP_REASONS.APP_STARTUP;
// Eventually set INSTALLED reason when a bootstrap addon
// is dropped in profile folder and automatically installed
if (AddonManager.getStartupChanges(AddonManager.STARTUP_CHANGE_INSTALLED)
.indexOf(id) !== -1)
reason = BOOTSTRAP_REASONS.ADDON_INSTALL;
this.callBootstrapMethod(createAddonDetails(id, this.bootstrappedAddons[id]),
file, "startup", reason);
}
catch (e) {
logger.error("Failed to load bootstrap addon " + id + " from " +
this.bootstrappedAddons[id].descriptor, e);
}
}
AddonManagerPrivate.recordTimestamp("XPI_bootstrap_addons_end");
}
catch (e) {
logger.error("bootstrap startup failed", e);
AddonManagerPrivate.recordException("XPI-BOOTSTRAP", "startup failed", e);
}
// Let these shutdown a little earlier when they still have access to most
// of XPCOM
Services.obs.addObserver({
observe: function shutdownObserver(aSubject, aTopic, aData) {
XPIProvider._closing = true;
for (let id in XPIProvider.bootstrappedAddons) {
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
file.persistentDescriptor = XPIProvider.bootstrappedAddons[id].descriptor;
let addon = createAddonDetails(id, XPIProvider.bootstrappedAddons[id]);
XPIProvider.callBootstrapMethod(addon, file, "shutdown",
BOOTSTRAP_REASONS.APP_SHUTDOWN);
}
Services.obs.removeObserver(this, "quit-application-granted");
}
}, "quit-application-granted", false);
// Detect final-ui-startup for telemetry reporting
Services.obs.addObserver({
observe: function uiStartupObserver(aSubject, aTopic, aData) {
AddonManagerPrivate.recordTimestamp("XPI_finalUIStartup");
XPIProvider.runPhase = XPI_AFTER_UI_STARTUP;
Services.obs.removeObserver(this, "final-ui-startup");
}
}, "final-ui-startup", false);
AddonManagerPrivate.recordTimestamp("XPI_startup_end");
this.extensionsActive = true;
this.runPhase = XPI_BEFORE_UI_STARTUP;
}
catch (e) {
logger.error("startup failed", e);
AddonManagerPrivate.recordException("XPI", "startup failed", e);
}
},
/**
* Shuts down the database and releases all references.
* Return: Promise{integer} resolves / rejects with the result of
* flushing the XPI Database if it was loaded,
* 0 otherwise.
*/
shutdown: function XPI_shutdown() {
logger.debug("shutdown");
// Stop anything we were doing asynchronously
this.cancelAll();
this.bootstrappedAddons = {};
this.bootstrapScopes = {};
this.enabledAddons = null;
this.allAppGlobal = true;
// If there are pending operations then we must update the list of active
// add-ons
if (Preferences.get(PREF_PENDING_OPERATIONS, false)) {
AddonManagerPrivate.recordSimpleMeasure("XPIDB_pending_ops", 1);
XPIDatabase.updateActiveAddons();
Services.prefs.setBoolPref(PREF_PENDING_OPERATIONS,
!XPIDatabase.writeAddonsList());
}
this.installs = null;
this.installLocations = null;
this.installLocationsByName = null;
// This is needed to allow xpcshell tests to simulate a restart
this.extensionsActive = false;
this._addonFileMap.clear();
if (gLazyObjectsLoaded) {
let done = XPIDatabase.shutdown();
done.then(
ret => {
logger.debug("Notifying XPI shutdown observers");
Services.obs.notifyObservers(null, "xpi-provider-shutdown", null);
},
err => {
logger.debug("Notifying XPI shutdown observers");
this._shutdownError = err;
Services.obs.notifyObservers(null, "xpi-provider-shutdown", err);
}
);
return done;
}
else {
logger.debug("Notifying XPI shutdown observers");
Services.obs.notifyObservers(null, "xpi-provider-shutdown", null);
}
},
/**
* Applies any pending theme change to the preferences.
*/
applyThemeChange: function XPI_applyThemeChange() {
if (!Preferences.get(PREF_DSS_SWITCHPENDING, false))
return;
// Tell the Chrome Registry which Skin to select
try {
this.selectedSkin = Preferences.get(PREF_DSS_SKIN_TO_SELECT);
Services.prefs.setCharPref(PREF_GENERAL_SKINS_SELECTEDSKIN,
this.selectedSkin);
Services.prefs.clearUserPref(PREF_DSS_SKIN_TO_SELECT);
logger.debug("Changed skin to " + this.selectedSkin);
this.currentSkin = this.selectedSkin;
}
catch (e) {
logger.error("Error applying theme change", e);
}
Services.prefs.clearUserPref(PREF_DSS_SWITCHPENDING);
},
/**
* If the application has been upgraded and there are add-ons outside the
* application directory then we may need to synchronize compatibility
* information but only if the mismatch UI isn't disabled.
*
* @returns False if no update check is needed, otherwise an array of add-on
* IDs to check for updates. Array may be empty if no add-ons can be/need
* to be updated, but the metadata check needs to be performed.
*/
shouldForceUpdateCheck: function XPI_shouldForceUpdateCheck(aAppChanged) {
AddonManagerPrivate.recordSimpleMeasure("XPIDB_metadata_age", AddonRepository.metadataAge());
let startupChanges = AddonManager.getStartupChanges(AddonManager.STARTUP_CHANGE_DISABLED);
logger.debug("shouldForceUpdateCheck startupChanges: " + startupChanges.toSource());
AddonManagerPrivate.recordSimpleMeasure("XPIDB_startup_disabled", startupChanges.length);
let forceUpdate = [];
if (startupChanges.length > 0) {
let addons = XPIDatabase.getAddons();
for (let addon of addons) {
if ((startupChanges.indexOf(addon.id) != -1) &&
(addon.permissions() & AddonManager.PERM_CAN_UPGRADE)) {
logger.debug("shouldForceUpdateCheck: can upgrade disabled add-on " + addon.id);
forceUpdate.push(addon.id);
}
}
}
if (AddonRepository.isMetadataStale()) {
logger.debug("shouldForceUpdateCheck: metadata is stale");
return forceUpdate;
}
if (forceUpdate.length > 0) {
return forceUpdate;
}
return false;
},
/**
* Shows the "Compatibility Updates" UI.
*
* @param aAddonIDs
* Array opf addon IDs that were disabled by the application update, and
* should therefore be checked for updates.
*/
showUpgradeUI: function XPI_showUpgradeUI(aAddonIDs) {
logger.debug("XPI_showUpgradeUI: " + aAddonIDs.toSource());
// Flip a flag to indicate that we interrupted startup with an interactive prompt
Services.startup.interrupted = true;
var variant = Cc["@mozilla.org/variant;1"].
createInstance(Ci.nsIWritableVariant);
variant.setFromVariant(aAddonIDs);
// This *must* be modal as it has to block startup.
var features = "chrome,centerscreen,dialog,titlebar,modal";
var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIWindowWatcher);
ww.openWindow(null, URI_EXTENSION_UPDATE_DIALOG, "", features, variant);
// Ensure any changes to the add-ons list are flushed to disk
Services.prefs.setBoolPref(PREF_PENDING_OPERATIONS,
!XPIDatabase.writeAddonsList());
},
/**
* Persists changes to XPIProvider.bootstrappedAddons to its store (a pref).
*/
persistBootstrappedAddons: function XPI_persistBootstrappedAddons() {
// Experiments are disabled upon app load, so don't persist references.
let filtered = {};
for (let id in this.bootstrappedAddons) {
let entry = this.bootstrappedAddons[id];
if (entry.type == "experiment") {
continue;
}
filtered[id] = entry;
}
Services.prefs.setCharPref(PREF_BOOTSTRAP_ADDONS,
JSON.stringify(filtered));
},
/**
* Adds a list of currently active add-ons to the next crash report.
*/
addAddonsToCrashReporter: function XPI_addAddonsToCrashReporter() {
if (!("nsICrashReporter" in Ci) ||
!(Services.appinfo instanceof Ci.nsICrashReporter))
return;
// In safe mode no add-ons are loaded so we should not include them in the
// crash report
if (Services.appinfo.inSafeMode)
return;
let data = this.enabledAddons;
for (let id in this.bootstrappedAddons) {
data += (data ? "," : "") + encodeURIComponent(id) + ":" +
encodeURIComponent(this.bootstrappedAddons[id].version);
}
try {
Services.appinfo.annotateCrashReport("Add-ons", data);
}
catch (e) { }
let TelemetrySession =
Cu.import("resource://gre/modules/TelemetrySession.jsm", {}).TelemetrySession;
TelemetrySession.setAddOns(data);
},
/**
* Check the staging directories of install locations for any add-ons to be
* installed or add-ons to be uninstalled.
*
* @param aManifests
* A dictionary to add detected install manifests to for the purpose
* of passing through updated compatibility information
* @return true if an add-on was installed or uninstalled
*/
processPendingFileChanges: function XPI_processPendingFileChanges(aManifests) {
let changed = false;
this.installLocations.forEach(function(aLocation) {
aManifests[aLocation.name] = {};
// We can't install or uninstall anything in locked locations
if (aLocation.locked)
return;
let stagedXPIDir = aLocation.getXPIStagingDir();
let stagingDir = aLocation.getStagingDir();
if (stagedXPIDir.exists() && stagedXPIDir.isDirectory()) {
let entries = stagedXPIDir.directoryEntries
.QueryInterface(Ci.nsIDirectoryEnumerator);
while (entries.hasMoreElements()) {
let stageDirEntry = entries.nextFile;
if (!stageDirEntry.isDirectory()) {
logger.warn("Ignoring file in XPI staging directory: " + stageDirEntry.path);
continue;
}
// Find the last added XPI file in the directory
let stagedXPI = null;
var xpiEntries = stageDirEntry.directoryEntries
.QueryInterface(Ci.nsIDirectoryEnumerator);
while (xpiEntries.hasMoreElements()) {
let file = xpiEntries.nextFile;
if (file.isDirectory())
continue;
let extension = file.leafName;
extension = extension.substring(extension.length - 4);
if (extension != ".xpi" && extension != ".jar")
continue;
stagedXPI = file;
}
xpiEntries.close();
if (!stagedXPI)
continue;
let addon = null;
try {
addon = loadManifestFromZipFile(stagedXPI);
}
catch (e) {
logger.error("Unable to read add-on manifest from " + stagedXPI.path, e);
continue;
}
logger.debug("Migrating staged install of " + addon.id + " in " + aLocation.name);
if (addon.unpack || Preferences.get(PREF_XPI_UNPACK, false)) {
let targetDir = stagingDir.clone();
targetDir.append(addon.id);
try {
targetDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
}
catch (e) {
logger.error("Failed to create staging directory for add-on " + addon.id, e);
continue;
}
try {
ZipUtils.extractFiles(stagedXPI, targetDir);
}
catch (e