Index: chrome/browser/resources/pdf/pdf_scripting_api.js |
diff --git a/chrome/browser/resources/pdf/pdf_scripting_api.js b/chrome/browser/resources/pdf/pdf_scripting_api.js |
index 564467f9c81802d8d87392eab644b553c8dbd000..85a9b2755e985badf285ad09ebdbc18167dfba81 100644 |
--- a/chrome/browser/resources/pdf/pdf_scripting_api.js |
+++ b/chrome/browser/resources/pdf/pdf_scripting_api.js |
@@ -6,21 +6,19 @@ |
* Create a new PDFScriptingAPI. This provides a scripting interface to |
* the PDF viewer so that it can be customized by things like print preview. |
* @param {Window} window the window of the page containing the pdf viewer. |
- * @param {string} extensionUrl the url of the PDF extension. |
+ * @param {string} plugin the plugin element containing the pdf viewer. |
Sam McNally
2015/01/06 05:31:04
Not a string anymore.
raymes
2015/01/07 03:17:58
Done.
|
*/ |
-function PDFScriptingAPI(window, extensionUrl) { |
- this.extensionUrl_ = extensionUrl; |
+function PDFScriptingAPI(window, plugin) { |
+ this.setPlugin(plugin); |
this.messageQueue_ = []; |
+ |
window.addEventListener('message', function(event) { |
- if (event.origin != this.extensionUrl_) { |
+ if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai') { |
console.error('Received message that was not from the extension: ' + |
event); |
return; |
} |
switch (event.data.type) { |
- case 'readyToReceive': |
- this.setDestinationWindow(event.source); |
- break; |
case 'viewport': |
if (this.viewportChangedCallback_) |
this.viewportChangedCallback_(event.data.pageX, |
@@ -39,6 +37,11 @@ function PDFScriptingAPI(window, extensionUrl) { |
this.accessibilityCallback_ = null; |
} |
break; |
+ case 'readyToReceive': |
+ // Safely flush any pending messages. |
+ while (this.messageQueue_.length > 0) |
+ this.sendMessage_(this.messageQueue_.shift()); |
+ break; |
} |
}.bind(this), false); |
} |
@@ -52,25 +55,26 @@ PDFScriptingAPI.prototype = { |
* @param {Object} message The message to send. |
*/ |
sendMessage_: function(message) { |
- if (!this.pdfWindow_) { |
+ if (this.plugin_) |
+ this.plugin_.postMessage(message, '*'); |
+ else |
this.messageQueue_.push(message); |
- return; |
- } |
- |
- this.pdfWindow_.postMessage(message, this.extensionUrl_); |
}, |
- /** |
- * Sets the destination window containing the PDF viewer. This will be called |
- * when a 'readyToReceive' message is received from the PDF viewer or it can |
- * be called during tests. It then flushes any pending messages to the window. |
- * @param {Window} pdfWindow the window containing the PDF viewer. |
- */ |
- setDestinationWindow: function(pdfWindow) { |
- this.pdfWindow_ = pdfWindow; |
- while (this.messageQueue_.length != 0) { |
- this.pdfWindow_.postMessage(this.messageQueue_.shift(), |
- this.extensionUrl_); |
+ /** |
+ * Sets the plugin element containing the PDF viewer. The element will usually |
+ * be passed into the PDFScriptingAPI constructor but may also be set later. |
+ * @param {Object} plugin the plugin element containing the PDF viewer. |
+ */ |
+ setPlugin: function(plugin) { |
+ this.plugin_ = plugin; |
+ |
+ // Send an initialization message to the plugin which allows it to respond |
+ // to the page containing it. |
+ if (this.plugin_) { |
+ this.sendMessage_({ |
+ type: 'setParentWindow', |
+ }); |
} |
}, |
@@ -165,11 +169,13 @@ PDFScriptingAPI.prototype = { |
* @return {HTMLIFrameElement} the iframe element containing the PDF viewer. |
*/ |
function PDFCreateOutOfProcessPlugin(src) { |
- var EXTENSION_URL = 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai'; |
var iframe = window.document.createElement('iframe'); |
- iframe.setAttribute('src', EXTENSION_URL + '/index.html?' + src); |
- var client = new PDFScriptingAPI(window, EXTENSION_URL); |
- |
+ iframe.setAttribute('src', |
+ 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?' + src); |
Sam McNally
2015/01/06 05:31:04
Either line this up with the ( above or break the
raymes
2015/01/07 03:17:58
Done.
|
+ var client = new PDFScriptingAPI(window); |
+ iframe.onload = function() { |
+ client.setPlugin(iframe.contentWindow); |
+ }; |
// Add the functions to the iframe so that they can be called directly. |
iframe.setViewportChangedCallback = |
client.setViewportChangedCallback.bind(client); |