/*
* Copyright (C) 2023-2024 Yomitan Authors
* Copyright (C) 2016-2022 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import {ExtensionError} from '../core/extension-error.js';
import {isObject} from '../core/utilities.js';
import {base64ToArrayBuffer} from '../data/sandbox/array-buffer-util.js';
export class OffscreenProxy {
/**
* @param {import('../extension/web-extension.js').WebExtension} webExtension
*/
constructor(webExtension) {
/** @type {import('../extension/web-extension.js').WebExtension} */
this._webExtension = webExtension;
/** @type {?Promise} */
this._creatingOffscreen = null;
}
/**
* @see https://developer.chrome.com/docs/extensions/reference/offscreen/
*/
async prepare() {
if (await this._hasOffscreenDocument()) {
return;
}
if (this._creatingOffscreen) {
await this._creatingOffscreen;
return;
}
this._creatingOffscreen = chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: [
/** @type {chrome.offscreen.Reason} */ ('CLIPBOARD')
],
justification: 'Access to the clipboard'
});
await this._creatingOffscreen;
this._creatingOffscreen = null;
}
/**
* @returns {Promise}
*/
async _hasOffscreenDocument() {
const offscreenUrl = chrome.runtime.getURL('offscreen.html');
// @ts-expect-error - API not defined yet
if (!chrome.runtime.getContexts) { // chrome version below 116
// Clients: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/clients
// @ts-expect-error - Types not set up for service workers yet
const matchedClients = await clients.matchAll();
// @ts-expect-error - Types not set up for service workers yet
return await matchedClients.some((client) => client.url === offscreenUrl);
}
// @ts-expect-error - API not defined yet
const contexts = await chrome.runtime.getContexts({
contextTypes: ['OFFSCREEN_DOCUMENT'],
documentUrls: [offscreenUrl]
});
return !!contexts.length;
}
/**
* @template {import('offscreen').ApiNames} TMessageType
* @param {import('offscreen').ApiMessage} message
* @returns {Promise>}
*/
async sendMessagePromise(message) {
const response = await this._webExtension.sendMessagePromise(message);
return this._getMessageResponseResult(/** @type {import('core').Response>} */ (response));
}
/**
* @template [TReturn=unknown]
* @param {import('core').Response} response
* @returns {TReturn}
* @throws {Error}
*/
_getMessageResponseResult(response) {
const runtimeError = chrome.runtime.lastError;
if (typeof runtimeError !== 'undefined') {
throw new Error(runtimeError.message);
}
if (!isObject(response)) {
throw new Error('Offscreen document did not respond');
}
const responseError = response.error;
if (responseError) {
throw ExtensionError.deserialize(responseError);
}
return response.result;
}
}
export class DictionaryDatabaseProxy {
/**
* @param {OffscreenProxy} offscreen
*/
constructor(offscreen) {
/** @type {OffscreenProxy} */
this._offscreen = offscreen;
}
/**
* @returns {Promise}
*/
async prepare() {
await this._offscreen.sendMessagePromise({action: 'databasePrepareOffscreen'});
}
/**
* @returns {Promise}
*/
async getDictionaryInfo() {
return this._offscreen.sendMessagePromise({action: 'getDictionaryInfoOffscreen'});
}
/**
* @returns {Promise}
*/
async purge() {
return await this._offscreen.sendMessagePromise({action: 'databasePurgeOffscreen'});
}
/**
* @param {import('dictionary-database').MediaRequest[]} targets
* @returns {Promise}
*/
async getMedia(targets) {
const serializedMedia = /** @type {import('dictionary-database').Media[]} */ (await this._offscreen.sendMessagePromise({action: 'databaseGetMediaOffscreen', params: {targets}}));
const media = serializedMedia.map((m) => ({...m, content: base64ToArrayBuffer(m.content)}));
return media;
}
}
export class TranslatorProxy {
/**
* @param {OffscreenProxy} offscreen
*/
constructor(offscreen) {
/** @type {OffscreenProxy} */
this._offscreen = offscreen;
}
/**
* @param {import('language-transformer').LanguageTransformDescriptor} descriptor
*/
async prepare(descriptor) {
await this._offscreen.sendMessagePromise({action: 'translatorPrepareOffscreen', params: {descriptor}});
}
/**
* @param {string} text
* @param {import('translation').FindKanjiOptions} options
* @returns {Promise}
*/
async findKanji(text, options) {
const enabledDictionaryMapList = [...options.enabledDictionaryMap];
/** @type {import('offscreen').FindKanjiOptionsOffscreen} */
const modifiedOptions = {
...options,
enabledDictionaryMap: enabledDictionaryMapList
};
return this._offscreen.sendMessagePromise({action: 'findKanjiOffscreen', params: {text, options: modifiedOptions}});
}
/**
* @param {import('translator').FindTermsMode} mode
* @param {string} text
* @param {import('translation').FindTermsOptions} options
* @returns {Promise}
*/
async findTerms(mode, text, options) {
const {enabledDictionaryMap, excludeDictionaryDefinitions, textReplacements} = options;
const enabledDictionaryMapList = [...enabledDictionaryMap];
const excludeDictionaryDefinitionsList = excludeDictionaryDefinitions ? [...excludeDictionaryDefinitions] : null;
const textReplacementsSerialized = textReplacements.map((group) => {
return group !== null ? group.map((opt) => ({...opt, pattern: opt.pattern.toString()})) : null;
});
/** @type {import('offscreen').FindTermsOptionsOffscreen} */
const modifiedOptions = {
...options,
enabledDictionaryMap: enabledDictionaryMapList,
excludeDictionaryDefinitions: excludeDictionaryDefinitionsList,
textReplacements: textReplacementsSerialized
};
return this._offscreen.sendMessagePromise({action: 'findTermsOffscreen', params: {mode, text, options: modifiedOptions}});
}
/**
* @param {import('translator').TermReadingList} termReadingList
* @param {string[]} dictionaries
* @returns {Promise}
*/
async getTermFrequencies(termReadingList, dictionaries) {
return this._offscreen.sendMessagePromise({action: 'getTermFrequenciesOffscreen', params: {termReadingList, dictionaries}});
}
/** */
async clearDatabaseCaches() {
await this._offscreen.sendMessagePromise({action: 'clearDatabaseCachesOffscreen'});
}
}
export class ClipboardReaderProxy {
/**
* @param {OffscreenProxy} offscreen
*/
constructor(offscreen) {
/** @type {?import('environment').Browser} */
this._browser = null;
/** @type {OffscreenProxy} */
this._offscreen = offscreen;
}
/** @type {?import('environment').Browser} */
get browser() { return this._browser; }
set browser(value) {
if (this._browser === value) { return; }
this._browser = value;
this._offscreen.sendMessagePromise({action: 'clipboardSetBrowserOffscreen', params: {value}});
}
/**
* @param {boolean} useRichText
* @returns {Promise}
*/
async getText(useRichText) {
return await this._offscreen.sendMessagePromise({action: 'clipboardGetTextOffscreen', params: {useRichText}});
}
/**
* @returns {Promise}
*/
async getImage() {
return await this._offscreen.sendMessagePromise({action: 'clipboardGetImageOffscreen'});
}
}