Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Unified Diff: chrome/browser/resources/pdf/pdf.js

Issue 870453002: Make the scripting OOP PDF API easier to use (take 2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf_scripting_api.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/pdf/pdf.js
diff --git a/chrome/browser/resources/pdf/pdf.js b/chrome/browser/resources/pdf/pdf.js
index c9b50d9d9c72eda9eee4a42f1cf9e631ee3c64a1..3883709bb3df6dc21f96d51f319698aa2119dc36 100644
--- a/chrome/browser/resources/pdf/pdf.js
+++ b/chrome/browser/resources/pdf/pdf.js
@@ -48,6 +48,8 @@ function PDFViewer(streamDetails) {
this.loaded_ = false;
this.parentWindow_ = null;
+ this.delayedScriptingMessages_ = [];
+
this.isPrintPreview_ =
this.streamDetails_.originalUrl.indexOf('chrome://print') == 0;
this.isMaterial_ = location.pathname.substring(1) == 'index-material.html';
@@ -340,9 +342,8 @@ PDFViewer.prototype = {
this.viewport_.position = this.lastViewportPosition_;
this.handleURLParams_();
this.loaded_ = true;
- this.sendScriptingMessage_({
- type: 'documentLoaded'
- });
+ while (this.delayedScriptingMessages_.length > 0)
+ this.handleScriptingMessage(this.delayedScriptingMessages_.shift());
}
},
@@ -613,25 +614,57 @@ PDFViewer.prototype = {
},
/**
- * @private
* Handle a scripting message from outside the extension (typically sent by
* PDFScriptingAPI in a page containing the extension) to interact with the
* plugin.
* @param {MessageObject} message the message to handle.
*/
handleScriptingMessage: function(message) {
+ if (this.parentWindow_ != message.source)
+ this.parentWindow_ = message.source;
+
+ if (this.handlePrintPreviewScriptingMessage_(message))
+ return;
+
+ // Delay scripting messages from users of the scripting API until the
+ // document is loaded. This simplifies use of the APIs.
+ if (!this.loaded_) {
+ this.delayedScriptingMessages_.push(message);
+ return;
+ }
+
switch (message.data.type.toString()) {
case 'getAccessibilityJSON':
case 'getSelectedText':
- case 'loadPreviewPage':
case 'print':
case 'selectAll':
this.plugin_.postMessage(message.data);
break;
- case 'resetPrintPreviewMode':
- if (!this.isPrintPreview_)
- break;
+ case 'isDocumentLoaded':
+ // Since this is only hit after the document has been loaded, we can
+ // send a reply immediately.
+ this.sendScriptingMessage_({
+ type: 'documentLoaded'
+ });
+ break;
+ }
+ },
+ /**
+ * @private
+ * Handle scripting messages specific to print preview.
+ * @param {MessageObject} message the message to handle.
+ * @return {boolean} true if the message was handled, false otherwise.
+ */
+ handlePrintPreviewScriptingMessage_: function(message) {
+ if (!this.isPrintPreview_)
+ return false;
+
+ switch (message.data.type.toString()) {
+ case 'loadPreviewPage':
+ this.plugin_.postMessage(message.data);
+ return true;
+ case 'resetPrintPreviewMode':
if (!this.inPrintPreviewMode_) {
this.inPrintPreviewMode_ = true;
this.viewport_.fitToPage();
@@ -660,26 +693,16 @@ PDFViewer.prototype = {
pageCount: (message.data.modifiable ?
message.data.pageNumbers.length : 0)
});
- break;
+ return true;
case 'sendKeyEvent':
var e = document.createEvent('Event');
e.initEvent('scriptingKeypress');
e.keyCode = message.data.keyCode;
this.handleKeyEvent_(e);
- break;
- case 'setParentWindow':
- if (this.parentWindow_ != message.source) {
- this.parentWindow_ = message.source;
- // If the document has already loaded, we always send a message that
- // indicates that so that the embedder is aware.
- if (this.loaded_) {
- this.sendScriptingMessage_({
- type: 'documentLoaded'
- });
- }
- }
- break;
+ return true;
}
+
+ return false;
},
/**
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf_scripting_api.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698