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 {string} extensionUrl the url of the PDF extension. |
10 */ | 10 */ |
11 function PDFScriptingAPI(window, extensionUrl) { | 11 function PDFScriptingAPI(window, extensionUrl) { |
12 this.extensionUrl_ = extensionUrl; | 12 this.extensionUrl_ = extensionUrl; |
13 this.readyToReceive = false; | |
14 this.messageQueue_ = []; | 13 this.messageQueue_ = []; |
15 window.addEventListener('message', function(event) { | 14 window.addEventListener('message', function(event) { |
16 if (event.origin != this.extensionUrl_) | 15 if (event.origin != this.extensionUrl_) { |
16 console.error('Received message that was not from the extension: ' + | |
17 event); | |
17 return; | 18 return; |
19 } | |
18 switch (event.data.type) { | 20 switch (event.data.type) { |
19 case 'readyToReceive': | 21 case 'readyToReceive': |
20 this.pdfWindow_ = event.source; | 22 this.setDestinationWindow(event.source); |
21 this.flushPendingMessages_(); | |
22 break; | 23 break; |
23 case 'viewport': | 24 case 'viewport': |
24 if (this.viewportChangedCallback_) | 25 if (this.viewportChangedCallback_) |
25 this.viewportChangedCallback_(event.data.pageX, | 26 this.viewportChangedCallback_(event.data.pageX, |
26 event.data.pageY, | 27 event.data.pageY, |
27 event.data.pageWidth, | 28 event.data.pageWidth, |
28 event.data.viewportWidth, | 29 event.data.viewportWidth, |
29 event.data.viewportHeight); | 30 event.data.viewportHeight); |
30 break; | 31 break; |
31 case 'documentLoaded': | 32 case 'documentLoaded': |
32 if (this.loadCallback_) | 33 if (this.loadCallback_) |
33 this.loadCallback_(); | 34 this.loadCallback_(); |
34 break; | 35 break; |
36 case 'getAccessibilityJSONReply': | |
37 if (this.accessibilityCallback_) { | |
38 this.accessibilityCallback_(event.data.json); | |
39 this.accessibilityCallback_ = null; | |
40 } | |
41 break; | |
35 } | 42 } |
36 }.bind(this), false); | 43 }.bind(this), false); |
37 } | 44 } |
38 | 45 |
39 PDFScriptingAPI.prototype = { | 46 PDFScriptingAPI.prototype = { |
40 /** | 47 /** |
41 * @private | 48 * @private |
42 * Send a message to the extension. If we try to send messages prior to the | 49 * Send a message to the extension. If we try to send messages prior to the |
43 * extension being ready to receive messages (i.e. before it has finished | 50 * extension being ready to receive messages (i.e. before it has finished |
44 * loading) we queue up the messages and flush them later. | 51 * loading) we queue up the messages and flush them later. |
45 * @param {MessageObject} the message to send. | 52 * @param {Object} the message to send. |
46 */ | 53 */ |
47 sendMessage_: function(message) { | 54 sendMessage_: function(message) { |
48 if (!this.readyToReceive) { | 55 if (!this.pdfWindow_) { |
49 this.messageQueue_.push(message); | 56 this.messageQueue_.push(message); |
50 return; | 57 return; |
51 } | 58 } |
52 | 59 |
53 this.pdfWindow_.postMessage(message, this.extensionUrl_); | 60 this.pdfWindow_.postMessage(message, this.extensionUrl_); |
54 }, | 61 }, |
55 | 62 |
56 /** | 63 /** |
57 * @private | 64 * Sets the destination window containing the PDF viewer. This will be called |
58 * Flushes all pending messages to the extension. | 65 * when a 'readyToReceive' message is received from the PDF viewer or it can |
66 * be called during tests. It then flushes any pending messages to the window. | |
67 * @param {Window} pdfWindow the window containing the PDF viewer. | |
59 */ | 68 */ |
60 flushPendingMessages_: function() { | 69 setDestinationWindow: function(pdfWindow) { |
61 this.readyToReceive = true; | 70 this.pdfWindow_ = pdfWindow; |
62 while (this.messageQueue_.length != 0) { | 71 while (this.messageQueue_.length != 0) { |
63 this.pdfWindow_.postMessage(this.messageQueue_.shift(), | 72 this.pdfWindow_.postMessage(this.messageQueue_.shift(), |
64 this.extensionUrl_); | 73 this.extensionUrl_); |
65 } | 74 } |
66 }, | 75 }, |
67 | 76 |
68 /** | 77 /** |
69 * Sets the callback which will be run when the PDF viewport changes. | 78 * Sets the callback which will be run when the PDF viewport changes. |
70 * @param {Function} callback the callback to be called. | 79 * @param {Function} callback the callback to be called. |
71 */ | 80 */ |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 */ | 115 */ |
107 loadPreviewPage: function(url, index) { | 116 loadPreviewPage: function(url, index) { |
108 this.sendMessage_({ | 117 this.sendMessage_({ |
109 type: 'loadPreviewPage', | 118 type: 'loadPreviewPage', |
110 url: url, | 119 url: url, |
111 index: index | 120 index: index |
112 }); | 121 }); |
113 }, | 122 }, |
114 | 123 |
115 /** | 124 /** |
125 * Get accessibility JSON for the document. | |
126 * @param {Function} callback a callback to be called with the accessibility | |
127 * json that has been retrieved. | |
128 * @param {number} [page] the index of the page to get accessibility data for. | |
Lei Zhang
2014/05/08 06:42:53
Is this 0 indexed or 1 indexed?
raymes
2014/05/09 06:19:45
0 indexed.
| |
129 * If this is not provided, data about the entire document is returned. | |
130 * @return {boolean} true if the function is successful, false if there is an | |
Lei Zhang
2014/05/08 06:42:53
Should we leave this as is, or queue up the reques
dmazzoni
2014/05/08 13:33:56
This should be fine, we can wait for the first pag
| |
131 * outstanding request for accessibility data that has not been answered. | |
132 */ | |
133 getAccessibilityJSON: function(callback, page) { | |
134 if (this.accessibilityCallback_) | |
135 return false; | |
136 this.accessibilityCallback_ = callback; | |
137 var message = { | |
138 type: 'getAccessibilityJSON', | |
139 }; | |
140 if (page || page == 0) | |
141 message.page = page; | |
142 this.sendMessage_(message); | |
143 return true; | |
144 }, | |
145 | |
146 /** | |
116 * Send a key event to the extension. | 147 * Send a key event to the extension. |
117 * @param {number} keyCode the key code to send to the extension. | 148 * @param {number} keyCode the key code to send to the extension. |
118 */ | 149 */ |
119 sendKeyEvent: function(keyCode) { | 150 sendKeyEvent: function(keyCode) { |
120 // TODO(raymes): Figure out a way to do this. It's only used to do scrolling | 151 // TODO(raymes): Figure out a way to do this. It's only used to do scrolling |
121 // the viewport, so probably just expose viewport controls instead. | 152 // the viewport, so probably just expose viewport controls instead. |
122 }, | 153 }, |
123 }; | 154 }; |
124 | 155 |
125 /** | 156 /** |
(...skipping 12 matching lines...) Expand all Loading... | |
138 | 169 |
139 // Add the functions to the iframe so that they can be called directly. | 170 // Add the functions to the iframe so that they can be called directly. |
140 iframe.setViewportChangedCallback = | 171 iframe.setViewportChangedCallback = |
141 client.setViewportChangedCallback.bind(client); | 172 client.setViewportChangedCallback.bind(client); |
142 iframe.setLoadCallback = client.setLoadCallback.bind(client); | 173 iframe.setLoadCallback = client.setLoadCallback.bind(client); |
143 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); | 174 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); |
144 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); | 175 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); |
145 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); | 176 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); |
146 return iframe; | 177 return iframe; |
147 } | 178 } |
OLD | NEW |