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
|
/* 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/. */
var {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
const {DownloadUtils} = ChromeUtils.import("resource://gre/modules/DownloadUtils.jsm");
const kInterval = 750; // Default to .75 seconds.
var gPersist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
.createInstance(Ci.nsIWebBrowserPersist);
var gSource = window.arguments[0].QueryInterface(Ci.nsIFileURL);
var gTarget = window.arguments[1].QueryInterface(Ci.nsIURL);
var gFileName = gSource.file.leafName;
var gFileSize = gSource.file.fileSize;
var gPercent = -1;
var gStartTime;
var gLastUpdate;
var gLastSeconds;
var gBundle;
var gStatus;
var gTime;
var gSize;
var gProgress;
var gMeter;
function onLoad()
{
gBundle = document.getElementById("dmBundle");
gStatus = document.getElementById("status");
gTime = document.getElementById("timeElapsed");
gSize = document.getElementById("size");
gProgress = document.getElementById("progressText");
gMeter = document.getElementById("progress");
var status = gBundle.getString("stateNotStarted");
document.title =
gBundle.getFormattedString("progressTitle", [gFileName, status]);
gStatus.value = status;
gTime.value = gBundle.getFormattedString("timeSingle",
DownloadUtils.convertTimeUnits(0));
gSize.value = DownloadUtils.getTransferTotal(0, gFileSize);
document.getElementById("target").value =
gBundle.getFormattedString("toTarget", [gTarget.resolve(".")]);
document.getElementById("source").value =
gBundle.getFormattedString("fromSource", [gSource.file.leafName]);
gPersist.progressListener = gProgressListener;
gPersist.saveURI(gSource, null, null, 0, null, null, gTarget, null);
document.documentElement.getButton("cancel").focus();
}
function onUnload()
{
if (gPersist)
gPersist.cancel(Cr.NS_BINDING_ABORTED);
gPersist = null;
}
function setPercent(aPercent, aStatus)
{
gPercent = aPercent;
document.title = gBundle.getFormattedString("progressTitlePercent",
[aPercent, gFileName, aStatus]);
gProgress.value = gBundle.getFormattedString("percentFormat", [aPercent]);
gMeter.mode = "normal";
gMeter.value = aPercent;
}
var gProgressListener = {
// ----- nsIWebProgressListener methods -----
// Look for STATE_STOP and close dialog to indicate completion when it happens.
onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
if (aRequest instanceof Ci.nsIChannel &&
aRequest.URI.equals(gTarget) &&
aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
gPersist = null;
var status = gBundle.getString("stateCompleted");
setPercent(100, status);
gStatus.value = status;
gSize.value = DownloadUtils.getTransferTotal(gFileSize, gFileSize);
setTimeout(window.close, kInterval);
}
},
// Handle progress notifications.
onProgressChange: function(aWebProgress, aRequest,
aCurSelfProgress, aMaxSelfProgress,
aCurTotalProgress, aMaxTotalProgress) {
return this.onProgressChange64(aWebProgress, aRequest,
aCurSelfProgress, aMaxSelfProgress,
aCurTotalProgress, aMaxTotalProgress);
},
onProgressChange64: function(aWebProgress, aRequest,
aCurSelfProgress, aMaxSelfProgress,
aCurTotalProgress, aMaxTotalProgress) {
if (aRequest instanceof Ci.nsIChannel &&
aRequest.URI.equals(gTarget)) {
// Get current time.
var now = Date.now();
// If interval hasn't elapsed, ignore it.
if (!gStartTime)
gStartTime = now;
else if (now - gLastUpdate < kInterval && aCurTotalProgress < gFileSize)
return;
// Update this time.
gLastUpdate = now;
// Update elapsed time.
var elapsed = (now - gStartTime) / 1000;
// Calculate percentage.
var status = gBundle.getString("stateUploading");
var percent = -1;
if (gFileSize > 0)
percent = Math.floor(aCurTotalProgress * 100 / gFileSize);
if (percent != gPercent)
setPercent(percent, status);
// Update time remaining.
var rate = elapsed && aCurTotalProgress / elapsed;
if (rate && gFileSize) {
var timeLeft;
[timeLeft, gLastSeconds] =
DownloadUtils.getTimeLeft((gFileSize - aCurTotalProgress) / rate,
gLastSeconds);
status = gBundle.getFormattedString("statusActive", [status, timeLeft]);
}
gStatus.value = status;
// Update dialog's display of elapsed time.
var timeUnits = DownloadUtils.convertTimeUnits(elapsed);
var timeString = timeUnits[2] ? "timeDouble" : "timeSingle";
gTime.value = gBundle.getFormattedString(timeString, timeUnits);
// Update size (nn KB of mm KB at xx.x KB/sec)
var size = DownloadUtils.getTransferTotal(aCurTotalProgress, gFileSize);
if (elapsed)
size = gBundle.getFormattedString("sizeSpeed", [size,
gBundle.getFormattedString("speedFormat",
DownloadUtils.convertByteUnits(rate))]);
gSize.value = size;
}
},
// Look for error notifications and display alert to user.
onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {
// Check for error condition (only if dialog is still open).
if (!Cr.isSuccessCode(aStatus)) {
// Display error alert (using text supplied by back-end).
Services.prompt.alert(window, document.title, aMessage);
// Close the dialog.
window.close();
}
},
// Ignore onLocationChange and onSecurityChange notifications.
onLocationChange: function( aWebProgress, aRequest, aLocation, aFlags ) {
},
onSecurityChange: function( aWebProgress, aRequest, aState ) {
},
// ---------- nsISupports methods ----------
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIWebProgressListener2,
Ci.nsIWebProgressListener,
Ci.nsIInterfaceRequestor]),
// ---------- nsIInterfaceRequestor methods ----------
getInterface: function(aIID) {
if (aIID.equals(Ci.nsIPrompt) ||
aIID.equals(Ci.nsIAuthPrompt)) {
var prompt;
if (aIID.equals(Ci.nsIPrompt))
prompt = Services.ww.getNewPrompter(window);
else
prompt = Services.ww.getNewAuthPrompter(window);
return prompt;
}
throw Cr.NS_ERROR_NO_INTERFACE;
}
}
|