Browse Source

Merge pull request #4426 from timvandermeij/extensions-chromium-syntax

Making extensions/chromium/*.js adhere to the style guide
Jonas Jenwald 11 years ago
parent
commit
ba8a59034c
  1. 126
      extensions/chromium/chrome.tabs.executeScriptInFrame.js
  2. 1
      extensions/chromium/extension-router.js
  3. 137
      extensions/chromium/pdfHandler-v2.js
  4. 24
      extensions/chromium/pdfHandler-vcros.js
  5. 23
      extensions/chromium/pdfHandler.js

126
extensions/chromium/chrome.tabs.executeScriptInFrame.js

@ -18,32 +18,36 @@
* In addition, the following field must also be set in manifest.json: * In addition, the following field must also be set in manifest.json:
* "web_accessible_resources": ["getFrameId"] * "web_accessible_resources": ["getFrameId"]
*/ */
/* globals chrome, console */
(function() { (function() {
/* jshint browser:true, maxlen:100 */ /* jshint browser:true, maxlen:100 */
/* globals chrome, console */ 'use strict';
'use strict';
chrome.tabs.executeScriptInFrame = executeScript;
// This URL is used to communicate the frameId. The resource is never visited, so it should chrome.tabs.executeScriptInFrame = executeScript;
// be a non-existent location. Do not use *, ", ' or line breaks in the file name.
var URL_WHAT_IS_MY_FRAME_ID = chrome.extension.getURL('getFrameId');
// The callback will be called within ... ms:
// Don't set a too low value.
var MAXIMUM_RESPONSE_TIME_MS = 1000;
// Callbacks are stored here until they're invoked. // This URL is used to communicate the frameId. The resource is never
// Key = dummyUrl, value = callback function // visited, so it should be a non-existent location. Do not use *, ", '
var callbacks = {}; // or line breaks in the file name.
var URL_WHAT_IS_MY_FRAME_ID = chrome.extension.getURL('getFrameId');
// The callback will be called within ... ms:
// Don't set a too low value.
var MAXIMUM_RESPONSE_TIME_MS = 1000;
chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) { // Callbacks are stored here until they're invoked.
// Key = dummyUrl, value = callback function
var callbacks = {};
chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) {
// Positive integer frameId >= 0 // Positive integer frameId >= 0
// Since an image is used as a data transport, we add 1 to get a non-zero width. // Since an image is used as a data transport, we add 1 to get a
// non-zero width.
var frameId = details.frameId + 1; var frameId = details.frameId + 1;
// Assume that the frameId fits in three bytes - which is a very reasonable assumption. // Assume that the frameId fits in three bytes - which is a very
// reasonable assumption.
var width = String.fromCharCode(frameId & 0xFF, (frameId >> 8) & 0xFF); var width = String.fromCharCode(frameId & 0xFF, (frameId >> 8) & 0xFF);
// When frameId > 0xFFFF, use the height to convey the additional information. // When frameId > 0xFFFF, use the height to convey the additional
// Again, add 1 to make sure that the height is non-zero. // information. Again, add 1 to make sure that the height is non-zero.
var height = String.fromCharCode((frameId >> 16) + 1, 0); var height = String.fromCharCode((frameId >> 16) + 1, 0);
// Convert data to base64 to avoid loss of bytes // Convert data to base64 to avoid loss of bytes
var image = 'data:image/gif;base64,' + btoa( var image = 'data:image/gif;base64,' + btoa(
@ -77,12 +81,13 @@ chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) {
'\x3b' '\x3b'
); );
return {redirectUrl: image}; return {redirectUrl: image};
}, { }, {
urls: [URL_WHAT_IS_MY_FRAME_ID + '*'], urls: [URL_WHAT_IS_MY_FRAME_ID + '*'],
types: ['image'] types: ['image']
}, ['blocking']); }, ['blocking']);
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { chrome.runtime.onMessage.addListener(function(message, sender,
sendResponse) {
if (message && message.executeScriptCallback) { if (message && message.executeScriptCallback) {
var callback = callbacks[message.identifier]; var callback = callbacks[message.identifier];
if (callback) { if (callback) {
@ -91,40 +96,52 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
return; return;
} }
delete callbacks[message.identifier]; delete callbacks[message.identifier];
// Result within an array to be consistent with the chrome.tabs.executeScript API. // Result within an array to be consistent with the
// chrome.tabs.executeScript API.
callback([message.evalResult]); callback([message.evalResult]);
} else { } else {
console.warn('Callback not found for response in tab ' + sender.tab.id); console.warn('Callback not found for response in tab ' +
sender.tab.id);
} }
} }
}); });
/** /**
* Execute content script in a specific frame. * Execute content script in a specific frame.
* *
* @param tabId {integer} required * @param tabId {integer} required
* @param details.frameId {integer} required * @param details.frameId {integer} required
* @param details.code {string} Code or file is required (not both) * @param details.code {string} Code or file is required (not both)
* @param details.file {string} Code or file is required (not both) * @param details.file {string} Code or file is required (not both)
* @param details.runAt {optional string} One of "document_start", "document_end", "document_idle" * @param details.runAt {optional string} One of "document_start",
* @param callback {optional function(optional array of any result)} When an error occurs, result * "document_end", "document_idle"
* @param callback {optional function(optional result array)} When an error
* occurs, result
* is not set. * is not set.
*/ */
function executeScript(tabId, details, callback) { function executeScript(tabId, details, callback) {
console.assert(typeof details === 'object', 'details must be an object (argument 0)'); console.assert(typeof details === 'object',
'details must be an object (argument 0)');
var frameId = details.frameId; var frameId = details.frameId;
console.assert(typeof tabId === 'number', 'details.tabId must be a number'); console.assert(typeof tabId === 'number',
console.assert(typeof frameId === 'number', 'details.frameId must be a number'); 'details.tabId must be a number');
var sourceType = 'code' in details ? 'code' : 'file'; console.assert(typeof frameId === 'number',
'details.frameId must be a number');
var sourceType = ('code' in details ? 'code' : 'file');
console.assert(sourceType in details, 'No source code or file specified'); console.assert(sourceType in details, 'No source code or file specified');
var sourceValue = details[sourceType]; var sourceValue = details[sourceType];
console.assert(typeof sourceValue === 'string', 'details.' + sourceType + ' must be a string'); console.assert(typeof sourceValue === 'string',
'details.' + sourceType + ' must be a string');
var runAt = details.runAt; var runAt = details.runAt;
if (!callback) callback = function() {/* no-op*/}; if (!callback) {
console.assert(typeof callback === 'function', 'callback must be a function'); callback = function() {/* no-op*/};
}
console.assert(typeof callback === 'function',
'callback must be a function');
if (frameId === 0) { if (frameId === 0) {
// No need for heavy lifting if we want to inject the script in the main frame // No need for heavy lifting if we want to inject the script
// in the main frame
var injectDetails = { var injectDetails = {
allFrames: false, allFrames: false,
runAt: runAt runAt: runAt
@ -149,7 +166,8 @@ function executeScript(tabId, details, callback) {
x.onerror = function executeScriptResourceFetchError() { x.onerror = function executeScriptResourceFetchError() {
var message = 'Failed to load file: "' + sourceValue + '".'; var message = 'Failed to load file: "' + sourceValue + '".';
console.error('executeScript: ' + message); console.error('executeScript: ' + message);
chrome.runtime.lastError = chrome.extension.lastError = { message: message }; chrome.runtime.lastError = chrome.extension.lastError =
{ message: message };
try { try {
callback(); callback();
} finally { } finally {
@ -160,7 +178,6 @@ function executeScript(tabId, details, callback) {
})(); })();
} }
function executeScriptInFrame() { function executeScriptInFrame() {
callbacks[identifier] = callback; callbacks[identifier] = callback;
chrome.tabs.executeScript(tabId, { chrome.tabs.executeScript(tabId, {
@ -173,7 +190,8 @@ function executeScript(tabId, details, callback) {
runAt: 'document_start' runAt: 'document_start'
}, function(results) { }, function(results) {
if (results) { if (results) {
callback.timer = setTimeout(executeScriptTimedOut, MAXIMUM_RESPONSE_TIME_MS); callback.timer = setTimeout(executeScriptTimedOut,
MAXIMUM_RESPONSE_TIME_MS);
} else { } else {
// Failed :( // Failed :(
delete callbacks[identifier]; delete callbacks[identifier];
@ -181,32 +199,37 @@ function executeScript(tabId, details, callback) {
} }
}); });
} }
function executeScriptTimedOut() { function executeScriptTimedOut() {
var callback = callbacks[identifier]; var callback = callbacks[identifier];
if (!callback) { if (!callback) {
return; return;
} }
delete callbacks[identifier]; delete callbacks[identifier];
var message = 'Failed to execute script: Frame ' + frameId + ' not found in tab ' + tabId; var message = 'Failed to execute script: Frame ' + frameId +
' not found in tab ' + tabId;
console.error('executeScript: ' + message); console.error('executeScript: ' + message);
chrome.runtime.lastError = chrome.extension.lastError = { message: message }; chrome.runtime.lastError = chrome.extension.lastError =
{ message: message };
try { try {
callback(); callback();
} finally { } finally {
chrome.runtime.lastError = chrome.extension.lastError = undefined; chrome.runtime.lastError = chrome.extension.lastError = undefined;
} }
} }
} }
/** /**
* Code executed as a content script. * Code executed as a content script.
*/ */
var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) { var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId,
code) {
var i; var i;
if ('__executeScript_frameId__' in window) { if ('__executeScript_frameId__' in window) {
evalAsContentScript(); evalAsContentScript();
} else { } else {
// Do NOT use new Image(), because of http://crbug.com/245296 in Chrome 27-29 // Do NOT use new Image() because of http://crbug.com/245296
// in Chrome 27-29
i = window.document.createElement('img'); i = window.document.createElement('img');
i.onload = function() { i.onload = function() {
window.__executeScript_frameId__ = (this.naturalWidth - 1) + window.__executeScript_frameId__ = (this.naturalWidth - 1) +
@ -215,7 +238,8 @@ var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) {
}; };
// Trigger webRequest event to get frameId // Trigger webRequest event to get frameId
// (append extra characters to bust the cache) // (append extra characters to bust the cache)
i.src = 'URL_WHAT_IS_MY_FRAME_ID?' + Math.random().toString(36).slice(-6); i.src = 'URL_WHAT_IS_MY_FRAME_ID?' +
Math.random().toString(36).slice(-6);
} }
for (i = 0 ; i < window.frames.length; ++i) { for (i = 0 ; i < window.frames.length; ++i) {
@ -226,17 +250,18 @@ var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) {
checkFrame(frame, identifier, frameId, code); checkFrame(frame, identifier, frameId, code);
} }
} catch (e) { } catch (e) {
// blocked by same origin policy, so it's not a javascript: / about:blank // blocked by same origin policy, so it's not a javascript:/about:blank
// URL. chrome.tabs.executeScript will run the script for the frame. // URL. chrome.tabs.executeScript will run the script for the frame.
} }
} }
function evalAsContentScript() { function evalAsContentScript() {
if (window.__executeScript_frameId__ !== frameId) { if (window.__executeScript_frameId__ !== frameId) {
return; return;
} }
// Send an early message to make sure that any blocking code // Send an early message to make sure that any blocking code
// in the evaluated code does not cause the time-out in the background page // in the evaluated code does not cause the time-out in the background
// to be triggered // page to be triggered
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
executeScriptCallback: true, executeScriptCallback: true,
hello: true, hello: true,
@ -254,6 +279,5 @@ var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) {
}); });
} }
} }
}.toString().replace('URL_WHAT_IS_MY_FRAME_ID', URL_WHAT_IS_MY_FRAME_ID); }.toString().replace('URL_WHAT_IS_MY_FRAME_ID', URL_WHAT_IS_MY_FRAME_ID);
})(); })();

