toolkit/mozapps/extensions/content/xpinstallConfirm.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 var XPInstallConfirm = {};
     9 XPInstallConfirm.init = function XPInstallConfirm_init()
    10 {
    11   var _installCountdown;
    12   var _installCountdownInterval;
    13   var _focused;
    14   var _timeout;
    16   // Default to cancelling the install when the window unloads
    17   XPInstallConfirm._installOK = false;
    19   var bundle = document.getElementById("xpinstallConfirmStrings");
    21   let args = window.arguments[0].wrappedJSObject;
    23   var _installCountdownLength = 5;
    24   try {
    25     var prefs = Components.classes["@mozilla.org/preferences-service;1"]
    26                           .getService(Components.interfaces.nsIPrefBranch);
    27     var delay_in_milliseconds = prefs.getIntPref("security.dialog_enable_delay");
    28     _installCountdownLength = Math.round(delay_in_milliseconds / 500);
    29   } catch (ex) { }
    31   var itemList = document.getElementById("itemList");
    33   var numItemsToInstall = args.installs.length;
    34   for (let install of args.installs) {
    35     var installItem = document.createElement("installitem");
    36     itemList.appendChild(installItem);
    38     installItem.name = install.addon.name;
    39     installItem.url = install.sourceURI.spec;
    40     var icon = install.iconURL;
    41     if (icon)
    42       installItem.icon = icon;
    43     var type = install.type;
    44     if (type)
    45       installItem.type = type;
    46     if (install.certName) {
    47       installItem.cert = bundle.getFormattedString("signed", [install.certName]);
    48     }
    49     else {
    50       installItem.cert = bundle.getString("unverified");
    51     }
    52     installItem.signed = install.certName ? "true" : "false";
    53   }
    55   var introString = bundle.getString("itemWarnIntroSingle");
    56   if (numItemsToInstall > 4)
    57     introString = bundle.getFormattedString("itemWarnIntroMultiple", [numItemsToInstall]);
    58   var textNode = document.createTextNode(introString);
    59   var introNode = document.getElementById("itemWarningIntro");
    60   while (introNode.hasChildNodes())
    61     introNode.removeChild(introNode.firstChild);
    62   introNode.appendChild(textNode);
    64   var okButton = document.documentElement.getButton("accept");
    65   okButton.focus();
    67   function okButtonCountdown() {
    68     _installCountdown -= 1;
    70     if (_installCountdown < 1) {
    71       okButton.label = bundle.getString("installButtonLabel");
    72       okButton.disabled = false;
    73       clearInterval(_installCountdownInterval);
    74     }
    75     else
    76       okButton.label = bundle.getFormattedString("installButtonDisabledLabel", [_installCountdown]);
    77   }
    79   function myfocus() {
    80     // Clear the timeout if it exists so only the last one will be used.
    81     if (_timeout)
    82       clearTimeout(_timeout);
    84     // Use setTimeout since the last focus or blur event to fire is the one we
    85     // want
    86     _timeout = setTimeout(setWidgetsAfterFocus, 0);
    87   }
    89   function setWidgetsAfterFocus() {
    90     if (_focused)
    91       return;
    93     _installCountdown = _installCountdownLength;
    94     _installCountdownInterval = setInterval(okButtonCountdown, 500);
    95     okButton.label = bundle.getFormattedString("installButtonDisabledLabel", [_installCountdown]);
    96     _focused = true;
    97   }
    99   function myblur() {
   100     // Clear the timeout if it exists so only the last one will be used.
   101     if (_timeout)
   102       clearTimeout(_timeout);
   104     // Use setTimeout since the last focus or blur event to fire is the one we
   105     // want
   106     _timeout = setTimeout(setWidgetsAfterBlur, 0);
   107   }
   109   function setWidgetsAfterBlur() {
   110     if (!_focused)
   111       return;
   113     // Set _installCountdown to the inital value set in setWidgetsAfterFocus
   114     // plus 1 so when the window is focused there is immediate ui feedback.
   115     _installCountdown = _installCountdownLength + 1;
   116     okButton.label = bundle.getFormattedString("installButtonDisabledLabel", [_installCountdown]);
   117     okButton.disabled = true;
   118     clearInterval(_installCountdownInterval);
   119     _focused = false;
   120   }
   122   function myUnload() {
   123     if (_installCountdownLength > 0) {
   124       document.removeEventListener("focus", myfocus, true);
   125       document.removeEventListener("blur", myblur, true);
   126     }
   127     window.removeEventListener("unload", myUnload, false);
   129     // Now perform the desired action - either install the
   130     // addons or cancel the installations
   131     if (XPInstallConfirm._installOK) {
   132       for (let install of args.installs)
   133         install.install();
   134     }
   135     else {
   136       for (let install of args.installs)
   137         install.cancel();
   138     }
   139   }
   141   window.addEventListener("unload", myUnload, false);
   143   if (_installCountdownLength > 0) {
   144     document.addEventListener("focus", myfocus, true);
   145     document.addEventListener("blur", myblur, true);
   147     okButton.disabled = true;
   148     setWidgetsAfterFocus();
   149   }
   150   else
   151     okButton.label = bundle.getString("installButtonLabel");
   152 }
   154 XPInstallConfirm.onOK = function XPInstallConfirm_onOk()
   155 {
   156   Components.classes["@mozilla.org/base/telemetry;1"].
   157     getService(Components.interfaces.nsITelemetry).
   158     getHistogramById("SECURITY_UI").
   159     add(Components.interfaces.nsISecurityUITelemetry.WARNING_CONFIRM_ADDON_INSTALL_CLICK_THROUGH);
   160   // Perform the install or cancel after the window has unloaded
   161   XPInstallConfirm._installOK = true;
   162   return true;
   163 }
   165 XPInstallConfirm.onCancel = function XPInstallConfirm_onCancel()
   166 {
   167   // Perform the install or cancel after the window has unloaded
   168   XPInstallConfirm._installOK = false;
   169   return true;
   170 }

mercurial