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