Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const Cc = Components.classes; |
michael@0 | 8 | const Ci = Components.interfaces; |
michael@0 | 9 | const Cu = Components.utils; |
michael@0 | 10 | const Cr = Components.results; |
michael@0 | 11 | |
michael@0 | 12 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 13 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 14 | Cu.import("resource://gre/modules/DownloadUtils.jsm"); |
michael@0 | 15 | Cu.import("resource://gre/modules/AddonManager.jsm"); |
michael@0 | 16 | Cu.import("resource://gre/modules/addons/AddonRepository.jsm"); |
michael@0 | 17 | |
michael@0 | 18 | XPCOMUtils.defineLazyModuleGetter(this, "PluralForm", |
michael@0 | 19 | "resource://gre/modules/PluralForm.jsm"); |
michael@0 | 20 | |
michael@0 | 21 | XPCOMUtils.defineLazyGetter(this, "BrowserToolboxProcess", function () { |
michael@0 | 22 | return Cu.import("resource:///modules/devtools/ToolboxProcess.jsm", {}). |
michael@0 | 23 | BrowserToolboxProcess; |
michael@0 | 24 | }); |
michael@0 | 25 | XPCOMUtils.defineLazyModuleGetter(this, "Experiments", |
michael@0 | 26 | "resource:///modules/experiments/Experiments.jsm"); |
michael@0 | 27 | |
michael@0 | 28 | const PREF_DISCOVERURL = "extensions.webservice.discoverURL"; |
michael@0 | 29 | const PREF_DISCOVER_ENABLED = "extensions.getAddons.showPane"; |
michael@0 | 30 | const PREF_XPI_ENABLED = "xpinstall.enabled"; |
michael@0 | 31 | const PREF_MAXRESULTS = "extensions.getAddons.maxResults"; |
michael@0 | 32 | const PREF_GETADDONS_CACHE_ENABLED = "extensions.getAddons.cache.enabled"; |
michael@0 | 33 | const PREF_GETADDONS_CACHE_ID_ENABLED = "extensions.%ID%.getAddons.cache.enabled"; |
michael@0 | 34 | const PREF_UI_TYPE_HIDDEN = "extensions.ui.%TYPE%.hidden"; |
michael@0 | 35 | const PREF_UI_LASTCATEGORY = "extensions.ui.lastCategory"; |
michael@0 | 36 | const PREF_ADDON_DEBUGGING_ENABLED = "devtools.chrome.enabled"; |
michael@0 | 37 | const PREF_REMOTE_DEBUGGING_ENABLED = "devtools.debugger.remote-enabled"; |
michael@0 | 38 | |
michael@0 | 39 | const LOADING_MSG_DELAY = 100; |
michael@0 | 40 | |
michael@0 | 41 | const SEARCH_SCORE_MULTIPLIER_NAME = 2; |
michael@0 | 42 | const SEARCH_SCORE_MULTIPLIER_DESCRIPTION = 2; |
michael@0 | 43 | |
michael@0 | 44 | // Use integers so search scores are sortable by nsIXULSortService |
michael@0 | 45 | const SEARCH_SCORE_MATCH_WHOLEWORD = 10; |
michael@0 | 46 | const SEARCH_SCORE_MATCH_WORDBOUNDRY = 6; |
michael@0 | 47 | const SEARCH_SCORE_MATCH_SUBSTRING = 3; |
michael@0 | 48 | |
michael@0 | 49 | const UPDATES_RECENT_TIMESPAN = 2 * 24 * 3600000; // 2 days (in milliseconds) |
michael@0 | 50 | const UPDATES_RELEASENOTES_TRANSFORMFILE = "chrome://mozapps/content/extensions/updateinfo.xsl"; |
michael@0 | 51 | |
michael@0 | 52 | const XMLURI_PARSE_ERROR = "http://www.mozilla.org/newlayout/xml/parsererror.xml" |
michael@0 | 53 | |
michael@0 | 54 | const VIEW_DEFAULT = "addons://discover/"; |
michael@0 | 55 | |
michael@0 | 56 | var gStrings = {}; |
michael@0 | 57 | XPCOMUtils.defineLazyServiceGetter(gStrings, "bundleSvc", |
michael@0 | 58 | "@mozilla.org/intl/stringbundle;1", |
michael@0 | 59 | "nsIStringBundleService"); |
michael@0 | 60 | |
michael@0 | 61 | XPCOMUtils.defineLazyGetter(gStrings, "brand", function brandLazyGetter() { |
michael@0 | 62 | return this.bundleSvc.createBundle("chrome://branding/locale/brand.properties"); |
michael@0 | 63 | }); |
michael@0 | 64 | XPCOMUtils.defineLazyGetter(gStrings, "ext", function extLazyGetter() { |
michael@0 | 65 | return this.bundleSvc.createBundle("chrome://mozapps/locale/extensions/extensions.properties"); |
michael@0 | 66 | }); |
michael@0 | 67 | XPCOMUtils.defineLazyGetter(gStrings, "dl", function dlLazyGetter() { |
michael@0 | 68 | return this.bundleSvc.createBundle("chrome://mozapps/locale/downloads/downloads.properties"); |
michael@0 | 69 | }); |
michael@0 | 70 | |
michael@0 | 71 | XPCOMUtils.defineLazyGetter(gStrings, "brandShortName", function brandShortNameLazyGetter() { |
michael@0 | 72 | return this.brand.GetStringFromName("brandShortName"); |
michael@0 | 73 | }); |
michael@0 | 74 | XPCOMUtils.defineLazyGetter(gStrings, "appVersion", function appVersionLazyGetter() { |
michael@0 | 75 | return Services.appinfo.version; |
michael@0 | 76 | }); |
michael@0 | 77 | |
michael@0 | 78 | document.addEventListener("load", initialize, true); |
michael@0 | 79 | window.addEventListener("unload", shutdown, false); |
michael@0 | 80 | |
michael@0 | 81 | var gPendingInitializations = 1; |
michael@0 | 82 | this.__defineGetter__("gIsInitializing", function gIsInitializingGetter() gPendingInitializations > 0); |
michael@0 | 83 | |
michael@0 | 84 | function initialize(event) { |
michael@0 | 85 | // XXXbz this listener gets _all_ load events for all nodes in the |
michael@0 | 86 | // document... but relies on not being called "too early". |
michael@0 | 87 | if (event.target instanceof XMLStylesheetProcessingInstruction) { |
michael@0 | 88 | return; |
michael@0 | 89 | } |
michael@0 | 90 | document.removeEventListener("load", initialize, true); |
michael@0 | 91 | |
michael@0 | 92 | let globalCommandSet = document.getElementById("globalCommandSet"); |
michael@0 | 93 | globalCommandSet.addEventListener("command", function(event) { |
michael@0 | 94 | gViewController.doCommand(event.target.id); |
michael@0 | 95 | }); |
michael@0 | 96 | |
michael@0 | 97 | let viewCommandSet = document.getElementById("viewCommandSet"); |
michael@0 | 98 | viewCommandSet.addEventListener("commandupdate", function(event) { |
michael@0 | 99 | gViewController.updateCommands(); |
michael@0 | 100 | }); |
michael@0 | 101 | viewCommandSet.addEventListener("command", function(event) { |
michael@0 | 102 | gViewController.doCommand(event.target.id); |
michael@0 | 103 | }); |
michael@0 | 104 | |
michael@0 | 105 | let detailScreenshot = document.getElementById("detail-screenshot"); |
michael@0 | 106 | detailScreenshot.addEventListener("load", function(event) { |
michael@0 | 107 | this.removeAttribute("loading"); |
michael@0 | 108 | }); |
michael@0 | 109 | detailScreenshot.addEventListener("error", function(event) { |
michael@0 | 110 | this.setAttribute("loading", "error"); |
michael@0 | 111 | }); |
michael@0 | 112 | |
michael@0 | 113 | let addonPage = document.getElementById("addons-page"); |
michael@0 | 114 | addonPage.addEventListener("dragenter", function(event) { |
michael@0 | 115 | gDragDrop.onDragOver(event); |
michael@0 | 116 | }); |
michael@0 | 117 | addonPage.addEventListener("dragover", function(event) { |
michael@0 | 118 | gDragDrop.onDragOver(event); |
michael@0 | 119 | }); |
michael@0 | 120 | addonPage.addEventListener("drop", function(event) { |
michael@0 | 121 | gDragDrop.onDrop(event); |
michael@0 | 122 | }); |
michael@0 | 123 | addonPage.addEventListener("keypress", function(event) { |
michael@0 | 124 | gHeader.onKeyPress(event); |
michael@0 | 125 | }); |
michael@0 | 126 | |
michael@0 | 127 | gViewController.initialize(); |
michael@0 | 128 | gCategories.initialize(); |
michael@0 | 129 | gHeader.initialize(); |
michael@0 | 130 | gEventManager.initialize(); |
michael@0 | 131 | Services.obs.addObserver(sendEMPong, "EM-ping", false); |
michael@0 | 132 | Services.obs.notifyObservers(window, "EM-loaded", ""); |
michael@0 | 133 | |
michael@0 | 134 | // If the initial view has already been selected (by a call to loadView from |
michael@0 | 135 | // the above notifications) then bail out now |
michael@0 | 136 | if (gViewController.initialViewSelected) |
michael@0 | 137 | return; |
michael@0 | 138 | |
michael@0 | 139 | // If there is a history state to restore then use that |
michael@0 | 140 | if (window.history.state) { |
michael@0 | 141 | gViewController.updateState(window.history.state); |
michael@0 | 142 | return; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | // Default to the last selected category |
michael@0 | 146 | var view = gCategories.node.value; |
michael@0 | 147 | |
michael@0 | 148 | // Allow passing in a view through the window arguments |
michael@0 | 149 | if ("arguments" in window && window.arguments.length > 0 && |
michael@0 | 150 | window.arguments[0] !== null && "view" in window.arguments[0]) { |
michael@0 | 151 | view = window.arguments[0].view; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | gViewController.loadInitialView(view); |
michael@0 | 155 | |
michael@0 | 156 | Services.prefs.addObserver(PREF_ADDON_DEBUGGING_ENABLED, debuggingPrefChanged, false); |
michael@0 | 157 | Services.prefs.addObserver(PREF_REMOTE_DEBUGGING_ENABLED, debuggingPrefChanged, false); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | function notifyInitialized() { |
michael@0 | 161 | if (!gIsInitializing) |
michael@0 | 162 | return; |
michael@0 | 163 | |
michael@0 | 164 | gPendingInitializations--; |
michael@0 | 165 | if (!gIsInitializing) { |
michael@0 | 166 | var event = document.createEvent("Events"); |
michael@0 | 167 | event.initEvent("Initialized", true, true); |
michael@0 | 168 | document.dispatchEvent(event); |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | function shutdown() { |
michael@0 | 173 | gCategories.shutdown(); |
michael@0 | 174 | gSearchView.shutdown(); |
michael@0 | 175 | gEventManager.shutdown(); |
michael@0 | 176 | gViewController.shutdown(); |
michael@0 | 177 | Services.obs.removeObserver(sendEMPong, "EM-ping"); |
michael@0 | 178 | Services.prefs.removeObserver(PREF_ADDON_DEBUGGING_ENABLED, debuggingPrefChanged); |
michael@0 | 179 | Services.prefs.removeObserver(PREF_REMOTE_DEBUGGING_ENABLED, debuggingPrefChanged); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | function sendEMPong(aSubject, aTopic, aData) { |
michael@0 | 183 | Services.obs.notifyObservers(window, "EM-pong", ""); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | // Used by external callers to load a specific view into the manager |
michael@0 | 187 | function loadView(aViewId) { |
michael@0 | 188 | if (!gViewController.initialViewSelected) { |
michael@0 | 189 | // The caller opened the window and immediately loaded the view so it |
michael@0 | 190 | // should be the initial history entry |
michael@0 | 191 | |
michael@0 | 192 | gViewController.loadInitialView(aViewId); |
michael@0 | 193 | } else { |
michael@0 | 194 | gViewController.loadView(aViewId); |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | function isDiscoverEnabled() { |
michael@0 | 199 | if (Services.prefs.getPrefType(PREF_DISCOVERURL) == Services.prefs.PREF_INVALID) |
michael@0 | 200 | return false; |
michael@0 | 201 | |
michael@0 | 202 | try { |
michael@0 | 203 | if (!Services.prefs.getBoolPref(PREF_DISCOVER_ENABLED)) |
michael@0 | 204 | return false; |
michael@0 | 205 | } catch (e) {} |
michael@0 | 206 | |
michael@0 | 207 | try { |
michael@0 | 208 | if (!Services.prefs.getBoolPref(PREF_XPI_ENABLED)) |
michael@0 | 209 | return false; |
michael@0 | 210 | } catch (e) {} |
michael@0 | 211 | |
michael@0 | 212 | return true; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | function getExperimentEndDate(aAddon) { |
michael@0 | 216 | if (!("@mozilla.org/browser/experiments-service;1" in Cc)) { |
michael@0 | 217 | return 0; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | if (!aAddon.isActive) { |
michael@0 | 221 | return aAddon.endDate; |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | let experiment = Experiments.instance().getActiveExperiment(); |
michael@0 | 225 | if (!experiment) { |
michael@0 | 226 | return 0; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | return experiment.endDate; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | /** |
michael@0 | 233 | * Obtain the main DOMWindow for the current context. |
michael@0 | 234 | */ |
michael@0 | 235 | function getMainWindow() { |
michael@0 | 236 | return window.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 237 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 238 | .QueryInterface(Ci.nsIDocShellTreeItem) |
michael@0 | 239 | .rootTreeItem |
michael@0 | 240 | .QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 241 | .getInterface(Ci.nsIDOMWindow); |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | /** |
michael@0 | 245 | * Obtain the DOMWindow that can open a preferences pane. |
michael@0 | 246 | * |
michael@0 | 247 | * This is essentially "get the browser chrome window" with the added check |
michael@0 | 248 | * that the supposed browser chrome window is capable of opening a preferences |
michael@0 | 249 | * pane. |
michael@0 | 250 | * |
michael@0 | 251 | * This may return null if we can't find the browser chrome window. |
michael@0 | 252 | */ |
michael@0 | 253 | function getMainWindowWithPreferencesPane() { |
michael@0 | 254 | let mainWindow = getMainWindow(); |
michael@0 | 255 | if (mainWindow && "openAdvancedPreferences" in mainWindow) { |
michael@0 | 256 | return mainWindow; |
michael@0 | 257 | } else { |
michael@0 | 258 | return null; |
michael@0 | 259 | } |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | /** |
michael@0 | 263 | * A wrapper around the HTML5 session history service that allows the browser |
michael@0 | 264 | * back/forward controls to work within the manager |
michael@0 | 265 | */ |
michael@0 | 266 | var HTML5History = { |
michael@0 | 267 | get index() { |
michael@0 | 268 | return window.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 269 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 270 | .sessionHistory.index; |
michael@0 | 271 | }, |
michael@0 | 272 | |
michael@0 | 273 | get canGoBack() { |
michael@0 | 274 | return window.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 275 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 276 | .canGoBack; |
michael@0 | 277 | }, |
michael@0 | 278 | |
michael@0 | 279 | get canGoForward() { |
michael@0 | 280 | return window.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 281 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 282 | .canGoForward; |
michael@0 | 283 | }, |
michael@0 | 284 | |
michael@0 | 285 | back: function HTML5History_back() { |
michael@0 | 286 | window.history.back(); |
michael@0 | 287 | gViewController.updateCommand("cmd_back"); |
michael@0 | 288 | gViewController.updateCommand("cmd_forward"); |
michael@0 | 289 | }, |
michael@0 | 290 | |
michael@0 | 291 | forward: function HTML5History_forward() { |
michael@0 | 292 | window.history.forward(); |
michael@0 | 293 | gViewController.updateCommand("cmd_back"); |
michael@0 | 294 | gViewController.updateCommand("cmd_forward"); |
michael@0 | 295 | }, |
michael@0 | 296 | |
michael@0 | 297 | pushState: function HTML5History_pushState(aState) { |
michael@0 | 298 | window.history.pushState(aState, document.title); |
michael@0 | 299 | }, |
michael@0 | 300 | |
michael@0 | 301 | replaceState: function HTML5History_replaceState(aState) { |
michael@0 | 302 | window.history.replaceState(aState, document.title); |
michael@0 | 303 | }, |
michael@0 | 304 | |
michael@0 | 305 | popState: function HTML5History_popState() { |
michael@0 | 306 | function onStatePopped(aEvent) { |
michael@0 | 307 | window.removeEventListener("popstate", onStatePopped, true); |
michael@0 | 308 | // TODO To ensure we can't go forward again we put an additional entry |
michael@0 | 309 | // for the current state into the history. Ideally we would just strip |
michael@0 | 310 | // the history but there doesn't seem to be a way to do that. Bug 590661 |
michael@0 | 311 | window.history.pushState(aEvent.state, document.title); |
michael@0 | 312 | } |
michael@0 | 313 | window.addEventListener("popstate", onStatePopped, true); |
michael@0 | 314 | window.history.back(); |
michael@0 | 315 | gViewController.updateCommand("cmd_back"); |
michael@0 | 316 | gViewController.updateCommand("cmd_forward"); |
michael@0 | 317 | } |
michael@0 | 318 | }; |
michael@0 | 319 | |
michael@0 | 320 | /** |
michael@0 | 321 | * A wrapper around a fake history service |
michael@0 | 322 | */ |
michael@0 | 323 | var FakeHistory = { |
michael@0 | 324 | pos: 0, |
michael@0 | 325 | states: [null], |
michael@0 | 326 | |
michael@0 | 327 | get index() { |
michael@0 | 328 | return this.pos; |
michael@0 | 329 | }, |
michael@0 | 330 | |
michael@0 | 331 | get canGoBack() { |
michael@0 | 332 | return this.pos > 0; |
michael@0 | 333 | }, |
michael@0 | 334 | |
michael@0 | 335 | get canGoForward() { |
michael@0 | 336 | return (this.pos + 1) < this.states.length; |
michael@0 | 337 | }, |
michael@0 | 338 | |
michael@0 | 339 | back: function FakeHistory_back() { |
michael@0 | 340 | if (this.pos == 0) |
michael@0 | 341 | throw Components.Exception("Cannot go back from this point"); |
michael@0 | 342 | |
michael@0 | 343 | this.pos--; |
michael@0 | 344 | gViewController.updateState(this.states[this.pos]); |
michael@0 | 345 | gViewController.updateCommand("cmd_back"); |
michael@0 | 346 | gViewController.updateCommand("cmd_forward"); |
michael@0 | 347 | }, |
michael@0 | 348 | |
michael@0 | 349 | forward: function FakeHistory_forward() { |
michael@0 | 350 | if ((this.pos + 1) >= this.states.length) |
michael@0 | 351 | throw Components.Exception("Cannot go forward from this point"); |
michael@0 | 352 | |
michael@0 | 353 | this.pos++; |
michael@0 | 354 | gViewController.updateState(this.states[this.pos]); |
michael@0 | 355 | gViewController.updateCommand("cmd_back"); |
michael@0 | 356 | gViewController.updateCommand("cmd_forward"); |
michael@0 | 357 | }, |
michael@0 | 358 | |
michael@0 | 359 | pushState: function FakeHistory_pushState(aState) { |
michael@0 | 360 | this.pos++; |
michael@0 | 361 | this.states.splice(this.pos, this.states.length); |
michael@0 | 362 | this.states.push(aState); |
michael@0 | 363 | }, |
michael@0 | 364 | |
michael@0 | 365 | replaceState: function FakeHistory_replaceState(aState) { |
michael@0 | 366 | this.states[this.pos] = aState; |
michael@0 | 367 | }, |
michael@0 | 368 | |
michael@0 | 369 | popState: function FakeHistory_popState() { |
michael@0 | 370 | if (this.pos == 0) |
michael@0 | 371 | throw Components.Exception("Cannot popState from this view"); |
michael@0 | 372 | |
michael@0 | 373 | this.states.splice(this.pos, this.states.length); |
michael@0 | 374 | this.pos--; |
michael@0 | 375 | |
michael@0 | 376 | gViewController.updateState(this.states[this.pos]); |
michael@0 | 377 | gViewController.updateCommand("cmd_back"); |
michael@0 | 378 | gViewController.updateCommand("cmd_forward"); |
michael@0 | 379 | } |
michael@0 | 380 | }; |
michael@0 | 381 | |
michael@0 | 382 | // If the window has a session history then use the HTML5 History wrapper |
michael@0 | 383 | // otherwise use our fake history implementation |
michael@0 | 384 | if (window.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 385 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 386 | .sessionHistory) { |
michael@0 | 387 | var gHistory = HTML5History; |
michael@0 | 388 | } |
michael@0 | 389 | else { |
michael@0 | 390 | gHistory = FakeHistory; |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | var gEventManager = { |
michael@0 | 394 | _listeners: {}, |
michael@0 | 395 | _installListeners: [], |
michael@0 | 396 | |
michael@0 | 397 | initialize: function gEM_initialize() { |
michael@0 | 398 | var self = this; |
michael@0 | 399 | const ADDON_EVENTS = ["onEnabling", "onEnabled", "onDisabling", |
michael@0 | 400 | "onDisabled", "onUninstalling", "onUninstalled", |
michael@0 | 401 | "onInstalled", "onOperationCancelled", |
michael@0 | 402 | "onUpdateAvailable", "onUpdateFinished", |
michael@0 | 403 | "onCompatibilityUpdateAvailable", |
michael@0 | 404 | "onPropertyChanged"]; |
michael@0 | 405 | for (let evt of ADDON_EVENTS) { |
michael@0 | 406 | let event = evt; |
michael@0 | 407 | self[event] = function initialize_delegateAddonEvent(...aArgs) { |
michael@0 | 408 | self.delegateAddonEvent(event, aArgs); |
michael@0 | 409 | }; |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | const INSTALL_EVENTS = ["onNewInstall", "onDownloadStarted", |
michael@0 | 413 | "onDownloadEnded", "onDownloadFailed", |
michael@0 | 414 | "onDownloadProgress", "onDownloadCancelled", |
michael@0 | 415 | "onInstallStarted", "onInstallEnded", |
michael@0 | 416 | "onInstallFailed", "onInstallCancelled", |
michael@0 | 417 | "onExternalInstall"]; |
michael@0 | 418 | for (let evt of INSTALL_EVENTS) { |
michael@0 | 419 | let event = evt; |
michael@0 | 420 | self[event] = function initialize_delegateInstallEvent(...aArgs) { |
michael@0 | 421 | self.delegateInstallEvent(event, aArgs); |
michael@0 | 422 | }; |
michael@0 | 423 | } |
michael@0 | 424 | |
michael@0 | 425 | AddonManager.addManagerListener(this); |
michael@0 | 426 | AddonManager.addInstallListener(this); |
michael@0 | 427 | AddonManager.addAddonListener(this); |
michael@0 | 428 | |
michael@0 | 429 | this.refreshGlobalWarning(); |
michael@0 | 430 | this.refreshAutoUpdateDefault(); |
michael@0 | 431 | |
michael@0 | 432 | var contextMenu = document.getElementById("addonitem-popup"); |
michael@0 | 433 | contextMenu.addEventListener("popupshowing", function contextMenu_onPopupshowing() { |
michael@0 | 434 | var addon = gViewController.currentViewObj.getSelectedAddon(); |
michael@0 | 435 | contextMenu.setAttribute("addontype", addon.type); |
michael@0 | 436 | |
michael@0 | 437 | var menuSep = document.getElementById("addonitem-menuseparator"); |
michael@0 | 438 | var countEnabledMenuCmds = 0; |
michael@0 | 439 | for (let child of contextMenu.children) { |
michael@0 | 440 | if (child.nodeName == "menuitem" && |
michael@0 | 441 | gViewController.isCommandEnabled(child.command)) { |
michael@0 | 442 | countEnabledMenuCmds++; |
michael@0 | 443 | } |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | // with only one menu item, we hide the menu separator |
michael@0 | 447 | menuSep.hidden = (countEnabledMenuCmds <= 1); |
michael@0 | 448 | |
michael@0 | 449 | }, false); |
michael@0 | 450 | }, |
michael@0 | 451 | |
michael@0 | 452 | shutdown: function gEM_shutdown() { |
michael@0 | 453 | AddonManager.removeManagerListener(this); |
michael@0 | 454 | AddonManager.removeInstallListener(this); |
michael@0 | 455 | AddonManager.removeAddonListener(this); |
michael@0 | 456 | }, |
michael@0 | 457 | |
michael@0 | 458 | registerAddonListener: function gEM_registerAddonListener(aListener, aAddonId) { |
michael@0 | 459 | if (!(aAddonId in this._listeners)) |
michael@0 | 460 | this._listeners[aAddonId] = []; |
michael@0 | 461 | else if (this._listeners[aAddonId].indexOf(aListener) != -1) |
michael@0 | 462 | return; |
michael@0 | 463 | this._listeners[aAddonId].push(aListener); |
michael@0 | 464 | }, |
michael@0 | 465 | |
michael@0 | 466 | unregisterAddonListener: function gEM_unregisterAddonListener(aListener, aAddonId) { |
michael@0 | 467 | if (!(aAddonId in this._listeners)) |
michael@0 | 468 | return; |
michael@0 | 469 | var index = this._listeners[aAddonId].indexOf(aListener); |
michael@0 | 470 | if (index == -1) |
michael@0 | 471 | return; |
michael@0 | 472 | this._listeners[aAddonId].splice(index, 1); |
michael@0 | 473 | }, |
michael@0 | 474 | |
michael@0 | 475 | registerInstallListener: function gEM_registerInstallListener(aListener) { |
michael@0 | 476 | if (this._installListeners.indexOf(aListener) != -1) |
michael@0 | 477 | return; |
michael@0 | 478 | this._installListeners.push(aListener); |
michael@0 | 479 | }, |
michael@0 | 480 | |
michael@0 | 481 | unregisterInstallListener: function gEM_unregisterInstallListener(aListener) { |
michael@0 | 482 | var i = this._installListeners.indexOf(aListener); |
michael@0 | 483 | if (i == -1) |
michael@0 | 484 | return; |
michael@0 | 485 | this._installListeners.splice(i, 1); |
michael@0 | 486 | }, |
michael@0 | 487 | |
michael@0 | 488 | delegateAddonEvent: function gEM_delegateAddonEvent(aEvent, aParams) { |
michael@0 | 489 | var addon = aParams.shift(); |
michael@0 | 490 | if (!(addon.id in this._listeners)) |
michael@0 | 491 | return; |
michael@0 | 492 | |
michael@0 | 493 | var listeners = this._listeners[addon.id]; |
michael@0 | 494 | for (let listener of listeners) { |
michael@0 | 495 | if (!(aEvent in listener)) |
michael@0 | 496 | continue; |
michael@0 | 497 | try { |
michael@0 | 498 | listener[aEvent].apply(listener, aParams); |
michael@0 | 499 | } catch(e) { |
michael@0 | 500 | // this shouldn't be fatal |
michael@0 | 501 | Cu.reportError(e); |
michael@0 | 502 | } |
michael@0 | 503 | } |
michael@0 | 504 | }, |
michael@0 | 505 | |
michael@0 | 506 | delegateInstallEvent: function gEM_delegateInstallEvent(aEvent, aParams) { |
michael@0 | 507 | var existingAddon = aEvent == "onExternalInstall" ? aParams[1] : aParams[0].existingAddon; |
michael@0 | 508 | // If the install is an update then send the event to all listeners |
michael@0 | 509 | // registered for the existing add-on |
michael@0 | 510 | if (existingAddon) |
michael@0 | 511 | this.delegateAddonEvent(aEvent, [existingAddon].concat(aParams)); |
michael@0 | 512 | |
michael@0 | 513 | for (let listener of this._installListeners) { |
michael@0 | 514 | if (!(aEvent in listener)) |
michael@0 | 515 | continue; |
michael@0 | 516 | try { |
michael@0 | 517 | listener[aEvent].apply(listener, aParams); |
michael@0 | 518 | } catch(e) { |
michael@0 | 519 | // this shouldn't be fatal |
michael@0 | 520 | Cu.reportError(e); |
michael@0 | 521 | } |
michael@0 | 522 | } |
michael@0 | 523 | }, |
michael@0 | 524 | |
michael@0 | 525 | refreshGlobalWarning: function gEM_refreshGlobalWarning() { |
michael@0 | 526 | var page = document.getElementById("addons-page"); |
michael@0 | 527 | |
michael@0 | 528 | if (Services.appinfo.inSafeMode) { |
michael@0 | 529 | page.setAttribute("warning", "safemode"); |
michael@0 | 530 | return; |
michael@0 | 531 | } |
michael@0 | 532 | |
michael@0 | 533 | if (AddonManager.checkUpdateSecurityDefault && |
michael@0 | 534 | !AddonManager.checkUpdateSecurity) { |
michael@0 | 535 | page.setAttribute("warning", "updatesecurity"); |
michael@0 | 536 | return; |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | if (!AddonManager.checkCompatibility) { |
michael@0 | 540 | page.setAttribute("warning", "checkcompatibility"); |
michael@0 | 541 | return; |
michael@0 | 542 | } |
michael@0 | 543 | |
michael@0 | 544 | page.removeAttribute("warning"); |
michael@0 | 545 | }, |
michael@0 | 546 | |
michael@0 | 547 | refreshAutoUpdateDefault: function gEM_refreshAutoUpdateDefault() { |
michael@0 | 548 | var updateEnabled = AddonManager.updateEnabled; |
michael@0 | 549 | var autoUpdateDefault = AddonManager.autoUpdateDefault; |
michael@0 | 550 | |
michael@0 | 551 | // The checkbox needs to reflect that both prefs need to be true |
michael@0 | 552 | // for updates to be checked for and applied automatically |
michael@0 | 553 | document.getElementById("utils-autoUpdateDefault") |
michael@0 | 554 | .setAttribute("checked", updateEnabled && autoUpdateDefault); |
michael@0 | 555 | |
michael@0 | 556 | document.getElementById("utils-resetAddonUpdatesToAutomatic").hidden = !autoUpdateDefault; |
michael@0 | 557 | document.getElementById("utils-resetAddonUpdatesToManual").hidden = autoUpdateDefault; |
michael@0 | 558 | }, |
michael@0 | 559 | |
michael@0 | 560 | onCompatibilityModeChanged: function gEM_onCompatibilityModeChanged() { |
michael@0 | 561 | this.refreshGlobalWarning(); |
michael@0 | 562 | }, |
michael@0 | 563 | |
michael@0 | 564 | onCheckUpdateSecurityChanged: function gEM_onCheckUpdateSecurityChanged() { |
michael@0 | 565 | this.refreshGlobalWarning(); |
michael@0 | 566 | }, |
michael@0 | 567 | |
michael@0 | 568 | onUpdateModeChanged: function gEM_onUpdateModeChanged() { |
michael@0 | 569 | this.refreshAutoUpdateDefault(); |
michael@0 | 570 | } |
michael@0 | 571 | }; |
michael@0 | 572 | |
michael@0 | 573 | |
michael@0 | 574 | var gViewController = { |
michael@0 | 575 | viewPort: null, |
michael@0 | 576 | currentViewId: "", |
michael@0 | 577 | currentViewObj: null, |
michael@0 | 578 | currentViewRequest: 0, |
michael@0 | 579 | viewObjects: {}, |
michael@0 | 580 | viewChangeCallback: null, |
michael@0 | 581 | initialViewSelected: false, |
michael@0 | 582 | lastHistoryIndex: -1, |
michael@0 | 583 | |
michael@0 | 584 | initialize: function gVC_initialize() { |
michael@0 | 585 | this.viewPort = document.getElementById("view-port"); |
michael@0 | 586 | |
michael@0 | 587 | this.viewObjects["search"] = gSearchView; |
michael@0 | 588 | this.viewObjects["discover"] = gDiscoverView; |
michael@0 | 589 | this.viewObjects["list"] = gListView; |
michael@0 | 590 | this.viewObjects["detail"] = gDetailView; |
michael@0 | 591 | this.viewObjects["updates"] = gUpdatesView; |
michael@0 | 592 | |
michael@0 | 593 | for each (let view in this.viewObjects) |
michael@0 | 594 | view.initialize(); |
michael@0 | 595 | |
michael@0 | 596 | window.controllers.appendController(this); |
michael@0 | 597 | |
michael@0 | 598 | window.addEventListener("popstate", |
michael@0 | 599 | function window_onStatePopped(e) { |
michael@0 | 600 | gViewController.updateState(e.state); |
michael@0 | 601 | }, |
michael@0 | 602 | false); |
michael@0 | 603 | }, |
michael@0 | 604 | |
michael@0 | 605 | shutdown: function gVC_shutdown() { |
michael@0 | 606 | if (this.currentViewObj) |
michael@0 | 607 | this.currentViewObj.hide(); |
michael@0 | 608 | this.currentViewRequest = 0; |
michael@0 | 609 | |
michael@0 | 610 | for each(let view in this.viewObjects) { |
michael@0 | 611 | if ("shutdown" in view) { |
michael@0 | 612 | try { |
michael@0 | 613 | view.shutdown(); |
michael@0 | 614 | } catch(e) { |
michael@0 | 615 | // this shouldn't be fatal |
michael@0 | 616 | Cu.reportError(e); |
michael@0 | 617 | } |
michael@0 | 618 | } |
michael@0 | 619 | } |
michael@0 | 620 | |
michael@0 | 621 | window.controllers.removeController(this); |
michael@0 | 622 | }, |
michael@0 | 623 | |
michael@0 | 624 | updateState: function gVC_updateState(state) { |
michael@0 | 625 | try { |
michael@0 | 626 | this.loadViewInternal(state.view, state.previousView, state); |
michael@0 | 627 | this.lastHistoryIndex = gHistory.index; |
michael@0 | 628 | } |
michael@0 | 629 | catch (e) { |
michael@0 | 630 | // The attempt to load the view failed, try moving further along history |
michael@0 | 631 | if (this.lastHistoryIndex > gHistory.index) { |
michael@0 | 632 | if (gHistory.canGoBack) |
michael@0 | 633 | gHistory.back(); |
michael@0 | 634 | else |
michael@0 | 635 | gViewController.replaceView(VIEW_DEFAULT); |
michael@0 | 636 | } else { |
michael@0 | 637 | if (gHistory.canGoForward) |
michael@0 | 638 | gHistory.forward(); |
michael@0 | 639 | else |
michael@0 | 640 | gViewController.replaceView(VIEW_DEFAULT); |
michael@0 | 641 | } |
michael@0 | 642 | } |
michael@0 | 643 | }, |
michael@0 | 644 | |
michael@0 | 645 | parseViewId: function gVC_parseViewId(aViewId) { |
michael@0 | 646 | var matchRegex = /^addons:\/\/([^\/]+)\/(.*)$/; |
michael@0 | 647 | var [,viewType, viewParam] = aViewId.match(matchRegex) || []; |
michael@0 | 648 | return {type: viewType, param: decodeURIComponent(viewParam)}; |
michael@0 | 649 | }, |
michael@0 | 650 | |
michael@0 | 651 | get isLoading() { |
michael@0 | 652 | return !this.currentViewObj || this.currentViewObj.node.hasAttribute("loading"); |
michael@0 | 653 | }, |
michael@0 | 654 | |
michael@0 | 655 | loadView: function gVC_loadView(aViewId) { |
michael@0 | 656 | var isRefresh = false; |
michael@0 | 657 | if (aViewId == this.currentViewId) { |
michael@0 | 658 | if (this.isLoading) |
michael@0 | 659 | return; |
michael@0 | 660 | if (!("refresh" in this.currentViewObj)) |
michael@0 | 661 | return; |
michael@0 | 662 | if (!this.currentViewObj.canRefresh()) |
michael@0 | 663 | return; |
michael@0 | 664 | isRefresh = true; |
michael@0 | 665 | } |
michael@0 | 666 | |
michael@0 | 667 | var state = { |
michael@0 | 668 | view: aViewId, |
michael@0 | 669 | previousView: this.currentViewId |
michael@0 | 670 | }; |
michael@0 | 671 | if (!isRefresh) { |
michael@0 | 672 | gHistory.pushState(state); |
michael@0 | 673 | this.lastHistoryIndex = gHistory.index; |
michael@0 | 674 | } |
michael@0 | 675 | this.loadViewInternal(aViewId, this.currentViewId, state); |
michael@0 | 676 | }, |
michael@0 | 677 | |
michael@0 | 678 | // Replaces the existing view with a new one, rewriting the current history |
michael@0 | 679 | // entry to match. |
michael@0 | 680 | replaceView: function gVC_replaceView(aViewId) { |
michael@0 | 681 | if (aViewId == this.currentViewId) |
michael@0 | 682 | return; |
michael@0 | 683 | |
michael@0 | 684 | var state = { |
michael@0 | 685 | view: aViewId, |
michael@0 | 686 | previousView: null |
michael@0 | 687 | }; |
michael@0 | 688 | gHistory.replaceState(state); |
michael@0 | 689 | this.loadViewInternal(aViewId, null, state); |
michael@0 | 690 | }, |
michael@0 | 691 | |
michael@0 | 692 | loadInitialView: function gVC_loadInitialView(aViewId) { |
michael@0 | 693 | var state = { |
michael@0 | 694 | view: aViewId, |
michael@0 | 695 | previousView: null |
michael@0 | 696 | }; |
michael@0 | 697 | gHistory.replaceState(state); |
michael@0 | 698 | |
michael@0 | 699 | this.loadViewInternal(aViewId, null, state); |
michael@0 | 700 | this.initialViewSelected = true; |
michael@0 | 701 | notifyInitialized(); |
michael@0 | 702 | }, |
michael@0 | 703 | |
michael@0 | 704 | loadViewInternal: function gVC_loadViewInternal(aViewId, aPreviousView, aState) { |
michael@0 | 705 | var view = this.parseViewId(aViewId); |
michael@0 | 706 | |
michael@0 | 707 | if (!view.type || !(view.type in this.viewObjects)) |
michael@0 | 708 | throw Components.Exception("Invalid view: " + view.type); |
michael@0 | 709 | |
michael@0 | 710 | var viewObj = this.viewObjects[view.type]; |
michael@0 | 711 | if (!viewObj.node) |
michael@0 | 712 | throw Components.Exception("Root node doesn't exist for '" + view.type + "' view"); |
michael@0 | 713 | |
michael@0 | 714 | if (this.currentViewObj && aViewId != aPreviousView) { |
michael@0 | 715 | try { |
michael@0 | 716 | let canHide = this.currentViewObj.hide(); |
michael@0 | 717 | if (canHide === false) |
michael@0 | 718 | return; |
michael@0 | 719 | this.viewPort.selectedPanel.removeAttribute("loading"); |
michael@0 | 720 | } catch (e) { |
michael@0 | 721 | // this shouldn't be fatal |
michael@0 | 722 | Cu.reportError(e); |
michael@0 | 723 | } |
michael@0 | 724 | } |
michael@0 | 725 | |
michael@0 | 726 | gCategories.select(aViewId, aPreviousView); |
michael@0 | 727 | |
michael@0 | 728 | this.currentViewId = aViewId; |
michael@0 | 729 | this.currentViewObj = viewObj; |
michael@0 | 730 | |
michael@0 | 731 | this.viewPort.selectedPanel = this.currentViewObj.node; |
michael@0 | 732 | this.viewPort.selectedPanel.setAttribute("loading", "true"); |
michael@0 | 733 | this.currentViewObj.node.focus(); |
michael@0 | 734 | |
michael@0 | 735 | if (aViewId == aPreviousView) |
michael@0 | 736 | this.currentViewObj.refresh(view.param, ++this.currentViewRequest, aState); |
michael@0 | 737 | else |
michael@0 | 738 | this.currentViewObj.show(view.param, ++this.currentViewRequest, aState); |
michael@0 | 739 | }, |
michael@0 | 740 | |
michael@0 | 741 | // Moves back in the document history and removes the current history entry |
michael@0 | 742 | popState: function gVC_popState(aCallback) { |
michael@0 | 743 | this.viewChangeCallback = aCallback; |
michael@0 | 744 | gHistory.popState(); |
michael@0 | 745 | }, |
michael@0 | 746 | |
michael@0 | 747 | notifyViewChanged: function gVC_notifyViewChanged() { |
michael@0 | 748 | this.viewPort.selectedPanel.removeAttribute("loading"); |
michael@0 | 749 | |
michael@0 | 750 | if (this.viewChangeCallback) { |
michael@0 | 751 | this.viewChangeCallback(); |
michael@0 | 752 | this.viewChangeCallback = null; |
michael@0 | 753 | } |
michael@0 | 754 | |
michael@0 | 755 | var event = document.createEvent("Events"); |
michael@0 | 756 | event.initEvent("ViewChanged", true, true); |
michael@0 | 757 | this.currentViewObj.node.dispatchEvent(event); |
michael@0 | 758 | }, |
michael@0 | 759 | |
michael@0 | 760 | commands: { |
michael@0 | 761 | cmd_back: { |
michael@0 | 762 | isEnabled: function cmd_back_isEnabled() { |
michael@0 | 763 | return gHistory.canGoBack; |
michael@0 | 764 | }, |
michael@0 | 765 | doCommand: function cmd_back_doCommand() { |
michael@0 | 766 | gHistory.back(); |
michael@0 | 767 | } |
michael@0 | 768 | }, |
michael@0 | 769 | |
michael@0 | 770 | cmd_forward: { |
michael@0 | 771 | isEnabled: function cmd_forward_isEnabled() { |
michael@0 | 772 | return gHistory.canGoForward; |
michael@0 | 773 | }, |
michael@0 | 774 | doCommand: function cmd_forward_doCommand() { |
michael@0 | 775 | gHistory.forward(); |
michael@0 | 776 | } |
michael@0 | 777 | }, |
michael@0 | 778 | |
michael@0 | 779 | cmd_focusSearch: { |
michael@0 | 780 | isEnabled: () => true, |
michael@0 | 781 | doCommand: function cmd_focusSearch_doCommand() { |
michael@0 | 782 | gHeader.focusSearchBox(); |
michael@0 | 783 | } |
michael@0 | 784 | }, |
michael@0 | 785 | |
michael@0 | 786 | cmd_restartApp: { |
michael@0 | 787 | isEnabled: function cmd_restartApp_isEnabled() true, |
michael@0 | 788 | doCommand: function cmd_restartApp_doCommand() { |
michael@0 | 789 | let cancelQuit = Cc["@mozilla.org/supports-PRBool;1"]. |
michael@0 | 790 | createInstance(Ci.nsISupportsPRBool); |
michael@0 | 791 | Services.obs.notifyObservers(cancelQuit, "quit-application-requested", |
michael@0 | 792 | "restart"); |
michael@0 | 793 | if (cancelQuit.data) |
michael@0 | 794 | return; // somebody canceled our quit request |
michael@0 | 795 | |
michael@0 | 796 | let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"]. |
michael@0 | 797 | getService(Ci.nsIAppStartup); |
michael@0 | 798 | appStartup.quit(Ci.nsIAppStartup.eAttemptQuit | Ci.nsIAppStartup.eRestart); |
michael@0 | 799 | } |
michael@0 | 800 | }, |
michael@0 | 801 | |
michael@0 | 802 | cmd_enableCheckCompatibility: { |
michael@0 | 803 | isEnabled: function cmd_enableCheckCompatibility_isEnabled() true, |
michael@0 | 804 | doCommand: function cmd_enableCheckCompatibility_doCommand() { |
michael@0 | 805 | AddonManager.checkCompatibility = true; |
michael@0 | 806 | } |
michael@0 | 807 | }, |
michael@0 | 808 | |
michael@0 | 809 | cmd_enableUpdateSecurity: { |
michael@0 | 810 | isEnabled: function cmd_enableUpdateSecurity_isEnabled() true, |
michael@0 | 811 | doCommand: function cmd_enableUpdateSecurity_doCommand() { |
michael@0 | 812 | AddonManager.checkUpdateSecurity = true; |
michael@0 | 813 | } |
michael@0 | 814 | }, |
michael@0 | 815 | |
michael@0 | 816 | cmd_pluginCheck: { |
michael@0 | 817 | isEnabled: function cmd_pluginCheck_isEnabled() true, |
michael@0 | 818 | doCommand: function cmd_pluginCheck_doCommand() { |
michael@0 | 819 | openURL(Services.urlFormatter.formatURLPref("plugins.update.url")); |
michael@0 | 820 | } |
michael@0 | 821 | }, |
michael@0 | 822 | |
michael@0 | 823 | cmd_toggleAutoUpdateDefault: { |
michael@0 | 824 | isEnabled: function cmd_toggleAutoUpdateDefault_isEnabled() true, |
michael@0 | 825 | doCommand: function cmd_toggleAutoUpdateDefault_doCommand() { |
michael@0 | 826 | if (!AddonManager.updateEnabled || !AddonManager.autoUpdateDefault) { |
michael@0 | 827 | // One or both of the prefs is false, i.e. the checkbox is not checked. |
michael@0 | 828 | // Now toggle both to true. If the user wants us to auto-update |
michael@0 | 829 | // add-ons, we also need to auto-check for updates. |
michael@0 | 830 | AddonManager.updateEnabled = true; |
michael@0 | 831 | AddonManager.autoUpdateDefault = true; |
michael@0 | 832 | } else { |
michael@0 | 833 | // Both prefs are true, i.e. the checkbox is checked. |
michael@0 | 834 | // Toggle the auto pref to false, but don't touch the enabled check. |
michael@0 | 835 | AddonManager.autoUpdateDefault = false; |
michael@0 | 836 | } |
michael@0 | 837 | } |
michael@0 | 838 | }, |
michael@0 | 839 | |
michael@0 | 840 | cmd_resetAddonAutoUpdate: { |
michael@0 | 841 | isEnabled: function cmd_resetAddonAutoUpdate_isEnabled() true, |
michael@0 | 842 | doCommand: function cmd_resetAddonAutoUpdate_doCommand() { |
michael@0 | 843 | AddonManager.getAllAddons(function cmd_resetAddonAutoUpdate_getAllAddons(aAddonList) { |
michael@0 | 844 | for (let addon of aAddonList) { |
michael@0 | 845 | if ("applyBackgroundUpdates" in addon) |
michael@0 | 846 | addon.applyBackgroundUpdates = AddonManager.AUTOUPDATE_DEFAULT; |
michael@0 | 847 | } |
michael@0 | 848 | }); |
michael@0 | 849 | } |
michael@0 | 850 | }, |
michael@0 | 851 | |
michael@0 | 852 | cmd_goToDiscoverPane: { |
michael@0 | 853 | isEnabled: function cmd_goToDiscoverPane_isEnabled() { |
michael@0 | 854 | return gDiscoverView.enabled; |
michael@0 | 855 | }, |
michael@0 | 856 | doCommand: function cmd_goToDiscoverPane_doCommand() { |
michael@0 | 857 | gViewController.loadView("addons://discover/"); |
michael@0 | 858 | } |
michael@0 | 859 | }, |
michael@0 | 860 | |
michael@0 | 861 | cmd_goToRecentUpdates: { |
michael@0 | 862 | isEnabled: function cmd_goToRecentUpdates_isEnabled() true, |
michael@0 | 863 | doCommand: function cmd_goToRecentUpdates_doCommand() { |
michael@0 | 864 | gViewController.loadView("addons://updates/recent"); |
michael@0 | 865 | } |
michael@0 | 866 | }, |
michael@0 | 867 | |
michael@0 | 868 | cmd_goToAvailableUpdates: { |
michael@0 | 869 | isEnabled: function cmd_goToAvailableUpdates_isEnabled() true, |
michael@0 | 870 | doCommand: function cmd_goToAvailableUpdates_doCommand() { |
michael@0 | 871 | gViewController.loadView("addons://updates/available"); |
michael@0 | 872 | } |
michael@0 | 873 | }, |
michael@0 | 874 | |
michael@0 | 875 | cmd_showItemDetails: { |
michael@0 | 876 | isEnabled: function cmd_showItemDetails_isEnabled(aAddon) { |
michael@0 | 877 | return !!aAddon && (gViewController.currentViewObj != gDetailView); |
michael@0 | 878 | }, |
michael@0 | 879 | doCommand: function cmd_showItemDetails_doCommand(aAddon, aScrollToPreferences) { |
michael@0 | 880 | gViewController.loadView("addons://detail/" + |
michael@0 | 881 | encodeURIComponent(aAddon.id) + |
michael@0 | 882 | (aScrollToPreferences ? "/preferences" : "")); |
michael@0 | 883 | } |
michael@0 | 884 | }, |
michael@0 | 885 | |
michael@0 | 886 | cmd_findAllUpdates: { |
michael@0 | 887 | inProgress: false, |
michael@0 | 888 | isEnabled: function cmd_findAllUpdates_isEnabled() !this.inProgress, |
michael@0 | 889 | doCommand: function cmd_findAllUpdates_doCommand() { |
michael@0 | 890 | this.inProgress = true; |
michael@0 | 891 | gViewController.updateCommand("cmd_findAllUpdates"); |
michael@0 | 892 | document.getElementById("updates-noneFound").hidden = true; |
michael@0 | 893 | document.getElementById("updates-progress").hidden = false; |
michael@0 | 894 | document.getElementById("updates-manualUpdatesFound-btn").hidden = true; |
michael@0 | 895 | |
michael@0 | 896 | var pendingChecks = 0; |
michael@0 | 897 | var numUpdated = 0; |
michael@0 | 898 | var numManualUpdates = 0; |
michael@0 | 899 | var restartNeeded = false; |
michael@0 | 900 | var self = this; |
michael@0 | 901 | |
michael@0 | 902 | function updateStatus() { |
michael@0 | 903 | if (pendingChecks > 0) |
michael@0 | 904 | return; |
michael@0 | 905 | |
michael@0 | 906 | self.inProgress = false; |
michael@0 | 907 | gViewController.updateCommand("cmd_findAllUpdates"); |
michael@0 | 908 | document.getElementById("updates-progress").hidden = true; |
michael@0 | 909 | gUpdatesView.maybeRefresh(); |
michael@0 | 910 | |
michael@0 | 911 | if (numManualUpdates > 0 && numUpdated == 0) { |
michael@0 | 912 | document.getElementById("updates-manualUpdatesFound-btn").hidden = false; |
michael@0 | 913 | return; |
michael@0 | 914 | } |
michael@0 | 915 | |
michael@0 | 916 | if (numUpdated == 0) { |
michael@0 | 917 | document.getElementById("updates-noneFound").hidden = false; |
michael@0 | 918 | return; |
michael@0 | 919 | } |
michael@0 | 920 | |
michael@0 | 921 | if (restartNeeded) { |
michael@0 | 922 | document.getElementById("updates-downloaded").hidden = false; |
michael@0 | 923 | document.getElementById("updates-restart-btn").hidden = false; |
michael@0 | 924 | } else { |
michael@0 | 925 | document.getElementById("updates-installed").hidden = false; |
michael@0 | 926 | } |
michael@0 | 927 | } |
michael@0 | 928 | |
michael@0 | 929 | var updateInstallListener = { |
michael@0 | 930 | onDownloadFailed: function cmd_findAllUpdates_downloadFailed() { |
michael@0 | 931 | pendingChecks--; |
michael@0 | 932 | updateStatus(); |
michael@0 | 933 | }, |
michael@0 | 934 | onInstallFailed: function cmd_findAllUpdates_installFailed() { |
michael@0 | 935 | pendingChecks--; |
michael@0 | 936 | updateStatus(); |
michael@0 | 937 | }, |
michael@0 | 938 | onInstallEnded: function cmd_findAllUpdates_installEnded(aInstall, aAddon) { |
michael@0 | 939 | pendingChecks--; |
michael@0 | 940 | numUpdated++; |
michael@0 | 941 | if (isPending(aInstall.existingAddon, "upgrade")) |
michael@0 | 942 | restartNeeded = true; |
michael@0 | 943 | updateStatus(); |
michael@0 | 944 | } |
michael@0 | 945 | }; |
michael@0 | 946 | |
michael@0 | 947 | var updateCheckListener = { |
michael@0 | 948 | onUpdateAvailable: function cmd_findAllUpdates_updateAvailable(aAddon, aInstall) { |
michael@0 | 949 | gEventManager.delegateAddonEvent("onUpdateAvailable", |
michael@0 | 950 | [aAddon, aInstall]); |
michael@0 | 951 | if (AddonManager.shouldAutoUpdate(aAddon)) { |
michael@0 | 952 | aInstall.addListener(updateInstallListener); |
michael@0 | 953 | aInstall.install(); |
michael@0 | 954 | } else { |
michael@0 | 955 | pendingChecks--; |
michael@0 | 956 | numManualUpdates++; |
michael@0 | 957 | updateStatus(); |
michael@0 | 958 | } |
michael@0 | 959 | }, |
michael@0 | 960 | onNoUpdateAvailable: function cmd_findAllUpdates_noUpdateAvailable(aAddon) { |
michael@0 | 961 | pendingChecks--; |
michael@0 | 962 | updateStatus(); |
michael@0 | 963 | }, |
michael@0 | 964 | onUpdateFinished: function cmd_findAllUpdates_updateFinished(aAddon, aError) { |
michael@0 | 965 | gEventManager.delegateAddonEvent("onUpdateFinished", |
michael@0 | 966 | [aAddon, aError]); |
michael@0 | 967 | } |
michael@0 | 968 | }; |
michael@0 | 969 | |
michael@0 | 970 | AddonManager.getAddonsByTypes(null, function cmd_findAllUpdates_getAddonsByTypes(aAddonList) { |
michael@0 | 971 | for (let addon of aAddonList) { |
michael@0 | 972 | if (addon.permissions & AddonManager.PERM_CAN_UPGRADE) { |
michael@0 | 973 | pendingChecks++; |
michael@0 | 974 | addon.findUpdates(updateCheckListener, |
michael@0 | 975 | AddonManager.UPDATE_WHEN_USER_REQUESTED); |
michael@0 | 976 | } |
michael@0 | 977 | } |
michael@0 | 978 | |
michael@0 | 979 | if (pendingChecks == 0) |
michael@0 | 980 | updateStatus(); |
michael@0 | 981 | }); |
michael@0 | 982 | } |
michael@0 | 983 | }, |
michael@0 | 984 | |
michael@0 | 985 | cmd_findItemUpdates: { |
michael@0 | 986 | isEnabled: function cmd_findItemUpdates_isEnabled(aAddon) { |
michael@0 | 987 | if (!aAddon) |
michael@0 | 988 | return false; |
michael@0 | 989 | return hasPermission(aAddon, "upgrade"); |
michael@0 | 990 | }, |
michael@0 | 991 | doCommand: function cmd_findItemUpdates_doCommand(aAddon) { |
michael@0 | 992 | var listener = { |
michael@0 | 993 | onUpdateAvailable: function cmd_findItemUpdates_updateAvailable(aAddon, aInstall) { |
michael@0 | 994 | gEventManager.delegateAddonEvent("onUpdateAvailable", |
michael@0 | 995 | [aAddon, aInstall]); |
michael@0 | 996 | if (AddonManager.shouldAutoUpdate(aAddon)) |
michael@0 | 997 | aInstall.install(); |
michael@0 | 998 | }, |
michael@0 | 999 | onNoUpdateAvailable: function cmd_findItemUpdates_noUpdateAvailable(aAddon) { |
michael@0 | 1000 | gEventManager.delegateAddonEvent("onNoUpdateAvailable", |
michael@0 | 1001 | [aAddon]); |
michael@0 | 1002 | } |
michael@0 | 1003 | }; |
michael@0 | 1004 | gEventManager.delegateAddonEvent("onCheckingUpdate", [aAddon]); |
michael@0 | 1005 | aAddon.findUpdates(listener, AddonManager.UPDATE_WHEN_USER_REQUESTED); |
michael@0 | 1006 | } |
michael@0 | 1007 | }, |
michael@0 | 1008 | |
michael@0 | 1009 | cmd_debugItem: { |
michael@0 | 1010 | doCommand: function cmd_debugItem_doCommand(aAddon) { |
michael@0 | 1011 | BrowserToolboxProcess.init({ addonID: aAddon.id }); |
michael@0 | 1012 | }, |
michael@0 | 1013 | |
michael@0 | 1014 | isEnabled: function cmd_debugItem_isEnabled(aAddon) { |
michael@0 | 1015 | let debuggerEnabled = Services.prefs. |
michael@0 | 1016 | getBoolPref(PREF_ADDON_DEBUGGING_ENABLED); |
michael@0 | 1017 | let remoteEnabled = Services.prefs. |
michael@0 | 1018 | getBoolPref(PREF_REMOTE_DEBUGGING_ENABLED); |
michael@0 | 1019 | return aAddon && aAddon.isDebuggable && debuggerEnabled && remoteEnabled; |
michael@0 | 1020 | } |
michael@0 | 1021 | }, |
michael@0 | 1022 | |
michael@0 | 1023 | cmd_showItemPreferences: { |
michael@0 | 1024 | isEnabled: function cmd_showItemPreferences_isEnabled(aAddon) { |
michael@0 | 1025 | if (!aAddon || !aAddon.isActive || !aAddon.optionsURL) |
michael@0 | 1026 | return false; |
michael@0 | 1027 | if (gViewController.currentViewObj == gDetailView && |
michael@0 | 1028 | aAddon.optionsType == AddonManager.OPTIONS_TYPE_INLINE) { |
michael@0 | 1029 | return false; |
michael@0 | 1030 | } |
michael@0 | 1031 | if (aAddon.optionsType == AddonManager.OPTIONS_TYPE_INLINE_INFO) |
michael@0 | 1032 | return false; |
michael@0 | 1033 | return true; |
michael@0 | 1034 | }, |
michael@0 | 1035 | doCommand: function cmd_showItemPreferences_doCommand(aAddon) { |
michael@0 | 1036 | if (aAddon.optionsType == AddonManager.OPTIONS_TYPE_INLINE) { |
michael@0 | 1037 | gViewController.commands.cmd_showItemDetails.doCommand(aAddon, true); |
michael@0 | 1038 | return; |
michael@0 | 1039 | } |
michael@0 | 1040 | var optionsURL = aAddon.optionsURL; |
michael@0 | 1041 | if (aAddon.optionsType == AddonManager.OPTIONS_TYPE_TAB && |
michael@0 | 1042 | openOptionsInTab(optionsURL)) { |
michael@0 | 1043 | return; |
michael@0 | 1044 | } |
michael@0 | 1045 | var windows = Services.wm.getEnumerator(null); |
michael@0 | 1046 | while (windows.hasMoreElements()) { |
michael@0 | 1047 | var win = windows.getNext(); |
michael@0 | 1048 | if (win.closed) { |
michael@0 | 1049 | continue; |
michael@0 | 1050 | } |
michael@0 | 1051 | if (win.document.documentURI == optionsURL) { |
michael@0 | 1052 | win.focus(); |
michael@0 | 1053 | return; |
michael@0 | 1054 | } |
michael@0 | 1055 | } |
michael@0 | 1056 | var features = "chrome,titlebar,toolbar,centerscreen"; |
michael@0 | 1057 | try { |
michael@0 | 1058 | var instantApply = Services.prefs.getBoolPref("browser.preferences.instantApply"); |
michael@0 | 1059 | features += instantApply ? ",dialog=no" : ",modal"; |
michael@0 | 1060 | } catch (e) { |
michael@0 | 1061 | features += ",modal"; |
michael@0 | 1062 | } |
michael@0 | 1063 | openDialog(optionsURL, "", features); |
michael@0 | 1064 | } |
michael@0 | 1065 | }, |
michael@0 | 1066 | |
michael@0 | 1067 | cmd_showItemAbout: { |
michael@0 | 1068 | isEnabled: function cmd_showItemAbout_isEnabled(aAddon) { |
michael@0 | 1069 | // XXXunf This may be applicable to install items too. See bug 561260 |
michael@0 | 1070 | return !!aAddon; |
michael@0 | 1071 | }, |
michael@0 | 1072 | doCommand: function cmd_showItemAbout_doCommand(aAddon) { |
michael@0 | 1073 | var aboutURL = aAddon.aboutURL; |
michael@0 | 1074 | if (aboutURL) |
michael@0 | 1075 | openDialog(aboutURL, "", "chrome,centerscreen,modal", aAddon); |
michael@0 | 1076 | else |
michael@0 | 1077 | openDialog("chrome://mozapps/content/extensions/about.xul", |
michael@0 | 1078 | "", "chrome,centerscreen,modal", aAddon); |
michael@0 | 1079 | } |
michael@0 | 1080 | }, |
michael@0 | 1081 | |
michael@0 | 1082 | cmd_enableItem: { |
michael@0 | 1083 | isEnabled: function cmd_enableItem_isEnabled(aAddon) { |
michael@0 | 1084 | if (!aAddon) |
michael@0 | 1085 | return false; |
michael@0 | 1086 | let addonType = AddonManager.addonTypes[aAddon.type]; |
michael@0 | 1087 | return (!(addonType.flags & AddonManager.TYPE_SUPPORTS_ASK_TO_ACTIVATE) && |
michael@0 | 1088 | hasPermission(aAddon, "enable")); |
michael@0 | 1089 | }, |
michael@0 | 1090 | doCommand: function cmd_enableItem_doCommand(aAddon) { |
michael@0 | 1091 | aAddon.userDisabled = false; |
michael@0 | 1092 | }, |
michael@0 | 1093 | getTooltip: function cmd_enableItem_getTooltip(aAddon) { |
michael@0 | 1094 | if (!aAddon) |
michael@0 | 1095 | return ""; |
michael@0 | 1096 | if (aAddon.operationsRequiringRestart & AddonManager.OP_NEEDS_RESTART_ENABLE) |
michael@0 | 1097 | return gStrings.ext.GetStringFromName("enableAddonRestartRequiredTooltip"); |
michael@0 | 1098 | return gStrings.ext.GetStringFromName("enableAddonTooltip"); |
michael@0 | 1099 | } |
michael@0 | 1100 | }, |
michael@0 | 1101 | |
michael@0 | 1102 | cmd_disableItem: { |
michael@0 | 1103 | isEnabled: function cmd_disableItem_isEnabled(aAddon) { |
michael@0 | 1104 | if (!aAddon) |
michael@0 | 1105 | return false; |
michael@0 | 1106 | let addonType = AddonManager.addonTypes[aAddon.type]; |
michael@0 | 1107 | return (!(addonType.flags & AddonManager.TYPE_SUPPORTS_ASK_TO_ACTIVATE) && |
michael@0 | 1108 | hasPermission(aAddon, "disable")); |
michael@0 | 1109 | }, |
michael@0 | 1110 | doCommand: function cmd_disableItem_doCommand(aAddon) { |
michael@0 | 1111 | aAddon.userDisabled = true; |
michael@0 | 1112 | }, |
michael@0 | 1113 | getTooltip: function cmd_disableItem_getTooltip(aAddon) { |
michael@0 | 1114 | if (!aAddon) |
michael@0 | 1115 | return ""; |
michael@0 | 1116 | if (aAddon.operationsRequiringRestart & AddonManager.OP_NEEDS_RESTART_DISABLE) |
michael@0 | 1117 | return gStrings.ext.GetStringFromName("disableAddonRestartRequiredTooltip"); |
michael@0 | 1118 | return gStrings.ext.GetStringFromName("disableAddonTooltip"); |
michael@0 | 1119 | } |
michael@0 | 1120 | }, |
michael@0 | 1121 | |
michael@0 | 1122 | cmd_installItem: { |
michael@0 | 1123 | isEnabled: function cmd_installItem_isEnabled(aAddon) { |
michael@0 | 1124 | if (!aAddon) |
michael@0 | 1125 | return false; |
michael@0 | 1126 | return aAddon.install && aAddon.install.state == AddonManager.STATE_AVAILABLE; |
michael@0 | 1127 | }, |
michael@0 | 1128 | doCommand: function cmd_installItem_doCommand(aAddon) { |
michael@0 | 1129 | function doInstall() { |
michael@0 | 1130 | gViewController.currentViewObj.getListItemForID(aAddon.id)._installStatus.installRemote(); |
michael@0 | 1131 | } |
michael@0 | 1132 | |
michael@0 | 1133 | if (gViewController.currentViewObj == gDetailView) |
michael@0 | 1134 | gViewController.popState(doInstall); |
michael@0 | 1135 | else |
michael@0 | 1136 | doInstall(); |
michael@0 | 1137 | } |
michael@0 | 1138 | }, |
michael@0 | 1139 | |
michael@0 | 1140 | cmd_purchaseItem: { |
michael@0 | 1141 | isEnabled: function cmd_purchaseItem_isEnabled(aAddon) { |
michael@0 | 1142 | if (!aAddon) |
michael@0 | 1143 | return false; |
michael@0 | 1144 | return !!aAddon.purchaseURL; |
michael@0 | 1145 | }, |
michael@0 | 1146 | doCommand: function cmd_purchaseItem_doCommand(aAddon) { |
michael@0 | 1147 | openURL(aAddon.purchaseURL); |
michael@0 | 1148 | } |
michael@0 | 1149 | }, |
michael@0 | 1150 | |
michael@0 | 1151 | cmd_uninstallItem: { |
michael@0 | 1152 | isEnabled: function cmd_uninstallItem_isEnabled(aAddon) { |
michael@0 | 1153 | if (!aAddon) |
michael@0 | 1154 | return false; |
michael@0 | 1155 | return hasPermission(aAddon, "uninstall"); |
michael@0 | 1156 | }, |
michael@0 | 1157 | doCommand: function cmd_uninstallItem_doCommand(aAddon) { |
michael@0 | 1158 | if (gViewController.currentViewObj != gDetailView) { |
michael@0 | 1159 | aAddon.uninstall(); |
michael@0 | 1160 | return; |
michael@0 | 1161 | } |
michael@0 | 1162 | |
michael@0 | 1163 | gViewController.popState(function cmd_uninstallItem_popState() { |
michael@0 | 1164 | gViewController.currentViewObj.getListItemForID(aAddon.id).uninstall(); |
michael@0 | 1165 | }); |
michael@0 | 1166 | }, |
michael@0 | 1167 | getTooltip: function cmd_uninstallItem_getTooltip(aAddon) { |
michael@0 | 1168 | if (!aAddon) |
michael@0 | 1169 | return ""; |
michael@0 | 1170 | if (aAddon.operationsRequiringRestart & AddonManager.OP_NEEDS_RESTART_UNINSTALL) |
michael@0 | 1171 | return gStrings.ext.GetStringFromName("uninstallAddonRestartRequiredTooltip"); |
michael@0 | 1172 | return gStrings.ext.GetStringFromName("uninstallAddonTooltip"); |
michael@0 | 1173 | } |
michael@0 | 1174 | }, |
michael@0 | 1175 | |
michael@0 | 1176 | cmd_cancelUninstallItem: { |
michael@0 | 1177 | isEnabled: function cmd_cancelUninstallItem_isEnabled(aAddon) { |
michael@0 | 1178 | if (!aAddon) |
michael@0 | 1179 | return false; |
michael@0 | 1180 | return isPending(aAddon, "uninstall"); |
michael@0 | 1181 | }, |
michael@0 | 1182 | doCommand: function cmd_cancelUninstallItem_doCommand(aAddon) { |
michael@0 | 1183 | aAddon.cancelUninstall(); |
michael@0 | 1184 | } |
michael@0 | 1185 | }, |
michael@0 | 1186 | |
michael@0 | 1187 | cmd_installFromFile: { |
michael@0 | 1188 | isEnabled: function cmd_installFromFile_isEnabled() true, |
michael@0 | 1189 | doCommand: function cmd_installFromFile_doCommand() { |
michael@0 | 1190 | const nsIFilePicker = Ci.nsIFilePicker; |
michael@0 | 1191 | var fp = Cc["@mozilla.org/filepicker;1"] |
michael@0 | 1192 | .createInstance(nsIFilePicker); |
michael@0 | 1193 | fp.init(window, |
michael@0 | 1194 | gStrings.ext.GetStringFromName("installFromFile.dialogTitle"), |
michael@0 | 1195 | nsIFilePicker.modeOpenMultiple); |
michael@0 | 1196 | try { |
michael@0 | 1197 | fp.appendFilter(gStrings.ext.GetStringFromName("installFromFile.filterName"), |
michael@0 | 1198 | "*.xpi;*.jar"); |
michael@0 | 1199 | fp.appendFilters(nsIFilePicker.filterAll); |
michael@0 | 1200 | } catch (e) { } |
michael@0 | 1201 | |
michael@0 | 1202 | if (fp.show() != nsIFilePicker.returnOK) |
michael@0 | 1203 | return; |
michael@0 | 1204 | |
michael@0 | 1205 | var files = fp.files; |
michael@0 | 1206 | var installs = []; |
michael@0 | 1207 | |
michael@0 | 1208 | function buildNextInstall() { |
michael@0 | 1209 | if (!files.hasMoreElements()) { |
michael@0 | 1210 | if (installs.length > 0) { |
michael@0 | 1211 | // Display the normal install confirmation for the installs |
michael@0 | 1212 | AddonManager.installAddonsFromWebpage("application/x-xpinstall", |
michael@0 | 1213 | window, null, installs); |
michael@0 | 1214 | } |
michael@0 | 1215 | return; |
michael@0 | 1216 | } |
michael@0 | 1217 | |
michael@0 | 1218 | var file = files.getNext(); |
michael@0 | 1219 | AddonManager.getInstallForFile(file, function cmd_installFromFile_getInstallForFile(aInstall) { |
michael@0 | 1220 | installs.push(aInstall); |
michael@0 | 1221 | buildNextInstall(); |
michael@0 | 1222 | }); |
michael@0 | 1223 | } |
michael@0 | 1224 | |
michael@0 | 1225 | buildNextInstall(); |
michael@0 | 1226 | } |
michael@0 | 1227 | }, |
michael@0 | 1228 | |
michael@0 | 1229 | cmd_cancelOperation: { |
michael@0 | 1230 | isEnabled: function cmd_cancelOperation_isEnabled(aAddon) { |
michael@0 | 1231 | if (!aAddon) |
michael@0 | 1232 | return false; |
michael@0 | 1233 | return aAddon.pendingOperations != AddonManager.PENDING_NONE; |
michael@0 | 1234 | }, |
michael@0 | 1235 | doCommand: function cmd_cancelOperation_doCommand(aAddon) { |
michael@0 | 1236 | if (isPending(aAddon, "install")) { |
michael@0 | 1237 | aAddon.install.cancel(); |
michael@0 | 1238 | } else if (isPending(aAddon, "upgrade")) { |
michael@0 | 1239 | aAddon.pendingUpgrade.install.cancel(); |
michael@0 | 1240 | } else if (isPending(aAddon, "uninstall")) { |
michael@0 | 1241 | aAddon.cancelUninstall(); |
michael@0 | 1242 | } else if (isPending(aAddon, "enable")) { |
michael@0 | 1243 | aAddon.userDisabled = true; |
michael@0 | 1244 | } else if (isPending(aAddon, "disable")) { |
michael@0 | 1245 | aAddon.userDisabled = false; |
michael@0 | 1246 | } |
michael@0 | 1247 | } |
michael@0 | 1248 | }, |
michael@0 | 1249 | |
michael@0 | 1250 | cmd_contribute: { |
michael@0 | 1251 | isEnabled: function cmd_contribute_isEnabled(aAddon) { |
michael@0 | 1252 | if (!aAddon) |
michael@0 | 1253 | return false; |
michael@0 | 1254 | return ("contributionURL" in aAddon && aAddon.contributionURL); |
michael@0 | 1255 | }, |
michael@0 | 1256 | doCommand: function cmd_contribute_doCommand(aAddon) { |
michael@0 | 1257 | openURL(aAddon.contributionURL); |
michael@0 | 1258 | } |
michael@0 | 1259 | }, |
michael@0 | 1260 | |
michael@0 | 1261 | cmd_askToActivateItem: { |
michael@0 | 1262 | isEnabled: function cmd_askToActivateItem_isEnabled(aAddon) { |
michael@0 | 1263 | if (!aAddon) |
michael@0 | 1264 | return false; |
michael@0 | 1265 | let addonType = AddonManager.addonTypes[aAddon.type]; |
michael@0 | 1266 | return ((addonType.flags & AddonManager.TYPE_SUPPORTS_ASK_TO_ACTIVATE) && |
michael@0 | 1267 | hasPermission(aAddon, "ask_to_activate")); |
michael@0 | 1268 | }, |
michael@0 | 1269 | doCommand: function cmd_askToActivateItem_doCommand(aAddon) { |
michael@0 | 1270 | aAddon.userDisabled = AddonManager.STATE_ASK_TO_ACTIVATE; |
michael@0 | 1271 | } |
michael@0 | 1272 | }, |
michael@0 | 1273 | |
michael@0 | 1274 | cmd_alwaysActivateItem: { |
michael@0 | 1275 | isEnabled: function cmd_alwaysActivateItem_isEnabled(aAddon) { |
michael@0 | 1276 | if (!aAddon) |
michael@0 | 1277 | return false; |
michael@0 | 1278 | let addonType = AddonManager.addonTypes[aAddon.type]; |
michael@0 | 1279 | return ((addonType.flags & AddonManager.TYPE_SUPPORTS_ASK_TO_ACTIVATE) && |
michael@0 | 1280 | hasPermission(aAddon, "enable")); |
michael@0 | 1281 | }, |
michael@0 | 1282 | doCommand: function cmd_alwaysActivateItem_doCommand(aAddon) { |
michael@0 | 1283 | aAddon.userDisabled = false; |
michael@0 | 1284 | } |
michael@0 | 1285 | }, |
michael@0 | 1286 | |
michael@0 | 1287 | cmd_neverActivateItem: { |
michael@0 | 1288 | isEnabled: function cmd_neverActivateItem_isEnabled(aAddon) { |
michael@0 | 1289 | if (!aAddon) |
michael@0 | 1290 | return false; |
michael@0 | 1291 | let addonType = AddonManager.addonTypes[aAddon.type]; |
michael@0 | 1292 | return ((addonType.flags & AddonManager.TYPE_SUPPORTS_ASK_TO_ACTIVATE) && |
michael@0 | 1293 | hasPermission(aAddon, "disable")); |
michael@0 | 1294 | }, |
michael@0 | 1295 | doCommand: function cmd_neverActivateItem_doCommand(aAddon) { |
michael@0 | 1296 | aAddon.userDisabled = true; |
michael@0 | 1297 | } |
michael@0 | 1298 | }, |
michael@0 | 1299 | |
michael@0 | 1300 | cmd_experimentsLearnMore: { |
michael@0 | 1301 | isEnabled: function cmd_experimentsLearnMore_isEnabled() { |
michael@0 | 1302 | let mainWindow = getMainWindow(); |
michael@0 | 1303 | return mainWindow && "switchToTabHavingURI" in mainWindow; |
michael@0 | 1304 | }, |
michael@0 | 1305 | doCommand: function cmd_experimentsLearnMore_doCommand() { |
michael@0 | 1306 | let url = Services.prefs.getCharPref("toolkit.telemetry.infoURL"); |
michael@0 | 1307 | openOptionsInTab(url); |
michael@0 | 1308 | }, |
michael@0 | 1309 | }, |
michael@0 | 1310 | |
michael@0 | 1311 | cmd_experimentsOpenTelemetryPreferences: { |
michael@0 | 1312 | isEnabled: function cmd_experimentsOpenTelemetryPreferences_isEnabled() { |
michael@0 | 1313 | return !!getMainWindowWithPreferencesPane(); |
michael@0 | 1314 | }, |
michael@0 | 1315 | doCommand: function cmd_experimentsOpenTelemetryPreferences_doCommand() { |
michael@0 | 1316 | let mainWindow = getMainWindowWithPreferencesPane(); |
michael@0 | 1317 | mainWindow.openAdvancedPreferences("dataChoicesTab"); |
michael@0 | 1318 | }, |
michael@0 | 1319 | }, |
michael@0 | 1320 | }, |
michael@0 | 1321 | |
michael@0 | 1322 | supportsCommand: function gVC_supportsCommand(aCommand) { |
michael@0 | 1323 | return (aCommand in this.commands); |
michael@0 | 1324 | }, |
michael@0 | 1325 | |
michael@0 | 1326 | isCommandEnabled: function gVC_isCommandEnabled(aCommand) { |
michael@0 | 1327 | if (!this.supportsCommand(aCommand)) |
michael@0 | 1328 | return false; |
michael@0 | 1329 | var addon = this.currentViewObj.getSelectedAddon(); |
michael@0 | 1330 | return this.commands[aCommand].isEnabled(addon); |
michael@0 | 1331 | }, |
michael@0 | 1332 | |
michael@0 | 1333 | updateCommands: function gVC_updateCommands() { |
michael@0 | 1334 | // wait until the view is initialized |
michael@0 | 1335 | if (!this.currentViewObj) |
michael@0 | 1336 | return; |
michael@0 | 1337 | var addon = this.currentViewObj.getSelectedAddon(); |
michael@0 | 1338 | for (let commandId in this.commands) |
michael@0 | 1339 | this.updateCommand(commandId, addon); |
michael@0 | 1340 | }, |
michael@0 | 1341 | |
michael@0 | 1342 | updateCommand: function gVC_updateCommand(aCommandId, aAddon) { |
michael@0 | 1343 | if (typeof aAddon == "undefined") |
michael@0 | 1344 | aAddon = this.currentViewObj.getSelectedAddon(); |
michael@0 | 1345 | var cmd = this.commands[aCommandId]; |
michael@0 | 1346 | var cmdElt = document.getElementById(aCommandId); |
michael@0 | 1347 | cmdElt.setAttribute("disabled", !cmd.isEnabled(aAddon)); |
michael@0 | 1348 | if ("getTooltip" in cmd) { |
michael@0 | 1349 | let tooltip = cmd.getTooltip(aAddon); |
michael@0 | 1350 | if (tooltip) |
michael@0 | 1351 | cmdElt.setAttribute("tooltiptext", tooltip); |
michael@0 | 1352 | else |
michael@0 | 1353 | cmdElt.removeAttribute("tooltiptext"); |
michael@0 | 1354 | } |
michael@0 | 1355 | }, |
michael@0 | 1356 | |
michael@0 | 1357 | doCommand: function gVC_doCommand(aCommand, aAddon) { |
michael@0 | 1358 | if (!this.supportsCommand(aCommand)) |
michael@0 | 1359 | return; |
michael@0 | 1360 | var cmd = this.commands[aCommand]; |
michael@0 | 1361 | if (!aAddon) |
michael@0 | 1362 | aAddon = this.currentViewObj.getSelectedAddon(); |
michael@0 | 1363 | if (!cmd.isEnabled(aAddon)) |
michael@0 | 1364 | return; |
michael@0 | 1365 | cmd.doCommand(aAddon); |
michael@0 | 1366 | }, |
michael@0 | 1367 | |
michael@0 | 1368 | onEvent: function gVC_onEvent() {} |
michael@0 | 1369 | }; |
michael@0 | 1370 | |
michael@0 | 1371 | function hasInlineOptions(aAddon) { |
michael@0 | 1372 | return (aAddon.optionsType == AddonManager.OPTIONS_TYPE_INLINE || |
michael@0 | 1373 | aAddon.optionsType == AddonManager.OPTIONS_TYPE_INLINE_INFO); |
michael@0 | 1374 | } |
michael@0 | 1375 | |
michael@0 | 1376 | function openOptionsInTab(optionsURL) { |
michael@0 | 1377 | let mainWindow = getMainWindow(); |
michael@0 | 1378 | if ("switchToTabHavingURI" in mainWindow) { |
michael@0 | 1379 | mainWindow.switchToTabHavingURI(optionsURL, true); |
michael@0 | 1380 | return true; |
michael@0 | 1381 | } |
michael@0 | 1382 | return false; |
michael@0 | 1383 | } |
michael@0 | 1384 | |
michael@0 | 1385 | function formatDate(aDate) { |
michael@0 | 1386 | return Cc["@mozilla.org/intl/scriptabledateformat;1"] |
michael@0 | 1387 | .getService(Ci.nsIScriptableDateFormat) |
michael@0 | 1388 | .FormatDate("", |
michael@0 | 1389 | Ci.nsIScriptableDateFormat.dateFormatLong, |
michael@0 | 1390 | aDate.getFullYear(), |
michael@0 | 1391 | aDate.getMonth() + 1, |
michael@0 | 1392 | aDate.getDate() |
michael@0 | 1393 | ); |
michael@0 | 1394 | } |
michael@0 | 1395 | |
michael@0 | 1396 | |
michael@0 | 1397 | function hasPermission(aAddon, aPerm) { |
michael@0 | 1398 | var perm = AddonManager["PERM_CAN_" + aPerm.toUpperCase()]; |
michael@0 | 1399 | return !!(aAddon.permissions & perm); |
michael@0 | 1400 | } |
michael@0 | 1401 | |
michael@0 | 1402 | |
michael@0 | 1403 | function isPending(aAddon, aAction) { |
michael@0 | 1404 | var action = AddonManager["PENDING_" + aAction.toUpperCase()]; |
michael@0 | 1405 | return !!(aAddon.pendingOperations & action); |
michael@0 | 1406 | } |
michael@0 | 1407 | |
michael@0 | 1408 | function isInState(aInstall, aState) { |
michael@0 | 1409 | var state = AddonManager["STATE_" + aState.toUpperCase()]; |
michael@0 | 1410 | return aInstall.state == state; |
michael@0 | 1411 | } |
michael@0 | 1412 | |
michael@0 | 1413 | function shouldShowVersionNumber(aAddon) { |
michael@0 | 1414 | if (!aAddon.version) |
michael@0 | 1415 | return false; |
michael@0 | 1416 | |
michael@0 | 1417 | // The version number is hidden for lightweight themes. |
michael@0 | 1418 | if (aAddon.type == "theme") |
michael@0 | 1419 | return !/@personas\.mozilla\.org$/.test(aAddon.id); |
michael@0 | 1420 | |
michael@0 | 1421 | return true; |
michael@0 | 1422 | } |
michael@0 | 1423 | |
michael@0 | 1424 | function createItem(aObj, aIsInstall, aIsRemote) { |
michael@0 | 1425 | let item = document.createElement("richlistitem"); |
michael@0 | 1426 | |
michael@0 | 1427 | item.setAttribute("class", "addon addon-view"); |
michael@0 | 1428 | item.setAttribute("name", aObj.name); |
michael@0 | 1429 | item.setAttribute("type", aObj.type); |
michael@0 | 1430 | item.setAttribute("remote", !!aIsRemote); |
michael@0 | 1431 | |
michael@0 | 1432 | if (aIsInstall) { |
michael@0 | 1433 | item.mInstall = aObj; |
michael@0 | 1434 | |
michael@0 | 1435 | if (aObj.state != AddonManager.STATE_INSTALLED) { |
michael@0 | 1436 | item.setAttribute("status", "installing"); |
michael@0 | 1437 | return item; |
michael@0 | 1438 | } |
michael@0 | 1439 | aObj = aObj.addon; |
michael@0 | 1440 | } |
michael@0 | 1441 | |
michael@0 | 1442 | item.mAddon = aObj; |
michael@0 | 1443 | |
michael@0 | 1444 | item.setAttribute("status", "installed"); |
michael@0 | 1445 | |
michael@0 | 1446 | // set only attributes needed for sorting and XBL binding, |
michael@0 | 1447 | // the binding handles the rest |
michael@0 | 1448 | item.setAttribute("value", aObj.id); |
michael@0 | 1449 | |
michael@0 | 1450 | if (aObj.type == "experiment") { |
michael@0 | 1451 | item.endDate = getExperimentEndDate(aObj); |
michael@0 | 1452 | } |
michael@0 | 1453 | |
michael@0 | 1454 | return item; |
michael@0 | 1455 | } |
michael@0 | 1456 | |
michael@0 | 1457 | function sortElements(aElements, aSortBy, aAscending) { |
michael@0 | 1458 | // aSortBy is an Array of attributes to sort by, in decending |
michael@0 | 1459 | // order of priority. |
michael@0 | 1460 | |
michael@0 | 1461 | const DATE_FIELDS = ["updateDate"]; |
michael@0 | 1462 | const NUMERIC_FIELDS = ["size", "relevancescore", "purchaseAmount"]; |
michael@0 | 1463 | |
michael@0 | 1464 | // We're going to group add-ons into the following buckets: |
michael@0 | 1465 | // |
michael@0 | 1466 | // enabledInstalled |
michael@0 | 1467 | // * Enabled |
michael@0 | 1468 | // * Incompatible but enabled because compatibility checking is off |
michael@0 | 1469 | // * Waiting to be installed |
michael@0 | 1470 | // * Waiting to be enabled |
michael@0 | 1471 | // |
michael@0 | 1472 | // pendingDisable |
michael@0 | 1473 | // * Waiting to be disabled |
michael@0 | 1474 | // |
michael@0 | 1475 | // pendingUninstall |
michael@0 | 1476 | // * Waiting to be removed |
michael@0 | 1477 | // |
michael@0 | 1478 | // disabledIncompatibleBlocked |
michael@0 | 1479 | // * Disabled |
michael@0 | 1480 | // * Incompatible |
michael@0 | 1481 | // * Blocklisted |
michael@0 | 1482 | |
michael@0 | 1483 | const UISTATE_ORDER = ["enabled", "pendingDisable", "pendingUninstall", |
michael@0 | 1484 | "disabled"]; |
michael@0 | 1485 | |
michael@0 | 1486 | function dateCompare(a, b) { |
michael@0 | 1487 | var aTime = a.getTime(); |
michael@0 | 1488 | var bTime = b.getTime(); |
michael@0 | 1489 | if (aTime < bTime) |
michael@0 | 1490 | return -1; |
michael@0 | 1491 | if (aTime > bTime) |
michael@0 | 1492 | return 1; |
michael@0 | 1493 | return 0; |
michael@0 | 1494 | } |
michael@0 | 1495 | |
michael@0 | 1496 | function numberCompare(a, b) { |
michael@0 | 1497 | return a - b; |
michael@0 | 1498 | } |
michael@0 | 1499 | |
michael@0 | 1500 | function stringCompare(a, b) { |
michael@0 | 1501 | return a.localeCompare(b); |
michael@0 | 1502 | } |
michael@0 | 1503 | |
michael@0 | 1504 | function uiStateCompare(a, b) { |
michael@0 | 1505 | // If we're in descending order, swap a and b, because |
michael@0 | 1506 | // we don't ever want to have descending uiStates |
michael@0 | 1507 | if (!aAscending) |
michael@0 | 1508 | [a, b] = [b, a]; |
michael@0 | 1509 | |
michael@0 | 1510 | return (UISTATE_ORDER.indexOf(a) - UISTATE_ORDER.indexOf(b)); |
michael@0 | 1511 | } |
michael@0 | 1512 | |
michael@0 | 1513 | function getValue(aObj, aKey) { |
michael@0 | 1514 | if (!aObj) |
michael@0 | 1515 | return null; |
michael@0 | 1516 | |
michael@0 | 1517 | if (aObj.hasAttribute(aKey)) |
michael@0 | 1518 | return aObj.getAttribute(aKey); |
michael@0 | 1519 | |
michael@0 | 1520 | var addon = aObj.mAddon || aObj.mInstall; |
michael@0 | 1521 | if (!addon) |
michael@0 | 1522 | return null; |
michael@0 | 1523 | |
michael@0 | 1524 | if (aKey == "uiState") { |
michael@0 | 1525 | if (addon.pendingOperations == AddonManager.PENDING_DISABLE) |
michael@0 | 1526 | return "pendingDisable"; |
michael@0 | 1527 | if (addon.pendingOperations == AddonManager.PENDING_UNINSTALL) |
michael@0 | 1528 | return "pendingUninstall"; |
michael@0 | 1529 | if (!addon.isActive && |
michael@0 | 1530 | (addon.pendingOperations != AddonManager.PENDING_ENABLE && |
michael@0 | 1531 | addon.pendingOperations != AddonManager.PENDING_INSTALL)) |
michael@0 | 1532 | return "disabled"; |
michael@0 | 1533 | else |
michael@0 | 1534 | return "enabled"; |
michael@0 | 1535 | } |
michael@0 | 1536 | |
michael@0 | 1537 | return addon[aKey]; |
michael@0 | 1538 | } |
michael@0 | 1539 | |
michael@0 | 1540 | // aSortFuncs will hold the sorting functions that we'll |
michael@0 | 1541 | // use per element, in the correct order. |
michael@0 | 1542 | var aSortFuncs = []; |
michael@0 | 1543 | |
michael@0 | 1544 | for (let i = 0; i < aSortBy.length; i++) { |
michael@0 | 1545 | var sortBy = aSortBy[i]; |
michael@0 | 1546 | |
michael@0 | 1547 | aSortFuncs[i] = stringCompare; |
michael@0 | 1548 | |
michael@0 | 1549 | if (sortBy == "uiState") |
michael@0 | 1550 | aSortFuncs[i] = uiStateCompare; |
michael@0 | 1551 | else if (DATE_FIELDS.indexOf(sortBy) != -1) |
michael@0 | 1552 | aSortFuncs[i] = dateCompare; |
michael@0 | 1553 | else if (NUMERIC_FIELDS.indexOf(sortBy) != -1) |
michael@0 | 1554 | aSortFuncs[i] = numberCompare; |
michael@0 | 1555 | } |
michael@0 | 1556 | |
michael@0 | 1557 | |
michael@0 | 1558 | aElements.sort(function elementsSort(a, b) { |
michael@0 | 1559 | if (!aAscending) |
michael@0 | 1560 | [a, b] = [b, a]; |
michael@0 | 1561 | |
michael@0 | 1562 | for (let i = 0; i < aSortFuncs.length; i++) { |
michael@0 | 1563 | var sortBy = aSortBy[i]; |
michael@0 | 1564 | var aValue = getValue(a, sortBy); |
michael@0 | 1565 | var bValue = getValue(b, sortBy); |
michael@0 | 1566 | |
michael@0 | 1567 | if (!aValue && !bValue) |
michael@0 | 1568 | return 0; |
michael@0 | 1569 | if (!aValue) |
michael@0 | 1570 | return -1; |
michael@0 | 1571 | if (!bValue) |
michael@0 | 1572 | return 1; |
michael@0 | 1573 | if (aValue != bValue) { |
michael@0 | 1574 | var result = aSortFuncs[i](aValue, bValue); |
michael@0 | 1575 | |
michael@0 | 1576 | if (result != 0) |
michael@0 | 1577 | return result; |
michael@0 | 1578 | } |
michael@0 | 1579 | } |
michael@0 | 1580 | |
michael@0 | 1581 | // If we got here, then all values of a and b |
michael@0 | 1582 | // must have been equal. |
michael@0 | 1583 | return 0; |
michael@0 | 1584 | |
michael@0 | 1585 | }); |
michael@0 | 1586 | } |
michael@0 | 1587 | |
michael@0 | 1588 | function sortList(aList, aSortBy, aAscending) { |
michael@0 | 1589 | var elements = Array.slice(aList.childNodes, 0); |
michael@0 | 1590 | sortElements(elements, [aSortBy], aAscending); |
michael@0 | 1591 | |
michael@0 | 1592 | while (aList.listChild) |
michael@0 | 1593 | aList.removeChild(aList.lastChild); |
michael@0 | 1594 | |
michael@0 | 1595 | for (let element of elements) |
michael@0 | 1596 | aList.appendChild(element); |
michael@0 | 1597 | } |
michael@0 | 1598 | |
michael@0 | 1599 | function getAddonsAndInstalls(aType, aCallback) { |
michael@0 | 1600 | let addons = null, installs = null; |
michael@0 | 1601 | let types = (aType != null) ? [aType] : null; |
michael@0 | 1602 | |
michael@0 | 1603 | AddonManager.getAddonsByTypes(types, function getAddonsAndInstalls_getAddonsByTypes(aAddonsList) { |
michael@0 | 1604 | addons = aAddonsList; |
michael@0 | 1605 | if (installs != null) |
michael@0 | 1606 | aCallback(addons, installs); |
michael@0 | 1607 | }); |
michael@0 | 1608 | |
michael@0 | 1609 | AddonManager.getInstallsByTypes(types, function getAddonsAndInstalls_getInstallsByTypes(aInstallsList) { |
michael@0 | 1610 | // skip over upgrade installs and non-active installs |
michael@0 | 1611 | installs = aInstallsList.filter(function installsFilter(aInstall) { |
michael@0 | 1612 | return !(aInstall.existingAddon || |
michael@0 | 1613 | aInstall.state == AddonManager.STATE_AVAILABLE); |
michael@0 | 1614 | }); |
michael@0 | 1615 | |
michael@0 | 1616 | if (addons != null) |
michael@0 | 1617 | aCallback(addons, installs) |
michael@0 | 1618 | }); |
michael@0 | 1619 | } |
michael@0 | 1620 | |
michael@0 | 1621 | function doPendingUninstalls(aListBox) { |
michael@0 | 1622 | // Uninstalling add-ons can mutate the list so find the add-ons first then |
michael@0 | 1623 | // uninstall them |
michael@0 | 1624 | var items = []; |
michael@0 | 1625 | var listitem = aListBox.firstChild; |
michael@0 | 1626 | while (listitem) { |
michael@0 | 1627 | if (listitem.getAttribute("pending") == "uninstall" && |
michael@0 | 1628 | !listitem.isPending("uninstall")) |
michael@0 | 1629 | items.push(listitem.mAddon); |
michael@0 | 1630 | listitem = listitem.nextSibling; |
michael@0 | 1631 | } |
michael@0 | 1632 | |
michael@0 | 1633 | for (let addon of items) |
michael@0 | 1634 | addon.uninstall(); |
michael@0 | 1635 | } |
michael@0 | 1636 | |
michael@0 | 1637 | var gCategories = { |
michael@0 | 1638 | node: null, |
michael@0 | 1639 | _search: null, |
michael@0 | 1640 | |
michael@0 | 1641 | initialize: function gCategories_initialize() { |
michael@0 | 1642 | this.node = document.getElementById("categories"); |
michael@0 | 1643 | this._search = this.get("addons://search/"); |
michael@0 | 1644 | |
michael@0 | 1645 | var types = AddonManager.addonTypes; |
michael@0 | 1646 | for (var type in types) |
michael@0 | 1647 | this.onTypeAdded(types[type]); |
michael@0 | 1648 | |
michael@0 | 1649 | AddonManager.addTypeListener(this); |
michael@0 | 1650 | |
michael@0 | 1651 | try { |
michael@0 | 1652 | this.node.value = Services.prefs.getCharPref(PREF_UI_LASTCATEGORY); |
michael@0 | 1653 | } catch (e) { } |
michael@0 | 1654 | |
michael@0 | 1655 | // If there was no last view or no existing category matched the last view |
michael@0 | 1656 | // then the list will default to selecting the search category and we never |
michael@0 | 1657 | // want to show that as the first view so switch to the default category |
michael@0 | 1658 | if (!this.node.selectedItem || this.node.selectedItem == this._search) |
michael@0 | 1659 | this.node.value = VIEW_DEFAULT; |
michael@0 | 1660 | |
michael@0 | 1661 | var self = this; |
michael@0 | 1662 | this.node.addEventListener("select", function node_onSelected() { |
michael@0 | 1663 | self.maybeHideSearch(); |
michael@0 | 1664 | gViewController.loadView(self.node.selectedItem.value); |
michael@0 | 1665 | }, false); |
michael@0 | 1666 | |
michael@0 | 1667 | this.node.addEventListener("click", function node_onClicked(aEvent) { |
michael@0 | 1668 | var selectedItem = self.node.selectedItem; |
michael@0 | 1669 | if (aEvent.target.localName == "richlistitem" && |
michael@0 | 1670 | aEvent.target == selectedItem) { |
michael@0 | 1671 | var viewId = selectedItem.value; |
michael@0 | 1672 | |
michael@0 | 1673 | if (gViewController.parseViewId(viewId).type == "search") { |
michael@0 | 1674 | viewId += encodeURIComponent(gHeader.searchQuery); |
michael@0 | 1675 | } |
michael@0 | 1676 | |
michael@0 | 1677 | gViewController.loadView(viewId); |
michael@0 | 1678 | } |
michael@0 | 1679 | }, false); |
michael@0 | 1680 | }, |
michael@0 | 1681 | |
michael@0 | 1682 | shutdown: function gCategories_shutdown() { |
michael@0 | 1683 | AddonManager.removeTypeListener(this); |
michael@0 | 1684 | }, |
michael@0 | 1685 | |
michael@0 | 1686 | _insertCategory: function gCategories_insertCategory(aId, aName, aView, aPriority, aStartHidden) { |
michael@0 | 1687 | // If this category already exists then don't re-add it |
michael@0 | 1688 | if (document.getElementById("category-" + aId)) |
michael@0 | 1689 | return; |
michael@0 | 1690 | |
michael@0 | 1691 | var category = document.createElement("richlistitem"); |
michael@0 | 1692 | category.setAttribute("id", "category-" + aId); |
michael@0 | 1693 | category.setAttribute("value", aView); |
michael@0 | 1694 | category.setAttribute("class", "category"); |
michael@0 | 1695 | category.setAttribute("name", aName); |
michael@0 | 1696 | category.setAttribute("tooltiptext", aName); |
michael@0 | 1697 | category.setAttribute("priority", aPriority); |
michael@0 | 1698 | category.setAttribute("hidden", aStartHidden); |
michael@0 | 1699 | |
michael@0 | 1700 | var node; |
michael@0 | 1701 | for (node of this.node.children) { |
michael@0 | 1702 | var nodePriority = parseInt(node.getAttribute("priority")); |
michael@0 | 1703 | // If the new type's priority is higher than this one then this is the |
michael@0 | 1704 | // insertion point |
michael@0 | 1705 | if (aPriority < nodePriority) |
michael@0 | 1706 | break; |
michael@0 | 1707 | // If the new type's priority is lower than this one then this is isn't |
michael@0 | 1708 | // the insertion point |
michael@0 | 1709 | if (aPriority > nodePriority) |
michael@0 | 1710 | continue; |
michael@0 | 1711 | // If the priorities are equal and the new type's name is earlier |
michael@0 | 1712 | // alphabetically then this is the insertion point |
michael@0 | 1713 | if (String.localeCompare(aName, node.getAttribute("name")) < 0) |
michael@0 | 1714 | break; |
michael@0 | 1715 | } |
michael@0 | 1716 | |
michael@0 | 1717 | this.node.insertBefore(category, node); |
michael@0 | 1718 | }, |
michael@0 | 1719 | |
michael@0 | 1720 | _removeCategory: function gCategories_removeCategory(aId) { |
michael@0 | 1721 | var category = document.getElementById("category-" + aId); |
michael@0 | 1722 | if (!category) |
michael@0 | 1723 | return; |
michael@0 | 1724 | |
michael@0 | 1725 | // If this category is currently selected then switch to the default view |
michael@0 | 1726 | if (this.node.selectedItem == category) |
michael@0 | 1727 | gViewController.replaceView(VIEW_DEFAULT); |
michael@0 | 1728 | |
michael@0 | 1729 | this.node.removeChild(category); |
michael@0 | 1730 | }, |
michael@0 | 1731 | |
michael@0 | 1732 | onTypeAdded: function gCategories_onTypeAdded(aType) { |
michael@0 | 1733 | // Ignore types that we don't have a view object for |
michael@0 | 1734 | if (!(aType.viewType in gViewController.viewObjects)) |
michael@0 | 1735 | return; |
michael@0 | 1736 | |
michael@0 | 1737 | var aViewId = "addons://" + aType.viewType + "/" + aType.id; |
michael@0 | 1738 | |
michael@0 | 1739 | var startHidden = false; |
michael@0 | 1740 | if (aType.flags & AddonManager.TYPE_UI_HIDE_EMPTY) { |
michael@0 | 1741 | var prefName = PREF_UI_TYPE_HIDDEN.replace("%TYPE%", aType.id); |
michael@0 | 1742 | try { |
michael@0 | 1743 | startHidden = Services.prefs.getBoolPref(prefName); |
michael@0 | 1744 | } |
michael@0 | 1745 | catch (e) { |
michael@0 | 1746 | // Default to hidden |
michael@0 | 1747 | startHidden = true; |
michael@0 | 1748 | } |
michael@0 | 1749 | |
michael@0 | 1750 | var self = this; |
michael@0 | 1751 | gPendingInitializations++; |
michael@0 | 1752 | getAddonsAndInstalls(aType.id, function onTypeAdded_getAddonsAndInstalls(aAddonsList, aInstallsList) { |
michael@0 | 1753 | var hidden = (aAddonsList.length == 0 && aInstallsList.length == 0); |
michael@0 | 1754 | var item = self.get(aViewId); |
michael@0 | 1755 | |
michael@0 | 1756 | // Don't load view that is becoming hidden |
michael@0 | 1757 | if (hidden && aViewId == gViewController.currentViewId) |
michael@0 | 1758 | gViewController.loadView(VIEW_DEFAULT); |
michael@0 | 1759 | |
michael@0 | 1760 | item.hidden = hidden; |
michael@0 | 1761 | Services.prefs.setBoolPref(prefName, hidden); |
michael@0 | 1762 | |
michael@0 | 1763 | if (aAddonsList.length > 0 || aInstallsList.length > 0) { |
michael@0 | 1764 | notifyInitialized(); |
michael@0 | 1765 | return; |
michael@0 | 1766 | } |
michael@0 | 1767 | |
michael@0 | 1768 | gEventManager.registerInstallListener({ |
michael@0 | 1769 | onDownloadStarted: function gCategories_onDownloadStarted(aInstall) { |
michael@0 | 1770 | this._maybeShowCategory(aInstall); |
michael@0 | 1771 | }, |
michael@0 | 1772 | |
michael@0 | 1773 | onInstallStarted: function gCategories_onInstallStarted(aInstall) { |
michael@0 | 1774 | this._maybeShowCategory(aInstall); |
michael@0 | 1775 | }, |
michael@0 | 1776 | |
michael@0 | 1777 | onInstallEnded: function gCategories_onInstallEnded(aInstall, aAddon) { |
michael@0 | 1778 | this._maybeShowCategory(aAddon); |
michael@0 | 1779 | }, |
michael@0 | 1780 | |
michael@0 | 1781 | onExternalInstall: function gCategories_onExternalInstall(aAddon, aExistingAddon, aRequiresRestart) { |
michael@0 | 1782 | this._maybeShowCategory(aAddon); |
michael@0 | 1783 | }, |
michael@0 | 1784 | |
michael@0 | 1785 | _maybeShowCategory: function gCategories_maybeShowCategory(aAddon) { |
michael@0 | 1786 | if (aType.id == aAddon.type) { |
michael@0 | 1787 | self.get(aViewId).hidden = false; |
michael@0 | 1788 | Services.prefs.setBoolPref(prefName, false); |
michael@0 | 1789 | gEventManager.unregisterInstallListener(this); |
michael@0 | 1790 | } |
michael@0 | 1791 | } |
michael@0 | 1792 | }); |
michael@0 | 1793 | |
michael@0 | 1794 | notifyInitialized(); |
michael@0 | 1795 | }); |
michael@0 | 1796 | } |
michael@0 | 1797 | |
michael@0 | 1798 | this._insertCategory(aType.id, aType.name, aViewId, aType.uiPriority, |
michael@0 | 1799 | startHidden); |
michael@0 | 1800 | }, |
michael@0 | 1801 | |
michael@0 | 1802 | onTypeRemoved: function gCategories_onTypeRemoved(aType) { |
michael@0 | 1803 | this._removeCategory(aType.id); |
michael@0 | 1804 | }, |
michael@0 | 1805 | |
michael@0 | 1806 | get selected() { |
michael@0 | 1807 | return this.node.selectedItem ? this.node.selectedItem.value : null; |
michael@0 | 1808 | }, |
michael@0 | 1809 | |
michael@0 | 1810 | select: function gCategories_select(aId, aPreviousView) { |
michael@0 | 1811 | var view = gViewController.parseViewId(aId); |
michael@0 | 1812 | if (view.type == "detail" && aPreviousView) { |
michael@0 | 1813 | aId = aPreviousView; |
michael@0 | 1814 | view = gViewController.parseViewId(aPreviousView); |
michael@0 | 1815 | } |
michael@0 | 1816 | |
michael@0 | 1817 | Services.prefs.setCharPref(PREF_UI_LASTCATEGORY, aId); |
michael@0 | 1818 | |
michael@0 | 1819 | if (this.node.selectedItem && |
michael@0 | 1820 | this.node.selectedItem.value == aId) { |
michael@0 | 1821 | this.node.selectedItem.hidden = false; |
michael@0 | 1822 | this.node.selectedItem.disabled = false; |
michael@0 | 1823 | return; |
michael@0 | 1824 | } |
michael@0 | 1825 | |
michael@0 | 1826 | if (view.type == "search") |
michael@0 | 1827 | var item = this._search; |
michael@0 | 1828 | else |
michael@0 | 1829 | var item = this.get(aId); |
michael@0 | 1830 | |
michael@0 | 1831 | if (item) { |
michael@0 | 1832 | item.hidden = false; |
michael@0 | 1833 | item.disabled = false; |
michael@0 | 1834 | this.node.suppressOnSelect = true; |
michael@0 | 1835 | this.node.selectedItem = item; |
michael@0 | 1836 | this.node.suppressOnSelect = false; |
michael@0 | 1837 | this.node.ensureElementIsVisible(item); |
michael@0 | 1838 | |
michael@0 | 1839 | this.maybeHideSearch(); |
michael@0 | 1840 | } |
michael@0 | 1841 | }, |
michael@0 | 1842 | |
michael@0 | 1843 | get: function gCategories_get(aId) { |
michael@0 | 1844 | var items = document.getElementsByAttribute("value", aId); |
michael@0 | 1845 | if (items.length) |
michael@0 | 1846 | return items[0]; |
michael@0 | 1847 | return null; |
michael@0 | 1848 | }, |
michael@0 | 1849 | |
michael@0 | 1850 | setBadge: function gCategories_setBadge(aId, aCount) { |
michael@0 | 1851 | let item = this.get(aId); |
michael@0 | 1852 | if (item) |
michael@0 | 1853 | item.badgeCount = aCount; |
michael@0 | 1854 | }, |
michael@0 | 1855 | |
michael@0 | 1856 | maybeHideSearch: function gCategories_maybeHideSearch() { |
michael@0 | 1857 | var view = gViewController.parseViewId(this.node.selectedItem.value); |
michael@0 | 1858 | this._search.disabled = view.type != "search"; |
michael@0 | 1859 | } |
michael@0 | 1860 | }; |
michael@0 | 1861 | |
michael@0 | 1862 | |
michael@0 | 1863 | var gHeader = { |
michael@0 | 1864 | _search: null, |
michael@0 | 1865 | _dest: "", |
michael@0 | 1866 | |
michael@0 | 1867 | initialize: function gHeader_initialize() { |
michael@0 | 1868 | this._search = document.getElementById("header-search"); |
michael@0 | 1869 | |
michael@0 | 1870 | this._search.addEventListener("command", function search_onCommand(aEvent) { |
michael@0 | 1871 | var query = aEvent.target.value; |
michael@0 | 1872 | if (query.length == 0) |
michael@0 | 1873 | return; |
michael@0 | 1874 | |
michael@0 | 1875 | gViewController.loadView("addons://search/" + encodeURIComponent(query)); |
michael@0 | 1876 | }, false); |
michael@0 | 1877 | |
michael@0 | 1878 | function updateNavButtonVisibility() { |
michael@0 | 1879 | var shouldShow = gHeader.shouldShowNavButtons; |
michael@0 | 1880 | document.getElementById("back-btn").hidden = !shouldShow; |
michael@0 | 1881 | document.getElementById("forward-btn").hidden = !shouldShow; |
michael@0 | 1882 | } |
michael@0 | 1883 | |
michael@0 | 1884 | window.addEventListener("focus", function window_onFocus(aEvent) { |
michael@0 | 1885 | if (aEvent.target == window) |
michael@0 | 1886 | updateNavButtonVisibility(); |
michael@0 | 1887 | }, false); |
michael@0 | 1888 | |
michael@0 | 1889 | updateNavButtonVisibility(); |
michael@0 | 1890 | }, |
michael@0 | 1891 | |
michael@0 | 1892 | focusSearchBox: function gHeader_focusSearchBox() { |
michael@0 | 1893 | this._search.focus(); |
michael@0 | 1894 | }, |
michael@0 | 1895 | |
michael@0 | 1896 | onKeyPress: function gHeader_onKeyPress(aEvent) { |
michael@0 | 1897 | if (String.fromCharCode(aEvent.charCode) == "/") { |
michael@0 | 1898 | this.focusSearchBox(); |
michael@0 | 1899 | return; |
michael@0 | 1900 | } |
michael@0 | 1901 | |
michael@0 | 1902 | // XXXunf Temporary until bug 371900 is fixed. |
michael@0 | 1903 | let key = document.getElementById("focusSearch").getAttribute("key"); |
michael@0 | 1904 | #ifdef XP_MACOSX |
michael@0 | 1905 | let keyModifier = aEvent.metaKey; |
michael@0 | 1906 | #else |
michael@0 | 1907 | let keyModifier = aEvent.ctrlKey; |
michael@0 | 1908 | #endif |
michael@0 | 1909 | if (String.fromCharCode(aEvent.charCode) == key && keyModifier) { |
michael@0 | 1910 | this.focusSearchBox(); |
michael@0 | 1911 | return; |
michael@0 | 1912 | } |
michael@0 | 1913 | }, |
michael@0 | 1914 | |
michael@0 | 1915 | get shouldShowNavButtons() { |
michael@0 | 1916 | var docshellItem = window.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 1917 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 1918 | .QueryInterface(Ci.nsIDocShellTreeItem); |
michael@0 | 1919 | |
michael@0 | 1920 | // If there is no outer frame then make the buttons visible |
michael@0 | 1921 | if (docshellItem.rootTreeItem == docshellItem) |
michael@0 | 1922 | return true; |
michael@0 | 1923 | |
michael@0 | 1924 | var outerWin = docshellItem.rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 1925 | .getInterface(Ci.nsIDOMWindow); |
michael@0 | 1926 | var outerDoc = outerWin.document; |
michael@0 | 1927 | var node = outerDoc.getElementById("back-button"); |
michael@0 | 1928 | // If the outer frame has no back-button then make the buttons visible |
michael@0 | 1929 | if (!node) |
michael@0 | 1930 | return true; |
michael@0 | 1931 | |
michael@0 | 1932 | // If the back-button or any of its parents are hidden then make the buttons |
michael@0 | 1933 | // visible |
michael@0 | 1934 | while (node != outerDoc) { |
michael@0 | 1935 | var style = outerWin.getComputedStyle(node, ""); |
michael@0 | 1936 | if (style.display == "none") |
michael@0 | 1937 | return true; |
michael@0 | 1938 | if (style.visibility != "visible") |
michael@0 | 1939 | return true; |
michael@0 | 1940 | node = node.parentNode; |
michael@0 | 1941 | } |
michael@0 | 1942 | |
michael@0 | 1943 | return false; |
michael@0 | 1944 | }, |
michael@0 | 1945 | |
michael@0 | 1946 | get searchQuery() { |
michael@0 | 1947 | return this._search.value; |
michael@0 | 1948 | }, |
michael@0 | 1949 | |
michael@0 | 1950 | set searchQuery(aQuery) { |
michael@0 | 1951 | this._search.value = aQuery; |
michael@0 | 1952 | }, |
michael@0 | 1953 | }; |
michael@0 | 1954 | |
michael@0 | 1955 | |
michael@0 | 1956 | var gDiscoverView = { |
michael@0 | 1957 | node: null, |
michael@0 | 1958 | enabled: true, |
michael@0 | 1959 | // Set to true after the view is first shown. If initialization completes |
michael@0 | 1960 | // after this then it must also load the discover homepage |
michael@0 | 1961 | loaded: false, |
michael@0 | 1962 | _browser: null, |
michael@0 | 1963 | _loading: null, |
michael@0 | 1964 | _error: null, |
michael@0 | 1965 | homepageURL: null, |
michael@0 | 1966 | _loadListeners: [], |
michael@0 | 1967 | |
michael@0 | 1968 | initialize: function gDiscoverView_initialize() { |
michael@0 | 1969 | this.enabled = isDiscoverEnabled(); |
michael@0 | 1970 | if (!this.enabled) { |
michael@0 | 1971 | gCategories.get("addons://discover/").hidden = true; |
michael@0 | 1972 | return; |
michael@0 | 1973 | } |
michael@0 | 1974 | |
michael@0 | 1975 | this.node = document.getElementById("discover-view"); |
michael@0 | 1976 | this._loading = document.getElementById("discover-loading"); |
michael@0 | 1977 | this._error = document.getElementById("discover-error"); |
michael@0 | 1978 | this._browser = document.getElementById("discover-browser"); |
michael@0 | 1979 | |
michael@0 | 1980 | let compatMode = "normal"; |
michael@0 | 1981 | if (!AddonManager.checkCompatibility) |
michael@0 | 1982 | compatMode = "ignore"; |
michael@0 | 1983 | else if (AddonManager.strictCompatibility) |
michael@0 | 1984 | compatMode = "strict"; |
michael@0 | 1985 | |
michael@0 | 1986 | var url = Services.prefs.getCharPref(PREF_DISCOVERURL); |
michael@0 | 1987 | url = url.replace("%COMPATIBILITY_MODE%", compatMode); |
michael@0 | 1988 | url = Services.urlFormatter.formatURL(url); |
michael@0 | 1989 | |
michael@0 | 1990 | var self = this; |
michael@0 | 1991 | |
michael@0 | 1992 | function setURL(aURL) { |
michael@0 | 1993 | try { |
michael@0 | 1994 | self.homepageURL = Services.io.newURI(aURL, null, null); |
michael@0 | 1995 | } catch (e) { |
michael@0 | 1996 | self.showError(); |
michael@0 | 1997 | notifyInitialized(); |
michael@0 | 1998 | return; |
michael@0 | 1999 | } |
michael@0 | 2000 | |
michael@0 | 2001 | self._browser.homePage = self.homepageURL.spec; |
michael@0 | 2002 | self._browser.addProgressListener(self); |
michael@0 | 2003 | |
michael@0 | 2004 | if (self.loaded) |
michael@0 | 2005 | self._loadURL(self.homepageURL.spec, false, notifyInitialized); |
michael@0 | 2006 | else |
michael@0 | 2007 | notifyInitialized(); |
michael@0 | 2008 | } |
michael@0 | 2009 | |
michael@0 | 2010 | if (Services.prefs.getBoolPref(PREF_GETADDONS_CACHE_ENABLED) == false) { |
michael@0 | 2011 | setURL(url); |
michael@0 | 2012 | return; |
michael@0 | 2013 | } |
michael@0 | 2014 | |
michael@0 | 2015 | gPendingInitializations++; |
michael@0 | 2016 | AddonManager.getAllAddons(function initialize_getAllAddons(aAddons) { |
michael@0 | 2017 | var list = {}; |
michael@0 | 2018 | for (let addon of aAddons) { |
michael@0 | 2019 | var prefName = PREF_GETADDONS_CACHE_ID_ENABLED.replace("%ID%", |
michael@0 | 2020 | addon.id); |
michael@0 | 2021 | try { |
michael@0 | 2022 | if (!Services.prefs.getBoolPref(prefName)) |
michael@0 | 2023 | continue; |
michael@0 | 2024 | } catch (e) { } |
michael@0 | 2025 | list[addon.id] = { |
michael@0 | 2026 | name: addon.name, |
michael@0 | 2027 | version: addon.version, |
michael@0 | 2028 | type: addon.type, |
michael@0 | 2029 | userDisabled: addon.userDisabled, |
michael@0 | 2030 | isCompatible: addon.isCompatible, |
michael@0 | 2031 | isBlocklisted: addon.blocklistState == Ci.nsIBlocklistService.STATE_BLOCKED |
michael@0 | 2032 | } |
michael@0 | 2033 | } |
michael@0 | 2034 | |
michael@0 | 2035 | setURL(url + "#" + JSON.stringify(list)); |
michael@0 | 2036 | }); |
michael@0 | 2037 | }, |
michael@0 | 2038 | |
michael@0 | 2039 | destroy: function gDiscoverView_destroy() { |
michael@0 | 2040 | try { |
michael@0 | 2041 | this._browser.removeProgressListener(this); |
michael@0 | 2042 | } |
michael@0 | 2043 | catch (e) { |
michael@0 | 2044 | // Ignore the case when the listener wasn't already registered |
michael@0 | 2045 | } |
michael@0 | 2046 | }, |
michael@0 | 2047 | |
michael@0 | 2048 | show: function gDiscoverView_show(aParam, aRequest, aState, aIsRefresh) { |
michael@0 | 2049 | gViewController.updateCommands(); |
michael@0 | 2050 | |
michael@0 | 2051 | // If we're being told to load a specific URL then just do that |
michael@0 | 2052 | if (aState && "url" in aState) { |
michael@0 | 2053 | this.loaded = true; |
michael@0 | 2054 | this._loadURL(aState.url); |
michael@0 | 2055 | } |
michael@0 | 2056 | |
michael@0 | 2057 | // If the view has loaded before and still at the homepage (if refreshing), |
michael@0 | 2058 | // and the error page is not visible then there is nothing else to do |
michael@0 | 2059 | if (this.loaded && this.node.selectedPanel != this._error && |
michael@0 | 2060 | (!aIsRefresh || (this._browser.currentURI && |
michael@0 | 2061 | this._browser.currentURI.spec == this._browser.homePage))) { |
michael@0 | 2062 | gViewController.notifyViewChanged(); |
michael@0 | 2063 | return; |
michael@0 | 2064 | } |
michael@0 | 2065 | |
michael@0 | 2066 | this.loaded = true; |
michael@0 | 2067 | |
michael@0 | 2068 | // No homepage means initialization isn't complete, the browser will get |
michael@0 | 2069 | // loaded once initialization is complete |
michael@0 | 2070 | if (!this.homepageURL) { |
michael@0 | 2071 | this._loadListeners.push(gViewController.notifyViewChanged.bind(gViewController)); |
michael@0 | 2072 | return; |
michael@0 | 2073 | } |
michael@0 | 2074 | |
michael@0 | 2075 | this._loadURL(this.homepageURL.spec, aIsRefresh, |
michael@0 | 2076 | gViewController.notifyViewChanged.bind(gViewController)); |
michael@0 | 2077 | }, |
michael@0 | 2078 | |
michael@0 | 2079 | canRefresh: function gDiscoverView_canRefresh() { |
michael@0 | 2080 | if (this._browser.currentURI && |
michael@0 | 2081 | this._browser.currentURI.spec == this._browser.homePage) |
michael@0 | 2082 | return false; |
michael@0 | 2083 | return true; |
michael@0 | 2084 | }, |
michael@0 | 2085 | |
michael@0 | 2086 | refresh: function gDiscoverView_refresh(aParam, aRequest, aState) { |
michael@0 | 2087 | this.show(aParam, aRequest, aState, true); |
michael@0 | 2088 | }, |
michael@0 | 2089 | |
michael@0 | 2090 | hide: function gDiscoverView_hide() { }, |
michael@0 | 2091 | |
michael@0 | 2092 | showError: function gDiscoverView_showError() { |
michael@0 | 2093 | this.node.selectedPanel = this._error; |
michael@0 | 2094 | }, |
michael@0 | 2095 | |
michael@0 | 2096 | _loadURL: function gDiscoverView_loadURL(aURL, aKeepHistory, aCallback) { |
michael@0 | 2097 | if (this._browser.currentURI.spec == aURL) { |
michael@0 | 2098 | if (aCallback) |
michael@0 | 2099 | aCallback(); |
michael@0 | 2100 | return; |
michael@0 | 2101 | } |
michael@0 | 2102 | |
michael@0 | 2103 | if (aCallback) |
michael@0 | 2104 | this._loadListeners.push(aCallback); |
michael@0 | 2105 | |
michael@0 | 2106 | var flags = 0; |
michael@0 | 2107 | if (!aKeepHistory) |
michael@0 | 2108 | flags |= Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY; |
michael@0 | 2109 | |
michael@0 | 2110 | this._browser.loadURIWithFlags(aURL, flags); |
michael@0 | 2111 | }, |
michael@0 | 2112 | |
michael@0 | 2113 | onLocationChange: function gDiscoverView_onLocationChange(aWebProgress, aRequest, aLocation, aFlags) { |
michael@0 | 2114 | // Ignore the about:blank load |
michael@0 | 2115 | if (aLocation.spec == "about:blank") |
michael@0 | 2116 | return; |
michael@0 | 2117 | |
michael@0 | 2118 | // When using the real session history the inner-frame will update the |
michael@0 | 2119 | // session history automatically, if using the fake history though it must |
michael@0 | 2120 | // be manually updated |
michael@0 | 2121 | if (gHistory == FakeHistory) { |
michael@0 | 2122 | var docshell = aWebProgress.QueryInterface(Ci.nsIDocShell); |
michael@0 | 2123 | |
michael@0 | 2124 | var state = { |
michael@0 | 2125 | view: "addons://discover/", |
michael@0 | 2126 | url: aLocation.spec |
michael@0 | 2127 | }; |
michael@0 | 2128 | |
michael@0 | 2129 | var replaceHistory = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY << 16; |
michael@0 | 2130 | if (docshell.loadType & replaceHistory) |
michael@0 | 2131 | gHistory.replaceState(state); |
michael@0 | 2132 | else |
michael@0 | 2133 | gHistory.pushState(state); |
michael@0 | 2134 | gViewController.lastHistoryIndex = gHistory.index; |
michael@0 | 2135 | } |
michael@0 | 2136 | |
michael@0 | 2137 | gViewController.updateCommands(); |
michael@0 | 2138 | |
michael@0 | 2139 | // If the hostname is the same as the new location's host and either the |
michael@0 | 2140 | // default scheme is insecure or the new location is secure then continue |
michael@0 | 2141 | // with the load |
michael@0 | 2142 | if (aLocation.host == this.homepageURL.host && |
michael@0 | 2143 | (!this.homepageURL.schemeIs("https") || aLocation.schemeIs("https"))) |
michael@0 | 2144 | return; |
michael@0 | 2145 | |
michael@0 | 2146 | // Canceling the request will send an error to onStateChange which will show |
michael@0 | 2147 | // the error page |
michael@0 | 2148 | aRequest.cancel(Components.results.NS_BINDING_ABORTED); |
michael@0 | 2149 | }, |
michael@0 | 2150 | |
michael@0 | 2151 | onSecurityChange: function gDiscoverView_onSecurityChange(aWebProgress, aRequest, aState) { |
michael@0 | 2152 | // Don't care about security if the page is not https |
michael@0 | 2153 | if (!this.homepageURL.schemeIs("https")) |
michael@0 | 2154 | return; |
michael@0 | 2155 | |
michael@0 | 2156 | // If the request was secure then it is ok |
michael@0 | 2157 | if (aState & Ci.nsIWebProgressListener.STATE_IS_SECURE) |
michael@0 | 2158 | return; |
michael@0 | 2159 | |
michael@0 | 2160 | // Canceling the request will send an error to onStateChange which will show |
michael@0 | 2161 | // the error page |
michael@0 | 2162 | aRequest.cancel(Components.results.NS_BINDING_ABORTED); |
michael@0 | 2163 | }, |
michael@0 | 2164 | |
michael@0 | 2165 | onStateChange: function gDiscoverView_onStateChange(aWebProgress, aRequest, aStateFlags, aStatus) { |
michael@0 | 2166 | let transferStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | |
michael@0 | 2167 | Ci.nsIWebProgressListener.STATE_IS_REQUEST | |
michael@0 | 2168 | Ci.nsIWebProgressListener.STATE_TRANSFERRING; |
michael@0 | 2169 | // Once transferring begins show the content |
michael@0 | 2170 | if (aStateFlags & transferStart) |
michael@0 | 2171 | this.node.selectedPanel = this._browser; |
michael@0 | 2172 | |
michael@0 | 2173 | // Only care about the network events |
michael@0 | 2174 | if (!(aStateFlags & (Ci.nsIWebProgressListener.STATE_IS_NETWORK))) |
michael@0 | 2175 | return; |
michael@0 | 2176 | |
michael@0 | 2177 | // If this is the start of network activity then show the loading page |
michael@0 | 2178 | if (aStateFlags & (Ci.nsIWebProgressListener.STATE_START)) |
michael@0 | 2179 | this.node.selectedPanel = this._loading; |
michael@0 | 2180 | |
michael@0 | 2181 | // Ignore anything except stop events |
michael@0 | 2182 | if (!(aStateFlags & (Ci.nsIWebProgressListener.STATE_STOP))) |
michael@0 | 2183 | return; |
michael@0 | 2184 | |
michael@0 | 2185 | // Consider the successful load of about:blank as still loading |
michael@0 | 2186 | if (aRequest instanceof Ci.nsIChannel && aRequest.URI.spec == "about:blank") |
michael@0 | 2187 | return; |
michael@0 | 2188 | |
michael@0 | 2189 | // If there was an error loading the page or the new hostname is not the |
michael@0 | 2190 | // same as the default hostname or the default scheme is secure and the new |
michael@0 | 2191 | // scheme is insecure then show the error page |
michael@0 | 2192 | const NS_ERROR_PARSED_DATA_CACHED = 0x805D0021; |
michael@0 | 2193 | if (!(Components.isSuccessCode(aStatus) || aStatus == NS_ERROR_PARSED_DATA_CACHED) || |
michael@0 | 2194 | (aRequest && aRequest instanceof Ci.nsIHttpChannel && !aRequest.requestSucceeded)) { |
michael@0 | 2195 | this.showError(); |
michael@0 | 2196 | } else { |
michael@0 | 2197 | // Got a successful load, make sure the browser is visible |
michael@0 | 2198 | this.node.selectedPanel = this._browser; |
michael@0 | 2199 | gViewController.updateCommands(); |
michael@0 | 2200 | } |
michael@0 | 2201 | |
michael@0 | 2202 | var listeners = this._loadListeners; |
michael@0 | 2203 | this._loadListeners = []; |
michael@0 | 2204 | |
michael@0 | 2205 | for (let listener of listeners) |
michael@0 | 2206 | listener(); |
michael@0 | 2207 | }, |
michael@0 | 2208 | |
michael@0 | 2209 | onProgressChange: function gDiscoverView_onProgressChange() { }, |
michael@0 | 2210 | onStatusChange: function gDiscoverView_onStatusChange() { }, |
michael@0 | 2211 | |
michael@0 | 2212 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, |
michael@0 | 2213 | Ci.nsISupportsWeakReference]), |
michael@0 | 2214 | |
michael@0 | 2215 | getSelectedAddon: function gDiscoverView_getSelectedAddon() null |
michael@0 | 2216 | }; |
michael@0 | 2217 | |
michael@0 | 2218 | |
michael@0 | 2219 | var gCachedAddons = {}; |
michael@0 | 2220 | |
michael@0 | 2221 | var gSearchView = { |
michael@0 | 2222 | node: null, |
michael@0 | 2223 | _filter: null, |
michael@0 | 2224 | _sorters: null, |
michael@0 | 2225 | _loading: null, |
michael@0 | 2226 | _listBox: null, |
michael@0 | 2227 | _emptyNotice: null, |
michael@0 | 2228 | _allResultsLink: null, |
michael@0 | 2229 | _lastQuery: null, |
michael@0 | 2230 | _lastRemoteTotal: 0, |
michael@0 | 2231 | _pendingSearches: 0, |
michael@0 | 2232 | |
michael@0 | 2233 | initialize: function gSearchView_initialize() { |
michael@0 | 2234 | this.node = document.getElementById("search-view"); |
michael@0 | 2235 | this._filter = document.getElementById("search-filter-radiogroup"); |
michael@0 | 2236 | this._sorters = document.getElementById("search-sorters"); |
michael@0 | 2237 | this._sorters.handler = this; |
michael@0 | 2238 | this._loading = document.getElementById("search-loading"); |
michael@0 | 2239 | this._listBox = document.getElementById("search-list"); |
michael@0 | 2240 | this._emptyNotice = document.getElementById("search-list-empty"); |
michael@0 | 2241 | this._allResultsLink = document.getElementById("search-allresults-link"); |
michael@0 | 2242 | |
michael@0 | 2243 | if (!AddonManager.isInstallEnabled("application/x-xpinstall")) |
michael@0 | 2244 | this._filter.hidden = true; |
michael@0 | 2245 | |
michael@0 | 2246 | var self = this; |
michael@0 | 2247 | this._listBox.addEventListener("keydown", function listbox_onKeydown(aEvent) { |
michael@0 | 2248 | if (aEvent.keyCode == aEvent.DOM_VK_RETURN) { |
michael@0 | 2249 | var item = self._listBox.selectedItem; |
michael@0 | 2250 | if (item) |
michael@0 | 2251 | item.showInDetailView(); |
michael@0 | 2252 | } |
michael@0 | 2253 | }, false); |
michael@0 | 2254 | |
michael@0 | 2255 | this._filter.addEventListener("command", function filter_onCommand() self.updateView(), false); |
michael@0 | 2256 | }, |
michael@0 | 2257 | |
michael@0 | 2258 | shutdown: function gSearchView_shutdown() { |
michael@0 | 2259 | if (AddonRepository.isSearching) |
michael@0 | 2260 | AddonRepository.cancelSearch(); |
michael@0 | 2261 | }, |
michael@0 | 2262 | |
michael@0 | 2263 | get isSearching() { |
michael@0 | 2264 | return this._pendingSearches > 0; |
michael@0 | 2265 | }, |
michael@0 | 2266 | |
michael@0 | 2267 | show: function gSearchView_show(aQuery, aRequest) { |
michael@0 | 2268 | gEventManager.registerInstallListener(this); |
michael@0 | 2269 | |
michael@0 | 2270 | this.showEmptyNotice(false); |
michael@0 | 2271 | this.showAllResultsLink(0); |
michael@0 | 2272 | this.showLoading(true); |
michael@0 | 2273 | this._sorters.showprice = false; |
michael@0 | 2274 | |
michael@0 | 2275 | gHeader.searchQuery = aQuery; |
michael@0 | 2276 | aQuery = aQuery.trim().toLocaleLowerCase(); |
michael@0 | 2277 | if (this._lastQuery == aQuery) { |
michael@0 | 2278 | this.updateView(); |
michael@0 | 2279 | gViewController.notifyViewChanged(); |
michael@0 | 2280 | return; |
michael@0 | 2281 | } |
michael@0 | 2282 | this._lastQuery = aQuery; |
michael@0 | 2283 | |
michael@0 | 2284 | if (AddonRepository.isSearching) |
michael@0 | 2285 | AddonRepository.cancelSearch(); |
michael@0 | 2286 | |
michael@0 | 2287 | while (this._listBox.firstChild.localName == "richlistitem") |
michael@0 | 2288 | this._listBox.removeChild(this._listBox.firstChild); |
michael@0 | 2289 | |
michael@0 | 2290 | var self = this; |
michael@0 | 2291 | gCachedAddons = {}; |
michael@0 | 2292 | this._pendingSearches = 2; |
michael@0 | 2293 | this._sorters.setSort("relevancescore", false); |
michael@0 | 2294 | |
michael@0 | 2295 | var elements = []; |
michael@0 | 2296 | |
michael@0 | 2297 | function createSearchResults(aObjsList, aIsInstall, aIsRemote) { |
michael@0 | 2298 | for (let index in aObjsList) { |
michael@0 | 2299 | let obj = aObjsList[index]; |
michael@0 | 2300 | let score = aObjsList.length - index; |
michael@0 | 2301 | if (!aIsRemote && aQuery.length > 0) { |
michael@0 | 2302 | score = self.getMatchScore(obj, aQuery); |
michael@0 | 2303 | if (score == 0) |
michael@0 | 2304 | continue; |
michael@0 | 2305 | } |
michael@0 | 2306 | |
michael@0 | 2307 | let item = createItem(obj, aIsInstall, aIsRemote); |
michael@0 | 2308 | item.setAttribute("relevancescore", score); |
michael@0 | 2309 | if (aIsRemote) { |
michael@0 | 2310 | gCachedAddons[obj.id] = obj; |
michael@0 | 2311 | if (obj.purchaseURL) |
michael@0 | 2312 | self._sorters.showprice = true; |
michael@0 | 2313 | } |
michael@0 | 2314 | |
michael@0 | 2315 | elements.push(item); |
michael@0 | 2316 | } |
michael@0 | 2317 | } |
michael@0 | 2318 | |
michael@0 | 2319 | function finishSearch(createdCount) { |
michael@0 | 2320 | if (elements.length > 0) { |
michael@0 | 2321 | sortElements(elements, [self._sorters.sortBy], self._sorters.ascending); |
michael@0 | 2322 | for (let element of elements) |
michael@0 | 2323 | self._listBox.insertBefore(element, self._listBox.lastChild); |
michael@0 | 2324 | self.updateListAttributes(); |
michael@0 | 2325 | } |
michael@0 | 2326 | |
michael@0 | 2327 | self._pendingSearches--; |
michael@0 | 2328 | self.updateView(); |
michael@0 | 2329 | |
michael@0 | 2330 | if (!self.isSearching) |
michael@0 | 2331 | gViewController.notifyViewChanged(); |
michael@0 | 2332 | } |
michael@0 | 2333 | |
michael@0 | 2334 | getAddonsAndInstalls(null, function show_getAddonsAndInstalls(aAddons, aInstalls) { |
michael@0 | 2335 | if (gViewController && aRequest != gViewController.currentViewRequest) |
michael@0 | 2336 | return; |
michael@0 | 2337 | |
michael@0 | 2338 | createSearchResults(aAddons, false, false); |
michael@0 | 2339 | createSearchResults(aInstalls, true, false); |
michael@0 | 2340 | finishSearch(); |
michael@0 | 2341 | }); |
michael@0 | 2342 | |
michael@0 | 2343 | var maxRemoteResults = 0; |
michael@0 | 2344 | try { |
michael@0 | 2345 | maxRemoteResults = Services.prefs.getIntPref(PREF_MAXRESULTS); |
michael@0 | 2346 | } catch(e) {} |
michael@0 | 2347 | |
michael@0 | 2348 | if (maxRemoteResults <= 0) { |
michael@0 | 2349 | finishSearch(0); |
michael@0 | 2350 | return; |
michael@0 | 2351 | } |
michael@0 | 2352 | |
michael@0 | 2353 | AddonRepository.searchAddons(aQuery, maxRemoteResults, { |
michael@0 | 2354 | searchFailed: function show_SearchFailed() { |
michael@0 | 2355 | if (gViewController && aRequest != gViewController.currentViewRequest) |
michael@0 | 2356 | return; |
michael@0 | 2357 | |
michael@0 | 2358 | self._lastRemoteTotal = 0; |
michael@0 | 2359 | |
michael@0 | 2360 | // XXXunf Better handling of AMO search failure. See bug 579502 |
michael@0 | 2361 | finishSearch(0); // Silently fail |
michael@0 | 2362 | }, |
michael@0 | 2363 | |
michael@0 | 2364 | searchSucceeded: function show_SearchSucceeded(aAddonsList, aAddonCount, aTotalResults) { |
michael@0 | 2365 | if (gViewController && aRequest != gViewController.currentViewRequest) |
michael@0 | 2366 | return; |
michael@0 | 2367 | |
michael@0 | 2368 | if (aTotalResults > maxRemoteResults) |
michael@0 | 2369 | self._lastRemoteTotal = aTotalResults; |
michael@0 | 2370 | else |
michael@0 | 2371 | self._lastRemoteTotal = 0; |
michael@0 | 2372 | |
michael@0 | 2373 | var createdCount = createSearchResults(aAddonsList, false, true); |
michael@0 | 2374 | finishSearch(createdCount); |
michael@0 | 2375 | } |
michael@0 | 2376 | }); |
michael@0 | 2377 | }, |
michael@0 | 2378 | |
michael@0 | 2379 | showLoading: function gSearchView_showLoading(aLoading) { |
michael@0 | 2380 | this._loading.hidden = !aLoading; |
michael@0 | 2381 | this._listBox.hidden = aLoading; |
michael@0 | 2382 | }, |
michael@0 | 2383 | |
michael@0 | 2384 | updateView: function gSearchView_updateView() { |
michael@0 | 2385 | var showLocal = this._filter.value == "local"; |
michael@0 | 2386 | |
michael@0 | 2387 | if (!showLocal && !AddonManager.isInstallEnabled("application/x-xpinstall")) |
michael@0 | 2388 | showLocal = true; |
michael@0 | 2389 | |
michael@0 | 2390 | this._listBox.setAttribute("local", showLocal); |
michael@0 | 2391 | this._listBox.setAttribute("remote", !showLocal); |
michael@0 | 2392 | |
michael@0 | 2393 | this.showLoading(this.isSearching && !showLocal); |
michael@0 | 2394 | if (!this.isSearching) { |
michael@0 | 2395 | var isEmpty = true; |
michael@0 | 2396 | var results = this._listBox.getElementsByTagName("richlistitem"); |
michael@0 | 2397 | for (let result of results) { |
michael@0 | 2398 | var isRemote = (result.getAttribute("remote") == "true"); |
michael@0 | 2399 | if ((isRemote && !showLocal) || (!isRemote && showLocal)) { |
michael@0 | 2400 | isEmpty = false; |
michael@0 | 2401 | break; |
michael@0 | 2402 | } |
michael@0 | 2403 | } |
michael@0 | 2404 | |
michael@0 | 2405 | this.showEmptyNotice(isEmpty); |
michael@0 | 2406 | this.showAllResultsLink(this._lastRemoteTotal); |
michael@0 | 2407 | } |
michael@0 | 2408 | |
michael@0 | 2409 | gViewController.updateCommands(); |
michael@0 | 2410 | }, |
michael@0 | 2411 | |
michael@0 | 2412 | hide: function gSearchView_hide() { |
michael@0 | 2413 | gEventManager.unregisterInstallListener(this); |
michael@0 | 2414 | doPendingUninstalls(this._listBox); |
michael@0 | 2415 | }, |
michael@0 | 2416 | |
michael@0 | 2417 | getMatchScore: function gSearchView_getMatchScore(aObj, aQuery) { |
michael@0 | 2418 | var score = 0; |
michael@0 | 2419 | score += this.calculateMatchScore(aObj.name, aQuery, |
michael@0 | 2420 | SEARCH_SCORE_MULTIPLIER_NAME); |
michael@0 | 2421 | score += this.calculateMatchScore(aObj.description, aQuery, |
michael@0 | 2422 | SEARCH_SCORE_MULTIPLIER_DESCRIPTION); |
michael@0 | 2423 | return score; |
michael@0 | 2424 | }, |
michael@0 | 2425 | |
michael@0 | 2426 | calculateMatchScore: function gSearchView_calculateMatchScore(aStr, aQuery, aMultiplier) { |
michael@0 | 2427 | var score = 0; |
michael@0 | 2428 | if (!aStr || aQuery.length == 0) |
michael@0 | 2429 | return score; |
michael@0 | 2430 | |
michael@0 | 2431 | aStr = aStr.trim().toLocaleLowerCase(); |
michael@0 | 2432 | var haystack = aStr.split(/\s+/); |
michael@0 | 2433 | var needles = aQuery.split(/\s+/); |
michael@0 | 2434 | |
michael@0 | 2435 | for (let needle of needles) { |
michael@0 | 2436 | for (let hay of haystack) { |
michael@0 | 2437 | if (hay == needle) { |
michael@0 | 2438 | // matching whole words is best |
michael@0 | 2439 | score += SEARCH_SCORE_MATCH_WHOLEWORD; |
michael@0 | 2440 | } else { |
michael@0 | 2441 | let i = hay.indexOf(needle); |
michael@0 | 2442 | if (i == 0) // matching on word boundries is also good |
michael@0 | 2443 | score += SEARCH_SCORE_MATCH_WORDBOUNDRY; |
michael@0 | 2444 | else if (i > 0) // substring matches not so good |
michael@0 | 2445 | score += SEARCH_SCORE_MATCH_SUBSTRING; |
michael@0 | 2446 | } |
michael@0 | 2447 | } |
michael@0 | 2448 | } |
michael@0 | 2449 | |
michael@0 | 2450 | // give progressively higher score for longer queries, since longer queries |
michael@0 | 2451 | // are more likely to be unique and therefore more relevant. |
michael@0 | 2452 | if (needles.length > 1 && aStr.indexOf(aQuery) != -1) |
michael@0 | 2453 | score += needles.length; |
michael@0 | 2454 | |
michael@0 | 2455 | return score * aMultiplier; |
michael@0 | 2456 | }, |
michael@0 | 2457 | |
michael@0 | 2458 | showEmptyNotice: function gSearchView_showEmptyNotice(aShow) { |
michael@0 | 2459 | this._emptyNotice.hidden = !aShow; |
michael@0 | 2460 | this._listBox.hidden = aShow; |
michael@0 | 2461 | }, |
michael@0 | 2462 | |
michael@0 | 2463 | showAllResultsLink: function gSearchView_showAllResultsLink(aTotalResults) { |
michael@0 | 2464 | if (aTotalResults == 0) { |
michael@0 | 2465 | this._allResultsLink.hidden = true; |
michael@0 | 2466 | return; |
michael@0 | 2467 | } |
michael@0 | 2468 | |
michael@0 | 2469 | var linkStr = gStrings.ext.GetStringFromName("showAllSearchResults"); |
michael@0 | 2470 | linkStr = PluralForm.get(aTotalResults, linkStr); |
michael@0 | 2471 | linkStr = linkStr.replace("#1", aTotalResults); |
michael@0 | 2472 | this._allResultsLink.setAttribute("value", linkStr); |
michael@0 | 2473 | |
michael@0 | 2474 | this._allResultsLink.setAttribute("href", |
michael@0 | 2475 | AddonRepository.getSearchURL(this._lastQuery)); |
michael@0 | 2476 | this._allResultsLink.hidden = false; |
michael@0 | 2477 | }, |
michael@0 | 2478 | |
michael@0 | 2479 | updateListAttributes: function gSearchView_updateListAttributes() { |
michael@0 | 2480 | var item = this._listBox.querySelector("richlistitem[remote='true'][first]"); |
michael@0 | 2481 | if (item) |
michael@0 | 2482 | item.removeAttribute("first"); |
michael@0 | 2483 | item = this._listBox.querySelector("richlistitem[remote='true'][last]"); |
michael@0 | 2484 | if (item) |
michael@0 | 2485 | item.removeAttribute("last"); |
michael@0 | 2486 | var items = this._listBox.querySelectorAll("richlistitem[remote='true']"); |
michael@0 | 2487 | if (items.length > 0) { |
michael@0 | 2488 | items[0].setAttribute("first", true); |
michael@0 | 2489 | items[items.length - 1].setAttribute("last", true); |
michael@0 | 2490 | } |
michael@0 | 2491 | |
michael@0 | 2492 | item = this._listBox.querySelector("richlistitem:not([remote='true'])[first]"); |
michael@0 | 2493 | if (item) |
michael@0 | 2494 | item.removeAttribute("first"); |
michael@0 | 2495 | item = this._listBox.querySelector("richlistitem:not([remote='true'])[last]"); |
michael@0 | 2496 | if (item) |
michael@0 | 2497 | item.removeAttribute("last"); |
michael@0 | 2498 | items = this._listBox.querySelectorAll("richlistitem:not([remote='true'])"); |
michael@0 | 2499 | if (items.length > 0) { |
michael@0 | 2500 | items[0].setAttribute("first", true); |
michael@0 | 2501 | items[items.length - 1].setAttribute("last", true); |
michael@0 | 2502 | } |
michael@0 | 2503 | |
michael@0 | 2504 | }, |
michael@0 | 2505 | |
michael@0 | 2506 | onSortChanged: function gSearchView_onSortChanged(aSortBy, aAscending) { |
michael@0 | 2507 | var footer = this._listBox.lastChild; |
michael@0 | 2508 | this._listBox.removeChild(footer); |
michael@0 | 2509 | |
michael@0 | 2510 | sortList(this._listBox, aSortBy, aAscending); |
michael@0 | 2511 | this.updateListAttributes(); |
michael@0 | 2512 | |
michael@0 | 2513 | this._listBox.appendChild(footer); |
michael@0 | 2514 | }, |
michael@0 | 2515 | |
michael@0 | 2516 | onDownloadCancelled: function gSearchView_onDownloadCancelled(aInstall) { |
michael@0 | 2517 | this.removeInstall(aInstall); |
michael@0 | 2518 | }, |
michael@0 | 2519 | |
michael@0 | 2520 | onInstallCancelled: function gSearchView_onInstallCancelled(aInstall) { |
michael@0 | 2521 | this.removeInstall(aInstall); |
michael@0 | 2522 | }, |
michael@0 | 2523 | |
michael@0 | 2524 | removeInstall: function gSearchView_removeInstall(aInstall) { |
michael@0 | 2525 | for (let item of this._listBox.childNodes) { |
michael@0 | 2526 | if (item.mInstall == aInstall) { |
michael@0 | 2527 | this._listBox.removeChild(item); |
michael@0 | 2528 | return; |
michael@0 | 2529 | } |
michael@0 | 2530 | } |
michael@0 | 2531 | }, |
michael@0 | 2532 | |
michael@0 | 2533 | getSelectedAddon: function gSearchView_getSelectedAddon() { |
michael@0 | 2534 | var item = this._listBox.selectedItem; |
michael@0 | 2535 | if (item) |
michael@0 | 2536 | return item.mAddon; |
michael@0 | 2537 | return null; |
michael@0 | 2538 | }, |
michael@0 | 2539 | |
michael@0 | 2540 | getListItemForID: function gSearchView_getListItemForID(aId) { |
michael@0 | 2541 | var listitem = this._listBox.firstChild; |
michael@0 | 2542 | while (listitem) { |
michael@0 | 2543 | if (listitem.getAttribute("status") == "installed" && listitem.mAddon.id == aId) |
michael@0 | 2544 | return listitem; |
michael@0 | 2545 | listitem = listitem.nextSibling; |
michael@0 | 2546 | } |
michael@0 | 2547 | return null; |
michael@0 | 2548 | } |
michael@0 | 2549 | }; |
michael@0 | 2550 | |
michael@0 | 2551 | |
michael@0 | 2552 | var gListView = { |
michael@0 | 2553 | node: null, |
michael@0 | 2554 | _listBox: null, |
michael@0 | 2555 | _emptyNotice: null, |
michael@0 | 2556 | _type: null, |
michael@0 | 2557 | |
michael@0 | 2558 | initialize: function gListView_initialize() { |
michael@0 | 2559 | this.node = document.getElementById("list-view"); |
michael@0 | 2560 | this._listBox = document.getElementById("addon-list"); |
michael@0 | 2561 | this._emptyNotice = document.getElementById("addon-list-empty"); |
michael@0 | 2562 | |
michael@0 | 2563 | var self = this; |
michael@0 | 2564 | this._listBox.addEventListener("keydown", function listbox_onKeydown(aEvent) { |
michael@0 | 2565 | if (aEvent.keyCode == aEvent.DOM_VK_RETURN) { |
michael@0 | 2566 | var item = self._listBox.selectedItem; |
michael@0 | 2567 | if (item) |
michael@0 | 2568 | item.showInDetailView(); |
michael@0 | 2569 | } |
michael@0 | 2570 | }, false); |
michael@0 | 2571 | }, |
michael@0 | 2572 | |
michael@0 | 2573 | show: function gListView_show(aType, aRequest) { |
michael@0 | 2574 | if (!(aType in AddonManager.addonTypes)) |
michael@0 | 2575 | throw Components.Exception("Attempting to show unknown type " + aType, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 2576 | |
michael@0 | 2577 | this._type = aType; |
michael@0 | 2578 | this.node.setAttribute("type", aType); |
michael@0 | 2579 | this.showEmptyNotice(false); |
michael@0 | 2580 | |
michael@0 | 2581 | while (this._listBox.itemCount > 0) |
michael@0 | 2582 | this._listBox.removeItemAt(0); |
michael@0 | 2583 | |
michael@0 | 2584 | var self = this; |
michael@0 | 2585 | getAddonsAndInstalls(aType, function show_getAddonsAndInstalls(aAddonsList, aInstallsList) { |
michael@0 | 2586 | if (gViewController && aRequest != gViewController.currentViewRequest) |
michael@0 | 2587 | return; |
michael@0 | 2588 | |
michael@0 | 2589 | var elements = []; |
michael@0 | 2590 | |
michael@0 | 2591 | for (let addonItem of aAddonsList) |
michael@0 | 2592 | elements.push(createItem(addonItem)); |
michael@0 | 2593 | |
michael@0 | 2594 | for (let installItem of aInstallsList) |
michael@0 | 2595 | elements.push(createItem(installItem, true)); |
michael@0 | 2596 | |
michael@0 | 2597 | self.showEmptyNotice(elements.length == 0); |
michael@0 | 2598 | if (elements.length > 0) { |
michael@0 | 2599 | sortElements(elements, ["uiState", "name"], true); |
michael@0 | 2600 | for (let element of elements) |
michael@0 | 2601 | self._listBox.appendChild(element); |
michael@0 | 2602 | } |
michael@0 | 2603 | |
michael@0 | 2604 | gEventManager.registerInstallListener(self); |
michael@0 | 2605 | gViewController.updateCommands(); |
michael@0 | 2606 | gViewController.notifyViewChanged(); |
michael@0 | 2607 | }); |
michael@0 | 2608 | }, |
michael@0 | 2609 | |
michael@0 | 2610 | hide: function gListView_hide() { |
michael@0 | 2611 | gEventManager.unregisterInstallListener(this); |
michael@0 | 2612 | doPendingUninstalls(this._listBox); |
michael@0 | 2613 | }, |
michael@0 | 2614 | |
michael@0 | 2615 | showEmptyNotice: function gListView_showEmptyNotice(aShow) { |
michael@0 | 2616 | this._emptyNotice.hidden = !aShow; |
michael@0 | 2617 | }, |
michael@0 | 2618 | |
michael@0 | 2619 | onSortChanged: function gListView_onSortChanged(aSortBy, aAscending) { |
michael@0 | 2620 | sortList(this._listBox, aSortBy, aAscending); |
michael@0 | 2621 | }, |
michael@0 | 2622 | |
michael@0 | 2623 | onExternalInstall: function gListView_onExternalInstall(aAddon, aExistingAddon, aRequiresRestart) { |
michael@0 | 2624 | // The existing list item will take care of upgrade installs |
michael@0 | 2625 | if (aExistingAddon) |
michael@0 | 2626 | return; |
michael@0 | 2627 | |
michael@0 | 2628 | this.addItem(aAddon); |
michael@0 | 2629 | }, |
michael@0 | 2630 | |
michael@0 | 2631 | onDownloadStarted: function gListView_onDownloadStarted(aInstall) { |
michael@0 | 2632 | this.addItem(aInstall, true); |
michael@0 | 2633 | }, |
michael@0 | 2634 | |
michael@0 | 2635 | onInstallStarted: function gListView_onInstallStarted(aInstall) { |
michael@0 | 2636 | this.addItem(aInstall, true); |
michael@0 | 2637 | }, |
michael@0 | 2638 | |
michael@0 | 2639 | onDownloadCancelled: function gListView_onDownloadCancelled(aInstall) { |
michael@0 | 2640 | this.removeItem(aInstall, true); |
michael@0 | 2641 | }, |
michael@0 | 2642 | |
michael@0 | 2643 | onInstallCancelled: function gListView_onInstallCancelled(aInstall) { |
michael@0 | 2644 | this.removeItem(aInstall, true); |
michael@0 | 2645 | }, |
michael@0 | 2646 | |
michael@0 | 2647 | onInstallEnded: function gListView_onInstallEnded(aInstall) { |
michael@0 | 2648 | // Remove any install entries for upgrades, their status will appear against |
michael@0 | 2649 | // the existing item |
michael@0 | 2650 | if (aInstall.existingAddon) |
michael@0 | 2651 | this.removeItem(aInstall, true); |
michael@0 | 2652 | |
michael@0 | 2653 | if (aInstall.addon.type == "experiment") { |
michael@0 | 2654 | let item = this.getListItemForID(aInstall.addon.id); |
michael@0 | 2655 | if (item) { |
michael@0 | 2656 | item.endDate = getExperimentEndDate(aInstall.addon); |
michael@0 | 2657 | } |
michael@0 | 2658 | } |
michael@0 | 2659 | }, |
michael@0 | 2660 | |
michael@0 | 2661 | addItem: function gListView_addItem(aObj, aIsInstall) { |
michael@0 | 2662 | if (aObj.type != this._type) |
michael@0 | 2663 | return; |
michael@0 | 2664 | |
michael@0 | 2665 | if (aIsInstall && aObj.existingAddon) |
michael@0 | 2666 | return; |
michael@0 | 2667 | |
michael@0 | 2668 | let prop = aIsInstall ? "mInstall" : "mAddon"; |
michael@0 | 2669 | for (let item of this._listBox.childNodes) { |
michael@0 | 2670 | if (item[prop] == aObj) |
michael@0 | 2671 | return; |
michael@0 | 2672 | } |
michael@0 | 2673 | |
michael@0 | 2674 | let item = createItem(aObj, aIsInstall); |
michael@0 | 2675 | this._listBox.insertBefore(item, this._listBox.firstChild); |
michael@0 | 2676 | this.showEmptyNotice(false); |
michael@0 | 2677 | }, |
michael@0 | 2678 | |
michael@0 | 2679 | removeItem: function gListView_removeItem(aObj, aIsInstall) { |
michael@0 | 2680 | let prop = aIsInstall ? "mInstall" : "mAddon"; |
michael@0 | 2681 | |
michael@0 | 2682 | for (let item of this._listBox.childNodes) { |
michael@0 | 2683 | if (item[prop] == aObj) { |
michael@0 | 2684 | this._listBox.removeChild(item); |
michael@0 | 2685 | this.showEmptyNotice(this._listBox.itemCount == 0); |
michael@0 | 2686 | return; |
michael@0 | 2687 | } |
michael@0 | 2688 | } |
michael@0 | 2689 | }, |
michael@0 | 2690 | |
michael@0 | 2691 | getSelectedAddon: function gListView_getSelectedAddon() { |
michael@0 | 2692 | var item = this._listBox.selectedItem; |
michael@0 | 2693 | if (item) |
michael@0 | 2694 | return item.mAddon; |
michael@0 | 2695 | return null; |
michael@0 | 2696 | }, |
michael@0 | 2697 | |
michael@0 | 2698 | getListItemForID: function gListView_getListItemForID(aId) { |
michael@0 | 2699 | var listitem = this._listBox.firstChild; |
michael@0 | 2700 | while (listitem) { |
michael@0 | 2701 | if (listitem.getAttribute("status") == "installed" && listitem.mAddon.id == aId) |
michael@0 | 2702 | return listitem; |
michael@0 | 2703 | listitem = listitem.nextSibling; |
michael@0 | 2704 | } |
michael@0 | 2705 | return null; |
michael@0 | 2706 | } |
michael@0 | 2707 | }; |
michael@0 | 2708 | |
michael@0 | 2709 | |
michael@0 | 2710 | var gDetailView = { |
michael@0 | 2711 | node: null, |
michael@0 | 2712 | _addon: null, |
michael@0 | 2713 | _loadingTimer: null, |
michael@0 | 2714 | _autoUpdate: null, |
michael@0 | 2715 | |
michael@0 | 2716 | initialize: function gDetailView_initialize() { |
michael@0 | 2717 | this.node = document.getElementById("detail-view"); |
michael@0 | 2718 | |
michael@0 | 2719 | this._autoUpdate = document.getElementById("detail-autoUpdate"); |
michael@0 | 2720 | |
michael@0 | 2721 | var self = this; |
michael@0 | 2722 | this._autoUpdate.addEventListener("command", function autoUpdate_onCommand() { |
michael@0 | 2723 | self._addon.applyBackgroundUpdates = self._autoUpdate.value; |
michael@0 | 2724 | }, true); |
michael@0 | 2725 | }, |
michael@0 | 2726 | |
michael@0 | 2727 | shutdown: function gDetailView_shutdown() { |
michael@0 | 2728 | AddonManager.removeManagerListener(this); |
michael@0 | 2729 | }, |
michael@0 | 2730 | |
michael@0 | 2731 | onUpdateModeChanged: function gDetailView_onUpdateModeChanged() { |
michael@0 | 2732 | this.onPropertyChanged(["applyBackgroundUpdates"]); |
michael@0 | 2733 | }, |
michael@0 | 2734 | |
michael@0 | 2735 | _updateView: function gDetailView_updateView(aAddon, aIsRemote, aScrollToPreferences) { |
michael@0 | 2736 | AddonManager.addManagerListener(this); |
michael@0 | 2737 | this.clearLoading(); |
michael@0 | 2738 | |
michael@0 | 2739 | this._addon = aAddon; |
michael@0 | 2740 | gEventManager.registerAddonListener(this, aAddon.id); |
michael@0 | 2741 | gEventManager.registerInstallListener(this); |
michael@0 | 2742 | |
michael@0 | 2743 | this.node.setAttribute("type", aAddon.type); |
michael@0 | 2744 | |
michael@0 | 2745 | // If the search category isn't selected then make sure to select the |
michael@0 | 2746 | // correct category |
michael@0 | 2747 | if (gCategories.selected != "addons://search/") |
michael@0 | 2748 | gCategories.select("addons://list/" + aAddon.type); |
michael@0 | 2749 | |
michael@0 | 2750 | document.getElementById("detail-name").textContent = aAddon.name; |
michael@0 | 2751 | var icon = aAddon.icon64URL ? aAddon.icon64URL : aAddon.iconURL; |
michael@0 | 2752 | document.getElementById("detail-icon").src = icon ? icon : ""; |
michael@0 | 2753 | document.getElementById("detail-creator").setCreator(aAddon.creator, aAddon.homepageURL); |
michael@0 | 2754 | |
michael@0 | 2755 | var version = document.getElementById("detail-version"); |
michael@0 | 2756 | if (shouldShowVersionNumber(aAddon)) { |
michael@0 | 2757 | version.hidden = false; |
michael@0 | 2758 | version.value = aAddon.version; |
michael@0 | 2759 | } else { |
michael@0 | 2760 | version.hidden = true; |
michael@0 | 2761 | } |
michael@0 | 2762 | |
michael@0 | 2763 | var screenshot = document.getElementById("detail-screenshot"); |
michael@0 | 2764 | if (aAddon.screenshots && aAddon.screenshots.length > 0) { |
michael@0 | 2765 | if (aAddon.screenshots[0].thumbnailURL) { |
michael@0 | 2766 | screenshot.src = aAddon.screenshots[0].thumbnailURL; |
michael@0 | 2767 | screenshot.width = aAddon.screenshots[0].thumbnailWidth; |
michael@0 | 2768 | screenshot.height = aAddon.screenshots[0].thumbnailHeight; |
michael@0 | 2769 | } else { |
michael@0 | 2770 | screenshot.src = aAddon.screenshots[0].url; |
michael@0 | 2771 | screenshot.width = aAddon.screenshots[0].width; |
michael@0 | 2772 | screenshot.height = aAddon.screenshots[0].height; |
michael@0 | 2773 | } |
michael@0 | 2774 | screenshot.setAttribute("loading", "true"); |
michael@0 | 2775 | screenshot.hidden = false; |
michael@0 | 2776 | } else { |
michael@0 | 2777 | screenshot.hidden = true; |
michael@0 | 2778 | } |
michael@0 | 2779 | |
michael@0 | 2780 | var desc = document.getElementById("detail-desc"); |
michael@0 | 2781 | desc.textContent = aAddon.description; |
michael@0 | 2782 | |
michael@0 | 2783 | var fullDesc = document.getElementById("detail-fulldesc"); |
michael@0 | 2784 | if (aAddon.fullDescription) { |
michael@0 | 2785 | fullDesc.textContent = aAddon.fullDescription; |
michael@0 | 2786 | fullDesc.hidden = false; |
michael@0 | 2787 | } else { |
michael@0 | 2788 | fullDesc.hidden = true; |
michael@0 | 2789 | } |
michael@0 | 2790 | |
michael@0 | 2791 | var contributions = document.getElementById("detail-contributions"); |
michael@0 | 2792 | if ("contributionURL" in aAddon && aAddon.contributionURL) { |
michael@0 | 2793 | contributions.hidden = false; |
michael@0 | 2794 | var amount = document.getElementById("detail-contrib-suggested"); |
michael@0 | 2795 | if (aAddon.contributionAmount) { |
michael@0 | 2796 | amount.value = gStrings.ext.formatStringFromName("contributionAmount2", |
michael@0 | 2797 | [aAddon.contributionAmount], |
michael@0 | 2798 | 1); |
michael@0 | 2799 | amount.hidden = false; |
michael@0 | 2800 | } else { |
michael@0 | 2801 | amount.hidden = true; |
michael@0 | 2802 | } |
michael@0 | 2803 | } else { |
michael@0 | 2804 | contributions.hidden = true; |
michael@0 | 2805 | } |
michael@0 | 2806 | |
michael@0 | 2807 | if ("purchaseURL" in aAddon && aAddon.purchaseURL) { |
michael@0 | 2808 | var purchase = document.getElementById("detail-purchase-btn"); |
michael@0 | 2809 | purchase.label = gStrings.ext.formatStringFromName("cmd.purchaseAddon.label", |
michael@0 | 2810 | [aAddon.purchaseDisplayAmount], |
michael@0 | 2811 | 1); |
michael@0 | 2812 | purchase.accesskey = gStrings.ext.GetStringFromName("cmd.purchaseAddon.accesskey"); |
michael@0 | 2813 | } |
michael@0 | 2814 | |
michael@0 | 2815 | var updateDateRow = document.getElementById("detail-dateUpdated"); |
michael@0 | 2816 | if (aAddon.updateDate) { |
michael@0 | 2817 | var date = formatDate(aAddon.updateDate); |
michael@0 | 2818 | updateDateRow.value = date; |
michael@0 | 2819 | } else { |
michael@0 | 2820 | updateDateRow.value = null; |
michael@0 | 2821 | } |
michael@0 | 2822 | |
michael@0 | 2823 | // TODO if the add-on was downloaded from releases.mozilla.org link to the |
michael@0 | 2824 | // AMO profile (bug 590344) |
michael@0 | 2825 | if (false) { |
michael@0 | 2826 | document.getElementById("detail-repository-row").hidden = false; |
michael@0 | 2827 | document.getElementById("detail-homepage-row").hidden = true; |
michael@0 | 2828 | var repository = document.getElementById("detail-repository"); |
michael@0 | 2829 | repository.value = aAddon.homepageURL; |
michael@0 | 2830 | repository.href = aAddon.homepageURL; |
michael@0 | 2831 | } else if (aAddon.homepageURL) { |
michael@0 | 2832 | document.getElementById("detail-repository-row").hidden = true; |
michael@0 | 2833 | document.getElementById("detail-homepage-row").hidden = false; |
michael@0 | 2834 | var homepage = document.getElementById("detail-homepage"); |
michael@0 | 2835 | homepage.value = aAddon.homepageURL; |
michael@0 | 2836 | homepage.href = aAddon.homepageURL; |
michael@0 | 2837 | } else { |
michael@0 | 2838 | document.getElementById("detail-repository-row").hidden = true; |
michael@0 | 2839 | document.getElementById("detail-homepage-row").hidden = true; |
michael@0 | 2840 | } |
michael@0 | 2841 | |
michael@0 | 2842 | var rating = document.getElementById("detail-rating"); |
michael@0 | 2843 | if (aAddon.averageRating) { |
michael@0 | 2844 | rating.averageRating = aAddon.averageRating; |
michael@0 | 2845 | rating.hidden = false; |
michael@0 | 2846 | } else { |
michael@0 | 2847 | rating.hidden = true; |
michael@0 | 2848 | } |
michael@0 | 2849 | |
michael@0 | 2850 | var reviews = document.getElementById("detail-reviews"); |
michael@0 | 2851 | if (aAddon.reviewURL) { |
michael@0 | 2852 | var text = gStrings.ext.GetStringFromName("numReviews"); |
michael@0 | 2853 | text = PluralForm.get(aAddon.reviewCount, text) |
michael@0 | 2854 | text = text.replace("#1", aAddon.reviewCount); |
michael@0 | 2855 | reviews.value = text; |
michael@0 | 2856 | reviews.hidden = false; |
michael@0 | 2857 | reviews.href = aAddon.reviewURL; |
michael@0 | 2858 | } else { |
michael@0 | 2859 | reviews.hidden = true; |
michael@0 | 2860 | } |
michael@0 | 2861 | |
michael@0 | 2862 | document.getElementById("detail-rating-row").hidden = !aAddon.averageRating && !aAddon.reviewURL; |
michael@0 | 2863 | |
michael@0 | 2864 | var sizeRow = document.getElementById("detail-size"); |
michael@0 | 2865 | if (aAddon.size && aIsRemote) { |
michael@0 | 2866 | let [size, unit] = DownloadUtils.convertByteUnits(parseInt(aAddon.size)); |
michael@0 | 2867 | let formatted = gStrings.dl.GetStringFromName("doneSize"); |
michael@0 | 2868 | formatted = formatted.replace("#1", size).replace("#2", unit); |
michael@0 | 2869 | sizeRow.value = formatted; |
michael@0 | 2870 | } else { |
michael@0 | 2871 | sizeRow.value = null; |
michael@0 | 2872 | } |
michael@0 | 2873 | |
michael@0 | 2874 | var downloadsRow = document.getElementById("detail-downloads"); |
michael@0 | 2875 | if (aAddon.totalDownloads && aIsRemote) { |
michael@0 | 2876 | var downloads = aAddon.totalDownloads; |
michael@0 | 2877 | downloadsRow.value = downloads; |
michael@0 | 2878 | } else { |
michael@0 | 2879 | downloadsRow.value = null; |
michael@0 | 2880 | } |
michael@0 | 2881 | |
michael@0 | 2882 | var canUpdate = !aIsRemote && hasPermission(aAddon, "upgrade") && aAddon.id != AddonManager.hotfixID; |
michael@0 | 2883 | document.getElementById("detail-updates-row").hidden = !canUpdate; |
michael@0 | 2884 | |
michael@0 | 2885 | if ("applyBackgroundUpdates" in aAddon) { |
michael@0 | 2886 | this._autoUpdate.hidden = false; |
michael@0 | 2887 | this._autoUpdate.value = aAddon.applyBackgroundUpdates; |
michael@0 | 2888 | let hideFindUpdates = AddonManager.shouldAutoUpdate(this._addon); |
michael@0 | 2889 | document.getElementById("detail-findUpdates-btn").hidden = hideFindUpdates; |
michael@0 | 2890 | } else { |
michael@0 | 2891 | this._autoUpdate.hidden = true; |
michael@0 | 2892 | document.getElementById("detail-findUpdates-btn").hidden = false; |
michael@0 | 2893 | } |
michael@0 | 2894 | |
michael@0 | 2895 | document.getElementById("detail-prefs-btn").hidden = !aIsRemote && |
michael@0 | 2896 | !gViewController.commands.cmd_showItemPreferences.isEnabled(aAddon); |
michael@0 | 2897 | |
michael@0 | 2898 | var gridRows = document.querySelectorAll("#detail-grid rows row"); |
michael@0 | 2899 | let first = true; |
michael@0 | 2900 | for (let gridRow of gridRows) { |
michael@0 | 2901 | if (first && window.getComputedStyle(gridRow, null).getPropertyValue("display") != "none") { |
michael@0 | 2902 | gridRow.setAttribute("first-row", true); |
michael@0 | 2903 | first = false; |
michael@0 | 2904 | } else { |
michael@0 | 2905 | gridRow.removeAttribute("first-row"); |
michael@0 | 2906 | } |
michael@0 | 2907 | } |
michael@0 | 2908 | |
michael@0 | 2909 | if (this._addon.type == "experiment") { |
michael@0 | 2910 | let prefix = "details.experiment."; |
michael@0 | 2911 | let active = this._addon.isActive; |
michael@0 | 2912 | |
michael@0 | 2913 | let stateKey = prefix + "state." + (active ? "active" : "complete"); |
michael@0 | 2914 | let node = document.getElementById("detail-experiment-state"); |
michael@0 | 2915 | node.value = gStrings.ext.GetStringFromName(stateKey); |
michael@0 | 2916 | |
michael@0 | 2917 | let now = Date.now(); |
michael@0 | 2918 | let end = getExperimentEndDate(this._addon); |
michael@0 | 2919 | let days = Math.abs(end - now) / (24 * 60 * 60 * 1000); |
michael@0 | 2920 | |
michael@0 | 2921 | let timeKey = prefix + "time."; |
michael@0 | 2922 | let timeMessage; |
michael@0 | 2923 | if (days < 1) { |
michael@0 | 2924 | timeKey += (active ? "endsToday" : "endedToday"); |
michael@0 | 2925 | timeMessage = gStrings.ext.GetStringFromName(timeKey); |
michael@0 | 2926 | } else { |
michael@0 | 2927 | timeKey += (active ? "daysRemaining" : "daysPassed"); |
michael@0 | 2928 | days = Math.round(days); |
michael@0 | 2929 | let timeString = gStrings.ext.GetStringFromName(timeKey); |
michael@0 | 2930 | timeMessage = PluralForm.get(days, timeString) |
michael@0 | 2931 | .replace("#1", days); |
michael@0 | 2932 | } |
michael@0 | 2933 | |
michael@0 | 2934 | document.getElementById("detail-experiment-time").value = timeMessage; |
michael@0 | 2935 | } |
michael@0 | 2936 | |
michael@0 | 2937 | this.fillSettingsRows(aScrollToPreferences, (function updateView_fillSettingsRows() { |
michael@0 | 2938 | this.updateState(); |
michael@0 | 2939 | gViewController.notifyViewChanged(); |
michael@0 | 2940 | }).bind(this)); |
michael@0 | 2941 | }, |
michael@0 | 2942 | |
michael@0 | 2943 | show: function gDetailView_show(aAddonId, aRequest) { |
michael@0 | 2944 | let index = aAddonId.indexOf("/preferences"); |
michael@0 | 2945 | let scrollToPreferences = false; |
michael@0 | 2946 | if (index >= 0) { |
michael@0 | 2947 | aAddonId = aAddonId.substring(0, index); |
michael@0 | 2948 | scrollToPreferences = true; |
michael@0 | 2949 | } |
michael@0 | 2950 | |
michael@0 | 2951 | var self = this; |
michael@0 | 2952 | this._loadingTimer = setTimeout(function loadTimeOutTimer() { |
michael@0 | 2953 | self.node.setAttribute("loading-extended", true); |
michael@0 | 2954 | }, LOADING_MSG_DELAY); |
michael@0 | 2955 | |
michael@0 | 2956 | var view = gViewController.currentViewId; |
michael@0 | 2957 | |
michael@0 | 2958 | AddonManager.getAddonByID(aAddonId, function show_getAddonByID(aAddon) { |
michael@0 | 2959 | if (gViewController && aRequest != gViewController.currentViewRequest) |
michael@0 | 2960 | return; |
michael@0 | 2961 | |
michael@0 | 2962 | if (aAddon) { |
michael@0 | 2963 | self._updateView(aAddon, false, scrollToPreferences); |
michael@0 | 2964 | return; |
michael@0 | 2965 | } |
michael@0 | 2966 | |
michael@0 | 2967 | // Look for an add-on pending install |
michael@0 | 2968 | AddonManager.getAllInstalls(function show_getAllInstalls(aInstalls) { |
michael@0 | 2969 | for (let install of aInstalls) { |
michael@0 | 2970 | if (install.state == AddonManager.STATE_INSTALLED && |
michael@0 | 2971 | install.addon.id == aAddonId) { |
michael@0 | 2972 | self._updateView(install.addon, false); |
michael@0 | 2973 | return; |
michael@0 | 2974 | } |
michael@0 | 2975 | } |
michael@0 | 2976 | |
michael@0 | 2977 | if (aAddonId in gCachedAddons) { |
michael@0 | 2978 | self._updateView(gCachedAddons[aAddonId], true); |
michael@0 | 2979 | return; |
michael@0 | 2980 | } |
michael@0 | 2981 | |
michael@0 | 2982 | // This might happen due to session restore restoring us back to an |
michael@0 | 2983 | // add-on that doesn't exist but otherwise shouldn't normally happen. |
michael@0 | 2984 | // Either way just revert to the default view. |
michael@0 | 2985 | gViewController.replaceView(VIEW_DEFAULT); |
michael@0 | 2986 | }); |
michael@0 | 2987 | }); |
michael@0 | 2988 | }, |
michael@0 | 2989 | |
michael@0 | 2990 | hide: function gDetailView_hide() { |
michael@0 | 2991 | AddonManager.removeManagerListener(this); |
michael@0 | 2992 | this.clearLoading(); |
michael@0 | 2993 | if (this._addon) { |
michael@0 | 2994 | if (hasInlineOptions(this._addon)) { |
michael@0 | 2995 | Services.obs.notifyObservers(document, |
michael@0 | 2996 | AddonManager.OPTIONS_NOTIFICATION_HIDDEN, |
michael@0 | 2997 | this._addon.id); |
michael@0 | 2998 | } |
michael@0 | 2999 | |
michael@0 | 3000 | gEventManager.unregisterAddonListener(this, this._addon.id); |
michael@0 | 3001 | gEventManager.unregisterInstallListener(this); |
michael@0 | 3002 | this._addon = null; |
michael@0 | 3003 | |
michael@0 | 3004 | // Flush the preferences to disk so they survive any crash |
michael@0 | 3005 | if (this.node.getElementsByTagName("setting").length) |
michael@0 | 3006 | Services.prefs.savePrefFile(null); |
michael@0 | 3007 | } |
michael@0 | 3008 | }, |
michael@0 | 3009 | |
michael@0 | 3010 | updateState: function gDetailView_updateState() { |
michael@0 | 3011 | gViewController.updateCommands(); |
michael@0 | 3012 | |
michael@0 | 3013 | var pending = this._addon.pendingOperations; |
michael@0 | 3014 | if (pending != AddonManager.PENDING_NONE) { |
michael@0 | 3015 | this.node.removeAttribute("notification"); |
michael@0 | 3016 | |
michael@0 | 3017 | var pending = null; |
michael@0 | 3018 | const PENDING_OPERATIONS = ["enable", "disable", "install", "uninstall", |
michael@0 | 3019 | "upgrade"]; |
michael@0 | 3020 | for (let op of PENDING_OPERATIONS) { |
michael@0 | 3021 | if (isPending(this._addon, op)) |
michael@0 | 3022 | pending = op; |
michael@0 | 3023 | } |
michael@0 | 3024 | |
michael@0 | 3025 | this.node.setAttribute("pending", pending); |
michael@0 | 3026 | document.getElementById("detail-pending").textContent = gStrings.ext.formatStringFromName( |
michael@0 | 3027 | "details.notification." + pending, |
michael@0 | 3028 | [this._addon.name, gStrings.brandShortName], 2 |
michael@0 | 3029 | ); |
michael@0 | 3030 | } else { |
michael@0 | 3031 | this.node.removeAttribute("pending"); |
michael@0 | 3032 | |
michael@0 | 3033 | if (this._addon.blocklistState == Ci.nsIBlocklistService.STATE_BLOCKED) { |
michael@0 | 3034 | this.node.setAttribute("notification", "error"); |
michael@0 | 3035 | document.getElementById("detail-error").textContent = gStrings.ext.formatStringFromName( |
michael@0 | 3036 | "details.notification.blocked", |
michael@0 | 3037 | [this._addon.name], 1 |
michael@0 | 3038 | ); |
michael@0 | 3039 | var errorLink = document.getElementById("detail-error-link"); |
michael@0 | 3040 | errorLink.value = gStrings.ext.GetStringFromName("details.notification.blocked.link"); |
michael@0 | 3041 | errorLink.href = this._addon.blocklistURL; |
michael@0 | 3042 | errorLink.hidden = false; |
michael@0 | 3043 | } else if (!this._addon.isCompatible && (AddonManager.checkCompatibility || |
michael@0 | 3044 | (this._addon.blocklistState != Ci.nsIBlocklistService.STATE_SOFTBLOCKED))) { |
michael@0 | 3045 | this.node.setAttribute("notification", "warning"); |
michael@0 | 3046 | document.getElementById("detail-warning").textContent = gStrings.ext.formatStringFromName( |
michael@0 | 3047 | "details.notification.incompatible", |
michael@0 | 3048 | [this._addon.name, gStrings.brandShortName, gStrings.appVersion], 3 |
michael@0 | 3049 | ); |
michael@0 | 3050 | document.getElementById("detail-warning-link").hidden = true; |
michael@0 | 3051 | } else if (this._addon.blocklistState == Ci.nsIBlocklistService.STATE_SOFTBLOCKED) { |
michael@0 | 3052 | this.node.setAttribute("notification", "warning"); |
michael@0 | 3053 | document.getElementById("detail-warning").textContent = gStrings.ext.formatStringFromName( |
michael@0 | 3054 | "details.notification.softblocked", |
michael@0 | 3055 | [this._addon.name], 1 |
michael@0 | 3056 | ); |
michael@0 | 3057 | var warningLink = document.getElementById("detail-warning-link"); |
michael@0 | 3058 | warningLink.value = gStrings.ext.GetStringFromName("details.notification.softblocked.link"); |
michael@0 | 3059 | warningLink.href = this._addon.blocklistURL; |
michael@0 | 3060 | warningLink.hidden = false; |
michael@0 | 3061 | } else if (this._addon.blocklistState == Ci.nsIBlocklistService.STATE_OUTDATED) { |
michael@0 | 3062 | this.node.setAttribute("notification", "warning"); |
michael@0 | 3063 | document.getElementById("detail-warning").textContent = gStrings.ext.formatStringFromName( |
michael@0 | 3064 | "details.notification.outdated", |
michael@0 | 3065 | [this._addon.name], 1 |
michael@0 | 3066 | ); |
michael@0 | 3067 | var warningLink = document.getElementById("detail-warning-link"); |
michael@0 | 3068 | warningLink.value = gStrings.ext.GetStringFromName("details.notification.outdated.link"); |
michael@0 | 3069 | warningLink.href = Services.urlFormatter.formatURLPref("plugins.update.url"); |
michael@0 | 3070 | warningLink.hidden = false; |
michael@0 | 3071 | } else if (this._addon.blocklistState == Ci.nsIBlocklistService.STATE_VULNERABLE_UPDATE_AVAILABLE) { |
michael@0 | 3072 | this.node.setAttribute("notification", "error"); |
michael@0 | 3073 | document.getElementById("detail-error").textContent = gStrings.ext.formatStringFromName( |
michael@0 | 3074 | "details.notification.vulnerableUpdatable", |
michael@0 | 3075 | [this._addon.name], 1 |
michael@0 | 3076 | ); |
michael@0 | 3077 | var errorLink = document.getElementById("detail-error-link"); |
michael@0 | 3078 | errorLink.value = gStrings.ext.GetStringFromName("details.notification.vulnerableUpdatable.link"); |
michael@0 | 3079 | errorLink.href = this._addon.blocklistURL; |
michael@0 | 3080 | errorLink.hidden = false; |
michael@0 | 3081 | } else if (this._addon.blocklistState == Ci.nsIBlocklistService.STATE_VULNERABLE_NO_UPDATE) { |
michael@0 | 3082 | this.node.setAttribute("notification", "error"); |
michael@0 | 3083 | document.getElementById("detail-error").textContent = gStrings.ext.formatStringFromName( |
michael@0 | 3084 | "details.notification.vulnerableNoUpdate", |
michael@0 | 3085 | [this._addon.name], 1 |
michael@0 | 3086 | ); |
michael@0 | 3087 | var errorLink = document.getElementById("detail-error-link"); |
michael@0 | 3088 | errorLink.value = gStrings.ext.GetStringFromName("details.notification.vulnerableNoUpdate.link"); |
michael@0 | 3089 | errorLink.href = this._addon.blocklistURL; |
michael@0 | 3090 | errorLink.hidden = false; |
michael@0 | 3091 | } else { |
michael@0 | 3092 | this.node.removeAttribute("notification"); |
michael@0 | 3093 | } |
michael@0 | 3094 | } |
michael@0 | 3095 | |
michael@0 | 3096 | let menulist = document.getElementById("detail-state-menulist"); |
michael@0 | 3097 | let addonType = AddonManager.addonTypes[this._addon.type]; |
michael@0 | 3098 | if (addonType.flags & AddonManager.TYPE_SUPPORTS_ASK_TO_ACTIVATE && |
michael@0 | 3099 | (hasPermission(this._addon, "ask_to_activate") || |
michael@0 | 3100 | hasPermission(this._addon, "enable") || |
michael@0 | 3101 | hasPermission(this._addon, "disable"))) { |
michael@0 | 3102 | let askItem = document.getElementById("detail-ask-to-activate-menuitem"); |
michael@0 | 3103 | let alwaysItem = document.getElementById("detail-always-activate-menuitem"); |
michael@0 | 3104 | let neverItem = document.getElementById("detail-never-activate-menuitem"); |
michael@0 | 3105 | if (this._addon.userDisabled === true) { |
michael@0 | 3106 | menulist.selectedItem = neverItem; |
michael@0 | 3107 | } else if (this._addon.userDisabled == AddonManager.STATE_ASK_TO_ACTIVATE) { |
michael@0 | 3108 | menulist.selectedItem = askItem; |
michael@0 | 3109 | } else { |
michael@0 | 3110 | menulist.selectedItem = alwaysItem; |
michael@0 | 3111 | } |
michael@0 | 3112 | menulist.hidden = false; |
michael@0 | 3113 | } else { |
michael@0 | 3114 | menulist.hidden = true; |
michael@0 | 3115 | } |
michael@0 | 3116 | |
michael@0 | 3117 | this.node.setAttribute("active", this._addon.isActive); |
michael@0 | 3118 | }, |
michael@0 | 3119 | |
michael@0 | 3120 | clearLoading: function gDetailView_clearLoading() { |
michael@0 | 3121 | if (this._loadingTimer) { |
michael@0 | 3122 | clearTimeout(this._loadingTimer); |
michael@0 | 3123 | this._loadingTimer = null; |
michael@0 | 3124 | } |
michael@0 | 3125 | |
michael@0 | 3126 | this.node.removeAttribute("loading-extended"); |
michael@0 | 3127 | }, |
michael@0 | 3128 | |
michael@0 | 3129 | emptySettingsRows: function gDetailView_emptySettingsRows() { |
michael@0 | 3130 | var lastRow = document.getElementById("detail-downloads"); |
michael@0 | 3131 | var rows = lastRow.parentNode; |
michael@0 | 3132 | while (lastRow.nextSibling) |
michael@0 | 3133 | rows.removeChild(rows.lastChild); |
michael@0 | 3134 | }, |
michael@0 | 3135 | |
michael@0 | 3136 | fillSettingsRows: function gDetailView_fillSettingsRows(aScrollToPreferences, aCallback) { |
michael@0 | 3137 | this.emptySettingsRows(); |
michael@0 | 3138 | if (!hasInlineOptions(this._addon)) { |
michael@0 | 3139 | if (aCallback) |
michael@0 | 3140 | aCallback(); |
michael@0 | 3141 | return; |
michael@0 | 3142 | } |
michael@0 | 3143 | |
michael@0 | 3144 | // This function removes and returns the text content of aNode without |
michael@0 | 3145 | // removing any child elements. Removing the text nodes ensures any XBL |
michael@0 | 3146 | // bindings apply properly. |
michael@0 | 3147 | function stripTextNodes(aNode) { |
michael@0 | 3148 | var text = ''; |
michael@0 | 3149 | for (var i = 0; i < aNode.childNodes.length; i++) { |
michael@0 | 3150 | if (aNode.childNodes[i].nodeType != document.ELEMENT_NODE) { |
michael@0 | 3151 | text += aNode.childNodes[i].textContent; |
michael@0 | 3152 | aNode.removeChild(aNode.childNodes[i--]); |
michael@0 | 3153 | } else { |
michael@0 | 3154 | text += stripTextNodes(aNode.childNodes[i]); |
michael@0 | 3155 | } |
michael@0 | 3156 | } |
michael@0 | 3157 | return text; |
michael@0 | 3158 | } |
michael@0 | 3159 | |
michael@0 | 3160 | var rows = document.getElementById("detail-downloads").parentNode; |
michael@0 | 3161 | |
michael@0 | 3162 | try { |
michael@0 | 3163 | var xhr = new XMLHttpRequest(); |
michael@0 | 3164 | xhr.open("GET", this._addon.optionsURL, true); |
michael@0 | 3165 | xhr.responseType = "xml"; |
michael@0 | 3166 | xhr.onload = (function fillSettingsRows_onload() { |
michael@0 | 3167 | var xml = xhr.responseXML; |
michael@0 | 3168 | var settings = xml.querySelectorAll(":root > setting"); |
michael@0 | 3169 | |
michael@0 | 3170 | var firstSetting = null; |
michael@0 | 3171 | for (var setting of settings) { |
michael@0 | 3172 | |
michael@0 | 3173 | var desc = stripTextNodes(setting).trim(); |
michael@0 | 3174 | if (!setting.hasAttribute("desc")) |
michael@0 | 3175 | setting.setAttribute("desc", desc); |
michael@0 | 3176 | |
michael@0 | 3177 | var type = setting.getAttribute("type"); |
michael@0 | 3178 | if (type == "file" || type == "directory") |
michael@0 | 3179 | setting.setAttribute("fullpath", "true"); |
michael@0 | 3180 | |
michael@0 | 3181 | setting = document.importNode(setting, true); |
michael@0 | 3182 | var style = setting.getAttribute("style"); |
michael@0 | 3183 | if (style) { |
michael@0 | 3184 | setting.removeAttribute("style"); |
michael@0 | 3185 | setting.setAttribute("style", style); |
michael@0 | 3186 | } |
michael@0 | 3187 | |
michael@0 | 3188 | rows.appendChild(setting); |
michael@0 | 3189 | var visible = window.getComputedStyle(setting, null).getPropertyValue("display") != "none"; |
michael@0 | 3190 | if (!firstSetting && visible) { |
michael@0 | 3191 | setting.setAttribute("first-row", true); |
michael@0 | 3192 | firstSetting = setting; |
michael@0 | 3193 | } |
michael@0 | 3194 | } |
michael@0 | 3195 | |
michael@0 | 3196 | // Ensure the page has loaded and force the XBL bindings to be synchronously applied, |
michael@0 | 3197 | // then notify observers. |
michael@0 | 3198 | if (gViewController.viewPort.selectedPanel.hasAttribute("loading")) { |
michael@0 | 3199 | gDetailView.node.addEventListener("ViewChanged", function viewChangedEventListener() { |
michael@0 | 3200 | gDetailView.node.removeEventListener("ViewChanged", viewChangedEventListener, false); |
michael@0 | 3201 | if (firstSetting) |
michael@0 | 3202 | firstSetting.clientTop; |
michael@0 | 3203 | Services.obs.notifyObservers(document, |
michael@0 | 3204 | AddonManager.OPTIONS_NOTIFICATION_DISPLAYED, |
michael@0 | 3205 | gDetailView._addon.id); |
michael@0 | 3206 | if (aScrollToPreferences) |
michael@0 | 3207 | gDetailView.scrollToPreferencesRows(); |
michael@0 | 3208 | }, false); |
michael@0 | 3209 | } else { |
michael@0 | 3210 | if (firstSetting) |
michael@0 | 3211 | firstSetting.clientTop; |
michael@0 | 3212 | Services.obs.notifyObservers(document, |
michael@0 | 3213 | AddonManager.OPTIONS_NOTIFICATION_DISPLAYED, |
michael@0 | 3214 | this._addon.id); |
michael@0 | 3215 | if (aScrollToPreferences) |
michael@0 | 3216 | gDetailView.scrollToPreferencesRows(); |
michael@0 | 3217 | } |
michael@0 | 3218 | if (aCallback) |
michael@0 | 3219 | aCallback(); |
michael@0 | 3220 | }).bind(this); |
michael@0 | 3221 | xhr.onerror = function fillSettingsRows_onerror(aEvent) { |
michael@0 | 3222 | Cu.reportError("Error " + aEvent.target.status + |
michael@0 | 3223 | " occurred while receiving " + this._addon.optionsURL); |
michael@0 | 3224 | if (aCallback) |
michael@0 | 3225 | aCallback(); |
michael@0 | 3226 | }; |
michael@0 | 3227 | xhr.send(); |
michael@0 | 3228 | } catch(e) { |
michael@0 | 3229 | Cu.reportError(e); |
michael@0 | 3230 | if (aCallback) |
michael@0 | 3231 | aCallback(); |
michael@0 | 3232 | } |
michael@0 | 3233 | }, |
michael@0 | 3234 | |
michael@0 | 3235 | scrollToPreferencesRows: function gDetailView_scrollToPreferencesRows() { |
michael@0 | 3236 | // We find this row, rather than remembering it from above, |
michael@0 | 3237 | // in case it has been changed by the observers. |
michael@0 | 3238 | let firstRow = gDetailView.node.querySelector('setting[first-row="true"]'); |
michael@0 | 3239 | if (firstRow) { |
michael@0 | 3240 | let top = firstRow.boxObject.y; |
michael@0 | 3241 | top -= parseInt(window.getComputedStyle(firstRow, null).getPropertyValue("margin-top")); |
michael@0 | 3242 | |
michael@0 | 3243 | let detailViewBoxObject = gDetailView.node.boxObject; |
michael@0 | 3244 | top -= detailViewBoxObject.y; |
michael@0 | 3245 | |
michael@0 | 3246 | detailViewBoxObject.QueryInterface(Ci.nsIScrollBoxObject); |
michael@0 | 3247 | detailViewBoxObject.scrollTo(0, top); |
michael@0 | 3248 | } |
michael@0 | 3249 | }, |
michael@0 | 3250 | |
michael@0 | 3251 | getSelectedAddon: function gDetailView_getSelectedAddon() { |
michael@0 | 3252 | return this._addon; |
michael@0 | 3253 | }, |
michael@0 | 3254 | |
michael@0 | 3255 | onEnabling: function gDetailView_onEnabling() { |
michael@0 | 3256 | this.updateState(); |
michael@0 | 3257 | }, |
michael@0 | 3258 | |
michael@0 | 3259 | onEnabled: function gDetailView_onEnabled() { |
michael@0 | 3260 | this.updateState(); |
michael@0 | 3261 | this.fillSettingsRows(); |
michael@0 | 3262 | }, |
michael@0 | 3263 | |
michael@0 | 3264 | onDisabling: function gDetailView_onDisabling(aNeedsRestart) { |
michael@0 | 3265 | this.updateState(); |
michael@0 | 3266 | if (!aNeedsRestart && hasInlineOptions(this._addon)) { |
michael@0 | 3267 | Services.obs.notifyObservers(document, |
michael@0 | 3268 | AddonManager.OPTIONS_NOTIFICATION_HIDDEN, |
michael@0 | 3269 | this._addon.id); |
michael@0 | 3270 | } |
michael@0 | 3271 | }, |
michael@0 | 3272 | |
michael@0 | 3273 | onDisabled: function gDetailView_onDisabled() { |
michael@0 | 3274 | this.updateState(); |
michael@0 | 3275 | this.emptySettingsRows(); |
michael@0 | 3276 | }, |
michael@0 | 3277 | |
michael@0 | 3278 | onUninstalling: function gDetailView_onUninstalling() { |
michael@0 | 3279 | this.updateState(); |
michael@0 | 3280 | }, |
michael@0 | 3281 | |
michael@0 | 3282 | onUninstalled: function gDetailView_onUninstalled() { |
michael@0 | 3283 | gViewController.popState(); |
michael@0 | 3284 | }, |
michael@0 | 3285 | |
michael@0 | 3286 | onOperationCancelled: function gDetailView_onOperationCancelled() { |
michael@0 | 3287 | this.updateState(); |
michael@0 | 3288 | }, |
michael@0 | 3289 | |
michael@0 | 3290 | onPropertyChanged: function gDetailView_onPropertyChanged(aProperties) { |
michael@0 | 3291 | if (aProperties.indexOf("applyBackgroundUpdates") != -1) { |
michael@0 | 3292 | this._autoUpdate.value = this._addon.applyBackgroundUpdates; |
michael@0 | 3293 | let hideFindUpdates = AddonManager.shouldAutoUpdate(this._addon); |
michael@0 | 3294 | document.getElementById("detail-findUpdates-btn").hidden = hideFindUpdates; |
michael@0 | 3295 | } |
michael@0 | 3296 | |
michael@0 | 3297 | if (aProperties.indexOf("appDisabled") != -1 || |
michael@0 | 3298 | aProperties.indexOf("userDisabled") != -1) |
michael@0 | 3299 | this.updateState(); |
michael@0 | 3300 | }, |
michael@0 | 3301 | |
michael@0 | 3302 | onExternalInstall: function gDetailView_onExternalInstall(aAddon, aExistingAddon, aNeedsRestart) { |
michael@0 | 3303 | // Only care about upgrades for the currently displayed add-on |
michael@0 | 3304 | if (!aExistingAddon || aExistingAddon.id != this._addon.id) |
michael@0 | 3305 | return; |
michael@0 | 3306 | |
michael@0 | 3307 | if (!aNeedsRestart) |
michael@0 | 3308 | this._updateView(aAddon, false); |
michael@0 | 3309 | else |
michael@0 | 3310 | this.updateState(); |
michael@0 | 3311 | }, |
michael@0 | 3312 | |
michael@0 | 3313 | onInstallCancelled: function gDetailView_onInstallCancelled(aInstall) { |
michael@0 | 3314 | if (aInstall.addon.id == this._addon.id) |
michael@0 | 3315 | gViewController.popState(); |
michael@0 | 3316 | } |
michael@0 | 3317 | }; |
michael@0 | 3318 | |
michael@0 | 3319 | |
michael@0 | 3320 | var gUpdatesView = { |
michael@0 | 3321 | node: null, |
michael@0 | 3322 | _listBox: null, |
michael@0 | 3323 | _emptyNotice: null, |
michael@0 | 3324 | _sorters: null, |
michael@0 | 3325 | _updateSelected: null, |
michael@0 | 3326 | _categoryItem: null, |
michael@0 | 3327 | |
michael@0 | 3328 | initialize: function gUpdatesView_initialize() { |
michael@0 | 3329 | this.node = document.getElementById("updates-view"); |
michael@0 | 3330 | this._listBox = document.getElementById("updates-list"); |
michael@0 | 3331 | this._emptyNotice = document.getElementById("updates-list-empty"); |
michael@0 | 3332 | this._sorters = document.getElementById("updates-sorters"); |
michael@0 | 3333 | this._sorters.handler = this; |
michael@0 | 3334 | |
michael@0 | 3335 | this._categoryItem = gCategories.get("addons://updates/available"); |
michael@0 | 3336 | |
michael@0 | 3337 | this._updateSelected = document.getElementById("update-selected-btn"); |
michael@0 | 3338 | this._updateSelected.addEventListener("command", function updateSelected_onCommand() { |
michael@0 | 3339 | gUpdatesView.installSelected(); |
michael@0 | 3340 | }, false); |
michael@0 | 3341 | |
michael@0 | 3342 | this.updateAvailableCount(true); |
michael@0 | 3343 | |
michael@0 | 3344 | AddonManager.addAddonListener(this); |
michael@0 | 3345 | AddonManager.addInstallListener(this); |
michael@0 | 3346 | }, |
michael@0 | 3347 | |
michael@0 | 3348 | shutdown: function gUpdatesView_shutdown() { |
michael@0 | 3349 | AddonManager.removeAddonListener(this); |
michael@0 | 3350 | AddonManager.removeInstallListener(this); |
michael@0 | 3351 | }, |
michael@0 | 3352 | |
michael@0 | 3353 | show: function gUpdatesView_show(aType, aRequest) { |
michael@0 | 3354 | document.getElementById("empty-availableUpdates-msg").hidden = aType != "available"; |
michael@0 | 3355 | document.getElementById("empty-recentUpdates-msg").hidden = aType != "recent"; |
michael@0 | 3356 | this.showEmptyNotice(false); |
michael@0 | 3357 | |
michael@0 | 3358 | while (this._listBox.itemCount > 0) |
michael@0 | 3359 | this._listBox.removeItemAt(0); |
michael@0 | 3360 | |
michael@0 | 3361 | this.node.setAttribute("updatetype", aType); |
michael@0 | 3362 | if (aType == "recent") |
michael@0 | 3363 | this._showRecentUpdates(aRequest); |
michael@0 | 3364 | else |
michael@0 | 3365 | this._showAvailableUpdates(false, aRequest); |
michael@0 | 3366 | }, |
michael@0 | 3367 | |
michael@0 | 3368 | hide: function gUpdatesView_hide() { |
michael@0 | 3369 | this._updateSelected.hidden = true; |
michael@0 | 3370 | this._categoryItem.disabled = this._categoryItem.badgeCount == 0; |
michael@0 | 3371 | doPendingUninstalls(this._listBox); |
michael@0 | 3372 | }, |
michael@0 | 3373 | |
michael@0 | 3374 | _showRecentUpdates: function gUpdatesView_showRecentUpdates(aRequest) { |
michael@0 | 3375 | var self = this; |
michael@0 | 3376 | AddonManager.getAllAddons(function showRecentUpdates_getAllAddons(aAddonsList) { |
michael@0 | 3377 | if (gViewController && aRequest != gViewController.currentViewRequest) |
michael@0 | 3378 | return; |
michael@0 | 3379 | |
michael@0 | 3380 | var elements = []; |
michael@0 | 3381 | let threshold = Date.now() - UPDATES_RECENT_TIMESPAN; |
michael@0 | 3382 | for (let addon of aAddonsList) { |
michael@0 | 3383 | if (!addon.updateDate || addon.updateDate.getTime() < threshold) |
michael@0 | 3384 | continue; |
michael@0 | 3385 | |
michael@0 | 3386 | elements.push(createItem(addon)); |
michael@0 | 3387 | } |
michael@0 | 3388 | |
michael@0 | 3389 | self.showEmptyNotice(elements.length == 0); |
michael@0 | 3390 | if (elements.length > 0) { |
michael@0 | 3391 | sortElements(elements, [self._sorters.sortBy], self._sorters.ascending); |
michael@0 | 3392 | for (let element of elements) |
michael@0 | 3393 | self._listBox.appendChild(element); |
michael@0 | 3394 | } |
michael@0 | 3395 | |
michael@0 | 3396 | gViewController.notifyViewChanged(); |
michael@0 | 3397 | }); |
michael@0 | 3398 | }, |
michael@0 | 3399 | |
michael@0 | 3400 | _showAvailableUpdates: function gUpdatesView_showAvailableUpdates(aIsRefresh, aRequest) { |
michael@0 | 3401 | /* Disable the Update Selected button so it can't get clicked |
michael@0 | 3402 | before everything is initialized asynchronously. |
michael@0 | 3403 | It will get re-enabled by maybeDisableUpdateSelected(). */ |
michael@0 | 3404 | this._updateSelected.disabled = true; |
michael@0 | 3405 | |
michael@0 | 3406 | var self = this; |
michael@0 | 3407 | AddonManager.getAllInstalls(function showAvailableUpdates_getAllInstalls(aInstallsList) { |
michael@0 | 3408 | if (!aIsRefresh && gViewController && aRequest && |
michael@0 | 3409 | aRequest != gViewController.currentViewRequest) |
michael@0 | 3410 | return; |
michael@0 | 3411 | |
michael@0 | 3412 | if (aIsRefresh) { |
michael@0 | 3413 | self.showEmptyNotice(false); |
michael@0 | 3414 | self._updateSelected.hidden = true; |
michael@0 | 3415 | |
michael@0 | 3416 | while (self._listBox.itemCount > 0) |
michael@0 | 3417 | self._listBox.removeItemAt(0); |
michael@0 | 3418 | } |
michael@0 | 3419 | |
michael@0 | 3420 | var elements = []; |
michael@0 | 3421 | |
michael@0 | 3422 | for (let install of aInstallsList) { |
michael@0 | 3423 | if (!self.isManualUpdate(install)) |
michael@0 | 3424 | continue; |
michael@0 | 3425 | |
michael@0 | 3426 | let item = createItem(install.existingAddon); |
michael@0 | 3427 | item.setAttribute("upgrade", true); |
michael@0 | 3428 | item.addEventListener("IncludeUpdateChanged", function item_onIncludeUpdateChanged() { |
michael@0 | 3429 | self.maybeDisableUpdateSelected(); |
michael@0 | 3430 | }, false); |
michael@0 | 3431 | elements.push(item); |
michael@0 | 3432 | } |
michael@0 | 3433 | |
michael@0 | 3434 | self.showEmptyNotice(elements.length == 0); |
michael@0 | 3435 | if (elements.length > 0) { |
michael@0 | 3436 | self._updateSelected.hidden = false; |
michael@0 | 3437 | sortElements(elements, [self._sorters.sortBy], self._sorters.ascending); |
michael@0 | 3438 | for (let element of elements) |
michael@0 | 3439 | self._listBox.appendChild(element); |
michael@0 | 3440 | } |
michael@0 | 3441 | |
michael@0 | 3442 | // ensure badge count is in sync |
michael@0 | 3443 | self._categoryItem.badgeCount = self._listBox.itemCount; |
michael@0 | 3444 | |
michael@0 | 3445 | gViewController.notifyViewChanged(); |
michael@0 | 3446 | }); |
michael@0 | 3447 | }, |
michael@0 | 3448 | |
michael@0 | 3449 | showEmptyNotice: function gUpdatesView_showEmptyNotice(aShow) { |
michael@0 | 3450 | this._emptyNotice.hidden = !aShow; |
michael@0 | 3451 | }, |
michael@0 | 3452 | |
michael@0 | 3453 | isManualUpdate: function gUpdatesView_isManualUpdate(aInstall, aOnlyAvailable) { |
michael@0 | 3454 | var isManual = aInstall.existingAddon && |
michael@0 | 3455 | !AddonManager.shouldAutoUpdate(aInstall.existingAddon); |
michael@0 | 3456 | if (isManual && aOnlyAvailable) |
michael@0 | 3457 | return isInState(aInstall, "available"); |
michael@0 | 3458 | return isManual; |
michael@0 | 3459 | }, |
michael@0 | 3460 | |
michael@0 | 3461 | maybeRefresh: function gUpdatesView_maybeRefresh() { |
michael@0 | 3462 | if (gViewController.currentViewId == "addons://updates/available") |
michael@0 | 3463 | this._showAvailableUpdates(true); |
michael@0 | 3464 | this.updateAvailableCount(); |
michael@0 | 3465 | }, |
michael@0 | 3466 | |
michael@0 | 3467 | updateAvailableCount: function gUpdatesView_updateAvailableCount(aInitializing) { |
michael@0 | 3468 | if (aInitializing) |
michael@0 | 3469 | gPendingInitializations++; |
michael@0 | 3470 | var self = this; |
michael@0 | 3471 | AddonManager.getAllInstalls(function updateAvailableCount_getAllInstalls(aInstallsList) { |
michael@0 | 3472 | var count = aInstallsList.filter(function installListFilter(aInstall) { |
michael@0 | 3473 | return self.isManualUpdate(aInstall, true); |
michael@0 | 3474 | }).length; |
michael@0 | 3475 | self._categoryItem.disabled = gViewController.currentViewId != "addons://updates/available" && |
michael@0 | 3476 | count == 0; |
michael@0 | 3477 | self._categoryItem.badgeCount = count; |
michael@0 | 3478 | if (aInitializing) |
michael@0 | 3479 | notifyInitialized(); |
michael@0 | 3480 | }); |
michael@0 | 3481 | }, |
michael@0 | 3482 | |
michael@0 | 3483 | maybeDisableUpdateSelected: function gUpdatesView_maybeDisableUpdateSelected() { |
michael@0 | 3484 | for (let item of this._listBox.childNodes) { |
michael@0 | 3485 | if (item.includeUpdate) { |
michael@0 | 3486 | this._updateSelected.disabled = false; |
michael@0 | 3487 | return; |
michael@0 | 3488 | } |
michael@0 | 3489 | } |
michael@0 | 3490 | this._updateSelected.disabled = true; |
michael@0 | 3491 | }, |
michael@0 | 3492 | |
michael@0 | 3493 | installSelected: function gUpdatesView_installSelected() { |
michael@0 | 3494 | for (let item of this._listBox.childNodes) { |
michael@0 | 3495 | if (item.includeUpdate) |
michael@0 | 3496 | item.upgrade(); |
michael@0 | 3497 | } |
michael@0 | 3498 | |
michael@0 | 3499 | this._updateSelected.disabled = true; |
michael@0 | 3500 | }, |
michael@0 | 3501 | |
michael@0 | 3502 | getSelectedAddon: function gUpdatesView_getSelectedAddon() { |
michael@0 | 3503 | var item = this._listBox.selectedItem; |
michael@0 | 3504 | if (item) |
michael@0 | 3505 | return item.mAddon; |
michael@0 | 3506 | return null; |
michael@0 | 3507 | }, |
michael@0 | 3508 | |
michael@0 | 3509 | getListItemForID: function gUpdatesView_getListItemForID(aId) { |
michael@0 | 3510 | var listitem = this._listBox.firstChild; |
michael@0 | 3511 | while (listitem) { |
michael@0 | 3512 | if (listitem.mAddon.id == aId) |
michael@0 | 3513 | return listitem; |
michael@0 | 3514 | listitem = listitem.nextSibling; |
michael@0 | 3515 | } |
michael@0 | 3516 | return null; |
michael@0 | 3517 | }, |
michael@0 | 3518 | |
michael@0 | 3519 | onSortChanged: function gUpdatesView_onSortChanged(aSortBy, aAscending) { |
michael@0 | 3520 | sortList(this._listBox, aSortBy, aAscending); |
michael@0 | 3521 | }, |
michael@0 | 3522 | |
michael@0 | 3523 | onNewInstall: function gUpdatesView_onNewInstall(aInstall) { |
michael@0 | 3524 | if (!this.isManualUpdate(aInstall)) |
michael@0 | 3525 | return; |
michael@0 | 3526 | this.maybeRefresh(); |
michael@0 | 3527 | }, |
michael@0 | 3528 | |
michael@0 | 3529 | onInstallStarted: function gUpdatesView_onInstallStarted(aInstall) { |
michael@0 | 3530 | this.updateAvailableCount(); |
michael@0 | 3531 | }, |
michael@0 | 3532 | |
michael@0 | 3533 | onInstallCancelled: function gUpdatesView_onInstallCancelled(aInstall) { |
michael@0 | 3534 | if (!this.isManualUpdate(aInstall)) |
michael@0 | 3535 | return; |
michael@0 | 3536 | this.maybeRefresh(); |
michael@0 | 3537 | }, |
michael@0 | 3538 | |
michael@0 | 3539 | onPropertyChanged: function gUpdatesView_onPropertyChanged(aAddon, aProperties) { |
michael@0 | 3540 | if (aProperties.indexOf("applyBackgroundUpdates") != -1) |
michael@0 | 3541 | this.updateAvailableCount(); |
michael@0 | 3542 | } |
michael@0 | 3543 | }; |
michael@0 | 3544 | |
michael@0 | 3545 | function debuggingPrefChanged() { |
michael@0 | 3546 | gViewController.updateState(); |
michael@0 | 3547 | gViewController.updateCommands(); |
michael@0 | 3548 | gViewController.notifyViewChanged(); |
michael@0 | 3549 | } |
michael@0 | 3550 | |
michael@0 | 3551 | var gDragDrop = { |
michael@0 | 3552 | onDragOver: function gDragDrop_onDragOver(aEvent) { |
michael@0 | 3553 | var types = aEvent.dataTransfer.types; |
michael@0 | 3554 | if (types.contains("text/uri-list") || |
michael@0 | 3555 | types.contains("text/x-moz-url") || |
michael@0 | 3556 | types.contains("application/x-moz-file")) |
michael@0 | 3557 | aEvent.preventDefault(); |
michael@0 | 3558 | }, |
michael@0 | 3559 | |
michael@0 | 3560 | onDrop: function gDragDrop_onDrop(aEvent) { |
michael@0 | 3561 | var dataTransfer = aEvent.dataTransfer; |
michael@0 | 3562 | var urls = []; |
michael@0 | 3563 | |
michael@0 | 3564 | // Convert every dropped item into a url |
michael@0 | 3565 | for (var i = 0; i < dataTransfer.mozItemCount; i++) { |
michael@0 | 3566 | var url = dataTransfer.mozGetDataAt("text/uri-list", i); |
michael@0 | 3567 | if (url) { |
michael@0 | 3568 | urls.push(url); |
michael@0 | 3569 | continue; |
michael@0 | 3570 | } |
michael@0 | 3571 | |
michael@0 | 3572 | url = dataTransfer.mozGetDataAt("text/x-moz-url", i); |
michael@0 | 3573 | if (url) { |
michael@0 | 3574 | urls.push(url.split("\n")[0]); |
michael@0 | 3575 | continue; |
michael@0 | 3576 | } |
michael@0 | 3577 | |
michael@0 | 3578 | var file = dataTransfer.mozGetDataAt("application/x-moz-file", i); |
michael@0 | 3579 | if (file) { |
michael@0 | 3580 | urls.push(Services.io.newFileURI(file).spec); |
michael@0 | 3581 | continue; |
michael@0 | 3582 | } |
michael@0 | 3583 | } |
michael@0 | 3584 | |
michael@0 | 3585 | var pos = 0; |
michael@0 | 3586 | var installs = []; |
michael@0 | 3587 | |
michael@0 | 3588 | function buildNextInstall() { |
michael@0 | 3589 | if (pos == urls.length) { |
michael@0 | 3590 | if (installs.length > 0) { |
michael@0 | 3591 | // Display the normal install confirmation for the installs |
michael@0 | 3592 | AddonManager.installAddonsFromWebpage("application/x-xpinstall", |
michael@0 | 3593 | window, null, installs); |
michael@0 | 3594 | } |
michael@0 | 3595 | return; |
michael@0 | 3596 | } |
michael@0 | 3597 | |
michael@0 | 3598 | AddonManager.getInstallForURL(urls[pos++], function onDrop_getInstallForURL(aInstall) { |
michael@0 | 3599 | installs.push(aInstall); |
michael@0 | 3600 | buildNextInstall(); |
michael@0 | 3601 | }, "application/x-xpinstall"); |
michael@0 | 3602 | } |
michael@0 | 3603 | |
michael@0 | 3604 | buildNextInstall(); |
michael@0 | 3605 | |
michael@0 | 3606 | aEvent.preventDefault(); |
michael@0 | 3607 | } |
michael@0 | 3608 | }; |