| 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. | 6 * Turn a dictionary received from postMessage into a key event. |
| 7 * @param {Object} dict A dictionary representing the key event. | 7 * @param {Object} dict A dictionary representing the key event. |
| 8 * @return {Event} A key event. | 8 * @return {Event} A key event. |
| 9 */ | 9 */ |
| 10 function DeserializeKeyEvent(dict) { | 10 function DeserializeKeyEvent(dict) { |
| 11 var e = document.createEvent('Event'); | 11 var e = document.createEvent('Event'); |
| 12 e.initEvent('keydown'); | 12 e.initEvent('keydown', true, true); |
| 13 e.keyCode = dict.keyCode; | 13 e.keyCode = dict.keyCode; |
| 14 e.shiftKey = dict.shiftKey; | 14 e.shiftKey = dict.shiftKey; |
| 15 e.ctrlKey = dict.ctrlKey; | 15 e.ctrlKey = dict.ctrlKey; |
| 16 e.altKey = dict.altKey; | 16 e.altKey = dict.altKey; |
| 17 e.metaKey = dict.metaKey; | 17 e.metaKey = dict.metaKey; |
| 18 e.fromScriptingAPI = true; | 18 e.fromScriptingAPI = true; |
| 19 return e; | 19 return e; |
| 20 } | 20 } |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Turn a key event into a dictionary which can be sent over postMessage. | 23 * Turn a key event into a dictionary which can be sent over postMessage. |
| 24 * @param {Event} event A key event. | 24 * @param {Event} event A key event. |
| 25 * @return {Object} A dictionary representing the key event. | 25 * @return {Object} A dictionary representing the key event. |
| 26 */ | 26 */ |
| 27 function SerializeKeyEvent(event) { | 27 function SerializeKeyEvent(event) { |
| 28 return { | 28 return { |
| 29 keyCode: event.keyCode, | 29 keyCode: event.keyCode, |
| 30 shiftKey: event.shiftKey, | 30 shiftKey: event.shiftKey, |
| 31 ctrlKey: event.ctrlKey, | 31 ctrlKey: event.ctrlKey, |
| 32 altKey: event.altKey, | 32 altKey: event.altKey, |
| 33 metaKey: event.metaKey | 33 metaKey: event.metaKey |
| 34 }; | 34 }; |
| 35 } | 35 } |
| 36 | 36 |
| 37 /** | 37 /** |
| 38 * An enum containing a value specifying whether the PDF is currently loading, | 38 * An enum containing a value specifying whether the PDF is currently loading, |
| 39 * has finished loading or failed to load. | 39 * has finished loading or failed to load. |
| 40 * @enum {string} |
| 40 */ | 41 */ |
| 41 var LoadState = { | 42 var LoadState = { |
| 42 LOADING: 'loading', | 43 LOADING: 'loading', |
| 43 SUCCESS: 'success', | 44 SUCCESS: 'success', |
| 44 FAILED: 'failed' | 45 FAILED: 'failed' |
| 45 }; | 46 }; |
| 46 | 47 |
| 47 /** | 48 /** |
| 48 * Create a new PDFScriptingAPI. This provides a scripting interface to | 49 * Create a new PDFScriptingAPI. This provides a scripting interface to |
| 49 * the PDF viewer so that it can be customized by things like print preview. | 50 * the PDF viewer so that it can be customized by things like print preview. |
| 50 * @param {Window} window the window of the page containing the pdf viewer. | 51 * @param {Window} window the window of the page containing the pdf viewer. |
| 51 * @param {Object} plugin the plugin element containing the pdf viewer. | 52 * @param {Object} plugin the plugin element containing the pdf viewer. |
| 53 * @constructor |
| 52 */ | 54 */ |
| 53 function PDFScriptingAPI(window, plugin) { | 55 function PDFScriptingAPI(window, plugin) { |
| 54 this.loadState_ = LoadState.LOADING; | 56 this.loadState_ = LoadState.LOADING; |
| 55 this.pendingScriptingMessages_ = []; | 57 this.pendingScriptingMessages_ = []; |
| 56 this.setPlugin(plugin); | 58 this.setPlugin(plugin); |
| 57 | 59 |
| 58 window.addEventListener('message', function(event) { | 60 window.addEventListener('message', function(event) { |
| 59 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' && | 61 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' && |
| 60 event.origin != 'chrome://print') { | 62 event.origin != 'chrome://print') { |
| 61 console.error('Received message that was not from the extension: ' + | 63 console.error('Received message that was not from the extension: ' + |
| 62 event); | 64 event); |
| 63 return; | 65 return; |
| 64 } | 66 } |
| 65 switch (event.data.type) { | 67 switch (event.data.type) { |
| 66 case 'viewport': | 68 case 'viewport': |
| 69 event.data = /** |
| 70 * @type {{pageX: number, |
| 71 * pageY: number, |
| 72 * pageWidth: number, |
| 73 * viewportWidth: number, |
| 74 * viewportHeight: number}} |
| 75 */ (event.data); |
| 67 if (this.viewportChangedCallback_) | 76 if (this.viewportChangedCallback_) |
| 68 this.viewportChangedCallback_(event.data.pageX, | 77 this.viewportChangedCallback_(event.data.pageX, |
| 69 event.data.pageY, | 78 event.data.pageY, |
| 70 event.data.pageWidth, | 79 event.data.pageWidth, |
| 71 event.data.viewportWidth, | 80 event.data.viewportWidth, |
| 72 event.data.viewportHeight); | 81 event.data.viewportHeight); |
| 73 break; | 82 break; |
| 74 case 'documentLoaded': | 83 case 'documentLoaded': |
| 84 event.data = /** @type {{load_state: LoadState}} */ (event.data); |
| 75 this.loadState_ = event.data.load_state; | 85 this.loadState_ = event.data.load_state; |
| 76 if (this.loadCallback_) | 86 if (this.loadCallback_) |
| 77 this.loadCallback_(this.loadState_ == LoadState.SUCCESS); | 87 this.loadCallback_(this.loadState_ == LoadState.SUCCESS); |
| 78 break; | 88 break; |
| 79 case 'getSelectedTextReply': | 89 case 'getSelectedTextReply': |
| 90 event.data = /**@type {{selectedText: string}} */(event.data); |
| 80 if (this.selectedTextCallback_) { | 91 if (this.selectedTextCallback_) { |
| 81 this.selectedTextCallback_(event.data.selectedText); | 92 this.selectedTextCallback_(event.data.selectedText); |
| 82 this.selectedTextCallback_ = null; | 93 this.selectedTextCallback_ = null; |
| 83 } | 94 } |
| 84 break; | 95 break; |
| 85 case 'sendKeyEvent': | 96 case 'sendKeyEvent': |
| 86 if (this.keyEventCallback_) | 97 if (this.keyEventCallback_) |
| 87 this.keyEventCallback_(DeserializeKeyEvent(event.data.keyEvent)); | 98 this.keyEventCallback_(DeserializeKeyEvent(event.data.keyEvent)); |
| 88 break; | 99 break; |
| 89 } | 100 } |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 244 |
| 234 /** | 245 /** |
| 235 * Creates a PDF viewer with a scripting interface. This is basically 1) an | 246 * Creates a PDF viewer with a scripting interface. This is basically 1) an |
| 236 * iframe which is navigated to the PDF viewer extension and 2) a scripting | 247 * iframe which is navigated to the PDF viewer extension and 2) a scripting |
| 237 * interface which provides access to various features of the viewer for use | 248 * interface which provides access to various features of the viewer for use |
| 238 * by print preview and accessibility. | 249 * by print preview and accessibility. |
| 239 * @param {string} src the source URL of the PDF to load initially. | 250 * @param {string} src the source URL of the PDF to load initially. |
| 240 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. | 251 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. |
| 241 */ | 252 */ |
| 242 function PDFCreateOutOfProcessPlugin(src) { | 253 function PDFCreateOutOfProcessPlugin(src) { |
| 243 var client = new PDFScriptingAPI(window); | 254 var client = new PDFScriptingAPI(window, null); |
| 244 var iframe = window.document.createElement('iframe'); | 255 var iframe = assertInstanceof(window.document.createElement('iframe'), |
| 256 HTMLIFrameElement); |
| 245 iframe.setAttribute('src', 'pdf_preview.html?' + src); | 257 iframe.setAttribute('src', 'pdf_preview.html?' + src); |
| 246 // Prevent the frame from being tab-focusable. | 258 // Prevent the frame from being tab-focusable. |
| 247 iframe.setAttribute('tabindex', '-1'); | 259 iframe.setAttribute('tabindex', '-1'); |
| 248 | 260 |
| 249 iframe.onload = function() { | 261 iframe.onload = function() { |
| 250 client.setPlugin(iframe.contentWindow); | 262 client.setPlugin(iframe.contentWindow); |
| 251 }; | 263 }; |
| 252 | 264 |
| 253 // Add the functions to the iframe so that they can be called directly. | 265 // Add the functions to the iframe so that they can be called directly. |
| 254 iframe.setViewportChangedCallback = | 266 iframe.setViewportChangedCallback = |
| 255 client.setViewportChangedCallback.bind(client); | 267 client.setViewportChangedCallback.bind(client); |
| 256 iframe.setLoadCallback = client.setLoadCallback.bind(client); | 268 iframe.setLoadCallback = client.setLoadCallback.bind(client); |
| 257 iframe.setKeyEventCallback = client.setKeyEventCallback.bind(client); | 269 iframe.setKeyEventCallback = client.setKeyEventCallback.bind(client); |
| 258 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); | 270 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); |
| 259 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); | 271 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); |
| 260 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); | 272 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); |
| 261 return iframe; | 273 return iframe; |
| 262 } | 274 } |
| OLD | NEW |