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 /** | 43 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); | 44 var url = window.location.search.substring(1); |
74 var streamDetails = { | 45 var streamDetails = { |
75 streamUrl: url, | 46 streamUrl: url, |
76 originalUrl: url, | 47 originalUrl: url, |
77 responseHeaders: '', | 48 responseHeaders: '', |
78 embedded: window.parent != window, | 49 embedded: window.parent != window, |
79 tabId: -1 | 50 tabId: -1 |
80 }; | 51 }; |
81 if (!chrome.tabs) { | 52 if (!chrome.tabs) { |
82 initViewer(streamDetails); | 53 initViewer(streamDetails); |
83 return; | 54 return; |
84 } | 55 } |
85 chrome.tabs.getCurrent(function(tab) { | 56 chrome.tabs.getCurrent(function(tab) { |
86 if (tab && tab.id != undefined) | 57 streamDetails.tabId = tab.id; |
87 streamDetails.tabId = tab.id; | |
88 initViewer(streamDetails); | 58 initViewer(streamDetails); |
89 }); | 59 }); |
90 } | 60 } |
91 | 61 |
| 62 /** |
| 63 * Entrypoint for starting the PDF viewer. This function obtains the details |
| 64 * of the PDF 'stream' (the data that points to the PDF) and constructs a |
| 65 * PDFViewer object with it. |
| 66 */ |
| 67 function main() { |
| 68 // Set up an event listener to catch scripting messages which are sent prior |
| 69 // to the PDFViewer being created. |
| 70 window.addEventListener('message', handleScriptingMessage, false); |
| 71 |
| 72 if (window.location.search) { |
| 73 generateStreamDetailsAndInitViewer(); |
| 74 return; |
| 75 } |
| 76 |
| 77 // If the viewer is started from the browser plugin, getStreamInfo will |
| 78 // return the details of the stream. |
| 79 chrome.mimeHandlerPrivate.getStreamInfo(function(streamDetails) { |
| 80 initViewer(streamDetails); |
| 81 }); |
| 82 }; |
| 83 |
92 main(); | 84 main(); |
93 })(); | 85 })(); |
OLD | NEW |