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.
6 * Create a new PDFScriptingAPI. This provides a scripting interface to
7 * the PDF viewer so that it can be customized by things like print preview.
8 * @param {Window} window the window of the page containing the pdf viewer.
9 * @param {string} extensionUrl the url of the PDF extension.
11 function PDFScriptingAPI(window, extensionUrl) {
12 this.extensionUrl_ = extensionUrl;
13 this.messageQueue_ = [];
14 window.addEventListener('message', function(event) {
15 if (event.origin != this.extensionUrl_) {
16 console.error('Received message that was not from the extension: ' +
20 switch (event.data.type) {
21 case 'readyToReceive':
22 this.setDestinationWindow(event.source);
25 if (this.viewportChangedCallback_)
26 this.viewportChangedCallback_(event.data.pageX,
29 event.data.viewportWidth,
30 event.data.viewportHeight);
32 case 'documentLoaded':
33 if (this.loadCallback_)
36 case 'getAccessibilityJSONReply':
37 if (this.accessibilityCallback_) {
38 this.accessibilityCallback_(event.data.json);
39 this.accessibilityCallback_ = null;
46 PDFScriptingAPI.prototype = {
49 * Send a message to the extension. If we try to send messages prior to the
50 * extension being ready to receive messages (i.e. before it has finished
51 * loading) we queue up the messages and flush them later.
52 * @param {Object} the message to send.
54 sendMessage_: function(message) {
55 if (!this.pdfWindow_) {
56 this.messageQueue_.push(message);
60 this.pdfWindow_.postMessage(message, this.extensionUrl_);
64 * Sets the destination window containing the PDF viewer. This will be called
65 * when a 'readyToReceive' message is received from the PDF viewer or it can
66 * be called during tests. It then flushes any pending messages to the window.
67 * @param {Window} pdfWindow the window containing the PDF viewer.
69 setDestinationWindow: function(pdfWindow) {
70 this.pdfWindow_ = pdfWindow;
71 while (this.messageQueue_.length != 0) {
72 this.pdfWindow_.postMessage(this.messageQueue_.shift(),
78 * Sets the callback which will be run when the PDF viewport changes.
79 * @param {Function} callback the callback to be called.
81 setViewportChangedCallback: function(callback) {
82 this.viewportChangedCallback_ = callback;
86 * Sets the callback which will be run when the PDF document has finished
88 * @param {Function} callback the callback to be called.
90 setLoadCallback: function(callback) {
91 this.loadCallback_ = callback;
95 * Resets the PDF viewer into print preview mode.
96 * @param {string} url the url of the PDF to load.
97 * @param {boolean} grayscale whether or not to display the PDF in grayscale.
98 * @param {Array.<number>} pageNumbers an array of the page numbers.
99 * @param {boolean} modifiable whether or not the document is modifiable.
101 resetPrintPreviewMode: function(url, grayscale, pageNumbers, modifiable) {
103 type: 'resetPrintPreviewMode',
105 grayscale: grayscale,
106 pageNumbers: pageNumbers,
107 modifiable: modifiable
112 * Load a page into the document while in print preview mode.
113 * @param {string} url the url of the pdf page to load.
114 * @param {number} index the index of the page to load.
116 loadPreviewPage: function(url, index) {
118 type: 'loadPreviewPage',
125 * Get accessibility JSON for the document.
126 * @param {Function} callback a callback to be called with the accessibility
127 * json that has been retrieved.
128 * @param {number} [page] the 0-indexed page number to get accessibility data
129 * for. If this is not provided, data about the entire document is
131 * @return {boolean} true if the function is successful, false if there is an
132 * outstanding request for accessibility data that has not been answered.
134 getAccessibilityJSON: function(callback, page) {
135 if (this.accessibilityCallback_)
137 this.accessibilityCallback_ = callback;
139 type: 'getAccessibilityJSON',
141 if (page || page == 0)
143 this.sendMessage_(message);
148 * Send a key event to the extension.
149 * @param {number} keyCode the key code to send to the extension.
151 sendKeyEvent: function(keyCode) {
153 type: 'sendKeyEvent',
160 * Creates a PDF viewer with a scripting interface. This is basically 1) an
161 * iframe which is navigated to the PDF viewer extension and 2) a scripting
162 * interface which provides access to various features of the viewer for use
163 * by print preview and accessbility.
164 * @param {string} src the source URL of the PDF to load initially.
165 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer.
167 function PDFCreateOutOfProcessPlugin(src) {
168 var EXTENSION_URL = 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai';
169 var iframe = window.document.createElement('iframe');
170 iframe.setAttribute('src', EXTENSION_URL + '/index.html?' + src);
171 var client = new PDFScriptingAPI(window, EXTENSION_URL);
173 // Add the functions to the iframe so that they can be called directly.
174 iframe.setViewportChangedCallback =
175 client.setViewportChangedCallback.bind(client);
176 iframe.setLoadCallback = client.setLoadCallback.bind(client);
177 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client);
178 iframe.loadPreviewPage = client.loadPreviewPage.bind(client);
179 iframe.sendKeyEvent = client.sendKeyEvent.bind(client);