Skip to content

Commit c1f73b9

Browse files
committed
Use open with/save as dialog for fallback and download.
1 parent 982c7a0 commit c1f73b9

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

extensions/firefox/components/PdfStreamConverter.js

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,37 @@ function ChromeActions() {
4444
}
4545
ChromeActions.prototype = {
4646
download: function(data) {
47-
Services.wm.getMostRecentWindow('navigator:browser').saveURL(data);
48-
},
49-
fallback: function(data) {
5047
let mimeService = Cc['@mozilla.org/mime;1'].getService(Ci.nsIMIMEService);
5148
var handlerInfo = mimeService.
5249
getFromTypeAndExtension('application/pdf', 'pdf');
5350
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;
51+
52+
var extHelperAppSvc =
53+
Cc['@mozilla.org/uriloader/external-helper-app-service;1'].
54+
getService(Ci.nsIExternalHelperAppService);
55+
var frontWindow = Cc['@mozilla.org/embedcomp/window-watcher;1'].
56+
getService(Ci.nsIWindowWatcher).activeWindow;
57+
var ioService = Services.io;
58+
var channel = ioService.newChannel(data, null, null);
59+
var listener = {
60+
extListener: null,
61+
onStartRequest: function(aRequest, aContext) {
62+
this.extListener = extHelperAppSvc.doContent('application/pdf',
63+
aRequest, frontWindow, false);
64+
this.extListener.onStartRequest(aRequest, aContext);
65+
},
66+
onStopRequest: function(aRequest, aContext, aStatusCode) {
67+
if (this.extListener)
68+
this.extListener.onStopRequest(aRequest, aContext, aStatusCode);
69+
},
70+
onDataAvailable: function(aRequest, aContext, aInputStream, aOffset,
71+
aCount) {
72+
this.extListener.onDataAvailable(aRequest, aContext, aInputStream,
73+
aOffset, aCount);
7374
}
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-
});
75+
};
76+
77+
channel.asyncOpen(listener, null);
8378
},
8479
setDatabase: function(data) {
8580
if (this.inPrivateBrowswing)

web/viewer.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,12 @@
101101
<span data-l10n-id="print_label">Print</span>
102102
</button>
103103
-->
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">
104+
105+
<button id="download" class="toolbarButton download" title="Download" onclick="PDFView.download();" tabindex="12" data-l10n-id="download">
108106
<span data-l10n-id="download_label">Download</span>
109107
</button>
110108
<!-- <div class="toolbarButtonSpacer"></div> -->
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>
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>
112110
</div>
113111
<div class="outerCenter">
114112
<div class="innerCenter" id="toolbarViewerMiddle">

web/viewer.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ var PDFView = {
375375
},
376376

377377
fallback: function pdfViewDownload() {
378+
var url = this.url.split('#')[0];
378379
if (!PDFJS.isFirefoxExtension)
379380
return; // can't do this with regular viewer
380-
var url = this.url.split('#')[0];
381381
FirefoxCom.request('fallback', url);
382382
},
383383

@@ -1359,9 +1359,6 @@ window.addEventListener('load', function webViewerLoad(evt) {
13591359
document.getElementById('fileInput').value = null;
13601360
}
13611361

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

0 commit comments

Comments
 (0)