From df516c0a52f6f0ca41a38df065320b0e3a466960 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Wed, 21 Mar 2018 15:44:31 +0100 Subject: [PATCH 1/2] [CRX] Stop intercepting ftp in Chrome 59+ The extension cannot easily fetch data from ftp:-resources any more in Chrome 59+. So don't intercept such URLs. --- extensions/chromium/pdfHandler.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/extensions/chromium/pdfHandler.js b/extensions/chromium/pdfHandler.js index 905d51fa1..0eba4e34b 100644 --- a/extensions/chromium/pdfHandler.js +++ b/extensions/chromium/pdfHandler.js @@ -153,10 +153,16 @@ chrome.webRequest.onBeforeRequest.addListener( urls: [ 'file://*/*.pdf', 'file://*/*.PDF', - // Note: Chrome 59 has disabled ftp resource loading by default: - // https://www.chromestatus.com/feature/5709390967472128 - 'ftp://*/*.pdf', - 'ftp://*/*.PDF', + ...( + // Duck-typing: MediaError.prototype.message was added in Chrome 59. + MediaError.prototype.hasOwnProperty('message') ? [] : + [ + // Note: Chrome 59 has disabled ftp resource loading by default: + // https://www.chromestatus.com/feature/5709390967472128 + 'ftp://*/*.pdf', + 'ftp://*/*.PDF', + ] + ), ], types: ['main_frame', 'sub_frame'], }, From 29c370af276893989d401921ef98ee5f65f276c0 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Wed, 21 Mar 2018 18:14:22 +0100 Subject: [PATCH 2/2] [CRX] Add file chooser as fallback (fixes #9411) Test case to exercise the different encodings: 1. Create a file "some file#@%M
%25 .pdf" 2. Build the extension with `gulp chromium` and load it in Chrome. 3. Go to `chrome://extensions/` and ensure that the "Allow access to file URLs" is disabled. 4. Try to open the file from step 1 in Chrome (maybe reload once). 5. PDF.js should be showing a file chooser button. 6. Click on that button and select a different file. Test: Check that a confirmation dialog pops up that warns about a different file name. Cancel the dialog. 7. Click on the button again and select the original file. Test: Check that the file opens as expected. --- web/app.js | 2 +- web/chromecom.js | 27 +++++++++++++++++++++++-- web/viewer-snippet-chrome-overlays.html | 6 ++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/web/app.js b/web/app.js index b009ad9f8..124320a73 100644 --- a/web/app.js +++ b/web/app.js @@ -597,7 +597,7 @@ let PDFViewerApplication = { args = { length, }; } if (originalURL !== undefined) { - file = { file: url, originalURL, }; + file = { url, originalURL, }; } PDFViewerApplication.open(file, args); }, diff --git a/web/chromecom.js b/web/chromecom.js index 173ca02ab..11c525119 100644 --- a/web/chromecom.js +++ b/web/chromecom.js @@ -91,7 +91,7 @@ let ChromeCom = { if (isAllowedAccess) { callback(file); } else { - requestAccessToLocalFile(file, overlayManager); + requestAccessToLocalFile(file, overlayManager, callback); } }); }); @@ -137,7 +137,7 @@ function reloadIfRuntimeIsUnavailable() { } let chromeFileAccessOverlayPromise; -function requestAccessToLocalFile(fileUrl, overlayManager) { +function requestAccessToLocalFile(fileUrl, overlayManager, callback) { let onCloseOverlay = null; if (top !== window) { // When the extension reloads after receiving new permissions, the pages @@ -197,6 +197,29 @@ function requestAccessToLocalFile(fileUrl, overlayManager) { // why this permission request is shown. document.getElementById('chrome-url-of-local-file').textContent = fileUrl; + document.getElementById('chrome-file-fallback').onchange = function() { + let file = this.files[0]; + if (file) { + let originalFilename = decodeURIComponent(fileUrl.split('/').pop()); + let originalURL = fileUrl; + if (originalFilename !== file.name) { + let msg = 'The selected file does not match the original file.' + + '\nOriginal: ' + originalFilename + + '\nSelected: ' + file.name + + '\nDo you want to open the selected file?'; + if (!confirm(msg)) { + this.value = ''; + return; + } + // There is no way to retrieve the original URL from the File object. + // So just generate a fake path. + originalURL = 'file:///fakepath/to/' + encodeURIComponent(file.name); + } + callback(URL.createObjectURL(file), file.size, originalURL); + overlayManager.close('chromeFileAccessOverlay'); + } + }; + overlayManager.open('chromeFileAccessOverlay'); }); } diff --git a/web/viewer-snippet-chrome-overlays.html b/web/viewer-snippet-chrome-overlays.html index 9e03fe577..7f8e03f55 100644 --- a/web/viewer-snippet-chrome-overlays.html +++ b/web/viewer-snippet-chrome-overlays.html @@ -22,5 +22,11 @@ to view this PDF file.

+
+

+ or select the file again: + +

+