Making extensions/chromium/pdfHandler-v2.js adhere to the style guide and fixing a small lint issue in pdfHandler-vcros.js

This commit is contained in:
Tim van der Meij 2014-03-09 23:07:42 +01:00
parent 8ec46e6413
commit ce6e269d02
2 changed files with 237 additions and 221 deletions

View File

@ -18,48 +18,51 @@ limitations under the License.
/* globals chrome, URL, getViewerURL */
(function() {
'use strict';
'use strict';
if (!chrome.streamsPrivate) {
if (!chrome.streamsPrivate) {
// Aww, PDF.js is still not whitelisted... See http://crbug.com/326949
console.warn('streamsPrivate not available, PDF from FTP or POST ' +
'requests will not be displayed using this extension! ' +
'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') {
sendResponse();
}
});
return;
}
}
//
// Stream URL storage manager
//
//
// Stream URL storage manager
//
// Hash map of "<tab id>": { "<pdf url>": ["<stream url>", ...], ... }
var urlToStream = {};
// Hash map of "<tab id>": { "<pdf url>": ["<stream url>", ...], ... }
var urlToStream = {};
chrome.streamsPrivate.onExecuteMimeTypeHandler.addListener(handleStream);
chrome.streamsPrivate.onExecuteMimeTypeHandler.addListener(handleStream);
// Chrome before 27 does not support tabIds on stream events.
var streamSupportsTabId = true;
// "tabId" used for Chrome before 27.
var STREAM_NO_TABID = 0;
// Chrome before 27 does not support tabIds on stream events.
var streamSupportsTabId = true;
// "tabId" used for Chrome before 27.
var STREAM_NO_TABID = 0;
function hasStream(tabId, pdfUrl) {
function hasStream(tabId, pdfUrl) {
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
* will be removed from the list.
* @return {object} An object with property url (= blob:-URL) and
* property contentLength (= expected size)
*/
function getStream(tabId, pdfUrl) {
if (!streamSupportsTabId) tabId = STREAM_NO_TABID;
/**
* Get stream URL for a given tabId and PDF url. The retrieved stream URL
* will be removed from the list.
* @return {object} An object with property url (= blob:-URL) and
* property contentLength (= expected size)
*/
function getStream(tabId, pdfUrl) {
if (!streamSupportsTabId) {
tabId = STREAM_NO_TABID;
}
if (hasStream(tabId, pdfUrl)) {
var streamInfo = urlToStream[tabId][pdfUrl].shift();
if (urlToStream[tabId][pdfUrl].length === 0) {
@ -70,22 +73,26 @@ function getStream(tabId, pdfUrl) {
}
return streamInfo;
}
}
}
function setStream(tabId, pdfUrl, streamUrl, expectedSize) {
function setStream(tabId, pdfUrl, streamUrl, expectedSize) {
tabId = tabId || STREAM_NO_TABID;
if (!urlToStream[tabId]) urlToStream[tabId] = {};
if (!urlToStream[tabId][pdfUrl]) urlToStream[tabId][pdfUrl] = [];
if (!urlToStream[tabId]) {
urlToStream[tabId] = {};
}
if (!urlToStream[tabId][pdfUrl]) {
urlToStream[tabId][pdfUrl] = [];
}
urlToStream[tabId][pdfUrl].push({
streamUrl: streamUrl,
contentLength: expectedSize
});
}
}
// http://crbug.com/276898 - the onExecuteMimeTypeHandler event is sometimes
// 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) {
// http://crbug.com/276898 - the onExecuteMimeTypeHandler event is sometimes
// 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;
@ -114,30 +121,38 @@ function transferStreamToIncognitoProfile(tabId, pdfUrl) {
}
});
});
}
if (chrome.extension.inIncognitoContext) {
}
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);
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
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
}
// 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') {
var pdfUrl = message.data;
var streamInfo = getStream(sender.tab.id, pdfUrl) || {};
@ -146,13 +161,13 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
contentLength: streamInfo.contentLength
});
}
});
});
//
// PDF detection and activation of PDF viewer.
//
//
// PDF detection and activation of PDF viewer.
//
/**
/**
* Callback for when we receive a 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.
* (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 ' +
pdfUrl + '\nAvailable as: ' + streamUrl);
streamSupportsTabId = typeof tabId === 'number';
@ -184,7 +199,7 @@ function handleStream(mimeType, pdfUrl, streamUrl, tabId, expectedSize) {
}, function(details) {
if (details) {
details = details.filter(function(frame) {
return frame.url === pdfUrl;
return (frame.url === pdfUrl);
});
if (details.length > 0) {
if (details.length !== 1) {
@ -212,21 +227,22 @@ function handleStream(mimeType, pdfUrl, streamUrl, tabId, expectedSize) {
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.
* @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) {
function handleWebNavigation(details) {
var tabId = details.tabId;
var frameId = details.frameId;
var pdfUrl = details.url;
@ -256,6 +272,5 @@ function handleWebNavigation(details) {
}
});
}
}
}
})();

View File

@ -29,7 +29,8 @@ limitations under the License.
/**
* 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
*/
function onExecuteFileBrowserHandler(id, details) {