|
1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
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/. */ |
|
6 |
|
7 var XPInstallConfirm = {}; |
|
8 |
|
9 XPInstallConfirm.init = function XPInstallConfirm_init() |
|
10 { |
|
11 var _installCountdown; |
|
12 var _installCountdownInterval; |
|
13 var _focused; |
|
14 var _timeout; |
|
15 |
|
16 // Default to cancelling the install when the window unloads |
|
17 XPInstallConfirm._installOK = false; |
|
18 |
|
19 var bundle = document.getElementById("xpinstallConfirmStrings"); |
|
20 |
|
21 let args = window.arguments[0].wrappedJSObject; |
|
22 |
|
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) { } |
|
30 |
|
31 var itemList = document.getElementById("itemList"); |
|
32 |
|
33 var numItemsToInstall = args.installs.length; |
|
34 for (let install of args.installs) { |
|
35 var installItem = document.createElement("installitem"); |
|
36 itemList.appendChild(installItem); |
|
37 |
|
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 } |
|
54 |
|
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); |
|
63 |
|
64 var okButton = document.documentElement.getButton("accept"); |
|
65 okButton.focus(); |
|
66 |
|
67 function okButtonCountdown() { |
|
68 _installCountdown -= 1; |
|
69 |
|
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 } |
|
78 |
|
79 function myfocus() { |
|
80 // Clear the timeout if it exists so only the last one will be used. |
|
81 if (_timeout) |
|
82 clearTimeout(_timeout); |
|
83 |
|
84 // Use setTimeout since the last focus or blur event to fire is the one we |
|
85 // want |
|
86 _timeout = setTimeout(setWidgetsAfterFocus, 0); |
|
87 } |
|
88 |
|
89 function setWidgetsAfterFocus() { |
|
90 if (_focused) |
|
91 return; |
|
92 |
|
93 _installCountdown = _installCountdownLength; |
|
94 _installCountdownInterval = setInterval(okButtonCountdown, 500); |
|
95 okButton.label = bundle.getFormattedString("installButtonDisabledLabel", [_installCountdown]); |
|
96 _focused = true; |
|
97 } |
|
98 |
|
99 function myblur() { |
|
100 // Clear the timeout if it exists so only the last one will be used. |
|
101 if (_timeout) |
|
102 clearTimeout(_timeout); |
|
103 |
|
104 // Use setTimeout since the last focus or blur event to fire is the one we |
|
105 // want |
|
106 _timeout = setTimeout(setWidgetsAfterBlur, 0); |
|
107 } |
|
108 |
|
109 function setWidgetsAfterBlur() { |
|
110 if (!_focused) |
|
111 return; |
|
112 |
|
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 } |
|
121 |
|
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); |
|
128 |
|
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 } |
|
140 |
|
141 window.addEventListener("unload", myUnload, false); |
|
142 |
|
143 if (_installCountdownLength > 0) { |
|
144 document.addEventListener("focus", myfocus, true); |
|
145 document.addEventListener("blur", myblur, true); |
|
146 |
|
147 okButton.disabled = true; |
|
148 setWidgetsAfterFocus(); |
|
149 } |
|
150 else |
|
151 okButton.label = bundle.getString("installButtonLabel"); |
|
152 } |
|
153 |
|
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 } |
|
164 |
|
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 } |