Bug 901360 - (Part 2) Fix PDF downloads. r=margaret

--HG--
extra : rebase_source : c0b856edd344292fc28384e18e1474cb7198b28a
This commit is contained in:
Wes Johnston 2014-12-08 11:26:35 -08:00
Родитель 692304178e
Коммит a1ef6f3837
1 изменённых файлов: 26 добавлений и 46 удалений

Просмотреть файл

@ -35,6 +35,9 @@ XPCOMUtils.defineLazyModuleGetter(this, "UITelemetry",
XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
"resource://gre/modules/PluralForm.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
"resource://gre/modules/Downloads.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Messaging",
"resource://gre/modules/Messaging.jsm");
@ -103,6 +106,9 @@ XPCOMUtils.defineLazyModuleGetter(this, "PermissionsUtils",
XPCOMUtils.defineLazyModuleGetter(this, "SharedPreferences",
"resource://gre/modules/SharedPreferences.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Notifications",
"resource://gre/modules/Notifications.jsm");
// Lazily-loaded browser scripts:
[
["SelectHelper", "chrome://browser/content/SelectHelper.js"],
@ -137,7 +143,6 @@ XPCOMUtils.defineLazyModuleGetter(this, "SharedPreferences",
["FeedHandler", ["Feeds:Subscribe"], "chrome://browser/content/FeedHandler.js"],
["Feedback", ["Feedback:Show"], "chrome://browser/content/Feedback.js"],
["SelectionHandler", ["TextSelection:Get"], "chrome://browser/content/SelectionHandler.js"],
["Notifications", ["Notification:Event"], "chrome://browser/content/Notifications.jsm"],
["EmbedRT", ["GeckoView:ImportScript"], "chrome://browser/content/EmbedRT.js"],
["Reader", ["Reader:Removed"], "chrome://browser/content/Reader.js"],
].forEach(function (aScript) {
@ -417,7 +422,7 @@ var BrowserApp = {
NativeWindow.init();
LightWeightThemeWebInstaller.init();
Downloads.init();
DownloadNotifications.init();
FormAssistant.init();
IndexedDB.init();
HealthReportStatusListener.init();
@ -1199,54 +1204,29 @@ var BrowserApp = {
},
saveAsPDF: function saveAsPDF(aBrowser) {
// Create the final destination file location
let fileName = ContentAreaUtils.getDefaultFileName(aBrowser.contentTitle, aBrowser.currentURI, null, null);
fileName = fileName.trim() + ".pdf";
Task.spawn(function* () {
let fileName = ContentAreaUtils.getDefaultFileName(aBrowser.contentTitle, aBrowser.currentURI, null, null);
fileName = fileName.trim() + ".pdf";
let dm = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
let downloadsDir = dm.defaultDownloadsDirectory;
let downloadsDir = yield Downloads.getPreferredDownloadsDirectory();
let file = OS.Path.join(downloadsDir, fileName);
let file = downloadsDir.clone();
file.append(fileName);
file.createUnique(file.NORMAL_FILE_TYPE, parseInt("666", 8));
// Force this to have a unique name.
let openedFile = yield OS.File.openUnique(file, { humanReadable: true });
file = openedFile.path;
yield openedFile.file.close();
let printSettings = Cc["@mozilla.org/gfx/printsettings-service;1"].getService(Ci.nsIPrintSettingsService).newPrintSettings;
printSettings.printSilent = true;
printSettings.showPrintProgress = false;
printSettings.printBGImages = true;
printSettings.printBGColors = true;
printSettings.printToFile = true;
printSettings.toFileName = file.path;
printSettings.printFrameType = Ci.nsIPrintSettings.kFramesAsIs;
printSettings.outputFormat = Ci.nsIPrintSettings.kOutputFormatPDF;
let download = yield Downloads.createDownload({
source: aBrowser.contentWindow,
target: file,
saver: "pdf",
startTime: Date.now(),
});
//XXX we probably need a preference here, the header can be useful
printSettings.footerStrCenter = "";
printSettings.footerStrLeft = "";
printSettings.footerStrRight = "";
printSettings.headerStrCenter = "";
printSettings.headerStrLeft = "";
printSettings.headerStrRight = "";
// Create a valid mimeInfo for the PDF
let ms = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
let mimeInfo = ms.getFromTypeAndExtension("application/pdf", "pdf");
let webBrowserPrint = aBrowser.contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebBrowserPrint);
let cancelable = {
cancel: function (aReason) {
webBrowserPrint.cancel();
}
}
let isPrivate = PrivateBrowsingUtils.isBrowserPrivate(aBrowser);
let download = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
aBrowser.currentURI,
Services.io.newFileURI(file), "", mimeInfo,
Date.now() * 1000, null, cancelable, isPrivate);
webBrowserPrint.print(printSettings, download);
let list = yield Downloads.getList(download.source.isPrivate ? Downloads.PRIVATE : Downloads.PUBLIC)
yield list.add(download);
yield download.start();
});
},
notifyPrefObservers: function(aPref) {