diff options
author | raymes@chromium.org <raymes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-12 05:35:10 +0000 |
---|---|---|
committer | raymes@chromium.org <raymes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-12 05:35:10 +0000 |
commit | a28568038542be354f9a92c7e887529348311100 (patch) | |
tree | ad3772300488d5fd82a106c10df06da8b5f1e699 | |
parent | 30d82149616dafe805d5fb22b1ddfcddfeee5a22 (diff) | |
download | chromium_src-a28568038542be354f9a92c7e887529348311100.zip chromium_src-a28568038542be354f9a92c7e887529348311100.tar.gz chromium_src-a28568038542be354f9a92c7e887529348311100.tar.bz2 |
Add accessibility function to PDFScriptingAPI
This adds the ability to fetch accessibility data from the plugin. It also adds a test for this function.
BUG=303491
Review URL: https://codereview.chromium.org/265283005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269742 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/resources/pdf/pdf.js | 24 | ||||
-rw-r--r-- | chrome/browser/resources/pdf/pdf_scripting_api.js | 52 | ||||
-rw-r--r-- | chrome/test/data/pdf/basic_plugin_test.js | 27 |
3 files changed, 83 insertions, 20 deletions
diff --git a/chrome/browser/resources/pdf/pdf.js b/chrome/browser/resources/pdf/pdf.js index 663ee2d..1e6938f 100644 --- a/chrome/browser/resources/pdf/pdf.js +++ b/chrome/browser/resources/pdf/pdf.js @@ -257,12 +257,23 @@ PDFViewer.prototype = { this.pageIndicator_.initialFadeIn(); this.toolbar_.initialFadeIn(); break; - case 'loadProgress': - this.updateProgress_(message.data.progress); + case 'getAccessibilityJSONReply': + this.sendScriptingMessage_(message.data); + break; + case 'getPassword': + // If the password screen isn't up, put it up. Otherwise we're + // responding to an incorrect password so deny it. + if (!this.passwordScreen_.active) + this.passwordScreen_.active = true; + else + this.passwordScreen_.deny(); break; case 'goToPage': this.viewport_.goToPage(message.data.page); break; + case 'loadProgress': + this.updateProgress_(message.data.progress); + break; case 'setScrollPosition': var position = this.viewport_.position; if (message.data.x != undefined) @@ -271,14 +282,6 @@ PDFViewer.prototype = { position.y = message.data.y; this.viewport_.position = position; break; - case 'getPassword': - // If the password screen isn't up, put it up. Otherwise we're - // responding to an incorrect password so deny it. - if (!this.passwordScreen_.active) - this.passwordScreen_.active = true; - else - this.passwordScreen_.deny(); - break; case 'setTranslatedStrings': this.passwordScreen_.text = message.data.getPasswordString; this.progressBar_.text = message.data.loadingString; @@ -385,6 +388,7 @@ PDFViewer.prototype = { }); break; case 'loadPreviewPage': + case 'getAccessibilityJSON': this.plugin_.postMessage(message.data); break; } diff --git a/chrome/browser/resources/pdf/pdf_scripting_api.js b/chrome/browser/resources/pdf/pdf_scripting_api.js index 69a0d53..81774f3 100644 --- a/chrome/browser/resources/pdf/pdf_scripting_api.js +++ b/chrome/browser/resources/pdf/pdf_scripting_api.js @@ -10,15 +10,16 @@ */ function PDFScriptingAPI(window, extensionUrl) { this.extensionUrl_ = extensionUrl; - this.readyToReceive = false; this.messageQueue_ = []; window.addEventListener('message', function(event) { - if (event.origin != this.extensionUrl_) + if (event.origin != this.extensionUrl_) { + console.error('Received message that was not from the extension: ' + + event); return; + } switch (event.data.type) { case 'readyToReceive': - this.pdfWindow_ = event.source; - this.flushPendingMessages_(); + this.setDestinationWindow(event.source); break; case 'viewport': if (this.viewportChangedCallback_) @@ -32,6 +33,12 @@ function PDFScriptingAPI(window, extensionUrl) { if (this.loadCallback_) this.loadCallback_(); break; + case 'getAccessibilityJSONReply': + if (this.accessibilityCallback_) { + this.accessibilityCallback_(event.data.json); + this.accessibilityCallback_ = null; + } + break; } }.bind(this), false); } @@ -42,10 +49,10 @@ PDFScriptingAPI.prototype = { * Send a message to the extension. If we try to send messages prior to the * extension being ready to receive messages (i.e. before it has finished * loading) we queue up the messages and flush them later. - * @param {MessageObject} the message to send. + * @param {Object} the message to send. */ sendMessage_: function(message) { - if (!this.readyToReceive) { + if (!this.pdfWindow_) { this.messageQueue_.push(message); return; } @@ -54,11 +61,13 @@ PDFScriptingAPI.prototype = { }, /** - * @private - * Flushes all pending messages to the extension. + * 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. */ - flushPendingMessages_: function() { - this.readyToReceive = true; + setDestinationWindow: function(pdfWindow) { + this.pdfWindow_ = pdfWindow; while (this.messageQueue_.length != 0) { this.pdfWindow_.postMessage(this.messageQueue_.shift(), this.extensionUrl_); @@ -113,6 +122,29 @@ PDFScriptingAPI.prototype = { }, /** + * Get accessibility JSON for the document. + * @param {Function} callback a callback to be called with the accessibility + * json that has been retrieved. + * @param {number} [page] the 0-indexed page number to get accessibility data + * for. If this is not provided, data about the entire document is + * returned. + * @return {boolean} true if the function is successful, false if there is an + * outstanding request for accessibility data that has not been answered. + */ + getAccessibilityJSON: function(callback, page) { + if (this.accessibilityCallback_) + return false; + this.accessibilityCallback_ = callback; + var message = { + type: 'getAccessibilityJSON', + }; + if (page || page == 0) + message.page = page; + this.sendMessage_(message); + return true; + }, + + /** * Send a key event to the extension. * @param {number} keyCode the key code to send to the extension. */ diff --git a/chrome/test/data/pdf/basic_plugin_test.js b/chrome/test/data/pdf/basic_plugin_test.js index 0413d46..8413c52 100644 --- a/chrome/test/data/pdf/basic_plugin_test.js +++ b/chrome/test/data/pdf/basic_plugin_test.js @@ -18,6 +18,33 @@ var tests = [ chrome.test.assertEq(1066, sizer.offsetHeight); chrome.test.succeed(); }, + + function testAccessibility() { + var client = new PDFScriptingAPI(window, window.location.origin); + client.setDestinationWindow(window); + client.getAccessibilityJSON(chrome.test.callbackPass(function(json) { + chrome.test.assertEq('{"copyable":true,"loaded":true,"numberOfPages":1}', + json); + })); + }, + + function testAccessibilityWithPage() { + var client = new PDFScriptingAPI(window, window.location.origin); + client.setDestinationWindow(window); + client.getAccessibilityJSON(chrome.test.callbackPass(function(json) { + var dict = JSON.parse(json); + chrome.test.assertEq(612, dict.width); + chrome.test.assertEq(792, dict.height); + chrome.test.assertEq(1.0, dict.textBox[0].fontSize); + chrome.test.assertEq('text', dict.textBox[0].textNodes[0].type); + chrome.test.assertEq('this is some text', + dict.textBox[0].textNodes[0].text); + chrome.test.assertEq(1.0, dict.textBox[1].fontSize); + chrome.test.assertEq('text', dict.textBox[1].textNodes[0].type); + chrome.test.assertEq('some more text', + dict.textBox[1].textNodes[0].text); + }), 0); + } ]; window.addEventListener('pdfload', function() { |