0

Make the scripting OOP PDF API easier to use (take 2)

This mainly removes the need to call setParentWindow before using the API but
it also delays more messages from being received until the document is loaded
which makes the API easier to use.

BUG=415858

Review URL: https://codereview.chromium.org/870453002

Cr-Commit-Position: refs/heads/master@{#312722}
This commit is contained in:
raymes
2015-01-22 17:14:47 -08:00
committed by Commit bot
parent 298156be04
commit c121c22690
4 changed files with 72 additions and 61 deletions

@ -48,6 +48,8 @@ function PDFViewer(streamDetails) {
this.loaded_ = false; this.loaded_ = false;
this.parentWindow_ = null; this.parentWindow_ = null;
this.delayedScriptingMessages_ = [];
this.isPrintPreview_ = this.isPrintPreview_ =
this.streamDetails_.originalUrl.indexOf('chrome://print') == 0; this.streamDetails_.originalUrl.indexOf('chrome://print') == 0;
this.isMaterial_ = location.pathname.substring(1) == 'index-material.html'; this.isMaterial_ = location.pathname.substring(1) == 'index-material.html';
@ -340,9 +342,8 @@ PDFViewer.prototype = {
this.viewport_.position = this.lastViewportPosition_; this.viewport_.position = this.lastViewportPosition_;
this.handleURLParams_(); this.handleURLParams_();
this.loaded_ = true; this.loaded_ = true;
this.sendScriptingMessage_({ while (this.delayedScriptingMessages_.length > 0)
type: 'documentLoaded' this.handleScriptingMessage(this.delayedScriptingMessages_.shift());
});
} }
}, },
@ -613,25 +614,57 @@ PDFViewer.prototype = {
}, },
/** /**
* @private
* Handle a scripting message from outside the extension (typically sent by * Handle a scripting message from outside the extension (typically sent by
* PDFScriptingAPI in a page containing the extension) to interact with the * PDFScriptingAPI in a page containing the extension) to interact with the
* plugin. * plugin.
* @param {MessageObject} message the message to handle. * @param {MessageObject} message the message to handle.
*/ */
handleScriptingMessage: function(message) { 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()) { switch (message.data.type.toString()) {
case 'getAccessibilityJSON': case 'getAccessibilityJSON':
case 'getSelectedText': case 'getSelectedText':
case 'loadPreviewPage':
case 'print': case 'print':
case 'selectAll': case 'selectAll':
this.plugin_.postMessage(message.data); this.plugin_.postMessage(message.data);
break; break;
case 'resetPrintPreviewMode': case 'isDocumentLoaded':
if (!this.isPrintPreview_) // Since this is only hit after the document has been loaded, we can
break; // 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_) { if (!this.inPrintPreviewMode_) {
this.inPrintPreviewMode_ = true; this.inPrintPreviewMode_ = true;
this.viewport_.fitToPage(); this.viewport_.fitToPage();
@ -660,26 +693,16 @@ PDFViewer.prototype = {
pageCount: (message.data.modifiable ? pageCount: (message.data.modifiable ?
message.data.pageNumbers.length : 0) message.data.pageNumbers.length : 0)
}); });
break; return true;
case 'sendKeyEvent': case 'sendKeyEvent':
var e = document.createEvent('Event'); var e = document.createEvent('Event');
e.initEvent('scriptingKeypress'); e.initEvent('scriptingKeypress');
e.keyCode = message.data.keyCode; e.keyCode = message.data.keyCode;
this.handleKeyEvent_(e); this.handleKeyEvent_(e);
break; return true;
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 false;
}, },
/** /**

@ -9,7 +9,6 @@
* @param {Object} plugin the plugin element containing the pdf viewer. * @param {Object} plugin the plugin element containing the pdf viewer.
*/ */
function PDFScriptingAPI(window, plugin) { function PDFScriptingAPI(window, plugin) {
this.loaded_ = false;
this.pendingScriptingMessages_ = []; this.pendingScriptingMessages_ = [];
this.setPlugin(plugin); this.setPlugin(plugin);
@ -29,9 +28,10 @@ function PDFScriptingAPI(window, plugin) {
event.data.viewportHeight); event.data.viewportHeight);
break; break;
case 'documentLoaded': case 'documentLoaded':
this.loaded_ = true; if (this.loadCallback_) {
if (this.loadCallback_)
this.loadCallback_(); this.loadCallback_();
this.loadCallback_ = null;
}
break; break;
case 'getAccessibilityJSONReply': case 'getAccessibilityJSONReply':
if (this.accessibilityCallback_) { if (this.accessibilityCallback_) {
@ -72,14 +72,8 @@ PDFScriptingAPI.prototype = {
setPlugin: function(plugin) { setPlugin: function(plugin) {
this.plugin_ = plugin; this.plugin_ = plugin;
// Send an initialization message to the plugin indicating the window to
// respond to.
if (this.plugin_) { if (this.plugin_) {
this.sendMessage_({ // Flush pending messages.
type: 'setParentWindow'
});
// Now we can flush pending messages
while (this.pendingScriptingMessages_.length > 0) while (this.pendingScriptingMessages_.length > 0)
this.sendMessage_(this.pendingScriptingMessages_.shift()); this.sendMessage_(this.pendingScriptingMessages_.shift());
} }
@ -97,11 +91,17 @@ PDFScriptingAPI.prototype = {
* Sets the callback which will be run when the PDF document has finished * Sets the callback which will be run when the PDF document has finished
* loading. If the document is already loaded, it will be run immediately. * loading. If the document is already loaded, it will be run immediately.
* @param {Function} callback the callback to be called. * @param {Function} callback the callback to be called.
* @return {boolean} false if there is a callback already set and true
* otherwise.
*/ */
setLoadCallback: function(callback) { setLoadCallback: function(callback) {
if (this.loadCallback_)
return false;
this.loadCallback_ = callback; this.loadCallback_ = callback;
if (this.loaded_ && callback) this.sendMessage_({
callback(); type: 'isDocumentLoaded'
});
return true;
}, },
/** /**

@ -267,8 +267,7 @@ OutOfProcessInstance::OutOfProcessInstance(PP_Instance instance)
recently_sent_find_update_(false), recently_sent_find_update_(false),
received_viewport_message_(false), received_viewport_message_(false),
did_call_start_loading_(false), did_call_start_loading_(false),
stop_scrolling_(false), stop_scrolling_(false) {
delay_print_(false) {
loader_factory_.Initialize(this); loader_factory_.Initialize(this);
timer_factory_.Initialize(this); timer_factory_.Initialize(this);
form_factory_.Initialize(this); form_factory_.Initialize(this);
@ -970,11 +969,6 @@ void OutOfProcessInstance::Email(const std::string& to,
} }
void OutOfProcessInstance::Print() { void OutOfProcessInstance::Print() {
if (document_load_state_ == LOAD_STATE_LOADING) {
delay_print_ = true;
return;
}
if (!engine_->HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY) && if (!engine_->HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY) &&
!engine_->HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) { !engine_->HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) {
return; return;
@ -1102,25 +1096,23 @@ void OutOfProcessInstance::DocumentLoadComplete(int page_count) {
progress_message.Set(pp::Var(kJSProgressPercentage), pp::Var(100)); progress_message.Set(pp::Var(kJSProgressPercentage), pp::Var(100));
PostMessage(progress_message); PostMessage(progress_message);
if (full_) { if (!full_)
if (did_call_start_loading_) { return;
pp::PDF::DidStopLoading(this);
did_call_start_loading_ = false;
}
int content_restrictions = if (did_call_start_loading_) {
CONTENT_RESTRICTION_CUT | CONTENT_RESTRICTION_PASTE; pp::PDF::DidStopLoading(this);
if (!engine_->HasPermission(PDFEngine::PERMISSION_COPY)) did_call_start_loading_ = false;
content_restrictions |= CONTENT_RESTRICTION_COPY;
pp::PDF::SetContentRestriction(this, content_restrictions);
uma_.HistogramCustomCounts("PDF.PageCount", page_count,
1, 1000000, 50);
} }
if (delay_print_) int content_restrictions =
Print(); CONTENT_RESTRICTION_CUT | CONTENT_RESTRICTION_PASTE;
if (!engine_->HasPermission(PDFEngine::PERMISSION_COPY))
content_restrictions |= CONTENT_RESTRICTION_COPY;
pp::PDF::SetContentRestriction(this, content_restrictions);
uma_.HistogramCustomCounts("PDF.PageCount", page_count,
1, 1000000, 50);
} }
void OutOfProcessInstance::RotateClockwise() { void OutOfProcessInstance::RotateClockwise() {

@ -339,10 +339,6 @@ class OutOfProcessInstance : public pp::Instance,
// zooming the plugin so that flickering doesn't occur while zooming. // zooming the plugin so that flickering doesn't occur while zooming.
bool stop_scrolling_; bool stop_scrolling_;
// If a print command comes in before the document has loaded, we set
// |delay_print_| to true and print after the document has loaded.
bool delay_print_;
// The callback for receiving the password from the page. // The callback for receiving the password from the page.
scoped_ptr<pp::CompletionCallbackWithOutput<pp::Var> > password_callback_; scoped_ptr<pp::CompletionCallbackWithOutput<pp::Var> > password_callback_;
}; };