1
extensions/chromium/extension-router.js

@ -18,6 +18,7 @@ limitations under the License.
/* globals chrome */ /* globals chrome */
'use strict'; 'use strict';
(function ExtensionRouterClosure() { (function ExtensionRouterClosure() {
var VIEWER_URL = chrome.extension.getURL('content/web/viewer.html'); var VIEWER_URL = chrome.extension.getURL('content/web/viewer.html');
var CRX_BASE_URL = chrome.extension.getURL('/'); var CRX_BASE_URL = chrome.extension.getURL('/');

137
extensions/chromium/pdfHandler-v2.js

@ -18,48 +18,51 @@ limitations under the License.
/* globals chrome, URL, getViewerURL */ /* globals chrome, URL, getViewerURL */
(function() { (function() {
'use strict'; 'use strict';
if (!chrome.streamsPrivate) { if (!chrome.streamsPrivate) {
// Aww, PDF.js is still not whitelisted... See http://crbug.com/326949 // Aww, PDF.js is still not whitelisted... See http://crbug.com/326949
console.warn('streamsPrivate not available, PDF from FTP or POST ' + console.warn('streamsPrivate not available, PDF from FTP or POST ' +
'requests will not be displayed using this extension! ' + 'requests will not be displayed using this extension! ' +
'See http://crbug.com/326949'); 'See http://crbug.com/326949');
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { chrome.runtime.onMessage.addListener(function(message, sender,
sendResponse) {
if (message && message.action === 'getPDFStream') { if (message && message.action === 'getPDFStream') {
sendResponse(); sendResponse();
} }
}); });
return; return;
} }
// //
// Stream URL storage manager // Stream URL storage manager
// //
// Hash map of "<tab id>": { "<pdf url>": ["<stream url>", ...], ... } // Hash map of "<tab id>": { "<pdf url>": ["<stream url>", ...], ... }
var urlToStream = {}; var urlToStream = {};
chrome.streamsPrivate.onExecuteMimeTypeHandler.addListener(handleStream); chrome.streamsPrivate.onExecuteMimeTypeHandler.addListener(handleStream);
// Chrome before 27 does not support tabIds on stream events. // Chrome before 27 does not support tabIds on stream events.
var streamSupportsTabId = true; var streamSupportsTabId = true;
// "tabId" used for Chrome before 27. // "tabId" used for Chrome before 27.
var STREAM_NO_TABID = 0; var STREAM_NO_TABID = 0;
function hasStream(tabId, pdfUrl) { function hasStream(tabId, pdfUrl) {
var streams = urlToStream[streamSupportsTabId ? tabId : STREAM_NO_TABID]; var streams = urlToStream[streamSupportsTabId ? tabId : STREAM_NO_TABID];
return streams && streams[pdfUrl] && streams[pdfUrl].length > 0; return (streams && streams[pdfUrl] && streams[pdfUrl].length > 0);
} }
/** /**
* Get stream URL for a given tabId and PDF url. The retrieved stream URL * Get stream URL for a given tabId and PDF url. The retrieved stream URL
* will be removed from the list. * will be removed from the list.
* @return {object} An object with property url (= blob:-URL) and * @return {object} An object with property url (= blob:-URL) and
* property contentLength (= expected size) * property contentLength (= expected size)
*/ */
function getStream(tabId, pdfUrl) { function getStream(tabId, pdfUrl) {
if (!streamSupportsTabId) tabId = STREAM_NO_TABID; if (!streamSupportsTabId) {
tabId = STREAM_NO_TABID;
}
if (hasStream(tabId, pdfUrl)) { if (hasStream(tabId, pdfUrl)) {
var streamInfo = urlToStream[tabId][pdfUrl].shift(); var streamInfo = urlToStream[tabId][pdfUrl].shift();
if (urlToStream[tabId][pdfUrl].length === 0) { if (urlToStream[tabId][pdfUrl].length === 0) {
@ -70,22 +73,26 @@ function getStream(tabId, pdfUrl) {
} }
return streamInfo; return streamInfo;
} }
} }
function setStream(tabId, pdfUrl, streamUrl, expectedSize) { function setStream(tabId, pdfUrl, streamUrl, expectedSize) {
tabId = tabId || STREAM_NO_TABID; tabId = tabId || STREAM_NO_TABID;
if (!urlToStream[tabId]) urlToStream[tabId] = {}; if (!urlToStream[tabId]) {
if (!urlToStream[tabId][pdfUrl]) urlToStream[tabId][pdfUrl] = []; urlToStream[tabId] = {};
}
if (!urlToStream[tabId][pdfUrl]) {
urlToStream[tabId][pdfUrl] = [];
}
urlToStream[tabId][pdfUrl].push({ urlToStream[tabId][pdfUrl].push({
streamUrl: streamUrl, streamUrl: streamUrl,
contentLength: expectedSize contentLength: expectedSize
}); });
} }
// http://crbug.com/276898 - the onExecuteMimeTypeHandler event is sometimes // http://crbug.com/276898 - the onExecuteMimeTypeHandler event is sometimes
// dispatched in the wrong incognito profile. To work around the bug, transfer // dispatched in the wrong incognito profile. To work around the bug, transfer
// the stream information from the incognito session when the bug is detected. // the stream information from the incognito session when the bug is detected.
function transferStreamToIncognitoProfile(tabId, pdfUrl) { function transferStreamToIncognitoProfile(tabId, pdfUrl) {
if (chrome.extension.inIncognitoContext) { if (chrome.extension.inIncognitoContext) {
console.log('Already within incognito profile. Aborted stream transfer.'); console.log('Already within incognito profile. Aborted stream transfer.');
return; return;
@ -114,30 +121,38 @@ function transferStreamToIncognitoProfile(tabId, pdfUrl) {
} }
}); });
}); });
} }
if (chrome.extension.inIncognitoContext) {
if (chrome.extension.inIncognitoContext) {
var importStream = function(itemId, streamInfo) { var importStream = function(itemId, streamInfo) {
if (itemId.lastIndexOf('streamInfo:', 0) !== 0) return; if (itemId.lastIndexOf('streamInfo:', 0) !== 0) {
console.log('Importing stream info from non-incognito profile', streamInfo); return;
handleStream('', streamInfo.pdfUrl, streamInfo.streamUrl, streamInfo.tabId, }
streamInfo.contentLength); console.log('Importing stream info from non-incognito profile',
streamInfo);
handleStream('', streamInfo.pdfUrl, streamInfo.streamUrl,
streamInfo.tabId, streamInfo.contentLength);
chrome.storage.local.remove(itemId); chrome.storage.local.remove(itemId);
}; };
var handleStorageItems = function(items) { var handleStorageItems = function(items) {
Object.keys(items).forEach(function(itemId) { Object.keys(items).forEach(function(itemId) {
var item = items[itemId]; var item = items[itemId];
if (item.oldValue && !item.newValue) return; // storage remove event if (item.oldValue && !item.newValue) {
if (item.newValue) item = item.newValue; // storage setter event return; // storage remove event
}
if (item.newValue) {
item = item.newValue; // storage setter event
}
importStream(itemId, item); importStream(itemId, item);
}); });
}; };
// Parse information that was set before the event pages were ready. // Parse information that was set before the event pages were ready.
chrome.storage.local.get(null, handleStorageItems); chrome.storage.local.get(null, handleStorageItems);
chrome.storage.onChanged.addListener(handleStorageItems); chrome.storage.onChanged.addListener(handleStorageItems);
} }
// End of work-around for crbug 276898 // End of work-around for crbug 276898
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message && message.action === 'getPDFStream') { if (message && message.action === 'getPDFStream') {
var pdfUrl = message.data; var pdfUrl = message.data;
var streamInfo = getStream(sender.tab.id, pdfUrl) || {}; var streamInfo = getStream(sender.tab.id, pdfUrl) || {};
@ -146,13 +161,13 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
contentLength: streamInfo.contentLength contentLength: streamInfo.contentLength
}); });
} }
}); });
// //
// PDF detection and activation of PDF viewer. // PDF detection and activation of PDF viewer.
// //
/** /**
* Callback for when we receive a stream * Callback for when we receive a stream
* *
* @param mimeType {string} The mime type of the incoming stream * @param mimeType {string} The mime type of the incoming stream
@ -163,7 +178,7 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
* @param expectedSize {number} The expected content length of the stream. * @param expectedSize {number} The expected content length of the stream.
* (added in Chrome 29, http://crbug.com/230346) * (added in Chrome 29, http://crbug.com/230346)
*/ */
function handleStream(mimeType, pdfUrl, streamUrl, tabId, expectedSize) { function handleStream(mimeType, pdfUrl, streamUrl, tabId, expectedSize) {
console.log('Intercepted ' + mimeType + ' in tab ' + tabId + ' with URL ' + console.log('Intercepted ' + mimeType + ' in tab ' + tabId + ' with URL ' +
pdfUrl + '\nAvailable as: ' + streamUrl); pdfUrl + '\nAvailable as: ' + streamUrl);
streamSupportsTabId = typeof tabId === 'number'; streamSupportsTabId = typeof tabId === 'number';
@ -184,7 +199,7 @@ function handleStream(mimeType, pdfUrl, streamUrl, tabId, expectedSize) {
}, function(details) { }, function(details) {
if (details) { if (details) {
details = details.filter(function(frame) { details = details.filter(function(frame) {
return frame.url === pdfUrl; return (frame.url === pdfUrl);
}); });
if (details.length > 0) { if (details.length > 0) {
if (details.length !== 1) { if (details.length !== 1) {
@ -212,21 +227,22 @@ function handleStream(mimeType, pdfUrl, streamUrl, tabId, expectedSize) {
transferStreamToIncognitoProfile(tabId, pdfUrl); transferStreamToIncognitoProfile(tabId, pdfUrl);
} }
}); });
} }
/** /**
* This method is called when the chrome.streamsPrivate API has intercepted * This method is called when the chrome.streamsPrivate API has intercepted
* the PDF stream. This method detects such streams, finds the frame where * the PDF stream. This method detects such streams, finds the frame where
* the request was made, and loads the viewer in that frame. * the request was made, and loads the viewer in that frame.
* *
* @param details {object} * @param details {object}
* @param details.tabId {number} The ID of the tab * @param details.tabId {number} The ID of the tab
* @param details.url {string} The URL being navigated when the error occurred. * @param details.url {string} The URL being navigated when the error
* @param details.frameId {number} 0 indicates the navigation happens in the tab * occurred.
* content window; a positive value indicates * @param details.frameId {number} 0 indicates the navigation happens in
* navigation in a subframe. * the tab content window; a positive value
* indicates navigation in a subframe.
*/ */
function handleWebNavigation(details) { function handleWebNavigation(details) {
var tabId = details.tabId; var tabId = details.tabId;
var frameId = details.frameId; var frameId = details.frameId;
var pdfUrl = details.url; var pdfUrl = details.url;
@ -256,6 +272,5 @@ function handleWebNavigation(details) {
} }
}); });
} }
} }
})(); })();

