Skip to content

Commit ba8dae6

Browse files
committed
Convert the PDFDocumentProperties class to use private methods
Given that none of these methods were ever intended to be accessed directly from the outside, we can use modern ECMAScript features to ensure that they are indeed private. This patch also makes `fieldData` private, to remove the old hack used to prevent it from being modified from the outside.
1 parent 9e4aaf1 commit ba8dae6

File tree

1 file changed

+29
-52
lines changed

1 file changed

+29
-52
lines changed

web/pdf_document_properties.js

+29-52
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ function getPageName(size, isPortrait, pageNames) {
5252
*/
5353

5454
class PDFDocumentProperties {
55+
#fieldData = null;
56+
5557
/**
5658
* @param {PDFDocumentPropertiesOptions} options
5759
* @param {OverlayManager} overlayManager - Manager for the viewer overlays.
@@ -70,7 +72,7 @@ class PDFDocumentProperties {
7072
this.overlayManager = overlayManager;
7173
this.l10n = l10n;
7274

73-
this._reset();
75+
this.#reset();
7476
// Bind the event listener for the Close button.
7577
closeButton.addEventListener("click", this.close.bind(this));
7678

@@ -97,15 +99,6 @@ class PDFDocumentProperties {
9799
* Open the document properties overlay.
98100
*/
99101
async open() {
100-
const freezeFieldData = data => {
101-
Object.defineProperty(this, "fieldData", {
102-
value: Object.freeze(data),
103-
writable: false,
104-
enumerable: true,
105-
configurable: true,
106-
});
107-
};
108-
109102
await Promise.all([
110103
this.overlayManager.open(this.overlayName),
111104
this._dataAvailableCapability.promise,
@@ -116,11 +109,11 @@ class PDFDocumentProperties {
116109
// If the document properties were previously fetched (for this PDF file),
117110
// just update the dialog immediately to avoid redundant lookups.
118111
if (
119-
this.fieldData &&
120-
currentPageNumber === this.fieldData._currentPageNumber &&
121-
pagesRotation === this.fieldData._pagesRotation
112+
this.#fieldData &&
113+
currentPageNumber === this.#fieldData._currentPageNumber &&
114+
pagesRotation === this.#fieldData._pagesRotation
122115
) {
123-
this._updateUI();
116+
this.#updateUI();
124117
return;
125118
}
126119

@@ -141,16 +134,16 @@ class PDFDocumentProperties {
141134
isLinearized,
142135
] = await Promise.all([
143136
contentDispositionFilename || getPdfFilenameFromUrl(this.url),
144-
this._parseFileSize(contentLength),
145-
this._parseDate(info.CreationDate),
146-
this._parseDate(info.ModDate),
137+
this.#parseFileSize(contentLength),
138+
this.#parseDate(info.CreationDate),
139+
this.#parseDate(info.ModDate),
147140
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
148-
return this._parsePageSize(getPageSizeInches(pdfPage), pagesRotation);
141+
return this.#parsePageSize(getPageSizeInches(pdfPage), pagesRotation);
149142
}),
150-
this._parseLinearization(info.IsLinearized),
143+
this.#parseLinearization(info.IsLinearized),
151144
]);
152145

153-
freezeFieldData({
146+
this.#fieldData = Object.freeze({
154147
fileName,
155148
fileSize,
156149
title: info.Title,
@@ -168,19 +161,19 @@ class PDFDocumentProperties {
168161
_currentPageNumber: currentPageNumber,
169162
_pagesRotation: pagesRotation,
170163
});
171-
this._updateUI();
164+
this.#updateUI();
172165

173166
// Get the correct fileSize, since it may not have been available
174167
// or could potentially be wrong.
175168
const { length } = await this.pdfDocument.getDownloadInfo();
176169
if (contentLength === length) {
177170
return; // The fileSize has already been correctly set.
178171
}
179-
const data = Object.assign(Object.create(null), this.fieldData);
180-
data.fileSize = await this._parseFileSize(length);
172+
const data = Object.assign(Object.create(null), this.#fieldData);
173+
data.fileSize = await this.#parseFileSize(length);
181174

182-
freezeFieldData(data);
183-
this._updateUI();
175+
this.#fieldData = Object.freeze(data);
176+
this.#updateUI();
184177
}
185178

186179
/**
@@ -201,8 +194,8 @@ class PDFDocumentProperties {
201194
*/
202195
setDocument(pdfDocument, url = null) {
203196
if (this.pdfDocument) {
204-
this._reset();
205-
this._updateUI(true);
197+
this.#reset();
198+
this.#updateUI(true);
206199
}
207200
if (!pdfDocument) {
208201
return;
@@ -213,14 +206,11 @@ class PDFDocumentProperties {
213206
this._dataAvailableCapability.resolve();
214207
}
215208

216-
/**
217-
* @private
218-
*/
219-
_reset() {
209+
#reset() {
220210
this.pdfDocument = null;
221211
this.url = null;
222212

223-
delete this.fieldData;
213+
this.#fieldData = null;
224214
this._dataAvailableCapability = createPromiseCapability();
225215
this._currentPageNumber = 1;
226216
this._pagesRotation = 0;
@@ -230,10 +220,9 @@ class PDFDocumentProperties {
230220
* Always updates all of the dialog fields, to prevent inconsistent UI state.
231221
* NOTE: If the contents of a particular field is neither a non-empty string,
232222
* nor a number, it will fall back to `DEFAULT_FIELD_CONTENT`.
233-
* @private
234223
*/
235-
_updateUI(reset = false) {
236-
if (reset || !this.fieldData) {
224+
#updateUI(reset = false) {
225+
if (reset || !this.#fieldData) {
237226
for (const id in this.fields) {
238227
this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
239228
}
@@ -245,16 +234,13 @@ class PDFDocumentProperties {
245234
return;
246235
}
247236
for (const id in this.fields) {
248-
const content = this.fieldData[id];
237+
const content = this.#fieldData[id];
249238
this.fields[id].textContent =
250239
content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
251240
}
252241
}
253242

254-
/**
255-
* @private
256-
*/
257-
async _parseFileSize(fileSize = 0) {
243+
async #parseFileSize(fileSize = 0) {
258244
const kb = fileSize / 1024,
259245
mb = kb / 1024;
260246
if (!kb) {
@@ -267,10 +253,7 @@ class PDFDocumentProperties {
267253
});
268254
}
269255

270-
/**
271-
* @private
272-
*/
273-
async _parsePageSize(pageSizeInches, pagesRotation) {
256+
async #parsePageSize(pageSizeInches, pagesRotation) {
274257
if (!pageSizeInches) {
275258
return undefined;
276259
}
@@ -364,10 +347,7 @@ class PDFDocumentProperties {
364347
);
365348
}
366349

367-
/**
368-
* @private
369-
*/
370-
async _parseDate(inputDate) {
350+
async #parseDate(inputDate) {
371351
const dateObject = PDFDateString.toDateObject(inputDate);
372352
if (!dateObject) {
373353
return undefined;
@@ -378,10 +358,7 @@ class PDFDocumentProperties {
378358
});
379359
}
380360

381-
/**
382-
* @private
383-
*/
384-
_parseLinearization(isLinearized) {
361+
#parseLinearization(isLinearized) {
385362
return this.l10n.get(
386363
`document_properties_linearized_${isLinearized ? "yes" : "no"}`
387364
);

0 commit comments

Comments
 (0)