Add contentScript parameter to backend message handlers (#512)

* Add contentScript parameter to message handlers

* Remove _validatePrivilegedMessageSender calls from handler functions

* Reformat handlers
This commit is contained in:
toasted-nutbread 2020-05-07 19:37:25 -04:00 committed by GitHub
parent f361139d74
commit caca9869c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -81,53 +81,53 @@ class Backend {
this._logErrorLevel = null; this._logErrorLevel = null;
this._messageHandlers = new Map([ this._messageHandlers = new Map([
['yomichanCoreReady', {handler: this._onApiYomichanCoreReady.bind(this), async: false}], ['yomichanCoreReady', {async: false, contentScript: true, handler: this._onApiYomichanCoreReady.bind(this)}],
['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: false}], ['optionsSchemaGet', {async: false, contentScript: true, handler: this._onApiOptionsSchemaGet.bind(this)}],
['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: false}], ['optionsGet', {async: false, contentScript: true, handler: this._onApiOptionsGet.bind(this)}],
['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: false}], ['optionsGetFull', {async: false, contentScript: true, handler: this._onApiOptionsGetFull.bind(this)}],
['optionsSave', {handler: this._onApiOptionsSave.bind(this), async: true}], ['optionsSave', {async: true, contentScript: true, handler: this._onApiOptionsSave.bind(this)}],
['kanjiFind', {handler: this._onApiKanjiFind.bind(this), async: true}], ['kanjiFind', {async: true, contentScript: true, handler: this._onApiKanjiFind.bind(this)}],
['termsFind', {handler: this._onApiTermsFind.bind(this), async: true}], ['termsFind', {async: true, contentScript: true, handler: this._onApiTermsFind.bind(this)}],
['textParse', {handler: this._onApiTextParse.bind(this), async: true}], ['textParse', {async: true, contentScript: true, handler: this._onApiTextParse.bind(this)}],
['definitionAdd', {handler: this._onApiDefinitionAdd.bind(this), async: true}], ['definitionAdd', {async: true, contentScript: true, handler: this._onApiDefinitionAdd.bind(this)}],
['definitionsAddable', {handler: this._onApiDefinitionsAddable.bind(this), async: true}], ['definitionsAddable', {async: true, contentScript: true, handler: this._onApiDefinitionsAddable.bind(this)}],
['noteView', {handler: this._onApiNoteView.bind(this), async: true}], ['noteView', {async: true, contentScript: true, handler: this._onApiNoteView.bind(this)}],
['templateRender', {handler: this._onApiTemplateRender.bind(this), async: true}], ['templateRender', {async: true, contentScript: true, handler: this._onApiTemplateRender.bind(this)}],
['commandExec', {handler: this._onApiCommandExec.bind(this), async: false}], ['commandExec', {async: false, contentScript: true, handler: this._onApiCommandExec.bind(this)}],
['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}], ['audioGetUri', {async: true, contentScript: true, handler: this._onApiAudioGetUri.bind(this)}],
['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}], ['screenshotGet', {async: true, contentScript: true, handler: this._onApiScreenshotGet.bind(this)}],
['sendMessageToFrame', {handler: this._onApiSendMessageToFrame.bind(this), async: false}], ['sendMessageToFrame', {async: false, contentScript: true, handler: this._onApiSendMessageToFrame.bind(this)}],
['broadcastTab', {handler: this._onApiBroadcastTab.bind(this), async: false}], ['broadcastTab', {async: false, contentScript: true, handler: this._onApiBroadcastTab.bind(this)}],
['frameInformationGet', {handler: this._onApiFrameInformationGet.bind(this), async: true}], ['frameInformationGet', {async: true, contentScript: true, handler: this._onApiFrameInformationGet.bind(this)}],
['injectStylesheet', {handler: this._onApiInjectStylesheet.bind(this), async: true}], ['injectStylesheet', {async: true, contentScript: true, handler: this._onApiInjectStylesheet.bind(this)}],
['getEnvironmentInfo', {handler: this._onApiGetEnvironmentInfo.bind(this), async: true}], ['getEnvironmentInfo', {async: true, contentScript: true, handler: this._onApiGetEnvironmentInfo.bind(this)}],
['clipboardGet', {handler: this._onApiClipboardGet.bind(this), async: true}], ['clipboardGet', {async: true, contentScript: true, handler: this._onApiClipboardGet.bind(this)}],
['getDisplayTemplatesHtml', {handler: this._onApiGetDisplayTemplatesHtml.bind(this), async: true}], ['getDisplayTemplatesHtml', {async: true, contentScript: true, handler: this._onApiGetDisplayTemplatesHtml.bind(this)}],
['getQueryParserTemplatesHtml', {handler: this._onApiGetQueryParserTemplatesHtml.bind(this), async: true}], ['getQueryParserTemplatesHtml', {async: true, contentScript: true, handler: this._onApiGetQueryParserTemplatesHtml.bind(this)}],
['getZoom', {handler: this._onApiGetZoom.bind(this), async: true}], ['getZoom', {async: true, contentScript: true, handler: this._onApiGetZoom.bind(this)}],
['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: false}], ['getDefaultAnkiFieldTemplates', {async: false, contentScript: true, handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this)}],
['getAnkiDeckNames', {handler: this._onApiGetAnkiDeckNames.bind(this), async: true}], ['getAnkiDeckNames', {async: true, contentScript: false, handler: this._onApiGetAnkiDeckNames.bind(this)}],
['getAnkiModelNames', {handler: this._onApiGetAnkiModelNames.bind(this), async: true}], ['getAnkiModelNames', {async: true, contentScript: false, handler: this._onApiGetAnkiModelNames.bind(this)}],
['getAnkiModelFieldNames', {handler: this._onApiGetAnkiModelFieldNames.bind(this), async: true}], ['getAnkiModelFieldNames', {async: true, contentScript: false, handler: this._onApiGetAnkiModelFieldNames.bind(this)}],
['getDictionaryInfo', {handler: this._onApiGetDictionaryInfo.bind(this), async: true}], ['getDictionaryInfo', {async: true, contentScript: false, handler: this._onApiGetDictionaryInfo.bind(this)}],
['getDictionaryCounts', {handler: this._onApiGetDictionaryCounts.bind(this), async: true}], ['getDictionaryCounts', {async: true, contentScript: false, handler: this._onApiGetDictionaryCounts.bind(this)}],
['purgeDatabase', {handler: this._onApiPurgeDatabase.bind(this), async: true}], ['purgeDatabase', {async: true, contentScript: false, handler: this._onApiPurgeDatabase.bind(this)}],
['getMedia', {handler: this._onApiGetMedia.bind(this), async: true}], ['getMedia', {async: true, contentScript: true, handler: this._onApiGetMedia.bind(this)}],
['log', {handler: this._onApiLog.bind(this), async: false}], ['log', {async: false, contentScript: true, handler: this._onApiLog.bind(this)}],
['logIndicatorClear', {handler: this._onApiLogIndicatorClear.bind(this), async: false}], ['logIndicatorClear', {async: false, contentScript: true, handler: this._onApiLogIndicatorClear.bind(this)}],
['createActionPort', {handler: this._onApiCreateActionPort.bind(this), async: false}], ['createActionPort', {async: false, contentScript: true, handler: this._onApiCreateActionPort.bind(this)}],
['modifySettings', {handler: this._onApiModifySettings.bind(this), async: true}] ['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}]
]); ]);
this._messageHandlersWithProgress = new Map([ this._messageHandlersWithProgress = new Map([
['importDictionaryArchive', {handler: this._onApiImportDictionaryArchive.bind(this), async: true}], ['importDictionaryArchive', {async: true, contentScript: false, handler: this._onApiImportDictionaryArchive.bind(this)}],
['deleteDictionary', {handler: this._onApiDeleteDictionary.bind(this), async: true}] ['deleteDictionary', {async: true, contentScript: false, handler: this._onApiDeleteDictionary.bind(this)}]
]); ]);
this._commandHandlers = new Map([ this._commandHandlers = new Map([
['search', this._onCommandSearch.bind(this)], ['search', this._onCommandSearch.bind(this)],
['help', this._onCommandHelp.bind(this)], ['help', this._onCommandHelp.bind(this)],
['options', this._onCommandOptions.bind(this)], ['options', this._onCommandOptions.bind(this)],
['toggle', this._onCommandToggle.bind(this)] ['toggle', this._onCommandToggle.bind(this)]
]); ]);
} }
@ -206,9 +206,13 @@ class Backend {
const messageHandler = this._messageHandlers.get(action); const messageHandler = this._messageHandlers.get(action);
if (typeof messageHandler === 'undefined') { return false; } if (typeof messageHandler === 'undefined') { return false; }
const {handler, async} = messageHandler; const {handler, async, contentScript} = messageHandler;
try { try {
if (!contentScript) {
this._validatePrivilegedMessageSender(sender);
}
const promiseOrResult = handler(params, sender); const promiseOrResult = handler(params, sender);
if (async) { if (async) {
promiseOrResult.then( promiseOrResult.then(
@ -710,33 +714,27 @@ class Backend {
return this.defaultAnkiFieldTemplates; return this.defaultAnkiFieldTemplates;
} }
async _onApiGetAnkiDeckNames(params, sender) { async _onApiGetAnkiDeckNames() {
this._validatePrivilegedMessageSender(sender);
return await this.anki.getDeckNames(); return await this.anki.getDeckNames();
} }
async _onApiGetAnkiModelNames(params, sender) { async _onApiGetAnkiModelNames() {
this._validatePrivilegedMessageSender(sender);
return await this.anki.getModelNames(); return await this.anki.getModelNames();
} }
async _onApiGetAnkiModelFieldNames({modelName}, sender) { async _onApiGetAnkiModelFieldNames({modelName}) {
this._validatePrivilegedMessageSender(sender);
return await this.anki.getModelFieldNames(modelName); return await this.anki.getModelFieldNames(modelName);
} }
async _onApiGetDictionaryInfo(params, sender) { async _onApiGetDictionaryInfo() {
this._validatePrivilegedMessageSender(sender);
return await this.translator.database.getDictionaryInfo(); return await this.translator.database.getDictionaryInfo();
} }
async _onApiGetDictionaryCounts({dictionaryNames, getTotal}, sender) { async _onApiGetDictionaryCounts({dictionaryNames, getTotal}) {
this._validatePrivilegedMessageSender(sender);
return await this.translator.database.getDictionaryCounts(dictionaryNames, getTotal); return await this.translator.database.getDictionaryCounts(dictionaryNames, getTotal);
} }
async _onApiPurgeDatabase(params, sender) { async _onApiPurgeDatabase() {
this._validatePrivilegedMessageSender(sender);
this.translator.clearDatabaseCaches(); this.translator.clearDatabaseCaches();
await this.database.purge(); await this.database.purge();
} }
@ -782,12 +780,10 @@ class Backend {
} }
async _onApiImportDictionaryArchive({archiveContent, details}, sender, onProgress) { async _onApiImportDictionaryArchive({archiveContent, details}, sender, onProgress) {
this._validatePrivilegedMessageSender(sender);
return await this.dictionaryImporter.import(this.database, archiveContent, details, onProgress); return await this.dictionaryImporter.import(this.database, archiveContent, details, onProgress);
} }
async _onApiDeleteDictionary({dictionaryName}, sender, onProgress) { async _onApiDeleteDictionary({dictionaryName}, sender, onProgress) {
this._validatePrivilegedMessageSender(sender);
this.translator.clearDatabaseCaches(); this.translator.clearDatabaseCaches();
await this.database.deleteDictionary(dictionaryName, {rate: 1000}, onProgress); await this.database.deleteDictionary(dictionaryName, {rate: 1000}, onProgress);
} }
@ -832,7 +828,11 @@ class Backend {
if (typeof messageHandler === 'undefined') { if (typeof messageHandler === 'undefined') {
throw new Error('Invalid action'); throw new Error('Invalid action');
} }
const {handler, async} = messageHandler; const {handler, async, contentScript} = messageHandler;
if (!contentScript) {
this._validatePrivilegedMessageSender(sender);
}
const promiseOrResult = handler(params, sender, onProgress); const promiseOrResult = handler(params, sender, onProgress);
const result = async ? await promiseOrResult : promiseOrResult; const result = async ? await promiseOrResult : promiseOrResult;