Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Global PDFViewer object, accessible for testing. | 8 * Global PDFViewer object, accessible for testing. |
| 9 * @type Object | 9 * @type Object |
| 10 */ | 10 */ |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 * contained in the PDF. | 33 * contained in the PDF. |
| 34 */ | 34 */ |
| 35 function initViewer(streamDetails) { | 35 function initViewer(streamDetails) { |
| 36 // PDFViewer will handle any messages after it is created. | 36 // PDFViewer will handle any messages after it is created. |
| 37 window.removeEventListener('message', handleScriptingMessage, false); | 37 window.removeEventListener('message', handleScriptingMessage, false); |
| 38 viewer = new PDFViewer(streamDetails); | 38 viewer = new PDFViewer(streamDetails); |
| 39 while (pendingMessages.length > 0) | 39 while (pendingMessages.length > 0) |
| 40 viewer.handleScriptingMessage(pendingMessages.shift()); | 40 viewer.handleScriptingMessage(pendingMessages.shift()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 function generateStreamDetailsAndInitViewer() { | |
| 44 var url = window.location.search.substring(1); | |
| 45 var streamDetails = { | |
| 46 streamUrl: url, | |
| 47 originalUrl: url, | |
| 48 responseHeaders: '', | |
| 49 embedded: window.parent != window, | |
| 50 tabId: -1 | |
| 51 }; | |
| 52 if (chrome.tabs) { | |
| 53 chrome.tabs.getCurrent(function(tab) { | |
| 54 streamDetails.tabId = tab.id; | |
| 55 initViewer(streamDetails); | |
| 56 }); | |
| 57 return; | |
| 58 } | |
| 59 initViewer(streamDetails); | |
|
raymes
2015/01/12 05:26:04
See my comment below about putting the synchronous
Sam McNally
2015/01/12 07:13:35
Done.
| |
| 60 } | |
| 61 | |
| 43 /** | 62 /** |
| 44 * Entrypoint for starting the PDF viewer. This function obtains the details | 63 * Entrypoint for starting the PDF viewer. This function obtains the details |
| 45 * of the PDF 'stream' (the data that points to the PDF) and constructs a | 64 * of the PDF 'stream' (the data that points to the PDF) and constructs a |
| 46 * PDFViewer object with it. | 65 * PDFViewer object with it. |
| 47 */ | 66 */ |
| 48 function main() { | 67 function main() { |
| 49 // Set up an event listener to catch scripting messages which are sent prior | 68 // Set up an event listener to catch scripting messages which are sent prior |
| 50 // to the PDFViewer being created. | 69 // to the PDFViewer being created. |
| 51 window.addEventListener('message', handleScriptingMessage, false); | 70 window.addEventListener('message', handleScriptingMessage, false); |
| 52 | 71 |
| 53 // If the viewer is started from the browser plugin, the view ID will be | 72 // If the viewer is started from the browser plugin, getStreamInfo will |
| 54 // passed in which identifies the instance of the plugin. | 73 // return the details of the stream. |
| 55 var params = window.location.search.substring(1).split('='); | 74 if (chrome.mimeHandler) { |
| 56 if (params.length == 2 && params[0] == 'id') { | 75 chrome.mimeHandler.getStreamInfo(function(streamDetails) { |
| 57 var viewId = params[1]; | 76 if (streamDetails) { |
| 58 | 77 initViewer(streamDetails); |
| 59 // Send a message to the background page to obtain the stream details. It | 78 return; |
| 60 // will run the callback function passed in to initialize the viewer. | 79 } |
| 61 chrome.runtime.sendMessage( | 80 generateStreamDetailsAndInitViewer(); |
|
raymes
2015/01/12 05:26:04
I think an if/else here might be cleaer
Sam McNally
2015/01/12 07:13:35
Done.
| |
| 62 'mhjfbmdgcfjbbpaeojofohoefgiehjai', | 81 }); |
| 63 {viewId: viewId}, | |
| 64 initViewer); | |
| 65 return; | 82 return; |
| 66 } | 83 } |
| 67 | 84 generateStreamDetailsAndInitViewer(); |
|
raymes
2015/01/12 05:26:04
Could you put the synchronous case first? This mig
Sam McNally
2015/01/12 07:13:35
Done.
| |
| 68 // The viewer may be started directly by passing in the URL of the PDF to | 85 }; |
| 69 // load as the query string. This is used for print preview in particular. | |
| 70 // The URL of this page will be of the form | |
| 71 // 'chrome-extension://<extension id>?<pdf url>'. We pull out the <pdf url> | |
| 72 // part here. | |
| 73 var url = window.location.search.substring(1); | |
| 74 var streamDetails = { | |
| 75 streamUrl: url, | |
| 76 originalUrl: url, | |
| 77 responseHeaders: '', | |
| 78 embedded: window.parent != window, | |
| 79 tabId: -1 | |
| 80 }; | |
| 81 if (!chrome.tabs) { | |
| 82 initViewer(streamDetails); | |
| 83 return; | |
| 84 } | |
| 85 chrome.tabs.getCurrent(function(tab) { | |
| 86 if (tab && tab.id != undefined) | |
| 87 streamDetails.tabId = tab.id; | |
| 88 initViewer(streamDetails); | |
| 89 }); | |
| 90 } | |
| 91 | 86 |
| 92 main(); | 87 main(); |
| 93 })(); | 88 })(); |
| OLD | NEW |