Chromium Code Reviews| Index: chrome/browser/resources/pdf/main.js |
| diff --git a/chrome/browser/resources/pdf/main.js b/chrome/browser/resources/pdf/main.js |
| index cb7f0732ba92f2ae69adb128f1e8b635e064a479..24ae9668d733b621893ed37670e608fefab282cc 100644 |
| --- a/chrome/browser/resources/pdf/main.js |
| +++ b/chrome/browser/resources/pdf/main.js |
| @@ -10,47 +10,84 @@ |
| */ |
| var viewer; |
| -/** |
| - * Entrypoint for starting the PDF viewer. This function obtains the details |
| - * of the PDF 'stream' (the data that points to the PDF) and constructs a |
| - * PDFViewer object with it. |
| - */ |
| -(function main() { |
| - // If the viewer is started from the browser plugin, the view ID will be |
| - // passed in which identifies the instance of the plugin. |
| - var params = window.location.search.substring(1).split('='); |
| - if (params.length == 2 && params[0] == 'id') { |
| - var viewId = params[1]; |
| - |
| - // Send a message to the background page to obtain the stream details. It |
| - // will run the callback function passed in to initialize the viewer. |
| - chrome.runtime.sendMessage( |
| - 'mhjfbmdgcfjbbpaeojofohoefgiehjai', |
| - {viewId: viewId}, |
| - function(streamDetails) { viewer = new PDFViewer(streamDetails); }); |
| - return; |
| + |
| +(function() { |
| + /** |
| + * Stores any pending messages received which should be passed to the |
| + * PDFViewer when it is created. |
| + * @type Array |
| + */ |
| + var pendingMessages = []; |
| + |
| + /** |
| + * Handles events that are received prior to the PDFViewer being created. |
| + * @param {Object} message A message event received. |
| + */ |
| + function handleScriptingMessage(message) { |
| + pendingMessages.push(message); |
| } |
| - // The viewer may be started directly by passing in the URL of the PDF to load |
| - // as the query string. This is used for print preview in particular. The URL |
| - // of this page will be of the form |
| - // 'chrome-extension://<extension id>?<pdf url>'. We pull out the <pdf url> |
| - // part here. |
| - var url = window.location.search.substring(1); |
| - var streamDetails = { |
| - streamUrl: url, |
| - originalUrl: url, |
| - responseHeaders: '', |
| - embedded: window.parent != window, |
| - tabId: -1 |
| - }; |
| - if (!chrome.tabs) { |
| + /** |
| + * Initialize the global PDFViewer and pass any outstanding messages to it. |
| + * @param {Object} streamDetails The stream object which points to the data |
| + * contained in the PDF. |
| + */ |
| + function initViewer(streamDetails) { |
| + // PDFViewer will handle any messages after it is created. |
| + window.removeEventListener('message', handleScriptingMessage, false); |
| viewer = new PDFViewer(streamDetails); |
| - return; |
| + while (pendingMessages.length > 0) |
| + viewer.handleScriptingMessage(pendingMessages.shift()); |
| } |
| - chrome.tabs.getCurrent(function(tab) { |
| - if (tab && tab.id != undefined) |
| - streamDetails.tabId = tab.id; |
| - viewer = new PDFViewer(streamDetails); |
| - }); |
| + |
| + /** |
| + * Entrypoint for starting the PDF viewer. This function obtains the details |
| + * of the PDF 'stream' (the data that points to the PDF) and constructs a |
| + * PDFViewer object with it. |
| + */ |
| + function main() { |
| + // Set up an event listener to catch scripting messages which are sent prior |
| + // to the PDFViewer being created. |
| + window.addEventListener('message', handleScriptingMessage, false); |
| + |
| + // If the viewer is started from the browser plugin, the view ID will be |
| + // passed in which identifies the instance of the plugin. |
| + var params = window.location.search.substring(1).split('='); |
| + if (params.length == 2 && params[0] == 'id') { |
| + var viewId = params[1]; |
| + |
| + // Send a message to the background page to obtain the stream details. It |
| + // will run the callback function passed in to initialize the viewer. |
| + chrome.runtime.sendMessage( |
| + 'mhjfbmdgcfjbbpaeojofohoefgiehjai', |
| + {viewId: viewId}, |
| + function(streamDetails) { initViewer(streamDetails); }); |
|
Sam McNally
2015/01/08 03:50:26
Pass initViewer directly.
raymes
2015/01/08 22:08:21
Done.
|
| + return; |
| + } |
| + |
| + // The viewer may be started directly by passing in the URL of the PDF to |
| + // load as the query string. This is used for print preview in particular. |
| + // The URL of this page will be of the form |
| + // 'chrome-extension://<extension id>?<pdf url>'. We pull out the <pdf url> |
| + // part here. |
| + var url = window.location.search.substring(1); |
| + var streamDetails = { |
| + streamUrl: url, |
| + originalUrl: url, |
| + responseHeaders: '', |
| + embedded: window.parent != window, |
| + tabId: -1 |
| + }; |
| + if (!chrome.tabs) { |
| + initViewer(streamDetails); |
| + return; |
| + } |
| + chrome.tabs.getCurrent(function(tab) { |
| + if (tab && tab.id != undefined) |
| + streamDetails.tabId = tab.id; |
| + initViewer(streamDetails); |
| + }); |
| + } |
| + |
| + main(); |
|
Sam McNally
2015/01/08 03:50:26
How about doing the (function() {})() thing here t
raymes
2015/01/08 22:08:21
I almost did but it added more brackets everywhere
|
| })(); |