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. 346
      extensions/chromium/chrome.tabs.executeScriptInFrame.js
  2. 11
      extensions/chromium/extension-router.js
  3. 433
      extensions/chromium/pdfHandler-v2.js
  4. 134
      extensions/chromium/pdfHandler-vcros.js
  5. 45
      extensions/chromium/pdfHandler.js

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

@ -5,7 +5,7 @@
* *
* Implements the chrome.tabs.executeScriptInFrame API. * Implements the chrome.tabs.executeScriptInFrame API.
* This API is similar to the chrome.tabs.executeScript method, except * This API is similar to the chrome.tabs.executeScript method, except
* that it also recognizes the "frameId" property. * that it also recognizes the "frameId" property.
* This frameId can be obtained through the webNavigation or webRequest API. * This frameId can be obtained through the webNavigation or webRequest API.
* *
* When an error occurs, chrome.runtime.lastError is set. * When an error occurs, chrome.runtime.lastError is set.
@ -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; chrome.tabs.executeScriptInFrame = executeScript;
// This URL is used to communicate the frameId. The resource is never visited, so it should // This URL is used to communicate the frameId. The resource is never
// be a non-existent location. Do not use *, ", ' or line breaks in the file name. // visited, so it should be a non-existent location. Do not use *, ", '
var URL_WHAT_IS_MY_FRAME_ID = chrome.extension.getURL('getFrameId'); // or line breaks in the file name.
// The callback will be called within ... ms: var URL_WHAT_IS_MY_FRAME_ID = chrome.extension.getURL('getFrameId');
// Don't set a too low value. // The callback will be called within ... ms:
var MAXIMUM_RESPONSE_TIME_MS = 1000; // Don't set a too low value.
var MAXIMUM_RESPONSE_TIME_MS = 1000;
// Callbacks are stored here until they're invoked. // Callbacks are stored here until they're invoked.
// Key = dummyUrl, value = callback function // Key = dummyUrl, value = callback function
var callbacks = {}; var callbacks = {};
chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) { 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(
@ -76,184 +80,204 @@ chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) {
// GIF trailer // GIF trailer
'\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) {
if (message.hello) { if (message.hello) {
clearTimeout(callback.timer); clearTimeout(callback.timer);
return; return;
}
delete callbacks[message.identifier];
// Result within an array to be consistent with the chrome.tabs.executeScript API.
callback([message.evalResult]);
} else {
console.warn('Callback not found for response in tab ' + sender.tab.id);
} }
delete callbacks[message.identifier];
// Result within an array to be consistent with the
// chrome.tabs.executeScript API.
callback([message.evalResult]);
} else {
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"
* is not set. * @param callback {optional function(optional result array)} When an error
*/ * occurs, result
function executeScript(tabId, details, callback) { * is not set.
console.assert(typeof details === 'object', 'details must be an object (argument 0)'); */
function executeScript(tabId, details, callback) {
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
var injectDetails = { // in the main frame
allFrames: false, var injectDetails = {
runAt: runAt allFrames: false,
}; runAt: runAt
injectDetails[sourceType] = sourceValue; };
chrome.tabs.executeScript(tabId, injectDetails, callback); injectDetails[sourceType] = sourceValue;
return; chrome.tabs.executeScript(tabId, injectDetails, callback);
return;
} }
var identifier = Math.random().toString(36); var identifier = Math.random().toString(36);
if (sourceType === 'code') { if (sourceType === 'code') {
executeScriptInFrame(); executeScriptInFrame();
} else { // sourceType === 'file' } else { // sourceType === 'file'
(function() { (function() {
var x = new XMLHttpRequest(); var x = new XMLHttpRequest();
x.open('GET', chrome.extension.getURL(sourceValue), true); x.open('GET', chrome.extension.getURL(sourceValue), true);
x.onload = function() { x.onload = function() {
sourceValue = x.responseText; sourceValue = x.responseText;
executeScriptInFrame(); executeScriptInFrame();
}; };
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 =
try { { message: message };
callback(); try {
} finally { callback();
chrome.runtime.lastError = chrome.extension.lastError = undefined; } finally {
} chrome.runtime.lastError = chrome.extension.lastError = undefined;
}; }
x.send(); };
})(); x.send();
})();
} }
function executeScriptInFrame() { function executeScriptInFrame() {
callbacks[identifier] = callback; callbacks[identifier] = callback;
chrome.tabs.executeScript(tabId, { chrome.tabs.executeScript(tabId, {
code: '(' + DETECT_FRAME + ')(' + code: '(' + DETECT_FRAME + ')(' +
'window,' + 'window,' +
JSON.stringify(identifier) + ',' + JSON.stringify(identifier) + ',' +
frameId + ',' + frameId + ',' +
JSON.stringify(sourceValue) + ')', JSON.stringify(sourceValue) + ')',
allFrames: true, allFrames: true,
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,
} else { MAXIMUM_RESPONSE_TIME_MS);
// Failed :( } else {
delete callbacks[identifier]; // Failed :(
callback(); delete callbacks[identifier];
} 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 +
console.error('executeScript: ' + message); ' not found in tab ' + tabId;
chrome.runtime.lastError = chrome.extension.lastError = { message: message }; console.error('executeScript: ' + message);
try { chrome.runtime.lastError = chrome.extension.lastError =
callback(); { message: message };
} finally { try {
chrome.runtime.lastError = chrome.extension.lastError = undefined; callback();
} } finally {
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
i = window.document.createElement('img'); // in Chrome 27-29
i.onload = function() { i = window.document.createElement('img');
window.__executeScript_frameId__ = (this.naturalWidth - 1) + i.onload = function() {
(this.naturalHeight - 1 << 16); window.__executeScript_frameId__ = (this.naturalWidth - 1) +
evalAsContentScript(); (this.naturalHeight - 1 << 16);
}; evalAsContentScript();
// Trigger webRequest event to get frameId };
// (append extra characters to bust the cache) // Trigger webRequest event to get frameId
i.src = 'URL_WHAT_IS_MY_FRAME_ID?' + Math.random().toString(36).slice(-6); // (append extra characters to bust the cache)
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) {
try { try {
var frame = window.frames[i]; var frame = window.frames[i];
var scheme = frame.location.protocol; var scheme = frame.location.protocol;
if (scheme !== 'https:' && scheme !== 'http:' && scheme !== 'file:') { if (scheme !== 'https:' && scheme !== 'http:' && scheme !== 'file:') {
checkFrame(frame, identifier, frameId, code); checkFrame(frame, identifier, frameId, code);
}
} catch (e) {
// blocked by same origin policy, so it's not a javascript: / about:blank
// URL. chrome.tabs.executeScript will run the script for the frame.
} }
} catch (e) {
// blocked by same origin policy, so it's not a javascript:/about:blank
// 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({
executeScriptCallback: true,
hello: true,
identifier: identifier
});
var result = null;
try {
// jshint evil:true
result = window.eval(code);
} finally {
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
executeScriptCallback: true, executeScriptCallback: true,
hello: true, evalResult: result,
identifier: identifier identifier: identifier
}); });
var result = null; }
try {
// jshint evil:true
result = window.eval(code);
} finally {
chrome.runtime.sendMessage({
executeScriptCallback: true,
evalResult: result,
identifier: identifier
});
}
} }
}.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);
})(); })();

