Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | "use strict"; |
michael@0 | 8 | |
michael@0 | 9 | Components.utils.import("resource://gre/modules/AddonManager.jsm"); |
michael@0 | 10 | Components.utils.import("resource://gre/modules/addons/AddonRepository.jsm"); |
michael@0 | 11 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | const Cc = Components.classes; |
michael@0 | 14 | const Ci = Components.interfaces; |
michael@0 | 15 | |
michael@0 | 16 | var gView = null; |
michael@0 | 17 | |
michael@0 | 18 | function showView(aView) { |
michael@0 | 19 | gView = aView; |
michael@0 | 20 | |
michael@0 | 21 | gView.show(); |
michael@0 | 22 | |
michael@0 | 23 | // If the view's show method immediately showed a different view then don't |
michael@0 | 24 | // do anything else |
michael@0 | 25 | if (gView != aView) |
michael@0 | 26 | return; |
michael@0 | 27 | |
michael@0 | 28 | let viewNode = document.getElementById(gView.nodeID); |
michael@0 | 29 | viewNode.parentNode.selectedPanel = viewNode; |
michael@0 | 30 | |
michael@0 | 31 | // For testing dispatch an event when the view changes |
michael@0 | 32 | var event = document.createEvent("Events"); |
michael@0 | 33 | event.initEvent("ViewChanged", true, true); |
michael@0 | 34 | viewNode.dispatchEvent(event); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | function showButtons(aCancel, aBack, aNext, aDone) { |
michael@0 | 38 | document.getElementById("cancel").hidden = !aCancel; |
michael@0 | 39 | document.getElementById("back").hidden = !aBack; |
michael@0 | 40 | document.getElementById("next").hidden = !aNext; |
michael@0 | 41 | document.getElementById("done").hidden = !aDone; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | function isAddonDistroInstalled(aID) { |
michael@0 | 45 | let branch = Services.prefs.getBranch("extensions.installedDistroAddon."); |
michael@0 | 46 | if (!branch.prefHasUserValue(aID)) |
michael@0 | 47 | return false; |
michael@0 | 48 | |
michael@0 | 49 | return branch.getBoolPref(aID); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | function orderForScope(aScope) { |
michael@0 | 53 | return aScope == AddonManager.SCOPE_PROFILE ? 1 : 0; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | var gAddons = {}; |
michael@0 | 57 | |
michael@0 | 58 | var gChecking = { |
michael@0 | 59 | nodeID: "checking", |
michael@0 | 60 | |
michael@0 | 61 | _progress: null, |
michael@0 | 62 | _addonCount: 0, |
michael@0 | 63 | _completeCount: 0, |
michael@0 | 64 | |
michael@0 | 65 | show: function gChecking_show() { |
michael@0 | 66 | showButtons(true, false, false, false); |
michael@0 | 67 | this._progress = document.getElementById("checking-progress"); |
michael@0 | 68 | |
michael@0 | 69 | let self = this; |
michael@0 | 70 | AddonManager.getAllAddons(function gChecking_getAllAddons(aAddons) { |
michael@0 | 71 | if (aAddons.length == 0) { |
michael@0 | 72 | window.close(); |
michael@0 | 73 | return; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | aAddons = aAddons.filter(function gChecking_filterAddons(aAddon) { |
michael@0 | 77 | if (aAddon.type == "plugin" || aAddon.type == "service") |
michael@0 | 78 | return false; |
michael@0 | 79 | |
michael@0 | 80 | if (aAddon.type == "theme") { |
michael@0 | 81 | // Don't show application shipped themes |
michael@0 | 82 | if (aAddon.scope == AddonManager.SCOPE_APPLICATION) |
michael@0 | 83 | return false; |
michael@0 | 84 | // Don't show already disabled themes |
michael@0 | 85 | if (aAddon.userDisabled) |
michael@0 | 86 | return false; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | return true; |
michael@0 | 90 | }); |
michael@0 | 91 | |
michael@0 | 92 | self._addonCount = aAddons.length; |
michael@0 | 93 | self._progress.value = 0; |
michael@0 | 94 | self._progress.max = aAddons.length; |
michael@0 | 95 | self._progress.mode = "determined"; |
michael@0 | 96 | |
michael@0 | 97 | // Ensure compatibility overrides are up to date before checking for |
michael@0 | 98 | // individual addon updates. |
michael@0 | 99 | let ids = [addon.id for each (addon in aAddons)]; |
michael@0 | 100 | AddonRepository.repopulateCache(ids, function gChecking_repopulateCache() { |
michael@0 | 101 | for (let addonItem of aAddons) { |
michael@0 | 102 | // Ignore disabled themes |
michael@0 | 103 | if (addonItem.type != "theme" || !addonItem.userDisabled) { |
michael@0 | 104 | gAddons[addonItem.id] = { |
michael@0 | 105 | addon: addonItem, |
michael@0 | 106 | install: null, |
michael@0 | 107 | wasActive: addonItem.isActive |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | addonItem.findUpdates(self, AddonManager.UPDATE_WHEN_NEW_APP_INSTALLED); |
michael@0 | 112 | } |
michael@0 | 113 | }); |
michael@0 | 114 | }); |
michael@0 | 115 | }, |
michael@0 | 116 | |
michael@0 | 117 | onUpdateAvailable: function gChecking_onUpdateAvailable(aAddon, aInstall) { |
michael@0 | 118 | // If the add-on can be upgraded then remember the new version |
michael@0 | 119 | if (aAddon.permissions & AddonManager.PERM_CAN_UPGRADE) |
michael@0 | 120 | gAddons[aAddon.id].install = aInstall; |
michael@0 | 121 | }, |
michael@0 | 122 | |
michael@0 | 123 | onUpdateFinished: function gChecking_onUpdateFinished(aAddon, aError) { |
michael@0 | 124 | this._completeCount++; |
michael@0 | 125 | this._progress.value = this._completeCount; |
michael@0 | 126 | |
michael@0 | 127 | if (this._completeCount < this._addonCount) |
michael@0 | 128 | return; |
michael@0 | 129 | |
michael@0 | 130 | var addons = [gAddons[id] for (id in gAddons)]; |
michael@0 | 131 | |
michael@0 | 132 | addons.sort(function sortAddons(a, b) { |
michael@0 | 133 | let orderA = orderForScope(a.addon.scope); |
michael@0 | 134 | let orderB = orderForScope(b.addon.scope); |
michael@0 | 135 | |
michael@0 | 136 | if (orderA != orderB) |
michael@0 | 137 | return orderA - orderB; |
michael@0 | 138 | |
michael@0 | 139 | return String.localeCompare(a.addon.name, b.addon.name); |
michael@0 | 140 | }); |
michael@0 | 141 | |
michael@0 | 142 | let rows = document.getElementById("select-rows"); |
michael@0 | 143 | let lastAddon = null; |
michael@0 | 144 | for (let entry of addons) { |
michael@0 | 145 | if (lastAddon && |
michael@0 | 146 | orderForScope(entry.addon.scope) != orderForScope(lastAddon.scope)) { |
michael@0 | 147 | let separator = document.createElement("separator"); |
michael@0 | 148 | rows.appendChild(separator); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | let row = document.createElement("row"); |
michael@0 | 152 | row.setAttribute("id", entry.addon.id); |
michael@0 | 153 | row.setAttribute("class", "addon"); |
michael@0 | 154 | rows.appendChild(row); |
michael@0 | 155 | row.setAddon(entry.addon, entry.install, entry.wasActive, |
michael@0 | 156 | isAddonDistroInstalled(entry.addon.id)); |
michael@0 | 157 | |
michael@0 | 158 | if (entry.install) |
michael@0 | 159 | entry.install.addListener(gUpdate); |
michael@0 | 160 | |
michael@0 | 161 | lastAddon = entry.addon; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | showView(gSelect); |
michael@0 | 165 | } |
michael@0 | 166 | }; |
michael@0 | 167 | |
michael@0 | 168 | var gSelect = { |
michael@0 | 169 | nodeID: "select", |
michael@0 | 170 | |
michael@0 | 171 | show: function gSelect_show() { |
michael@0 | 172 | this.updateButtons(); |
michael@0 | 173 | }, |
michael@0 | 174 | |
michael@0 | 175 | updateButtons: function gSelect_updateButtons() { |
michael@0 | 176 | for (let row = document.getElementById("select-rows").firstChild; |
michael@0 | 177 | row; row = row.nextSibling) { |
michael@0 | 178 | if (row.localName == "separator") |
michael@0 | 179 | continue; |
michael@0 | 180 | |
michael@0 | 181 | if (row.action) { |
michael@0 | 182 | showButtons(false, false, true, false); |
michael@0 | 183 | return; |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | showButtons(false, false, false, true); |
michael@0 | 188 | }, |
michael@0 | 189 | |
michael@0 | 190 | next: function gSelect_next() { |
michael@0 | 191 | showView(gConfirm); |
michael@0 | 192 | }, |
michael@0 | 193 | |
michael@0 | 194 | done: function gSelect_done() { |
michael@0 | 195 | window.close(); |
michael@0 | 196 | } |
michael@0 | 197 | }; |
michael@0 | 198 | |
michael@0 | 199 | var gConfirm = { |
michael@0 | 200 | nodeID: "confirm", |
michael@0 | 201 | |
michael@0 | 202 | show: function gConfirm_show() { |
michael@0 | 203 | showButtons(false, true, false, true); |
michael@0 | 204 | |
michael@0 | 205 | let box = document.getElementById("confirm-scrollbox").firstChild; |
michael@0 | 206 | while (box) { |
michael@0 | 207 | box.hidden = true; |
michael@0 | 208 | while (box.lastChild != box.firstChild) |
michael@0 | 209 | box.removeChild(box.lastChild); |
michael@0 | 210 | box = box.nextSibling; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | for (let row = document.getElementById("select-rows").firstChild; |
michael@0 | 214 | row; row = row.nextSibling) { |
michael@0 | 215 | if (row.localName == "separator") |
michael@0 | 216 | continue; |
michael@0 | 217 | |
michael@0 | 218 | let action = row.action; |
michael@0 | 219 | if (!action) |
michael@0 | 220 | continue; |
michael@0 | 221 | |
michael@0 | 222 | let list = document.getElementById(action + "-list"); |
michael@0 | 223 | |
michael@0 | 224 | list.hidden = false; |
michael@0 | 225 | let item = document.createElement("hbox"); |
michael@0 | 226 | item.setAttribute("id", row._addon.id); |
michael@0 | 227 | item.setAttribute("class", "addon"); |
michael@0 | 228 | item.setAttribute("type", row._addon.type); |
michael@0 | 229 | item.setAttribute("name", row._addon.name); |
michael@0 | 230 | if (action == "update" || action == "enable") |
michael@0 | 231 | item.setAttribute("active", "true"); |
michael@0 | 232 | list.appendChild(item); |
michael@0 | 233 | |
michael@0 | 234 | if (action == "update") |
michael@0 | 235 | showButtons(false, true, true, false); |
michael@0 | 236 | } |
michael@0 | 237 | }, |
michael@0 | 238 | |
michael@0 | 239 | back: function gConfirm_back() { |
michael@0 | 240 | showView(gSelect); |
michael@0 | 241 | }, |
michael@0 | 242 | |
michael@0 | 243 | next: function gConfirm_next() { |
michael@0 | 244 | showView(gUpdate); |
michael@0 | 245 | }, |
michael@0 | 246 | |
michael@0 | 247 | done: function gConfirm_done() { |
michael@0 | 248 | for (let row = document.getElementById("select-rows").firstChild; |
michael@0 | 249 | row; row = row.nextSibling) { |
michael@0 | 250 | if (row.localName != "separator") |
michael@0 | 251 | row.apply(); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | window.close(); |
michael@0 | 255 | } |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | var gUpdate = { |
michael@0 | 259 | nodeID: "update", |
michael@0 | 260 | |
michael@0 | 261 | _progress: null, |
michael@0 | 262 | _waitingCount: 0, |
michael@0 | 263 | _completeCount: 0, |
michael@0 | 264 | _errorCount: 0, |
michael@0 | 265 | |
michael@0 | 266 | show: function gUpdate_show() { |
michael@0 | 267 | showButtons(true, false, false, false); |
michael@0 | 268 | |
michael@0 | 269 | this._progress = document.getElementById("update-progress"); |
michael@0 | 270 | |
michael@0 | 271 | for (let row = document.getElementById("select-rows").firstChild; |
michael@0 | 272 | row; row = row.nextSibling) { |
michael@0 | 273 | if (row.localName != "separator") |
michael@0 | 274 | row.apply(); |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | this._progress.mode = "determined"; |
michael@0 | 278 | this._progress.max = this._waitingCount; |
michael@0 | 279 | this._progress.value = this._completeCount; |
michael@0 | 280 | }, |
michael@0 | 281 | |
michael@0 | 282 | checkComplete: function gUpdate_checkComplete() { |
michael@0 | 283 | this._progress.value = this._completeCount; |
michael@0 | 284 | if (this._completeCount < this._waitingCount) |
michael@0 | 285 | return; |
michael@0 | 286 | |
michael@0 | 287 | if (this._errorCount > 0) { |
michael@0 | 288 | showView(gErrors); |
michael@0 | 289 | return; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | window.close(); |
michael@0 | 293 | }, |
michael@0 | 294 | |
michael@0 | 295 | onDownloadStarted: function gUpdate_onDownloadStarted(aInstall) { |
michael@0 | 296 | this._waitingCount++; |
michael@0 | 297 | }, |
michael@0 | 298 | |
michael@0 | 299 | onDownloadFailed: function gUpdate_onDownloadFailed(aInstall) { |
michael@0 | 300 | this._errorCount++; |
michael@0 | 301 | this._completeCount++; |
michael@0 | 302 | this.checkComplete(); |
michael@0 | 303 | }, |
michael@0 | 304 | |
michael@0 | 305 | onInstallFailed: function gUpdate_onInstallFailed(aInstall) { |
michael@0 | 306 | this._errorCount++; |
michael@0 | 307 | this._completeCount++; |
michael@0 | 308 | this.checkComplete(); |
michael@0 | 309 | }, |
michael@0 | 310 | |
michael@0 | 311 | onInstallEnded: function gUpdate_onInstallEnded(aInstall) { |
michael@0 | 312 | this._completeCount++; |
michael@0 | 313 | this.checkComplete(); |
michael@0 | 314 | } |
michael@0 | 315 | }; |
michael@0 | 316 | |
michael@0 | 317 | var gErrors = { |
michael@0 | 318 | nodeID: "errors", |
michael@0 | 319 | |
michael@0 | 320 | show: function gErrors_show() { |
michael@0 | 321 | showButtons(false, false, false, true); |
michael@0 | 322 | }, |
michael@0 | 323 | |
michael@0 | 324 | done: function gErrors_done() { |
michael@0 | 325 | window.close(); |
michael@0 | 326 | } |
michael@0 | 327 | }; |
michael@0 | 328 | |
michael@0 | 329 | window.addEventListener("load", function loadEventListener() { |
michael@0 | 330 | showView(gChecking); }, false); |
michael@0 | 331 | |
michael@0 | 332 | // When closing the window cancel any pending or in-progress installs |
michael@0 | 333 | window.addEventListener("unload", function unloadEventListener() { |
michael@0 | 334 | for (let id in gAddons) { |
michael@0 | 335 | let entry = gAddons[id]; |
michael@0 | 336 | if (!entry.install) |
michael@0 | 337 | return; |
michael@0 | 338 | |
michael@0 | 339 | aEntry.install.removeListener(gUpdate); |
michael@0 | 340 | |
michael@0 | 341 | if (entry.install.state != AddonManager.STATE_INSTALLED && |
michael@0 | 342 | entry.install.state != AddonManager.STATE_DOWNLOAD_FAILED && |
michael@0 | 343 | entry.install.state != AddonManager.STATE_INSTALL_FAILED) { |
michael@0 | 344 | entry.install.cancel(); |
michael@0 | 345 | } |
michael@0 | 346 | } |
michael@0 | 347 | }, false); |