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