/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Update Service.
 *
 * The Initial Developer of the Original Code is Ben Goodger.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *  Ben Goodger <ben@mozilla.org> (Original Author)
 *  Darin Fisher <darin@meer.net>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

const PREF_APP_UPDATE_ENABLED             = "app.update.enabled";
const PREF_APP_UPDATE_AUTO                = "app.update.auto";
const PREF_APP_UPDATE_MODE                = "app.update.mode";
const PREF_APP_UPDATE_SILENT              = "app.update.silent";
const PREF_APP_UPDATE_INTERVAL            = "app.update.interval";
const PREF_APP_UPDATE_TIMER               = "app.update.timer";
const PREF_APP_UPDATE_LOG_BRANCH          = "app.update.log.";
const PREF_APP_UPDATE_URL                 = "app.update.url";
const PREF_APP_UPDATE_URL_OVERRIDE        = "app.update.url.override";
const PREF_APP_UPDATE_CHANNEL             = "app.update.channel";
const PREF_APP_UPDATE_SHOW_INSTALLED_UI   = "app.update.showInstalledUI";
const PREF_APP_UPDATE_LASTUPDATETIME_FMT  = "app.update.lastUpdateTime.%ID%";
const PREF_APP_EXTENSIONS_VERSION         = "app.extensions.version";
const PREF_GENERAL_USERAGENT_LOCALE       = "general.useragent.locale";
const PREF_APP_UPDATE_INCOMPATIBLE_MODE   = "app.update.incompatible.mode";

const URI_UPDATE_PROMPT_DIALOG  = "chrome://mozapps/content/update/updates.xul";
const URI_UPDATE_HISTORY_DIALOG = "chrome://mozapps/content/update/history.xul";
const URI_BRAND_PROPERTIES      = "chrome://branding/locale/brand.properties";
const URI_UPDATES_PROPERTIES    = "chrome://mozapps/locale/update/updates.properties";
const URI_UPDATE_NS             = "http://www.mozilla.org/2005/app-update";

const KEY_APPDIR          = "XCurProcD";

const DIR_UPDATES         = "updates";
const FILE_UPDATE_STATUS  = "update.status";
const FILE_UPDATE_ARCHIVE = "update.mar";
const FILE_UPDATE_INFO    = "update.info";
const FILE_UPDATES_DB     = "updates.xml";
const FILE_UPDATE_ACTIVE  = "active-update.xml";
const FILE_PERMS_TEST     = "update.test";

const MODE_RDONLY   = 0x01;
const MODE_WRONLY   = 0x02;
const MODE_CREATE   = 0x08;
const MODE_APPEND   = 0x10;
const MODE_TRUNCATE = 0x20;

const PERMS_FILE      = 0644;
const PERMS_DIRECTORY = 0755;

const STATE_NONE            = null;
const STATE_DOWNLOADING     = "downloading";
const STATE_PENDING         = "pending";
const STATE_APPLYING        = "applying";
const STATE_SUCCEEDED       = "succeeded";
const STATE_DOWNLOAD_FAILED = "download-failed";
const STATE_FAILED          = "failed";

const DOWNLOAD_CHUNK_SIZE           = 65536;
const DOWNLOAD_BACKGROUND_INTERVAL  = 60;  // seconds
const DOWNLOAD_FOREGROUND_INTERVAL  = 0;

// Values for the PREF_APP_UPDATE_INCOMPATIBLE_MODE pref. See documentation in
// code below. 
const INCOMPATIBLE_MODE_NEWVERSIONS   = 0;
const INCOMPATIBLE_MODE_NONEWVERSIONS = 1;

const POST_UPDATE_CONTRACTID = "@mozilla.org/updates/post-update;1";

const nsILocalFile            = Components.interfaces.nsILocalFile;
const nsIUpdateService        = Components.interfaces.nsIUpdateService;
const nsIUpdateItem           = Components.interfaces.nsIUpdateItem;
const nsIPrefLocalizedString  = Components.interfaces.nsIPrefLocalizedString;
const nsIIncrementalDownload  = Components.interfaces.nsIIncrementalDownload;
const nsIFileInputStream      = Components.interfaces.nsIFileInputStream;
const nsIFileOutputStream     = Components.interfaces.nsIFileOutputStream;
const nsICryptoHash           = Components.interfaces.nsICryptoHash;

const Node = Components.interfaces.nsIDOMNode;

var gApp        = null;
var gPref       = null;
var gOS         = null;
var gConsole    = null;
var gLogEnabled = { };

/**
 * Logs a string to the error console. 
 * @param   string
 *          The string to write to the error console..
 */  
function LOG(module, string) {
  if (module in gLogEnabled) {
    dump("*** " + module + ":" + string + "\n");
    gConsole.logStringMessage(string);
  }
}

/**
 * Convert a string containing binary values to hex.
 */
function binaryToHex(input) {
  var result = "";
  for (var i = 0; i < input.length; ++i) {
    var hex = input.charCodeAt(i).toString(16);
    if (hex.length == 1)
      hex = "0" + hex;
    result += hex;
  }
  return result;
}

/**
 * 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);
}

/**
 * Gets the specified directory at the specified 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, Components.interfaces.nsIFile);
  for (var i = 0; i < pathArray.length; ++i) {
    dir.append(pathArray[i]);
    if (shouldCreate && !dir.exists())
      dir.create(nsILocalFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
  }
  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;
}

/**
 * Closes a Safe Output Stream
 * @param   fos
 *          The Safe Output Stream to close
 */
function closeSafeOutputStream(fos) {
  if (fos instanceof Components.interfaces.nsISafeOutputStream) {
    try {
      fos.finish();
    }
    catch (e) {
      fos.close();
    }
  }
  else
    fos.close();
}

/**
 * Returns human readable status text from the updates.propreties bundle
 * based on an error code
 * @param   code
 *          The error code to look up human readable status text for
 * @param   defaultCode
 *          The default code to look up should human readable status text
 *          not exist for |code|
 * @returns A human readable status text string
 */
function getStatusTextFromCode(code, defaultCode) {
  var sbs = 
      Components.classes["@mozilla.org/intl/stringbundle;1"].
      getService(Components.interfaces.nsIStringBundleService);
  var updateBundle = sbs.createBundle(URI_UPDATES_PROPERTIES);
  var reason = updateBundle.GetStringFromName("checker_error-" + defaultCode);
  try {
    reason = updateBundle.GetStringFromName("checker_error-" + code);
    LOG("General", "Transfer Error: " + reason + ", code: " + code);
  }
  catch (e) {
    // Use the default reason
    LOG("General", "Transfer Error: " + reason + ", code: " + defaultCode);
  }
  return reason;
}

/**
 * Get the Active Updates directory
 * @returns The active updates directory, as a nsIFile object
 */
function getUpdatesDir() {
  // Right now, we only support downloading one patch at a time, so we always
  // use the same target directory.
  var fileLocator =
      Components.classes["@mozilla.org/file/directory_service;1"].
      getService(Components.interfaces.nsIProperties);
  var appDir = fileLocator.get(KEY_APPDIR, Components.interfaces.nsIFile);
  appDir.append(DIR_UPDATES);
  appDir.append("0");
  if (!appDir.exists())
    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;
}

/**
 * Removes the Updates Directory
 */
