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.pendingScriptingMessages_ = []; |
13 this.messageQueue_ = []; | 13 this.setPlugin(plugin); |
| 14 |
14 window.addEventListener('message', function(event) { | 15 window.addEventListener('message', function(event) { |
15 if (event.origin != this.extensionUrl_) { | 16 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai') { |
16 console.error('Received message that was not from the extension: ' + | 17 console.error('Received message that was not from the extension: ' + |
17 event); | 18 event); |
18 return; | 19 return; |
19 } | 20 } |
20 switch (event.data.type) { | 21 switch (event.data.type) { |
21 case 'readyToReceive': | |
22 this.setDestinationWindow(event.source); | |
23 break; | |
24 case 'viewport': | 22 case 'viewport': |
25 if (this.viewportChangedCallback_) | 23 if (this.viewportChangedCallback_) |
26 this.viewportChangedCallback_(event.data.pageX, | 24 this.viewportChangedCallback_(event.data.pageX, |
27 event.data.pageY, | 25 event.data.pageY, |
28 event.data.pageWidth, | 26 event.data.pageWidth, |
29 event.data.viewportWidth, | 27 event.data.viewportWidth, |
30 event.data.viewportHeight); | 28 event.data.viewportHeight); |
31 break; | 29 break; |
32 case 'documentLoaded': | 30 case 'documentLoaded': |
33 if (this.loadCallback_) | 31 if (this.loadCallback_) |
34 this.loadCallback_(); | 32 this.loadCallback_(); |
35 break; | 33 break; |
36 case 'getAccessibilityJSONReply': | 34 case 'getAccessibilityJSONReply': |
37 if (this.accessibilityCallback_) { | 35 if (this.accessibilityCallback_) { |
38 this.accessibilityCallback_(event.data.json); | 36 this.accessibilityCallback_(event.data.json); |
39 this.accessibilityCallback_ = null; | 37 this.accessibilityCallback_ = null; |
40 } | 38 } |
41 break; | 39 break; |
42 } | 40 } |
43 }.bind(this), false); | 41 }.bind(this), false); |
44 } | 42 } |
45 | 43 |
46 PDFScriptingAPI.prototype = { | 44 PDFScriptingAPI.prototype = { |
47 /** | 45 /** |
48 * @private | 46 * @private |
49 * Send a message to the extension. If we try to send messages prior to the | 47 * 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 | 48 * 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. | 49 * can happen in print preview). |
52 * @param {Object} message The message to send. | 50 * @param {Object} message The message to send. |
53 */ | 51 */ |
54 sendMessage_: function(message) { | 52 sendMessage_: function(message) { |
55 if (!this.pdfWindow_) { | 53 if (this.plugin_) |
56 this.messageQueue_.push(message); | 54 this.plugin_.postMessage(message, '*'); |
57 return; | 55 else |
58 } | 56 this.pendingScriptingMessages_.push(message); |
59 | |
60 this.pdfWindow_.postMessage(message, this.extensionUrl_); | |
61 }, | 57 }, |
62 | 58 |
63 /** | 59 /** |
64 * Sets the destination window containing the PDF viewer. This will be called | 60 * 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 | 61 * 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. | 62 * @param {Object} plugin the plugin element containing the PDF viewer. |
67 * @param {Window} pdfWindow the window containing the PDF viewer. | 63 */ |
68 */ | 64 setPlugin: function(plugin) { |
69 setDestinationWindow: function(pdfWindow) { | 65 this.plugin_ = plugin; |
70 this.pdfWindow_ = pdfWindow; | 66 |
71 while (this.messageQueue_.length != 0) { | 67 // Send an initialization message to the plugin indicating the window to |
72 this.pdfWindow_.postMessage(this.messageQueue_.shift(), | 68 // respond to. |
73 this.extensionUrl_); | 69 if (this.plugin_) { |
| 70 this.sendMessage_({ |
| 71 type: 'setParentWindow' |
| 72 }); |
| 73 |
| 74 // Now we can flush pending messages |
| 75 while (this.pendingScriptingMessages_.length > 0) |
| 76 this.sendMessage_(this.pendingScriptingMessages_.shift()); |
74 } | 77 } |
75 }, | 78 }, |
76 | 79 |
77 /** | 80 /** |
78 * Sets the callback which will be run when the PDF viewport changes. | 81 * Sets the callback which will be run when the PDF viewport changes. |
79 * @param {Function} callback the callback to be called. | 82 * @param {Function} callback the callback to be called. |
80 */ | 83 */ |
81 setViewportChangedCallback: function(callback) { | 84 setViewportChangedCallback: function(callback) { |
82 this.viewportChangedCallback_ = callback; | 85 this.viewportChangedCallback_ = callback; |
83 }, | 86 }, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 161 |
159 /** | 162 /** |
160 * Creates a PDF viewer with a scripting interface. This is basically 1) an | 163 * 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 | 164 * 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 | 165 * interface which provides access to various features of the viewer for use |
163 * by print preview and accessibility. | 166 * by print preview and accessibility. |
164 * @param {string} src the source URL of the PDF to load initially. | 167 * @param {string} src the source URL of the PDF to load initially. |
165 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. | 168 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. |
166 */ | 169 */ |
167 function PDFCreateOutOfProcessPlugin(src) { | 170 function PDFCreateOutOfProcessPlugin(src) { |
168 var EXTENSION_URL = 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai'; | |
169 var iframe = window.document.createElement('iframe'); | 171 var iframe = window.document.createElement('iframe'); |
170 iframe.setAttribute('src', EXTENSION_URL + '/index.html?' + src); | 172 iframe.setAttribute( |
171 var client = new PDFScriptingAPI(window, EXTENSION_URL); | 173 'src', |
172 | 174 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?' + src); |
| 175 var client = new PDFScriptingAPI(window); |
| 176 iframe.onload = function() { |
| 177 client.setPlugin(iframe.contentWindow); |
| 178 }; |
173 // Add the functions to the iframe so that they can be called directly. | 179 // Add the functions to the iframe so that they can be called directly. |
174 iframe.setViewportChangedCallback = | 180 iframe.setViewportChangedCallback = |
175 client.setViewportChangedCallback.bind(client); | 181 client.setViewportChangedCallback.bind(client); |
176 iframe.setLoadCallback = client.setLoadCallback.bind(client); | 182 iframe.setLoadCallback = client.setLoadCallback.bind(client); |
177 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); | 183 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); |
178 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); | 184 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); |
179 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); | 185 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); |
180 return iframe; | 186 return iframe; |
181 } | 187 } |
OLD | NEW |