зеркало из https://github.com/mozilla/gecko-dev.git
Bug 326840 Make progress dialogs display download speed where available p=son.le0@gmail.com r=biesi sr=me
This commit is contained in:
Родитель
53cbfd182b
Коммит
6a6d38fbe5
|
@ -53,8 +53,8 @@ interface nsIObserver;
|
|||
* 2. Set appropriate attributes that control the display and behavior
|
||||
* of the dialog.
|
||||
* 3. Open the dialog.
|
||||
* 4. Send progress notifications to the dialog, via it's
|
||||
* nsIWebProgressListener methods.
|
||||
* 4. Send progress notifications to the dialog, via its
|
||||
* nsIDownloadProgressListener methods.
|
||||
* 5. Close the dialog when the operation completes, or when the user
|
||||
* closes it manually.
|
||||
* 6. Release the instance. The instance will be referenced by
|
||||
|
|
|
@ -181,10 +181,14 @@ nsProgressDialog.prototype = {
|
|||
this.operation = aOperation;
|
||||
},
|
||||
|
||||
// ---------- nsIWebProgressListener methods ----------
|
||||
// ----- nsIDownloadProgressListener/nsIWebProgressListener methods -----
|
||||
// Take advantage of javascript's function overloading feature to combine
|
||||
// similiar nsIDownloadProgressListener and nsIWebProgressListener methods
|
||||
// in one. For nsIWebProgressListener calls, the aDownload paramater will
|
||||
// always be undefined.
|
||||
|
||||
// Look for STATE_STOP and update dialog to indicate completion when it happens.
|
||||
onStateChange: function( aWebProgress, aRequest, aStateFlags, aStatus ) {
|
||||
onStateChange: function( aWebProgress, aRequest, aStateFlags, aStatus, aDownload ) {
|
||||
if ( aStateFlags & nsIWebProgressListener.STATE_STOP ) {
|
||||
// if we are downloading, then just wait for the first STATE_STOP
|
||||
if ( this.targetFile != null ) {
|
||||
|
@ -213,9 +217,10 @@ nsProgressDialog.prototype = {
|
|||
aCurSelfProgress,
|
||||
aMaxSelfProgress,
|
||||
aCurTotalProgress,
|
||||
aMaxTotalProgress ) {
|
||||
return onProgressChange64(aWebProgress, aRequest, aCurSelfProgress,
|
||||
aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
|
||||
aMaxTotalProgress,
|
||||
aDownload ) {
|
||||
return this.onProgressChange64(aWebProgress, aRequest, aCurSelfProgress,
|
||||
aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress, aDownload);
|
||||
},
|
||||
|
||||
onProgressChange64: function( aWebProgress,
|
||||
|
@ -223,16 +228,13 @@ nsProgressDialog.prototype = {
|
|||
aCurSelfProgress,
|
||||
aMaxSelfProgress,
|
||||
aCurTotalProgress,
|
||||
aMaxTotalProgress ) {
|
||||
var overallProgress = aCurTotalProgress;
|
||||
|
||||
aMaxTotalProgress,
|
||||
aDownload ) {
|
||||
// Get current time.
|
||||
var now = ( new Date() ).getTime();
|
||||
|
||||
// If interval hasn't elapsed, ignore it.
|
||||
if ( now - this.lastUpdate < this.interval &&
|
||||
aMaxTotalProgress != "-1" &&
|
||||
parseInt( aCurTotalProgress ) < parseInt( aMaxTotalProgress ) ) {
|
||||
if ( now - this.lastUpdate < this.interval ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -244,7 +246,7 @@ nsProgressDialog.prototype = {
|
|||
|
||||
// Calculate percentage.
|
||||
if ( aMaxTotalProgress > 0) {
|
||||
this.percent = Math.floor( ( overallProgress * 100.0 ) / aMaxTotalProgress );
|
||||
this.percent = Math.floor( ( aCurTotalProgress * 100.0 ) / aMaxTotalProgress );
|
||||
} else {
|
||||
this.percent = -1;
|
||||
}
|
||||
|
@ -258,11 +260,11 @@ nsProgressDialog.prototype = {
|
|||
this.setValue( "timeElapsed", this.formatSeconds( this.elapsed / 1000 ) );
|
||||
|
||||
// Now that we've set the progress and the time, update # bytes downloaded...
|
||||
// Update status (nnK of mmK bytes at xx.xK aCurTotalProgress/sec)
|
||||
// Update status (nn KB of mm KB at xx.x KB/sec)
|
||||
var status = this.getString( "progressMsg" );
|
||||
|
||||
// Insert 1 is the number of kilobytes downloaded so far.
|
||||
status = this.replaceInsert( status, 1, parseInt( overallProgress/1024 + .5 ) );
|
||||
status = this.replaceInsert( status, 1, parseInt( aCurTotalProgress/1024 + .5 ) );
|
||||
|
||||
// Insert 2 is the total number of kilobytes to be downloaded (if known).
|
||||
if ( aMaxTotalProgress != "-1" ) {
|
||||
|
@ -273,8 +275,14 @@ nsProgressDialog.prototype = {
|
|||
|
||||
// Insert 3 is the download rate.
|
||||
if ( this.elapsed ) {
|
||||
this.rate = ( aCurTotalProgress * 1000 ) / this.elapsed;
|
||||
status = this.replaceInsert( status, 3, this.rateToKRate( this.rate ) );
|
||||
// Use the download speed where available, otherwise calculate
|
||||
// rate using current progress and elapsed time.
|
||||
if ( aDownload ) {
|
||||
this.rate = aDownload.speed;
|
||||
} else {
|
||||
this.rate = ( aCurTotalProgress * 1000 ) / this.elapsed;
|
||||
}
|
||||
status = this.replaceInsert( status, 3, this.kRate.toFixed(1) );
|
||||
} else {
|
||||
// Rate not established, yet.
|
||||
status = this.replaceInsert( status, 3, "??.?" );
|
||||
|
@ -295,7 +303,7 @@ nsProgressDialog.prototype = {
|
|||
},
|
||||
|
||||
// Look for error notifications and display alert to user.
|
||||
onStatusChange: function( aWebProgress, aRequest, aStatus, aMessage ) {
|
||||
onStatusChange: function( aWebProgress, aRequest, aStatus, aMessage, aDownload ) {
|
||||
// Check for error condition (only if dialog is still open).
|
||||
if ( aStatus != Components.results.NS_OK ) {
|
||||
if ( this.loaded ) {
|
||||
|
@ -323,10 +331,10 @@ nsProgressDialog.prototype = {
|
|||
},
|
||||
|
||||
// Ignore onLocationChange and onSecurityChange notifications.
|
||||
onLocationChange: function( aWebProgress, aRequest, aLocation ) {
|
||||
onLocationChange: function( aWebProgress, aRequest, aLocation, aDownload ) {
|
||||
},
|
||||
|
||||
onSecurityChange: function( aWebProgress, aRequest, state ) {
|
||||
onSecurityChange: function( aWebProgress, aRequest, aState, aDownload ) {
|
||||
},
|
||||
|
||||
// ---------- nsIObserver methods ----------
|
||||
|
@ -372,6 +380,7 @@ nsProgressDialog.prototype = {
|
|||
iid.equals(Components.interfaces.nsITransfer) ||
|
||||
iid.equals(Components.interfaces.nsIWebProgressListener) ||
|
||||
iid.equals(Components.interfaces.nsIWebProgressListener2) ||
|
||||
iid.equals(Components.interfaces.nsIDownloadProgressListener) ||
|
||||
iid.equals(Components.interfaces.nsIObserver) ||
|
||||
iid.equals(Components.interfaces.nsIInterfaceRequestor) ||
|
||||
iid.equals(Components.interfaces.nsISupports))
|
||||
|
@ -466,7 +475,7 @@ nsProgressDialog.prototype = {
|
|||
|
||||
var now = ( new Date() ).getTime();
|
||||
|
||||
// Intialize the elapsed time.
|
||||
// Initialize the elapsed time.
|
||||
if ( !this.elapsed ) {
|
||||
this.elapsed = now - this.startTime;
|
||||
}
|
||||
|
@ -671,19 +680,8 @@ nsProgressDialog.prototype = {
|
|||
},
|
||||
|
||||
// Update download rate and dialog display.
|
||||
// Note that we don't want the displayed value to quiver
|
||||
// between essentially identical values (e.g., 99.9Kb and
|
||||
// 100.0Kb) so we only update if we see a big change.
|
||||
setRate: function( rate ) {
|
||||
if ( rate ) {
|
||||
// rate is bytes/sec
|
||||
var change = Math.abs( this.rate - rate );
|
||||
// Don't update too often!
|
||||
if ( change > this.rate / 10 ) {
|
||||
// Displayed rate changes.
|
||||
this.mRate = rate;
|
||||
}
|
||||
}
|
||||
this.mRate = rate;
|
||||
return this.mRate;
|
||||
},
|
||||
|
||||
|
@ -778,11 +776,6 @@ nsProgressDialog.prototype = {
|
|||
return this.mPaused = pausing;
|
||||
},
|
||||
|
||||
// Convert raw rate (bytes/sec) to Kbytes/sec (to nearest tenth).
|
||||
rateToKRate: function( rate ) {
|
||||
return ( rate / 1024 ).toFixed(1);
|
||||
},
|
||||
|
||||
// Format number of seconds in hh:mm:ss form.
|
||||
formatSeconds: function( secs ) {
|
||||
// Round the number of seconds to remove fractions.
|
||||
|
@ -835,7 +828,7 @@ nsProgressDialog.prototype = {
|
|||
this.dialogElement( id ).value = val;
|
||||
},
|
||||
|
||||
// Enable dialgo element.
|
||||
// Enable dialog element.
|
||||
enable: function( field ) {
|
||||
this.dialogElement( field ).removeAttribute( "disabled" );
|
||||
},
|
||||
|
|
|
@ -49,7 +49,6 @@
|
|||
#include "nsIDOMEventTarget.h"
|
||||
#include "nsRDFCID.h"
|
||||
#include "nsAppDirectoryServiceDefs.h"
|
||||
#include "nsIWebBrowserPersist.h"
|
||||
#include "nsIObserver.h"
|
||||
#include "nsIProgressDialog.h"
|
||||
#include "nsIWebBrowserPersist.h"
|
||||
|
@ -774,7 +773,7 @@ nsDownloadManager::OpenProgressDialogFor(nsIDownload* aDownload, nsIDOMWindow* a
|
|||
dialog->SetObserver(internalDownload);
|
||||
|
||||
// now set the listener so we forward notifications to the dialog
|
||||
nsCOMPtr<nsIWebProgressListener2> listener = do_QueryInterface(dialog);
|
||||
nsCOMPtr<nsIDownloadProgressListener> listener = do_QueryInterface(dialog);
|
||||
internalDownload->SetDialogListener(listener);
|
||||
|
||||
internalDownload->SetDialog(dialog);
|
||||
|
@ -1092,8 +1091,8 @@ nsDownload::OnProgressChange64(nsIWebProgress *aWebProgress,
|
|||
}
|
||||
|
||||
if (mDialogListener) {
|
||||
mDialogListener->OnProgressChange64(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress,
|
||||
aCurTotalProgress, aMaxTotalProgress);
|
||||
mDialogListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress,
|
||||
aCurTotalProgress, aMaxTotalProgress, this);
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
|
@ -1128,7 +1127,7 @@ nsDownload::OnLocationChange(nsIWebProgress *aWebProgress,
|
|||
}
|
||||
|
||||
if (mDialogListener)
|
||||
mDialogListener->OnLocationChange(aWebProgress, aRequest, aLocation);
|
||||
mDialogListener->OnLocationChange(aWebProgress, aRequest, aLocation, this);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -1154,7 +1153,7 @@ nsDownload::OnStatusChange(nsIWebProgress *aWebProgress,
|
|||
}
|
||||
|
||||
if (mDialogListener)
|
||||
mDialogListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
|
||||
mDialogListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage, this);
|
||||
else {
|
||||
// Need to display error alert ourselves, if an error occurred.
|
||||
if (NS_FAILED(aStatus)) {
|
||||
|
@ -1314,7 +1313,7 @@ nsDownload::OnStateChange(nsIWebProgress* aWebProgress,
|
|||
}
|
||||
|
||||
if (mDialogListener) {
|
||||
mDialogListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
|
||||
mDialogListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus, this);
|
||||
if (aStateFlags & STATE_STOP) {
|
||||
// Break this cycle, too
|
||||
mDialogListener = nsnull;
|
||||
|
@ -1336,7 +1335,7 @@ nsDownload::OnSecurityChange(nsIWebProgress *aWebProgress,
|
|||
}
|
||||
|
||||
if (mDialogListener)
|
||||
mDialogListener->OnSecurityChange(aWebProgress, aRequest, aState);
|
||||
mDialogListener->OnSecurityChange(aWebProgress, aRequest, aState, this);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
|
|
@ -49,7 +49,6 @@
|
|||
#include "nsIDOMDocument.h"
|
||||
#include "nsIDOMEventListener.h"
|
||||
#include "nsIRDFContainerUtils.h"
|
||||
#include "nsIWebProgressListener.h"
|
||||
#include "nsIURI.h"
|
||||
#include "nsILocalFile.h"
|
||||
#include "nsRefPtrHashtable.h"
|
||||
|
@ -128,7 +127,7 @@ public:
|
|||
nsresult Resume();
|
||||
void DisplayDownloadFinishedAlert();
|
||||
|
||||
void SetDialogListener(nsIWebProgressListener2* aInternalListener) {
|
||||
void SetDialogListener(nsIDownloadProgressListener* aInternalListener) {
|
||||
mDialogListener = aInternalListener;
|
||||
}
|
||||
void SetDialog(nsIProgressDialog* aDialog) {
|
||||
|
@ -170,7 +169,7 @@ private:
|
|||
|
||||
nsCOMPtr<nsIURI> mTarget;
|
||||
nsCOMPtr<nsIURI> mSource;
|
||||
nsCOMPtr<nsIWebProgressListener2> mDialogListener;
|
||||
nsCOMPtr<nsIDownloadProgressListener> mDialogListener;
|
||||
nsCOMPtr<nsICancelable> mCancelable;
|
||||
nsCOMPtr<nsIRequest> mRequest;
|
||||
nsCOMPtr<nsIProgressDialog> mDialog;
|
||||
|
|
Загрузка…
Ссылка в новой задаче