Chromium Code Reviews| 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 PDFMessagingClient. 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 {HTMLIFrameElement} iframe an iframe containing the PDF viewer. | 8 * @param {Window} window the window of the page containing pdfWindow. |
| 9 * @param {Window} window the window of the page containing the iframe. | |
| 10 * @param {string} extensionUrl the url of the PDF extension. | 9 * @param {string} extensionUrl the url of the PDF extension. |
| 11 */ | 10 */ |
| 12 function PDFMessagingClient(iframe, window, extensionUrl) { | 11 function PDFScriptingAPI(window, extensionUrl) { |
| 13 this.iframe_ = iframe; | |
| 14 this.extensionUrl_ = extensionUrl; | 12 this.extensionUrl_ = extensionUrl; |
| 15 this.readyToReceive_ = false; | 13 this.readyToReceive = false; |
|
Lei Zhang
2014/05/06 21:33:48
Why is this variable no longer named as a private
raymes
2014/05/07 00:30:00
I have a follow-up CL where I was going to use thi
| |
| 16 this.messageQueue_ = []; | 14 this.messageQueue_ = []; |
| 17 window.addEventListener('message', function(event) { | 15 window.addEventListener('message', function(event) { |
| 16 if (event.origin != this.extensionUrl_) | |
| 17 return; | |
| 18 switch (event.data.type) { | 18 switch (event.data.type) { |
| 19 case 'readyToReceive': | 19 case 'readyToReceive': |
| 20 this.pdfWindow_ = event.source; | |
| 20 this.flushPendingMessages_(); | 21 this.flushPendingMessages_(); |
| 21 break; | 22 break; |
| 22 case 'viewportChanged': | 23 case 'viewport': |
| 23 this.viewportChangedCallback_(event.data.pageX, | 24 if (this.viewportChangedCallback_) |
| 24 event.data.pageY, | 25 this.viewportChangedCallback_(event.data.pageX, |
| 25 event.data.pageWidth, | 26 event.data.pageY, |
| 26 event.data.viewportWidth, | 27 event.data.pageWidth, |
| 27 event.data.viewportHeight); | 28 event.data.viewportWidth, |
| 29 event.data.viewportHeight); | |
| 28 break; | 30 break; |
| 29 case 'documentLoaded': | 31 case 'documentLoaded': |
| 30 this.loadCallback_(); | 32 if (this.loadCallback_) |
| 33 this.loadCallback_(); | |
| 31 break; | 34 break; |
| 32 } | 35 } |
| 33 }.bind(this), false); | 36 }.bind(this), false); |
| 34 } | 37 } |
| 35 | 38 |
| 36 PDFMessagingClient.prototype = { | 39 PDFScriptingAPI.prototype = { |
| 37 /** | 40 /** |
| 38 * @private | 41 * @private |
| 39 * Send a message to the extension. If we try to send messages prior to the | 42 * Send a message to the extension. If we try to send messages prior to the |
| 40 * extension being ready to receive messages (i.e. before it has finished | 43 * extension being ready to receive messages (i.e. before it has finished |
| 41 * loading) we queue up the messages and flush them later. | 44 * loading) we queue up the messages and flush them later. |
| 42 * @param {MessageObject} the message to send. | 45 * @param {MessageObject} the message to send. |
| 43 */ | 46 */ |
| 44 sendMessage_: function(message) { | 47 sendMessage_: function(message) { |
| 45 if (!this.readyToReceive_) { | 48 if (!this.readyToReceive) { |
| 46 this.messageQueue_.push(message); | 49 this.messageQueue_.push(message); |
| 47 return; | 50 return; |
| 48 } | 51 } |
| 49 | 52 |
| 50 this.iframe_.contentWindow.postMessage(message, this.extensionUrl_); | 53 this.pdfWindow_.postMessage(message, this.extensionUrl_); |
| 51 }, | 54 }, |
| 52 | 55 |
| 53 /** | 56 /** |
| 54 * @private | 57 * @private |
| 55 * Flushes all pending messages to the extension. | 58 * Flushes all pending messages to the extension. |
| 56 */ | 59 */ |
| 57 flushPendingMessages_: function() { | 60 flushPendingMessages_: function() { |
| 58 this.readyToReceive_ = true; | 61 this.readyToReceive = true; |
| 59 while (this.messageQueue_.length != 0) { | 62 while (this.messageQueue_.length != 0) { |
| 60 this.iframe_.contentWindow.postMessage(this.messageQueue_.shift(), | 63 this.pdfWindow_.postMessage(this.messageQueue_.shift(), |
| 61 this.extensionUrl_); | 64 this.extensionUrl_); |
| 62 } | 65 } |
| 63 }, | 66 }, |
| 64 | 67 |
| 65 /** | 68 /** |
| 66 * Sets the callback which will be run when the PDF viewport changes. | 69 * Sets the callback which will be run when the PDF viewport changes. |
| 67 * @param {Function} callback the callback to be called. | 70 * @param {Function} callback the callback to be called. |
| 68 */ | 71 */ |
| 69 setViewportChangedCallback: function(callback) { | 72 setViewportChangedCallback: function(callback) { |
| 70 this.viewportChangedCallback_ = callback; | 73 this.viewportChangedCallback_ = callback; |
| 71 }, | 74 }, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 * iframe which is navigated to the PDF viewer extension and 2) a scripting | 127 * iframe which is navigated to the PDF viewer extension and 2) a scripting |
| 125 * interface which provides access to various features of the viewer for use | 128 * interface which provides access to various features of the viewer for use |
| 126 * by print preview and accessbility. | 129 * by print preview and accessbility. |
| 127 * @param {string} src the source URL of the PDF to load initially. | 130 * @param {string} src the source URL of the PDF to load initially. |
| 128 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. | 131 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. |
| 129 */ | 132 */ |
| 130 function PDFCreateOutOfProcessPlugin(src) { | 133 function PDFCreateOutOfProcessPlugin(src) { |
| 131 var EXTENSION_URL = 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai'; | 134 var EXTENSION_URL = 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai'; |
| 132 var iframe = window.document.createElement('iframe'); | 135 var iframe = window.document.createElement('iframe'); |
| 133 iframe.setAttribute('src', EXTENSION_URL + '/index.html?' + src); | 136 iframe.setAttribute('src', EXTENSION_URL + '/index.html?' + src); |
| 134 var client = new PDFMessagingClient(iframe, window, EXTENSION_URL); | 137 var client = new PDFScriptingAPI(window, EXTENSION_URL); |
| 135 | 138 |
| 136 // Add the functions to the iframe so that they can be called directly. | 139 // Add the functions to the iframe so that they can be called directly. |
| 137 iframe.setViewportChangedCallback = | 140 iframe.setViewportChangedCallback = |
| 138 client.setViewportChangedCallback.bind(client); | 141 client.setViewportChangedCallback.bind(client); |
| 139 iframe.setLoadCallback = client.setLoadCallback.bind(client); | 142 iframe.setLoadCallback = client.setLoadCallback.bind(client); |
| 140 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); | 143 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); |
| 141 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); | 144 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); |
| 142 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); | 145 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); |
| 143 return iframe; | 146 return iframe; |
| 144 } | 147 } |
| 145 | |
| 146 /** | |
| 147 * Create a new PDFMessagingHost. This is the extension-side of the scripting | |
| 148 * interface to the PDF viewer. It handles requests from a page which contains | |
| 149 * a PDF viewer extension and translates them into actions on the viewer. | |
| 150 * @param {Window} window the window containing the PDF extension. | |
| 151 * @param {PDFViewer} pdfViewer the object which provides access to the viewer. | |
| 152 */ | |
| 153 function PDFMessagingHost(window, pdfViewer) { | |
| 154 this.window_ = window; | |
| 155 this.pdfViewer_ = pdfViewer; | |
| 156 this.viewport_ = pdfViewer.viewport; | |
| 157 | |
| 158 window.addEventListener('message', function(event) { | |
| 159 switch (event.data.type) { | |
| 160 case 'resetPrintPreviewMode': | |
| 161 this.pdfViewer_.resetPrintPreviewMode( | |
| 162 event.data.url, | |
| 163 event.data.grayscale, | |
| 164 event.data.pageNumbers, | |
| 165 event.data.modifiable); | |
| 166 | |
| 167 break; | |
| 168 case 'loadPreviewPage': | |
| 169 this.pdfViewer_.loadPreviewPage(event.data.url, event.data.index); | |
| 170 break; | |
| 171 } | |
| 172 }.bind(this), false); | |
| 173 | |
| 174 if (this.window_.parent != this.window_) | |
| 175 this.sendMessage_({type: 'readyToReceive'}); | |
| 176 } | |
| 177 | |
| 178 PDFMessagingHost.prototype = { | |
| 179 sendMessage_: function(message) { | |
| 180 if (this.window_.parent == this.window_) | |
| 181 return; | |
| 182 this.window_.parent.postMessage(message, '*'); | |
| 183 }, | |
| 184 | |
| 185 viewportChanged: function() { | |
| 186 var visiblePage = this.viewport_.getMostVisiblePage(); | |
| 187 var pageDimensions = this.viewport_.getPageScreenRect(visiblePage); | |
| 188 var size = this.viewport_.size; | |
| 189 | |
| 190 this.sendMessage_({ | |
| 191 type: 'viewportChanged', | |
| 192 pageX: pageDimensions.x, | |
| 193 pageY: pageDimensions.y, | |
| 194 pageWidth: pageDimensions.width, | |
| 195 viewportWidth: size.width, | |
| 196 viewportHeight: size.height, | |
| 197 }); | |
| 198 }, | |
| 199 | |
| 200 documentLoaded: function() { | |
| 201 if (this.window_.parent == this.window_) | |
| 202 return; | |
| 203 this.sendMessage_({ type: 'documentLoaded' }); | |
| 204 } | |
| 205 }; | |
| OLD | NEW |