michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: 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: michael@0: var XPInstallConfirm = {}; michael@0: michael@0: XPInstallConfirm.init = function XPInstallConfirm_init() michael@0: { michael@0: var _installCountdown; michael@0: var _installCountdownInterval; michael@0: var _focused; michael@0: var _timeout; michael@0: michael@0: // Default to cancelling the install when the window unloads michael@0: XPInstallConfirm._installOK = false; michael@0: michael@0: var bundle = document.getElementById("xpinstallConfirmStrings"); michael@0: michael@0: let args = window.arguments[0].wrappedJSObject; michael@0: michael@0: var _installCountdownLength = 5; michael@0: try { michael@0: var prefs = Components.classes["@mozilla.org/preferences-service;1"] michael@0: .getService(Components.interfaces.nsIPrefBranch); michael@0: var delay_in_milliseconds = prefs.getIntPref("security.dialog_enable_delay"); michael@0: _installCountdownLength = Math.round(delay_in_milliseconds / 500); michael@0: } catch (ex) { } michael@0: michael@0: var itemList = document.getElementById("itemList"); michael@0: michael@0: var numItemsToInstall = args.installs.length; michael@0: for (let install of args.installs) { michael@0: var installItem = document.createElement("installitem"); michael@0: itemList.appendChild(installItem); michael@0: michael@0: installItem.name = install.addon.name; michael@0: installItem.url = install.sourceURI.spec; michael@0: var icon = install.iconURL; michael@0: if (icon) michael@0: installItem.icon = icon; michael@0: var type = install.type; michael@0: if (type) michael@0: installItem.type = type; michael@0: if (install.certName) { michael@0: installItem.cert = bundle.getFormattedString("signed", [install.certName]); michael@0: } michael@0: else { michael@0: installItem.cert = bundle.getString("unverified"); michael@0: } michael@0: installItem.signed = install.certName ? "true" : "false"; michael@0: } michael@0: michael@0: var introString = bundle.getString("itemWarnIntroSingle"); michael@0: if (numItemsToInstall > 4) michael@0: introString = bundle.getFormattedString("itemWarnIntroMultiple", [numItemsToInstall]); michael@0: var textNode = document.createTextNode(introString); michael@0: var introNode = document.getElementById("itemWarningIntro"); michael@0: while (introNode.hasChildNodes()) michael@0: introNode.removeChild(introNode.firstChild); michael@0: introNode.appendChild(textNode); michael@0: michael@0: var okButton = document.documentElement.getButton("accept"); michael@0: okButton.focus(); michael@0: michael@0: function okButtonCountdown() { michael@0: _installCountdown -= 1; michael@0: michael@0: if (_installCountdown < 1) { michael@0: okButton.label = bundle.getString("installButtonLabel"); michael@0: okButton.disabled = false; michael@0: clearInterval(_installCountdownInterval); michael@0: } michael@0: else michael@0: okButton.label = bundle.getFormattedString("installButtonDisabledLabel", [_installCountdown]); michael@0: } michael@0: michael@0: function myfocus() { michael@0: // Clear the timeout if it exists so only the last one will be used. michael@0: if (_timeout) michael@0: clearTimeout(_timeout); michael@0: michael@0: // Use setTimeout since the last focus or blur event to fire is the one we michael@0: // want michael@0: _timeout = setTimeout(setWidgetsAfterFocus, 0); michael@0: } michael@0: michael@0: function setWidgetsAfterFocus() { michael@0: if (_focused) michael@0: return; michael@0: michael@0: _installCountdown = _installCountdownLength; michael@0: _installCountdownInterval = setInterval(okButtonCountdown, 500); michael@0: okButton.label = bundle.getFormattedString("installButtonDisabledLabel", [_installCountdown]); michael@0: _focused = true; michael@0: } michael@0: michael@0: function myblur() { michael@0: // Clear the timeout if it exists so only the last one will be used. michael@0: if (_timeout) michael@0: clearTimeout(_timeout); michael@0: michael@0: // Use setTimeout since the last focus or blur event to fire is the one we michael@0: // want michael@0: _timeout = setTimeout(setWidgetsAfterBlur, 0); michael@0: } michael@0: michael@0: function setWidgetsAfterBlur() { michael@0: if (!_focused) michael@0: return; michael@0: michael@0: // Set _installCountdown to the inital value set in setWidgetsAfterFocus michael@0: // plus 1 so when the window is focused there is immediate ui feedback. michael@0: _installCountdown = _installCountdownLength + 1; michael@0: okButton.label = bundle.getFormattedString("installButtonDisabledLabel", [_installCountdown]); michael@0: okButton.disabled = true; michael@0: clearInterval(_installCountdownInterval); michael@0: _focused = false; michael@0: } michael@0: michael@0: function myUnload() { michael@0: if (_installCountdownLength > 0) { michael@0: document.removeEventListener("focus", myfocus, true); michael@0: document.removeEventListener("blur", myblur, true); michael@0: } michael@0: window.removeEventListener("unload", myUnload, false); michael@0: michael@0: // Now perform the desired action - either install the michael@0: // addons or cancel the installations michael@0: if (XPInstallConfirm._installOK) { michael@0: for (let install of args.installs) michael@0: install.install(); michael@0: } michael@0: else { michael@0: for (let install of args.installs) michael@0: install.cancel(); michael@0: } michael@0: } michael@0: michael@0: window.addEventListener("unload", myUnload, false); michael@0: michael@0: if (_installCountdownLength > 0) { michael@0: document.addEventListener("focus", myfocus, true); michael@0: document.addEventListener("blur", myblur, true); michael@0: michael@0: okButton.disabled = true; michael@0: setWidgetsAfterFocus(); michael@0: } michael@0: else michael@0: okButton.label = bundle.getString("installButtonLabel"); michael@0: } michael@0: michael@0: XPInstallConfirm.onOK = function XPInstallConfirm_onOk() michael@0: { michael@0: Components.classes["@mozilla.org/base/telemetry;1"]. michael@0: getService(Components.interfaces.nsITelemetry). michael@0: getHistogramById("SECURITY_UI"). michael@0: add(Components.interfaces.nsISecurityUITelemetry.WARNING_CONFIRM_ADDON_INSTALL_CLICK_THROUGH); michael@0: // Perform the install or cancel after the window has unloaded michael@0: XPInstallConfirm._installOK = true; michael@0: return true; michael@0: } michael@0: michael@0: XPInstallConfirm.onCancel = function XPInstallConfirm_onCancel() michael@0: { michael@0: // Perform the install or cancel after the window has unloaded michael@0: XPInstallConfirm._installOK = false; michael@0: return true; michael@0: }