michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: let gAppUpdater; michael@0: michael@0: let AboutFlyoutPanel = { michael@0: init: function() { michael@0: if (this._isInitialized) { michael@0: Cu.reportError("Attempted to initialize AboutFlyoutPanel more than once"); michael@0: } michael@0: michael@0: this._isInitialized = true; michael@0: michael@0: let self = this; michael@0: this._elements = {}; michael@0: [ michael@0: ['versionLabel', 'about-version-label'], michael@0: ['AboutFlyoutPanel', 'about-flyoutpanel'], michael@0: ].forEach(function(aElement) { michael@0: let [name, id] = aElement; michael@0: XPCOMUtils.defineLazyGetter(self._elements, name, function() { michael@0: return document.getElementById(id); michael@0: }); michael@0: }); michael@0: michael@0: this._topmostElement = this._elements.AboutFlyoutPanel; michael@0: michael@0: // Include the build ID if this is an "a#" (nightly or aurora) build michael@0: let version = Services.appinfo.version; michael@0: if (/a\d+$/.test(version)) { michael@0: let buildID = Services.appinfo.appBuildID; michael@0: let buildDate = buildID.slice(0,4) + "-" + buildID.slice(4,6) + michael@0: "-" + buildID.slice(6,8); michael@0: this._elements.versionLabel.textContent +=" (" + buildDate + ")"; michael@0: } michael@0: michael@0: window.addEventListener('MozFlyoutPanelShowing', this, false); michael@0: window.addEventListener('MozFlyoutPanelHiding', this, false); michael@0: michael@0: #if MOZ_UPDATE_CHANNEL != release michael@0: let defaults = Services.prefs.getDefaultBranch(""); michael@0: let channelLabel = document.getElementById("currentChannel"); michael@0: channelLabel.value = defaults.getCharPref("app.update.channel"); michael@0: #endif michael@0: }, michael@0: michael@0: onPolicyClick: function(aEvent) { michael@0: if (aEvent.button != 0) { michael@0: return; michael@0: } michael@0: let url = Services.urlFormatter.formatURLPref("app.privacyURL"); michael@0: BrowserUI.addAndShowTab(url, Browser.selectedTab); michael@0: }, michael@0: michael@0: handleEvent: function(aEvent) { michael@0: switch (aEvent.type) { michael@0: case 'MozFlyoutPanelShowing': michael@0: #ifdef MOZ_UPDATER michael@0: this.appUpdater = new appUpdater(); michael@0: gAppUpdater = this.appUpdater; michael@0: #endif michael@0: break; michael@0: case 'MozFlyoutPanelHiding': michael@0: #ifdef MOZ_UPDATER michael@0: onUnload(); michael@0: #endif michael@0: break; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: #ifdef MOZ_UPDATER michael@0: function onUnload(aEvent) { michael@0: if (!gAppUpdater) { michael@0: return; michael@0: } michael@0: michael@0: if (gAppUpdater.isChecking) michael@0: gAppUpdater.checker.stopChecking(Components.interfaces.nsIUpdateChecker.CURRENT_CHECK); michael@0: // Safe to call even when there isn't a download in progress. michael@0: gAppUpdater.removeDownloadListener(); michael@0: gAppUpdater = null; michael@0: AboutFlyoutPanel.appUpdater = null; michael@0: } michael@0: michael@0: function appUpdater() michael@0: { michael@0: this.updateDeck = document.getElementById("updateDeck"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "aus", michael@0: "@mozilla.org/updates/update-service;1", michael@0: "nsIApplicationUpdateService"); michael@0: XPCOMUtils.defineLazyServiceGetter(this, "checker", michael@0: "@mozilla.org/updates/update-checker;1", michael@0: "nsIUpdateChecker"); michael@0: XPCOMUtils.defineLazyServiceGetter(this, "um", michael@0: "@mozilla.org/updates/update-manager;1", michael@0: "nsIUpdateManager"); michael@0: michael@0: this.bundle = Services.strings. michael@0: createBundle("chrome://browser/locale/browser.properties"); michael@0: michael@0: this.updateBtn = document.getElementById("updateButton"); michael@0: michael@0: // The button label value must be set so its height is correct. michael@0: this.setupUpdateButton("update.checkInsideButton"); michael@0: michael@0: let manualURL = Services.urlFormatter.formatURLPref("app.update.url.manual"); michael@0: let manualLink = document.getElementById("manualLink"); michael@0: manualLink.value = manualURL; michael@0: manualLink.href = manualURL; michael@0: document.getElementById("failedLink").href = manualURL; michael@0: michael@0: if (this.updateDisabledAndLocked) { michael@0: this.selectPanel("adminDisabled"); michael@0: return; michael@0: } michael@0: michael@0: if (this.isPending || this.isApplied) { michael@0: this.setupUpdateButton("update.restart." + michael@0: (this.isMajor ? "upgradeButton" : "updateButton")); michael@0: return; michael@0: } michael@0: michael@0: if (this.aus.isOtherInstanceHandlingUpdates) { michael@0: this.selectPanel("otherInstanceHandlingUpdates"); michael@0: return; michael@0: } michael@0: michael@0: if (this.isDownloading) { michael@0: this.startDownload(); michael@0: return; michael@0: } michael@0: michael@0: if (this.updateEnabled && this.updateAuto) { michael@0: this.selectPanel("checkingForUpdates"); michael@0: this.isChecking = true; michael@0: this.checker.checkForUpdates(this.updateCheckListener, true); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: appUpdater.prototype = michael@0: { michael@0: // true when there is an update check in progress. michael@0: isChecking: false, michael@0: michael@0: // true when there is an update already staged / ready to be applied. michael@0: get isPending() { michael@0: if (this.update) { michael@0: return this.update.state == "pending" || michael@0: this.update.state == "pending-service"; michael@0: } michael@0: return this.um.activeUpdate && michael@0: (this.um.activeUpdate.state == "pending" || michael@0: this.um.activeUpdate.state == "pending-service"); michael@0: }, michael@0: michael@0: // true when there is an update already installed in the background. michael@0: get isApplied() { michael@0: if (this.update) michael@0: return this.update.state == "applied" || michael@0: this.update.state == "applied-service"; michael@0: return this.um.activeUpdate && michael@0: (this.um.activeUpdate.state == "applied" || michael@0: this.um.activeUpdate.state == "applied-service"); michael@0: }, michael@0: michael@0: // true when there is an update download in progress. michael@0: get isDownloading() { michael@0: if (this.update) michael@0: return this.update.state == "downloading"; michael@0: return this.um.activeUpdate && michael@0: this.um.activeUpdate.state == "downloading"; michael@0: }, michael@0: michael@0: // true when the update type is major. michael@0: get isMajor() { michael@0: if (this.update) michael@0: return this.update.type == "major"; michael@0: return this.um.activeUpdate.type == "major"; michael@0: }, michael@0: michael@0: // true when updating is disabled by an administrator. michael@0: get updateDisabledAndLocked() { michael@0: return !this.updateEnabled && michael@0: Services.prefs.prefIsLocked("app.update.enabled"); michael@0: }, michael@0: michael@0: // true when updating is enabled. michael@0: get updateEnabled() { michael@0: let updatesEnabled = true; michael@0: try { michael@0: updatesEnabled = Services.prefs.getBoolPref("app.update.metro.enabled"); michael@0: } michael@0: catch (e) { } michael@0: if (!updatesEnabled) { michael@0: return false; michael@0: } michael@0: michael@0: try { michael@0: updatesEnabled = Services.prefs.getBoolPref("app.update.enabled") michael@0: } michael@0: catch (e) { } michael@0: michael@0: return updatesEnabled; michael@0: }, michael@0: michael@0: // true when updating in background is enabled. michael@0: get backgroundUpdateEnabled() { michael@0: return this.updateEnabled && michael@0: gAppUpdater.aus.canStageUpdates; michael@0: }, michael@0: michael@0: // true when updating is automatic. michael@0: get updateAuto() { michael@0: try { michael@0: return Services.prefs.getBoolPref("app.update.auto"); michael@0: } michael@0: catch (e) { } michael@0: return true; // Firefox default is true michael@0: }, michael@0: michael@0: /** michael@0: * Sets the deck's selected panel. michael@0: * michael@0: * @param aChildID michael@0: * The id of the deck's child to select. michael@0: */ michael@0: selectPanel: function(aChildID) { michael@0: this.updateDeck.selectedPanel = document.getElementById(aChildID); michael@0: this.updateBtn.disabled = (aChildID != "updateButtonBox"); michael@0: }, michael@0: michael@0: /** michael@0: * Sets the update button's label and accesskey. michael@0: * michael@0: * @param aKeyPrefix michael@0: * The prefix for the properties file entry to use for setting the michael@0: * label and accesskey. michael@0: */ michael@0: setupUpdateButton: function(aKeyPrefix) { michael@0: this.updateBtn.label = this.bundle.GetStringFromName(aKeyPrefix + ".label"); michael@0: this.updateBtn.accessKey = this.bundle.GetStringFromName(aKeyPrefix + ".accesskey"); michael@0: if (!document.commandDispatcher.focusedElement || michael@0: document.commandDispatcher.focusedElement == this.updateBtn) michael@0: this.updateBtn.focus(); michael@0: }, michael@0: michael@0: /** michael@0: * Handles oncommand for the update button. michael@0: */ michael@0: buttonOnCommand: function() { michael@0: if (this.isPending || this.isApplied) { michael@0: // Notify all windows that an application quit has been requested. michael@0: let cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"]. michael@0: createInstance(Components.interfaces.nsISupportsPRBool); michael@0: Services.obs.notifyObservers(cancelQuit, "quit-application-requested", "restart"); michael@0: michael@0: // Something aborted the quit process. michael@0: if (cancelQuit.data) michael@0: return; michael@0: michael@0: let appStartup = Components.classes["@mozilla.org/toolkit/app-startup;1"]. michael@0: getService(Components.interfaces.nsIAppStartup); michael@0: michael@0: // If already in safe mode restart in safe mode (bug 327119) michael@0: if (Services.appinfo.inSafeMode) { michael@0: appStartup.restartInSafeMode(Components.interfaces.nsIAppStartup.eAttemptQuit); michael@0: return; michael@0: } michael@0: michael@0: Services.metro.updatePending = true; michael@0: appStartup.quit(Components.interfaces.nsIAppStartup.eAttemptQuit | michael@0: Components.interfaces.nsIAppStartup.eRestartTouchEnvironment); michael@0: return; michael@0: } michael@0: michael@0: // XXX We can't create dialogs in metro, and we currently don't support addons, so michael@0: // commenting this out for now. michael@0: /* const URI_UPDATE_PROMPT_DIALOG = "chrome://mozapps/content/update/updates.xul"; michael@0: // Firefox no longer displays a license for updates and the licenseURL check michael@0: // is just in case a distibution does. michael@0: if (this.update && (this.update.billboardURL || this.update.licenseURL || michael@0: this.addons.length != 0)) { michael@0: var ary = null; michael@0: ary = Components.classes["@mozilla.org/supports-array;1"]. michael@0: createInstance(Components.interfaces.nsISupportsArray); michael@0: ary.AppendElement(this.update); michael@0: var openFeatures = "chrome,centerscreen,dialog=no,resizable=no,titlebar,toolbar=no"; michael@0: Services.ww.openWindow(null, URI_UPDATE_PROMPT_DIALOG, "", openFeatures, ary); michael@0: window.close(); michael@0: return; michael@0: }*/ michael@0: michael@0: this.selectPanel("checkingForUpdates"); michael@0: this.isChecking = true; michael@0: this.checker.checkForUpdates(this.updateCheckListener, true); michael@0: }, michael@0: michael@0: /** michael@0: * Implements nsIUpdateCheckListener. The methods implemented by michael@0: * nsIUpdateCheckListener are in a different scope from nsIIncrementalDownload michael@0: * to make it clear which are used by each interface. michael@0: */ michael@0: updateCheckListener: { michael@0: /** michael@0: * See nsIUpdateService.idl michael@0: */ michael@0: onCheckComplete: function(aRequest, aUpdates, aUpdateCount) { michael@0: gAppUpdater.isChecking = false; michael@0: gAppUpdater.update = gAppUpdater.aus. michael@0: selectUpdate(aUpdates, aUpdates.length); michael@0: if (!gAppUpdater.update) { michael@0: gAppUpdater.selectPanel("noUpdatesFound"); michael@0: return; michael@0: } michael@0: michael@0: if (!gAppUpdater.aus.canApplyUpdates) { michael@0: gAppUpdater.selectPanel("manualUpdate"); michael@0: return; michael@0: } michael@0: michael@0: // Firefox no longer displays a license for updates and the licenseURL michael@0: // check is just in case a distibution does. michael@0: if (gAppUpdater.update.billboardURL || gAppUpdater.update.licenseURL) { michael@0: gAppUpdater.selectPanel("updateButtonBox"); michael@0: gAppUpdater.setupUpdateButton("update.openUpdateUI." + michael@0: (this.isMajor ? "upgradeButton" michael@0: : "applyButton")); michael@0: return; michael@0: } michael@0: michael@0: if (!gAppUpdater.update.appVersion || michael@0: Services.vc.compare(gAppUpdater.update.appVersion, michael@0: Services.appinfo.version) == 0) { michael@0: gAppUpdater.startDownload(); michael@0: return; michael@0: } michael@0: michael@0: gAppUpdater.checkAddonCompatibility(); michael@0: }, michael@0: michael@0: /** michael@0: * See nsIUpdateService.idl michael@0: */ michael@0: onError: function(aRequest, aUpdate) { michael@0: // Errors in the update check are treated as no updates found. If the michael@0: // update check fails repeatedly without a success the user will be michael@0: // notified with the normal app update user interface so this is safe. michael@0: gAppUpdater.isChecking = false; michael@0: gAppUpdater.selectPanel("noUpdatesFound"); michael@0: }, michael@0: michael@0: /** michael@0: * See nsISupports.idl michael@0: */ michael@0: QueryInterface: function(aIID) { michael@0: if (!aIID.equals(Components.interfaces.nsIUpdateCheckListener) && michael@0: !aIID.equals(Components.interfaces.nsISupports)) michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: return this; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Checks the compatibility of add-ons for the application update. michael@0: */ michael@0: checkAddonCompatibility: function() { michael@0: try { michael@0: var hotfixID = Services.prefs.getCharPref(PREF_EM_HOTFIX_ID); michael@0: } michael@0: catch (e) { } michael@0: michael@0: var self = this; michael@0: AddonManager.getAllAddons(function(aAddons) { michael@0: self.addons = []; michael@0: self.addonsCheckedCount = 0; michael@0: aAddons.forEach(function(aAddon) { michael@0: // Protect against code that overrides the add-ons manager and doesn't michael@0: // implement the isCompatibleWith or the findUpdates method. michael@0: if (!("isCompatibleWith" in aAddon) || !("findUpdates" in aAddon)) { michael@0: let errMsg = "Add-on doesn't implement either the isCompatibleWith " + michael@0: "or the findUpdates method!"; michael@0: if (aAddon.id) michael@0: errMsg += " Add-on ID: " + aAddon.id; michael@0: Components.utils.reportError(errMsg); michael@0: return; michael@0: } michael@0: michael@0: // If an add-on isn't appDisabled and isn't userDisabled then it is michael@0: // either active now or the user expects it to be active after the michael@0: // restart. If that is the case and the add-on is not installed by the michael@0: // application and is not compatible with the new application version michael@0: // then the user should be warned that the add-on will become michael@0: // incompatible. If an addon's type equals plugin it is skipped since michael@0: // checking plugins compatibility information isn't supported and michael@0: // getting the scope property of a plugin breaks in some environments michael@0: // (see bug 566787). The hotfix add-on is also ignored as it shouldn't michael@0: // block the user from upgrading. michael@0: try { michael@0: if (aAddon.type != "plugin" && aAddon.id != hotfixID && michael@0: !aAddon.appDisabled && !aAddon.userDisabled && michael@0: aAddon.scope != AddonManager.SCOPE_APPLICATION && michael@0: aAddon.isCompatible && michael@0: !aAddon.isCompatibleWith(self.update.appVersion, michael@0: self.update.platformVersion)) michael@0: self.addons.push(aAddon); michael@0: } michael@0: catch (e) { michael@0: Components.utils.reportError(e); michael@0: } michael@0: }); michael@0: self.addonsTotalCount = self.addons.length; michael@0: if (self.addonsTotalCount == 0) { michael@0: self.startDownload(); michael@0: return; michael@0: } michael@0: michael@0: self.checkAddonsForUpdates(); michael@0: }); michael@0: }, michael@0: michael@0: /** michael@0: * Checks if there are updates for add-ons that are incompatible with the michael@0: * application update. michael@0: */ michael@0: checkAddonsForUpdates: function() { michael@0: this.addons.forEach(function(aAddon) { michael@0: aAddon.findUpdates(this, AddonManager.UPDATE_WHEN_NEW_APP_DETECTED, michael@0: this.update.appVersion, michael@0: this.update.platformVersion); michael@0: }, this); michael@0: }, michael@0: michael@0: /** michael@0: * See XPIProvider.jsm michael@0: */ michael@0: onCompatibilityUpdateAvailable: function(aAddon) { michael@0: for (var i = 0; i < this.addons.length; ++i) { michael@0: if (this.addons[i].id == aAddon.id) { michael@0: this.addons.splice(i, 1); michael@0: break; michael@0: } michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * See XPIProvider.jsm michael@0: */ michael@0: onUpdateAvailable: function(aAddon, aInstall) { michael@0: if (!Services.blocklist.isAddonBlocklisted(aAddon, michael@0: this.update.appVersion, michael@0: this.update.platformVersion)) { michael@0: // Compatibility or new version updates mean the same thing here. michael@0: this.onCompatibilityUpdateAvailable(aAddon); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * See XPIProvider.jsm michael@0: */ michael@0: onUpdateFinished: function(aAddon) { michael@0: ++this.addonsCheckedCount; michael@0: michael@0: if (this.addonsCheckedCount < this.addonsTotalCount) michael@0: return; michael@0: michael@0: if (this.addons.length == 0) { michael@0: // Compatibility updates or new version updates were found for all add-ons michael@0: this.startDownload(); michael@0: return; michael@0: } michael@0: michael@0: this.selectPanel("updateButtonBox"); michael@0: this.setupUpdateButton("update.openUpdateUI." + michael@0: (this.isMajor ? "upgradeButton" : "applyButton")); michael@0: }, michael@0: michael@0: /** michael@0: * Starts the download of an update mar. michael@0: */ michael@0: startDownload: function() { michael@0: if (!this.update) michael@0: this.update = this.um.activeUpdate; michael@0: this.update.QueryInterface(Components.interfaces.nsIWritablePropertyBag); michael@0: this.update.setProperty("foregroundDownload", "true"); michael@0: michael@0: this.aus.pauseDownload(); michael@0: let state = this.aus.downloadUpdate(this.update, false); michael@0: if (state == "failed") { michael@0: this.selectPanel("downloadFailed"); michael@0: return; michael@0: } michael@0: michael@0: this.setupDownloadingUI(); michael@0: }, michael@0: michael@0: /** michael@0: * Switches to the UI responsible for tracking the download. michael@0: */ michael@0: setupDownloadingUI: function() { michael@0: this.downloadStatus = document.getElementById("downloadStatus"); michael@0: this.downloadStatus.textContent = michael@0: DownloadUtils.getTransferTotal(0, this.update.selectedPatch.size); michael@0: this.selectPanel("downloading"); michael@0: this.aus.addDownloadListener(this); michael@0: }, michael@0: michael@0: removeDownloadListener: function() { michael@0: if (this.aus) { michael@0: this.aus.removeDownloadListener(this); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * See nsIRequestObserver.idl michael@0: */ michael@0: onStartRequest: function(aRequest, aContext) { michael@0: }, michael@0: michael@0: /** michael@0: * See nsIRequestObserver.idl michael@0: */ michael@0: onStopRequest: function(aRequest, aContext, aStatusCode) { michael@0: switch (aStatusCode) { michael@0: case Components.results.NS_ERROR_UNEXPECTED: michael@0: if (this.update.selectedPatch.state == "download-failed" && michael@0: (this.update.isCompleteUpdate || this.update.patchCount != 2)) { michael@0: // Verification error of complete patch, informational text is held in michael@0: // the update object. michael@0: this.removeDownloadListener(); michael@0: this.selectPanel("downloadFailed"); michael@0: break; michael@0: } michael@0: // Verification failed for a partial patch, complete patch is now michael@0: // downloading so return early and do NOT remove the download listener! michael@0: break; michael@0: case Components.results.NS_BINDING_ABORTED: michael@0: // Do not remove UI listener since the user may resume downloading again. michael@0: break; michael@0: case Components.results.NS_OK: michael@0: this.removeDownloadListener(); michael@0: if (this.backgroundUpdateEnabled) { michael@0: this.selectPanel("applying"); michael@0: let update = this.um.activeUpdate; michael@0: let self = this; michael@0: Services.obs.addObserver(function updateStaged(aSubject, aTopic, aData) { michael@0: // Update the UI when the background updater is finished michael@0: let status = aData; michael@0: if (status == "applied" || status == "applied-service" || michael@0: status == "pending" || status == "pending-service") { michael@0: // If the update is successfully applied, or if the updater has michael@0: // fallen back to non-staged updates, show the Restart to Update michael@0: // button. michael@0: self.selectPanel("updateButtonBox"); michael@0: self.setupUpdateButton("update.restart." + michael@0: (self.isMajor ? "upgradeButton" : "updateButton")); michael@0: } else if (status == "failed") { michael@0: // Background update has failed, let's show the UI responsible for michael@0: // prompting the user to update manually. michael@0: self.selectPanel("downloadFailed"); michael@0: } else if (status == "downloading") { michael@0: // We've fallen back to downloading the full update because the michael@0: // partial update failed to get staged in the background. michael@0: // Therefore we need to keep our observer. michael@0: self.setupDownloadingUI(); michael@0: return; michael@0: } michael@0: Services.obs.removeObserver(updateStaged, "update-staged"); michael@0: }, "update-staged", false); michael@0: } else { michael@0: this.selectPanel("updateButtonBox"); michael@0: this.setupUpdateButton("update.restart." + michael@0: (this.isMajor ? "upgradeButton" : "updateButton")); michael@0: } michael@0: break; michael@0: default: michael@0: this.removeDownloadListener(); michael@0: this.selectPanel("downloadFailed"); michael@0: break; michael@0: } michael@0: michael@0: }, michael@0: michael@0: /** michael@0: * See nsIProgressEventSink.idl michael@0: */ michael@0: onStatus: function(aRequest, aContext, aStatus, aStatusArg) { michael@0: }, michael@0: michael@0: /** michael@0: * See nsIProgressEventSink.idl michael@0: */ michael@0: onProgress: function(aRequest, aContext, aProgress, aProgressMax) { michael@0: this.downloadStatus.textContent = michael@0: DownloadUtils.getTransferTotal(aProgress, aProgressMax); michael@0: }, michael@0: michael@0: /** michael@0: * See nsISupports.idl michael@0: */ michael@0: QueryInterface: function(aIID) { michael@0: if (!aIID.equals(Components.interfaces.nsIProgressEventSink) && michael@0: !aIID.equals(Components.interfaces.nsIRequestObserver) && michael@0: !aIID.equals(Components.interfaces.nsISupports)) michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: return this; michael@0: } michael@0: }; michael@0: #endif