11
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('/');
@ -102,11 +103,11 @@ limitations under the License.
return { redirectUrl: url }; return { redirectUrl: url };
} }
}, { }, {
types: ['main_frame', 'sub_frame'], types: ['main_frame', 'sub_frame'],
urls: schemes.map(function(scheme) { urls: schemes.map(function(scheme) {
// Format: "chrome-extension://[EXTENSIONID]/<scheme>*" // Format: "chrome-extension://[EXTENSIONID]/<scheme>*"
return CRX_BASE_URL + scheme + '*'; return CRX_BASE_URL + scheme + '*';
}) })
}, ['blocking']); }, ['blocking']);
chrome.runtime.onMessage.addListener(function(message, sender) { chrome.runtime.onMessage.addListener(function(message, sender) {

433
extensions/chromium/pdfHandler-v2.js

@ -18,244 +18,259 @@ 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,
if (message && message.action === 'getPDFStream') { sendResponse) {
sendResponse(); if (message && message.action === 'getPDFStream') {
} 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) {
if (hasStream(tabId, pdfUrl)) { tabId = STREAM_NO_TABID;
var streamInfo = urlToStream[tabId][pdfUrl].shift(); }
if (urlToStream[tabId][pdfUrl].length === 0) { if (hasStream(tabId, pdfUrl)) {
delete urlToStream[tabId][pdfUrl]; var streamInfo = urlToStream[tabId][pdfUrl].shift();
if (Object.keys(urlToStream[tabId]).length === 0) { if (urlToStream[tabId][pdfUrl].length === 0) {
delete urlToStream[tabId]; delete urlToStream[tabId][pdfUrl];
if (Object.keys(urlToStream[tabId]).length === 0) {
delete urlToStream[tabId];
}
} }
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] = {};
urlToStream[tabId][pdfUrl].push({ }
streamUrl: streamUrl, if (!urlToStream[tabId][pdfUrl]) {
contentLength: expectedSize urlToStream[tabId][pdfUrl] = [];
}); }
} urlToStream[tabId][pdfUrl].push({
streamUrl: streamUrl,
// http://crbug.com/276898 - the onExecuteMimeTypeHandler event is sometimes contentLength: expectedSize
// dispatched in the wrong incognito profile. To work around the bug, transfer
// the stream information from the incognito session when the bug is detected.
function transferStreamToIncognitoProfile(tabId, pdfUrl) {
if (chrome.extension.inIncognitoContext) {
console.log('Already within incognito profile. Aborted stream transfer.');
return;
}
var streamInfo = getStream(tabId, pdfUrl);
if (!streamInfo) {
return;
}
console.log('Attempting to transfer stream info to a different profile...');
var itemId = 'streamInfo:' + window.performance.now();
var items = {};
items[itemId] = {
tabId: tabId,
pdfUrl: pdfUrl,
streamUrl: streamInfo.streamUrl,
contentLength: streamInfo.contentLength
};
// The key will be removed whenever an incognito session is started,
// or when an incognito session is active.
chrome.storage.local.set(items, function() {
chrome.extension.isAllowedIncognitoAccess(function(isAllowedAccess) {
if (!isAllowedAccess) {
// If incognito is disabled, forget about the stream.
console.warn('Incognito is disabled, unexpected unknown stream.');
chrome.storage.local.remove(items);
}
});
});
}
if (chrome.extension.inIncognitoContext) {
var importStream = function(itemId, streamInfo) {
if (itemId.lastIndexOf('streamInfo:', 0) !== 0) return;
console.log('Importing stream info from non-incognito profile', streamInfo);
handleStream('', streamInfo.pdfUrl, streamInfo.streamUrl, streamInfo.tabId,
streamInfo.contentLength);
chrome.storage.local.remove(itemId);
};
var handleStorageItems = function(items) {
Object.keys(items).forEach(function(itemId) {
var item = items[itemId];
if (item.oldValue && !item.newValue) return; // storage remove event
if (item.newValue) item = item.newValue; // storage setter event
importStream(itemId, item);
}); });
}; }
// Parse information that was set before the event pages were ready.
chrome.storage.local.get(null, handleStorageItems);
chrome.storage.onChanged.addListener(handleStorageItems);
}
// End of work-around for crbug 276898
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { // http://crbug.com/276898 - the onExecuteMimeTypeHandler event is sometimes
if (message && message.action === 'getPDFStream') { // dispatched in the wrong incognito profile. To work around the bug, transfer
var pdfUrl = message.data; // the stream information from the incognito session when the bug is detected.
var streamInfo = getStream(sender.tab.id, pdfUrl) || {}; function transferStreamToIncognitoProfile(tabId, pdfUrl) {
sendResponse({ if (chrome.extension.inIncognitoContext) {
console.log('Already within incognito profile. Aborted stream transfer.');
return;
}
var streamInfo = getStream(tabId, pdfUrl);
if (!streamInfo) {
return;
}
console.log('Attempting to transfer stream info to a different profile...');
var itemId = 'streamInfo:' + window.performance.now();
var items = {};
items[itemId] = {
tabId: tabId,
pdfUrl: pdfUrl,
streamUrl: streamInfo.streamUrl, streamUrl: streamInfo.streamUrl,
contentLength: streamInfo.contentLength contentLength: streamInfo.contentLength
};
// The key will be removed whenever an incognito session is started,
// or when an incognito session is active.
chrome.storage.local.set(items, function() {
chrome.extension.isAllowedIncognitoAccess(function(isAllowedAccess) {
if (!isAllowedAccess) {
// If incognito is disabled, forget about the stream.
console.warn('Incognito is disabled, unexpected unknown stream.');
chrome.storage.local.remove(items);
}
});
}); });
} }
});
//
// PDF detection and activation of PDF viewer.
//
/**
* Callback for when we receive a stream
*
* @param mimeType {string} The mime type of the incoming stream
* @param pdfUrl {string} The full URL to the file
* @param streamUrl {string} The url pointing to the open stream
* @param tabId {number} The ID of the tab in which the stream has been opened
* (undefined before Chrome 27, http://crbug.com/225605)
* @param expectedSize {number} The expected content length of the stream.
* (added in Chrome 29, http://crbug.com/230346)
*/
function handleStream(mimeType, pdfUrl, streamUrl, tabId, expectedSize) {
console.log('Intercepted ' + mimeType + ' in tab ' + tabId + ' with URL ' +
pdfUrl + '\nAvailable as: ' + streamUrl);
streamSupportsTabId = typeof tabId === 'number';
setStream(tabId, pdfUrl, streamUrl, expectedSize); if (chrome.extension.inIncognitoContext) {
var importStream = function(itemId, streamInfo) {
if (!tabId) { // Chrome doesn't set the tabId before v27 if (itemId.lastIndexOf('streamInfo:', 0) !== 0) {
// PDF.js targets Chrome 28+ because of fatal bugs in incognito mode return;
// for older versions of Chrome. So, don't bother implementing a fallback. }
// For those who are interested, either loop through all tabs, or use the console.log('Importing stream info from non-incognito profile',
// webNavigation.onBeforeNavigate event to map pdfUrls to tab + frame IDs. streamInfo);
return; handleStream('', streamInfo.pdfUrl, streamInfo.streamUrl,
streamInfo.tabId, streamInfo.contentLength);
chrome.storage.local.remove(itemId);
};
var handleStorageItems = function(items) {
Object.keys(items).forEach(function(itemId) {
var item = items[itemId];
if (item.oldValue && !item.newValue) {
return; // storage remove event
}
if (item.newValue) {
item = item.newValue; // storage setter event
}
importStream(itemId, item);
});
};
// Parse information that was set before the event pages were ready.
chrome.storage.local.get(null, handleStorageItems);
chrome.storage.onChanged.addListener(handleStorageItems);
} }
// End of work-around for crbug 276898
// Check if the frame has already been rendered. chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
chrome.webNavigation.getAllFrames({ if (message && message.action === 'getPDFStream') {
tabId: tabId var pdfUrl = message.data;
}, function(details) { var streamInfo = getStream(sender.tab.id, pdfUrl) || {};
if (details) { sendResponse({
details = details.filter(function(frame) { streamUrl: streamInfo.streamUrl,
return frame.url === pdfUrl; contentLength: streamInfo.contentLength
}); });
if (details.length > 0) {
if (details.length !== 1) {
// (Rare case) Multiple frames with same URL.
// TODO(rob): Find a better way to handle this case
// (e.g. open in new tab).
console.warn('More than one frame found for tabId ' + tabId +
' with URL ' + pdfUrl + '. Using first frame.');
}
details = details[0];
details = {
tabId: tabId,
frameId: details.frameId,
url: details.url
};
handleWebNavigation(details);
} else {
console.warn('No webNavigation frames found for tabId ' + tabId);
}
} else {
console.warn('Unable to get frame information for tabId ' + tabId);
// This branch may occur when a new incognito session is launched.
// The event is dispatched in the non-incognito session while it should
// be dispatched in the incognito session. See http://crbug.com/276898
transferStreamToIncognitoProfile(tabId, pdfUrl);
} }
}); });
}
/** //
* This method is called when the chrome.streamsPrivate API has intercepted // PDF detection and activation of PDF viewer.
* the PDF stream. This method detects such streams, finds the frame where //
* the request was made, and loads the viewer in that frame.
*
* @param details {object}
* @param details.tabId {number} The ID of the tab
* @param details.url {string} The URL being navigated when the error occurred.
* @param details.frameId {number} 0 indicates the navigation happens in the tab
* content window; a positive value indicates
* navigation in a subframe.
*/
function handleWebNavigation(details) {
var tabId = details.tabId;
var frameId = details.frameId;
var pdfUrl = details.url;
if (!hasStream(tabId, pdfUrl)) { /**
console.log('No PDF stream found in tab ' + tabId + ' for ' + pdfUrl); * Callback for when we receive a stream
return; *
} * @param mimeType {string} The mime type of the incoming stream
* @param pdfUrl {string} The full URL to the file
* @param streamUrl {string} The url pointing to the open stream
* @param tabId {number} The ID of the tab in which the stream has been opened
* (undefined before Chrome 27, http://crbug.com/225605)
* @param expectedSize {number} The expected content length of the stream.
* (added in Chrome 29, http://crbug.com/230346)
*/
function handleStream(mimeType, pdfUrl, streamUrl, tabId, expectedSize) {
console.log('Intercepted ' + mimeType + ' in tab ' + tabId + ' with URL ' +
pdfUrl + '\nAvailable as: ' + streamUrl);
streamSupportsTabId = typeof tabId === 'number';
var viewerUrl = getViewerURL(pdfUrl); setStream(tabId, pdfUrl, streamUrl, expectedSize);
if (frameId === 0) { // Main frame if (!tabId) { // Chrome doesn't set the tabId before v27
console.log('Going to render PDF Viewer in main frame for ' + pdfUrl); // PDF.js targets Chrome 28+ because of fatal bugs in incognito mode
chrome.tabs.update(tabId, { // for older versions of Chrome. So, don't bother implementing a fallback.
url: viewerUrl // For those who are interested, either loop through all tabs, or use the
}); // webNavigation.onBeforeNavigate event to map pdfUrls to tab + frame IDs.
} else { return;
console.log('Going to render PDF Viewer in sub frame for ' + pdfUrl); }
// Non-standard Chrome API. chrome.tabs.executeScriptInFrame and docs
// is available at https://github.com/Rob--W/chrome-api // Check if the frame has already been rendered.
chrome.tabs.executeScriptInFrame(tabId, { chrome.webNavigation.getAllFrames({
frameId: frameId, tabId: tabId
code: 'location.href = ' + JSON.stringify(viewerUrl) + ';' }, function(details) {
}, function(result) { if (details) {
if (!result) { // Did the tab disappear? Is the frame inaccessible? details = details.filter(function(frame) {
console.warn('Frame not found, viewer not rendered in tab ' + tabId); return (frame.url === pdfUrl);
});
if (details.length > 0) {
if (details.length !== 1) {
// (Rare case) Multiple frames with same URL.
// TODO(rob): Find a better way to handle this case
// (e.g. open in new tab).
console.warn('More than one frame found for tabId ' + tabId +
' with URL ' + pdfUrl + '. Using first frame.');
}
details = details[0];
details = {
tabId: tabId,
frameId: details.frameId,
url: details.url
};
handleWebNavigation(details);
} else {
console.warn('No webNavigation frames found for tabId ' + tabId);
}
} else {
console.warn('Unable to get frame information for tabId ' + tabId);
// This branch may occur when a new incognito session is launched.
// The event is dispatched in the non-incognito session while it should
// be dispatched in the incognito session. See http://crbug.com/276898
transferStreamToIncognitoProfile(tabId, pdfUrl);
} }
}); });
} }
}
/**
* This method is called when the chrome.streamsPrivate API has intercepted
* the PDF stream. This method detects such streams, finds the frame where
* the request was made, and loads the viewer in that frame.
*
* @param details {object}
* @param details.tabId {number} The ID of the tab
* @param details.url {string} The URL being navigated when the error
* occurred.
* @param details.frameId {number} 0 indicates the navigation happens in
* the tab content window; a positive value
* indicates navigation in a subframe.
*/
function handleWebNavigation(details) {
var tabId = details.tabId;
var frameId = details.frameId;
var pdfUrl = details.url;
if (!hasStream(tabId, pdfUrl)) {
console.log('No PDF stream found in tab ' + tabId + ' for ' + pdfUrl);
return;
}
var viewerUrl = getViewerURL(pdfUrl);
if (frameId === 0) { // Main frame
console.log('Going to render PDF Viewer in main frame for ' + pdfUrl);
chrome.tabs.update(tabId, {
url: viewerUrl
});
} else {
console.log('Going to render PDF Viewer in sub frame for ' + pdfUrl);
// Non-standard Chrome API. chrome.tabs.executeScriptInFrame and docs
// is available at https://github.com/Rob--W/chrome-api
chrome.tabs.executeScriptInFrame(tabId, {
frameId: frameId,
code: 'location.href = ' + JSON.stringify(viewerUrl) + ';'
}, function(result) {
if (!result) { // Did the tab disappear? Is the frame inaccessible?
console.warn('Frame not found, viewer not rendered in tab ' + tabId);
}
});
}
}
})(); })();

