Index: chrome/browser/resources/pdf/background.js |
diff --git a/chrome/browser/resources/pdf/background.js b/chrome/browser/resources/pdf/background.js |
index 2114010193fb47095da2464de33d67fdede00557..1bf748a8ac1ea11f86369e233ab92a690094b07c 100644 |
--- a/chrome/browser/resources/pdf/background.js |
+++ b/chrome/browser/resources/pdf/background.js |
@@ -6,28 +6,58 @@ |
'use strict'; |
/** |
- * Keep a stack of stream details for requests. These are pushed onto the |
- * stack as requests come in and popped off the stack as they are handled by a |
- * renderer. |
- * TODO(raymes): This is probably racy for multiple requests. We could |
- * associate an ID with the request but this code will probably change |
- * completely when MIME type handling is improved. |
+ * A map of view ID (which identifies a particular PDF viewer instance) to |
+ * stream object. |
+ * @type {Object.<string, Object>} |
*/ |
- var streamsCache = []; |
+ var streams = {}; |
- window.popStreamDetails = function() { |
- if (streamsCache.length > 0) |
- return streamsCache.pop(); |
- }; |
+ /** |
+ * A map of view ID (which identifies a particular PDF viewer instance) to |
+ * initialization function for that view. |
+ * @type {Object.<string, Function>} |
+ */ |
+ var pluginInitFunctions = {}; |
+ |
+ /** |
+ * If we have received a stream object and an initialization function for a |
+ * particular PDF viewer instance we know that the extension has loaded in |
+ * and we can pass it the stream. We can then delete the corresponding map |
+ * entries. |
+ * @param {string} viewId The ID of the view to initialize with a stream. |
+ */ |
+ function flush(viewId) { |
+ if (viewId in streams && viewId in pluginInitFunctions) { |
+ pluginInitFunctions[viewId](streams[viewId]); |
+ delete streams[viewId]; |
+ delete pluginInitFunctions[viewId]; |
+ } |
+ } |
+ /** |
+ * This is called when loading a document with the PDF mime type and passes a |
+ * stream that points to the PDF file. This may be run before or after we |
+ * receive a message from the PDF viewer with its initialization function. |
+ */ |
chrome.streamsPrivate.onExecuteMimeTypeHandler.addListener( |
function(streamDetails) { |
- // TODO(raymes): Currently this doesn't work with embedded PDFs (it |
- // causes the entire frame to navigate). Also work out how we can |
- // mask the URL with the URL of the PDF. |
- streamsCache.push(streamDetails); |
- chrome.tabs.update(streamDetails.tabId, {url: 'index.html'}); |
+ // Store the stream until we are contacted by the PDF viewer that owns the |
+ // stream. |
+ streams[streamDetails.viewId] = streamDetails; |
+ flush(streamDetails.viewId); |
} |
); |
+ /** |
+ * This is called when we receive a message from the PDF viewer indicating |
+ * it has loaded and is ready to receive a stream of the data. |
+ */ |
+ chrome.runtime.onMessage.addListener( |
+ function(request, sender, responseFunction) { |
+ // Store the initialization function until we receive the stream which |
+ // corresponds to the PDF viewer. |
+ pluginInitFunctions[request.viewId] = responseFunction; |
+ flush(request.viewId); |
+ } |
+ ); |
}()); |