mobile/android/modules/WebappManagerWorker.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

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 file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 importScripts("resource://gre/modules/osfile.jsm");
michael@0 6
michael@0 7 function log(message) {
michael@0 8 dump("WebManagerWorker " + message + "\n");
michael@0 9 }
michael@0 10
michael@0 11 onmessage = function(event) {
michael@0 12 let { url, path } = event.data;
michael@0 13
michael@0 14 let file = OS.File.open(path, { truncate: true });
michael@0 15 let request = new XMLHttpRequest({ mozSystem: true });
michael@0 16
michael@0 17 request.open("GET", url, true);
michael@0 18 request.responseType = "moz-chunked-arraybuffer";
michael@0 19
michael@0 20 request.onprogress = function(event) {
michael@0 21 log("onprogress: received " + request.response.byteLength + " bytes");
michael@0 22 let bytesWritten = file.write(new Uint8Array(request.response));
michael@0 23 log("onprogress: wrote " + bytesWritten + " bytes");
michael@0 24 };
michael@0 25
michael@0 26 request.onreadystatechange = function(event) {
michael@0 27 log("onreadystatechange: " + request.readyState);
michael@0 28
michael@0 29 if (request.readyState !== 4) {
michael@0 30 return;
michael@0 31 }
michael@0 32
michael@0 33 file.close();
michael@0 34
michael@0 35 if (request.status === 200) {
michael@0 36 postMessage({ type: "success" });
michael@0 37 } else {
michael@0 38 try {
michael@0 39 OS.File.remove(path);
michael@0 40 } catch(ex) {
michael@0 41 log("error removing " + path + ": " + ex);
michael@0 42 }
michael@0 43 let statusMessage = request.status + " - " + request.statusText;
michael@0 44 postMessage({ type: "failure", message: statusMessage });
michael@0 45 }
michael@0 46 };
michael@0 47
michael@0 48 request.send(null);
michael@0 49 }

mercurial