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 15 matching lines...) Expand all Loading... |
26 function handleScriptingMessage(message) { | 26 function handleScriptingMessage(message) { |
27 pendingMessages.push(message); | 27 pendingMessages.push(message); |
28 } | 28 } |
29 | 29 |
30 /** | 30 /** |
31 * Initialize the global PDFViewer and pass any outstanding messages to it. | 31 * Initialize the global PDFViewer and pass any outstanding messages to it. |
32 * @param {Object} streamDetails The stream object which points to the data | 32 * @param {Object} streamDetails The stream object which points to the data |
33 * contained in the PDF. | 33 * contained in the PDF. |
34 */ | 34 */ |
35 function initViewer(streamDetails) { | 35 function initViewer(streamDetails) { |
| 36 console.log(JSON.stringify(streamDetails)); |
36 // PDFViewer will handle any messages after it is created. | 37 // PDFViewer will handle any messages after it is created. |
37 window.removeEventListener('message', handleScriptingMessage, false); | 38 window.removeEventListener('message', handleScriptingMessage, false); |
38 viewer = new PDFViewer(streamDetails); | 39 viewer = new PDFViewer(streamDetails); |
39 while (pendingMessages.length > 0) | 40 while (pendingMessages.length > 0) |
40 viewer.handleScriptingMessage(pendingMessages.shift()); | 41 viewer.handleScriptingMessage(pendingMessages.shift()); |
41 } | 42 } |
42 | 43 |
43 /** | 44 function generateStreamDetailsAndInitViewer() { |
44 * 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 | |
46 * PDFViewer object with it. | |
47 */ | |
48 function main() { | |
49 // Set up an event listener to catch scripting messages which are sent prior | |
50 // to the PDFViewer being created. | |
51 window.addEventListener('message', handleScriptingMessage, false); | |
52 | |
53 // If the viewer is started from the browser plugin, the view ID will be | |
54 // passed in which identifies the instance of the plugin. | |
55 var params = window.location.search.substring(1).split('='); | |
56 if (params.length == 2 && params[0] == 'id') { | |
57 var viewId = params[1]; | |
58 | |
59 // Send a message to the background page to obtain the stream details. It | |
60 // will run the callback function passed in to initialize the viewer. | |
61 chrome.runtime.sendMessage( | |
62 'mhjfbmdgcfjbbpaeojofohoefgiehjai', | |
63 {viewId: viewId}, | |
64 initViewer); | |
65 return; | |
66 } | |
67 | |
68 // The viewer may be started directly by passing in the URL of the PDF to | |
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); | 45 var url = window.location.search.substring(1); |
74 var streamDetails = { | 46 var streamDetails = { |
75 streamUrl: url, | 47 streamUrl: url, |
76 originalUrl: url, | 48 originalUrl: url, |
77 responseHeaders: '', | 49 responseHeaders: '', |
78 embedded: window.parent != window, | 50 embedded: window.parent != window, |
79 tabId: -1 | 51 tabId: -1 |
80 }; | 52 }; |
81 if (!chrome.tabs) { | 53 if (!chrome.tabs) { |
82 initViewer(streamDetails); | 54 initViewer(streamDetails); |
83 return; | 55 return; |
84 } | 56 } |
85 chrome.tabs.getCurrent(function(tab) { | 57 chrome.tabs.getCurrent(function(tab) { |
86 if (tab && tab.id != undefined) | 58 streamDetails.tabId = tab.id; |
87 streamDetails.tabId = tab.id; | |
88 initViewer(streamDetails); | 59 initViewer(streamDetails); |
89 }); | 60 }); |
90 } | 61 } |
91 | 62 |
| 63 /** |
| 64 * Entrypoint for starting the PDF viewer. This function obtains the details |
| 65 * of the PDF 'stream' (the data that points to the PDF) and constructs a |
| 66 * PDFViewer object with it. |
| 67 */ |
| 68 function main() { |
| 69 // Set up an event listener to catch scripting messages which are sent prior |
| 70 // to the PDFViewer being created. |
| 71 window.addEventListener('message', handleScriptingMessage, false); |
| 72 |
| 73 // If the viewer is started from the browser plugin, getStreamInfo will |
| 74 // return the details of the stream. |
| 75 if (!chrome.mimeHandlerPrivate) { |
| 76 generateStreamDetailsAndInitViewer(); |
| 77 return; |
| 78 } |
| 79 chrome.mimeHandlerPrivate.getStreamInfo(function(streamDetails) { |
| 80 if (streamDetails) { |
| 81 initViewer(streamDetails); |
| 82 } else { |
| 83 generateStreamDetailsAndInitViewer(); |
| 84 } |
| 85 }); |
| 86 }; |
| 87 |
92 main(); | 88 main(); |
93 })(); | 89 })(); |
OLD | NEW |