Merge pull request #9410 from Rob--W/cleanup-chrome-compat

Cleanup code to drop support for Chrome < 49 in the Chrome extension
This commit is contained in:
Rob Wu 2018-01-26 16:32:18 +01:00 committed by GitHub
commit fd242ad2c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 203 deletions

View File

@ -1,125 +0,0 @@
/*
Copyright 2014 Mozilla Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';
var Features = {
featureDetectLastUA: '',
// Whether ftp: in XMLHttpRequest is allowed
extensionSupportsFTP: false,
// Whether redirectUrl at onHeadersReceived is supported.
webRequestRedirectUrl: false,
};
chrome.storage.local.get(Features, function(features) {
Features = features;
if (features.featureDetectLastUA === navigator.userAgent) {
// Browser not upgraded, so the features did probably not change.
return;
}
// In case of a downgrade, the features must be tested again.
var lastVersion = /Chrome\/\d+\.0\.(\d+)/.exec(features.featureDetectLastUA);
lastVersion = lastVersion ? parseInt(lastVersion[1], 10) : 0;
var newVersion = /Chrome\/\d+\.0\.(\d+)/.exec(navigator.userAgent);
var isDowngrade = newVersion && parseInt(newVersion[1], 10) < lastVersion;
var inconclusiveTestCount = 0;
if (isDowngrade || !features.extensionSupportsFTP) {
features.extensionSupportsFTP = featureTestFTP();
}
if (isDowngrade || !features.webRequestRedirectUrl) {
++inconclusiveTestCount;
// Relatively expensive (and asynchronous) test:
featureTestRedirectOnHeadersReceived(function(result) {
// result = 'yes', 'no' or 'maybe'.
if (result !== 'maybe') {
--inconclusiveTestCount;
}
features.webRequestRedirectUrl = result === 'yes';
checkTestCompletion();
});
}
checkTestCompletion();
function checkTestCompletion() {
// Only stamp the feature detection results when all tests have finished.
if (inconclusiveTestCount === 0) {
Features.featureDetectLastUA = navigator.userAgent;
}
chrome.storage.local.set(Features);
}
});
// Tests whether the extension can perform a FTP request.
// Feature is supported since Chromium 35.0.1888.0 (r256810).
function featureTestFTP() {
var x = new XMLHttpRequest();
// The URL does not need to exist, as long as the scheme is ftp:.
x.open('GET', 'ftp://ftp.mozilla.org/');
try {
x.send();
// Previous call did not throw error, so the feature is supported!
// Immediately abort the request so that the network is not hit at all.
x.abort();
return true;
} catch (e) {
return false;
}
}
// Tests whether redirectUrl at the onHeadersReceived stage is functional.
// Feature is supported since Chromium 35.0.1911.0 (r259546).
function featureTestRedirectOnHeadersReceived(callback) {
// The following URL is really going to be accessed via the network.
// It is the only way to feature-detect this feature, because the
// onHeadersReceived event is only triggered for http(s) requests.
var url = 'http://example.com/?feature-detect-' + chrome.runtime.id;
function onHeadersReceived(details) {
// If supported, the request is redirected.
// If not supported, the return value is ignored.
return {
redirectUrl: chrome.runtime.getURL('/manifest.json'),
};
}
chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {
types: ['xmlhttprequest'],
urls: [url],
}, ['blocking']);
var x = new XMLHttpRequest();
x.open('get', url);
x.onloadend = function() {
chrome.webRequest.onHeadersReceived.removeListener(onHeadersReceived);
if (!x.responseText) {
// Network error? Anyway, can't tell with certainty whether the feature
// is supported.
callback('maybe');
} else if (/^\s*\{/.test(x.responseText)) {
// If the response starts with "{", assume that the redirection to the
// manifest file succeeded, so the feature is supported.
callback('yes');
} else {
// Did not get the content of manifest.json, so the redirect seems not to
// be followed. The feature is not supported.
callback('no');
}
};
x.send();
}

View File

@ -14,7 +14,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
--> -->
<script src="feature-detect.js"></script>
<script src="options/migration.js"></script> <script src="options/migration.js"></script>
<script src="preserve-referer.js"></script> <script src="preserve-referer.js"></script>
<script src="pdfHandler.js"></script> <script src="pdfHandler.js"></script>

View File

@ -13,7 +13,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
/* import-globals-from feature-detect.js */
/* import-globals-from preserve-referer.js */ /* import-globals-from preserve-referer.js */
'use strict'; 'use strict';
@ -131,21 +130,7 @@ chrome.webRequest.onHeadersReceived.addListener(
// Implemented in preserve-referer.js // Implemented in preserve-referer.js
saveReferer(details); saveReferer(details);
// Replace frame with viewer
if (Features.webRequestRedirectUrl) {
return { redirectUrl: viewerUrl, }; return { redirectUrl: viewerUrl, };
}
// Aww.. redirectUrl is not yet supported, so we have to use a different
// method as fallback (Chromium <35).
if (details.frameId === 0) {
// Main frame. Just replace the tab and be done!
chrome.tabs.update(details.tabId, {
url: viewerUrl,
});
return { cancel: true, };
}
console.warn('Child frames are not supported in ancient Chrome builds!');
}, },
{ {
urls: [ urls: [
@ -155,36 +140,12 @@ chrome.webRequest.onHeadersReceived.addListener(
}, },
['blocking', 'responseHeaders']); ['blocking', 'responseHeaders']);
chrome.webRequest.onBeforeRequest.addListener(
function onBeforeRequestForFTP(details) {
if (!Features.extensionSupportsFTP) {
chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequestForFTP);
return;
}
if (isPdfDownloadable(details)) {
return;
}
var viewerUrl = getViewerURL(details.url);
return { redirectUrl: viewerUrl, };
},
{
urls: [
'ftp://*/*.pdf',
'ftp://*/*.PDF'
],
types: ['main_frame', 'sub_frame'],
},
['blocking']);
chrome.webRequest.onBeforeRequest.addListener( chrome.webRequest.onBeforeRequest.addListener(
function(details) { function(details) {
if (isPdfDownloadable(details)) { if (isPdfDownloadable(details)) {
return; return;
} }
// NOTE: The manifest file has declared an empty content script
// at file://*/* to make sure that the viewer can load the PDF file
// through XMLHttpRequest. Necessary to deal with http://crbug.com/302548
var viewerUrl = getViewerURL(details.url); var viewerUrl = getViewerURL(details.url);
return { redirectUrl: viewerUrl, }; return { redirectUrl: viewerUrl, };
@ -192,7 +153,11 @@ chrome.webRequest.onBeforeRequest.addListener(
{ {
urls: [ urls: [
'file://*/*.pdf', 'file://*/*.pdf',
'file://*/*.PDF' 'file://*/*.PDF',
// 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'], types: ['main_frame', 'sub_frame'],
}, },
@ -271,3 +236,10 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
} }
} }
}); });
// Remove keys from storage that were once part of the deleted feature-detect.js
chrome.storage.local.remove([
'featureDetectLastUA',
'webRequestRedirectUrl',
'extensionSupportsFTP',
]);