134
extensions/chromium/pdfHandler-vcros.js

@ -18,78 +18,78 @@ 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;
}
chrome.fileBrowserHandler.onExecute.addListener(onExecuteFileBrowserHandler);
/**
* 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 {Object} details Object of type FileHandlerExecuteEventDetails
*/
function onExecuteFileBrowserHandler(id, details) {
if (id !== 'open-as-pdf') {
return; return;
} }
var fileEntries = details.entries; chrome.fileBrowserHandler.onExecute.addListener(onExecuteFileBrowserHandler);
// "tab_id" is the currently documented format, but it is inconsistent with
// the other Chrome APIs that use "tabId" (http://crbug.com/179767)
var tabId = details.tab_id || details.tabId;
if (tabId > 0) {
chrome.tabs.get(tabId, function(tab) {
openViewer(tab && tab.windowId, fileEntries);
});
} else {
// Re-use existing window, if available.
chrome.windows.getLastFocused(function(chromeWindow) {
var windowId = chromeWindow && chromeWindow.id;
if (windowId) {
chrome.windows.update(windowId, { focused: true });
}
openViewer(windowId, fileEntries);
});
}
}
/** /**
* Open the PDF Viewer for the given list of PDF files. * Invoked when "Open with PDF Viewer" is chosen in the File browser.
* *
* @param {number} windowId * @param {String} id File browser action ID as specified in
* @param {Array} fileEntries List of Entry objects (HTML5 FileSystem API) * manifest.json
*/ * @param {Object} details Object of type FileHandlerExecuteEventDetails
function openViewer(windowId, fileEntries) { */
if (!fileEntries.length) { function onExecuteFileBrowserHandler(id, details) {
return; if (id !== 'open-as-pdf') {
return;
}
var fileEntries = details.entries;
// "tab_id" is the currently documented format, but it is inconsistent with
// the other Chrome APIs that use "tabId" (http://crbug.com/179767)
var tabId = details.tab_id || details.tabId;
if (tabId > 0) {
chrome.tabs.get(tabId, function(tab) {
openViewer(tab && tab.windowId, fileEntries);
});
} else {
// Re-use existing window, if available.
chrome.windows.getLastFocused(function(chromeWindow) {
var windowId = chromeWindow && chromeWindow.id;
if (windowId) {
chrome.windows.update(windowId, { focused: true });
}
openViewer(windowId, fileEntries);
});
}
} }
var fileEntry = fileEntries.shift();
var url = fileEntry.toURL();
// Use drive: alias to get shorter (more human-readable) URLs.
url = url.replace(/^filesystem:chrome-extension:\/\/[a-p]{32}\/external\//,
'drive:');
url = getViewerURL(url);
if (windowId) { /**
chrome.tabs.create({ * Open the PDF Viewer for the given list of PDF files.
windowId: windowId, *
active: true, * @param {number} windowId
url: url * @param {Array} fileEntries List of Entry objects (HTML5 FileSystem API)
}, function() { */
openViewer(windowId, fileEntries); function openViewer(windowId, fileEntries) {
}); if (!fileEntries.length) {
} else { return;
chrome.windows.create({ }
type: 'normal', var fileEntry = fileEntries.shift();
focused: true, var url = fileEntry.toURL();
url: url // Use drive: alias to get shorter (more human-readable) URLs.
}, function(chromeWindow) { url = url.replace(/^filesystem:chrome-extension:\/\/[a-p]{32}\/external\//,
openViewer(chromeWindow.id, fileEntries); 'drive:');
}); url = getViewerURL(url);
}
}
if (windowId) {
chrome.tabs.create({
windowId: windowId,
active: true,
url: url
}, function() {
openViewer(windowId, fileEntries);
});
} else {
chrome.windows.create({
type: 'normal',
focused: true,
url: url
}, function(chromeWindow) {
openViewer(chromeWindow.id, fileEntries);
});
}
}
})(); })();

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

Loading…
Cancel
Save