From 982c7a0f7e59e6ef09766202dec9a40c201ebc8a Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Tue, 8 May 2012 13:05:33 -0700 Subject: [PATCH 1/3] Add fallback option for the extension. --- .../firefox/components/PdfStreamConverter.js | 36 +++++++++++++++++++ web/viewer.html | 8 +++-- web/viewer.js | 10 ++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/extensions/firefox/components/PdfStreamConverter.js b/extensions/firefox/components/PdfStreamConverter.js index af9cf41b8..5d7ac69ea 100644 --- a/extensions/firefox/components/PdfStreamConverter.js +++ b/extensions/firefox/components/PdfStreamConverter.js @@ -16,6 +16,7 @@ const MAX_DATABASE_LENGTH = 4096; Cu.import('resource://gre/modules/XPCOMUtils.jsm'); Cu.import('resource://gre/modules/Services.jsm'); +Cu.import('resource://gre/modules/NetUtil.jsm'); let application = Cc['@mozilla.org/fuel/application;1'] .getService(Ci.fuelIApplication); @@ -45,6 +46,41 @@ ChromeActions.prototype = { download: function(data) { Services.wm.getMostRecentWindow('navigator:browser').saveURL(data); }, + fallback: function(data) { + let mimeService = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService); + var handlerInfo = mimeService. + getFromTypeAndExtension('application/pdf', 'pdf'); + var uri = NetUtil.newURI(data); + var filename = Services.wm.getMostRecentWindow('navigator:browser'). + getDefaultFileName('document.pdf', uri); + // Create a temporary file to output to. + var file = Cc['@mozilla.org/file/directory_service;1']. + getService(Ci.nsIProperties). + get('TmpD', Ci.nsIFile); + file.append(filename); + file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt('0666', 8)); + + var ostream = Cc['@mozilla.org/network/file-output-stream;1']. + createInstance(Ci.nsIFileOutputStream); + ostream.init(file, -1, -1, 0); + + // Fetch the file and once it's ready attempt to open it with the system's + // default pdf handler. + NetUtil.asyncFetch(uri, function(istream, aResult) { + if (!Components.isSuccessCode(aResult)) { + log('Error: Fetching file failed with code ' + aResult); + return; + } + NetUtil.asyncCopy(istream, ostream, function(aResult) { + if (!Components.isSuccessCode(aResult)) { + log('Error: Copying file failed with code: ' + aResult); + return; + } + handlerInfo.preferredAction = Ci.nsIHandlerInfo.useSystemDefault; + handlerInfo.launchWithFile(file); + }); + }); + }, setDatabase: function(data) { if (this.inPrivateBrowswing) return; diff --git a/web/viewer.html b/web/viewer.html index 0ec05e031..fa836fdab 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -101,12 +101,14 @@ Print --> - - + - Current View + Current View
diff --git a/web/viewer.js b/web/viewer.js index e8eb9ad3b..b15b123a4 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -374,6 +374,13 @@ var PDFView = { } }, + fallback: function pdfViewDownload() { + if (!PDFJS.isFirefoxExtension) + return; // can't do this with regular viewer + var url = this.url.split('#')[0]; + FirefoxCom.request('fallback', url); + }, + navigateTo: function pdfViewNavigateTo(dest) { if (typeof dest === 'string') dest = this.destinations[dest]; @@ -1352,6 +1359,9 @@ window.addEventListener('load', function webViewerLoad(evt) { document.getElementById('fileInput').value = null; } + if (PDFJS.isFirefoxExtension) + document.getElementById('fallback').removeAttribute('hidden'); + // Special debugging flags in the hash section of the URL. var hash = document.location.hash.substring(1); var hashParams = PDFView.parseQueryString(hash); From c1f73b96a40bd7d3d0ac17001509ff884df2c034 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Tue, 8 May 2012 16:11:50 -0700 Subject: [PATCH 2/3] Use open with/save as dialog for fallback and download. --- .../firefox/components/PdfStreamConverter.js | 55 +++++++++---------- web/viewer.html | 8 +-- web/viewer.js | 5 +- 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/extensions/firefox/components/PdfStreamConverter.js b/extensions/firefox/components/PdfStreamConverter.js index 5d7ac69ea..f723c69e4 100644 --- a/extensions/firefox/components/PdfStreamConverter.js +++ b/extensions/firefox/components/PdfStreamConverter.js @@ -44,42 +44,37 @@ function ChromeActions() { } ChromeActions.prototype = { download: function(data) { - Services.wm.getMostRecentWindow('navigator:browser').saveURL(data); - }, - fallback: function(data) { let mimeService = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService); var handlerInfo = mimeService. getFromTypeAndExtension('application/pdf', 'pdf'); var uri = NetUtil.newURI(data); - var filename = Services.wm.getMostRecentWindow('navigator:browser'). - getDefaultFileName('document.pdf', uri); - // Create a temporary file to output to. - var file = Cc['@mozilla.org/file/directory_service;1']. - getService(Ci.nsIProperties). - get('TmpD', Ci.nsIFile); - file.append(filename); - file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt('0666', 8)); - var ostream = Cc['@mozilla.org/network/file-output-stream;1']. - createInstance(Ci.nsIFileOutputStream); - ostream.init(file, -1, -1, 0); - - // Fetch the file and once it's ready attempt to open it with the system's - // default pdf handler. - NetUtil.asyncFetch(uri, function(istream, aResult) { - if (!Components.isSuccessCode(aResult)) { - log('Error: Fetching file failed with code ' + aResult); - return; + var extHelperAppSvc = + Cc['@mozilla.org/uriloader/external-helper-app-service;1']. + getService(Ci.nsIExternalHelperAppService); + var frontWindow = Cc['@mozilla.org/embedcomp/window-watcher;1']. + getService(Ci.nsIWindowWatcher).activeWindow; + var ioService = Services.io; + var channel = ioService.newChannel(data, null, null); + var listener = { + extListener: null, + onStartRequest: function(aRequest, aContext) { + this.extListener = extHelperAppSvc.doContent('application/pdf', + aRequest, frontWindow, false); + this.extListener.onStartRequest(aRequest, aContext); + }, + onStopRequest: function(aRequest, aContext, aStatusCode) { + if (this.extListener) + this.extListener.onStopRequest(aRequest, aContext, aStatusCode); + }, + onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, + aCount) { + this.extListener.onDataAvailable(aRequest, aContext, aInputStream, + aOffset, aCount); } - NetUtil.asyncCopy(istream, ostream, function(aResult) { - if (!Components.isSuccessCode(aResult)) { - log('Error: Copying file failed with code: ' + aResult); - return; - } - handlerInfo.preferredAction = Ci.nsIHandlerInfo.useSystemDefault; - handlerInfo.launchWithFile(file); - }); - }); + }; + + channel.asyncOpen(listener, null); }, setDatabase: function(data) { if (this.inPrivateBrowswing) diff --git a/web/viewer.html b/web/viewer.html index fa836fdab..0ec05e031 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -101,14 +101,12 @@ Print --> - - - Current View + Current View
diff --git a/web/viewer.js b/web/viewer.js index b15b123a4..857429c5e 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -375,9 +375,9 @@ var PDFView = { }, fallback: function pdfViewDownload() { + var url = this.url.split('#')[0]; if (!PDFJS.isFirefoxExtension) return; // can't do this with regular viewer - var url = this.url.split('#')[0]; FirefoxCom.request('fallback', url); }, @@ -1359,9 +1359,6 @@ window.addEventListener('load', function webViewerLoad(evt) { document.getElementById('fileInput').value = null; } - if (PDFJS.isFirefoxExtension) - document.getElementById('fallback').removeAttribute('hidden'); - // Special debugging flags in the hash section of the URL. var hash = document.location.hash.substring(1); var hashParams = PDFView.parseQueryString(hash); From 0cb1a088233b663785cfb2ead478c40774b050f4 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Wed, 9 May 2012 09:02:41 -0700 Subject: [PATCH 3/3] Remove fallback function. --- web/viewer.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/web/viewer.js b/web/viewer.js index 857429c5e..e8eb9ad3b 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -374,13 +374,6 @@ var PDFView = { } }, - fallback: function pdfViewDownload() { - var url = this.url.split('#')[0]; - if (!PDFJS.isFirefoxExtension) - return; // can't do this with regular viewer - FirefoxCom.request('fallback', url); - }, - navigateTo: function pdfViewNavigateTo(dest) { if (typeof dest === 'string') dest = this.destinations[dest];