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 * Create a new PDFScriptingAPI. This provides a scripting interface to | 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. | 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. | 8 * @param {Window} window the window of the page containing the pdf viewer. |
9 * @param {Object} plugin the plugin element containing the pdf viewer. | 9 * @param {Object} plugin the plugin element containing the pdf viewer. |
10 */ | 10 */ |
11 function PDFScriptingAPI(window, plugin) { | 11 function PDFScriptingAPI(window, plugin) { |
12 this.loaded_ = false; | |
13 this.pendingScriptingMessages_ = []; | 12 this.pendingScriptingMessages_ = []; |
14 this.setPlugin(plugin); | 13 this.setPlugin(plugin); |
15 | 14 |
16 window.addEventListener('message', function(event) { | 15 window.addEventListener('message', function(event) { |
17 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai') { | 16 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai') { |
18 console.error('Received message that was not from the extension: ' + | 17 console.error('Received message that was not from the extension: ' + |
19 event); | 18 event); |
20 return; | 19 return; |
21 } | 20 } |
22 switch (event.data.type) { | 21 switch (event.data.type) { |
23 case 'viewport': | 22 case 'viewport': |
24 if (this.viewportChangedCallback_) | 23 if (this.viewportChangedCallback_) |
25 this.viewportChangedCallback_(event.data.pageX, | 24 this.viewportChangedCallback_(event.data.pageX, |
26 event.data.pageY, | 25 event.data.pageY, |
27 event.data.pageWidth, | 26 event.data.pageWidth, |
28 event.data.viewportWidth, | 27 event.data.viewportWidth, |
29 event.data.viewportHeight); | 28 event.data.viewportHeight); |
30 break; | 29 break; |
31 case 'documentLoaded': | 30 case 'documentLoaded': |
32 this.loaded_ = true; | 31 if (this.loadCallback_) { |
33 if (this.loadCallback_) | |
34 this.loadCallback_(); | 32 this.loadCallback_(); |
| 33 this.loadCallback_ = null; |
| 34 } |
35 break; | 35 break; |
36 case 'getAccessibilityJSONReply': | 36 case 'getAccessibilityJSONReply': |
37 if (this.accessibilityCallback_) { | 37 if (this.accessibilityCallback_) { |
38 this.accessibilityCallback_(event.data.json); | 38 this.accessibilityCallback_(event.data.json); |
39 this.accessibilityCallback_ = null; | 39 this.accessibilityCallback_ = null; |
40 } | 40 } |
41 break; | 41 break; |
42 case 'getSelectedTextReply': | 42 case 'getSelectedTextReply': |
43 if (this.selectedTextCallback_) { | 43 if (this.selectedTextCallback_) { |
44 this.selectedTextCallback_(event.data.selectedText); | 44 this.selectedTextCallback_(event.data.selectedText); |
(...skipping 20 matching lines...) Expand all Loading... |
65 }, | 65 }, |
66 | 66 |
67 /** | 67 /** |
68 * Sets the plugin element containing the PDF viewer. The element will usually | 68 * Sets the plugin element containing the PDF viewer. The element will usually |
69 * be passed into the PDFScriptingAPI constructor but may also be set later. | 69 * be passed into the PDFScriptingAPI constructor but may also be set later. |
70 * @param {Object} plugin the plugin element containing the PDF viewer. | 70 * @param {Object} plugin the plugin element containing the PDF viewer. |
71 */ | 71 */ |
72 setPlugin: function(plugin) { | 72 setPlugin: function(plugin) { |
73 this.plugin_ = plugin; | 73 this.plugin_ = plugin; |
74 | 74 |
75 // Send an initialization message to the plugin indicating the window to | |
76 // respond to. | |
77 if (this.plugin_) { | 75 if (this.plugin_) { |
78 this.sendMessage_({ | 76 // Flush pending messages. |
79 type: 'setParentWindow' | |
80 }); | |
81 | |
82 // Now we can flush pending messages | |
83 while (this.pendingScriptingMessages_.length > 0) | 77 while (this.pendingScriptingMessages_.length > 0) |
84 this.sendMessage_(this.pendingScriptingMessages_.shift()); | 78 this.sendMessage_(this.pendingScriptingMessages_.shift()); |
85 } | 79 } |
86 }, | 80 }, |
87 | 81 |
88 /** | 82 /** |
89 * Sets the callback which will be run when the PDF viewport changes. | 83 * Sets the callback which will be run when the PDF viewport changes. |
90 * @param {Function} callback the callback to be called. | 84 * @param {Function} callback the callback to be called. |
91 */ | 85 */ |
92 setViewportChangedCallback: function(callback) { | 86 setViewportChangedCallback: function(callback) { |
93 this.viewportChangedCallback_ = callback; | 87 this.viewportChangedCallback_ = callback; |
94 }, | 88 }, |
95 | 89 |
96 /** | 90 /** |
97 * Sets the callback which will be run when the PDF document has finished | 91 * Sets the callback which will be run when the PDF document has finished |
98 * loading. If the document is already loaded, it will be run immediately. | 92 * loading. If the document is already loaded, it will be run immediately. |
99 * @param {Function} callback the callback to be called. | 93 * @param {Function} callback the callback to be called. |
| 94 * @return {boolean} false if there is a callback already set and true |
| 95 * otherwise. |
100 */ | 96 */ |
101 setLoadCallback: function(callback) { | 97 setLoadCallback: function(callback) { |
| 98 if (this.loadCallback_) |
| 99 return false; |
102 this.loadCallback_ = callback; | 100 this.loadCallback_ = callback; |
103 if (this.loaded_ && callback) | 101 this.sendMessage_({ |
104 callback(); | 102 type: 'isDocumentLoaded' |
| 103 }); |
| 104 return true; |
105 }, | 105 }, |
106 | 106 |
107 /** | 107 /** |
108 * Resets the PDF viewer into print preview mode. | 108 * Resets the PDF viewer into print preview mode. |
109 * @param {string} url the url of the PDF to load. | 109 * @param {string} url the url of the PDF to load. |
110 * @param {boolean} grayscale whether or not to display the PDF in grayscale. | 110 * @param {boolean} grayscale whether or not to display the PDF in grayscale. |
111 * @param {Array.<number>} pageNumbers an array of the page numbers. | 111 * @param {Array.<number>} pageNumbers an array of the page numbers. |
112 * @param {boolean} modifiable whether or not the document is modifiable. | 112 * @param {boolean} modifiable whether or not the document is modifiable. |
113 */ | 113 */ |
114 resetPrintPreviewMode: function(url, grayscale, pageNumbers, modifiable) { | 114 resetPrintPreviewMode: function(url, grayscale, pageNumbers, modifiable) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 }; | 225 }; |
226 // Add the functions to the iframe so that they can be called directly. | 226 // Add the functions to the iframe so that they can be called directly. |
227 iframe.setViewportChangedCallback = | 227 iframe.setViewportChangedCallback = |
228 client.setViewportChangedCallback.bind(client); | 228 client.setViewportChangedCallback.bind(client); |
229 iframe.setLoadCallback = client.setLoadCallback.bind(client); | 229 iframe.setLoadCallback = client.setLoadCallback.bind(client); |
230 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); | 230 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); |
231 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); | 231 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); |
232 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); | 232 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); |
233 return iframe; | 233 return iframe; |
234 } | 234 } |
OLD | NEW |