1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

JS -- Send events to the sandbox from annotation layer

This commit is contained in:
Calixte Denizet 2020-11-18 14:59:57 +01:00
parent 640a08444c
commit 6502ae889d
11 changed files with 647 additions and 113 deletions

View file

@ -47,6 +47,7 @@ class AnnotationLayerBuilder {
l10n = NullL10n,
enableScripting = false,
hasJSActionsPromise = null,
mouseState = null,
}) {
this.pageDiv = pageDiv;
this.pdfPage = pdfPage;
@ -58,6 +59,7 @@ class AnnotationLayerBuilder {
this.annotationStorage = annotationStorage;
this.enableScripting = enableScripting;
this._hasJSActionsPromise = hasJSActionsPromise;
this._mouseState = mouseState;
this.div = null;
this._cancelled = false;
@ -93,6 +95,7 @@ class AnnotationLayerBuilder {
annotationStorage: this.annotationStorage,
enableScripting: this.enableScripting,
hasJSActions,
mouseState: this._mouseState,
};
if (this.div) {
@ -139,6 +142,7 @@ class DefaultAnnotationLayerFactory {
* @param {IL10n} l10n
* @param {boolean} [enableScripting]
* @param {Promise<boolean>} [hasJSActionsPromise]
* @param {Object} [mouseState]
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder(
@ -149,7 +153,8 @@ class DefaultAnnotationLayerFactory {
renderInteractiveForms = true,
l10n = NullL10n,
enableScripting = false,
hasJSActionsPromise = null
hasJSActionsPromise = null,
mouseState = null
) {
return new AnnotationLayerBuilder({
pageDiv,
@ -161,6 +166,7 @@ class DefaultAnnotationLayerFactory {
annotationStorage,
enableScripting,
hasJSActionsPromise,
mouseState,
});
}
}

View file

@ -258,6 +258,7 @@ const PDFViewerApplication = {
_wheelUnusedTicks: 0,
_idleCallbacks: new Set(),
_scriptingInstance: null,
_mouseState: Object.create(null),
// Called once when the document is loaded.
async initialize(appConfig) {
@ -501,6 +502,7 @@ const PDFViewerApplication = {
useOnlyCssZoom: AppOptions.get("useOnlyCssZoom"),
maxCanvasPixels: AppOptions.get("maxCanvasPixels"),
enableScripting: AppOptions.get("enableScripting"),
mouseState: this._mouseState,
});
pdfRenderingQueue.setViewer(this.pdfViewer);
pdfLinkService.setViewer(this.pdfViewer);
@ -1538,6 +1540,17 @@ const PDFViewerApplication = {
dispatchEventInSandbox
);
const mouseDown = event => {
this._mouseState.isDown = true;
};
const mouseUp = event => {
this._mouseState.isDown = false;
};
window.addEventListener("mousedown", mouseDown);
this._scriptingInstance.events.set("mousedown", mouseDown);
window.addEventListener("mouseup", mouseUp);
this._scriptingInstance.events.set("mouseup", mouseUp);
const dispatchEventName = generateRandomStringForSandbox(objects);
if (!this._contentLength) {

View file

@ -79,6 +79,7 @@ const DEFAULT_CACHE_SIZE = 10;
* @property {IL10n} l10n - Localization service.
* @property {boolean} [enableScripting] - Enable embedded script execution.
* The default value is `false`.
* @property {Object} [mouseState] - The mouse button state.
*/
function PDFPageViewBuffer(size) {
@ -194,6 +195,7 @@ class BaseViewer {
this.maxCanvasPixels = options.maxCanvasPixels;
this.l10n = options.l10n || NullL10n;
this.enableScripting = options.enableScripting || false;
this.mouseState = options.mouseState || null;
this.defaultRenderingQueue = !options.renderingQueue;
if (this.defaultRenderingQueue) {
@ -533,6 +535,7 @@ class BaseViewer {
maxCanvasPixels: this.maxCanvasPixels,
l10n: this.l10n,
enableScripting: this.enableScripting,
mouseState: this.mouseState,
});
this._pages.push(pageView);
}
@ -1265,6 +1268,7 @@ class BaseViewer {
* @param {IL10n} l10n
* @param {boolean} [enableScripting]
* @param {Promise<boolean>} [hasJSActionsPromise]
* @param {Object} [mouseState]
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder(
@ -1275,7 +1279,8 @@ class BaseViewer {
renderInteractiveForms = false,
l10n = NullL10n,
enableScripting = false,
hasJSActionsPromise = null
hasJSActionsPromise = null,
mouseState = null
) {
return new AnnotationLayerBuilder({
pageDiv,
@ -1290,6 +1295,7 @@ class BaseViewer {
enableScripting,
hasJSActionsPromise:
hasJSActionsPromise || this.pdfDocument?.hasJSActions(),
mouseState,
});
}

View file

@ -188,6 +188,7 @@ class IPDFAnnotationLayerFactory {
* @param {IL10n} l10n
* @param {boolean} [enableScripting]
* @param {Promise<boolean>} [hasJSActionsPromise]
* @param {Object} [mouseState]
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder(
@ -198,7 +199,8 @@ class IPDFAnnotationLayerFactory {
renderInteractiveForms = true,
l10n = undefined,
enableScripting = false,
hasJSActionsPromise = null
hasJSActionsPromise = null,
mouseState = null
) {}
}

View file

@ -63,6 +63,7 @@ import { viewerCompatibilityParams } from "./viewer_compatibility.js";
* @property {IL10n} l10n - Localization service.
* @property {boolean} [enableScripting] - Enable embedded script execution.
* The default value is `false`.
* @property {Object} [mouseState] - The mouse button state.
*/
const MAX_CANVAS_PIXELS = viewerCompatibilityParams.maxCanvasPixels || 16777216;
@ -109,6 +110,7 @@ class PDFPageView {
this.enableWebGL = options.enableWebGL || false;
this.l10n = options.l10n || NullL10n;
this.enableScripting = options.enableScripting || false;
this.mouseState = options.mouseState || null;
this.paintTask = null;
this.paintedViewportMap = new WeakMap();
@ -551,7 +553,8 @@ class PDFPageView {
this.renderInteractiveForms,
this.l10n,
this.enableScripting,
/* hasJSActionsPromise = */ null
/* hasJSActionsPromise = */ null,
this.mouseState
);
}
this._renderAnnotationLayer();