Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | |
michael@0 | 11 | this.EXPORTED_SYMBOLS = ["DownloadsIPC"]; |
michael@0 | 12 | |
michael@0 | 13 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 14 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 15 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 16 | |
michael@0 | 17 | XPCOMUtils.defineLazyServiceGetter(this, "cpmm", |
michael@0 | 18 | "@mozilla.org/childprocessmessagemanager;1", |
michael@0 | 19 | "nsIMessageSender"); |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * This module lives in the child process and receives the ipc messages |
michael@0 | 23 | * from the parent. It saves the download's state and redispatch changes |
michael@0 | 24 | * to DOM objects using an observer notification. |
michael@0 | 25 | * |
michael@0 | 26 | * This module needs to be loaded once and only once per process. |
michael@0 | 27 | */ |
michael@0 | 28 | |
michael@0 | 29 | function debug(aStr) { |
michael@0 | 30 | #ifdef MOZ_DEBUG |
michael@0 | 31 | dump("-*- DownloadsIPC.jsm : " + aStr + "\n"); |
michael@0 | 32 | #endif |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | const ipcMessages = ["Downloads:Added", |
michael@0 | 36 | "Downloads:Removed", |
michael@0 | 37 | "Downloads:Changed", |
michael@0 | 38 | "Downloads:GetList:Return", |
michael@0 | 39 | "Downloads:ClearAllDone:Return", |
michael@0 | 40 | "Downloads:Remove:Return", |
michael@0 | 41 | "Downloads:Pause:Return", |
michael@0 | 42 | "Downloads:Resume:Return"]; |
michael@0 | 43 | |
michael@0 | 44 | this.DownloadsIPC = { |
michael@0 | 45 | downloads: {}, |
michael@0 | 46 | |
michael@0 | 47 | init: function() { |
michael@0 | 48 | debug("init"); |
michael@0 | 49 | Services.obs.addObserver(this, "xpcom-shutdown", false); |
michael@0 | 50 | ipcMessages.forEach((aMessage) => { |
michael@0 | 51 | cpmm.addMessageListener(aMessage, this); |
michael@0 | 52 | }); |
michael@0 | 53 | |
michael@0 | 54 | // We need to get the list of current downloads. |
michael@0 | 55 | this.ready = false; |
michael@0 | 56 | this.getListPromises = []; |
michael@0 | 57 | this.clearAllPromises = []; |
michael@0 | 58 | this.downloadPromises = {}; |
michael@0 | 59 | cpmm.sendAsyncMessage("Downloads:GetList", {}); |
michael@0 | 60 | this._promiseId = 0; |
michael@0 | 61 | }, |
michael@0 | 62 | |
michael@0 | 63 | notifyChanges: function(aId) { |
michael@0 | 64 | // TODO: use the subject instead of stringifying. |
michael@0 | 65 | if (this.downloads[aId]) { |
michael@0 | 66 | debug("notifyChanges notifying changes for " + aId); |
michael@0 | 67 | Services.obs.notifyObservers(null, "downloads-state-change-" + aId, |
michael@0 | 68 | JSON.stringify(this.downloads[aId])); |
michael@0 | 69 | } else { |
michael@0 | 70 | debug("notifyChanges failed for " + aId) |
michael@0 | 71 | } |
michael@0 | 72 | }, |
michael@0 | 73 | |
michael@0 | 74 | _updateDownloadsArray: function(aDownloads) { |
michael@0 | 75 | this.downloads = []; |
michael@0 | 76 | // We actually have an array of downloads. |
michael@0 | 77 | aDownloads.forEach((aDownload) => { |
michael@0 | 78 | this.downloads[aDownload.id] = aDownload; |
michael@0 | 79 | }); |
michael@0 | 80 | }, |
michael@0 | 81 | |
michael@0 | 82 | receiveMessage: function(aMessage) { |
michael@0 | 83 | let download = aMessage.data; |
michael@0 | 84 | debug("message: " + aMessage.name); |
michael@0 | 85 | switch(aMessage.name) { |
michael@0 | 86 | case "Downloads:GetList:Return": |
michael@0 | 87 | this._updateDownloadsArray(download); |
michael@0 | 88 | |
michael@0 | 89 | if (!this.ready) { |
michael@0 | 90 | this.getListPromises.forEach(aPromise => |
michael@0 | 91 | aPromise.resolve(this.downloads)); |
michael@0 | 92 | this.getListPromises.length = 0; |
michael@0 | 93 | } |
michael@0 | 94 | this.ready = true; |
michael@0 | 95 | break; |
michael@0 | 96 | case "Downloads:ClearAllDone:Return": |
michael@0 | 97 | this._updateDownloadsArray(download); |
michael@0 | 98 | this.clearAllPromises.forEach(aPromise => |
michael@0 | 99 | aPromise.resolve(this.downloads)); |
michael@0 | 100 | this.clearAllPromises.length = 0; |
michael@0 | 101 | break; |
michael@0 | 102 | case "Downloads:Added": |
michael@0 | 103 | this.downloads[download.id] = download; |
michael@0 | 104 | this.notifyChanges(download.id); |
michael@0 | 105 | break; |
michael@0 | 106 | case "Downloads:Removed": |
michael@0 | 107 | if (this.downloads[download.id]) { |
michael@0 | 108 | this.downloads[download.id] = download; |
michael@0 | 109 | this.notifyChanges(download.id); |
michael@0 | 110 | delete this.downloads[download.id]; |
michael@0 | 111 | } |
michael@0 | 112 | break; |
michael@0 | 113 | case "Downloads:Changed": |
michael@0 | 114 | // Only update properties that actually changed. |
michael@0 | 115 | let cached = this.downloads[download.id]; |
michael@0 | 116 | if (!cached) { |
michael@0 | 117 | debug("No download found for " + download.id); |
michael@0 | 118 | return; |
michael@0 | 119 | } |
michael@0 | 120 | let props = ["totalBytes", "currentBytes", "url", "path", "state", |
michael@0 | 121 | "contentType", "startTime"]; |
michael@0 | 122 | let changed = false; |
michael@0 | 123 | |
michael@0 | 124 | props.forEach((aProp) => { |
michael@0 | 125 | if (download[aProp] && (download[aProp] != cached[aProp])) { |
michael@0 | 126 | cached[aProp] = download[aProp]; |
michael@0 | 127 | changed = true; |
michael@0 | 128 | } |
michael@0 | 129 | }); |
michael@0 | 130 | |
michael@0 | 131 | // Updating the error property. We always get a 'state' change as |
michael@0 | 132 | // well. |
michael@0 | 133 | cached.error = download.error; |
michael@0 | 134 | |
michael@0 | 135 | if (changed) { |
michael@0 | 136 | this.notifyChanges(download.id); |
michael@0 | 137 | } |
michael@0 | 138 | break; |
michael@0 | 139 | case "Downloads:Remove:Return": |
michael@0 | 140 | case "Downloads:Pause:Return": |
michael@0 | 141 | case "Downloads:Resume:Return": |
michael@0 | 142 | if (this.downloadPromises[download.promiseId]) { |
michael@0 | 143 | if (!download.error) { |
michael@0 | 144 | this.downloadPromises[download.promiseId].resolve(download); |
michael@0 | 145 | } else { |
michael@0 | 146 | this.downloadPromises[download.promiseId].reject(download); |
michael@0 | 147 | } |
michael@0 | 148 | delete this.downloadPromises[download.promiseId]; |
michael@0 | 149 | } |
michael@0 | 150 | break; |
michael@0 | 151 | } |
michael@0 | 152 | }, |
michael@0 | 153 | |
michael@0 | 154 | /** |
michael@0 | 155 | * Returns a promise that is resolved with the list of current downloads. |
michael@0 | 156 | */ |
michael@0 | 157 | getDownloads: function() { |
michael@0 | 158 | debug("getDownloads()"); |
michael@0 | 159 | let deferred = Promise.defer(); |
michael@0 | 160 | if (this.ready) { |
michael@0 | 161 | debug("Returning existing list."); |
michael@0 | 162 | deferred.resolve(this.downloads); |
michael@0 | 163 | } else { |
michael@0 | 164 | this.getListPromises.push(deferred); |
michael@0 | 165 | } |
michael@0 | 166 | return deferred.promise; |
michael@0 | 167 | }, |
michael@0 | 168 | |
michael@0 | 169 | /** |
michael@0 | 170 | * Returns a promise that is resolved with the list of current downloads. |
michael@0 | 171 | */ |
michael@0 | 172 | clearAllDone: function() { |
michael@0 | 173 | debug("clearAllDone"); |
michael@0 | 174 | let deferred = Promise.defer(); |
michael@0 | 175 | this.clearAllPromises.push(deferred); |
michael@0 | 176 | cpmm.sendAsyncMessage("Downloads:ClearAllDone", {}); |
michael@0 | 177 | return deferred.promise; |
michael@0 | 178 | }, |
michael@0 | 179 | |
michael@0 | 180 | promiseId: function() { |
michael@0 | 181 | return this._promiseId++; |
michael@0 | 182 | }, |
michael@0 | 183 | |
michael@0 | 184 | remove: function(aId) { |
michael@0 | 185 | debug("remove " + aId); |
michael@0 | 186 | let deferred = Promise.defer(); |
michael@0 | 187 | let pId = this.promiseId(); |
michael@0 | 188 | this.downloadPromises[pId] = deferred; |
michael@0 | 189 | cpmm.sendAsyncMessage("Downloads:Remove", |
michael@0 | 190 | { id: aId, promiseId: pId }); |
michael@0 | 191 | return deferred.promise; |
michael@0 | 192 | }, |
michael@0 | 193 | |
michael@0 | 194 | pause: function(aId) { |
michael@0 | 195 | debug("pause " + aId); |
michael@0 | 196 | let deferred = Promise.defer(); |
michael@0 | 197 | let pId = this.promiseId(); |
michael@0 | 198 | this.downloadPromises[pId] = deferred; |
michael@0 | 199 | cpmm.sendAsyncMessage("Downloads:Pause", |
michael@0 | 200 | { id: aId, promiseId: pId }); |
michael@0 | 201 | return deferred.promise; |
michael@0 | 202 | }, |
michael@0 | 203 | |
michael@0 | 204 | resume: function(aId) { |
michael@0 | 205 | debug("resume " + aId); |
michael@0 | 206 | let deferred = Promise.defer(); |
michael@0 | 207 | let pId = this.promiseId(); |
michael@0 | 208 | this.downloadPromises[pId] = deferred; |
michael@0 | 209 | cpmm.sendAsyncMessage("Downloads:Resume", |
michael@0 | 210 | { id: aId, promiseId: pId }); |
michael@0 | 211 | return deferred.promise; |
michael@0 | 212 | }, |
michael@0 | 213 | |
michael@0 | 214 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 215 | if (aTopic == "xpcom-shutdown") { |
michael@0 | 216 | ipcMessages.forEach((aMessage) => { |
michael@0 | 217 | cpmm.removeMessageListener(aMessage, this); |
michael@0 | 218 | }); |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | }; |
michael@0 | 222 | |
michael@0 | 223 | DownloadsIPC.init(); |