Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* 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 file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | let Ci = Components.interfaces, Cc = Components.classes, Cu = Components.utils; |
michael@0 | 6 | |
michael@0 | 7 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 8 | Cu.import("resource://gre/modules/DownloadUtils.jsm"); |
michael@0 | 9 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 10 | Cu.import("resource://gre/modules/PluralForm.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm"); |
michael@0 | 14 | |
michael@0 | 15 | let gStrings = Services.strings.createBundle("chrome://browser/locale/aboutDownloads.properties"); |
michael@0 | 16 | |
michael@0 | 17 | let downloadTemplate = |
michael@0 | 18 | "<li downloadGUID='{guid}' class='list-item' role='button' state='{state}' contextmenu='downloadmenu'>" + |
michael@0 | 19 | "<img class='icon' src='{icon}'/>" + |
michael@0 | 20 | "<div class='details'>" + |
michael@0 | 21 | "<div class='row'>" + |
michael@0 | 22 | // This is a hack so that we can crop this label in its center |
michael@0 | 23 | "<xul:label class='title' crop='center' value='{target}'/>" + |
michael@0 | 24 | "<div class='date'>{date}</div>" + |
michael@0 | 25 | "</div>" + |
michael@0 | 26 | "<div class='size'>{size}</div>" + |
michael@0 | 27 | "<div class='domain'>{domain}</div>" + |
michael@0 | 28 | "<div class='displayState'>{displayState}</div>" + |
michael@0 | 29 | "</div>" + |
michael@0 | 30 | "</li>"; |
michael@0 | 31 | |
michael@0 | 32 | XPCOMUtils.defineLazyGetter(window, "gChromeWin", function () |
michael@0 | 33 | window.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 34 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 35 | .QueryInterface(Ci.nsIDocShellTreeItem) |
michael@0 | 36 | .rootTreeItem |
michael@0 | 37 | .QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 38 | .getInterface(Ci.nsIDOMWindow) |
michael@0 | 39 | .QueryInterface(Ci.nsIDOMChromeWindow)); |
michael@0 | 40 | |
michael@0 | 41 | |
michael@0 | 42 | var ContextMenus = { |
michael@0 | 43 | target: null, |
michael@0 | 44 | |
michael@0 | 45 | init: function() { |
michael@0 | 46 | document.addEventListener("contextmenu", this, false); |
michael@0 | 47 | document.getElementById("contextmenu-open").addEventListener("click", this.open.bind(this), false); |
michael@0 | 48 | document.getElementById("contextmenu-retry").addEventListener("click", this.retry.bind(this), false); |
michael@0 | 49 | document.getElementById("contextmenu-remove").addEventListener("click", this.remove.bind(this), false); |
michael@0 | 50 | document.getElementById("contextmenu-pause").addEventListener("click", this.pause.bind(this), false); |
michael@0 | 51 | document.getElementById("contextmenu-resume").addEventListener("click", this.resume.bind(this), false); |
michael@0 | 52 | document.getElementById("contextmenu-cancel").addEventListener("click", this.cancel.bind(this), false); |
michael@0 | 53 | document.getElementById("contextmenu-removeall").addEventListener("click", this.removeAll.bind(this), false); |
michael@0 | 54 | this.items = [ |
michael@0 | 55 | { name: "open", states: [Downloads._dlmgr.DOWNLOAD_FINISHED] }, |
michael@0 | 56 | { name: "retry", states: [Downloads._dlmgr.DOWNLOAD_FAILED, Downloads._dlmgr.DOWNLOAD_CANCELED] }, |
michael@0 | 57 | { name: "remove", states: [Downloads._dlmgr.DOWNLOAD_FINISHED,Downloads._dlmgr.DOWNLOAD_FAILED, Downloads._dlmgr.DOWNLOAD_CANCELED] }, |
michael@0 | 58 | { name: "removeall", states: [Downloads._dlmgr.DOWNLOAD_FINISHED,Downloads._dlmgr.DOWNLOAD_FAILED, Downloads._dlmgr.DOWNLOAD_CANCELED] }, |
michael@0 | 59 | { name: "pause", states: [Downloads._dlmgr.DOWNLOAD_DOWNLOADING] }, |
michael@0 | 60 | { name: "resume", states: [Downloads._dlmgr.DOWNLOAD_PAUSED] }, |
michael@0 | 61 | { name: "cancel", states: [Downloads._dlmgr.DOWNLOAD_DOWNLOADING, Downloads._dlmgr.DOWNLOAD_NOTSTARTED, Downloads._dlmgr.DOWNLOAD_QUEUED, Downloads._dlmgr.DOWNLOAD_PAUSED] }, |
michael@0 | 62 | ]; |
michael@0 | 63 | }, |
michael@0 | 64 | |
michael@0 | 65 | handleEvent: function(event) { |
michael@0 | 66 | // store the target of context menu events so that we know which app to act on |
michael@0 | 67 | this.target = event.target; |
michael@0 | 68 | while (!this.target.hasAttribute("contextmenu")) { |
michael@0 | 69 | this.target = this.target.parentNode; |
michael@0 | 70 | } |
michael@0 | 71 | if (!this.target) |
michael@0 | 72 | return; |
michael@0 | 73 | |
michael@0 | 74 | let state = parseInt(this.target.getAttribute("state")); |
michael@0 | 75 | for (let i = 0; i < this.items.length; i++) { |
michael@0 | 76 | var item = this.items[i]; |
michael@0 | 77 | let enabled = (item.states.indexOf(state) > -1); |
michael@0 | 78 | if (enabled) |
michael@0 | 79 | document.getElementById("contextmenu-" + item.name).removeAttribute("hidden"); |
michael@0 | 80 | else |
michael@0 | 81 | document.getElementById("contextmenu-" + item.name).setAttribute("hidden", "true"); |
michael@0 | 82 | } |
michael@0 | 83 | }, |
michael@0 | 84 | |
michael@0 | 85 | // Open shown only for downloads that completed successfully |
michael@0 | 86 | open: function(event) { |
michael@0 | 87 | Downloads.openDownload(this.target); |
michael@0 | 88 | this.target = null; |
michael@0 | 89 | }, |
michael@0 | 90 | |
michael@0 | 91 | // Retry shown when its failed, canceled, blocked(covered in failed, see _getState()) |
michael@0 | 92 | retry: function (event) { |
michael@0 | 93 | Downloads.retryDownload(this.target); |
michael@0 | 94 | this.target = null; |
michael@0 | 95 | }, |
michael@0 | 96 | |
michael@0 | 97 | // Remove shown when its canceled, finished, failed(failed includes blocked and dirty, see _getState()) |
michael@0 | 98 | remove: function (event) { |
michael@0 | 99 | Downloads.removeDownload(this.target); |
michael@0 | 100 | this.target = null; |
michael@0 | 101 | }, |
michael@0 | 102 | |
michael@0 | 103 | // Pause shown when item is currently downloading |
michael@0 | 104 | pause: function (event) { |
michael@0 | 105 | Downloads.pauseDownload(this.target); |
michael@0 | 106 | this.target = null; |
michael@0 | 107 | }, |
michael@0 | 108 | |
michael@0 | 109 | // Resume shown for paused items only |
michael@0 | 110 | resume: function (event) { |
michael@0 | 111 | Downloads.resumeDownload(this.target); |
michael@0 | 112 | this.target = null; |
michael@0 | 113 | }, |
michael@0 | 114 | |
michael@0 | 115 | // Cancel shown when its downloading, notstarted, queued or paused |
michael@0 | 116 | cancel: function (event) { |
michael@0 | 117 | Downloads.cancelDownload(this.target); |
michael@0 | 118 | this.target = null; |
michael@0 | 119 | }, |
michael@0 | 120 | |
michael@0 | 121 | removeAll: function(event) { |
michael@0 | 122 | Downloads.removeAll(); |
michael@0 | 123 | this.target = null; |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | |
michael@0 | 128 | let Downloads = { |
michael@0 | 129 | init: function dl_init() { |
michael@0 | 130 | function onClick(evt) { |
michael@0 | 131 | let target = evt.target; |
michael@0 | 132 | while (target.nodeName != "li") { |
michael@0 | 133 | target = target.parentNode; |
michael@0 | 134 | if (!target) |
michael@0 | 135 | return; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | Downloads.openDownload(target); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | this._normalList = document.getElementById("normal-downloads-list"); |
michael@0 | 142 | this._privateList = document.getElementById("private-downloads-list"); |
michael@0 | 143 | |
michael@0 | 144 | this._normalList.addEventListener("click", onClick, false); |
michael@0 | 145 | this._privateList.addEventListener("click", onClick, false); |
michael@0 | 146 | |
michael@0 | 147 | this._dlmgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager); |
michael@0 | 148 | this._dlmgr.addPrivacyAwareListener(this); |
michael@0 | 149 | |
michael@0 | 150 | Services.obs.addObserver(this, "last-pb-context-exited", false); |
michael@0 | 151 | Services.obs.addObserver(this, "download-manager-remove-download-guid", false); |
michael@0 | 152 | |
michael@0 | 153 | // If we have private downloads, show them all immediately. If we were to |
michael@0 | 154 | // add them asynchronously, there's a small chance we could get a |
michael@0 | 155 | // "last-pb-context-exited" notification before downloads are added to the |
michael@0 | 156 | // list, meaning we'd show private downloads without any private tabs open. |
michael@0 | 157 | let privateEntries = this.getDownloads({ isPrivate: true }); |
michael@0 | 158 | this._stepAddEntries(privateEntries, this._privateList, privateEntries.length); |
michael@0 | 159 | |
michael@0 | 160 | // Add non-private downloads |
michael@0 | 161 | let normalEntries = this.getDownloads({ isPrivate: false }); |
michael@0 | 162 | this._stepAddEntries(normalEntries, this._normalList, 1, this._scrollToSelectedDownload.bind(this)); |
michael@0 | 163 | ContextMenus.init(); |
michael@0 | 164 | }, |
michael@0 | 165 | |
michael@0 | 166 | uninit: function dl_uninit() { |
michael@0 | 167 | let contextmenus = gChromeWin.NativeWindow.contextmenus; |
michael@0 | 168 | contextmenus.remove(this.openMenuItem); |
michael@0 | 169 | contextmenus.remove(this.removeMenuItem); |
michael@0 | 170 | contextmenus.remove(this.pauseMenuItem); |
michael@0 | 171 | contextmenus.remove(this.resumeMenuItem); |
michael@0 | 172 | contextmenus.remove(this.retryMenuItem); |
michael@0 | 173 | contextmenus.remove(this.cancelMenuItem); |
michael@0 | 174 | contextmenus.remove(this.deleteAllMenuItem); |
michael@0 | 175 | |
michael@0 | 176 | this._dlmgr.removeListener(this); |
michael@0 | 177 | Services.obs.removeObserver(this, "last-pb-context-exited"); |
michael@0 | 178 | Services.obs.removeObserver(this, "download-manager-remove-download-guid"); |
michael@0 | 179 | }, |
michael@0 | 180 | |
michael@0 | 181 | onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, |
michael@0 | 182 | aCurTotalProgress, aMaxTotalProgress, aDownload) { }, |
michael@0 | 183 | onDownloadStateChange: function(aState, aDownload) { |
michael@0 | 184 | switch (aDownload.state) { |
michael@0 | 185 | case Ci.nsIDownloadManager.DOWNLOAD_FAILED: |
michael@0 | 186 | case Ci.nsIDownloadManager.DOWNLOAD_CANCELED: |
michael@0 | 187 | case Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_PARENTAL: |
michael@0 | 188 | case Ci.nsIDownloadManager.DOWNLOAD_DIRTY: |
michael@0 | 189 | case Ci.nsIDownloadManager.DOWNLOAD_FINISHED: |
michael@0 | 190 | // For all "completed" states, move them after active downloads |
michael@0 | 191 | this._moveDownloadAfterActive(this._getElementForDownload(aDownload.guid)); |
michael@0 | 192 | |
michael@0 | 193 | // Fall-through the rest |
michael@0 | 194 | case Ci.nsIDownloadManager.DOWNLOAD_SCANNING: |
michael@0 | 195 | case Ci.nsIDownloadManager.DOWNLOAD_QUEUED: |
michael@0 | 196 | case Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING: |
michael@0 | 197 | let item = this._getElementForDownload(aDownload.guid); |
michael@0 | 198 | if (item) |
michael@0 | 199 | this._updateDownloadRow(item, aDownload); |
michael@0 | 200 | else |
michael@0 | 201 | this._insertDownloadRow(aDownload); |
michael@0 | 202 | break; |
michael@0 | 203 | } |
michael@0 | 204 | }, |
michael@0 | 205 | onStateChange: function(aWebProgress, aRequest, aState, aStatus, aDownload) { }, |
michael@0 | 206 | onSecurityChange: function(aWebProgress, aRequest, aState, aDownload) { }, |
michael@0 | 207 | |
michael@0 | 208 | observe: function (aSubject, aTopic, aData) { |
michael@0 | 209 | switch (aTopic) { |
michael@0 | 210 | case "last-pb-context-exited": |
michael@0 | 211 | this._privateList.innerHTML = ""; |
michael@0 | 212 | break; |
michael@0 | 213 | case "download-manager-remove-download-guid": { |
michael@0 | 214 | let guid = aSubject.QueryInterface(Ci.nsISupportsCString).data; |
michael@0 | 215 | this._removeItem(this._getElementForDownload(guid)); |
michael@0 | 216 | break; |
michael@0 | 217 | } |
michael@0 | 218 | } |
michael@0 | 219 | }, |
michael@0 | 220 | |
michael@0 | 221 | _moveDownloadAfterActive: function dl_moveDownloadAfterActive(aItem) { |
michael@0 | 222 | // Move downloads that just reached a "completed" state below any active |
michael@0 | 223 | try { |
michael@0 | 224 | // Iterate down until we find a non-active download |
michael@0 | 225 | let next = aItem.nextElementSibling; |
michael@0 | 226 | while (next && this._inProgress(next.getAttribute("state"))) |
michael@0 | 227 | next = next.nextElementSibling; |
michael@0 | 228 | // Move the item |
michael@0 | 229 | aItem.parentNode.insertBefore(aItem, next); |
michael@0 | 230 | } catch (ex) { |
michael@0 | 231 | this.logError("_moveDownloadAfterActive() " + ex); |
michael@0 | 232 | } |
michael@0 | 233 | }, |
michael@0 | 234 | |
michael@0 | 235 | _inProgress: function dl_inProgress(aState) { |
michael@0 | 236 | return [ |
michael@0 | 237 | this._dlmgr.DOWNLOAD_NOTSTARTED, |
michael@0 | 238 | this._dlmgr.DOWNLOAD_QUEUED, |
michael@0 | 239 | this._dlmgr.DOWNLOAD_DOWNLOADING, |
michael@0 | 240 | this._dlmgr.DOWNLOAD_PAUSED, |
michael@0 | 241 | this._dlmgr.DOWNLOAD_SCANNING, |
michael@0 | 242 | ].indexOf(parseInt(aState)) != -1; |
michael@0 | 243 | }, |
michael@0 | 244 | |
michael@0 | 245 | _insertDownloadRow: function dl_insertDownloadRow(aDownload) { |
michael@0 | 246 | let updatedState = this._getState(aDownload.state); |
michael@0 | 247 | let item = this._createItem(downloadTemplate, { |
michael@0 | 248 | guid: aDownload.guid, |
michael@0 | 249 | target: aDownload.displayName, |
michael@0 | 250 | icon: "moz-icon://" + aDownload.displayName + "?size=64", |
michael@0 | 251 | date: DownloadUtils.getReadableDates(new Date())[0], |
michael@0 | 252 | domain: DownloadUtils.getURIHost(aDownload.source.spec)[0], |
michael@0 | 253 | size: this._getDownloadSize(aDownload.size), |
michael@0 | 254 | displayState: this._getStateString(updatedState), |
michael@0 | 255 | state: updatedState |
michael@0 | 256 | }); |
michael@0 | 257 | list = aDownload.isPrivate ? this._privateList : this._normalList; |
michael@0 | 258 | list.insertAdjacentHTML("afterbegin", item); |
michael@0 | 259 | }, |
michael@0 | 260 | |
michael@0 | 261 | _getDownloadSize: function dl_getDownloadSize(aSize) { |
michael@0 | 262 | if (aSize > 0) { |
michael@0 | 263 | let displaySize = DownloadUtils.convertByteUnits(aSize); |
michael@0 | 264 | return displaySize.join(""); // [0] is size, [1] is units |
michael@0 | 265 | } |
michael@0 | 266 | return gStrings.GetStringFromName("downloadState.unknownSize"); |
michael@0 | 267 | }, |
michael@0 | 268 | |
michael@0 | 269 | // Not all states are displayed as-is on mobile, some are translated to a generic state |
michael@0 | 270 | _getState: function dl_getState(aState) { |
michael@0 | 271 | let str; |
michael@0 | 272 | switch (aState) { |
michael@0 | 273 | // Downloading and Scanning states show up as "Downloading" |
michael@0 | 274 | case this._dlmgr.DOWNLOAD_DOWNLOADING: |
michael@0 | 275 | case this._dlmgr.DOWNLOAD_SCANNING: |
michael@0 | 276 | str = this._dlmgr.DOWNLOAD_DOWNLOADING; |
michael@0 | 277 | break; |
michael@0 | 278 | |
michael@0 | 279 | // Failed, Dirty and Blocked states show up as "Failed" |
michael@0 | 280 | case this._dlmgr.DOWNLOAD_FAILED: |
michael@0 | 281 | case this._dlmgr.DOWNLOAD_DIRTY: |
michael@0 | 282 | case this._dlmgr.DOWNLOAD_BLOCKED_POLICY: |
michael@0 | 283 | case this._dlmgr.DOWNLOAD_BLOCKED_PARENTAL: |
michael@0 | 284 | str = this._dlmgr.DOWNLOAD_FAILED; |
michael@0 | 285 | break; |
michael@0 | 286 | |
michael@0 | 287 | /* QUEUED and NOTSTARTED are not translated as they |
michael@0 | 288 | dont fall under a common state but we still need |
michael@0 | 289 | to display a common "status" on the UI */ |
michael@0 | 290 | |
michael@0 | 291 | default: |
michael@0 | 292 | str = aState; |
michael@0 | 293 | } |
michael@0 | 294 | return str; |
michael@0 | 295 | }, |
michael@0 | 296 | |
michael@0 | 297 | // Note: This doesn't cover all states as some of the states are translated in _getState() |
michael@0 | 298 | _getStateString: function dl_getStateString(aState) { |
michael@0 | 299 | let str; |
michael@0 | 300 | switch (aState) { |
michael@0 | 301 | case this._dlmgr.DOWNLOAD_DOWNLOADING: |
michael@0 | 302 | str = "downloadState.downloading"; |
michael@0 | 303 | break; |
michael@0 | 304 | case this._dlmgr.DOWNLOAD_CANCELED: |
michael@0 | 305 | str = "downloadState.canceled"; |
michael@0 | 306 | break; |
michael@0 | 307 | case this._dlmgr.DOWNLOAD_FAILED: |
michael@0 | 308 | str = "downloadState.failed"; |
michael@0 | 309 | break; |
michael@0 | 310 | case this._dlmgr.DOWNLOAD_PAUSED: |
michael@0 | 311 | str = "downloadState.paused"; |
michael@0 | 312 | break; |
michael@0 | 313 | |
michael@0 | 314 | // Queued and Notstarted show up as "Starting..." |
michael@0 | 315 | case this._dlmgr.DOWNLOAD_QUEUED: |
michael@0 | 316 | case this._dlmgr.DOWNLOAD_NOTSTARTED: |
michael@0 | 317 | str = "downloadState.starting"; |
michael@0 | 318 | break; |
michael@0 | 319 | |
michael@0 | 320 | default: |
michael@0 | 321 | return ""; |
michael@0 | 322 | } |
michael@0 | 323 | return gStrings.GetStringFromName(str); |
michael@0 | 324 | }, |
michael@0 | 325 | |
michael@0 | 326 | _updateItem: function dl_updateItem(aItem, aValues) { |
michael@0 | 327 | for (let i in aValues) { |
michael@0 | 328 | aItem.querySelector("." + i).textContent = aValues[i]; |
michael@0 | 329 | } |
michael@0 | 330 | }, |
michael@0 | 331 | |
michael@0 | 332 | _initStatement: function dv__initStatement(aIsPrivate) { |
michael@0 | 333 | let dbConn = aIsPrivate ? this._dlmgr.privateDBConnection : this._dlmgr.DBConnection; |
michael@0 | 334 | return dbConn.createStatement( |
michael@0 | 335 | "SELECT guid, name, source, state, startTime, endTime, referrer, " + |
michael@0 | 336 | "currBytes, maxBytes, state IN (?1, ?2, ?3, ?4, ?5) isActive " + |
michael@0 | 337 | "FROM moz_downloads " + |
michael@0 | 338 | "ORDER BY isActive DESC, endTime DESC, startTime DESC"); |
michael@0 | 339 | }, |
michael@0 | 340 | |
michael@0 | 341 | _createItem: function _createItem(aTemplate, aValues) { |
michael@0 | 342 | function htmlEscape(s) { |
michael@0 | 343 | s = s.replace(/&/g, "&"); |
michael@0 | 344 | s = s.replace(/>/g, ">"); |
michael@0 | 345 | s = s.replace(/</g, "<"); |
michael@0 | 346 | s = s.replace(/"/g, """); |
michael@0 | 347 | s = s.replace(/'/g, "'"); |
michael@0 | 348 | return s; |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | let t = aTemplate; |
michael@0 | 352 | for (let key in aValues) { |
michael@0 | 353 | if (aValues.hasOwnProperty(key)) { |
michael@0 | 354 | let regEx = new RegExp("{" + key + "}", "g"); |
michael@0 | 355 | let value = htmlEscape(aValues[key].toString()); |
michael@0 | 356 | t = t.replace(regEx, value); |
michael@0 | 357 | } |
michael@0 | 358 | } |
michael@0 | 359 | return t; |
michael@0 | 360 | }, |
michael@0 | 361 | |
michael@0 | 362 | _getEntry: function dv__getEntry(aStmt) { |
michael@0 | 363 | try { |
michael@0 | 364 | if (!aStmt.executeStep()) { |
michael@0 | 365 | return null; |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | let updatedState = this._getState(aStmt.row.state); |
michael@0 | 369 | // Try to get the attribute values from the statement |
michael@0 | 370 | |
michael@0 | 371 | return { |
michael@0 | 372 | guid: aStmt.row.guid, |
michael@0 | 373 | target: aStmt.row.name, |
michael@0 | 374 | icon: "moz-icon://" + aStmt.row.name + "?size=64", |
michael@0 | 375 | date: DownloadUtils.getReadableDates(new Date(aStmt.row.endTime / 1000))[0], |
michael@0 | 376 | domain: DownloadUtils.getURIHost(aStmt.row.source)[0], |
michael@0 | 377 | size: this._getDownloadSize(aStmt.row.maxBytes), |
michael@0 | 378 | displayState: this._getStateString(updatedState), |
michael@0 | 379 | state: updatedState |
michael@0 | 380 | }; |
michael@0 | 381 | |
michael@0 | 382 | } catch (e) { |
michael@0 | 383 | // Something went wrong when stepping or getting values, so clear and quit |
michael@0 | 384 | this.logError("_getEntry() " + e); |
michael@0 | 385 | aStmt.reset(); |
michael@0 | 386 | return null; |
michael@0 | 387 | } |
michael@0 | 388 | }, |
michael@0 | 389 | |
michael@0 | 390 | _stepAddEntries: function dv__stepAddEntries(aEntries, aList, aNumItems, aCallback) { |
michael@0 | 391 | |
michael@0 | 392 | if (aEntries.length == 0){ |
michael@0 | 393 | if (aCallback) |
michael@0 | 394 | aCallback(); |
michael@0 | 395 | |
michael@0 | 396 | return; |
michael@0 | 397 | } |
michael@0 | 398 | |
michael@0 | 399 | let attrs = aEntries.shift(); |
michael@0 | 400 | let item = this._createItem(downloadTemplate, attrs); |
michael@0 | 401 | aList.insertAdjacentHTML("beforeend", item); |
michael@0 | 402 | |
michael@0 | 403 | // Add another item to the list if we should; otherwise, let the UI update |
michael@0 | 404 | // and continue later |
michael@0 | 405 | if (aNumItems > 1) { |
michael@0 | 406 | this._stepAddEntries(aEntries, aList, aNumItems - 1, aCallback); |
michael@0 | 407 | } else { |
michael@0 | 408 | // Use a shorter delay for earlier downloads to display them faster |
michael@0 | 409 | let delay = Math.min(aList.itemCount * 10, 300); |
michael@0 | 410 | setTimeout(function () { |
michael@0 | 411 | this._stepAddEntries(aEntries, aList, 5, aCallback); |
michael@0 | 412 | }.bind(this), delay); |
michael@0 | 413 | } |
michael@0 | 414 | }, |
michael@0 | 415 | |
michael@0 | 416 | getDownloads: function dl_getDownloads(aParams) { |
michael@0 | 417 | aParams = aParams || {}; |
michael@0 | 418 | let stmt = this._initStatement(aParams.isPrivate); |
michael@0 | 419 | |
michael@0 | 420 | stmt.reset(); |
michael@0 | 421 | stmt.bindInt32Parameter(0, Ci.nsIDownloadManager.DOWNLOAD_NOTSTARTED); |
michael@0 | 422 | stmt.bindInt32Parameter(1, Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING); |
michael@0 | 423 | stmt.bindInt32Parameter(2, Ci.nsIDownloadManager.DOWNLOAD_PAUSED); |
michael@0 | 424 | stmt.bindInt32Parameter(3, Ci.nsIDownloadManager.DOWNLOAD_QUEUED); |
michael@0 | 425 | stmt.bindInt32Parameter(4, Ci.nsIDownloadManager.DOWNLOAD_SCANNING); |
michael@0 | 426 | |
michael@0 | 427 | let entries = []; |
michael@0 | 428 | while (entry = this._getEntry(stmt)) { |
michael@0 | 429 | entries.push(entry); |
michael@0 | 430 | } |
michael@0 | 431 | |
michael@0 | 432 | stmt.finalize(); |
michael@0 | 433 | |
michael@0 | 434 | return entries; |
michael@0 | 435 | }, |
michael@0 | 436 | |
michael@0 | 437 | _getElementForDownload: function dl_getElementForDownload(aKey) { |
michael@0 | 438 | return document.body.querySelector("li[downloadGUID='" + aKey + "']"); |
michael@0 | 439 | }, |
michael@0 | 440 | |
michael@0 | 441 | _getDownloadForElement: function dl_getDownloadForElement(aElement, aCallback) { |
michael@0 | 442 | let guid = aElement.getAttribute("downloadGUID"); |
michael@0 | 443 | this._dlmgr.getDownloadByGUID(guid, function(status, download) { |
michael@0 | 444 | if (!Components.isSuccessCode(status)) { |
michael@0 | 445 | return; |
michael@0 | 446 | } |
michael@0 | 447 | aCallback(download); |
michael@0 | 448 | }); |
michael@0 | 449 | }, |
michael@0 | 450 | |
michael@0 | 451 | _removeItem: function dl_removeItem(aItem) { |
michael@0 | 452 | // Make sure we have an item to remove |
michael@0 | 453 | if (!aItem) |
michael@0 | 454 | return; |
michael@0 | 455 | |
michael@0 | 456 | aItem.parentNode.removeChild(aItem); |
michael@0 | 457 | }, |
michael@0 | 458 | |
michael@0 | 459 | openDownload: function dl_openDownload(aItem) { |
michael@0 | 460 | this._getDownloadForElement(aItem, function(aDownload) { |
michael@0 | 461 | if (aDownload.state !== Ci.nsIDownloadManager.DOWNLOAD_FINISHED) { |
michael@0 | 462 | // Do not open unfinished downloads. |
michael@0 | 463 | return; |
michael@0 | 464 | } |
michael@0 | 465 | try { |
michael@0 | 466 | let f = aDownload.targetFile; |
michael@0 | 467 | if (f) f.launch(); |
michael@0 | 468 | } catch (ex) { |
michael@0 | 469 | this.logError("openDownload() " + ex, aDownload); |
michael@0 | 470 | } |
michael@0 | 471 | }.bind(this)); |
michael@0 | 472 | }, |
michael@0 | 473 | |
michael@0 | 474 | removeDownload: function dl_removeDownload(aItem) { |
michael@0 | 475 | this._getDownloadForElement(aItem, function(aDownload) { |
michael@0 | 476 | if (aDownload.targetFile) { |
michael@0 | 477 | OS.File.remove(aDownload.targetFile.path).then(null, function onError(reason) { |
michael@0 | 478 | if (!(reason instanceof OS.File.Error && reason.becauseNoSuchFile)) { |
michael@0 | 479 | this.logError("removeDownload() " + reason, aDownload); |
michael@0 | 480 | } |
michael@0 | 481 | }.bind(this)); |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | aDownload.remove(); |
michael@0 | 485 | }.bind(this)); |
michael@0 | 486 | }, |
michael@0 | 487 | |
michael@0 | 488 | removeAll: function dl_removeAll() { |
michael@0 | 489 | let title = gStrings.GetStringFromName("downloadAction.deleteAll"); |
michael@0 | 490 | let messageForm = gStrings.GetStringFromName("downloadMessage.deleteAll"); |
michael@0 | 491 | let elements = document.body.querySelectorAll("li[state='" + this._dlmgr.DOWNLOAD_FINISHED + "']," + |
michael@0 | 492 | "li[state='" + this._dlmgr.DOWNLOAD_CANCELED + "']," + |
michael@0 | 493 | "li[state='" + this._dlmgr.DOWNLOAD_FAILED + "']"); |
michael@0 | 494 | let message = PluralForm.get(elements.length, messageForm) |
michael@0 | 495 | .replace("#1", elements.length); |
michael@0 | 496 | let flags = Services.prompt.BUTTON_POS_0 * Services.prompt.BUTTON_TITLE_OK + |
michael@0 | 497 | Services.prompt.BUTTON_POS_1 * Services.prompt.BUTTON_TITLE_CANCEL; |
michael@0 | 498 | let choice = Services.prompt.confirmEx(null, title, message, flags, |
michael@0 | 499 | null, null, null, null, {}); |
michael@0 | 500 | if (choice == 0) { |
michael@0 | 501 | for (let i = 0; i < elements.length; i++) { |
michael@0 | 502 | this.removeDownload(elements[i]); |
michael@0 | 503 | } |
michael@0 | 504 | } |
michael@0 | 505 | }, |
michael@0 | 506 | |
michael@0 | 507 | pauseDownload: function dl_pauseDownload(aItem) { |
michael@0 | 508 | this._getDownloadForElement(aItem, function(aDownload) { |
michael@0 | 509 | try { |
michael@0 | 510 | aDownload.pause(); |
michael@0 | 511 | this._updateDownloadRow(aItem, aDownload); |
michael@0 | 512 | } catch (ex) { |
michael@0 | 513 | this.logError("Error: pauseDownload() " + ex, aDownload); |
michael@0 | 514 | } |
michael@0 | 515 | }.bind(this)); |
michael@0 | 516 | }, |
michael@0 | 517 | |
michael@0 | 518 | resumeDownload: function dl_resumeDownload(aItem) { |
michael@0 | 519 | this._getDownloadForElement(aItem, function(aDownload) { |
michael@0 | 520 | try { |
michael@0 | 521 | aDownload.resume(); |
michael@0 | 522 | this._updateDownloadRow(aItem, aDownload); |
michael@0 | 523 | } catch (ex) { |
michael@0 | 524 | this.logError("resumeDownload() " + ex, aDownload); |
michael@0 | 525 | } |
michael@0 | 526 | }.bind(this)); |
michael@0 | 527 | }, |
michael@0 | 528 | |
michael@0 | 529 | retryDownload: function dl_retryDownload(aItem) { |
michael@0 | 530 | this._getDownloadForElement(aItem, function(aDownload) { |
michael@0 | 531 | try { |
michael@0 | 532 | this._removeItem(aItem); |
michael@0 | 533 | aDownload.retry(); |
michael@0 | 534 | } catch (ex) { |
michael@0 | 535 | this.logError("retryDownload() " + ex, aDownload); |
michael@0 | 536 | } |
michael@0 | 537 | }.bind(this)); |
michael@0 | 538 | }, |
michael@0 | 539 | |
michael@0 | 540 | cancelDownload: function dl_cancelDownload(aItem) { |
michael@0 | 541 | this._getDownloadForElement(aItem, function(aDownload) { |
michael@0 | 542 | OS.File.remove(aDownload.targetFile.path).then(null, function onError(reason) { |
michael@0 | 543 | if (!(reason instanceof OS.File.Error && reason.becauseNoSuchFile)) { |
michael@0 | 544 | this.logError("cancelDownload() " + reason, aDownload); |
michael@0 | 545 | } |
michael@0 | 546 | }.bind(this)); |
michael@0 | 547 | |
michael@0 | 548 | aDownload.cancel(); |
michael@0 | 549 | |
michael@0 | 550 | this._updateDownloadRow(aItem, aDownload); |
michael@0 | 551 | }.bind(this)); |
michael@0 | 552 | }, |
michael@0 | 553 | |
michael@0 | 554 | _updateDownloadRow: function dl_updateDownloadRow(aItem, aDownload) { |
michael@0 | 555 | try { |
michael@0 | 556 | let updatedState = this._getState(aDownload.state); |
michael@0 | 557 | aItem.setAttribute("state", updatedState); |
michael@0 | 558 | this._updateItem(aItem, { |
michael@0 | 559 | size: this._getDownloadSize(aDownload.size), |
michael@0 | 560 | displayState: this._getStateString(updatedState), |
michael@0 | 561 | date: DownloadUtils.getReadableDates(new Date())[0] |
michael@0 | 562 | }); |
michael@0 | 563 | } catch (ex) { |
michael@0 | 564 | this.logError("_updateDownloadRow() " + ex, aDownload); |
michael@0 | 565 | } |
michael@0 | 566 | }, |
michael@0 | 567 | |
michael@0 | 568 | /** |
michael@0 | 569 | * In case a specific downloadId was passed while opening, scrolls the list to |
michael@0 | 570 | * the given elemenet |
michael@0 | 571 | */ |
michael@0 | 572 | |
michael@0 | 573 | _scrollToSelectedDownload : function dl_scrollToSelected() { |
michael@0 | 574 | let spec = document.location.href; |
michael@0 | 575 | let pos = spec.indexOf("?"); |
michael@0 | 576 | let query = ""; |
michael@0 | 577 | if (pos >= 0) |
michael@0 | 578 | query = spec.substring(pos + 1); |
michael@0 | 579 | |
michael@0 | 580 | // Just assume the query is "id=<id>" |
michael@0 | 581 | let id = query.substring(3); |
michael@0 | 582 | if (!id) { |
michael@0 | 583 | return; |
michael@0 | 584 | } |
michael@0 | 585 | downloadElement = this._getElementForDownload(id); |
michael@0 | 586 | if (!downloadElement) { |
michael@0 | 587 | return; |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | downloadElement.scrollIntoView(); |
michael@0 | 591 | }, |
michael@0 | 592 | |
michael@0 | 593 | /** |
michael@0 | 594 | * Logs the error to the console. |
michael@0 | 595 | * |
michael@0 | 596 | * @param aMessage error message to log |
michael@0 | 597 | * @param aDownload (optional) if given, and if the download is private, the |
michael@0 | 598 | * log message is suppressed |
michael@0 | 599 | */ |
michael@0 | 600 | logError: function dl_logError(aMessage, aDownload) { |
michael@0 | 601 | if (!aDownload || !aDownload.isPrivate) { |
michael@0 | 602 | console.log("Error: " + aMessage); |
michael@0 | 603 | } |
michael@0 | 604 | }, |
michael@0 | 605 | |
michael@0 | 606 | QueryInterface: function (aIID) { |
michael@0 | 607 | if (!aIID.equals(Ci.nsIDownloadProgressListener) && |
michael@0 | 608 | !aIID.equals(Ci.nsISupports)) |
michael@0 | 609 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 610 | return this; |
michael@0 | 611 | } |
michael@0 | 612 | } |
michael@0 | 613 | |
michael@0 | 614 | document.addEventListener("DOMContentLoaded", Downloads.init.bind(Downloads), true); |
michael@0 | 615 | window.addEventListener("unload", Downloads.uninit.bind(Downloads), false); |
michael@0 | 616 | |
michael@0 | 617 |