📄 nsextensionmanager.js
字号:
* Construct a nsIUpdateItem with the supplied metadata * ... */function makeItem(id, version, locationKey, minVersion, maxVersion, name, updateURL, updateHash, iconURL, updateRDF, type) { var item = Components.classes["@mozilla.org/updates/item;1"] .createInstance(Components.interfaces.nsIUpdateItem); item.init(id, version, locationKey, minVersion, maxVersion, name, updateURL, updateHash, iconURL, updateRDF, type); return item;}/** * Gets the specified directory at the speciifed hierarchy under a * Directory Service key. * @param key * The Directory Service Key to start from * @param pathArray * An array of path components to locate beneath the directory * specified by |key| * @return nsIFile object for the location specified. If the directory * requested does not exist, it is created, along with any * parent directories that need to be created. */function getDir(key, pathArray) { return getDirInternal(key, pathArray, true);}/** * Gets the specified directory at the speciifed hierarchy under a * Directory Service key. * @param key * The Directory Service Key to start from * @param pathArray * An array of path components to locate beneath the directory * specified by |key| * @return nsIFile object for the location specified. If the directory * requested does not exist, it is NOT created. */function getDirNoCreate(key, pathArray) { return getDirInternal(key, pathArray, false);}/** * Gets the specified directory at the speciifed hierarchy under a * Directory Service key. * @param key * The Directory Service Key to start from * @param pathArray * An array of path components to locate beneath the directory * specified by |key| * @param shouldCreate * true if the directory hierarchy specified in |pathArray| * should be created if it does not exist, * false otherwise. * @return nsIFile object for the location specified. */function getDirInternal(key, pathArray, shouldCreate) { var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties); var dir = fileLocator.get(key, nsILocalFile); for (var i = 0; i < pathArray.length; ++i) { dir.append(pathArray[i]); if (shouldCreate && !dir.exists()) dir.create(nsILocalFile.DIRECTORY_TYPE, PERMS_DIRECTORY); } dir.followLinks = false; return dir;}/** * Gets the file at the speciifed hierarchy under a Directory Service key. * @param key * The Directory Service Key to start from * @param pathArray * An array of path components to locate beneath the directory * specified by |key|. The last item in this array must be the * leaf name of a file. * @return nsIFile object for the file specified. The file is NOT created * if it does not exist, however all required directories along * the way are. */function getFile(key, pathArray) { var file = getDir(key, pathArray.slice(0, -1)); file.append(pathArray[pathArray.length - 1]); return file;}/** * Gets the descriptor of a directory as a relative path to common base * directories (profile, user home, app install dir, etc). * * @param itemLocation * The nsILocalFile representing the item's directory. * @param installLocation the nsIInstallLocation for this item */function getDescriptorFromFile(itemLocation, installLocation) { var baseDir = installLocation.location; if (baseDir && baseDir.contains(itemLocation, true)) { return "rel%" + itemLocation.getRelativeDescriptor(baseDir); } return "abs%" + itemLocation.persistentDescriptor;}function getAbsoluteDescriptor(itemLocation) { return itemLocation.persistentDescriptor;}/** * Initializes a Local File object based on a descriptor * provided by "getDescriptorFromFile". * * @param descriptor * The descriptor that locates the directory * @param installLocation * The nsIInstallLocation object for this item. * @returns The nsILocalFile object representing the location of the item */function getFileFromDescriptor(descriptor, installLocation) { var location = Components.classes["@mozilla.org/file/local;1"] .createInstance(nsILocalFile); var m = descriptor.match(/^(abs|rel)\%(.*)$/); if (!m) throw Components.results.NS_ERROR_INVALID_ARG; if (m[1] == "rel") { location.setRelativeDescriptor(installLocation.location, m[2]); } else { location.persistentDescriptor = m[2]; } return location;}/** * Determines if a file is an item package - either a XPI or a JAR file. * @param file * The file to check * @returns true if the file is an item package, false otherwise. */function fileIsItemPackage(file) { var fileURL = getURIFromFile(file); if (fileURL instanceof nsIURL) var extension = fileURL.fileExtension.toLowerCase(); return extension == "xpi" || extension == "jar";}/** * Return the leaf name used by the extension system for staging an item. * @param id * The GUID of the item * @param type * The nsIUpdateItem type of the item * @returns The leaf name of the staged file. */function getStagedLeafName(id, type) { if (type == nsIUpdateItem.TYPE_THEME) return id + ".jar"; return id + ".xpi";}/** * Opens a safe file output stream for writing. * @param file * The file to write to. * @param modeFlags * (optional) File open flags. Can be undefined. * @returns nsIFileOutputStream to write to. */function openSafeFileOutputStream(file, modeFlags) { var fos = Components.classes["@mozilla.org/network/safe-file-output-stream;1"] .createInstance(Components.interfaces.nsIFileOutputStream); if (modeFlags === undefined) modeFlags = MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE; if (!file.exists()) file.create(nsILocalFile.NORMAL_FILE_TYPE, PERMS_FILE); fos.init(file, modeFlags, PERMS_FILE, 0); return fos;}/** * Closes a safe file output stream. * @param stream * The stream to close. */function closeSafeFileOutputStream(stream) { if (stream instanceof Components.interfaces.nsISafeOutputStream) stream.finish(); else stream.close();}/** * Deletes a directory and its children. First it tries nsIFile::Remove(true). * If that fails it will fall back to recursing, setting the appropriate * permissions, and deleting the current entry. This is needed for when we have * rights to delete a directory but there are entries that have a read-only * attribute (e.g. a copy restore from a read-only CD, etc.) * @param dir * A nsIFile for the directory to be deleted */function removeDirRecursive(dir) { try { dir.remove(true); return; } catch (e) { } var dirEntries = dir.directoryEntries; while (dirEntries.hasMoreElements()) { var entry = dirEntries.getNext().QueryInterface(Components.interfaces.nsIFile); if (entry.isDirectory()) { removeDirRecursive(entry); } else { entry.permissions = PERMS_FILE; entry.remove(false); } } dir.permissions = PERMS_DIRECTORY; dir.remove(true);}/** * Logs a string to the error console. * @param string * The string to write to the error console.. */ function LOG(string) { if (gLoggingEnabled) { dump("*** " + string + "\n"); gConsole.logStringMessage(string); }}/** * Randomize the specified file name. Used to force RDF to bypass the cache * when loading certain types of files. * @param fileName * A file name to randomize, e.g. install.rdf * @returns A randomized file name, e.g. install-xyz.rdf */function getRandomFileName(fileName) { var extensionDelimiter = fileName.lastIndexOf("."); var prefix = fileName.substr(0, extensionDelimiter); var suffix = fileName.substr(extensionDelimiter); var characters = "abcdefghijklmnopqrstuvwxyz0123456789"; var nameString = prefix + "-"; for (var i = 0; i < 3; ++i) { var index = Math.round((Math.random()) * characters.length); nameString += characters.charAt(index); } return nameString + "." + suffix;}/** * Get the RDF URI prefix of a nsIUpdateItem type. This function should be used * ONLY to support Firefox 1.0 Update RDF files! Item URIs in the datasource * are NOT prefixed. * @param type * The nsIUpdateItem type to find a RDF URI prefix for * @returns The RDF URI prefix. */function getItemPrefix(type) { if (type & nsIUpdateItem.TYPE_EXTENSION) return PREFIX_EXTENSION; else if (type & nsIUpdateItem.TYPE_THEME) return PREFIX_THEME; return PREFIX_ITEM_URI;}/** * Trims a prefix from a string. * @param string * The source string * @param prefix * The prefix to remove. * @returns The suffix (string - prefix) */function stripPrefix(string, prefix) { return string.substr(prefix.length);}/** * Gets a File URL spec for a nsIFile * @param file * The file to get a file URL spec to * @returns The file URL spec to the file */function getURLSpecFromFile(file) { var ioServ = Components.classes["@mozilla.org/network/io-service;1"] .getService(Components.interfaces.nsIIOService); var fph = ioServ.getProtocolHandler("file") .QueryInterface(Components.interfaces.nsIFileProtocolHandler); return fph.getURLSpecFromFile(file);}/** * Constructs a URI to a spec. * @param spec * The spec to construct a URI to * @returns The nsIURI constructed. */function newURI(spec) { var ioServ = Components.classes["@mozilla.org/network/io-service;1"] .getService(Components.interfaces.nsIIOService); return ioServ.newURI(spec, null, null);}/** * Constructs a File URI to a nsIFile * @param file * The file to construct a File URI to * @returns The file URI to the file */function getURIFromFile(file) { var ioServ = Components.classes["@mozilla.org/network/io-service;1"] .getService(Components.interfaces.nsIIOService); return ioServ.newFileURI(file);}/** * @returns Whether or not we are currently running in safe mode. */function inSafeMode() { return gApp.inSafeMode;}/** * Extract the string value from a RDF Literal or Resource * @param literalOrResource * RDF String Literal or Resource * @returns String value of the literal or resource, or undefined if the object * supplied is not a RDF string literal or resource. */function stringData(literalOrResource) { if (literalOrResource instanceof Components.interfaces.nsIRDFLiteral) return literalOrResource.Value; if (literalOrResource instanceof Components.interfaces.nsIRDFResource) return literalOrResource.Value; return undefined;}/** * Extract the integer value of a RDF Literal * @param literal * nsIRDFInt literal * @return integer value of the literal */function intData(literal) { if (literal instanceof Components.interfaces.nsIRDFInt) return literal.Value; return undefined;}/** * Gets a property from an install manifest. * @param installManifest * An Install Manifest datasource to read from * @param property * The name of a proprety to read (sans EM_NS) * @returns The literal value of the property, or undefined if the property has * no value. */function getManifestProperty(installManifest, property) { var target = installManifest.GetTarget(gInstallManifestRoot, gRDF.GetResource(EM_NS(property)), true); var val = stringData(target); return val === undefined ? intData(target) : val;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -