home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 May / maximum-cd-2009-05.iso / DiscContents / Firefox Setup 3.0.6.exe / nonlocalized / chrome / toolkit.jar / content / mozapps / extensions / update.js < prev    next >
Encoding:
Text File  |  2008-03-06  |  17.5 KB  |  512 lines

  1. //@line 38 "/e/fx19rel/WINNT_5.2_Depend/mozilla/toolkit/mozapps/extensions/content/update.js"
  2.  
  3. // This UI is only opened from the Extension Manager when the app is upgraded.
  4.  
  5. const nsIExtensionManager = Components.interfaces.nsIExtensionManager;
  6. const nsIUpdateItem = Components.interfaces.nsIUpdateItem;
  7. const nsIAUCL = Components.interfaces.nsIAddonUpdateCheckListener;
  8.  
  9. const PREF_UPDATE_EXTENSIONS_ENABLED            = "extensions.update.enabled";
  10. const PREF_XPINSTALL_ENABLED                    = "xpinstall.enabled";
  11.  
  12. var gUpdateWizard = {
  13.   // When synchronizing app compatibility info this contains all installed
  14.   // add-ons. When checking for compatible versions this contains only
  15.   // incompatible add-ons.
  16.   items: [],
  17.   // The items that we found updates available for
  18.   itemsToUpdate: [],
  19.   shouldSuggestAutoChecking: false,
  20.   shouldAutoCheck: false,
  21.   xpinstallEnabled: false,
  22.   xpinstallLocked: false,
  23.  
  24.   init: function ()
  25.   {
  26.     var em = Components.classes["@mozilla.org/extensions/manager;1"]
  27.                         .getService(nsIExtensionManager);
  28.     // Retrieve all items in order to sync their app compatibility information
  29.     this.items = em.getItemList(nsIUpdateItem.TYPE_ANY, { });
  30.     var pref =
  31.         Components.classes["@mozilla.org/preferences-service;1"].
  32.         getService(Components.interfaces.nsIPrefBranch);
  33.  
  34.     try {
  35.       this.shouldSuggestAutoChecking =
  36.         !pref.getBoolPref(PREF_UPDATE_EXTENSIONS_ENABLED);
  37.     }
  38.     catch (e) {
  39.     }
  40.  
  41.     try {
  42.       this.xpinstallEnabled = pref.getBoolPref(PREF_XPINSTALL_ENABLED);
  43.       this.xpinstallLocked = pref.prefIsLocked(PREF_XPINSTALL_ENABLED);
  44.     }
  45.     catch (e) {
  46.     }
  47.     var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  48.                               .getService(Components.interfaces.nsIIOService);
  49.     if (ioService.offline)
  50.       document.documentElement.currentPage =
  51.         document.getElementById("offline");
  52.     else
  53.       document.documentElement.currentPage =
  54.         document.getElementById("versioninfo");
  55.   },
  56.  
  57.   onWizardFinish: function ()
  58.   {
  59.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  60.                          .getService(Components.interfaces.nsIPrefBranch);
  61.     if (this.shouldSuggestAutoChecking)
  62.       pref.setBoolPref(PREF_UPDATE_EXTENSIONS_ENABLED, this.shouldAutoCheck);
  63.   },
  64.  
  65.   _setUpButton: function (aButtonID, aButtonKey, aDisabled)
  66.   {
  67.     var strings = document.getElementById("updateStrings");
  68.     var button = document.documentElement.getButton(aButtonID);
  69.     if (aButtonKey) {
  70.       button.label = strings.getString(aButtonKey);
  71.       try {
  72.         button.setAttribute("accesskey", strings.getString(aButtonKey + "Accesskey"));
  73.       }
  74.       catch (e) {
  75.       }
  76.     }
  77.     button.disabled = aDisabled;
  78.   },
  79.  
  80.   setButtonLabels: function (aBackButton, aBackButtonIsDisabled,
  81.                              aNextButton, aNextButtonIsDisabled,
  82.                              aCancelButton, aCancelButtonIsDisabled)
  83.   {
  84.     this._setUpButton("back", aBackButton, aBackButtonIsDisabled);
  85.     this._setUpButton("next", aNextButton, aNextButtonIsDisabled);
  86.     this._setUpButton("cancel", aCancelButton, aCancelButtonIsDisabled);
  87.   },
  88.  
  89.   /////////////////////////////////////////////////////////////////////////////
  90.   // Update Errors
  91.   errorItems: [],
  92.   showErrors: function (aState, aErrors)
  93.   {
  94.     openDialog("chrome://mozapps/content/update/errors.xul", "",
  95.                "modal", { state: aState, errors: aErrors });
  96.   },
  97.  
  98.   // Displays a list of items that had an error during the update check. We
  99.   // don't display the actual error that occured since
  100.   // nsIAddonUpdateCheckListener doesn't return the error details.
  101.   showUpdateCheckErrors: function ()
  102.   {
  103.     var errors = [];
  104.     for (var i = 0; i < this.errorItems.length; ++i)
  105.       errors.push({ name: this.errorItems[i].name, error: true });
  106.     this.showErrors("checking", errors);
  107.   },
  108.  
  109.   checkForErrors: function (aElementIDToShow)
  110.   {
  111.     if (this.errorItems.length > 0)
  112.       document.getElementById(aElementIDToShow).hidden = false;
  113.   },
  114.  
  115.   onWizardClose: function (aEvent)
  116.   {
  117.     if (gInstallingPage._installing) {
  118.       var os = Components.classes["@mozilla.org/observer-service;1"]
  119.                          .getService(Components.interfaces.nsIObserverService);
  120.       os.notifyObservers(null, "xpinstall-progress", "cancel");
  121.       return false;
  122.     }
  123.     return true;
  124.   }
  125. };
  126.  
  127. var gOfflinePage = {
  128.   onPageAdvanced: function ()
  129.   {
  130.     var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  131.                               .getService(Components.interfaces.nsIIOService);
  132.     ioService.offline = false;
  133.     return true;
  134.   },
  135.  
  136.   toggleOffline: function ()
  137.   {
  138.     var nextbtn = document.documentElement.getButton("next");
  139.     nextbtn.disabled = !nextbtn.disabled;
  140.   }
  141. }
  142.  
  143. var gVersionInfoPage = {
  144.   _completeCount: 0,
  145.   _totalCount: 0,
  146.   onPageShow: function ()
  147.   {
  148.     gUpdateWizard.setButtonLabels(null, true,
  149.                                   "nextButtonText", true,
  150.                                   "cancelButtonText", false);
  151.     var em = Components.classes["@mozilla.org/extensions/manager;1"]
  152.                        .getService(nsIExtensionManager);
  153.     // Synchronize the app compatibility info for all items.
  154.     em.update([], 0, nsIExtensionManager.UPDATE_SYNC_COMPATIBILITY, this);
  155.   },
  156.  
  157.   /////////////////////////////////////////////////////////////////////////////
  158.   // nsIAddonUpdateCheckListener
  159.   onUpdateStarted: function() {
  160.     this._totalCount = gUpdateWizard.items.length;
  161.   },
  162.  
  163.   onUpdateEnded: function() {
  164.     var em = Components.classes["@mozilla.org/extensions/manager;1"]
  165.                        .getService(nsIExtensionManager);
  166.     // Retrieve the remaining incompatible items.
  167.     gUpdateWizard.items = em.getIncompatibleItemList(null, null, null,
  168.                                                      nsIUpdateItem.TYPE_ANY,
  169.                                                      true, { });
  170.     if (gUpdateWizard.items.length > 0) {
  171.       // There are still incompatible addons, inform the user.
  172.       document.documentElement.currentPage =
  173.         document.getElementById("mismatch");
  174.     }
  175.     else {
  176.       // VersionInfo compatibility updates resolved all compatibility problems,
  177.       // close this window and continue starting the application...
  178.       //XXX Bug 314754 - We need to use setTimeout to close the window due to
  179.       // the EM using xmlHttpRequest when checking for updates.
  180.       setTimeout(close, 0);
  181.     }
  182.   },
  183.  
  184.   onAddonUpdateStarted: function(addon) {
  185.   },
  186.  
  187.   onAddonUpdateEnded: function(addon, status) {
  188.     if (status == nsIAUCL.STATUS_VERSIONINFO) {
  189.       for (var i = 0; i < gUpdateWizard.items.length; ++i) {
  190.         var item = gUpdateWizard.items[i].QueryInterface(nsIUpdateItem);
  191.         if (addon.id == item.id) {
  192.           gUpdateWizard.items.splice(i, 1);
  193.           break;
  194.         }
  195.       }
  196.     }
  197.     else if (status == nsIAUCL.STATUS_FAILURE)
  198.       gUpdateWizard.errorItems.push(addon);
  199.  
  200.     ++this._completeCount;
  201.  
  202.     // Update the status text and progress bar
  203.     var updateStrings = document.getElementById("updateStrings");
  204.     var status = document.getElementById("versioninfo.status");
  205.     var statusString = updateStrings.getFormattedString("statusPrefix", [addon.name]);
  206.     status.setAttribute("value", statusString);
  207.  
  208.     // Update the status text and progress bar
  209.     var progress = document.getElementById("versioninfo.progress");
  210.     progress.mode = "normal";
  211.     progress.value = Math.ceil((this._completeCount / this._totalCount) * 100);
  212.   },
  213.  
  214.   /////////////////////////////////////////////////////////////////////////////
  215.   // nsISupports
  216.   QueryInterface: function(iid) {
  217.     if (!iid.equals(Components.interfaces.nsIAddonUpdateCheckListener) &&
  218.         !iid.equals(Components.interfaces.nsISupports))
  219.       throw Components.results.NS_ERROR_NO_INTERFACE;
  220.     return this;
  221.   }
  222. };
  223.  
  224. var gMismatchPage = {
  225.   onPageShow: function ()
  226.   {
  227.     gUpdateWizard.setButtonLabels(null, true,
  228.                                   "mismatchCheckNow", false,
  229.                                   "mismatchDontCheck", false);
  230.     document.documentElement.getButton("next").focus();
  231.  
  232.     var incompatible = document.getElementById("mismatch.incompatible");
  233.     for (var i = 0; i < gUpdateWizard.items.length; ++i) {
  234.       var item = gUpdateWizard.items[i].QueryInterface(nsIUpdateItem);
  235.       var listitem = document.createElement("listitem");
  236.       listitem.setAttribute("label", item.name + " " + item.version);
  237.       incompatible.appendChild(listitem);
  238.     }
  239.   }
  240. };
  241.  
  242. var gUpdatePage = {
  243.   _totalCount: 0,
  244.   _completeCount: 0,
  245.   onPageShow: function ()
  246.   {
  247.     if (!gUpdateWizard.xpinstallEnabled && gUpdateWizard.xpinstallLocked) {
  248.       document.documentElement.currentPage = document.getElementById("adminDisabled");
  249.       return;
  250.     }
  251.  
  252.     gUpdateWizard.setButtonLabels(null, true,
  253.                                   "nextButtonText", true,
  254.                                   "cancelButtonText", false);
  255.     document.documentElement.getButton("next").focus();
  256.  
  257.     gUpdateWizard.errorItems = [];
  258.  
  259.     this._totalCount = gUpdateWizard.items.length;
  260.     var em = Components.classes["@mozilla.org/extensions/manager;1"]
  261.                        .getService(nsIExtensionManager);
  262.     em.update(gUpdateWizard.items, this._totalCount,
  263.               nsIExtensionManager.UPDATE_CHECK_NEWVERSION, this);
  264.   },
  265.  
  266.   /////////////////////////////////////////////////////////////////////////////
  267.   // nsIAddonUpdateCheckListener
  268.   onUpdateStarted: function() {
  269.   },
  270.  
  271.   onUpdateEnded: function() {
  272.     var nextPage = document.getElementById("noupdates");
  273.     if (gUpdateWizard.itemsToUpdate.length > 0)
  274.       nextPage = document.getElementById("found");
  275.     document.documentElement.currentPage = nextPage;
  276.   },
  277.  
  278.   onAddonUpdateStarted: function(addon) {
  279.   },
  280.  
  281.   onAddonUpdateEnded: function(addon, status) {
  282.     if (status == nsIAUCL.STATUS_UPDATE)
  283.       gUpdateWizard.itemsToUpdate.push(addon);
  284.     else if (status == nsIAUCL.STATUS_FAILURE)
  285.       gUpdateWizard.errorItems.push(addon);
  286.  
  287.     ++this._completeCount;
  288.  
  289.     // Update the status text and progress bar
  290.     var updateStrings = document.getElementById("updateStrings");
  291.     var status = document.getElementById("checking.status");
  292.     var statusString = updateStrings.getFormattedString("statusPrefix", [addon.name]);
  293.     status.setAttribute("value", statusString);
  294.  
  295.     var progress = document.getElementById("checking.progress");
  296.     progress.value = Math.ceil((this._completeCount / this._totalCount) * 100);
  297.   },
  298.  
  299.   /////////////////////////////////////////////////////////////////////////////
  300.   // nsISupports
  301.   QueryInterface: function(iid) {
  302.     if (!iid.equals(Components.interfaces.nsIAddonUpdateCheckListener) &&
  303.         !iid.equals(Components.interfaces.nsISupports))
  304.       throw Components.results.NS_ERROR_NO_INTERFACE;
  305.     return this;
  306.   }
  307. };
  308.  
  309. var gFoundPage = {
  310.   onPageShow: function ()
  311.   {
  312.     gUpdateWizard.setButtonLabels(null, true,
  313.                                   "installButtonText", false,
  314.                                   null, false);
  315.  
  316.     var foundUpdates = document.getElementById("found.updates");
  317.     var itemCount = gUpdateWizard.itemsToUpdate.length;
  318.     for (var i = 0; i < itemCount; ++i) {
  319.       var item = gUpdateWizard.itemsToUpdate[i];
  320.       var listItem = foundUpdates.appendItem(item.name + " " + item.version,
  321.                                              item.xpiURL);
  322.       listItem.setAttribute("type", "checkbox");
  323.       listItem.setAttribute("checked", "true");
  324.       listItem.setAttribute("URL", item.xpiURL);
  325.       listItem.setAttribute("hash", item.xpiHash);
  326.     }
  327.  
  328.     if (!gUpdateWizard.xpinstallEnabled) {
  329.       document.getElementById("xpinstallDisabledAlert").hidden = false;
  330.       document.getElementById("enableXPInstall").focus();
  331.       document.documentElement.getButton("next").disabled = true;
  332.     }
  333.     else {
  334.       document.documentElement.getButton("next").focus();
  335.       document.documentElement.getButton("next").disabled = false;
  336.     }
  337.   },
  338.  
  339.   toggleXPInstallEnable: function(aEvent)
  340.   {
  341.     var enabled = aEvent.target.checked;
  342.     gUpdateWizard.xpinstallEnabled = enabled;
  343.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  344.                          .getService(Components.interfaces.nsIPrefBranch);
  345.     pref.setBoolPref(PREF_XPINSTALL_ENABLED, enabled);
  346.     this.updateNextButton();
  347.   },
  348.  
  349.   updateNextButton: function ()
  350.   {
  351.     if (!gUpdateWizard.xpinstallEnabled) {
  352.       document.documentElement.getButton("next").disabled = true;
  353.       return;
  354.     }
  355.  
  356.     var oneChecked = false;
  357.     var foundUpdates = document.getElementById("found.updates");
  358.     var updates = foundUpdates.getElementsByTagName("listitem");;
  359.     for (var i = 0; i < updates.length; ++i) {
  360.       if (!updates[i].checked)
  361.         continue;
  362.       oneChecked = true;
  363.       break;
  364.     }
  365.  
  366.     gUpdateWizard.setButtonLabels(null, true,
  367.                                   "installButtonText", true,
  368.                                   null, false);
  369.     document.getElementById("found").setAttribute("next", "installing");
  370.     document.documentElement.getButton("next").disabled = !oneChecked;
  371.   }
  372. };
  373.  
  374. var gInstallingPage = {
  375.   _installing       : false,
  376.   _objs             : [],
  377.   _errors           : [],
  378.  
  379.   onPageShow: function ()
  380.   {
  381.     gUpdateWizard.setButtonLabels(null, true,
  382.                                   "nextButtonText", true,
  383.                                   null, true);
  384.  
  385.     // Get XPInstallManager and kick off download/install
  386.     // process, registering us as an observer.
  387.     var items = [];
  388.     var hashes = [];
  389.     this._objs = [];
  390.     this._errors = [];
  391.  
  392.     var foundUpdates = document.getElementById("found.updates");
  393.     var updates = foundUpdates.getElementsByTagName("listitem");;
  394.     for (var i = 0; i < updates.length; ++i) {
  395.       if (!updates[i].checked)
  396.         continue;
  397.       items.push(updates[i].value);
  398.       hashes.push(updates[i].getAttribute("hash") ? updates[i].getAttribute("hash") : null);
  399.       this._objs.push({ name: updates[i].label });
  400.     }
  401.  
  402.     var xpimgr = Components.classes["@mozilla.org/xpinstall/install-manager;1"]
  403.                            .createInstance(Components.interfaces.nsIXPInstallManager);
  404.     xpimgr.initManagerWithHashes(items, hashes, items.length, this);
  405.   },
  406.  
  407.   /////////////////////////////////////////////////////////////////////////////
  408.   // nsIXPIProgressDialog
  409.   onStateChange: function (aIndex, aState, aValue)
  410.   {
  411.     var strings = document.getElementById("updateStrings");
  412.  
  413.     const nsIXPIProgressDialog = Components.interfaces.nsIXPIProgressDialog;
  414.     switch (aState) {
  415.     case nsIXPIProgressDialog.DOWNLOAD_START:
  416.       var label = strings.getFormattedString("downloadingPrefix", [this._objs[aIndex].name]);
  417.       var actionItem = document.getElementById("actionItem");
  418.       actionItem.value = label;
  419.       break;
  420.     case nsIXPIProgressDialog.DOWNLOAD_DONE:
  421.     case nsIXPIProgressDialog.INSTALL_START:
  422.       var label = strings.getFormattedString("installingPrefix", [this._objs[aIndex].name]);
  423.       var actionItem = document.getElementById("actionItem");
  424.       actionItem.value = label;
  425.       this._installing = true;
  426.       break;
  427.     case nsIXPIProgressDialog.INSTALL_DONE:
  428.       switch (aValue) {
  429.       case 999:
  430.       case 0:
  431.         break;
  432.       default:
  433.         this._errors.push({ name: this._objs[aIndex].name, error: aValue });
  434.       }
  435.       break;
  436.     case nsIXPIProgressDialog.DIALOG_CLOSE:
  437.       this._installing = false;
  438.       var nextPage = this._errors.length > 0 ? "installerrors" : "finished";
  439.       document.getElementById("installing").setAttribute("next", nextPage);
  440.       document.documentElement.advance();
  441.       break;
  442.     }
  443.   },
  444.  
  445.   onProgress: function (aIndex, aValue, aMaxValue)
  446.   {
  447.     var downloadProgress = document.getElementById("downloadProgress");
  448.     downloadProgress.value = Math.ceil((aValue/aMaxValue) * 100);
  449.   }
  450. };
  451.  
  452. var gInstallErrorsPage = {
  453.   onPageShow: function ()
  454.   {
  455.     gUpdateWizard.setButtonLabels(null, true, null, true, null, true);
  456.     document.documentElement.getButton("finish").focus();
  457.   },
  458.  
  459.   onShowErrors: function ()
  460.   {
  461.     gUpdateWizard.showErrors("install", gInstallingPage._errors);
  462.   }
  463. };
  464.  
  465. // Displayed when there are incompatible add-ons and the xpinstall.enabled
  466. // pref is false and locked.
  467. var gAdminDisabledPage = {
  468.   onPageShow: function ()
  469.   {
  470.     gUpdateWizard.setButtonLabels(null, true, null, true,
  471.                                   "cancelButtonText", true);
  472.     document.documentElement.getButton("finish").focus();
  473.   }
  474. };
  475.  
  476. // Displayed when selected add-on updates have been installed without error.
  477. // There can still be add-ons that are not compatible and don't have an update.
  478. var gFinishedPage = {
  479.   onPageShow: function ()
  480.   {
  481.     gUpdateWizard.setButtonLabels(null, true, null, true, null, true);
  482.     document.documentElement.getButton("finish").focus();
  483.  
  484.     if (gUpdateWizard.shouldSuggestAutoChecking) {
  485.       document.getElementById("finishedCheckDisabled").hidden = false;
  486.       gUpdateWizard.shouldAutoCheck = true;
  487.     }
  488.     else
  489.       document.getElementById("finishedCheckEnabled").hidden = false;
  490.  
  491.     document.documentElement.getButton("finish").focus();
  492.   }
  493. };
  494.  
  495. // Displayed when there are incompatible add-ons and there are no available
  496. // updates.
  497. var gNoUpdatesPage = {
  498.   onPageShow: function (aEvent)
  499.   {
  500.     gUpdateWizard.setButtonLabels(null, true, null, true, null, true);
  501.     if (gUpdateWizard.shouldSuggestAutoChecking) {
  502.       document.getElementById("noupdatesCheckDisabled").hidden = false;
  503.       gUpdateWizard.shouldAutoCheck = true;
  504.     }
  505.     else
  506.       document.getElementById("noupdatesCheckEnabled").hidden = false;
  507.  
  508.     gUpdateWizard.checkForErrors("updateCheckErrorNotFound");
  509.     document.documentElement.getButton("finish").focus();
  510.   }
  511. };
  512.