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
3 // found in the LICENSE file.
8 * Global PDFViewer object, accessible for testing.
16 * Stores any pending messages received which should be passed to the
17 * PDFViewer when it is created.
20 var pendingMessages = [];
23 * Handles events that are received prior to the PDFViewer being created.
24 * @param {Object} message A message event received.
26 function handleScriptingMessage(message) {
27 pendingMessages.push(message);
31 * Initialize the global PDFViewer and pass any outstanding messages to it.
32 * @param {Object} streamDetails The stream object which points to the data
33 * contained in the PDF.
35 function initViewer(streamDetails) {
36 // PDFViewer will handle any messages after it is created.
37 window.removeEventListener('message', handleScriptingMessage, false);
38 viewer = new PDFViewer(streamDetails);
39 while (pendingMessages.length > 0)
40 viewer.handleScriptingMessage(pendingMessages.shift());
43 function generateStreamDetailsAndInitViewer() {
44 var url = window.location.search.substring(1);
46 // Hack to enable custom scrollbars for print preview on non-retina mac
47 // displays. Remove after crbug.com/466039 is fixed.
48 if (url.indexOf(IS_MAC_PARAM) === 0) {
49 url = url.substring(IS_MAC_PARAM.length);
50 var link = document.createElement('link');
51 link.rel = 'stylesheet';
52 link.type = 'text/css';
53 link.href = 'scrollbars_mac.css';
54 document.getElementsByTagName('head')[0].appendChild(link);
61 embedded: window.parent != window,
65 initViewer(streamDetails);
68 chrome.tabs.getCurrent(function(tab) {
69 streamDetails.tabId = tab.id;
70 initViewer(streamDetails);
75 * Entrypoint for starting the PDF viewer. This function obtains the details
76 * of the PDF 'stream' (the data that points to the PDF) and constructs a
77 * PDFViewer object with it.
80 // Set up an event listener to catch scripting messages which are sent prior
81 // to the PDFViewer being created.
82 window.addEventListener('message', handleScriptingMessage, false);
84 if (window.location.search) {
85 generateStreamDetailsAndInitViewer();
89 // If the viewer is started from the browser plugin, getStreamInfo will
90 // return the details of the stream.
91 chrome.mimeHandlerPrivate.getStreamInfo(function(streamDetails) {
92 initViewer(streamDetails);