Commit a2856803 authored by raymes@chromium.org's avatar raymes@chromium.org

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
parent 30d82149
...@@ -257,12 +257,23 @@ PDFViewer.prototype = { ...@@ -257,12 +257,23 @@ PDFViewer.prototype = {
this.pageIndicator_.initialFadeIn(); this.pageIndicator_.initialFadeIn();
this.toolbar_.initialFadeIn(); this.toolbar_.initialFadeIn();
break; break;
case 'loadProgress': case 'getAccessibilityJSONReply':
this.updateProgress_(message.data.progress); 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; break;
case 'goToPage': case 'goToPage':
this.viewport_.goToPage(message.data.page); this.viewport_.goToPage(message.data.page);
break; break;
case 'loadProgress':
this.updateProgress_(message.data.progress);
break;
case 'setScrollPosition': case 'setScrollPosition':
var position = this.viewport_.position; var position = this.viewport_.position;
if (message.data.x != undefined) if (message.data.x != undefined)
...@@ -271,14 +282,6 @@ PDFViewer.prototype = { ...@@ -271,14 +282,6 @@ PDFViewer.prototype = {
position.y = message.data.y; position.y = message.data.y;
this.viewport_.position = position; this.viewport_.position = position;
break; 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': case 'setTranslatedStrings':
this.passwordScreen_.text = message.data.getPasswordString; this.passwordScreen_.text = message.data.getPasswordString;
this.progressBar_.text = message.data.loadingString; this.progressBar_.text = message.data.loadingString;
...@@ -385,6 +388,7 @@ PDFViewer.prototype = { ...@@ -385,6 +388,7 @@ PDFViewer.prototype = {
}); });
break; break;
case 'loadPreviewPage': case 'loadPreviewPage':
case 'getAccessibilityJSON':
this.plugin_.postMessage(message.data); this.plugin_.postMessage(message.data);
break; break;
} }
......
...@@ -10,15 +10,16 @@ ...@@ -10,15 +10,16 @@
*/ */
function PDFScriptingAPI(window, extensionUrl) { function PDFScriptingAPI(window, extensionUrl) {
this.extensionUrl_ = extensionUrl; this.extensionUrl_ = extensionUrl;
this.readyToReceive = false;
this.messageQueue_ = []; this.messageQueue_ = [];
window.addEventListener('message', function(event) { 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; return;
}
switch (event.data.type) { switch (event.data.type) {
case 'readyToReceive': case 'readyToReceive':
this.pdfWindow_ = event.source; this.setDestinationWindow(event.source);
this.flushPendingMessages_();
break; break;
case 'viewport': case 'viewport':
if (this.viewportChangedCallback_) if (this.viewportChangedCallback_)
...@@ -32,6 +33,12 @@ function PDFScriptingAPI(window, extensionUrl) { ...@@ -32,6 +33,12 @@ function PDFScriptingAPI(window, extensionUrl) {
if (this.loadCallback_) if (this.loadCallback_)
this.loadCallback_(); this.loadCallback_();
break; break;
case 'getAccessibilityJSONReply':
if (this.accessibilityCallback_) {
this.accessibilityCallback_(event.data.json);
this.accessibilityCallback_ = null;
}
break;
} }
}.bind(this), false); }.bind(this), false);
} }
...@@ -42,10 +49,10 @@ PDFScriptingAPI.prototype = { ...@@ -42,10 +49,10 @@ PDFScriptingAPI.prototype = {
* Send a message to the extension. If we try to send messages prior to the * 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 * extension being ready to receive messages (i.e. before it has finished
* loading) we queue up the messages and flush them later. * 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) { sendMessage_: function(message) {
if (!this.readyToReceive) { if (!this.pdfWindow_) {
this.messageQueue_.push(message); this.messageQueue_.push(message);
return; return;
} }
...@@ -54,11 +61,13 @@ PDFScriptingAPI.prototype = { ...@@ -54,11 +61,13 @@ PDFScriptingAPI.prototype = {
}, },
/** /**
* @private * Sets the destination window containing the PDF viewer. This will be called
* Flushes all pending messages to the extension. * 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() { setDestinationWindow: function(pdfWindow) {
this.readyToReceive = true; this.pdfWindow_ = pdfWindow;
while (this.messageQueue_.length != 0) { while (this.messageQueue_.length != 0) {
this.pdfWindow_.postMessage(this.messageQueue_.shift(), this.pdfWindow_.postMessage(this.messageQueue_.shift(),
this.extensionUrl_); this.extensionUrl_);
...@@ -112,6 +121,29 @@ PDFScriptingAPI.prototype = { ...@@ -112,6 +121,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. * Send a key event to the extension.
* @param {number} keyCode the key code to send to the extension. * @param {number} keyCode the key code to send to the extension.
......
...@@ -18,6 +18,33 @@ var tests = [ ...@@ -18,6 +18,33 @@ var tests = [
chrome.test.assertEq(1066, sizer.offsetHeight); chrome.test.assertEq(1066, sizer.offsetHeight);
chrome.test.succeed(); 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() { window.addEventListener('pdfload', function() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment