| 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) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 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 */ | 40 */ |
| 41 var LoadState = { | 41 var LoadState = {LOADING: 'loading', SUCCESS: 'success', FAILED: 'failed'}; |
| 42 LOADING: 'loading', | |
| 43 SUCCESS: 'success', | |
| 44 FAILED: 'failed' | |
| 45 }; | |
| 46 | 42 |
| 47 /** | 43 /** |
| 48 * Create a new PDFScriptingAPI. This provides a scripting interface to | 44 * 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. | 45 * 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. | 46 * @param {Window} window the window of the page containing the pdf viewer. |
| 51 * @param {Object} plugin the plugin element containing the pdf viewer. | 47 * @param {Object} plugin the plugin element containing the pdf viewer. |
| 52 */ | 48 */ |
| 53 function PDFScriptingAPI(window, plugin) { | 49 function PDFScriptingAPI(window, plugin) { |
| 54 this.loadState_ = LoadState.LOADING; | 50 this.loadState_ = LoadState.LOADING; |
| 55 this.pendingScriptingMessages_ = []; | 51 this.pendingScriptingMessages_ = []; |
| 56 this.setPlugin(plugin); | 52 this.setPlugin(plugin); |
| 57 | 53 |
| 58 window.addEventListener('message', function(event) { | 54 window.addEventListener('message', function(event) { |
| 59 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' && | 55 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' && |
| 60 event.origin != 'chrome://print') { | 56 event.origin != 'chrome://print') { |
| 61 console.error('Received message that was not from the extension: ' + | 57 console.error( |
| 62 event); | 58 'Received message that was not from the extension: ' + event); |
| 63 return; | 59 return; |
| 64 } | 60 } |
| 65 switch (event.data.type) { | 61 switch (event.data.type) { |
| 66 case 'viewport': | 62 case 'viewport': |
| 67 if (this.viewportChangedCallback_) | 63 if (this.viewportChangedCallback_) |
| 68 this.viewportChangedCallback_(event.data.pageX, | 64 this.viewportChangedCallback_( |
| 69 event.data.pageY, | 65 event.data.pageX, event.data.pageY, event.data.pageWidth, |
| 70 event.data.pageWidth, | 66 event.data.viewportWidth, event.data.viewportHeight); |
| 71 event.data.viewportWidth, | |
| 72 event.data.viewportHeight); | |
| 73 break; | 67 break; |
| 74 case 'documentLoaded': | 68 case 'documentLoaded': |
| 75 this.loadState_ = event.data.load_state; | 69 this.loadState_ = event.data.load_state; |
| 76 if (this.loadCallback_) | 70 if (this.loadCallback_) |
| 77 this.loadCallback_(this.loadState_ == LoadState.SUCCESS); | 71 this.loadCallback_(this.loadState_ == LoadState.SUCCESS); |
| 78 break; | 72 break; |
| 79 case 'getSelectedTextReply': | 73 case 'getSelectedTextReply': |
| 80 if (this.selectedTextCallback_) { | 74 if (this.selectedTextCallback_) { |
| 81 this.selectedTextCallback_(event.data.selectedText); | 75 this.selectedTextCallback_(event.data.selectedText); |
| 82 this.selectedTextCallback_ = null; | 76 this.selectedTextCallback_ = null; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 98 * can happen in print preview). | 92 * can happen in print preview). |
| 99 * @param {Object} message The message to send. | 93 * @param {Object} message The message to send. |
| 100 */ | 94 */ |
| 101 sendMessage_: function(message) { | 95 sendMessage_: function(message) { |
| 102 if (this.plugin_) | 96 if (this.plugin_) |
| 103 this.plugin_.postMessage(message, '*'); | 97 this.plugin_.postMessage(message, '*'); |
| 104 else | 98 else |
| 105 this.pendingScriptingMessages_.push(message); | 99 this.pendingScriptingMessages_.push(message); |
| 106 }, | 100 }, |
| 107 | 101 |
| 108 /** | 102 /** |
| 109 * Sets the plugin element containing the PDF viewer. The element will usually | 103 * Sets the plugin element containing the PDF viewer. The element will usually |
| 110 * be passed into the PDFScriptingAPI constructor but may also be set later. | 104 * be passed into the PDFScriptingAPI constructor but may also be set later. |
| 111 * @param {Object} plugin the plugin element containing the PDF viewer. | 105 * @param {Object} plugin the plugin element containing the PDF viewer. |
| 112 */ | 106 */ |
| 113 setPlugin: function(plugin) { | 107 setPlugin: function(plugin) { |
| 114 this.plugin_ = plugin; | 108 this.plugin_ = plugin; |
| 115 | 109 |
| 116 if (this.plugin_) { | 110 if (this.plugin_) { |
| 117 // Send a message to ensure the postMessage channel is initialized which | 111 // Send a message to ensure the postMessage channel is initialized which |
| 118 // allows us to receive messages. | 112 // allows us to receive messages. |
| 119 this.sendMessage_({ | 113 this.sendMessage_({type: 'initialize'}); |
| 120 type: 'initialize' | |
| 121 }); | |
| 122 // Flush pending messages. | 114 // Flush pending messages. |
| 123 while (this.pendingScriptingMessages_.length > 0) | 115 while (this.pendingScriptingMessages_.length > 0) |
| 124 this.sendMessage_(this.pendingScriptingMessages_.shift()); | 116 this.sendMessage_(this.pendingScriptingMessages_.shift()); |
| 125 } | 117 } |
| 126 }, | 118 }, |
| 127 | 119 |
| 128 /** | 120 /** |
| 129 * Sets the callback which will be run when the PDF viewport changes. | 121 * Sets the callback which will be run when the PDF viewport changes. |
| 130 * @param {Function} callback the callback to be called. | 122 * @param {Function} callback the callback to be called. |
| 131 */ | 123 */ |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 modifiable: modifiable | 161 modifiable: modifiable |
| 170 }); | 162 }); |
| 171 }, | 163 }, |
| 172 | 164 |
| 173 /** | 165 /** |
| 174 * Load a page into the document while in print preview mode. | 166 * Load a page into the document while in print preview mode. |
| 175 * @param {string} url the url of the pdf page to load. | 167 * @param {string} url the url of the pdf page to load. |
| 176 * @param {number} index the index of the page to load. | 168 * @param {number} index the index of the page to load. |
| 177 */ | 169 */ |
| 178 loadPreviewPage: function(url, index) { | 170 loadPreviewPage: function(url, index) { |
| 179 this.sendMessage_({ | 171 this.sendMessage_({type: 'loadPreviewPage', url: url, index: index}); |
| 180 type: 'loadPreviewPage', | |
| 181 url: url, | |
| 182 index: index | |
| 183 }); | |
| 184 }, | 172 }, |
| 185 | 173 |
| 186 /** | 174 /** |
| 187 * Select all the text in the document. May only be called after document | 175 * Select all the text in the document. May only be called after document |
| 188 * load. | 176 * load. |
| 189 */ | 177 */ |
| 190 selectAll: function() { | 178 selectAll: function() { |
| 191 this.sendMessage_({ | 179 this.sendMessage_({type: 'selectAll'}); |
| 192 type: 'selectAll' | |
| 193 }); | |
| 194 }, | 180 }, |
| 195 | 181 |
| 196 /** | 182 /** |
| 197 * Get the selected text in the document. The callback will be called with the | 183 * Get the selected text in the document. The callback will be called with the |
| 198 * text that is selected. May only be called after document load. | 184 * text that is selected. May only be called after document load. |
| 199 * @param {Function} callback a callback to be called with the selected text. | 185 * @param {Function} callback a callback to be called with the selected text. |
| 200 * @return {boolean} true if the function is successful, false if there is an | 186 * @return {boolean} true if the function is successful, false if there is an |
| 201 * outstanding request for selected text that has not been answered. | 187 * outstanding request for selected text that has not been answered. |
| 202 */ | 188 */ |
| 203 getSelectedText: function(callback) { | 189 getSelectedText: function(callback) { |
| 204 if (this.selectedTextCallback_) | 190 if (this.selectedTextCallback_) |
| 205 return false; | 191 return false; |
| 206 this.selectedTextCallback_ = callback; | 192 this.selectedTextCallback_ = callback; |
| 207 this.sendMessage_({ | 193 this.sendMessage_({type: 'getSelectedText'}); |
| 208 type: 'getSelectedText' | |
| 209 }); | |
| 210 return true; | 194 return true; |
| 211 }, | 195 }, |
| 212 | 196 |
| 213 /** | 197 /** |
| 214 * Print the document. May only be called after document load. | 198 * Print the document. May only be called after document load. |
| 215 */ | 199 */ |
| 216 print: function() { | 200 print: function() { |
| 217 this.sendMessage_({ | 201 this.sendMessage_({type: 'print'}); |
| 218 type: 'print' | |
| 219 }); | |
| 220 }, | 202 }, |
| 221 | 203 |
| 222 /** | 204 /** |
| 223 * Send a key event to the extension. | 205 * Send a key event to the extension. |
| 224 * @param {Event} keyEvent the key event to send to the extension. | 206 * @param {Event} keyEvent the key event to send to the extension. |
| 225 */ | 207 */ |
| 226 sendKeyEvent: function(keyEvent) { | 208 sendKeyEvent: function(keyEvent) { |
| 227 this.sendMessage_({ | 209 this.sendMessage_( |
| 228 type: 'sendKeyEvent', | 210 {type: 'sendKeyEvent', keyEvent: SerializeKeyEvent(keyEvent)}); |
| 229 keyEvent: SerializeKeyEvent(keyEvent) | |
| 230 }); | |
| 231 }, | 211 }, |
| 232 }; | 212 }; |
| 233 | 213 |
| 234 /** | 214 /** |
| 235 * Creates a PDF viewer with a scripting interface. This is basically 1) an | 215 * 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 | 216 * 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 | 217 * interface which provides access to various features of the viewer for use |
| 238 * by print preview and accessibility. | 218 * by print preview and accessibility. |
| 239 * @param {string} src the source URL of the PDF to load initially. | 219 * @param {string} src the source URL of the PDF to load initially. |
| 240 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. | 220 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 253 // Add the functions to the iframe so that they can be called directly. | 233 // Add the functions to the iframe so that they can be called directly. |
| 254 iframe.setViewportChangedCallback = | 234 iframe.setViewportChangedCallback = |
| 255 client.setViewportChangedCallback.bind(client); | 235 client.setViewportChangedCallback.bind(client); |
| 256 iframe.setLoadCallback = client.setLoadCallback.bind(client); | 236 iframe.setLoadCallback = client.setLoadCallback.bind(client); |
| 257 iframe.setKeyEventCallback = client.setKeyEventCallback.bind(client); | 237 iframe.setKeyEventCallback = client.setKeyEventCallback.bind(client); |
| 258 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); | 238 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); |
| 259 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); | 239 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); |
| 260 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); | 240 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); |
| 261 return iframe; | 241 return iframe; |
| 262 } | 242 } |
| OLD | NEW |