function cleanUpUpdatesDir() {
  var updateDir = getUpdatesDir();
  
  var e = updateDir.directoryEntries;
  while (e.hasMoreElements()) {
    var f = e.getNext().QueryInterface(Components.interfaces.nsIFile);
    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.
 */
function cleanupActiveUpdate() {
  // 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();
}

/**
 * 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);
}

/**
 * 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;
}

/**
 * Update Patch
 * @param   patch
 *          A <patch> element to initialize this object with
 * @constructor
 */
function UpdatePatch(patch) {
  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;
    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: { },
  
  /**
   * 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
 * @constructor
 */
function Update(update) {
  this._patches = [];
  this.installDate = 0;
  this.isCompleteUpdate = false;

  // Null <update>, assume this is a message container and do no 
  // further initialization
  if (!update)
    return;
    
  for (var i = 0; i < update.childNodes.length; ++i) {
    var patchElement = update.childNodes.item(i);
    if (patchElement.nodeType != Node.ELEMENT_NODE ||
        patchElement.localName != "patch")
      continue;

    patchElement.QueryInterface(Components.interfaces.nsIDOMElement);
    this._patches.push(new UpdatePatch(patchElement));
  }

  for (var i = 0; i < update.attributes.length; ++i) {
    var attr = update.attributes.item(i);
    attr.QueryInterface(Components.interfaces.nsIDOMAttr);
    if (attr.name == "installDate" && attr.value) 
      this.installDate = parseInt(attr.value);
    else if (attr.name == "isCompleteUpdate")
      this.isCompleteUpdate = attr.value == "true";
    else if (attr.name == "isSecurityUpdate")
      this.isSecurityUpdate = attr.value == "true";
    else
      this[attr.name] = attr.value;
  }
  
  // The Update Name is either the string provided by the <update> element, or
  // the string: "<App Name> <Update App Version>"
  var name = "";
  if (update.hasAttribute("name"))
    name = update.getAttribute("name");
  else {
    var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
                        .getService(Components.interfaces.nsIStringBundleService);
    var brandBundle = sbs.createBundle(URI_BRAND_PROPERTIES);
    var updateBundle = sbs.createBundle(URI_UPDATES_PROPERTIES);
    var appName = brandBundle.GetStringFromName("brandShortName");
    name = updateBundle.formatStringFromName("updateName", 
                                             [appName, this.version], 2);
  }
  this.name = name;
}
Update.prototype = {
  /**
   * See nsIUpdateService.idl
   */
  get patchCount() {
    return this._patches.length;
  },
  
  /**
   * See nsIUpdateService.idl
   */
  getPatchAt: function(index) {
    return this._patches[index];
  },

  /**
   * See nsIUpdateService.idl
   * 
   * We use a copy of the state cached on this object in |_state| only when 
   * there is no selected patch, i.e. in the case when we could not load 
   * |.activeUpdate| from the update manager for some reason but still have
   * the update.status file to work with. 
   */
  _state: "",
  set state(state) {
    if (this.selectedPatch)
      this.selectedPatch.state = state;
    this._state = state;
    return state;
  },
  get state() {
    if (this.selectedPatch)
      return this.selectedPatch.state;
    return this._state;
  },
    
  /**
   * See nsIUpdateService.idl
   */
  get selectedPatch() {
    for (var i = 0; i < this.patchCount; ++i) {
      if (this._patches[i].selected)
        return this._patches[i];
    }
    return null;
  },
  
  /**
   * See nsIUpdateService.idl
   */
  serialize: function(updates) {
    var update = updates.createElementNS(URI_UPDATE_NS, "update");
    update.setAttribute("type", this.type);
    update.setAttribute("name", this.name);
    update.setAttribute("version", this.version);
    update.setAttribute("extensionVersion", this.extensionVersion);
    update.setAttribute("detailsURL", this.detailsURL);
    update.setAttribute("licenseURL", this.licenseURL);
    update.setAttribute("serviceURL", this.serviceURL);
    update.setAttribute("installDate", this.installDate);
    update.setAttribute("statusText", this.statusText);
    update.setAttribute("isCompleteUpdate", this.isCompleteUpdate);
    updates.documentElement.appendChild(update);
    
    for (var p in this._properties) {
      if (this._properties[p].present)
        update.setAttribute(p, this._properties[p].data);
    }
    
    for (var i = 0; i < this.patchCount; ++i)
      update.appendChild(this.getPatchAt(i).serialize(updates));
    
    return update;
  },
   
  /**
   * A hash of custom properties
   */
  _properties: { },
  
  /**
   * 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;
  },
  
  /**
   * See nsISupports.idl
   */
  QueryInterface: function(iid) {
    if (!iid.equals(Components.interfaces.nsIUpdate) &&
        !iid.equals(Components.interfaces.nsIPropertyBag) &&
        !iid.equals(Components.interfaces.nsIWritablePropertyBag) &&
        !iid.equals(Components.interfaces.nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
}; 
 
/**
 * UpdateService
 * A Service for managing the discovery and installation of software updates.
 * @constructor
 */
function UpdateService() {
  gApp  = Components.classes["@mozilla.org/xre/app-info;1"]
                    .getService(Components.interfaces.nsIXULAppInfo)
                    .QueryInterface(Components.interfaces.nsIXULRuntime);
  gPref = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefBranch2);
  gOS   = Components.classes["@mozilla.org/observer-service;1"]
                    .getService(Components.interfaces.nsIObserverService);
  gConsole = Components.classes["@mozilla.org/consoleservice;1"]
                       .getService(Components.interfaces.nsIConsoleService);  

  // Start the update timer only after a profile has been selected so that the
  // appropriate values for the update check are read from the user's profile.  
  gOS.addObserver(this, "profile-after-change", false);

  // Observe xpcom-shutdown to unhook pref branch observers above to avoid 
  // shutdown leaks.
  gOS.addObserver(this, "xpcom-shutdown", false);
}

UpdateService.prototype = {
  /**
   * The downloader we are using to download updates. There is only ever one of
   * these.
   */
  _downloader: null,

  /**
   * Handle Observer Service notifications
   * @param   subject
   *          The subject of the notification
   * @param   topic
   *          The notification name
   * @param   data
   *          Additional data
   */
  observe: function(subject, topic, data) {
    switch (topic) {
    case "profile-after-change":
      gOS.removeObserver(this, "profile-after-change");
      this._start();
      break;
    case "xpcom-shutdown":
      gOS.removeObserver(this, "xpcom-shutdown");
      
      // Release Services
      gApp      = null;
      gPref     = null;
      gOS       = null;
      gConsole  = null;
      break;
    }
  },
  
  /**
   * Start the Update Service
   */
  _start: function() {
    // Start logging
    this._initLoggingPrefs();
    
    // Clean up any extant updates
    this._postUpdateProcessing();

    // Register a background update check timer
    var tm = 
        Components.classes["@mozilla.org/updates/timer-manager;1"]
                  .getService(Components.interfaces.nsIUpdateTimerManager);
    var interval = getPref("getIntPref", PREF_APP_UPDATE_INTERVAL, 86400000);
    tm.registerTimer("background-update-timer", this, interval);

    // Resume fetching...
    var um = Components.classes["@mozilla.org/updates/update-manager;1"]
                        .getService(Components.interfaces.nsIUpdateManager);
    var activeUpdate = um.activeUpdate;
    if (activeUpdate) {
      var status = this.downloadUpdate(activeUpdate, true);
      if (status == STATE_NONE)
        cleanupActiveUpdate();
    }
  },
  
  /**
   * Perform post-processing on updates lingering in the updates directory
   * from a previous browser session - either report install failures (and
   * optionally attempt to fetch a different version if appropriate) or 
   * notify the user of install success.
   */
  _postUpdateProcessing: function() {
    // Detect installation failures and notify
    var status = readStatusFile(getUpdatesDir()); 
    if (status == STATE_DOWNLOADING) {
      LOG("UpdateService", "_postUpdateProcessing: Downloading patch, resuming...");
    }
    else if (status != null) {
      // null status means the update.status file is not present, because either:
      // 1) no update was performed, and so there's no UI to show
      // 2) an update was attempted but failed during checking, transfer or 
      //    verification, and was cleaned up at that point, and UI notifying of
      //    that error was shown at that stage. 
      var um = 
          Components.classes["@mozilla.org/updates/update-manager;1"].
          getService(Components.interfaces.nsIUpdateManager);
      var prompter = 
          Components.classes["@mozilla.org/updates/update-prompt;1"].
          createInstance(Components.interfaces.nsIUpdatePrompt);

      var shouldCleanup = true;
      var update = um.activeUpdate;
      if (!update)
        update = new Update(null);
      update.state = status;
      var sbs = 
          Components.classes["@mozilla.org/intl/stringbundle;1"].
          getService(Components.interfaces.nsIStringBundleService);
      var bundle = sbs.createBundle(URI_UPDATES_PROPERTIES);
      if (status == STATE_SUCCEEDED) {
        update.statusText = bundle.GetStringFromName("installSuccess");
        LOG("UpdateService", "_postUpdateProcessing: Install Succeeded, Showing UI");
        prompter.showUpdateInstalled(update);

        // Perform platform-specific post-update processing.
        if (POST_UPDATE_CONTRACTID in Components.classes) {
          Components.classes[POST_UPDATE_CONTRACTID].
              createInstance(Components.interfaces.nsIRunnable).run();
        }
        
        // Done with this update. Clean it up.
        cleanupActiveUpdate();
      }
      else {
        // Something went wrong with the patch application process.
        cleanupActiveUpdate();

        update.statusText = bundle.GetStringFromName("patchApplyFailure");
        var oldType = update.selectedPatch ? update.selectedPatch.type 
                                           : "complete";
        if (update.selectedPatch && oldType == "partial") {
          // Partial patch application failed, try downloading the complete
          // update in the background instead.
          LOG("UpdateService", "_postUpdateProcessing: Install of Partial Patch " + 
              "failed, downloading Complete Patch and maybe showing UI");
          var status = this.downloadUpdate(update, true);
          if (status == STATE_NONE)
            cleanupActiveUpdate();
        }
        else {
          LOG("UpdateService", "_postUpdateProcessing: Install of Complete or " + 
              "only patch failed. Showing error.");
        }
        update.QueryInterface(Components.interfaces.nsIWritablePropertyBag);
        update.setProperty("patchingFailed", oldType);
        prompter.showUpdateError(update);
      }
    }
    else {
      LOG("UpdateService", "_postUpdateProcessing: No Status, No Update");
    }
  },

  /**
   * Initialize Logging preferences, formatted like so:
   *  app.update.log.<moduleName> = <true|false>
   */
  _initLoggingPrefs: function() {
    try {
      var ps = Components.classes["@mozilla.org/preferences-service;1"]
                        .getService(Components.interfaces.nsIPrefService);
      var logBranch = ps.getBranch(PREF_APP_UPDATE_LOG_BRANCH);
      var modules = logBranch.getChildList("", { value: 0 });

      for (var i = 0; i < modules.length; ++i) {
        if (logBranch.prefHasUserValue(modules[i]))
          gLogEnabled[modules[i]] = logBranch.getBoolPref(modules[i]);
      }
    }
    catch (e) {
    }
  },
  
  /**
   *
   */
  _needsToPromptForUpdate: function(updates) {
    // First, check for Extension incompatibilities. These trump any preference
    // settings.
    var em = Components.classes["@mozilla.org/extensions/manager;1"]
                       .getService(Components.interfaces.nsIExtensionManager);
    var incompatibleList = { };
    for (var i = 0; i < updates.length; ++i) {
      var count = {};
      em.getIncompatibleItemList(gApp.ID, updates[i].extensionVersion,
                                 nsIUpdateItem.TYPE_ADDON, false, count);
      if (count.value > 0)
        return true;
    }

    // Now, inspect user preferences.
    
    // No prompt necessary, silently update...
    return false;
  },
  
  /**
   * Notified when a timer fires
   * @param   timer
   *          The timer that fired
   */
  notify: function(timer) {
    // If a download is in progress, then do nothing.
    if (this.isDownloading || this._downloader && this._downloader.patchIsStaged)
      return;

    var self = this;
    var listener = {
      /**
       * See nsIUpdateService.idl
       */
      onProgress: function(request, position, totalSize) { 
      },
      
      /**
       * See nsIUpdateService.idl
       */
      onCheckComplete: function(request, updates, updateCount) {
        self._selectAndInstallUpdate(updates);
      },

      /**
       * See nsIUpdateService.idl
       */
      onError: function(request, update) { 
        LOG("Checker", "Error during background update: " + update.statusText);
      },
    }
    this.backgroundChecker.checkForUpdates(listener, false);
  },
  
  /**
   * Determine whether or not an update requires user confirmation before it
   * can be installed.
   * @param   update
   *          The update to be installed
   * @returns true if a prompt UI should be shown asking the user if they want
   *          to install the update, false if the update should just be 
   *          silently downloaded and installed.
   */
  _shouldPrompt: function(update) {
    // There are two possible outcomes here:
    // 1. download and install the update automatically
    // 2. alert the user about the presence of an update before doing anything
    //
    // The outcome we follow is determined as follows:
    // 
    // Update Type      Mode      Incompatible    Outcome
    // Major            0         Yes or No       Auto Install
    // Major            1         No              Auto Install
    // Major            1         Yes             Notify and Confirm
    // Major            2         Yes or No       Notify and Confirm
    // Minor            0         Yes or No       Auto Install
    // Minor            1         No              Auto Install
    // Minor            1         Yes             Notify and Confirm
    // Minor            2         No              Auto Install
    // Minor            2         Yes             Notify and Confirm
    //
    // In addition, if there is a license associated with an update, regardless
    // of type it must be agreed to. 
    //
    // If app.update.enabled is set to false, an update check is not performed
    // at all, and so none of the decision making above is entered into.
    //
    update.QueryInterface(Components.interfaces.nsIPropertyBag);
    try {
      var licenseAccepted = update.getProperty("licenseAccepted") == "true";
    }
    catch (e) {
      licenseAccepted = false;
    }
    if (update.licenseURL && !licenseAccepted) {
      LOG("Checker", "_shouldPrompt: Prompting because of an un-accepted " + 
          "license");
      return true;
    }
    
    var updateEnabled = getPref("getBoolPref", PREF_APP_UPDATE_ENABLED, true);
    if (!updateEnabled) {
      LOG("Checker", "_shouldPrompt: Not prompting because update is " + 
          "disabled");
      return false;
    }
    
    // User has turned off automatic download and install
    var autoEnabled = getPref("getBoolPref", PREF_APP_UPDATE_AUTO, true);
    if (!autoEnabled) {
      LOG("Checker", "_shouldPrompt: Prompting because auto install is disabled");
      return true;
    }
    
    switch (getPref("getIntPref", PREF_APP_UPDATE_MODE, 1)) {
    case 1:
      // Mode 1 is do not prompt only if there are no incompatibilities
      // releases
      LOG("Checker", "_shouldPrompt: Prompting if there are incompatibilities");
      return !isCompatible(update);
    case 2:
      // Mode 2 is do not prompt only if there are no incompatibilities for 
      // minor updates only
      LOG("Checker", "_shouldPrompt: Prompting if major or there are " + 
          "incompatibilities");
      return update.type == "minor" ? !isCompatible(update) : true;
    }
    // Mode 0 is do not prompt regardless of incompatibilities
    LOG("Checker", "_shouldPrompt: Not prompting the user - they choose to " +
        "ignore incompatibilities");
    return false;
  },
  
  /**
   * Determine which of the specified updates should be installed.
   * @param   updates
   *          An array of available updates
   */
  selectUpdate: function(updates) {
    if (updates.length == 0)
      return null;
    
    // Choose the newest of the available minor and major updates. 
    var majorUpdate = null, minorUpdate = null;
    var newestMinor = updates[0], newestMajor = updates[0];

    var vc = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
                       .getService(Components.interfaces.nsIVersionComparator);
    for (var i = 0; i < updates.length; ++i) {
      if (updates[i].type == "major" && 
          vc.compare(newestMajor.version, updates[i].version) <= 0)
        majorUpdate = newestMajor = updates[i];
      if (updates[i].type == "minor" && 
          vc.compare(newestMinor.version, updates[i].version) <= 0)
        minorUpdate = newestMinor = updates[i];
    }

    // If there's a major update, always try and fetch that one first, 
    // otherwise fall back to the newest minor update.
    return majorUpdate || minorUpdate;
  },
  
  /**
   * Determine which of the specified updates should be installed and
   * begin the download/installation process, optionally prompting the
   * user for permission if required.
   * @param   updates
   *          An array of available updates
   */
  _selectAndInstallUpdate: function(updates) {
    // Don't prompt if there's an active update - the user is already 
    // aware and is downloading, or performed some user action to prevent
    // notification.
    var um = Components.classes["@mozilla.org/updates/update-manager;1"]
                       .getService(Components.interfaces.nsIUpdateManager);
    if (um.activeUpdate)
      return;
    
    var update = this.selectUpdate(updates, updates.length);
    if (!update)
      return;
    
    if (this._shouldPrompt(update))
      showPromptIfNoIncompatibilities(update);
    else {
      LOG("UpdateService", "_selectAndInstallUpdate: No need to show prompt, just download update");
      var status = this.downloadUpdate(update, true);
      if (status == STATE_NONE)
        cleanupActiveUpdate();
    }
  },

  /**
   * The Checker used for background update checks.
   */
  _backgroundChecker: null,
  
  /**
   * See nsIUpdateService.idl
   */
  get backgroundChecker() {
    if (!this._backgroundChecker) 
      this._backgroundChecker = new Checker();
    return this._backgroundChecker;
  },
  
  /**
   * See nsIUpdateService.idl
   */
  get canUpdate() {
    try {
      var file = getFile(KEY_APPDIR, [FILE_PERMS_TEST]);
      if (!file.exists()) {
        file.create(nsILocalFile.NORMAL_FILE_TYPE, PERMS_FILE);
        file.remove(false);
      }
    }
    catch (e) {
      // No write privileges to install directory
      return false;
    }
    // If the administrator has locked the app update functionality 
    // OFF - this is not just a user setting, so disable the manual
    // UI too.
    var enabled = getPref("getBoolPref", PREF_APP_UPDATE_ENABLED, true);
    if (!enabled && gPref.prefIsLocked(PREF_APP_UPDATE_ENABLED))
      return false;
    return true;
  },
  
  /**
   * See nsIUpdateService.idl
   */
  addDownloadListener: function(listener) {
    if (!this._downloader) {
      LOG("UpdateService", "addDownloadListener: no downloader!\n");
      return;
    }
    this._downloader.addDownloadListener(listener);
  },
  
  /**
   * See nsIUpdateService.idl
   */
  removeDownloadListener: function(listener) {
    if (!this._downloader) {
      LOG("UpdateService", "removeDownloadListener: no downloader!\n");
      return;
    }
    this._downloader.removeDownloadListener(listener);
  },
  
  /**
   * See nsIUpdateService.idl
   */
  downloadUpdate: function(update, background) {
    if (!update)
      throw Components.results.NS_ERROR_NULL_POINTER;
    if (this.isDownloading) {
      if (update.isCompleteUpdate == this._downloader.isCompleteUpdate &&
          background == this._downloader.background) {
        LOG("UpdateService", "no support for downloading more than one update at a time");
        return readStatusFile(getUpdatesDir());
      }
      this._downloader.cancel();
    }
    this._downloader = new Downloader(background);
    return this._downloader.downloadUpdate(update);
  },
  
  /**
   * See nsIUpdateService.idl
   */
  pauseDownload: function() {
    if (this.isDownloading)
      this._downloader.cancel();
  },
  
  /**
   * See nsIUpdateService.idl
   */
  get isDownloading() {
    return this._downloader && this._downloader.isBusy;
  },
  
  /**
   * See nsISupports.idl
   */
  QueryInterface: function(iid) {
    if (!iid.equals(Components.interfaces.nsIApplicationUpdateService) &&
        !iid.equals(Components.interfaces.nsITimerCallback) && 
        !iid.equals(Components.interfaces.nsIObserver) && 
        !iid.equals(Components.interfaces.nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

/**
 * A service to manage active and past updates.
 * @constructor
 */
function UpdateManager() {
  // Ensure the Active Update file is loaded
  var updates = this._loadXMLFileIntoArray(getFile(KEY_APPDIR, 
    [FILE_UPDATE_ACTIVE]));
  if (updates.length > 0)
    this._activeUpdate = updates[0];
}
UpdateManager.prototype = {
  /**
   * All previously downloaded and installed updates, as an array of nsIUpdate
   * objects.
   */
  _updates: null,
  
  /**
   * The current actively downloading/installing update, as a nsIUpdate object.
   */
  _activeUpdate: null,
  
  /**
   * Loads an updates.xml formatted file into an array of nsIUpdate items.
   * @param   file
   *          A nsIFile for the updates.xml file
   * @returns The array of nsIUpdate items held in the file.
   */
  _loadXMLFileIntoArray: function(file) {
    if (!file.exists()) {
      LOG("UpdateManager", "_loadXMLFileIntoArray: XML File does not exist");
      return [];
    }

    var result = [];
    var fileStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
                               .createInstance(Components.interfaces.nsIFileInputStream);
    fileStream.init(file, MODE_RDONLY, PERMS_FILE, 0);
    try {
      var sis =
          Components.classes["@mozilla.org/scriptableinputstream;1"].
          createInstance(Components.interfaces.nsIScriptableInputStream);
      sis.init(fileStream);
      var data = sis.read(sis.available());
      var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
                            .createInstance(Components.interfaces.nsIDOMParser);
      var doc = parser.parseFromString(data, "text/xml");
      
      var updateCount = doc.documentElement.childNodes.length;
      for (var i = 0; i < updateCount; ++i) {
        var updateElement = doc.documentElement.childNodes.item(i);
        if (updateElement.nodeType != Node.ELEMENT_NODE ||
            updateElement.localName != "update")
          continue;

        updateElement.QueryInterface(Components.interfaces.nsIDOMElement);
        result.push(new Update(updateElement));
      }
    }
    catch (e) {
      LOG("UpdateManager", "_loadXMLFileIntoArray: Error constructing update list " + 
          e);
    }
    fileStream.close();
    return result;
  },
  
  /**
   * Load the update manager, initializing state from state files.
   */
  _ensureUpdates: function() {
    if (!this._updates)
      this._updates = this._loadXMLFileIntoArray(getFile(KEY_APPDIR, 
                        [FILE_UPDATES_DB]));
  },

  /**
   * See nsIUpdateService.idl
   */
  getUpdateAt: function(index) {
    this._ensureUpdates();
    return this._updates[index];
  },
  
  /**
   * See nsIUpdateService.idl
   */
  get updateCount() {
    this._ensureUpdates();
    return this._updates.length;
  },
  
  /**
   * See nsIUpdateService.idl
   */
  get activeUpdate() {
    if (this._activeUpdate &&
        this._activeUpdate.serviceURL != Checker.prototype.updateURL) {
      // User switched channels, clear out any old active updates and remove
      // partial downloads
      this._activeUpdate = null;
      
      // Destroy the updates directory, since we're done with it.
      cleanUpUpdatesDir();
    }
    return this._activeUpdate;
  },
  set activeUpdate(activeUpdate) {
    this._addUpdate(this._activeUpdate);
    this._activeUpdate = activeUpdate;
    if (!activeUpdate) {
      // If |activeUpdate| is null, we have updated both lists - the active list
      // and the history list, so we want to write both files.
      this.saveUpdates();
    }
    else
      this._writeUpdatesToXMLFile([this._activeUpdate], 
                                  getFile(KEY_APPDIR, [FILE_UPDATE_ACTIVE]));
    return activeUpdate;
  },
  
  /**
   * Add an update to the Updates list. If the item already exists in the list,
   * replace the existing value with the new value.
   * @param   update
   *          The nsIUpdate object to add.
   */
  _addUpdate: function(update) {
    if (!update)
      return;
    if (this._updates) {
      for (var i = 0; i < this._updates.length; ++i) {
        if (this._updates[i].version == update.version &&
            this._updates[i].buildID == update.buildID) {
          // Replace the existing entry with the new value, updating
          // all metadata.
          this._updates[i] = update;
          return;
        }
      }
    }
    // Otherwise add it to the front of the list.
    if (this._activeUpdate) 
      this._updates = [this._activeUpdate].concat(this._updates);
  },
  
  /**
   * Serializes an array of updates to an XML file
   * @param   updates
   *          An array of nsIUpdate objects
   * @param   file
   *          The nsIFile object to serialize to
   */
  _writeUpdatesToXMLFile: function(updates, file) {
    var fos = Components.classes["@mozilla.org/network/safe-file-output-stream;1"]
                        .createInstance(Components.interfaces.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);
    
    try {
      var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
                            .createInstance(Components.interfaces.nsIDOMParser);
      const EMPTY_UPDATES_DOCUMENT = "<?xml version=\"1.0\"?><updates xmlns=\"http://www.mozilla.org/2005/app-update\"></updates>";
      var doc = parser.parseFromString(EMPTY_UPDATES_DOCUMENT, "text/xml");

      for (var i = 0; i < updates.length; ++i) {
        if (updates[i])
          doc.documentElement.appendChild(updates[i].serialize(doc));
      }

      var serializer = Components.classes["@mozilla.org/xmlextras/xmlserializer;1"]
                                .createInstance(Components.interfaces.nsIDOMSerializer);
      serializer.serializeToStream(doc.documentElement, fos, null);
    }
    catch (e) {
    }
    
    closeSafeOutputStream(fos);
  },

  /**
   * See nsIUpdateService.idl
   */
  saveUpdates: function() {
    this._writeUpdatesToXMLFile([this._activeUpdate], 
                                getFile(KEY_APPDIR, [FILE_UPDATE_ACTIVE]));
    if (this._updates) {
      this._writeUpdatesToXMLFile(this._updates, 
                                  getFile(KEY_APPDIR, [FILE_UPDATES_DB]));
    }
  },
  
  /**
   * See nsISupports.idl
   */
  QueryInterface: function(iid) {
    if (!iid.equals(Components.interfaces.nsIUpdateManager) &&
        !iid.equals(Components.interfaces.nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

/**
 * Checker
 * Checks for new Updates
 * @constructor
 */
function Checker() {
}
Checker.prototype = {
  /**
   * The XMLHttpRequest object that performs the connection.
   */
  _request  : null,
  
  /**
   * The nsIUpdateCheckListener callback
   */
  _callback : null,
  
  /**
   * The URL of the update service XML file to connect to that contains details
   * about available updates.
   */
  get updateURL() {
    var defaults =
        gPref.QueryInterface(Components.interfaces.nsIPrefService).
        getDefaultBranch(null);

    // Use the override URL if specified.
    var url = getPref("getCharPref", PREF_APP_UPDATE_URL_OVERRIDE, null);

    // Otherwise, construct the update URL from component parts.
    if (!url) {
      try {
        url = defaults.getCharPref(PREF_APP_UPDATE_URL);
      } catch (e) {
      }
    }

    if (!url || url == "") {
      LOG("Checker", "Update URL not defined");
      return null;
    }

    // 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() {
      try {
        return defaults.getCharPref(PREF_APP_UPDATE_CHANNEL);
      } catch (e) {
        return "default";  // failover when pref not found
      }
    }

    url = url.replace(/%PRODUCT%/g, gApp.name);
    url = url.replace(/%VERSION%/g, gApp.version);
    url = url.replace(/%BUILD_ID%/g, gApp.appBuildID);
    url = url.replace(/%BUILD_TARGET%/g, gApp.OS + "_" + gApp.XPCOMABI);
    url = url.replace(/%LOCALE%/g, getLocale());
    url = url.replace(/%CHANNEL%/g, getUpdateChannel());
    url = url.replace(/\+/g, "%2B");

    LOG("Checker", "update url: " + url);
    return url;
  },
  
  /**
   * See nsIUpdateService.idl
   */
  checkForUpdates: function(listener, force) {
    if (!listener)
      throw Components.results.NS_ERROR_NULL_POINTER;
      
    if (!this.updateURL || (!this.enabled && !force))
      return;
      
    this._request = 
      Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
      createInstance(Components.interfaces.nsIXMLHttpRequest);
    this._request.open("GET", this.updateURL, true);
    this._request.overrideMimeType("text/xml");
    this._request.setRequestHeader("Cache-Control", "no-cache");
    
    var self = this;
    this._request.onerror     = function(event) { self.onError(event);    };
    this._request.onload      = function(event) { self.onLoad(event);     };
    this._request.onprogress  = function(event) { self.onProgress(event); };

    LOG("Checker", "checkForUpdates: sending request to " + this.updateURL);
    this._request.send(null);
    
    this._callback = listener;
  },
  
  /**
   * When progress associated with the XMLHttpRequest is received.
   * @param   event
   *          The nsIDOMLSProgressEvent for the load.
   */
  onProgress: function(event) {
    LOG("Checker", "onProgress: " + event.position + "/" + event.totalSize);
    this._callback.onProgress(event.target, event.position, event.totalSize);
  },
  
  /**
   * Returns an array of nsIUpdate objects discovered by the update check.
   */
  get _updates() {
    var updatesElement = this._request.responseXML.documentElement;
    if (!updatesElement) {
      LOG("Checker", "get_updates: empty updates document?!");
      return [];
    }

    if (updatesElement.nodeName != "updates") {
      LOG("Checker", "get_updates: unexpected node name!");
      throw "";
    }
    
    var updates = [];
    for (var i = 0; i < updatesElement.childNodes.length; ++i) {
      var updateElement = updatesElement.childNodes.item(i);
      if (updateElement.nodeType != Node.ELEMENT_NODE ||
          updateElement.localName != "update")
        continue;

      updateElement.QueryInterface(Components.interfaces.nsIDOMElement);
      var update = new Update(updateElement);
      update.serviceURL = this.updateURL;
      updates.push(update);
    }

    return updates;
  },
  
  /**
   * The XMLHttpRequest succeeded and the document was loaded.
   * @param   event
   *          The nsIDOMLSEvent for the load
   */
  onLoad: function(event) {
    LOG("Checker", "onLoad: request completed downloading document");
    
    // Analyze the resulting DOM and determine the set of updates to install
    try {
      var updates = this._updates;
      
      LOG("Checker", "Updates available: " + updates.length);
      
      // ... and tell the Update Service about what we discovered.
      this._callback.onCheckComplete(event.target, updates, updates.length);
    }
    catch (e) {
      LOG("Checker", "There was a problem with the update service URL specified, " + 
          "either the XML file was malformed or it does not exist at the location " + 
          "specified. Exception: " + e);
      var update = new Update(null);
      update.statusText = getStatusTextFromCode(404, 404);
      this._callback.onError(event.target, update);
    }
  },
  
  /**
   * There was an error of some kind during the XMLHttpRequest
   * @param   event
   *          The nsIDOMLSEvent for the load
   */
  onError: function(event) {
    LOG("Checker", "onError: error during load");
    
    var request = event.target;
    try {
      var status = request.status;
    }
    catch (e) {
      var req = request.channel.QueryInterface(Components.interfaces.nsIRequest);
      status = req.status;
    }
    
    // If we can't find an error string specific to this status code, 
    // just use the 200 message from above, which means everything 
    // "looks" fine but there was probably an XML error or a bogus file.
    var update = new Update(null);
    update.statusText = getStatusTextFromCode(status, 200);
    this._callback.onError(request, update);
  },
  
  /**
   * Whether or not we are allowed to do update checking.
   */
  _enabled: true,
  
  /**
   * See nsIUpdateService.idl
   */
  get enabled() {
    var aus = 
        Components.classes["@mozilla.org/updates/update-service;1"].
        getService(Components.interfaces.nsIApplicationUpdateService);
    var enabled = getPref("getBoolPref", PREF_APP_UPDATE_ENABLED, true) && 
                  aus.canUpdate && this._enabled;
    return enabled;
  },
  
  /**
   * See nsIUpdateService.idl
   */
  stopChecking: function(duration) {
    // Always stop the current check
    if (this._request)
      this._request.abort();
    
    const nsIUpdateChecker = Components.interfaces.nsIUpdateChecker;
    switch (duration) {
    case nsIUpdateChecker.CURRENT_SESSION:
      this._enabled = false;
      break;
    case nsIUpdateChecker.ANY_CHECKS:
      this._enabled = false;
      gPref.setBoolPref(PREF_APP_UPDATE_ENABLED, this._enabled);
      break;
    }
  },
  
  /**
   * See nsISupports.idl
   */
  QueryInterface: function(iid) {
    if (!iid.equals(Components.interfaces.nsIUpdateChecker) &&
        !iid.equals(Components.interfaces.nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

/**
 * Manages the download of updates
 * @param   background
 *          Whether or not this downloader is operating in background
 *          update mode. 
 * @constructor
 */
function Downloader(background) {
  this.background = background;
}
Downloader.prototype = {
  /**
   * The nsIUpdatePatch that we are downloading
   */
  _patch: null,
  
  /**
   * The nsIUpdate that we are downloading
   */
  _update: null,
  
  /**
   * The nsIIncrementalDownload object handling the download
   */
  _request: null,

  /**
   * Whether or not the update being downloaded is a complete replacement of
   * the user's existing installation or a patch representing the difference
   * between the new version and the previous version.
   */
  isCompleteUpdate: null,

  /**
   * Cancels the active download.
   */  
  cancel: function() {
    if (this._request && 
        this._request instanceof Components.interfaces.nsIRequest) {
      const NS_BINDING_ABORTED = 0x804b0002;
      this._request.cancel(NS_BINDING_ABORTED);
    }
  },

  /**
   * 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.
   */
  _writeStatusFile: function(dir, state) {
    var statusFile = dir.clone();
    statusFile.append(FILE_UPDATE_STATUS);
    writeStringToFile(statusFile, state);
  },

  /**
   * Whether or not a patch has been downloaded and staged for installation.
   */
  get patchIsStaged() {
    return readStatusFile(getUpdatesDir()) == STATE_PENDING;
  },

  /**
   * Verify the downloaded file.  We assume that the download is complete at
   * this point.
   */
  _verifyDownload: function() {
    if (!this._request)
      return false;
      
    var fileStream = Components.classes["@mozilla.org/network/file-input-stream;1"].
        createInstance(nsIFileInputStream);
    fileStream.init(this._request.destination, MODE_RDONLY, PERMS_FILE, 0);

    try {
      var hash = Components.classes["@mozilla.org/security/hash;1"].
          createInstance(nsICryptoHash);
      var hashFunction = nsICryptoHash[this._patch.hashFunction.toUpperCase()];
      if (hashFunction == undefined)
        throw Components.results.NS_ERROR_UNEXPECTED;
      hash.init(hashFunction);
      hash.updateFromStream(fileStream, -1);
      // NOTE: For now, we assume that the format of _patch.hashValue is hex
      // encoded binary (such as what is typically output by programs like
      // sha1sum).  In the future, this may change to base64 depending on how
      // we choose to compute these hashes.
      digest = binaryToHex(hash.finish(false));
    } catch (e) {
      LOG("Downloader", "failed to compute hash of downloaded update archive");
      digest = "";
    }

    fileStream.close();

    return digest == this._patch.hashValue.toLowerCase();
  },

  /**
   * Select the patch to use given the current state of updateDir and the given
   * set of update patches.
   * @param   update
   *          A nsIUpdate object to select a patch from
   * @param   updateDir
   *          A nsIFile representing the update directory
   * @returns A nsIUpdatePatch object to download
   */
  _selectPatch: function(update, updateDir) {
    // Given an update to download, we will always try to download the patch
    // for a partial update over the patch for a full update.

    /**
     * Return the first UpdatePatch with the given type.
     * @param   type
     *          The type of the patch ("complete" or "partial")
     * @returns A nsIUpdatePatch object matching the type specified
     */
    function getPatchOfType(type) {
      for (var i = 0; i < update.patchCount; ++i) {
        var patch = update.getPatchAt(i);
        if (patch && patch.type == type)
          return patch;
      }
      return null;
    }

    // Look to see if any of the patches in the Update object has been
    // pre-selected for download, otherwise we must figure out which one
    // to select ourselves. 
    var selectedPatch = update.selectedPatch;
    
    var state = readStatusFile(updateDir)

    // If this is a patch that we know about, then select it.  If it is a patch
    // that we do not know about, then remove it and use our default logic.
    var useComplete = false;
    if (selectedPatch) {
      LOG("Downloader", "found existing patch [state="+state+"]");
      switch (state) {
      case STATE_DOWNLOADING: 
        LOG("Downloader", "resuming download");
        return selectedPatch;
      case STATE_PENDING:
        LOG("Downloader", "already downloaded and staged");
        return null;
      case STATE_FAILED:
      case STATE_DOWNLOAD_FAILED:
      case STATE_APPLYING:
      case STATE_NONE:
        // Something went wrong when we tried to apply the previous patch.
        // Try the complete patch next time.
        if (update && selectedPatch.type == "partial") {
          useComplete = true;
        } else {
          // This is a pretty fatal error.  Just bail.
          LOG("Downloader", "failed to apply complete patch!");
          this._writeStatusFile(updateDir, STATE_NONE);
          return null;
        }
      }

      selectedPatch = null;
    }
    
    // If we were not able to discover an update from a previous download, we 
    // select the best patch from the given set.
    var partialPatch = getPatchOfType("partial");
    if (!useComplete)
      selectedPatch = partialPatch;
    if (!selectedPatch) {
      if (partialPatch)
        partialPatch.selected = false;
      selectedPatch = getPatchOfType("complete");
    }

    // Restore the updateDir since we may have deleted it.
    updateDir = getUpdatesDir();
    
    selectedPatch.selected = true;
    update.isCompleteUpdate = useComplete;
    
    // Reset the Active Update object on the Update Manager and flush the
    // Active Update DB. 
    var um = Components.classes["@mozilla.org/updates/update-manager;1"]
                       .getService(Components.interfaces.nsIUpdateManager);
    um.activeUpdate = update;

    return selectedPatch;
  },

  /**
   * Whether or not we are currently downloading something.
   */
  get isBusy() {
    return this._request != null;
  },
  
  /**
   * Download and stage the given update.
   * @param   update
   *          A nsIUpdate object to download a patch for. Cannot be null.
   */
  downloadUpdate: function(update) {
    if (!update)
      throw Components.results.NS_ERROR_NULL_POINTER;
    
    var updateDir = getUpdatesDir();

    this._update = update;

    // This function may return null, which indicates that there are no patches
    // to download.
    this._patch = this._selectPatch(update, updateDir);
    if (!this._patch) {
      LOG("Downloader", "no patch to download");
      return readStatusFile(updateDir);
    }
    this.isCompleteUpdate = this._patch.type == "complete";

    var patchFile = updateDir.clone();
    patchFile.append(FILE_UPDATE_ARCHIVE);

    var ios = Components.classes["@mozilla.org/network/io-service;1"].
        getService(Components.interfaces.nsIIOService);
    var uri = ios.newURI(this._patch.URL, null, null);

    this._request =
        Components.classes["@mozilla.org/network/incremental-download;1"].
        createInstance(nsIIncrementalDownload);

    LOG("Downloader", "downloadUpdate: Downloading from " + uri.spec + " to " + 
        patchFile.path);

    var interval = this.background ? DOWNLOAD_BACKGROUND_INTERVAL
                                   : DOWNLOAD_FOREGROUND_INTERVAL;
    this._request.init(uri, patchFile, DOWNLOAD_CHUNK_SIZE, interval);
    this._request.start(this, null);

    this._writeStatusFile(updateDir, STATE_DOWNLOADING);
    this._patch.QueryInterface(Components.interfaces.nsIWritablePropertyBag);
    this._patch.state = STATE_DOWNLOADING;
    var um = Components.classes["@mozilla.org/updates/update-manager;1"]
                       .getService(Components.interfaces.nsIUpdateManager);
    um.saveUpdates();
    return STATE_DOWNLOADING;
  },
  
  /**
   * An array of download listeners to notify when we receive 
   * nsIRequestObserver or nsIProgressEventSink method calls.
   */
  _listeners: [],

  /** 
   * Adds a listener to the download process
   * @param   listener
   *          A download listener, implementing nsIRequestObserver and
   *          nsIProgressEventSink
   */
  addDownloadListener: function(listener) {
    for (var i = 0; i < this._listeners.length; ++i) {
      if (this._listeners[i] == listener)
        return;
    }
    this._listeners.push(listener);
  },
  
  /** 
   * Removes a download listener
   * @param   listener
   *          The listener to remove.
   */
  removeDownloadListener: function(listener) {
    for (var i = 0; i < this._listeners.length; ++i) {
      if (this._listeners[i] == listener) {
        this._listeners.splice(i, 1);
        return;
      }
    }
  },
  
  /**
   * When the async request begins
   * @param   request
   *          The nsIRequest object for the transfer
   * @param   context
   *          Additional data
   */
  onStartRequest: function(request, context) {
    request.QueryInterface(nsIIncrementalDownload);
    LOG("Downloader", "onStartRequest: " + request.URI.spec);
    
    var listenerCount = this._listeners.length;
    for (var i = 0; i < listenerCount; ++i)
      this._listeners[i].onStartRequest(request, context);
  },
  
  /** 
   * When new data has been downloaded
   * @param   request
   *          The nsIRequest object for the transfer
   * @param   context
   *          Additional data
   * @param   progress
   *          The current number of bytes transferred
   * @param   maxProgress
   *          The total number of bytes that must be transferred
   */
  onProgress: function(request, context, progress, maxProgress) {
    request.QueryInterface(nsIIncrementalDownload);
    LOG("Downloader.onProgress", "onProgress: " + request.URI.spec + ", " + progress + "/" + maxProgress);
    
    var listenerCount = this._listeners.length;
    for (var i = 0; i < listenerCount; ++i) {
      var listener = this._listeners[i];
      if (listener instanceof Components.interfaces.nsIProgressEventSink)
        listener.onProgress(request, context, progress, maxProgress);
    }
  },
  
  /** 
   * When we have new status text
   * @param   request
   *          The nsIRequest object for the transfer
   * @param   context
   *          Additional data
   * @param   status
   *          A status code
   * @param   statusText
   *          Human readable version of |status|
   */
  onStatus: function(request, context, status, statusText) {
    request.QueryInterface(nsIIncrementalDownload);
    LOG("Downloader", "onStatus: " + request.URI.spec + " status = " + status + ", text = " + statusText);
    var listenerCount = this._listeners.length;
    for (var i = 0; i < listenerCount; ++i) {
      var listener = this._listeners[i];
      if (listener instanceof Components.interfaces.nsIProgressEventSink)
        listener.onStatus(request, context, status, statusText);
    }
  },
  
  /** 
   * When data transfer ceases
   * @param   request
   *          The nsIRequest object for the transfer
   * @param   context
   *          Additional data
   * @param   status
   *          Status code containing the reason for the cessation.
   */
  onStopRequest: function(request, context, status) {
    request.QueryInterface(nsIIncrementalDownload);
    LOG("Downloader", "onStopRequest: " + request.URI.spec + ", status = " + status);

    var state = this._patch.state;
    var shouldShowPrompt = false;
    var deleteActiveUpdate = false;
    const NS_BINDING_ABORTED = 0x804b0002;
    const NS_ERROR_ABORT = 0x80004004;
    if (Components.isSuccessCode(status)) {
      if (this._verifyDownload()) {
        state = STATE_PENDING;
        
        // We only need to explicitly show the prompt if this is a backround
        // download, since otherwise some kind of UI is already visible and 
        // that UI will notify. 
        if (this.background)
          shouldShowPrompt = true;
        
        // Tell the updater.exe we're ready to apply.
        this._writeStatusFile(getUpdatesDir(), state);
        this._update.installDate = (new Date()).getTime();
        this._update.statusText = "Install Pending";
      } else {
        LOG("Downloader", "onStopRequest: download verification failed");
        state = STATE_DOWNLOAD_FAILED;
        
        var sbs = 
            Components.classes["@mozilla.org/intl/stringbundle;1"].
            getService(Components.interfaces.nsIStringBundleService);
        var updateStrings = sbs.createBundle(URI_UPDATES_PROPERTIES);
        var brandStrings = sbs.createBundle(URI_BRAND_PROPERTIES);
        var brandShortName = brandStrings.GetStringFromName("brandShortName");
        this._update.statusText = updateStrings.
          formatStringFromName("verificationError", [brandShortName], 1);
        
        // TODO: use more informative error code here
        status = Components.results.NS_ERROR_UNEXPECTED;
        
        var message = getStatusTextFromCode("verification_failed", 
          "verification_failed");
        this._update.statusText = message;
        
        if (this._update.isCompleteUpdate)
          deleteActiveUpdate = true;

        // Destroy the updates directory, since we're done with it.
        cleanUpUpdatesDir();
      }
    }
    else if (status != NS_BINDING_ABORTED &&
             status != NS_ERROR_ABORT) {
      LOG("Downloader", "onStopRequest: Non-verification failure");
      // Some sort of other failure, log this in the |statusText| property
      state = STATE_DOWNLOAD_FAILED;
      
      // XXXben - if |request| (The Incremental Download) provided a means
      // for accessing the http channel we could do more here.
      
      const NS_BINDING_FAILED = 2152398849;
      this._update.statusText = getStatusTextFromCode(status, 
        NS_BINDING_FAILED);
      
      // Destroy the updates directory, since we're done with it.
      cleanUpUpdatesDir();
      
      deleteActiveUpdate = true;
    }
    LOG("Downloader", "Setting state to: " + state);
    this._patch.state = state;
    var um = 
        Components.classes["@mozilla.org/updates/update-manager;1"].
        getService(Components.interfaces.nsIUpdateManager);
    if (deleteActiveUpdate) {
      this._update.installDate = (new Date()).getTime();
      um.activeUpdate = null;
    }
    um.saveUpdates();
    
    var listenerCount = this._listeners.length;
    for (var i = 0; i < listenerCount; ++i)
      this._listeners[i].onStopRequest(request, context, status);

    this._request = null;
    
    if (state == STATE_DOWNLOAD_FAILED) {
      if (!this._update.isCompleteUpdate) {
        // If we were downloading a patch and the patch verification phase 
        // failed, log this and then commence downloading the complete update.
        LOG("Downloader", "onStopRequest: Verification of patch failed, downloading complete update");
        this._update.isCompleteUpdate = true;
        var status = this.downloadUpdate(this._update);
        if (status == STATE_NONE)
          cleanupActiveUpdate();
        // This will reset the |.state| property on this._update if a new 
        // download initiates.
      }
      else {
        // In all other failure cases, i.e. we're S.O.L. - no more failing over
        // ...
        
        // If this was ever a foreground download, and now there is no UI active
        // (e.g. because the user closed the download window) and there was an
        // error, we must notify now. Otherwise we can keep the failure to 
        // ourselves since the user won't be expecting it. 
        var fgdl = false;
        try {
          fgdl = this._update.getProperty("foregroundDownload");
        }
        catch (e) {
        }
        if (fgdl == "true") {
          var prompter = 
              Components.classes["@mozilla.org/updates/update-prompt;1"].
              createInstance(Components.interfaces.nsIUpdatePrompt);
          this._update.QueryInterface(Components.interfaces.nsIWritablePropertyBag);
          this._update.setProperty("downloadFailed", "true");
          prompter.showUpdateError(this._update);
        }
      }
    }

    // Do this after *everything* else, since it will likely cause the app 
    // to shut down. 
    if (shouldShowPrompt) {
      // Notify the user that an update has been downloaded and is ready for 
      // installation (i.e. that they should restart the application). We do
      // not notify on failed update attempts.
      var prompter = 
          Components.classes["@mozilla.org/updates/update-prompt;1"].
          createInstance(Components.interfaces.nsIUpdatePrompt);
      prompter.showUpdateDownloaded(this._update);
    }
  },

  /**
   * See nsIInterfaceRequestor.idl
   */
  getInterface: function(iid) {
    // The network request may require proxy authentication, so provide the
    // default nsIAuthPrompt if requested.
    if (iid.equals(Components.interfaces.nsIAuthPrompt)) {
      var prompt =
          Components.classes["@mozilla.org/network/default-auth-prompt;1"].
          createInstance();
      return prompt.QueryInterface(iid);
    }
    Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
    return null;
  },
   
  /**
   * See nsISupports.idl
   */
  QueryInterface: function(iid) {
    if (!iid.equals(Components.interfaces.nsIRequestObserver) &&
        !iid.equals(Components.interfaces.nsIProgressEventSink) &&
        !iid.equals(Components.interfaces.nsIInterfaceRequestor) &&
        !iid.equals(Components.interfaces.nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

/**
 * A manager for update check timers. Manages timers that fire over long 
 * periods of time (e.g. days, weeks).
 * @constructor
 */
function TimerManager() {
  gOS = Components.classes["@mozilla.org/observer-service;1"]
                  .getService(Components.interfaces.nsIObserverService);
  gOS.addObserver(this, "xpcom-shutdown", false);

  const nsITimer = Components.interfaces.nsITimer;
  this._timer = Components.classes["@mozilla.org/timer;1"]
                          .createInstance(nsITimer);
  var timerInterval = getPref("getIntPref", PREF_APP_UPDATE_TIMER, 600000);
  this._timer.initWithCallback(this, timerInterval, 
                               nsITimer.TYPE_REPEATING_SLACK);
}
TimerManager.prototype = {
  /**
   * See nsIObserver.idl
   */
  observe: function(subject, topic, data) {
    if (topic == "xpcom-shutdown") {
      gOS.removeObserver(this, "xpcom-shutdown");

      // Release everything we hold onto. 
      this._timer = null;
      this._timers = null;
    }
  },

  /**
   * The Checker Timer
   */
  _timer: null,
  
  /**
   * The set of registered timers.
   */
  _timers: { },
  
  /**
   * Called when the checking timer fires.
   * @param   timer
   *          The checking timer that fired. 
   */
  notify: function(timer) {
    for (var timerID in this._timers) {
      var timerData = this._timers[timerID];
      var lastUpdateTime = timerData.lastUpdateTime;
      var now = Math.round(Date.now() / 1000);
    
      // Fudge the lastUpdateTime by some random increment of the update 
      // check interval (e.g. some random slice of 10 minutes) so that when
      // the time comes to check, we offset each client request by a random
      // amount so they don't all hit at once.
      lastUpdateTime += Math.round(Math.random() * timerData.interval);

      if ((now - lastUpdateTime) > timerData.interval &&
          timerData.callback instanceof Components.interfaces.nsITimerCallback) {
        timerData.callback.notify(timer);
        timerData.lastUpdateTime = now;
        var preference = PREF_APP_UPDATE_LASTUPDATETIME_FMT.replace(/%ID%/, timerID);
        gPref.setIntPref(preference, now);
      }
    }
  },
  
  /**
   * See nsIUpdateService.idl
   */
  registerTimer: function(id, callback, interval) {
    var preference = PREF_APP_UPDATE_LASTUPDATETIME_FMT.replace(/%ID%/, id);
    var lastUpdateTime = getPref("getIntPref", preference, 
                                 Math.round(Date.now() / 1000));
    this._timers[id] = { callback       : callback, 
                         interval       : interval,
                         lastUpdateTime : lastUpdateTime }; 
  },

  /**
   * See nsISupports.idl
   */
  QueryInterface: function(iid) {
    if (!iid.equals(Components.interfaces.nsIUpdateTimerManager) &&
        !iid.equals(Components.interfaces.nsITimerCallback) &&
        !iid.equals(Components.interfaces.nsIObserver) &&
        !iid.equals(Components.interfaces.nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

//@line 2309 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8/WINNT_5.2_Depend/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in"
/**
 * UpdatePrompt
 * An object which can prompt the user with information about updates, request
 * action, etc. Embedding clients can override this component with one that 
 * invokes a native front end. 
 * @constructor
 */
function UpdatePrompt() {
}
UpdatePrompt.prototype = {
  /**
   * See nsIUpdateService.idl
   */
  checkForUpdates: function() {
    this._showUI(null, URI_UPDATE_PROMPT_DIALOG, null, "Update:Wizard", 
                 null, null);
  },
    
  /**
   * See nsIUpdateService.idl
   */
  showUpdateAvailable: function(update) {
    if (this._enabled) {
      this._showUI(null, URI_UPDATE_PROMPT_DIALOG, null, "Update:Wizard", 
                   "updatesavailable", update);
    }
  },
  
  /**
   * See nsIUpdateService.idl
   */
  showUpdateDownloaded: function(update) {
    if (this._enabled) {
      this._showUI(null, URI_UPDATE_PROMPT_DIALOG, null, "Update:Wizard", 
                   "finishedBackground", update);
    }
  },
  
  /**
   * See nsIUpdateService.idl
   */
  showUpdateInstalled: function(update) {
    var showUpdateInstalledUI = getPref("getBoolPref", 
      PREF_APP_UPDATE_SHOW_INSTALLED_UI, true);
    if (this._enabled && showUpdateInstalledUI) {
      this._showUI(null, URI_UPDATE_PROMPT_DIALOG, null, "Update:Wizard", 
                   "installed", update);
    }
  },
  
  /**
   * See nsIUpdateService.idl
   */
  showUpdateError: function(update) {
    if (this._enabled) {
      this._showUI(null, URI_UPDATE_PROMPT_DIALOG, null, "Update:Wizard", 
                   "errors", update);
    }
  },
  
  /**
   * See nsIUpdateService.idl
   */
  showUpdateHistory: function(parent) {
    this._showUI(parent, URI_UPDATE_HISTORY_DIALOG, "modal,dialog=yes", "Update:History", 
                 null, null);
  },
  
  /**
   * Whether or not we are enabled (i.e. not in Silent mode)
   */
  get _enabled() {
    return !getPref("getBoolPref", PREF_APP_UPDATE_SILENT, false);
  },
  
  /**
   * Show the Update Checking UI
   * @param   parent
   *          A parent window, can be null
   * @param   uri
   *          The URI string of the dialog to show
   * @param   name
   *          The Window Name of the dialog to show, in case it is already open
   *          and can merely be focused
   * @param   page
   *          The page of the wizard to be displayed, if one is already open.
   * @param   update
   *          An update to pass to the UI in the window arguments. 
   *          Can be null
   */
  _showUI: function(parent, uri, features, name, page, update) {
    var ary = null;
    if (update) {
      ary = Components.classes["@mozilla.org/supports-array;1"]
                      .createInstance(Components.interfaces.nsISupportsArray);
      ary.AppendElement(update);
    }
      
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                       .getService(Components.interfaces.nsIWindowMediator);
    var win = wm.getMostRecentWindow(name);
    if (win) {
      if (page && "setCurrentPage" in win)
        win.setCurrentPage(page);
      win.focus();
    }
    else {
      var openFeatures = "chrome,centerscreen,dialog=no,resizable=no,titlebar,toolbar=no";
      if (features)
        openFeatures += "," + features;
      var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
                         .getService(Components.interfaces.nsIWindowWatcher);
      ww.openWindow(parent, uri, "", openFeatures, ary);
    }
  },
  
  /**
   * See nsISupports.idl
   */
  QueryInterface: function(iid) {
    if (!iid.equals(Components.interfaces.nsIUpdatePrompt) &&
        !iid.equals(Components.interfaces.nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
};
//@line 2436 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8/WINNT_5.2_Depend/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in"

var gModule = {
  registerSelf: function(componentManager, fileSpec, location, type) {
    componentManager = componentManager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
    
    for (var key in this._objects) {
      var obj = this._objects[key];
      componentManager.registerFactoryLocation(obj.CID, obj.className, obj.contractID,
                                               fileSpec, location, type);
    }

    // Make the Update Service a startup observer
    var categoryManager = Components.classes["@mozilla.org/categorymanager;1"]
                                    .getService(Components.interfaces.nsICategoryManager);
    categoryManager.addCategoryEntry("app-startup", this._objects.service.className,
                                     "service," + this._objects.service.contractID, 
                                     true, true, null);
  },
  
  getClassObject: function(componentManager, cid, iid) {
    if (!iid.equals(Components.interfaces.nsIFactory))
      throw Components.results.NS_ERROR_NOT_IMPLEMENTED;

    for (var key in this._objects) {
      if (cid.equals(this._objects[key].CID))
        return this._objects[key].factory;
    }
    
    throw Components.results.NS_ERROR_NO_INTERFACE;
  },
  
  _makeFactory: #1= function(ctor) {
    function ci(outer, iid) {
      if (outer != null)
        throw Components.results.NS_ERROR_NO_AGGREGATION;
      return (new ctor()).QueryInterface(iid);
    } 
    return { createInstance: ci };
  },
  
  _objects: {
    service: { CID        : Components.ID("{B3C290A6-3943-4B89-8BBE-C01EB7B3B311}"),
               contractID : "@mozilla.org/updates/update-service;1",
               className  : "Update Service",
               factory    : #1#(UpdateService)
             },
    checker: { CID        : Components.ID("{898CDC9B-E43F-422F-9CC4-2F6291B415A3}"),
               contractID : "@mozilla.org/updates/update-checker;1",
               className  : "Update Checker",
               factory    : #1#(Checker)
             },
//@line 2488 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8/WINNT_5.2_Depend/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in"
    prompt:  { CID        : Components.ID("{27ABA825-35B5-4018-9FDD-F99250A0E722}"),
               contractID : "@mozilla.org/updates/update-prompt;1",
               className  : "Update Prompt",
               factory    : #1#(UpdatePrompt)
             },
//@line 2494 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8/WINNT_5.2_Depend/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in"
    timers:  { CID        : Components.ID("{B322A5C0-A419-484E-96BA-D7182163899F}"),
               contractID : "@mozilla.org/updates/timer-manager;1",
               className  : "Timer Manager",
               factory    : #1#(TimerManager)
             },
    manager: { CID        : Components.ID("{093C2356-4843-4C65-8709-D7DBCBBE7DFB}"),
               contractID : "@mozilla.org/updates/update-manager;1",
               className  : "Update Manager",
               factory    : #1#(UpdateManager)
             },
  },
  
  canUnload: function(componentManager) {
    return true;
  }
};

function NSGetModule(compMgr, fileSpec) {
  return gModule;
}

/**
 * Determines whether or there are installed addons which are incompatible 
 * with this update.
 * @param   update
 *          The update to check compatibility against
 * @returns true if there are no addons installed that are incompatible with
 *          the specified update, false otherwise.
 */
function isCompatible(update) {
//@line 2525 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8/WINNT_5.2_Depend/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in"
  var em = 
      Components.classes["@mozilla.org/extensions/manager;1"].
      getService(Components.interfaces.nsIExtensionManager);
  var items = em.getIncompatibleItemList("", update.extensionVersion,
    nsIUpdateItem.TYPE_ADDON, false, { });
  return items.length == 0;
//@line 2534 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8/WINNT_5.2_Depend/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in"
}

/**
 * Shows a prompt for an update, provided there are no incompatible addons.
 * If there are, kick off an update check and see if updates are available
 * that will resolve the incompatibilities.
 * @param   update
 *          The available update to show
 */
function showPromptIfNoIncompatibilities(update) {
  function showPrompt(update) {
    LOG("UpdateService", "_selectAndInstallUpdate: need to prompt user before continuing...");
    var prompter = 
        Components.classes["@mozilla.org/updates/update-prompt;1"].
        createInstance(Components.interfaces.nsIUpdatePrompt);
    prompter.showUpdateAvailable(update);
  }

//@line 2553 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8/WINNT_5.2_Depend/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in"
  /**
   * Determines if an addon is compatible with a particular update.
   * @param   addon
   *          The addon to check
   * @param   version
   *          The extensionVersion of the update to check for compatibility 
   *          against.
   * @returns true if the addon is compatible, false otherwise
   */
  function addonIsCompatible(addon, version) {
    var vc = 
        Components.classes["@mozilla.org/xpcom/version-comparator;1"].
        getService(Components.interfaces.nsIVersionComparator);
    return (vc.compare(version, addon.minAppVersion) >= 0) &&
          (vc.compare(version, addon.maxAppVersion) <= 0);
  }

  /**
   * An object implementing nsIAddonUpdateCheckListener that looks for 
   * available updates to addons and if updates are found that will make the 
   * user's installed addon set compatible with the update, suppresses the
   * prompt that would otherwise be shown.
   * @param   addons
   *          An array of incompatible addons that are installed.
   * @constructor
   */
  function Listener(addons) {
    this._addons = addons;
  }
  Listener.prototype = {
    _addons: null,
    
    /**
     * See nsIUpdateService.idl
     */
    onUpdateStarted: function() { 
    },
    onUpdateEnded: function() {
      // There are still incompatibilities, even after an extension update 
      // check to see if there were newer, compatible versions available, so
      // we have to prompt. 
      // 
      // PREF_APP_UPDATE_INCOMPATIBLE_MODE controls the mode in which we 
      // handle incompatibilities:
      // 0    We count both VersionInfo updates _and_ NewerVersion updates
      //      against the list of incompatible addons installed - i.e. if
      //      Foo 1.2 is installed and it is incompatible with the update, and
      //      we find Foo 2.0 which is but which is not yet downloaded or 
      //      installed, then we do NOT prompt because the user can download
      //      Foo 2.0 when they restart after the update during the mismatch
      //      checking UI. This is the default, since it suppresses most 
      //      prompt dialogs. 
      // 1    We count only VersionInfo updates against the list of 
      //      incompatible addons installed - i.e. if the situation above
      //      with Foo 1.2 and available update to 2.0 applies, we DO show
      //      the prompt since a download operation will be required after
      //      the update. This is not the default and is supplied only as
      //      a hidden option for those that want it. 
      var mode = getPref("getIntPref", PREF_APP_UPDATE_INCOMPATIBLE_MODE, 
                         INCOMPATIBLE_MODE_NEWVERSIONS);
      if ((mode == 0 && this._addons.length) || !isCompatible(update))
        showPrompt(update);
    },
    onAddonUpdateStarted: function(addon) {
    },
    onAddonUpdateEnded: function(addon, status) {
      if (status == nsIAddonUpdateCheckListener.STATUS_UPDATE &&
          addonIsCompatible(addon, update.extensionVersion)) {
        for (var i = 0; i < this._addons.length; ++i) {
          if (this._addons[i] == addon) {
            this._addons.splice(i, 1);
            break;
          }
        }
      }
    },
    /**
     * See nsISupports.idl
     */
    QueryInterface: function(iid) {
      if (!iid.equals(Components.interfaces.nsIAddonUpdateCheckListener) &&
          !iid.equals(Components.interfaces.nsISupports))
        throw Components.results.NS_ERROR_NO_INTERFACE;
      return this;
    }
  };
  
  if (!isCompatible(update)) {
    var listener = new Listener(em.getIncompatibleItemList("", 
      update.extensionVersion, nsIUpdateItem.TYPE_ADDON, false, { }));
    var em = 
        Components.classes["@mozilla.org/extensions/manager;1"].
        getService(Components.interfaces.nsIExtensionManager);
    // See documentation on |mode| above. 
    var mode = getPref("getIntPref", PREF_APP_UPDATE_INCOMPATIBLE_MODE, 
                       INCOMPATIBLE_MODE_NEWVERSIONS);
    em.update([], 0, mode != 0, listener);
  }
  else
//@line 2653 "/cygdrive/c/builds/tinderbox/Fx-Mozilla1.8/WINNT_5.2_Depend/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in"
    showPrompt(update);
}
