diff --git a/web/app.js b/web/app.js index 043cad8176937f..b8607ba184ebe2 100644 --- a/web/app.js +++ b/web/app.js @@ -242,6 +242,7 @@ const PDFViewerApplication = { triggerDelayedFallback: null, _saveInProgress: false, _wheelUnusedTicks: 0, + _idleCallbacks: new Set(), // Called once when the document is loaded. async initialize(appConfig) { @@ -738,6 +739,10 @@ const PDFViewerApplication = { this.contentDispositionFilename = null; this.triggerDelayedFallback = null; this._saveInProgress = false; + for (const callback of this._idleCallbacks) { + window.cancelIdleCallback(callback); + } + this._idleCallbacks.clear(); this.pdfSidebar.reset(); this.pdfOutlineViewer.reset(); @@ -1329,12 +1334,39 @@ const PDFViewerApplication = { pdfViewer.optionalContentConfigPromise.then(optionalContentConfig => { this.pdfLayerViewer.render({ optionalContentConfig, pdfDocument }); }); + if ("requestIdleCallback" in window) { + const callback = window.requestIdleCallback( + () => { + this._collectTelemetry(pdfDocument); + this._idleCallbacks.delete(callback); + }, + { timeout: 1000 } + ); + this._idleCallbacks.add(callback); + } }); this._initializePageLabels(pdfDocument); this._initializeMetadata(pdfDocument); }, + /** + * A place to fetch data for telemetry after one page is rendered and the + * viewer is idle. + * @private + */ + async _collectTelemetry(pdfDocument) { + const markInfo = await this.pdfDocument.getMarkInfo(); + if (pdfDocument !== this.pdfDocument) { + return; // Document was closed while waiting for mark info. + } + const tagged = markInfo?.Marked || false; + this.externalServices.reportTelemetry({ + type: "tagged", + tagged, + }); + }, + /** * @private */