Skip to content

Commit

Permalink
Merge branch 'omp-jb51673' into 'master'
Browse files Browse the repository at this point in the history
[embedlite-components] Add downloadpicker API to AppHelperDialog. Contributes to JB#51673

See merge request mer-core/embedlite-components!111
  • Loading branch information
llewelld committed Mar 23, 2021
2 parents b01ebf5 + d1673f7 commit a7465da
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
2 changes: 1 addition & 1 deletion jscomps/EmbedLiteJSComponents.manifest
Expand Up @@ -105,4 +105,4 @@ category app-startup EmbedliteDownloadManager service,@mozilla.org/embedlite/dow
# ContentPermissionManager.js
component {86d354c6-81bc-4eb5-82c3-4c9859586165} ContentPermissionManager.js
contract @mozilla.org/content-permission/manager;1 {86d354c6-81bc-4eb5-82c3-4c9859586165}
category app-startup ContentPermissionManager service,@mozilla.org/content-permission/manager;1
category app-startup ContentPermissionManager service,@mozilla.org/content-permission/manager;1
79 changes: 55 additions & 24 deletions jscomps/HelperAppDialog.js
Expand Up @@ -8,6 +8,7 @@ const Cu = Components.utils;
const Cr = Components.results;

const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir";
const PREF_BD_DOWNLOADDIR = "browser.download.dir";
const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download";

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Expand All @@ -20,6 +21,9 @@ ChromeUtils.import("resource://gre/modules/FileUtils.jsm");

Services.scriptloader.loadSubScript("chrome://embedlite/content/Logger.js");

XPCOMUtils.defineLazyServiceGetter(Services, "embedlite",
"@mozilla.org/embedlite-app-service;1",
"nsIEmbedAppService");
///////////////////////////////////////////////////////////////////////////////
//// Helper Functions

Expand Down Expand Up @@ -48,7 +52,15 @@ function HelperAppLauncherDialog() {

HelperAppLauncherDialog.prototype = {
classID: Components.ID("{e9d277a0-268a-4ec2-bb8c-10fdf3e44611}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog]),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog], [Ci.nsIObserver]),

observe: function(aSubject, aTopic, aData) {
switch (aTopic) {
case "embedui:downloadpicker":
this.saveAndDownload(JSON.parse(aData));
break;
}
},

show: function hald_show(aLauncher, aContext, aReason) {
// Check to see if we can open this file or not
Expand All @@ -67,30 +79,24 @@ HelperAppLauncherDialog.prototype = {
promptForSaveToFileAsync: function hald_promptForSaveToFileAsync(aLauncher, aWindowContext, aDefaultFileName,
aSuggestedFileExtension,
aForcePrompt) {
Logger.debug("HelperAppLauncherDialog promptForSaveToFileAsync");

let file = null;
this.mLauncher = aLauncher;

(async () => {
// Retrieve the user's default download directory
let preferredDir = await Downloads.getPreferredDownloadsDirectory();
let defaultFolder = new FileUtils.File(preferredDir);
try {
file = this.validateLeafName(defaultFolder, aDefaultFileName,
aSuggestedFileExtension);
} catch (e) {
// When the default download directory is write-protected,
// prompt the user for a different target file.
Logger.warn(e);
}

// Check to make sure we have a valid directory, otherwise, prompt
if (file) {
// This path is taken when we have a writable default download directory.
aLauncher.saveDestinationAvailable(file);
}
})().catch(Cu.reportError);
let downloadDir = Services.prefs.getStringPref(PREF_BD_DOWNLOADDIR);
var result = {
defaultFileName: aDefaultFileName,
suggestedFileExtension: aSuggestedFileExtension,
downloadDirectory: downloadDir
}
if (!aForcePrompt) {
let autodownload = Services.prefs.getBoolPref(PREF_BD_USEDOWNLOADDIR);

if (autodownload) {
this.saveAndDownload(result);
return;
}
}
Services.obs.addObserver(this, "embedui:downloadpicker", false);
Services.obs.notifyObservers(null, "embed:downloadpicker", JSON.stringify(result));
},

promptForSaveToFile: function hald_promptForSaveToFile(aLauncher, aContext, aDefaultFile, aSuggestedFileExt, aForcePrompt) {
Expand Down Expand Up @@ -127,7 +133,32 @@ HelperAppLauncherDialog.prototype = {
"alertDownloads",
"alertCantOpenDownload",
true, "", aCallback, "downloadopen-fail");
}
},
saveAndDownload: function(data) {
let file = null;

(async () => {
let prefferedDir = data.downloadDirectory;
let downloadFolder = new FileUtils.File(prefferedDir);
if (!isUsableDirectory(downloadFolder)) {
prefferedDir = await Downloads.getPreferredDownloadsDirectory();
downloadFolder = new FileUtils.File(prefferedDir);
}
try {
file = this.validateLeafName(downloadFolder, data.defaultFileName,
data.suggestedFileExtension);
} catch (e) {
// When the default download directory is write-protected,
// prompt the user for a different target file.
Logger.warn(e);
}

if (file && this.mLauncher) {
this.mLauncher.saveDestinationAvailable(file);
}
Services.obs.removeObserver(this, "embedui:downloadpicker", true);
})().catch(Cu.reportError);
},
};

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HelperAppLauncherDialog]);

0 comments on commit a7465da

Please sign in to comment.