Commit 55611965 authored by dpapad's avatar dpapad Committed by Commit Bot

PDF Viewer: Convert remaining classes to ES6.

Specifically
 - Converting PDFScriptingAPI, PDFViewer, ViewportScroller classes.
 - Fixing newly revealed type errors (only happening with classes
   which are @struct by default).

Bug: 1005029
Change-Id: I15490634db5c3e3c395facc4e57b4106eab3b494
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1820101
Auto-Submit: Demetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699162}
parent dde21e88
...@@ -53,17 +53,35 @@ const LoadState = { ...@@ -53,17 +53,35 @@ const LoadState = {
* Create a new PDFScriptingAPI. This provides a scripting interface to * Create a new PDFScriptingAPI. This provides a scripting interface to
* the PDF viewer so that it can be customized by things like print preview. * the PDF viewer so that it can be customized by things like print preview.
* *
*/
class PDFScriptingAPI {
/**
* @param {Window} window the window of the page containing the pdf viewer. * @param {Window} window the window of the page containing the pdf viewer.
* @param {Object} plugin the plugin element containing the pdf viewer. * @param {Object} plugin the plugin element containing the pdf viewer.
* @constructor
*/ */
function PDFScriptingAPI(window, plugin) { constructor(window, plugin) {
this.loadState_ = LoadState.LOADING; this.loadState_ = LoadState.LOADING;
this.pendingScriptingMessages_ = []; this.pendingScriptingMessages_ = [];
this.setPlugin(plugin); this.setPlugin(plugin);
/** @private {Function} */
this.viewportChangedCallback_;
/** @private {Function} */
this.loadCallback_;
/** @private {Function} */
this.selectedTextCallback_;
/** @private {Function} */
this.keyEventCallback_;
/** @private {Object} */
this.plugin_;
window.addEventListener('message', event => { window.addEventListener('message', event => {
if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' && if (event.origin !=
'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' &&
event.origin != 'chrome://print') { event.origin != 'chrome://print') {
console.error( console.error(
'Received message that was not from the extension: ' + event); 'Received message that was not from the extension: ' + event);
...@@ -110,9 +128,8 @@ function PDFScriptingAPI(window, plugin) { ...@@ -110,9 +128,8 @@ function PDFScriptingAPI(window, plugin) {
break; break;
} }
}, false); }, false);
} }
PDFScriptingAPI.prototype = {
/** /**
* Send a message to the extension. If messages try to get sent before there * Send a message to the extension. If messages try to get sent before there
* is a plugin element set, then we queue them up and send them later (this * is a plugin element set, then we queue them up and send them later (this
...@@ -121,13 +138,13 @@ PDFScriptingAPI.prototype = { ...@@ -121,13 +138,13 @@ PDFScriptingAPI.prototype = {
* @param {Object} message The message to send. * @param {Object} message The message to send.
* @private * @private
*/ */
sendMessage_: function(message) { sendMessage_(message) {
if (this.plugin_) { if (this.plugin_) {
this.plugin_.postMessage(message, '*'); this.plugin_.postMessage(message, '*');
} else { } else {
this.pendingScriptingMessages_.push(message); this.pendingScriptingMessages_.push(message);
} }
}, }
/** /**
* Sets the plugin element containing the PDF viewer. The element will usually * Sets the plugin element containing the PDF viewer. The element will usually
...@@ -135,7 +152,7 @@ PDFScriptingAPI.prototype = { ...@@ -135,7 +152,7 @@ PDFScriptingAPI.prototype = {
* *
* @param {Object} plugin the plugin element containing the PDF viewer. * @param {Object} plugin the plugin element containing the PDF viewer.
*/ */
setPlugin: function(plugin) { setPlugin(plugin) {
this.plugin_ = plugin; this.plugin_ = plugin;
if (this.plugin_) { if (this.plugin_) {
...@@ -147,16 +164,16 @@ PDFScriptingAPI.prototype = { ...@@ -147,16 +164,16 @@ PDFScriptingAPI.prototype = {
this.sendMessage_(this.pendingScriptingMessages_.shift()); this.sendMessage_(this.pendingScriptingMessages_.shift());
} }
} }
}, }
/** /**
* Sets the callback which will be run when the PDF viewport changes. * Sets the callback which will be run when the PDF viewport changes.
* *
* @param {Function} callback the callback to be called. * @param {Function} callback the callback to be called.
*/ */
setViewportChangedCallback: function(callback) { setViewportChangedCallback(callback) {
this.viewportChangedCallback_ = callback; this.viewportChangedCallback_ = callback;
}, }
/** /**
* 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
...@@ -164,20 +181,20 @@ PDFScriptingAPI.prototype = { ...@@ -164,20 +181,20 @@ PDFScriptingAPI.prototype = {
* *
* @param {Function} callback the callback to be called. * @param {Function} callback the callback to be called.
*/ */
setLoadCallback: function(callback) { setLoadCallback(callback) {
this.loadCallback_ = callback; this.loadCallback_ = callback;
if (this.loadState_ != LoadState.LOADING && this.loadCallback_) { if (this.loadState_ != LoadState.LOADING && this.loadCallback_) {
this.loadCallback_(this.loadState_ == LoadState.SUCCESS); this.loadCallback_(this.loadState_ == LoadState.SUCCESS);
} }
}, }
/** /**
* Sets a callback that gets run when a key event is fired in the PDF viewer. * Sets a callback that gets run when a key event is fired in the PDF viewer.
* @param {Function} callback the callback to be called with a key event. * @param {Function} callback the callback to be called with a key event.
*/ */
setKeyEventCallback: function(callback) { setKeyEventCallback(callback) {
this.keyEventCallback_ = callback; this.keyEventCallback_ = callback;
}, }
/** /**
* Resets the PDF viewer into print preview mode. * Resets the PDF viewer into print preview mode.
...@@ -187,7 +204,7 @@ PDFScriptingAPI.prototype = { ...@@ -187,7 +204,7 @@ PDFScriptingAPI.prototype = {
* @param {Array<number>} pageNumbers an array of the page numbers. * @param {Array<number>} pageNumbers an array of the page numbers.
* @param {boolean} modifiable whether or not the document is modifiable. * @param {boolean} modifiable whether or not the document is modifiable.
*/ */
resetPrintPreviewMode: function(url, grayscale, pageNumbers, modifiable) { resetPrintPreviewMode(url, grayscale, pageNumbers, modifiable) {
this.loadState_ = LoadState.LOADING; this.loadState_ = LoadState.LOADING;
this.sendMessage_({ this.sendMessage_({
type: 'resetPrintPreviewMode', type: 'resetPrintPreviewMode',
...@@ -196,14 +213,14 @@ PDFScriptingAPI.prototype = { ...@@ -196,14 +213,14 @@ PDFScriptingAPI.prototype = {
pageNumbers: pageNumbers, pageNumbers: pageNumbers,
modifiable: modifiable modifiable: modifiable
}); });
}, }
/** /**
* Hide the toolbars after a delay. * Hide the toolbars after a delay.
*/ */
hideToolbars: function() { hideToolbars() {
this.sendMessage_({type: 'hideToolbars'}); this.sendMessage_({type: 'hideToolbars'});
}, }
/** /**
* Load a page into the document while in print preview mode. * Load a page into the document while in print preview mode.
...@@ -211,22 +228,22 @@ PDFScriptingAPI.prototype = { ...@@ -211,22 +228,22 @@ PDFScriptingAPI.prototype = {
* @param {string} url the url of the pdf page to load. * @param {string} url the url of the pdf page to load.
* @param {number} index the index of the page to load. * @param {number} index the index of the page to load.
*/ */
loadPreviewPage: function(url, index) { loadPreviewPage(url, index) {
this.sendMessage_({type: 'loadPreviewPage', url: url, index: index}); this.sendMessage_({type: 'loadPreviewPage', url: url, index: index});
}, }
/** @param {boolean} darkMode Whether the page is in dark mode. */ /** @param {boolean} darkMode Whether the page is in dark mode. */
darkModeChanged: function(darkMode) { darkModeChanged(darkMode) {
this.sendMessage_({type: 'darkModeChanged', darkMode: darkMode}); this.sendMessage_({type: 'darkModeChanged', darkMode: darkMode});
}, }
/** /**
* Select all the text in the document. May only be called after document * Select all the text in the document. May only be called after document
* load. * load.
*/ */
selectAll: function() { selectAll() {
this.sendMessage_({type: 'selectAll'}); this.sendMessage_({type: 'selectAll'});
}, }
/** /**
* Get the selected text in the document. The callback will be called with the * Get the selected text in the document. The callback will be called with the
...@@ -236,40 +253,40 @@ PDFScriptingAPI.prototype = { ...@@ -236,40 +253,40 @@ PDFScriptingAPI.prototype = {
* @return {boolean} true if the function is successful, false if there is an * @return {boolean} true if the function is successful, false if there is an
* outstanding request for selected text that has not been answered. * outstanding request for selected text that has not been answered.
*/ */
getSelectedText: function(callback) { getSelectedText(callback) {
if (this.selectedTextCallback_) { if (this.selectedTextCallback_) {
return false; return false;
} }
this.selectedTextCallback_ = callback; this.selectedTextCallback_ = callback;
this.sendMessage_({type: 'getSelectedText'}); this.sendMessage_({type: 'getSelectedText'});
return true; return true;
}, }
/** /**
* Print the document. May only be called after document load. * Print the document. May only be called after document load.
*/ */
print: function() { print() {
this.sendMessage_({type: 'print'}); this.sendMessage_({type: 'print'});
}, }
/** /**
* Send a key event to the extension. * Send a key event to the extension.
* *
* @param {Event} keyEvent the key event to send to the extension. * @param {Event} keyEvent the key event to send to the extension.
*/ */
sendKeyEvent: function(keyEvent) { sendKeyEvent(keyEvent) {
this.sendMessage_( this.sendMessage_(
{type: 'sendKeyEvent', keyEvent: SerializeKeyEvent(keyEvent)}); {type: 'sendKeyEvent', keyEvent: SerializeKeyEvent(keyEvent)});
}, }
/** /**
* @param {number} scrollX The amount to horizontally scroll in pixels. * @param {number} scrollX The amount to horizontally scroll in pixels.
* @param {number} scrollY The amount to vertically scroll in pixels. * @param {number} scrollY The amount to vertically scroll in pixels.
*/ */
scrollPosition: function(scrollX, scrollY) { scrollPosition(scrollX, scrollY) {
this.sendMessage_({type: 'scrollPosition', x: scrollX, y: scrollY}); this.sendMessage_({type: 'scrollPosition', x: scrollX, y: scrollY});
}, }
}; }
/** /**
* Creates a PDF viewer with a scripting interface. This is basically 1) an * Creates a PDF viewer with a scripting interface. This is basically 1) an
......
This diff is collapsed.
...@@ -4,32 +4,19 @@ ...@@ -4,32 +4,19 @@
'use strict'; 'use strict';
/**
* The period of time in milliseconds to wait between updating the viewport
* position by the scroll velocity.
*
* @private
*/
ViewportScroller.DRAG_TIMER_INTERVAL_MS_ = 100;
/**
* The maximum drag scroll distance per DRAG_TIMER_INTERVAL in pixels.
*
* @private
*/
ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_ = 100;
/** /**
* Creates a new ViewportScroller. * Creates a new ViewportScroller.
* A ViewportScroller scrolls the page in response to drag selection with the * A ViewportScroller scrolls the page in response to drag selection with the
* mouse. * mouse.
* *
*/
class ViewportScroller {
/**
* @param {Object} viewport The viewport info of the page. * @param {Object} viewport The viewport info of the page.
* @param {Object} plugin The PDF plugin element. * @param {Object} plugin The PDF plugin element.
* @param {Object} window The window containing the viewer. * @param {Object} window The window containing the viewer.
* @constructor
*/ */
function ViewportScroller(viewport, plugin, window) { constructor(viewport, plugin, window) {
this.viewport_ = viewport; this.viewport_ = viewport;
this.plugin_ = plugin; this.plugin_ = plugin;
this.window_ = window; this.window_ = window;
...@@ -37,43 +24,42 @@ function ViewportScroller(viewport, plugin, window) { ...@@ -37,43 +24,42 @@ function ViewportScroller(viewport, plugin, window) {
this.timerId_ = null; this.timerId_ = null;
this.scrollVelocity_ = null; this.scrollVelocity_ = null;
this.lastFrameTime_ = 0; this.lastFrameTime_ = 0;
} }
ViewportScroller.prototype = {
/** /**
* Start scrolling the page by |scrollVelocity_| every * Start scrolling the page by |scrollVelocity_| every
* |DRAG_TIMER_INTERVAL_MS_|. * |DRAG_TIMER_INTERVAL_MS_|.
* *
* @private * @private
*/ */
startDragScrollTimer_: function() { startDragScrollTimer_() {
if (this.timerId_ === null) { if (this.timerId_ === null) {
this.timerId_ = this.window_.setInterval( this.timerId_ = this.window_.setInterval(
this.dragScrollPage_.bind(this), this.dragScrollPage_.bind(this),
ViewportScroller.DRAG_TIMER_INTERVAL_MS_); ViewportScroller.DRAG_TIMER_INTERVAL_MS_);
this.lastFrameTime_ = Date.now(); this.lastFrameTime_ = Date.now();
} }
}, }
/** /**
* Stops the drag scroll timer if it is active. * Stops the drag scroll timer if it is active.
* *
* @private * @private
*/ */
stopDragScrollTimer_: function() { stopDragScrollTimer_() {
if (this.timerId_ !== null) { if (this.timerId_ !== null) {
this.window_.clearInterval(this.timerId_); this.window_.clearInterval(this.timerId_);
this.timerId_ = null; this.timerId_ = null;
this.lastFrameTime_ = 0; this.lastFrameTime_ = 0;
} }
}, }
/** /**
* Scrolls the viewport by the current scroll velocity. * Scrolls the viewport by the current scroll velocity.
* *
* @private * @private
*/ */
dragScrollPage_: function() { dragScrollPage_() {
const position = this.viewport_.position; const position = this.viewport_.position;
const currentFrameTime = Date.now(); const currentFrameTime = Date.now();
const timeAdjustment = (currentFrameTime - this.lastFrameTime_) / const timeAdjustment = (currentFrameTime - this.lastFrameTime_) /
...@@ -82,7 +68,7 @@ ViewportScroller.prototype = { ...@@ -82,7 +68,7 @@ ViewportScroller.prototype = {
position.x += (this.scrollVelocity_.x * timeAdjustment); position.x += (this.scrollVelocity_.x * timeAdjustment);
this.viewport_.position = position; this.viewport_.position = position;
this.lastFrameTime_ = currentFrameTime; this.lastFrameTime_ = currentFrameTime;
}, }
/** /**
* Calculate the velocity to scroll while dragging using the distance of the * Calculate the velocity to scroll while dragging using the distance of the
...@@ -92,7 +78,7 @@ ViewportScroller.prototype = { ...@@ -92,7 +78,7 @@ ViewportScroller.prototype = {
* @return {Object} Object with x and y direction scroll velocity. * @return {Object} Object with x and y direction scroll velocity.
* @private * @private
*/ */
calculateVelocity_: function(event) { calculateVelocity_(event) {
const x = const x =
Math.min( Math.min(
Math.max( Math.max(
...@@ -106,7 +92,7 @@ ViewportScroller.prototype = { ...@@ -106,7 +92,7 @@ ViewportScroller.prototype = {
ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) * ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) *
Math.sign(event.offsetY); Math.sign(event.offsetY);
return {x: x, y: y}; return {x: x, y: y};
}, }
/** /**
* Handles mousemove events. It updates the scroll velocity and starts and * Handles mousemove events. It updates the scroll velocity and starts and
...@@ -115,14 +101,14 @@ ViewportScroller.prototype = { ...@@ -115,14 +101,14 @@ ViewportScroller.prototype = {
* @param {Object} event The mousemove event. * @param {Object} event The mousemove event.
* @private * @private
*/ */
onMousemove_: function(event) { onMousemove_(event) {
this.scrollVelocity_ = this.calculateVelocity_(event); this.scrollVelocity_ = this.calculateVelocity_(event);
if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) { if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) {
this.stopDragScrollTimer_(); this.stopDragScrollTimer_();
} else if (!this.timerId_) { } else if (!this.timerId_) {
this.startDragScrollTimer_(); this.startDragScrollTimer_();
} }
}, }
/** /**
* Sets whether to scroll the viewport when the mouse is outside the * Sets whether to scroll the viewport when the mouse is outside the
...@@ -130,7 +116,7 @@ ViewportScroller.prototype = { ...@@ -130,7 +116,7 @@ ViewportScroller.prototype = {
* *
* @param {boolean} isSelecting Represents selection status. * @param {boolean} isSelecting Represents selection status.
*/ */
setEnableScrolling: function(isSelecting) { setEnableScrolling(isSelecting) {
if (isSelecting) { if (isSelecting) {
if (!this.mousemoveCallback_) { if (!this.mousemoveCallback_) {
this.mousemoveCallback_ = this.onMousemove_.bind(this); this.mousemoveCallback_ = this.onMousemove_.bind(this);
...@@ -145,4 +131,19 @@ ViewportScroller.prototype = { ...@@ -145,4 +131,19 @@ ViewportScroller.prototype = {
} }
} }
} }
}; }
/**
* The period of time in milliseconds to wait between updating the viewport
* position by the scroll velocity.
*
* @private
*/
ViewportScroller.DRAG_TIMER_INTERVAL_MS_ = 100;
/**
* The maximum drag scroll distance per DRAG_TIMER_INTERVAL in pixels.
*
* @private
*/
ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_ = 100;
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