Bug 875731 - Replace callers of nsIDownloadManager.usersDownloadsDirectory with Downloads.getUserDownloadsDirectory in browser/metro/. r=mbrubeck

This commit is contained in:
Raymond Lee 2013-09-23 13:10:38 +08:00
parent 76822a147d
commit 6f41ba5c1a
7 changed files with 133 additions and 124 deletions

View File

@ -349,20 +349,22 @@ var ContextCommands = {
picker.appendFilters(Ci.nsIFilePicker.filterImages);
// prefered save location
var dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
picker.displayDirectory = dnldMgr.userDownloadsDirectory;
try {
let lastDir = Services.prefs.getComplexValue("browser.download.lastDir", Ci.nsILocalFile);
if (this.isAccessibleDirectory(lastDir))
picker.displayDirectory = lastDir;
}
catch (e) { }
Task.spawn(function() {
picker.displayDirectory = yield Downloads.getPreferredDownloadsDirectory();
this._picker = picker;
this._pickerUrl = mediaURL;
this._pickerContentDisp = aPopupState.contentDisposition;
this._contentType = aPopupState.contentType;
picker.open(ContextCommands);
try {
let lastDir = Services.prefs.getComplexValue("browser.download.lastDir", Ci.nsILocalFile);
if (this.isAccessibleDirectory(lastDir))
picker.displayDirectory = lastDir;
}
catch (e) { }
this._picker = picker;
this._pickerUrl = mediaURL;
this._pickerContentDisp = aPopupState.contentDisposition;
this._contentType = aPopupState.contentType;
picker.open(ContextCommands);
}.bind(this));
},
/*

View File

@ -10,7 +10,10 @@ Cu.import("resource://gre/modules/Services.jsm");
* JS modules
*/
XPCOMUtils.defineLazyModuleGetter(this , "FormHistory",
XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
"resource://gre/modules/Downloads.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FormHistory",
"resource://gre/modules/FormHistory.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
@ -97,7 +100,7 @@ let ScriptContexts = {};
["CommandUpdater", "chrome://browser/content/commandUtil.js"],
["ContextCommands", "chrome://browser/content/ContextCommands.js"],
["Bookmarks", "chrome://browser/content/bookmarks.js"],
["Downloads", "chrome://browser/content/downloads.js"],
["MetroDownloadsView", "chrome://browser/content/downloads.js"],
["ConsolePanelView", "chrome://browser/content/console.js"],
["Site", "chrome://browser/content/Site.js"],
["TopSites", "chrome://browser/content/TopSites.js"],

View File

@ -142,7 +142,7 @@ var BrowserUI = {
messageManager.addMessageListener("Browser:MozApplicationManifest", OfflineApps);
try {
Downloads.init();
MetroDownloadsView.init();
DialogUI.init();
FormHelperUI.init();
FindHelperUI.init();
@ -175,7 +175,7 @@ var BrowserUI = {
PanelUI.uninit();
FlyoutPanelsUI.uninit();
Downloads.uninit();
MetroDownloadsView.uninit();
SettingsCharm.uninit();
messageManager.removeMessageListener("Content:StateChange", this);
PageThumbs.uninit();

View File

@ -255,7 +255,7 @@
<observes element="bcast_urlbarState" attribute="*"/>
<hbox id="toolbar-context-page" pack="end">
<circularprogressindicator id="download-progress"
oncommand="Downloads.onDownloadButton()"/>
oncommand="MetroDownloadsView.onDownloadButton()"/>
<toolbarbutton id="star-button" class="appbar-primary"
type="checkbox"
oncommand="Appbar.onStarButton()"/>

View File

@ -6,7 +6,7 @@
const URI_GENERIC_ICON_DOWNLOAD = "chrome://browser/skin/images/alert-downloads-30.png";
const TOAST_URI_GENERIC_ICON_DOWNLOAD = "ms-appx:///metro/chrome/chrome/skin/images/alert-downloads-30.png"
var Downloads = {
var MetroDownloadsView = {
/**
* _downloadCount keeps track of the number of downloads that a single
* notification bar groups together. A download is grouped with other
@ -217,15 +217,15 @@ var Downloads = {
label: tryAgainButtonText,
accessKey: "",
callback: function() {
Downloads.manager.retryDownload(aDownload.id);
MetroDownloadsView.manager.retryDownload(aDownload.id);
}
},
{
label: cancelButtonText,
accessKey: "",
callback: function() {
Downloads.cancelDownload(aDownload);
Downloads._downloadProgressIndicator.reset();
MetroDownloadsView.cancelDownload(aDownload);
MetroDownloadsView._downloadProgressIndicator.reset();
}
}
];
@ -243,7 +243,7 @@ var Downloads = {
accessKey: "",
callback: function() {
let fileURI = aDownload.target;
let file = Downloads._getLocalFile(fileURI);
let file = MetroDownloadsView._getLocalFile(fileURI);
file.reveal();
}
}
@ -264,7 +264,7 @@ var Downloads = {
label: runButtonText,
accessKey: "",
callback: function() {
Downloads.openDownload(aDownload);
MetroDownloadsView.openDownload(aDownload);
}
});
}
@ -288,12 +288,12 @@ var Downloads = {
switch (aTopic) {
case "alertclickcallback":
let fileURI = aDownload.target;
let file = Downloads._getLocalFile(fileURI);
let file = MetroDownloadsView._getLocalFile(fileURI);
file.reveal();
let downloadCompleteNotification =
Downloads._notificationBox.getNotificationWithValue("download-complete");
Downloads._notificationBox.removeNotification(downloadCompleteNotification);
MetroDownloadsView._notificationBox.getNotificationWithValue("download-complete");
MetroDownloadsView._notificationBox.removeNotification(downloadCompleteNotification);
break;
}
}
@ -306,11 +306,11 @@ var Downloads = {
observe: function (aSubject, aTopic, aData) {
switch (aTopic) {
case "alertclickcallback":
Downloads.openDownload(aDownload);
MetroDownloadsView.openDownload(aDownload);
let downloadCompleteNotification =
Downloads._notificationBox.getNotificationWithValue("download-complete");
Downloads._notificationBox.removeNotification(downloadCompleteNotification);
MetroDownloadsView._notificationBox.getNotificationWithValue("download-complete");
MetroDownloadsView._notificationBox.removeNotification(downloadCompleteNotification);
break;
}
}
@ -406,8 +406,8 @@ var Downloads = {
label: cancelButtonText,
accessKey: "",
callback: function() {
Downloads.cancelDownloads();
Downloads._downloadProgressIndicator.reset();
MetroDownloadsView.cancelDownloads();
MetroDownloadsView._downloadProgressIndicator.reset();
}
}
];
@ -446,8 +446,8 @@ var Downloads = {
switch (aEvent.type) {
case "AlertClose":
if (aEvent.notification.value == "download-complete" &&
!Downloads._notificationBox.getNotificationWithValue("download-complete")) {
Downloads._downloadProgressIndicator.reset();
!MetroDownloadsView._notificationBox.getNotificationWithValue("download-complete")) {
MetroDownloadsView._downloadProgressIndicator.reset();
}
break;
}
@ -562,10 +562,10 @@ AlertDownloadProgressListener.prototype = {
let contentLength = aDownload.size;
if (availableSpace > 0 && contentLength > 0 && contentLength > availableSpace) {
Downloads.showAlert(aDownload.target.spec.replace("file:", "download:"),
strings.GetStringFromName("alertDownloadsNoSpace"),
strings.GetStringFromName("alertDownloadsSize"));
Downloads.cancelDownload(aDownload);
MetroDownloadsView.showAlert(aDownload.target.spec.replace("file:", "download:"),
strings.GetStringFromName("alertDownloadsNoSpace"),
strings.GetStringFromName("alertDownloadsSize"));
MetroDownloadsView.cancelDownload(aDownload);
}
},

View File

@ -47,7 +47,7 @@ function equalNumbers(){
}
function getPromisedDbResult(aStatement) {
let dbConnection = Downloads.manager.DBConnection;
let dbConnection = MetroDownloadsView.manager.DBConnection;
let statement = ("string" == typeof aStatement) ?
dbConnection.createAsyncStatement(
aStatement
@ -129,7 +129,7 @@ function resetDownloads(){
// Services.prefs.clearUserPref("browser.download.panel.shown");
// Ensure that data is unloaded.
let dlMgr = Downloads.manager;
let dlMgr = MetroDownloadsView.manager;
let dlsToRemove = [];
// Clear all completed/cancelled downloads
dlMgr.cleanUp();
@ -247,11 +247,11 @@ gTests.push({
// we're going to add stuff to the downloads db.
yield spawn( gen_addDownloadRows( DownloadData ) );
todo( false, "Check that Downloads._progressNotificationInfo and Downloads._downloadCount \
todo( false, "Check that MetroDownloadsView._progressNotificationInfo and MetroDownloadsView._downloadCount \
have the correct length (DownloadData.length) \
May also test that the correct notifications show up for various states.");
todo(false, "Iterate through download objects in Downloads._progressNotificationInfo \
todo(false, "Iterate through download objects in MetroDownloadsView._progressNotificationInfo \
and confirm that the downloads they refer to are the same as those in \
DownloadData.");
} catch(e) {
@ -299,7 +299,7 @@ gTests.push({
is(downloadRows.length, 3, "Correct number of downloads in the db before removal");
todo(false, "Get some download from Downloads._progressNotificationInfo, \
todo(false, "Get some download from MetroDownloadsView._progressNotificationInfo, \
confirm that its file exists, then remove it.");
// remove is async(?), wait a bit

View File

@ -18,6 +18,10 @@ XPCOMUtils.defineLazyGetter(this, "ContentUtil", function() {
Cu.import("resource:///modules/ContentUtil.jsm");
return ContentUtil;
});
XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
"resource://gre/modules/Downloads.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm");
// -----------------------------------------------------------------------
// HelperApp Launcher Dialog
@ -134,98 +138,98 @@ HelperAppLauncherDialog.prototype = {
},
promptForSaveToFile: function hald_promptForSaveToFile(aLauncher, aContext, aDefaultFile, aSuggestedFileExt, aForcePrompt) {
let file = null;
let prefs = Services.prefs;
if (!aForcePrompt) {
// Check to see if the user wishes to auto save to the default download
// folder without prompting. Note that preference might not be set.
let autodownload = true;
try {
autodownload = prefs.getBoolPref(PREF_BD_USEDOWNLOADDIR);
} catch (e) { }
if (autodownload) {
// Retrieve the user's default download directory
let dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
let defaultFolder = dnldMgr.userDownloadsDirectory;
return Task.spawn(function() {
let file = null;
let prefs = Services.prefs;
if (!aForcePrompt) {
// Check to see if the user wishes to auto save to the default download
// folder without prompting. Note that preference might not be set.
let autodownload = true;
try {
file = this.validateLeafName(defaultFolder, aDefaultFile, aSuggestedFileExt);
}
catch (e) {
}
autodownload = prefs.getBoolPref(PREF_BD_USEDOWNLOADDIR);
} catch (e) { }
// Check to make sure we have a valid directory, otherwise, prompt
if (file)
return file;
if (autodownload) {
// Retrieve the user's default download directory
let defaultFolder = yield Downloads.getPreferredDownloadsDirectory();
try {
file = this.validateLeafName(defaultFolder, aDefaultFile, aSuggestedFileExt);
}
catch (e) {
}
// Check to make sure we have a valid directory, otherwise, prompt
if (file)
throw new Task.Result(file);
}
}
}
// Use file picker to show dialog.
let picker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
let windowTitle = "";
let parent = aContext.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
picker.init(parent, windowTitle, Ci.nsIFilePicker.modeSave);
picker.defaultString = aDefaultFile;
// Use file picker to show dialog.
let picker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
let windowTitle = "";
let parent = aContext.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
picker.init(parent, windowTitle, Ci.nsIFilePicker.modeSave);
picker.defaultString = aDefaultFile;
if (aSuggestedFileExt) {
// aSuggestedFileExtension includes the period, so strip it
picker.defaultExtension = aSuggestedFileExt.substring(1);
}
else {
if (aSuggestedFileExt) {
// aSuggestedFileExtension includes the period, so strip it
picker.defaultExtension = aSuggestedFileExt.substring(1);
}
else {
try {
picker.defaultExtension = aLauncher.MIMEInfo.primaryExtension;
}
catch (e) { }
}
var wildCardExtension = "*";
if (aSuggestedFileExt) {
wildCardExtension += aSuggestedFileExt;
picker.appendFilter(aLauncher.MIMEInfo.description, wildCardExtension);
}
picker.appendFilters(Ci.nsIFilePicker.filterAll);
// Default to lastDir if it is valid, otherwise use the user's default
// downloads directory. userDownloadsDirectory should always return a
// valid directory, so we can safely default to it.
picker.displayDirectory = yield Downloads.getPreferredDownloadsDirectory();
// The last directory preference may not exist, which will throw.
try {
picker.defaultExtension = aLauncher.MIMEInfo.primaryExtension;
let lastDir = prefs.getComplexValue("browser.download.lastDir", Ci.nsILocalFile);
if (isUsableDirectory(lastDir))
picker.displayDirectory = lastDir;
}
catch (e) { }
}
var wildCardExtension = "*";
if (aSuggestedFileExt) {
wildCardExtension += aSuggestedFileExt;
picker.appendFilter(aLauncher.MIMEInfo.description, wildCardExtension);
}
picker.appendFilters(Ci.nsIFilePicker.filterAll);
// Default to lastDir if it is valid, otherwise use the user's default
// downloads directory. userDownloadsDirectory should always return a
// valid directory, so we can safely default to it.
var dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
picker.displayDirectory = dnldMgr.userDownloadsDirectory;
// The last directory preference may not exist, which will throw.
try {
let lastDir = prefs.getComplexValue("browser.download.lastDir", Ci.nsILocalFile);
if (isUsableDirectory(lastDir))
picker.displayDirectory = lastDir;
}
catch (e) { }
if (picker.show() == Ci.nsIFilePicker.returnCancel) {
// null result means user cancelled.
return null;
}
// Be sure to save the directory the user chose through the Save As...
// dialog as the new browser.download.dir since the old one
// didn't exist.
file = picker.file;
if (file) {
try {
// Remove the file so that it's not there when we ensure non-existence later;
// this is safe because for the file to exist, the user would have had to
// confirm that he wanted the file overwritten.
if (file.exists())
file.remove(false);
if (picker.show() == Ci.nsIFilePicker.returnCancel) {
// null result means user cancelled.
throw new Task.Result(null);
}
catch (e) { }
var newDir = file.parent.QueryInterface(Ci.nsILocalFile);
prefs.setComplexValue("browser.download.lastDir", Ci.nsILocalFile, newDir);
file = this.validateLeafName(newDir, file.leafName, null);
}
return file;
// Be sure to save the directory the user chose through the Save As...
// dialog as the new browser.download.dir since the old one
// didn't exist.
file = picker.file;
if (file) {
try {
// Remove the file so that it's not there when we ensure non-existence later;
// this is safe because for the file to exist, the user would have had to
// confirm that he wanted the file overwritten.
if (file.exists())
file.remove(false);
}
catch (e) { }
var newDir = file.parent.QueryInterface(Ci.nsILocalFile);
prefs.setComplexValue("browser.download.lastDir", Ci.nsILocalFile, newDir);
file = this.validateLeafName(newDir, file.leafName, null);
}
throw new Task.Result(file);
}.bind(this));
},
validateLeafName: function hald_validateLeafName(aLocalFile, aLeafName, aFileExt) {