⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nsupdateservice.js

📁 从国外的站点上淘的个人财务管理系统.开源.
💻 JS
📖 第 1 页 / 共 5 页
字号:
      if (programFilesDir.contains(appDir, true)) {        var relativePath = appDir.getRelativeDescriptor(programFilesDir);        var userLocalDir = fileLocator.get(KEY_LOCALDATA,            Components.interfaces.nsILocalFile).parent;        appDir.setRelativeDescriptor(userLocalDir, relativePath);      }    }    catch (e) {}//@line 379 "/c/builds/1813/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in"  }  appDir.append(DIR_UPDATES);  appDir.append("0");  if (!appDir.exists() && !key)    appDir.create(nsILocalFile.DIRECTORY_TYPE, PERMS_DIRECTORY);  return appDir;}/** * Reads the update state from the update.status file in the specified * directory. * @param   dir *          The dir to look for an update.status file in * @returns The status value of the update. */function readStatusFile(dir) {  var statusFile = dir.clone();  statusFile.append(FILE_UPDATE_STATUS);  LOG("General", "Reading Status File: " + statusFile.path);  return readStringFromFile(statusFile) || STATE_NONE;}/** * Writes the current update operation/state to a file in the patch  * directory, indicating to the patching system that operations need * to be performed. * @param   dir *          The patch directory where the update.status file should be  *          written. * @param   state *          The state value to write. */function writeStatusFile(dir, state) {  var statusFile = dir.clone();  statusFile.append(FILE_UPDATE_STATUS);  writeStringToFile(statusFile, state);}/** * Removes the Updates Directory * @param   key *          The Directory Service Key under which update directory resides *          (optional). */function cleanUpUpdatesDir(key) {  // Bail out if we don't have appropriate permissions  var updateDir;  try {    updateDir = getUpdatesDir(key);  }  catch (e) {    return;  }  var e = updateDir.directoryEntries;  while (e.hasMoreElements()) {    var f = e.getNext().QueryInterface(Components.interfaces.nsIFile);    // Preserve the last update log file for debugging purposes    if (f.leafName == FILE_UPDATE_LOG) {      try {        var dir = f.parent.parent;        var logFile = dir.clone();        logFile.append(FILE_LAST_LOG);        if (logFile.exists())          logFile.remove(false);        f.copyTo(dir, FILE_LAST_LOG);      }      catch (e) {        LOG("General", "Failed to copy file: " + f.path);      }    }    // Now, recursively remove this file.  The recusive removal is really    // only needed on Mac OSX because this directory will contain a copy of    // updater.app, which is itself a directory.    try {      f.remove(true);    }    catch (e) {      LOG("General", "Failed to remove file: " + f.path);    }  }  try {    updateDir.remove(false);  } catch (e) {    LOG("General", "Failed to remove update directory: " + updateDir.path +         " - This is almost always bad. Exception = " + e);    throw e;  }}/** * Clean up updates list and the updates directory. * @param   key *          The Directory Service Key under which update directory resides *          (optional). */function cleanupActiveUpdate(key) {  // Move the update from the Active Update list into the Past Updates list.  var um =       Components.classes["@mozilla.org/updates/update-manager;1"].      getService(Components.interfaces.nsIUpdateManager);  um.activeUpdate = null;  um.saveUpdates();  // Now trash the updates directory, since we're done with it  cleanUpUpdatesDir(key);}/** * Gets a preference value, handling the case where there is no default. * @param   func *          The name of the preference function to call, on nsIPrefBranch * @param   preference *          The name of the preference * @param   defaultValue *          The default value to return in the event the preference has  *          no setting * @returns The value of the preference, or undefined if there was no *          user or default value. */function getPref(func, preference, defaultValue) {  try {    return gPref[func](preference);  }  catch (e) {  }  return defaultValue;}/** * Gets the current value of the locale.  It's possible for this preference to * be localized, so we have to do a little extra work here.  Similar code * exists in nsHttpHandler.cpp when building the UA string. */function getLocale() {  try {    return gPref.getComplexValue(PREF_GENERAL_USERAGENT_LOCALE,                                 nsIPrefLocalizedString).data;  } catch (e) {}  return gPref.getCharPref(PREF_GENERAL_USERAGENT_LOCALE);}/** * Read the update channel from defaults only.  We do this to ensure that * the channel is tightly coupled with the application and does not apply * to other instances of the application that may use the same profile. */function getUpdateChannel() {  var channel = "default";  var prefName;  var prefValue;  var defaults =      gPref.QueryInterface(Components.interfaces.nsIPrefService).      getDefaultBranch(null);  try {    channel = defaults.getCharPref(PREF_APP_UPDATE_CHANNEL);  } catch (e) {    // use default when pref not found  }  try {    var partners = gPref.getChildList(PREF_PARTNER_BRANCH, { });    if (partners.length) {      channel += "-cck";      partners.sort();      for each (prefName in partners) {        prefValue = gPref.getCharPref(prefName);        channel += "-" + prefValue;      }    }  }  catch (e) {    Components.utils.reportError(e);  }  return channel;}/** * An enumeration of items in a JS array. * @constructor */function ArrayEnumerator(aItems) {  this._index = 0;  if (aItems) {    for (var i = 0; i < aItems.length; ++i) {      if (!aItems[i])        aItems.splice(i, 1);          }  }  this._contents = aItems;}ArrayEnumerator.prototype = {  _index: 0,  _contents: [],    hasMoreElements: function() {    return this._index < this._contents.length;  },    getNext: function() {    return this._contents[this._index++];        }};/** * 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);}/** * Writes a string of text to a file.  A newline will be appended to the data * written to the file.  This function only works with ASCII text. */function writeStringToFile(file, text) {  var fos =      Components.classes["@mozilla.org/network/safe-file-output-stream;1"].      createInstance(nsIFileOutputStream);  var 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);  text += "\n";  fos.write(text, text.length);      closeSafeOutputStream(fos);}/** * Reads a string of text from a file.  A trailing newline will be removed * before the result is returned.  This function only works with ASCII text. */function readStringFromFile(file) {  var fis =      Components.classes["@mozilla.org/network/file-input-stream;1"].      createInstance(nsIFileInputStream);  var modeFlags = MODE_RDONLY;  if (!file.exists())    return null;  fis.init(file, modeFlags, PERMS_FILE, 0);  var sis =      Components.classes["@mozilla.org/scriptableinputstream;1"].      createInstance(Components.interfaces.nsIScriptableInputStream);  sis.init(fis);  var text = sis.read(sis.available());  sis.close();  if (text[text.length - 1] == "\n")    text = text.slice(0, -1);  return text;}function getObserverService(){  return Components.classes["@mozilla.org/observer-service;1"]                   .getService(Components.interfaces.nsIObserverService);}/** * Update Patch * @param   patch *          A <patch> element to initialize this object with * @throws if patch has a size of 0 * @constructor */function UpdatePatch(patch) {  this._properties = {};  for (var i = 0; i < patch.attributes.length; ++i) {    var attr = patch.attributes.item(i);    attr.QueryInterface(Components.interfaces.nsIDOMAttr);    switch (attr.name) {    case "selected":      this.selected = attr.value == "true";      break;    case "size":      if (0 == parseInt(attr.value)) {        LOG("UpdatePatch", "0-sized patch!");        throw Components.results.NS_ERROR_ILLEGAL_VALUE;      }    default:      this[attr.name] = attr.value;      break;    };  }}UpdatePatch.prototype = {  /**   * See nsIUpdateService.idl   */  serialize: function(updates) {    var patch = updates.createElementNS(URI_UPDATE_NS, "patch");    patch.setAttribute("type", this.type);    patch.setAttribute("URL", this.URL);    patch.setAttribute("hashFunction", this.hashFunction);    patch.setAttribute("hashValue", this.hashValue);    patch.setAttribute("size", this.size);    patch.setAttribute("selected", this.selected);    patch.setAttribute("state", this.state);        for (var p in this._properties) {      if (this._properties[p].present)        patch.setAttribute(p, this._properties[p].data);    }        return patch;   },    /**   * A hash of custom properties   */  _properties: null,    /**   * See nsIWritablePropertyBag.idl   */  setProperty: function(name, value) {    this._properties[name] = { data: value, present: true };  },    /**   * See nsIWritablePropertyBag.idl   */  deleteProperty: function(name) {    if (name in this._properties)      this._properties[name].present = false;    else      throw Components.results.NS_ERROR_FAILURE;  },    /**   * See nsIPropertyBag.idl   */  get enumerator() {    var properties = [];    for (var p in this._properties)      properties.push(this._properties[p].data);    return new ArrayEnumerator(properties);  },    /**   * See nsIPropertyBag.idl   */  getProperty: function(name) {    if (name in this._properties &&        this._properties[name].present)      return this._properties[name].data;    throw Components.results.NS_ERROR_FAILURE;  },    /**   * Returns whether or not the update.status file for this patch exists at the    * appropriate location.    */  get statusFileExists() {    var statusFile = getUpdatesDir();    statusFile.append(FILE_UPDATE_STATUS);    return statusFile.exists();  },    /**   * See nsIUpdateService.idl   */  get state() {    if (!this.statusFileExists)      return STATE_NONE;    return this._properties.state;  },  set state(val) {    this._properties.state = val;  },    /**   * See nsISupports.idl   */  QueryInterface: function(iid) {    if (!iid.equals(Components.interfaces.nsIUpdatePatch) &&        !iid.equals(Components.interfaces.nsIPropertyBag) &&         !iid.equals(Components.interfaces.nsIWritablePropertyBag) &&         !iid.equals(Components.interfaces.nsISupports))      throw Components.results.NS_ERROR_NO_INTERFACE;    return this;  }};/** * Update * Implements nsIUpdate * @param   update *          An <update> element to initialize this object with * @throws if the update contains no patches

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -