Skip to content

Commit 982c7a0

Browse files
committed
Add fallback option for the extension.
1 parent 80728f7 commit 982c7a0

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

extensions/firefox/components/PdfStreamConverter.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const MAX_DATABASE_LENGTH = 4096;
1616

1717
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
1818
Cu.import('resource://gre/modules/Services.jsm');
19+
Cu.import('resource://gre/modules/NetUtil.jsm');
1920

2021
let application = Cc['@mozilla.org/fuel/application;1']
2122
.getService(Ci.fuelIApplication);
@@ -45,6 +46,41 @@ ChromeActions.prototype = {
4546
download: function(data) {
4647
Services.wm.getMostRecentWindow('navigator:browser').saveURL(data);
4748
},
49+
fallback: function(data) {
50+
let mimeService = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService);
51+
var handlerInfo = mimeService.
52+
getFromTypeAndExtension('application/pdf', 'pdf');
53+
var uri = NetUtil.newURI(data);
54+
var filename = Services.wm.getMostRecentWindow('navigator:browser').
55+
getDefaultFileName('document.pdf', uri);
56+
// Create a temporary file to output to.
57+
var file = Cc['@mozilla.org/file/directory_service;1'].
58+
getService(Ci.nsIProperties).
59+
get('TmpD', Ci.nsIFile);
60+
file.append(filename);
61+
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt('0666', 8));
62+
63+
var ostream = Cc['@mozilla.org/network/file-output-stream;1'].
64+
createInstance(Ci.nsIFileOutputStream);
65+
ostream.init(file, -1, -1, 0);
66+
67+
// Fetch the file and once it's ready attempt to open it with the system's
68+
// default pdf handler.
69+
NetUtil.asyncFetch(uri, function(istream, aResult) {
70+
if (!Components.isSuccessCode(aResult)) {
71+
log('Error: Fetching file failed with code ' + aResult);
72+
return;
73+
}
74+
NetUtil.asyncCopy(istream, ostream, function(aResult) {
75+
if (!Components.isSuccessCode(aResult)) {
76+
log('Error: Copying file failed with code: ' + aResult);
77+
return;
78+
}
79+
handlerInfo.preferredAction = Ci.nsIHandlerInfo.useSystemDefault;
80+
handlerInfo.launchWithFile(file);
81+
});
82+
});
83+
},
4884
setDatabase: function(data) {
4985
if (this.inPrivateBrowswing)
5086
return;

web/viewer.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@
101101
<span data-l10n-id="print_label">Print</span>
102102
</button>
103103
-->
104-
105-
<button id="download" class="toolbarButton download" title="Download" onclick="PDFView.download();" tabindex="12" data-l10n-id="download">
104+
<button id="fallback" class="toolbarButton fallback" title="Open with System Default PDF Viewer" onclick="PDFView.fallback();" tabindex="12" data-l10n-id="fallback" hidden="true">F
105+
<span data-l10n-id="fallback_label">Open with System Default PDF Viewer</span>
106+
</button>
107+
<button id="download" class="toolbarButton download" title="Download" onclick="PDFView.download();" tabindex="13" data-l10n-id="download">
106108
<span data-l10n-id="download_label">Download</span>
107109
</button>
108110
<!-- <div class="toolbarButtonSpacer"></div> -->
109-
<a href="#" id="viewBookmark" class="toolbarButton bookmark" title="Current view (copy or open in new window)" tabindex="13" data-l10n-id="bookmark"><span data-l10n-id="bookmark_label">Current View</span></a>
111+
<a href="#" id="viewBookmark" class="toolbarButton bookmark" title="Current view (copy or open in new window)" tabindex="14" data-l10n-id="bookmark"><span data-l10n-id="bookmark_label">Current View</span></a>
110112
</div>
111113
<div class="outerCenter">
112114
<div class="innerCenter" id="toolbarViewerMiddle">

web/viewer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,13 @@ var PDFView = {
374374
}
375375
},
376376

377+
fallback: function pdfViewDownload() {
378+
if (!PDFJS.isFirefoxExtension)
379+
return; // can't do this with regular viewer
380+
var url = this.url.split('#')[0];
381+
FirefoxCom.request('fallback', url);
382+
},
383+
377384
navigateTo: function pdfViewNavigateTo(dest) {
378385
if (typeof dest === 'string')
379386
dest = this.destinations[dest];
@@ -1352,6 +1359,9 @@ window.addEventListener('load', function webViewerLoad(evt) {
13521359
document.getElementById('fileInput').value = null;
13531360
}
13541361

1362+
if (PDFJS.isFirefoxExtension)
1363+
document.getElementById('fallback').removeAttribute('hidden');
1364+
13551365
// Special debugging flags in the hash section of the URL.
13561366
var hash = document.location.hash.substring(1);
13571367
var hashParams = PDFView.parseQueryString(hash);

0 commit comments

Comments
 (0)