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:
chrome/browser/resources/pdf
pdf
@ -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;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -9,7 +9,6 @@
|
||||
* @param {Object} plugin the plugin element containing the pdf viewer.
|
||||
*/
|
||||
function PDFScriptingAPI(window, plugin) {
|
||||
this.loaded_ = false;
|
||||
this.pendingScriptingMessages_ = [];
|
||||
this.setPlugin(plugin);
|
||||
|
||||
@ -29,9 +28,10 @@ function PDFScriptingAPI(window, plugin) {
|
||||
event.data.viewportHeight);
|
||||
break;
|
||||
case 'documentLoaded':
|
||||
this.loaded_ = true;
|
||||
if (this.loadCallback_)
|
||||
if (this.loadCallback_) {
|
||||
this.loadCallback_();
|
||||
this.loadCallback_ = null;
|
||||
}
|
||||
break;
|
||||
case 'getAccessibilityJSONReply':
|
||||
if (this.accessibilityCallback_) {
|
||||
@ -72,14 +72,8 @@ PDFScriptingAPI.prototype = {
|
||||
setPlugin: function(plugin) {
|
||||
this.plugin_ = plugin;
|
||||
|
||||
// Send an initialization message to the plugin indicating the window to
|
||||
// respond to.
|
||||
if (this.plugin_) {
|
||||
this.sendMessage_({
|
||||
type: 'setParentWindow'
|
||||
});
|
||||
|
||||
// Now we can flush pending messages
|
||||
// Flush pending messages.
|
||||
while (this.pendingScriptingMessages_.length > 0)
|
||||
this.sendMessage_(this.pendingScriptingMessages_.shift());
|
||||
}
|
||||
@ -97,11 +91,17 @@ PDFScriptingAPI.prototype = {
|
||||
* 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.
|
||||
* @param {Function} callback the callback to be called.
|
||||
* @return {boolean} false if there is a callback already set and true
|
||||
* otherwise.
|
||||
*/
|
||||
setLoadCallback: function(callback) {
|
||||
if (this.loadCallback_)
|
||||
return false;
|
||||
this.loadCallback_ = callback;
|
||||
if (this.loaded_ && callback)
|
||||
callback();
|
||||
this.sendMessage_({
|
||||
type: 'isDocumentLoaded'
|
||||
});
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -267,8 +267,7 @@ OutOfProcessInstance::OutOfProcessInstance(PP_Instance instance)
|
||||
recently_sent_find_update_(false),
|
||||
received_viewport_message_(false),
|
||||
did_call_start_loading_(false),
|
||||
stop_scrolling_(false),
|
||||
delay_print_(false) {
|
||||
stop_scrolling_(false) {
|
||||
loader_factory_.Initialize(this);
|
||||
timer_factory_.Initialize(this);
|
||||
form_factory_.Initialize(this);
|
||||
@ -970,11 +969,6 @@ void OutOfProcessInstance::Email(const std::string& to,
|
||||
}
|
||||
|
||||
void OutOfProcessInstance::Print() {
|
||||
if (document_load_state_ == LOAD_STATE_LOADING) {
|
||||
delay_print_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!engine_->HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY) &&
|
||||
!engine_->HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) {
|
||||
return;
|
||||
@ -1102,25 +1096,23 @@ void OutOfProcessInstance::DocumentLoadComplete(int page_count) {
|
||||
progress_message.Set(pp::Var(kJSProgressPercentage), pp::Var(100));
|
||||
PostMessage(progress_message);
|
||||
|
||||
if (full_) {
|
||||
if (did_call_start_loading_) {
|
||||
pp::PDF::DidStopLoading(this);
|
||||
did_call_start_loading_ = false;
|
||||
}
|
||||
if (!full_)
|
||||
return;
|
||||
|
||||
int content_restrictions =
|
||||
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);
|
||||
if (did_call_start_loading_) {
|
||||
pp::PDF::DidStopLoading(this);
|
||||
did_call_start_loading_ = false;
|
||||
}
|
||||
|
||||
if (delay_print_)
|
||||
Print();
|
||||
int content_restrictions =
|
||||
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() {
|
||||
|
@ -339,10 +339,6 @@ class OutOfProcessInstance : public pp::Instance,
|
||||
// zooming the plugin so that flickering doesn't occur while zooming.
|
||||
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.
|
||||
scoped_ptr<pp::CompletionCallbackWithOutput<pp::Var> > password_callback_;
|
||||
};
|
||||
|
Reference in New Issue
Block a user