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 {string} extensionUrl the url of the PDF extension. | 9 * @param {Object} plugin the plugin element containing the pdf viewer. |
10 */ | 10 */ |
11 function PDFScriptingAPI(window, extensionUrl) { | 11 function PDFScriptingAPI(window, plugin) { |
12 this.extensionUrl_ = extensionUrl; | 12 this.loaded_ = false; |
13 this.messageQueue_ = []; | 13 this.pendingScriptingMessages_ = []; |
| 14 this.setPlugin(plugin); |
| 15 |
14 window.addEventListener('message', function(event) { | 16 window.addEventListener('message', function(event) { |
15 if (event.origin != this.extensionUrl_) { | 17 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai') { |
16 console.error('Received message that was not from the extension: ' + | 18 console.error('Received message that was not from the extension: ' + |
17 event); | 19 event); |
18 return; | 20 return; |
19 } | 21 } |
20 switch (event.data.type) { | 22 switch (event.data.type) { |
21 case 'readyToReceive': | |
22 this.setDestinationWindow(event.source); | |
23 break; | |
24 case 'viewport': | 23 case 'viewport': |
25 if (this.viewportChangedCallback_) | 24 if (this.viewportChangedCallback_) |
26 this.viewportChangedCallback_(event.data.pageX, | 25 this.viewportChangedCallback_(event.data.pageX, |
27 event.data.pageY, | 26 event.data.pageY, |
28 event.data.pageWidth, | 27 event.data.pageWidth, |
29 event.data.viewportWidth, | 28 event.data.viewportWidth, |
30 event.data.viewportHeight); | 29 event.data.viewportHeight); |
31 break; | 30 break; |
32 case 'documentLoaded': | 31 case 'documentLoaded': |
| 32 this.loaded_ = true; |
33 if (this.loadCallback_) | 33 if (this.loadCallback_) |
34 this.loadCallback_(); | 34 this.loadCallback_(); |
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 } | 42 } |
43 }.bind(this), false); | 43 }.bind(this), false); |
44 } | 44 } |
45 | 45 |
46 PDFScriptingAPI.prototype = { | 46 PDFScriptingAPI.prototype = { |
47 /** | 47 /** |
48 * @private | 48 * @private |
49 * Send a message to the extension. If we try to send messages prior to the | 49 * Send a message to the extension. If messages try to get sent before there |
50 * extension being ready to receive messages (i.e. before it has finished | 50 * is a plugin element set, then we queue them up and send them later (this |
51 * loading) we queue up the messages and flush them later. | 51 * can happen in print preview). |
52 * @param {Object} message The message to send. | 52 * @param {Object} message The message to send. |
53 */ | 53 */ |
54 sendMessage_: function(message) { | 54 sendMessage_: function(message) { |
55 if (!this.pdfWindow_) { | 55 if (this.plugin_) |
56 this.messageQueue_.push(message); | 56 this.plugin_.postMessage(message, '*'); |
57 return; | 57 else |
58 } | 58 this.pendingScriptingMessages_.push(message); |
59 | |
60 this.pdfWindow_.postMessage(message, this.extensionUrl_); | |
61 }, | 59 }, |
62 | 60 |
63 /** | 61 /** |
64 * Sets the destination window containing the PDF viewer. This will be called | 62 * Sets the plugin element containing the PDF viewer. The element will usually |
65 * when a 'readyToReceive' message is received from the PDF viewer or it can | 63 * be passed into the PDFScriptingAPI constructor but may also be set later. |
66 * be called during tests. It then flushes any pending messages to the window. | 64 * @param {Object} plugin the plugin element containing the PDF viewer. |
67 * @param {Window} pdfWindow the window containing the PDF viewer. | 65 */ |
68 */ | 66 setPlugin: function(plugin) { |
69 setDestinationWindow: function(pdfWindow) { | 67 this.plugin_ = plugin; |
70 this.pdfWindow_ = pdfWindow; | 68 |
71 while (this.messageQueue_.length != 0) { | 69 // Send an initialization message to the plugin indicating the window to |
72 this.pdfWindow_.postMessage(this.messageQueue_.shift(), | 70 // respond to. |
73 this.extensionUrl_); | 71 if (this.plugin_) { |
| 72 this.sendMessage_({ |
| 73 type: 'setParentWindow' |
| 74 }); |
| 75 |
| 76 // Now we can flush pending messages |
| 77 while (this.pendingScriptingMessages_.length > 0) |
| 78 this.sendMessage_(this.pendingScriptingMessages_.shift()); |
74 } | 79 } |
75 }, | 80 }, |
76 | 81 |
77 /** | 82 /** |
78 * 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. |
79 * @param {Function} callback the callback to be called. | 84 * @param {Function} callback the callback to be called. |
80 */ | 85 */ |
81 setViewportChangedCallback: function(callback) { | 86 setViewportChangedCallback: function(callback) { |
82 this.viewportChangedCallback_ = callback; | 87 this.viewportChangedCallback_ = callback; |
83 }, | 88 }, |
84 | 89 |
85 /** | 90 /** |
86 * 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 |
87 * loading. | 92 * loading. If the document is already loaded, it will be run immediately. |
88 * @param {Function} callback the callback to be called. | 93 * @param {Function} callback the callback to be called. |
89 */ | 94 */ |
90 setLoadCallback: function(callback) { | 95 setLoadCallback: function(callback) { |
91 this.loadCallback_ = callback; | 96 this.loadCallback_ = callback; |
| 97 if (this.loaded_ && callback) |
| 98 callback(); |
92 }, | 99 }, |
93 | 100 |
94 /** | 101 /** |
95 * Resets the PDF viewer into print preview mode. | 102 * Resets the PDF viewer into print preview mode. |
96 * @param {string} url the url of the PDF to load. | 103 * @param {string} url the url of the PDF to load. |
97 * @param {boolean} grayscale whether or not to display the PDF in grayscale. | 104 * @param {boolean} grayscale whether or not to display the PDF in grayscale. |
98 * @param {Array.<number>} pageNumbers an array of the page numbers. | 105 * @param {Array.<number>} pageNumbers an array of the page numbers. |
99 * @param {boolean} modifiable whether or not the document is modifiable. | 106 * @param {boolean} modifiable whether or not the document is modifiable. |
100 */ | 107 */ |
101 resetPrintPreviewMode: function(url, grayscale, pageNumbers, modifiable) { | 108 resetPrintPreviewMode: function(url, grayscale, pageNumbers, modifiable) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 165 |
159 /** | 166 /** |
160 * Creates a PDF viewer with a scripting interface. This is basically 1) an | 167 * Creates a PDF viewer with a scripting interface. This is basically 1) an |
161 * iframe which is navigated to the PDF viewer extension and 2) a scripting | 168 * iframe which is navigated to the PDF viewer extension and 2) a scripting |
162 * interface which provides access to various features of the viewer for use | 169 * interface which provides access to various features of the viewer for use |
163 * by print preview and accessibility. | 170 * by print preview and accessibility. |
164 * @param {string} src the source URL of the PDF to load initially. | 171 * @param {string} src the source URL of the PDF to load initially. |
165 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. | 172 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. |
166 */ | 173 */ |
167 function PDFCreateOutOfProcessPlugin(src) { | 174 function PDFCreateOutOfProcessPlugin(src) { |
168 var EXTENSION_URL = 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai'; | |
169 var iframe = window.document.createElement('iframe'); | 175 var iframe = window.document.createElement('iframe'); |
170 iframe.setAttribute('src', EXTENSION_URL + '/index.html?' + src); | 176 iframe.setAttribute( |
171 var client = new PDFScriptingAPI(window, EXTENSION_URL); | 177 'src', |
172 | 178 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?' + src); |
| 179 var client = new PDFScriptingAPI(window); |
| 180 iframe.onload = function() { |
| 181 client.setPlugin(iframe.contentWindow); |
| 182 }; |
173 // Add the functions to the iframe so that they can be called directly. | 183 // Add the functions to the iframe so that they can be called directly. |
174 iframe.setViewportChangedCallback = | 184 iframe.setViewportChangedCallback = |
175 client.setViewportChangedCallback.bind(client); | 185 client.setViewportChangedCallback.bind(client); |
176 iframe.setLoadCallback = client.setLoadCallback.bind(client); | 186 iframe.setLoadCallback = client.setLoadCallback.bind(client); |
177 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); | 187 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); |
178 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); | 188 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); |
179 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); | 189 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); |
180 return iframe; | 190 return iframe; |
181 } | 191 } |
OLD | NEW |