Make the JARDownloader pipe server send 'fail' and 'progress' messages

This commit is contained in:
Marco Castelluccio 2014-12-11 20:41:52 +01:00
Родитель 4322b13b33
Коммит 37c186cbdc
1 изменённых файлов: 61 добавлений и 35 удалений

Просмотреть файл

@ -475,47 +475,73 @@ DumbPipe.registerOpener("notification", function(message, sender) {
}
});
function load(file, responseType) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest({ mozSystem: true });
xhr.open("GET", file, true);
xhr.responseType = responseType;
xhr.onload = function () {
resolve(xhr.response);
};
xhr.onerror = function() {
reject();
};
xhr.send(null);
});
function load(file, responseType, successCb, failureCb, progressCb, length) {
var xhr = new XMLHttpRequest({ mozSystem: true });
xhr.open("GET", file, true);
xhr.responseType = responseType;
if (progressCb) {
xhr.onprogress = function(e) {
if (e.lengthComputable) {
progressCb(e.loaded / e.total * 100);
} else if (length) {
progressCb(e.loaded / length * 100);
}
}
}
xhr.onload = function() {
if (xhr.status === 200) {
successCb(xhr.response);
} else {
failureCb();
}
};
xhr.onerror = function() {
failureCb();
};
xhr.send(null);
}
DumbPipe.registerOpener("JARDownloader", function(message, sender) {
load(urlParams.downloadJAD, "text").then(function(data) {
var manifest = {};
load(urlParams.downloadJAD, "text", function(data) {
try {
var manifest = {};
data
.replace(/\r\n|\r/g, "\n")
.replace(/\n /g, "")
.split("\n")
.forEach(function(entry) {
if (entry) {
var keyEnd = entry.indexOf(":");
var key = entry.substring(0, keyEnd);
var val = entry.substring(keyEnd + 1).trim();
manifest[key] = val;
data
.replace(/\r\n|\r/g, "\n")
.replace(/\n /g, "")
.split("\n")
.forEach(function(entry) {
if (entry) {
var keyEnd = entry.indexOf(":");
var key = entry.substring(0, keyEnd);
var val = entry.substring(keyEnd + 1).trim();
manifest[key] = val;
}
});
var jarURL = manifest["MIDlet-Jar-URL"];
if (!jarURL.startsWith("http")) {
var jarName = jarURL.substring(jarURL.lastIndexOf("/") + 1);
jarURL = urlParams.downloadJAD.substring(0, urlParams.downloadJAD.lastIndexOf("/") + 1) + jarName;
}
});
var jarURL = manifest["MIDlet-Jar-URL"];
if (!jarURL.startsWith("http")) {
var jarName = jarURL.substring(jarURL.lastIndexOf("/") + 1);
jarURL = urlParams.downloadJAD.substring(0, urlParams.downloadJAD.lastIndexOf("/") + 1) + jarName;
load(jarURL, "arraybuffer", function(data) {
sender({ type: "done", data: data });
}, function() {
sender({ type: "fail" })
}, function(progress) {
sender({ type: "progress", progress: progress });
}, manifest["MIDlet-Jar-Size"] || 0);
} catch (e) {
sender({ type: "fail" });
}
load(jarURL, "arraybuffer").then(function(data) {
sender({ data: data });
});
}, function() {
sender({ type: "fail" })
});
});