24
extensions/chromium/pdfHandler-vcros.js

@ -18,21 +18,22 @@ limitations under the License.
/* globals chrome, getViewerURL */ /* globals chrome, getViewerURL */
(function() { (function() {
'use strict'; 'use strict';
if (!chrome.fileBrowserHandler) { if (!chrome.fileBrowserHandler) {
// Not on Chromium OS, bail out // Not on Chromium OS, bail out
return; return;
} }
chrome.fileBrowserHandler.onExecute.addListener(onExecuteFileBrowserHandler); chrome.fileBrowserHandler.onExecute.addListener(onExecuteFileBrowserHandler);
/** /**
* Invoked when "Open with PDF Viewer" is chosen in the File browser. * Invoked when "Open with PDF Viewer" is chosen in the File browser.
* *
* @param {String} id File browser action ID as specified in manifest.json * @param {String} id File browser action ID as specified in
* manifest.json
* @param {Object} details Object of type FileHandlerExecuteEventDetails * @param {Object} details Object of type FileHandlerExecuteEventDetails
*/ */
function onExecuteFileBrowserHandler(id, details) { function onExecuteFileBrowserHandler(id, details) {
if (id !== 'open-as-pdf') { if (id !== 'open-as-pdf') {
return; return;
} }
@ -54,15 +55,15 @@ function onExecuteFileBrowserHandler(id, details) {
openViewer(windowId, fileEntries); openViewer(windowId, fileEntries);
}); });
} }
} }
/** /**
* Open the PDF Viewer for the given list of PDF files. * Open the PDF Viewer for the given list of PDF files.
* *
* @param {number} windowId * @param {number} windowId
* @param {Array} fileEntries List of Entry objects (HTML5 FileSystem API) * @param {Array} fileEntries List of Entry objects (HTML5 FileSystem API)
*/ */
function openViewer(windowId, fileEntries) { function openViewer(windowId, fileEntries) {
if (!fileEntries.length) { if (!fileEntries.length) {
return; return;
} }
@ -90,6 +91,5 @@ function openViewer(windowId, fileEntries) {
openViewer(chromeWindow.id, fileEntries); openViewer(chromeWindow.id, fileEntries);
}); });
} }
} }
})(); })();

23
extensions/chromium/pdfHandler.js

@ -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);
} }
} }
@ -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

Loading…
Cancel
Save