Skip to content

Commit 3f21efc

Browse files
Merge pull request #20607 from Snuffleupagus/rm-web-interfaces
Replace the various interfaces in `web/interfaces.js` with proper classes
2 parents 8eb9340 + aa4d0f7 commit 3f21efc

40 files changed

Lines changed: 331 additions & 516 deletions

.github/workflows/types_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
run: npm ci
2929

3030
- name: Run types tests
31-
run: npx gulp typestest
31+
run: npx gulp types

gulpfile.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function createWebpackAlias(defines) {
231231
libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js";
232232
libraryAlias["display-network"] = "src/display/network.js";
233233

234-
viewerAlias["web-download_manager"] = "web/download_manager.js";
234+
viewerAlias["web-download_manager"] = "web/chromecom.js";
235235
viewerAlias["web-external_services"] = "web/chromecom.js";
236236
viewerAlias["web-null_l10n"] = "web/l10n.js";
237237
viewerAlias["web-preferences"] = "web/chromecom.js";

src/display/annotation_layer.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
// eslint-disable-next-line max-len
1919
/** @typedef {import("../../web/text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
2020
// eslint-disable-next-line max-len
21-
/** @typedef {import("../../web/interfaces").IDownloadManager} IDownloadManager */
22-
/** @typedef {import("../../web/interfaces").IPDFLinkService} IPDFLinkService */
23-
// eslint-disable-next-line max-len
2421
/** @typedef {import("../src/display/editor/tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
2522
// eslint-disable-next-line max-len
2623
/** @typedef {import("../../web/struct_tree_layer_builder.js").StructTreeLayerBuilder} StructTreeLayerBuilder */
@@ -57,8 +54,8 @@ const TIMEZONE_OFFSET = new Date().getTimezoneOffset() * 60 * 1000;
5754
* @typedef {Object} AnnotationElementParameters
5855
* @property {Object} data
5956
* @property {HTMLDivElement} layer
60-
* @property {IPDFLinkService} linkService
61-
* @property {IDownloadManager} [downloadManager]
57+
* @property {PDFLinkService} linkService
58+
* @property {BaseDownloadManager} [downloadManager]
6259
* @property {AnnotationStorage} [annotationStorage]
6360
* @property {string} [imageResourcesPath] - Path for image resources, mainly
6461
* for annotation icons. Include trailing slash.
@@ -3736,8 +3733,8 @@ class FileAttachmentAnnotationElement extends AnnotationElement {
37363733
* @property {HTMLDivElement} div
37373734
* @property {Array} annotations
37383735
* @property {PDFPageProxy} page
3739-
* @property {IPDFLinkService} linkService
3740-
* @property {IDownloadManager} [downloadManager]
3736+
* @property {PDFLinkService} linkService
3737+
* @property {BaseDownloadManager} [downloadManager]
37413738
* @property {AnnotationStorage} [annotationStorage]
37423739
* @property {string} [imageResourcesPath] - Path for image resources, mainly
37433740
* for annotation icons. Include trailing slash.
@@ -4018,8 +4015,6 @@ class AnnotationLayer {
40184015
* Add link annotations to the annotation layer.
40194016
*
40204017
* @param {Array<Object>} annotations
4021-
* @param {IPDFLinkService} linkService
4022-
* @memberof AnnotationLayer
40234018
*/
40244019
async addLinkAnnotations(annotations) {
40254020
const elementParams = {

src/display/editor/annotation_editor_layer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
/** @typedef {import("../display_utils.js").PageViewport} PageViewport */
1919
// eslint-disable-next-line max-len
2020
/** @typedef {import("../../../web/text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
21-
/** @typedef {import("../../../web/interfaces").IL10n} IL10n */
2221
// eslint-disable-next-line max-len
2322
/** @typedef {import("../annotation_layer.js").AnnotationLayer} AnnotationLayer */
2423
/** @typedef {import("../draw_layer.js").DrawLayer} DrawLayer */
@@ -47,7 +46,7 @@ import { StampEditor } from "./stamp.js";
4746
* @property {boolean} enabled
4847
* @property {TextAccessibilityManager} [accessibilityManager]
4948
* @property {number} pageIndex
50-
* @property {IL10n} l10n
49+
* @property {L10n} l10n
5150
* @property {AnnotationLayer} [annotationLayer]
5251
* @property {HTMLDivElement} [textLayer]
5352
* @property {DrawLayer} drawLayer

src/display/xfa_layer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// eslint-disable-next-line max-len
1717
/** @typedef {import("./annotation_storage").AnnotationStorage} AnnotationStorage */
1818
/** @typedef {import("./display_utils").PageViewport} PageViewport */
19-
/** @typedef {import("../../web/interfaces").IPDFLinkService} IPDFLinkService */
2019

2120
import { XfaText } from "./xfa_text.js";
2221

@@ -26,7 +25,7 @@ import { XfaText } from "./xfa_text.js";
2625
* @property {HTMLDivElement} div
2726
* @property {Object} xfaHtml
2827
* @property {AnnotationStorage} [annotationStorage]
29-
* @property {IPDFLinkService} linkService
28+
* @property {PDFLinkService} linkService
3029
* @property {string} [intent] - (default value is 'display').
3130
*/
3231

test/unit/pdf_viewer.component_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
import {
2323
parseQueryString,
2424
ProgressBar,
25-
RenderingStates,
2625
ScrollMode,
2726
SpreadMode,
2827
} from "../../web/ui_utils.js";
@@ -35,6 +34,7 @@ import { PDFPageView } from "../../web/pdf_page_view.js";
3534
import { PDFScriptingManager } from "../../web/pdf_scripting_manager.component.js";
3635
import { PDFSinglePageViewer } from "../../web/pdf_single_page_viewer.js";
3736
import { PDFViewer } from "../../web/pdf_viewer.js";
37+
import { RenderingStates } from "../../web/renderable_view.js";
3838
import { StructTreeLayerBuilder } from "../../web/struct_tree_layer_builder.js";
3939
import { TextLayerBuilder } from "../../web/text_layer_builder.js";
4040
import { XfaLayerBuilder } from "../../web/xfa_layer_builder.js";

web/annotation_editor_layer_builder.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
/** @typedef {import("../src/display/editor/tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
2121
// eslint-disable-next-line max-len
2222
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
23-
/** @typedef {import("./interfaces").IL10n} IL10n */
2423
// eslint-disable-next-line max-len
2524
/** @typedef {import("../src/display/annotation_layer.js").AnnotationLayer} AnnotationLayer */
2625
// eslint-disable-next-line max-len
@@ -33,7 +32,7 @@ import { GenericL10n } from "web-null_l10n";
3332
* @typedef {Object} AnnotationEditorLayerBuilderOptions
3433
* @property {AnnotationEditorUIManager} [uiManager]
3534
* @property {PDFPageProxy} pdfPage
36-
* @property {IL10n} [l10n]
35+
* @property {L10n} [l10n]
3736
* @property {StructTreeLayerBuilder} [structTreeLayer]
3837
* @property {TextAccessibilityManager} [accessibilityManager]
3938
* @property {AnnotationLayer} [annotationLayer]

web/annotation_layer_builder.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
/** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */
1919
// eslint-disable-next-line max-len
2020
/** @typedef {import("../src/display/annotation_storage").AnnotationStorage} AnnotationStorage */
21-
/** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */
22-
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
2321
// eslint-disable-next-line max-len
2422
/** @typedef {import("./struct_tree_layer_builder.js").StructTreeLayerBuilder} StructTreeLayerBuilder */
2523
// eslint-disable-next-line max-len
@@ -43,8 +41,8 @@ import { PresentationModeState } from "./ui_utils.js";
4341
* @property {string} [imageResourcesPath] - Path for image resources, mainly
4442
* for annotation icons. Include trailing slash.
4543
* @property {boolean} renderForms
46-
* @property {IPDFLinkService} linkService
47-
* @property {IDownloadManager} [downloadManager]
44+
* @property {PDFLinkService} linkService
45+
* @property {BaseDownloadManager} [downloadManager]
4846
* @property {boolean} [enableComment]
4947
* @property {boolean} [enableScripting]
5048
* @property {Promise<boolean>} [hasJSActionsPromise]

web/app.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
/** @typedef {import("./interfaces.js").IL10n} IL10n */
1716
// eslint-disable-next-line max-len
1817
/** @typedef {import("../src/display/api.js").PDFDocumentProxy} PDFDocumentProxy */
1918
// eslint-disable-next-line max-len
@@ -34,7 +33,6 @@ import {
3433
normalizeWheelEventDirection,
3534
parseQueryString,
3635
ProgressBar,
37-
RenderingStates,
3836
ScrollMode,
3937
SidebarView,
4038
SpreadMode,
@@ -92,6 +90,7 @@ import { PdfTextExtractor } from "./pdf_text_extractor.js";
9290
import { PDFThumbnailViewer } from "web-pdf_thumbnail_viewer";
9391
import { PDFViewer } from "./pdf_viewer.js";
9492
import { Preferences } from "web-preferences";
93+
import { RenderingStates } from "./renderable_view.js";
9594
import { SecondaryToolbar } from "web-secondary_toolbar";
9695
import { SignatureManager } from "web-signature_manager";
9796
import { Toolbar } from "web-toolbar";
@@ -160,7 +159,7 @@ const PDFViewerApplication = {
160159
secondaryToolbar: null,
161160
/** @type {EventBus} */
162161
eventBus: null,
163-
/** @type {IL10n} */
162+
/** @type {L10n} */
164163
l10n: null,
165164
/** @type {AnnotationEditorParams} */
166165
annotationEditorParams: null,
@@ -2457,10 +2456,7 @@ const PDFViewerApplication = {
24572456
};
24582457

24592458
initCom(PDFViewerApplication);
2460-
2461-
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
2462-
PDFPrintServiceFactory.initGlobals(PDFViewerApplication);
2463-
}
2459+
PDFPrintServiceFactory.initGlobals(PDFViewerApplication);
24642460

24652461
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
24662462
const HOSTED_VIEWER_ORIGINS = new Set([

web/base_download_manager.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* Copyright 2013 Mozilla Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import { isPdfFile } from "pdfjs-lib";
17+
18+
class BaseDownloadManager {
19+
#openBlobUrls = new WeakMap();
20+
21+
constructor() {
22+
if (
23+
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
24+
this.constructor === BaseDownloadManager
25+
) {
26+
throw new Error("Cannot initialize BaseDownloadManager.");
27+
}
28+
}
29+
30+
_triggerDownload(blobUrl, originalUrl, filename, isAttachment = false) {
31+
throw new Error("Not implemented: _triggerDownload");
32+
}
33+
34+
_getOpenDataUrl(blobUrl, filename, dest = null) {
35+
throw new Error("Not implemented: _getOpenDataUrl");
36+
}
37+
38+
/**
39+
* @param {Uint8Array} data
40+
* @param {string} filename
41+
* @param {string} [contentType]
42+
*/
43+
downloadData(data, filename, contentType) {
44+
const blobUrl = URL.createObjectURL(
45+
new Blob([data], { type: contentType })
46+
);
47+
48+
this._triggerDownload(
49+
blobUrl,
50+
/* originalUrl = */ blobUrl,
51+
filename,
52+
/* isAttachment = */ true
53+
);
54+
}
55+
56+
/**
57+
* @param {Uint8Array} data
58+
* @param {string} filename
59+
* @param {string | null} [dest]
60+
* @returns {boolean} Indicating if the data was opened.
61+
*/
62+
openOrDownloadData(data, filename, dest = null) {
63+
const isPdfData = isPdfFile(filename);
64+
const contentType = isPdfData ? "application/pdf" : "";
65+
66+
if (isPdfData) {
67+
let blobUrl;
68+
try {
69+
blobUrl = this.#openBlobUrls.getOrInsertComputed(data, () =>
70+
URL.createObjectURL(new Blob([data], { type: contentType }))
71+
);
72+
const viewerUrl = this._getOpenDataUrl(blobUrl, filename, dest);
73+
74+
window.open(viewerUrl);
75+
return true;
76+
} catch (ex) {
77+
console.error("openOrDownloadData:", ex);
78+
// Release the `blobUrl`, since opening it failed, and fallback to
79+
// downloading the PDF file.
80+
URL.revokeObjectURL(blobUrl);
81+
this.#openBlobUrls.delete(data);
82+
}
83+
}
84+
85+
this.downloadData(data, filename, contentType);
86+
return false;
87+
}
88+
89+
/**
90+
* @param {Uint8Array} data
91+
* @param {string} url
92+
* @param {string} filename
93+
*/
94+
download(data, url, filename) {
95+
const blobUrl = data
96+
? URL.createObjectURL(new Blob([data], { type: "application/pdf" }))
97+
: null;
98+
99+
this._triggerDownload(blobUrl, /* originalUrl = */ url, filename);
100+
}
101+
}
102+
103+
export { BaseDownloadManager };

0 commit comments

Comments
 (0)