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 /** |
| 70 * @type {{ |
| 71 * pageX: number, |
| 72 * pageY: number, |
| 73 * pageWidth: number, |
| 74 * viewportWidth: number, |
| 75 * viewportHeight: number |
| 76 * }} |
| 77 */ |
| 78 var viewportData = event.data; |
67 if (this.viewportChangedCallback_) | 79 if (this.viewportChangedCallback_) |
68 this.viewportChangedCallback_(event.data.pageX, | 80 this.viewportChangedCallback_(viewportData.pageX, |
69 event.data.pageY, | 81 viewportData.pageY, |
70 event.data.pageWidth, | 82 viewportData.pageWidth, |
71 event.data.viewportWidth, | 83 viewportData.viewportWidth, |
72 event.data.viewportHeight); | 84 viewportData.viewportHeight); |
73 break; | 85 break; |
74 case 'documentLoaded': | 86 case 'documentLoaded': |
75 this.loadState_ = event.data.load_state; | 87 var data = /** @type {{load_state: LoadState}} */ (event.data); |
| 88 this.loadState_ = data.load_state; |
76 if (this.loadCallback_) | 89 if (this.loadCallback_) |
77 this.loadCallback_(this.loadState_ == LoadState.SUCCESS); | 90 this.loadCallback_(this.loadState_ == LoadState.SUCCESS); |
78 break; | 91 break; |
79 case 'getSelectedTextReply': | 92 case 'getSelectedTextReply': |
| 93 var data = /** @type {{selectedText: string}} */ (event.data); |
80 if (this.selectedTextCallback_) { | 94 if (this.selectedTextCallback_) { |
81 this.selectedTextCallback_(event.data.selectedText); | 95 this.selectedTextCallback_(data.selectedText); |
82 this.selectedTextCallback_ = null; | 96 this.selectedTextCallback_ = null; |
83 } | 97 } |
84 break; | 98 break; |
85 case 'sendKeyEvent': | 99 case 'sendKeyEvent': |
86 if (this.keyEventCallback_) | 100 if (this.keyEventCallback_) |
87 this.keyEventCallback_(DeserializeKeyEvent(event.data.keyEvent)); | 101 this.keyEventCallback_(DeserializeKeyEvent(event.data.keyEvent)); |
88 break; | 102 break; |
89 } | 103 } |
90 }.bind(this), false); | 104 }.bind(this), false); |
91 } | 105 } |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 247 |
234 /** | 248 /** |
235 * Creates a PDF viewer with a scripting interface. This is basically 1) an | 249 * 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 | 250 * 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 | 251 * interface which provides access to various features of the viewer for use |
238 * by print preview and accessibility. | 252 * by print preview and accessibility. |
239 * @param {string} src the source URL of the PDF to load initially. | 253 * @param {string} src the source URL of the PDF to load initially. |
240 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. | 254 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. |
241 */ | 255 */ |
242 function PDFCreateOutOfProcessPlugin(src) { | 256 function PDFCreateOutOfProcessPlugin(src) { |
243 var client = new PDFScriptingAPI(window); | 257 var client = new PDFScriptingAPI(window, null); |
244 var iframe = window.document.createElement('iframe'); | 258 var iframe = assertInstanceof(window.document.createElement('iframe'), |
| 259 HTMLIFrameElement); |
245 iframe.setAttribute('src', 'pdf_preview.html?' + src); | 260 iframe.setAttribute('src', 'pdf_preview.html?' + src); |
246 // Prevent the frame from being tab-focusable. | 261 // Prevent the frame from being tab-focusable. |
247 iframe.setAttribute('tabindex', '-1'); | 262 iframe.setAttribute('tabindex', '-1'); |
248 | 263 |
249 iframe.onload = function() { | 264 iframe.onload = function() { |
250 client.setPlugin(iframe.contentWindow); | 265 client.setPlugin(iframe.contentWindow); |
251 }; | 266 }; |
252 | 267 |
253 // Add the functions to the iframe so that they can be called directly. | 268 // Add the functions to the iframe so that they can be called directly. |
254 iframe.setViewportChangedCallback = | 269 iframe.setViewportChangedCallback = |
255 client.setViewportChangedCallback.bind(client); | 270 client.setViewportChangedCallback.bind(client); |
256 iframe.setLoadCallback = client.setLoadCallback.bind(client); | 271 iframe.setLoadCallback = client.setLoadCallback.bind(client); |
257 iframe.setKeyEventCallback = client.setKeyEventCallback.bind(client); | 272 iframe.setKeyEventCallback = client.setKeyEventCallback.bind(client); |
258 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); | 273 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); |
259 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); | 274 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); |
260 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); | 275 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); |
261 return iframe; | 276 return iframe; |
262 } | 277 } |
OLD | NEW |