| 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 /** | 5 /** |
| 6 * Turn a dictionary received from postMessage into a key event. |
| 7 * @param {Object} dict A dictionary representing the key event. |
| 8 * @return {Event} A key event. |
| 9 */ |
| 10 function DeserializeKeyEvent(dict) { |
| 11 var e = document.createEvent('Event'); |
| 12 e.initEvent('keydown'); |
| 13 e.keyCode = dict.keyCode; |
| 14 e.shiftKey = dict.shiftKey; |
| 15 e.ctrlKey = dict.ctrlKey; |
| 16 e.altKey = dict.altKey; |
| 17 e.metaKey = dict.metaKey; |
| 18 e.fromScriptingAPI = true; |
| 19 return e; |
| 20 } |
| 21 |
| 22 /** |
| 23 * Turn a key event into a dictionary which can be sent over postMessage. |
| 24 * @param {Event} event A key event. |
| 25 * @return {Object} A dictionary representing the key event. |
| 26 */ |
| 27 function SerializeKeyEvent(event) { |
| 28 return { |
| 29 keyCode: event.keyCode, |
| 30 shiftKey: event.shiftKey, |
| 31 ctrlKey: event.ctrlKey, |
| 32 altKey: event.altKey, |
| 33 metaKey: event.metaKey |
| 34 }; |
| 35 } |
| 36 |
| 37 /** |
| 6 * Create a new PDFScriptingAPI. This provides a scripting interface to | 38 * 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. | 39 * 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. | 40 * @param {Window} window the window of the page containing the pdf viewer. |
| 9 * @param {Object} plugin the plugin element containing the pdf viewer. | 41 * @param {Object} plugin the plugin element containing the pdf viewer. |
| 10 */ | 42 */ |
| 11 function PDFScriptingAPI(window, plugin) { | 43 function PDFScriptingAPI(window, plugin) { |
| 12 this.loaded_ = false; | 44 this.loaded_ = false; |
| 13 this.pendingScriptingMessages_ = []; | 45 this.pendingScriptingMessages_ = []; |
| 14 this.setPlugin(plugin); | 46 this.setPlugin(plugin); |
| 15 | 47 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 38 this.accessibilityCallback_(event.data.json); | 70 this.accessibilityCallback_(event.data.json); |
| 39 this.accessibilityCallback_ = null; | 71 this.accessibilityCallback_ = null; |
| 40 } | 72 } |
| 41 break; | 73 break; |
| 42 case 'getSelectedTextReply': | 74 case 'getSelectedTextReply': |
| 43 if (this.selectedTextCallback_) { | 75 if (this.selectedTextCallback_) { |
| 44 this.selectedTextCallback_(event.data.selectedText); | 76 this.selectedTextCallback_(event.data.selectedText); |
| 45 this.selectedTextCallback_ = null; | 77 this.selectedTextCallback_ = null; |
| 46 } | 78 } |
| 47 break; | 79 break; |
| 80 case 'sendKeyEvent': |
| 81 if (this.keyEventCallback_) |
| 82 this.keyEventCallback_(DeserializeKeyEvent(event.data.keyEvent)); |
| 83 break; |
| 48 } | 84 } |
| 49 }.bind(this), false); | 85 }.bind(this), false); |
| 50 } | 86 } |
| 51 | 87 |
| 52 PDFScriptingAPI.prototype = { | 88 PDFScriptingAPI.prototype = { |
| 53 /** | 89 /** |
| 54 * @private | 90 * @private |
| 55 * Send a message to the extension. If messages try to get sent before there | 91 * Send a message to the extension. If messages try to get sent before there |
| 56 * is a plugin element set, then we queue them up and send them later (this | 92 * is a plugin element set, then we queue them up and send them later (this |
| 57 * can happen in print preview). | 93 * can happen in print preview). |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 * loading. If the document is already loaded, it will be run immediately. | 133 * loading. If the document is already loaded, it will be run immediately. |
| 98 * @param {Function} callback the callback to be called. | 134 * @param {Function} callback the callback to be called. |
| 99 */ | 135 */ |
| 100 setLoadCallback: function(callback) { | 136 setLoadCallback: function(callback) { |
| 101 this.loadCallback_ = callback; | 137 this.loadCallback_ = callback; |
| 102 if (this.loaded_ && this.loadCallback_) | 138 if (this.loaded_ && this.loadCallback_) |
| 103 this.loadCallback_(); | 139 this.loadCallback_(); |
| 104 }, | 140 }, |
| 105 | 141 |
| 106 /** | 142 /** |
| 143 * Sets a callback that gets run when a key event is fired in the PDF viewer. |
| 144 * @param {Function} callback the callback to be called with a key event. |
| 145 */ |
| 146 setKeyEventCallback: function(callback) { |
| 147 this.keyEventCallback_ = callback; |
| 148 }, |
| 149 |
| 150 /** |
| 107 * Resets the PDF viewer into print preview mode. | 151 * Resets the PDF viewer into print preview mode. |
| 108 * @param {string} url the url of the PDF to load. | 152 * @param {string} url the url of the PDF to load. |
| 109 * @param {boolean} grayscale whether or not to display the PDF in grayscale. | 153 * @param {boolean} grayscale whether or not to display the PDF in grayscale. |
| 110 * @param {Array.<number>} pageNumbers an array of the page numbers. | 154 * @param {Array.<number>} pageNumbers an array of the page numbers. |
| 111 * @param {boolean} modifiable whether or not the document is modifiable. | 155 * @param {boolean} modifiable whether or not the document is modifiable. |
| 112 */ | 156 */ |
| 113 resetPrintPreviewMode: function(url, grayscale, pageNumbers, modifiable) { | 157 resetPrintPreviewMode: function(url, grayscale, pageNumbers, modifiable) { |
| 114 this.loaded_ = false; | 158 this.loaded_ = false; |
| 115 this.sendMessage_({ | 159 this.sendMessage_({ |
| 116 type: 'resetPrintPreviewMode', | 160 type: 'resetPrintPreviewMode', |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 * Print the document. May only be called after document load. | 233 * Print the document. May only be called after document load. |
| 190 */ | 234 */ |
| 191 print: function() { | 235 print: function() { |
| 192 this.sendMessage_({ | 236 this.sendMessage_({ |
| 193 type: 'print' | 237 type: 'print' |
| 194 }); | 238 }); |
| 195 }, | 239 }, |
| 196 | 240 |
| 197 /** | 241 /** |
| 198 * Send a key event to the extension. | 242 * Send a key event to the extension. |
| 199 * @param {number} keyCode the key code to send to the extension. | 243 * @param {Event} keyEvent the key event to send to the extension. |
| 200 */ | 244 */ |
| 201 sendKeyEvent: function(keyCode) { | 245 sendKeyEvent: function(keyEvent) { |
| 202 this.sendMessage_({ | 246 this.sendMessage_({ |
| 203 type: 'sendKeyEvent', | 247 type: 'sendKeyEvent', |
| 204 keyCode: keyCode | 248 keyEvent: SerializeKeyEvent(keyEvent) |
| 205 }); | 249 }); |
| 206 }, | 250 }, |
| 207 }; | 251 }; |
| 208 | 252 |
| 209 /** | 253 /** |
| 210 * Creates a PDF viewer with a scripting interface. This is basically 1) an | 254 * Creates a PDF viewer with a scripting interface. This is basically 1) an |
| 211 * iframe which is navigated to the PDF viewer extension and 2) a scripting | 255 * iframe which is navigated to the PDF viewer extension and 2) a scripting |
| 212 * interface which provides access to various features of the viewer for use | 256 * interface which provides access to various features of the viewer for use |
| 213 * by print preview and accessibility. | 257 * by print preview and accessibility. |
| 214 * @param {string} src the source URL of the PDF to load initially. | 258 * @param {string} src the source URL of the PDF to load initially. |
| 215 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. | 259 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. |
| 216 */ | 260 */ |
| 217 function PDFCreateOutOfProcessPlugin(src) { | 261 function PDFCreateOutOfProcessPlugin(src) { |
| 218 var iframe = window.document.createElement('iframe'); | 262 var iframe = window.document.createElement('iframe'); |
| 219 iframe.setAttribute( | 263 iframe.setAttribute( |
| 220 'src', | 264 'src', |
| 221 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?' + src); | 265 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?' + src); |
| 222 var client = new PDFScriptingAPI(window); | 266 var client = new PDFScriptingAPI(window); |
| 223 iframe.onload = function() { | 267 iframe.onload = function() { |
| 224 client.setPlugin(iframe.contentWindow); | 268 client.setPlugin(iframe.contentWindow); |
| 225 }; | 269 }; |
| 226 // Add the functions to the iframe so that they can be called directly. | 270 // Add the functions to the iframe so that they can be called directly. |
| 227 iframe.setViewportChangedCallback = | 271 iframe.setViewportChangedCallback = |
| 228 client.setViewportChangedCallback.bind(client); | 272 client.setViewportChangedCallback.bind(client); |
| 229 iframe.setLoadCallback = client.setLoadCallback.bind(client); | 273 iframe.setLoadCallback = client.setLoadCallback.bind(client); |
| 274 iframe.setKeyEventCallback = client.setKeyEventCallback.bind(client); |
| 230 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); | 275 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); |
| 231 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); | 276 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); |
| 232 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); | 277 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); |
| 233 return iframe; | 278 return iframe; |
| 234 } | 279 } |
| OLD | NEW |