View File

@ -18,7 +18,6 @@ import { DefaultExternalServices, PDFViewerApplication } from './app';
import { BasePreferences } from './preferences'; import { BasePreferences } from './preferences';
import { DownloadManager } from './download_manager'; import { DownloadManager } from './download_manager';
import { GenericL10n } from './genericl10n'; import { GenericL10n } from './genericl10n';
import { PDFJS } from 'pdfjs-lib';
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('CHROME')) { if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('CHROME')) {
throw new Error('Module "pdfjs-web/chromecom" shall not be used outside ' + throw new Error('Module "pdfjs-web/chromecom" shall not be used outside ' +
@ -65,26 +64,6 @@ let ChromeCom = {
file = file.replace(/^drive:/i, file = file.replace(/^drive:/i,
'filesystem:' + location.origin + '/external/'); 'filesystem:' + location.origin + '/external/');
if (/^filesystem:/.test(file) && !PDFJS.disableWorker) {
// The security origin of filesystem:-URLs are not preserved when the
// URL is passed to a Web worker, (http://crbug.com/362061), so we have
// to create an intermediate blob:-URL as a work-around.
let resolveLocalFileSystemURL = window.resolveLocalFileSystemURL ||
window.webkitResolveLocalFileSystemURL;
resolveLocalFileSystemURL(file, function onResolvedFSURL(fileEntry) {
fileEntry.file(function(fileObject) {
let blobUrl = URL.createObjectURL(fileObject);
callback(blobUrl, fileObject.size);
});
}, function onFileSystemError(error) {
// This should not happen. When it happens, just fall back to the
// usual way of getting the File's data (via the Web worker).
console.warn('Cannot resolve file ' + file + ', ' + error.name + ' ' +
error.message);
callback(file);
});
return;
}
if (/^https?:/.test(file)) { if (/^https?:/.test(file)) {
// Assumption: The file being opened is the file that was requested. // Assumption: The file being opened is the file that was requested.
// There is no UI to input a different URL, so this assumption will hold // There is no UI to input a different URL, so this assumption will hold

View File

@ -281,23 +281,10 @@ class PDFHistory {
this._updateInternalState(destination, newState.uid); this._updateInternalState(destination, newState.uid);
if (shouldReplace) { if (shouldReplace) {
if (typeof PDFJSDev !== 'undefined' &&
PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
// Providing the third argument causes a SecurityError for file:// URLs.
window.history.replaceState(newState, ''); window.history.replaceState(newState, '');
} else {
window.history.replaceState(newState, '', document.URL);
}
} else { } else {
this._maxUid = this._uid; this._maxUid = this._uid;
if (typeof PDFJSDev !== 'undefined' &&
PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
// Providing the third argument causes a SecurityError for file:// URLs.
window.history.pushState(newState, ''); window.history.pushState(newState, '');
} else {
window.history.pushState(newState, '', document.URL);
}
} }
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME') && if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME') &&