summaryrefslogtreecommitdiff
path: root/ext/js/background
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-12-21 09:29:52 -0500
committerGitHub <noreply@github.com>2023-12-21 14:29:52 +0000
commit7dcf023dcc164b24f2c18ed6ba1140342c2797c5 (patch)
tree9ce5fa98ef5ec19129acb0016ee42913bd8ed624 /ext/js/background
parent229f04ba358b3485fa7952088e03dad9e651bb23 (diff)
Remove _invokeWithProgress (#397)
Diffstat (limited to 'ext/js/background')
-rw-r--r--ext/js/background/backend.js151
1 files changed, 2 insertions, 149 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js
index d62c852b..c62685b2 100644
--- a/ext/js/background/backend.js
+++ b/ext/js/background/backend.js
@@ -22,9 +22,9 @@ import {AnkiConnect} from '../comm/anki-connect.js';
import {ClipboardMonitor} from '../comm/clipboard-monitor.js';
import {ClipboardReader} from '../comm/clipboard-reader.js';
import {Mecab} from '../comm/mecab.js';
-import {clone, deferPromise, generateId, invokeMessageHandler, isObject, log, promiseTimeout} from '../core.js';
+import {clone, deferPromise, invokeMessageHandler, isObject, log, promiseTimeout} from '../core.js';
import {ExtensionError} from '../core/extension-error.js';
-import {parseJson, readResponseJson} from '../core/json.js';
+import {readResponseJson} from '../core/json.js';
import {AnkiUtil} from '../data/anki-util.js';
import {OptionsUtil} from '../data/options-util.js';
import {PermissionsUtil} from '../data/permissions-util.js';
@@ -36,7 +36,6 @@ import {JapaneseUtil} from '../language/sandbox/japanese-util.js';
import {Translator} from '../language/translator.js';
import {AudioDownloader} from '../media/audio-downloader.js';
import {MediaUtil} from '../media/media-util.js';
-import {yomitan} from '../yomitan.js';
import {ClipboardReaderProxy, DictionaryDatabaseProxy, OffscreenProxy, TranslatorProxy} from './offscreen-proxy.js';
import {ProfileConditionsUtil} from './profile-conditions-util.js';
import {RequestBuilder} from './request-builder.js';
@@ -179,7 +178,6 @@ export class Backend {
['getMedia', this._onApiGetMedia.bind(this)],
['log', this._onApiLog.bind(this)],
['logIndicatorClear', this._onApiLogIndicatorClear.bind(this)],
- ['createActionPort', this._onApiCreateActionPort.bind(this)],
['modifySettings', this._onApiModifySettings.bind(this)],
['getSettings', this._onApiGetSettings.bind(this)],
['setAllSettings', this._onApiSetAllSettings.bind(this)],
@@ -194,10 +192,6 @@ export class Backend {
['openCrossFramePort', this._onApiOpenCrossFramePort.bind(this)]
]));
/* eslint-enable no-multi-spaces */
- /** @type {import('backend').MessageHandlerWithProgressMap} */
- this._messageHandlersWithProgress = new Map(/** @type {import('backend').MessageHandlerWithProgressMapInit} */ ([
- // Empty
- ]));
/** @type {Map<string, (params?: import('core').SerializableObject) => void>} */
this._commandHandlers = new Map(/** @type {[name: string, handler: (params?: import('core').SerializableObject) => void][]} */ ([
@@ -746,31 +740,6 @@ export class Backend {
this._updateBadge();
}
- /** @type {import('api').Handler<import('api').CreateActionPortDetails, import('api').CreateActionPortResult, true>} */
- _onApiCreateActionPort(_params, sender) {
- if (!sender || !sender.tab) { throw new Error('Invalid sender'); }
- const tabId = sender.tab.id;
- if (typeof tabId !== 'number') { throw new Error('Sender has invalid tab ID'); }
-
- const frameId = sender.frameId;
- const id = generateId(16);
- /** @type {import('cross-frame-api').ActionPortDetails} */
- const details = {
- name: 'action-port',
- id
- };
-
- const port = chrome.tabs.connect(tabId, {name: JSON.stringify(details), frameId});
- try {
- this._createActionListenerPort(port, sender, this._messageHandlersWithProgress);
- } catch (e) {
- port.disconnect();
- throw e;
- }
-
- return details;
- }
-
/** @type {import('api').Handler<import('api').ModifySettingsDetails, import('api').ModifySettingsResult>} */
_onApiModifySettings({targets, source}) {
return this._modifySettings(targets, source);
@@ -1484,107 +1453,6 @@ export class Backend {
}
/**
- * @param {chrome.runtime.Port} port
- * @param {chrome.runtime.MessageSender} sender
- * @param {import('backend').MessageHandlerWithProgressMap} handlers
- */
- _createActionListenerPort(port, sender, handlers) {
- let done = false;
- let hasStarted = false;
- /** @type {?string} */
- let messageString = '';
-
- /**
- * @param {...unknown} data
- */
- const onProgress = (...data) => {
- try {
- if (done) { return; }
- port.postMessage(/** @type {import('backend').InvokeWithProgressResponseProgressMessage} */ ({type: 'progress', data}));
- } catch (e) {
- // NOP
- }
- };
-
- /**
- * @param {import('backend').InvokeWithProgressRequestMessage} message
- */
- const onMessage = (message) => {
- if (hasStarted) { return; }
-
- try {
- const {action} = message;
- switch (action) {
- case 'fragment':
- messageString += message.data;
- break;
- case 'invoke':
- if (messageString !== null) {
- hasStarted = true;
- port.onMessage.removeListener(onMessage);
-
- /** @type {{action: string, params?: import('core').SerializableObject}} */
- const messageData = parseJson(messageString);
- messageString = null;
- onMessageComplete(messageData);
- }
- break;
- }
- } catch (e) {
- cleanup(e);
- }
- };
-
- /**
- * @param {{action: string, params?: import('core').SerializableObject}} message
- */
- const onMessageComplete = async (message) => {
- try {
- const {action, params} = message;
- port.postMessage(/** @type {import('backend').InvokeWithProgressResponseAcknowledgeMessage} */ ({type: 'ack'}));
-
- const messageHandler = handlers.get(action);
- if (typeof messageHandler === 'undefined') {
- throw new Error('Invalid action');
- }
- const {handler, async, contentScript} = messageHandler;
-
- if (!contentScript) {
- this._validatePrivilegedMessageSender(sender);
- }
-
- const promiseOrResult = handler(params, sender, onProgress);
- const result = async ? await promiseOrResult : promiseOrResult;
- port.postMessage(/** @type {import('backend').InvokeWithProgressResponseCompleteMessage} */ ({type: 'complete', data: result}));
- } catch (e) {
- cleanup(e);
- }
- };
-
- const onDisconnect = () => {
- cleanup(null);
- };
-
- /**
- * @param {unknown} error
- */
- const cleanup = (error) => {
- if (done) { return; }
- if (error !== null) {
- port.postMessage(/** @type {import('backend').InvokeWithProgressResponseErrorMessage} */ ({type: 'error', data: ExtensionError.serialize(error)}));
- }
- if (!hasStarted) {
- port.onMessage.removeListener(onMessage);
- }
- port.onDisconnect.removeListener(onDisconnect);
- done = true;
- };
-
- port.onMessage.addListener(onMessage);
- port.onDisconnect.addListener(onDisconnect);
- }
-
- /**
* @param {?import('log').LogLevel} errorLevel
* @returns {number}
*/
@@ -1693,21 +1561,6 @@ export class Backend {
}
/**
- * @param {chrome.runtime.MessageSender} sender
- * @throws {Error}
- */
- _validatePrivilegedMessageSender(sender) {
- let {url} = sender;
- if (typeof url === 'string' && yomitan.isExtensionUrl(url)) { return; }
- const {tab} = sender;
- if (typeof tab === 'object' && tab !== null) {
- ({url} = tab);
- if (typeof url === 'string' && yomitan.isExtensionUrl(url)) { return; }
- }
- throw new Error('Invalid message sender');
- }
-
- /**
* @returns {Promise<string>}
*/
_getBrowserIconTitle() {