[CRX] Pass expected length of stream to viewer

To get a progress bar for POST/FTP requests in Chromium.
This commit is contained in:
Rob Wu 2014-01-30 23:03:15 +01:00
parent acb33b3e7d
commit eaf7daf7db
3 changed files with 28 additions and 14 deletions

View File

@ -55,27 +55,31 @@ function hasStream(tabId, pdfUrl) {
/** /**
* 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 {string|undefined} The blob:-URL * @return {object} An object with property url (= blob:-URL) and
* 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 streamUrl = urlToStream[tabId][pdfUrl].shift(); var streamInfo = urlToStream[tabId][pdfUrl].shift();
if (urlToStream[tabId][pdfUrl].length === 0) { if (urlToStream[tabId][pdfUrl].length === 0) {
delete urlToStream[tabId][pdfUrl]; delete urlToStream[tabId][pdfUrl];
if (Object.keys(urlToStream[tabId]).length === 0) { if (Object.keys(urlToStream[tabId]).length === 0) {
delete urlToStream[tabId]; delete urlToStream[tabId];
} }
} }
return streamUrl; return streamInfo;
} }
} }
function setStream(tabId, pdfUrl, streamUrl) { function setStream(tabId, pdfUrl, streamUrl, expectedSize) {
tabId = tabId || STREAM_NO_TABID; tabId = tabId || STREAM_NO_TABID;
if (!urlToStream[tabId]) urlToStream[tabId] = {}; if (!urlToStream[tabId]) urlToStream[tabId] = {};
if (!urlToStream[tabId][pdfUrl]) urlToStream[tabId][pdfUrl] = []; if (!urlToStream[tabId][pdfUrl]) urlToStream[tabId][pdfUrl] = [];
urlToStream[tabId][pdfUrl].push(streamUrl); urlToStream[tabId][pdfUrl].push({
streamUrl: streamUrl,
contentLength: expectedSize
});
} }
// http://crbug.com/276898 - the onExecuteMimeTypeHandler event is sometimes // http://crbug.com/276898 - the onExecuteMimeTypeHandler event is sometimes
@ -86,14 +90,18 @@ function transferStreamToIncognitoProfile(tabId, pdfUrl) {
console.log('Already within incognito profile. Aborted stream transfer.'); console.log('Already within incognito profile. Aborted stream transfer.');
return; return;
} }
var streamUrl = getStream(tabId, pdfUrl); var streamInfo = getStream(tabId, pdfUrl);
if (!streamInfo) {
return;
}
console.log('Attempting to transfer stream info to a different profile...'); console.log('Attempting to transfer stream info to a different profile...');
var itemId = 'streamInfo:' + window.performance.now(); var itemId = 'streamInfo:' + window.performance.now();
var items = {}; var items = {};
items[itemId] = { items[itemId] = {
tabId: tabId, tabId: tabId,
pdfUrl: pdfUrl, pdfUrl: pdfUrl,
streamUrl: streamUrl streamUrl: streamInfo.streamUrl,
contentLength: streamInfo.contentLength
}; };
// The key will be removed whenever an incognito session is started, // The key will be removed whenever an incognito session is started,
// or when an incognito session is active. // or when an incognito session is active.
@ -111,7 +119,8 @@ 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) return;
console.log('Importing stream info from non-incognito profile', streamInfo); console.log('Importing stream info from non-incognito profile', streamInfo);
handleStream('', streamInfo.pdfUrl, streamInfo.streamUrl, streamInfo.tabId); 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) {
@ -131,9 +140,10 @@ if (chrome.extension.inIncognitoContext) {
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 streamUrl = getStream(sender.tab.id, pdfUrl); var streamInfo = getStream(sender.tab.id, pdfUrl) || {};
sendResponse({ sendResponse({
streamUrl: streamUrl streamUrl: streamInfo.streamUrl,
contentLength: streamInfo.contentLength
}); });
} }
}); });
@ -150,13 +160,15 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
* @param streamUrl {string} The url pointing to the open stream * @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 * @param tabId {number} The ID of the tab in which the stream has been opened
* (undefined before Chrome 27, http://crbug.com/225605) * (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) { 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';
setStream(tabId, pdfUrl, streamUrl); setStream(tabId, pdfUrl, streamUrl, expectedSize);
if (!tabId) { // Chrome doesn't set the tabId before v27 if (!tabId) { // Chrome doesn't set the tabId before v27
// PDF.js targets Chrome 28+ because of fatal bugs in incognito mode // PDF.js targets Chrome 28+ because of fatal bugs in incognito mode

View File

@ -160,7 +160,7 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
onProgress: function onProgress(evt) { onProgress: function onProgress(evt) {
handler.send('DocProgress', { handler.send('DocProgress', {
loaded: evt.loaded, loaded: evt.loaded,
total: evt.lengthComputable ? evt.total : void(0) total: evt.lengthComputable ? evt.total : source.length
}); });
} }
}); });

View File

@ -1821,7 +1821,9 @@ document.addEventListener('DOMContentLoaded', function webViewerLoad(evt) {
// var streamUrl = response.streamUrl; // var streamUrl = response.streamUrl;
// if (streamUrl) { // if (streamUrl) {
// console.log('Found data stream for ' + file); // console.log('Found data stream for ' + file);
// PDFView.open(streamUrl, 0); // PDFView.open(streamUrl, 0, undefined, undefined, {
// length: response.contentLength
// });
// PDFView.setTitleUsingUrl(file); // PDFView.setTitleUsingUrl(file);
// return; // return;
// } // }