2014-01-09 10:18:55 -08:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
importScripts("resource://gre/modules/osfile.jsm");
|
2014-05-09 13:15:54 -07:00
|
|
|
importScripts("resource://gre/modules/workers/require.js");
|
|
|
|
let Log = require("resource://gre/modules/AndroidLog.jsm");
|
2014-01-09 10:18:55 -08:00
|
|
|
|
2014-05-09 13:15:54 -07:00
|
|
|
// Define the "log" function as a binding of the Log.d function so it specifies
|
|
|
|
// the "debug" priority and a log tag.
|
|
|
|
let log = Log.d.bind(null, "WebappManagerWorker");
|
2014-01-09 10:18:55 -08:00
|
|
|
|
|
|
|
onmessage = function(event) {
|
|
|
|
let { url, path } = event.data;
|
|
|
|
|
|
|
|
let file = OS.File.open(path, { truncate: true });
|
|
|
|
let request = new XMLHttpRequest({ mozSystem: true });
|
|
|
|
|
|
|
|
request.open("GET", url, true);
|
|
|
|
request.responseType = "moz-chunked-arraybuffer";
|
|
|
|
|
|
|
|
request.onprogress = function(event) {
|
|
|
|
log("onprogress: received " + request.response.byteLength + " bytes");
|
|
|
|
let bytesWritten = file.write(new Uint8Array(request.response));
|
|
|
|
log("onprogress: wrote " + bytesWritten + " bytes");
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onreadystatechange = function(event) {
|
|
|
|
log("onreadystatechange: " + request.readyState);
|
|
|
|
|
2014-02-07 23:50:13 -08:00
|
|
|
if (request.readyState !== 4) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
2014-01-09 10:18:55 -08:00
|
|
|
|
2014-02-07 23:50:13 -08:00
|
|
|
if (request.status === 200) {
|
|
|
|
postMessage({ type: "success" });
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
OS.File.remove(path);
|
|
|
|
} catch(ex) {
|
|
|
|
log("error removing " + path + ": " + ex);
|
2014-01-09 10:18:55 -08:00
|
|
|
}
|
2014-02-07 23:50:13 -08:00
|
|
|
let statusMessage = request.status + " - " + request.statusText;
|
|
|
|
postMessage({ type: "failure", message: statusMessage });
|
2014-01-09 10:18:55 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
request.send(null);
|
|
|
|
}
|