Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Update pdf.js so it works without chrome.storage
Browse files Browse the repository at this point in the history
Fix #2619

Auditors: @bbondy
  • Loading branch information
diracdeltas committed Jul 26, 2016
1 parent d9bf739 commit 4ddbf6b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 113 deletions.
4 changes: 2 additions & 2 deletions app/extensions/pdfjs/content/build/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ factory((root.pdfjsDistBuildPdf = {}));
// Use strict in our context only - users might not want it
'use strict';

var pdfjsVersion = '1.5.361';
var pdfjsBuild = '452c06e';
var pdfjsVersion = '1.5.364';
var pdfjsBuild = '81cd66c';

var pdfjsFilePath =
typeof document !== 'undefined' && document.currentScript ?
Expand Down
4 changes: 2 additions & 2 deletions app/extensions/pdfjs/content/build/pdf.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ factory((root.pdfjsDistBuildPdfWorker = {}));
// Use strict in our context only - users might not want it
'use strict';

var pdfjsVersion = '1.5.361';
var pdfjsBuild = '452c06e';
var pdfjsVersion = '1.5.364';
var pdfjsBuild = '81cd66c';

var pdfjsFilePath =
typeof document !== 'undefined' && document.currentScript ?
Expand Down
2 changes: 1 addition & 1 deletion app/extensions/pdfjs/content/web/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9403,7 +9403,7 @@ exports.DefaultExernalServices = DefaultExernalServices;
}

function isAllowedFileSchemeAccess(callback) {
callback(true)
callback(true);
}

function isRuntimeAvailable() {
Expand Down
104 changes: 2 additions & 102 deletions app/extensions/pdfjs/feature-detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,109 +18,9 @@ limitations under the License.
'use strict';

var Features = {
featureDetectLastUA: '',
featureDetectLastUA: navigator.userAgent,
// Whether ftp: in XMLHttpRequest is allowed
extensionSupportsFTP: false,
// Whether redirectUrl at onHeadersReceived is supported.
webRequestRedirectUrl: false,
webRequestRedirectUrl: true,
};

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();
}
7 changes: 1 addition & 6 deletions app/extensions/pdfjs/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "PDF Viewer",
"version": "1.5.361",
"version": "1.5.364",
"description": "Uses HTML5 to display PDF files directly in the browser.",
"icons": {
"128": "icon128.png",
Expand Down Expand Up @@ -39,11 +39,6 @@
"storage": {
"managed_schema": "preferences_schema.json"
},
"options_ui": {
"page": "options/options.html",
"chrome_style": true
},
"options_page": "options/options.html",
"background": {
"page": "pdfHandler.html"
},
Expand Down

1 comment on commit 4ddbf6b

@bbondy
Copy link
Member

@bbondy bbondy commented on 4ddbf6b Jul 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

Please sign in to comment.