Making extensions/chromium/pdfHandler.js adhere to the style guide

This commit is contained in:
Tim van der Meij 2014-03-09 23:12:12 +01:00
parent ce6e269d02
commit c29350219e

View File

@ -31,15 +31,17 @@ function getViewerURL(pdf_url) {
* @return {boolean} True if the PDF file should be downloaded. * @return {boolean} True if the PDF file should be downloaded.
*/ */
function isPdfDownloadable(details) { function isPdfDownloadable(details) {
if (details.url.indexOf('pdfjs.action=download') >= 0) if (details.url.indexOf('pdfjs.action=download') >= 0) {
return true; return true;
}
// Display the PDF viewer regardless of the Content-Disposition header // Display the PDF viewer regardless of the Content-Disposition header
// if the file is displayed in the main frame. // if the file is displayed in the main frame.
if (details.type == 'main_frame') if (details.type == 'main_frame') {
return false; return false;
var cdHeader = details.responseHeaders && }
getHeaderFromHeaders(details.responseHeaders, 'content-disposition'); var cdHeader = (details.responseHeaders &&
return cdHeader && /^attachment/i.test(cdHeader.value); getHeaderFromHeaders(details.responseHeaders, 'content-disposition'));
return (cdHeader && /^attachment/i.test(cdHeader.value));
} }
/** /**
@ -67,9 +69,9 @@ function isPdfFile(details) {
var header = getHeaderFromHeaders(details.responseHeaders, 'content-type'); var header = getHeaderFromHeaders(details.responseHeaders, 'content-type');
if (header) { if (header) {
var headerValue = header.value.toLowerCase().split(';',1)[0].trim(); var headerValue = header.value.toLowerCase().split(';',1)[0].trim();
return headerValue === 'application/pdf' || return (headerValue === 'application/pdf' ||
headerValue === 'application/octet-stream' && headerValue === 'application/octet-stream' &&
details.url.toLowerCase().indexOf('.pdf') > 0; details.url.toLowerCase().indexOf('.pdf') > 0);
} }
} }
@ -83,16 +85,16 @@ function isPdfFile(details) {
* have been modified, undefined otherwise. * have been modified, undefined otherwise.
*/ */
function getHeadersWithContentDispositionAttachment(details) { function getHeadersWithContentDispositionAttachment(details) {
var headers = details.responseHeaders; var headers = details.responseHeaders;
var cdHeader = getHeaderFromHeaders(headers, 'content-disposition'); var cdHeader = getHeaderFromHeaders(headers, 'content-disposition');
if (!cdHeader) { if (!cdHeader) {
cdHeader = {name: 'Content-Disposition'}; cdHeader = {name: 'Content-Disposition'};
headers.push(cdHeader); headers.push(cdHeader);
} }
if (!/^attachment/i.test(cdHeader.value)) { if (!/^attachment/i.test(cdHeader.value)) {
cdHeader.value = 'attachment' + cdHeader.value.replace(/^[^;]+/i, ''); cdHeader.value = 'attachment' + cdHeader.value.replace(/^[^;]+/i, '');
return { responseHeaders: headers }; return { responseHeaders: headers };
} }
} }
chrome.webRequest.onHeadersReceived.addListener( chrome.webRequest.onHeadersReceived.addListener(
@ -101,9 +103,9 @@ chrome.webRequest.onHeadersReceived.addListener(
// Don't intercept POST requests until http://crbug.com/104058 is fixed. // Don't intercept POST requests until http://crbug.com/104058 is fixed.
return; return;
} }
if (!isPdfFile(details)) if (!isPdfFile(details)) {
return; return;
}
if (isPdfDownloadable(details)) { if (isPdfDownloadable(details)) {
// Force download by ensuring that Content-Disposition: attachment is set // Force download by ensuring that Content-Disposition: attachment is set
return getHeadersWithContentDispositionAttachment(details); return getHeadersWithContentDispositionAttachment(details);
@ -172,8 +174,9 @@ chrome.webRequest.onHeadersReceived.addListener(
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 // NOTE: The manifest file has declared an empty content script
// at file://*/* to make sure that the viewer can load the PDF file // at file://*/* to make sure that the viewer can load the PDF file