diff options
author | raymes <raymes@chromium.org> | 2015-01-08 17:56:56 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-09 01:57:44 +0000 |
commit | 7b1df224a957da8376e0ae71ab2d53dabf658b0a (patch) | |
tree | 84ecf9b7d07d2785a79bfd606bd645bdc468b7c6 | |
parent | 03c38d9dc88a9b1b040218fbdd56ed4e2eb82653 (diff) | |
download | chromium_src-7b1df224a957da8376e0ae71ab2d53dabf658b0a.zip chromium_src-7b1df224a957da8376e0ae71ab2d53dabf658b0a.tar.gz chromium_src-7b1df224a957da8376e0ae71ab2d53dabf658b0a.tar.bz2 |
Add print and getSelectedText functions to the OOP PDF scripting API
These are needed by some internal users (see the bug for more details).
A test is added for selectAll/getSelectedText.
BUG=415858
Review URL: https://codereview.chromium.org/843463002
Cr-Commit-Position: refs/heads/master@{#310663}
-rw-r--r-- | chrome/browser/resources/pdf/pdf.js | 6 | ||||
-rw-r--r-- | chrome/browser/resources/pdf/pdf_scripting_api.js | 45 | ||||
-rw-r--r-- | chrome/test/data/pdf/basic_plugin_test.js | 10 | ||||
-rw-r--r-- | pdf/out_of_process_instance.cc | 10 |
4 files changed, 69 insertions, 2 deletions
diff --git a/chrome/browser/resources/pdf/pdf.js b/chrome/browser/resources/pdf/pdf.js index a8ec308..735c508 100644 --- a/chrome/browser/resources/pdf/pdf.js +++ b/chrome/browser/resources/pdf/pdf.js @@ -376,6 +376,9 @@ PDFViewer.prototype = { else this.passwordScreen_.deny(); break; + case 'getSelectedTextReply': + this.sendScriptingMessage_(message.data); + break; case 'goToPage': this.viewport_.goToPage(message.data.page); break; @@ -525,7 +528,10 @@ PDFViewer.prototype = { handleScriptingMessage: function(message) { switch (message.data.type.toString()) { case 'getAccessibilityJSON': + case 'getSelectedText': case 'loadPreviewPage': + case 'print': + case 'selectAll': this.plugin_.postMessage(message.data); break; case 'resetPrintPreviewMode': diff --git a/chrome/browser/resources/pdf/pdf_scripting_api.js b/chrome/browser/resources/pdf/pdf_scripting_api.js index 33556b9..e8dd4bc 100644 --- a/chrome/browser/resources/pdf/pdf_scripting_api.js +++ b/chrome/browser/resources/pdf/pdf_scripting_api.js @@ -39,6 +39,12 @@ function PDFScriptingAPI(window, plugin) { this.accessibilityCallback_ = null; } break; + case 'getSelectedTextReply': + if (this.selectedTextCallback_) { + this.selectedTextCallback_(event.data.selectedText); + this.selectedTextCallback_ = null; + } + break; } }.bind(this), false); } @@ -129,7 +135,8 @@ PDFScriptingAPI.prototype = { }, /** - * Get accessibility JSON for the document. + * Get accessibility JSON for the document. May only be called after document + * load. * @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 @@ -152,6 +159,42 @@ PDFScriptingAPI.prototype = { }, /** + * Select all the text in the document. May only be called after document + * load. + */ + selectAll: function() { + this.sendMessage_({ + type: 'selectAll' + }); + }, + + /** + * Get the selected text in the document. The callback will be called with the + * text that is selected. May only be called after document load. + * @param {Function} callback a callback to be called with the selected text. + * @return {boolean} true if the function is successful, false if there is an + * outstanding request for selected text that has not been answered. + */ + getSelectedText: function(callback) { + if (this.selectedTextCallback_) + return false; + this.selectedTextCallback_ = callback; + this.sendMessage_({ + type: 'getSelectedText' + }); + return true; + }, + + /** + * Print the document. May only be called after document load. + */ + print: function() { + this.sendMessage_({ + type: 'print' + }); + }, + + /** * 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 3a1e360..08d2714 100644 --- a/chrome/test/data/pdf/basic_plugin_test.js +++ b/chrome/test/data/pdf/basic_plugin_test.js @@ -45,7 +45,15 @@ var tests = [ chrome.test.assertEq('some more text', dict.textBox[1].textNodes[0].text); }), 0); - } + }, + + function testGetSelectedText() { + var client = new PDFScriptingAPI(window, window); + client.selectAll(); + client.getSelectedText(chrome.test.callbackPass(function(selectedText) { + chrome.test.assertEq('this is some text\nsome more text', selectedText); + })); + }, ]; var scriptingAPI = new PDFScriptingAPI(window, window); diff --git a/pdf/out_of_process_instance.cc b/pdf/out_of_process_instance.cc index 59b0439..e0ab99a 100644 --- a/pdf/out_of_process_instance.cc +++ b/pdf/out_of_process_instance.cc @@ -127,6 +127,11 @@ const char kJSRotateClockwiseType[] = "rotateClockwise"; const char kJSRotateCounterclockwiseType[] = "rotateCounterclockwise"; // Select all text in the document (Page -> Plugin) const char kJSSelectAllType[] = "selectAll"; +// Get the selected text in the document (Page -> Plugin) +const char kJSGetSelectedTextType[] = "getSelectedText"; +// Reply with selected text (Plugin -> Page) +const char kJSGetSelectedTextReplyType[] = "getSelectedTextReply"; +const char kJSSelectedText[] = "selectedText"; const int kFindResultCooldownMs = 100; @@ -444,6 +449,11 @@ void OutOfProcessInstance::HandleMessage(const pp::Var& message) { PostMessage(reply); } else if (type == kJSStopScrollingType) { stop_scrolling_ = true; + } else if (type == kJSGetSelectedTextType) { + pp::VarDictionary reply; + reply.Set(pp::Var(kType), pp::Var(kJSGetSelectedTextReplyType)); + reply.Set(pp::Var(kJSSelectedText), engine_->GetSelectedText()); + PostMessage(reply); } else { NOTREACHED(); } |