2017-04-17 01:55:45 +02:00
|
|
|
/* Copyright 2017 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-10-19 14:33:37 +02:00
|
|
|
import { AppOptions } from "./app_options.js";
|
2024-01-26 14:31:42 +01:00
|
|
|
import { BaseExternalServices } from "./external_services.js";
|
2020-01-02 12:00:16 +01:00
|
|
|
import { BasePreferences } from "./preferences.js";
|
|
|
|
import { GenericL10n } from "./genericl10n.js";
|
2020-12-07 18:15:24 +01:00
|
|
|
import { GenericScripting } from "./generic_scripting.js";
|
2025-02-05 19:34:11 +01:00
|
|
|
import { SignatureStorage } from "./generic_signature_storage.js";
|
2017-04-17 01:55:45 +02:00
|
|
|
|
|
|
|
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) {
|
|
|
|
throw new Error(
|
2022-03-12 14:40:18 +01:00
|
|
|
'Module "pdfjs-web/genericcom" shall not be used outside GENERIC build.'
|
2017-04-17 01:55:45 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-09 12:01:59 +01:00
|
|
|
function initCom(app) {}
|
2017-04-17 01:55:45 +02:00
|
|
|
|
2024-01-26 12:04:54 +01:00
|
|
|
class Preferences extends BasePreferences {
|
2018-07-30 16:48:16 +02:00
|
|
|
async _writeToStorage(prefObj) {
|
|
|
|
localStorage.setItem("pdfjs.preferences", JSON.stringify(prefObj));
|
2017-04-17 13:21:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 16:48:16 +02:00
|
|
|
async _readFromStorage(prefObj) {
|
2023-10-31 11:39:04 +01:00
|
|
|
return { prefs: JSON.parse(localStorage.getItem("pdfjs.preferences")) };
|
2017-04-17 13:21:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 14:31:42 +01:00
|
|
|
class ExternalServices extends BaseExternalServices {
|
|
|
|
async createL10n() {
|
2024-07-23 09:43:12 +02:00
|
|
|
return new GenericL10n(AppOptions.get("localeProperties")?.lang);
|
2020-01-15 14:26:47 +01:00
|
|
|
}
|
2020-11-09 14:45:02 +01:00
|
|
|
|
2024-01-26 14:31:42 +01:00
|
|
|
createScripting() {
|
2023-12-09 09:40:07 +01:00
|
|
|
return new GenericScripting(AppOptions.get("sandboxBundleSrc"));
|
2020-11-09 14:45:02 +01:00
|
|
|
}
|
2025-02-05 19:34:11 +01:00
|
|
|
|
2025-02-13 21:15:27 +01:00
|
|
|
createSignatureStorage(eventBus, signal) {
|
|
|
|
return new SignatureStorage(eventBus, signal);
|
2025-02-05 19:34:11 +01:00
|
|
|
}
|
2020-01-15 14:26:47 +01:00
|
|
|
}
|
2017-04-17 01:55:45 +02:00
|
|
|
|
2024-02-20 09:49:20 +01:00
|
|
|
class MLManager {
|
2025-02-27 12:59:58 +01:00
|
|
|
static {
|
|
|
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
|
|
this.getFakeMLManager = options => new FakeMLManager(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-19 17:57:53 +02:00
|
|
|
async isEnabledFor(_name) {
|
2024-07-05 17:56:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-07-19 18:58:20 +02:00
|
|
|
async deleteModel(_service) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-07-24 09:34:32 +02:00
|
|
|
isReady(_name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
guess(_data) {}
|
|
|
|
|
|
|
|
toggleService(_name, _enabled) {}
|
|
|
|
}
|
|
|
|
|
2025-02-27 12:59:58 +01:00
|
|
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var FakeMLManager = class {
|
|
|
|
eventBus = null;
|
|
|
|
|
|
|
|
hasProgress = false;
|
|
|
|
|
|
|
|
constructor({ enableGuessAltText, enableAltTextModelDownload }) {
|
|
|
|
this.enableGuessAltText = enableGuessAltText;
|
|
|
|
this.enableAltTextModelDownload = enableAltTextModelDownload;
|
|
|
|
}
|
|
|
|
|
|
|
|
setEventBus(eventBus, abortSignal) {
|
|
|
|
this.eventBus = eventBus;
|
|
|
|
}
|
|
|
|
|
|
|
|
async isEnabledFor(_name) {
|
|
|
|
return this.enableGuessAltText;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteModel(_name) {
|
|
|
|
this.enableAltTextModelDownload = false;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadModel(_name) {}
|
|
|
|
|
|
|
|
async downloadModel(_name) {
|
|
|
|
// Simulate downloading the model but with progress.
|
|
|
|
// The progress can be seen in the new alt-text dialog.
|
|
|
|
this.hasProgress = true;
|
|
|
|
|
|
|
|
const { promise, resolve } = Promise.withResolvers();
|
|
|
|
const total = 1e8;
|
|
|
|
const end = 1.5 * total;
|
|
|
|
const increment = 5e6;
|
|
|
|
let loaded = 0;
|
|
|
|
const id = setInterval(() => {
|
|
|
|
loaded += increment;
|
|
|
|
if (loaded <= end) {
|
|
|
|
this.eventBus.dispatch("loadaiengineprogress", {
|
|
|
|
source: this,
|
|
|
|
detail: {
|
|
|
|
total,
|
|
|
|
totalLoaded: loaded,
|
|
|
|
finished: loaded + increment >= end,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
clearInterval(id);
|
|
|
|
this.hasProgress = false;
|
|
|
|
this.enableAltTextModelDownload = true;
|
|
|
|
resolve(true);
|
|
|
|
}, 900);
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
isReady(_name) {
|
|
|
|
return this.enableAltTextModelDownload;
|
|
|
|
}
|
|
|
|
|
|
|
|
guess({ request: { data } }) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(data ? { output: "Fake alt text." } : { error: true });
|
|
|
|
}, 3000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleService(_name, enabled) {
|
|
|
|
this.enableGuessAltText = enabled;
|
|
|
|
}
|
|
|
|
};
|
2024-02-20 09:49:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export { ExternalServices, initCom, MLManager, Preferences };
|