Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify message handlers #396

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ export class Frontend {

/* eslint-disable no-multi-spaces */
/** @type {import('core').MessageHandlerMap} */
this._runtimeMessageHandlers = new Map(/** @type {import('core').MessageHandlerArray} */ ([
['Frontend.requestReadyBroadcast', {async: false, handler: this._onMessageRequestFrontendReadyBroadcast.bind(this)}],
['Frontend.setAllVisibleOverride', {async: true, handler: this._onApiSetAllVisibleOverride.bind(this)}],
['Frontend.clearAllVisibleOverride', {async: true, handler: this._onApiClearAllVisibleOverride.bind(this)}]
this._runtimeMessageHandlers = new Map(/** @type {import('core').MessageHandlerMapInit} */ ([
['Frontend.requestReadyBroadcast', this._onMessageRequestFrontendReadyBroadcast.bind(this)],
['Frontend.setAllVisibleOverride', this._onApiSetAllVisibleOverride.bind(this)],
['Frontend.clearAllVisibleOverride', this._onApiClearAllVisibleOverride.bind(this)]
]));

this._hotkeyHandler.registerActions([
Expand Down Expand Up @@ -178,11 +178,11 @@ export class Frontend {

/* eslint-disable no-multi-spaces */
yomitan.crossFrame.registerHandlers([
['Frontend.closePopup', {async: false, handler: this._onApiClosePopup.bind(this)}],
['Frontend.copySelection', {async: false, handler: this._onApiCopySelection.bind(this)}],
['Frontend.getSelectionText', {async: false, handler: this._onApiGetSelectionText.bind(this)}],
['Frontend.getPopupInfo', {async: false, handler: this._onApiGetPopupInfo.bind(this)}],
['Frontend.getPageInfo', {async: false, handler: this._onApiGetPageInfo.bind(this)}]
['Frontend.closePopup', this._onApiClosePopup.bind(this)],
['Frontend.copySelection', this._onApiCopySelection.bind(this)],
['Frontend.getSelectionText', this._onApiGetSelectionText.bind(this)],
['Frontend.getPopupInfo', this._onApiGetPopupInfo.bind(this)],
['Frontend.getPageInfo', this._onApiGetPageInfo.bind(this)]
]);
/* eslint-enable no-multi-spaces */

Expand Down
30 changes: 15 additions & 15 deletions ext/js/app/popup-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ export class PopupFactory {
this._frameOffsetForwarder.prepare();
/* eslint-disable no-multi-spaces */
yomitan.crossFrame.registerHandlers([
['PopupFactory.getOrCreatePopup', {async: true, handler: this._onApiGetOrCreatePopup.bind(this)}],
['PopupFactory.setOptionsContext', {async: true, handler: this._onApiSetOptionsContext.bind(this)}],
['PopupFactory.hide', {async: true, handler: this._onApiHide.bind(this)}],
['PopupFactory.isVisible', {async: true, handler: this._onApiIsVisibleAsync.bind(this)}],
['PopupFactory.setVisibleOverride', {async: true, handler: this._onApiSetVisibleOverride.bind(this)}],
['PopupFactory.clearVisibleOverride', {async: true, handler: this._onApiClearVisibleOverride.bind(this)}],
['PopupFactory.containsPoint', {async: true, handler: this._onApiContainsPoint.bind(this)}],
['PopupFactory.showContent', {async: true, handler: this._onApiShowContent.bind(this)}],
['PopupFactory.setCustomCss', {async: true, handler: this._onApiSetCustomCss.bind(this)}],
['PopupFactory.clearAutoPlayTimer', {async: true, handler: this._onApiClearAutoPlayTimer.bind(this)}],
['PopupFactory.setContentScale', {async: true, handler: this._onApiSetContentScale.bind(this)}],
['PopupFactory.updateTheme', {async: true, handler: this._onApiUpdateTheme.bind(this)}],
['PopupFactory.setCustomOuterCss', {async: true, handler: this._onApiSetCustomOuterCss.bind(this)}],
['PopupFactory.getFrameSize', {async: true, handler: this._onApiGetFrameSize.bind(this)}],
['PopupFactory.setFrameSize', {async: true, handler: this._onApiSetFrameSize.bind(this)}]
['PopupFactory.getOrCreatePopup', this._onApiGetOrCreatePopup.bind(this)],
['PopupFactory.setOptionsContext', this._onApiSetOptionsContext.bind(this)],
['PopupFactory.hide', this._onApiHide.bind(this)],
['PopupFactory.isVisible', this._onApiIsVisibleAsync.bind(this)],
['PopupFactory.setVisibleOverride', this._onApiSetVisibleOverride.bind(this)],
['PopupFactory.clearVisibleOverride', this._onApiClearVisibleOverride.bind(this)],
['PopupFactory.containsPoint', this._onApiContainsPoint.bind(this)],
['PopupFactory.showContent', this._onApiShowContent.bind(this)],
['PopupFactory.setCustomCss', this._onApiSetCustomCss.bind(this)],
['PopupFactory.clearAutoPlayTimer', this._onApiClearAutoPlayTimer.bind(this)],
['PopupFactory.setContentScale', this._onApiSetContentScale.bind(this)],
['PopupFactory.updateTheme', this._onApiUpdateTheme.bind(this)],
['PopupFactory.setCustomOuterCss', this._onApiSetCustomOuterCss.bind(this)],
['PopupFactory.getFrameSize', this._onApiGetFrameSize.bind(this)],
['PopupFactory.setFrameSize', this._onApiSetFrameSize.bind(this)]
]);
/* eslint-enable no-multi-spaces */
}
Expand Down
100 changes: 45 additions & 55 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,51 +149,51 @@ export class Backend {
this._permissionsUtil = new PermissionsUtil();

/* eslint-disable no-multi-spaces */
/** @type {import('backend').MessageHandlerMap} */
this._messageHandlers = new Map(/** @type {import('backend').MessageHandlerMapInit} */ ([
['requestBackendReadySignal', {async: false, contentScript: true, handler: this._onApiRequestBackendReadySignal.bind(this)}],
['optionsGet', {async: false, contentScript: true, handler: this._onApiOptionsGet.bind(this)}],
['optionsGetFull', {async: false, contentScript: true, handler: this._onApiOptionsGetFull.bind(this)}],
['kanjiFind', {async: true, contentScript: true, handler: this._onApiKanjiFind.bind(this)}],
['termsFind', {async: true, contentScript: true, handler: this._onApiTermsFind.bind(this)}],
['parseText', {async: true, contentScript: true, handler: this._onApiParseText.bind(this)}],
['getAnkiConnectVersion', {async: true, contentScript: true, handler: this._onApiGetAnkiConnectVersion.bind(this)}],
['isAnkiConnected', {async: true, contentScript: true, handler: this._onApiIsAnkiConnected.bind(this)}],
['addAnkiNote', {async: true, contentScript: true, handler: this._onApiAddAnkiNote.bind(this)}],
['getAnkiNoteInfo', {async: true, contentScript: true, handler: this._onApiGetAnkiNoteInfo.bind(this)}],
['injectAnkiNoteMedia', {async: true, contentScript: true, handler: this._onApiInjectAnkiNoteMedia.bind(this)}],
['noteView', {async: true, contentScript: true, handler: this._onApiNoteView.bind(this)}],
['suspendAnkiCardsForNote', {async: true, contentScript: true, handler: this._onApiSuspendAnkiCardsForNote.bind(this)}],
['commandExec', {async: false, contentScript: true, handler: this._onApiCommandExec.bind(this)}],
['getTermAudioInfoList', {async: true, contentScript: true, handler: this._onApiGetTermAudioInfoList.bind(this)}],
['sendMessageToFrame', {async: false, contentScript: true, handler: this._onApiSendMessageToFrame.bind(this)}],
['broadcastTab', {async: false, contentScript: true, handler: this._onApiBroadcastTab.bind(this)}],
['frameInformationGet', {async: true, contentScript: true, handler: this._onApiFrameInformationGet.bind(this)}],
['injectStylesheet', {async: true, contentScript: true, handler: this._onApiInjectStylesheet.bind(this)}],
['getStylesheetContent', {async: true, contentScript: true, handler: this._onApiGetStylesheetContent.bind(this)}],
['getEnvironmentInfo', {async: false, contentScript: true, handler: this._onApiGetEnvironmentInfo.bind(this)}],
['clipboardGet', {async: true, contentScript: true, handler: this._onApiClipboardGet.bind(this)}],
['getDisplayTemplatesHtml', {async: true, contentScript: true, handler: this._onApiGetDisplayTemplatesHtml.bind(this)}],
['getZoom', {async: true, contentScript: true, handler: this._onApiGetZoom.bind(this)}],
['getDefaultAnkiFieldTemplates', {async: false, contentScript: true, handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this)}],
['getDictionaryInfo', {async: true, contentScript: true, handler: this._onApiGetDictionaryInfo.bind(this)}],
['purgeDatabase', {async: true, contentScript: false, handler: this._onApiPurgeDatabase.bind(this)}],
['getMedia', {async: true, contentScript: true, handler: this._onApiGetMedia.bind(this)}],
['log', {async: false, contentScript: true, handler: this._onApiLog.bind(this)}],
['logIndicatorClear', {async: false, contentScript: true, handler: this._onApiLogIndicatorClear.bind(this)}],
['createActionPort', {async: false, contentScript: true, handler: this._onApiCreateActionPort.bind(this)}],
['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}],
['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}],
['setAllSettings', {async: true, contentScript: false, handler: this._onApiSetAllSettings.bind(this)}],
['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}],
['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.bind(this)}],
['triggerDatabaseUpdated', {async: false, contentScript: true, handler: this._onApiTriggerDatabaseUpdated.bind(this)}],
['testMecab', {async: true, contentScript: true, handler: this._onApiTestMecab.bind(this)}],
['textHasJapaneseCharacters', {async: false, contentScript: true, handler: this._onApiTextHasJapaneseCharacters.bind(this)}],
['getTermFrequencies', {async: true, contentScript: true, handler: this._onApiGetTermFrequencies.bind(this)}],
['findAnkiNotes', {async: true, contentScript: true, handler: this._onApiFindAnkiNotes.bind(this)}],
['loadExtensionScripts', {async: true, contentScript: true, handler: this._onApiLoadExtensionScripts.bind(this)}],
['openCrossFramePort', {async: false, contentScript: true, handler: this._onApiOpenCrossFramePort.bind(this)}]
/** @type {import('core').MessageHandlerMap} */
this._messageHandlers = new Map(/** @type {import('core').MessageHandlerMapInit} */ ([
['requestBackendReadySignal', this._onApiRequestBackendReadySignal.bind(this)],
['optionsGet', this._onApiOptionsGet.bind(this)],
['optionsGetFull', this._onApiOptionsGetFull.bind(this)],
['kanjiFind', this._onApiKanjiFind.bind(this)],
['termsFind', this._onApiTermsFind.bind(this)],
['parseText', this._onApiParseText.bind(this)],
['getAnkiConnectVersion', this._onApiGetAnkiConnectVersion.bind(this)],
['isAnkiConnected', this._onApiIsAnkiConnected.bind(this)],
['addAnkiNote', this._onApiAddAnkiNote.bind(this)],
['getAnkiNoteInfo', this._onApiGetAnkiNoteInfo.bind(this)],
['injectAnkiNoteMedia', this._onApiInjectAnkiNoteMedia.bind(this)],
['noteView', this._onApiNoteView.bind(this)],
['suspendAnkiCardsForNote', this._onApiSuspendAnkiCardsForNote.bind(this)],
['commandExec', this._onApiCommandExec.bind(this)],
['getTermAudioInfoList', this._onApiGetTermAudioInfoList.bind(this)],
['sendMessageToFrame', this._onApiSendMessageToFrame.bind(this)],
['broadcastTab', this._onApiBroadcastTab.bind(this)],
['frameInformationGet', this._onApiFrameInformationGet.bind(this)],
['injectStylesheet', this._onApiInjectStylesheet.bind(this)],
['getStylesheetContent', this._onApiGetStylesheetContent.bind(this)],
['getEnvironmentInfo', this._onApiGetEnvironmentInfo.bind(this)],
['clipboardGet', this._onApiClipboardGet.bind(this)],
['getDisplayTemplatesHtml', this._onApiGetDisplayTemplatesHtml.bind(this)],
['getZoom', this._onApiGetZoom.bind(this)],
['getDefaultAnkiFieldTemplates', this._onApiGetDefaultAnkiFieldTemplates.bind(this)],
['getDictionaryInfo', this._onApiGetDictionaryInfo.bind(this)],
['purgeDatabase', this._onApiPurgeDatabase.bind(this)],
['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)],
['getOrCreateSearchPopup', this._onApiGetOrCreateSearchPopup.bind(this)],
['isTabSearchPopup', this._onApiIsTabSearchPopup.bind(this)],
['triggerDatabaseUpdated', this._onApiTriggerDatabaseUpdated.bind(this)],
['testMecab', this._onApiTestMecab.bind(this)],
['textHasJapaneseCharacters', this._onApiTextHasJapaneseCharacters.bind(this)],
['getTermFrequencies', this._onApiGetTermFrequencies.bind(this)],
['findAnkiNotes', this._onApiFindAnkiNotes.bind(this)],
['loadExtensionScripts', this._onApiLoadExtensionScripts.bind(this)],
['openCrossFramePort', this._onApiOpenCrossFramePort.bind(this)]
]));
/* eslint-enable no-multi-spaces */
/** @type {import('backend').MessageHandlerWithProgressMap} */
Expand Down Expand Up @@ -408,16 +408,6 @@ export class Backend {
_onMessage({action, params}, sender, callback) {
const messageHandler = this._messageHandlers.get(action);
if (typeof messageHandler === 'undefined') { return false; }

if (!messageHandler.contentScript) {
try {
this._validatePrivilegedMessageSender(sender);
} catch (error) {
callback({error: ExtensionError.serialize(error)});
return false;
}
}

return invokeMessageHandler(messageHandler, params, callback, sender);
}

Expand Down
32 changes: 15 additions & 17 deletions ext/js/background/offscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,21 @@ export class Offscreen {

/* eslint-disable no-multi-spaces */
/** @type {import('offscreen').MessageHandlerMap} */
const messageHandlers = new Map([
['clipboardGetTextOffscreen', {async: true, handler: this._getTextHandler.bind(this)}],
['clipboardGetImageOffscreen', {async: true, handler: this._getImageHandler.bind(this)}],
['clipboardSetBrowserOffscreen', {async: false, handler: this._setClipboardBrowser.bind(this)}],
['databasePrepareOffscreen', {async: true, handler: this._prepareDatabaseHandler.bind(this)}],
['getDictionaryInfoOffscreen', {async: true, handler: this._getDictionaryInfoHandler.bind(this)}],
['databasePurgeOffscreen', {async: true, handler: this._purgeDatabaseHandler.bind(this)}],
['databaseGetMediaOffscreen', {async: true, handler: this._getMediaHandler.bind(this)}],
['translatorPrepareOffscreen', {async: false, handler: this._prepareTranslatorHandler.bind(this)}],
['findKanjiOffscreen', {async: true, handler: this._findKanjiHandler.bind(this)}],
['findTermsOffscreen', {async: true, handler: this._findTermsHandler.bind(this)}],
['getTermFrequenciesOffscreen', {async: true, handler: this._getTermFrequenciesHandler.bind(this)}],
['clearDatabaseCachesOffscreen', {async: false, handler: this._clearDatabaseCachesHandler.bind(this)}]
]);
this._messageHandlers = new Map(/** @type {import('offscreen').MessageHandlerMapInit} */ ([
['clipboardGetTextOffscreen', this._getTextHandler.bind(this)],
['clipboardGetImageOffscreen', this._getImageHandler.bind(this)],
['clipboardSetBrowserOffscreen', this._setClipboardBrowser.bind(this)],
['databasePrepareOffscreen', this._prepareDatabaseHandler.bind(this)],
['getDictionaryInfoOffscreen', this._getDictionaryInfoHandler.bind(this)],
['databasePurgeOffscreen', this._purgeDatabaseHandler.bind(this)],
['databaseGetMediaOffscreen', this._getMediaHandler.bind(this)],
['translatorPrepareOffscreen', this._prepareTranslatorHandler.bind(this)],
['findKanjiOffscreen', this._findKanjiHandler.bind(this)],
['findTermsOffscreen', this._findTermsHandler.bind(this)],
['getTermFrequenciesOffscreen', this._getTermFrequenciesHandler.bind(this)],
['clearDatabaseCachesOffscreen', this._clearDatabaseCachesHandler.bind(this)]
]));
/* eslint-enable no-multi-spaces */
/** @type {import('offscreen').MessageHandlerMap<string>} */
this._messageHandlers = messageHandlers;

const onMessage = this._onMessage.bind(this);
chrome.runtime.onMessage.addListener(onMessage);
Expand Down Expand Up @@ -172,7 +170,7 @@ export class Offscreen {

/** @type {import('extension').ChromeRuntimeOnMessageCallback} */
_onMessage({action, params}, sender, callback) {
const messageHandler = this._messageHandlers.get(action);
const messageHandler = this._messageHandlers.get(/** @type {import('offscreen').MessageType} */ (action));
if (typeof messageHandler === 'undefined') { return false; }
return invokeMessageHandler(messageHandler, params, callback, sender);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/js/comm/cross-frame-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export class CrossFrameAPI {
}

/**
* @param {import('core').MessageHandlerArray} messageHandlers
* @param {import('core').MessageHandlerMapInit} messageHandlers
* @throws {Error}
*/
registerHandlers(messageHandlers) {
Expand Down
4 changes: 3 additions & 1 deletion ext/js/comm/frame-ancestry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ export class FrameAncestryHandler {
};

// Start
yomitan.crossFrame.registerHandlers([[responseMessageId, {async: false, handler: onMessage}]]);
yomitan.crossFrame.registerHandlers([
[responseMessageId, onMessage]
]);
resetTimeout();
const frameId = this._frameId;
this._requestFrameInfo(targetWindow, frameId, frameId, uniqueId, nonce);
Expand Down
Loading