aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-02-08 19:40:49 -0500
committerGitHub <noreply@github.com>2021-02-08 19:40:49 -0500
commit0f5fb804d03041b58813516721d897c4315dca8b (patch)
tree40d637ea5e696b0da5f9d083d7069c64ad23717f /ext/bg/js/backend.js
parent0ed24347ee275250bcd2c044a1b664f9f0273653 (diff)
Mecab test (#1361)
* Update Mecab API * Add testMecab API function * Add a test button for the Mecab component
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js50
1 files changed, 49 insertions, 1 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 67b17cc9..f1983cb3 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -122,7 +122,8 @@ class Backend {
['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)}]
+ ['triggerDatabaseUpdated', {async: false, contentScript: true, handler: this._onApiTriggerDatabaseUpdated.bind(this)}],
+ ['testMecab', {async: true, contentScript: true, handler: this._onApiTestMecab.bind(this)}]
]);
this._messageHandlersWithProgress = new Map([
]);
@@ -676,6 +677,42 @@ class Backend {
this._triggerDatabaseUpdated(type, cause);
}
+ async _onApiTestMecab() {
+ if (!this._mecab.isEnabled()) {
+ throw new Error('MeCab not enabled');
+ }
+
+ let permissionsOkay = false;
+ try {
+ permissionsOkay = await this._hasPermissions({permissions: ['nativeMessaging']});
+ } catch (e) {
+ // NOP
+ }
+ if (!permissionsOkay) {
+ throw new Error('Insufficient permissions');
+ }
+
+ const disconnect = !this._mecab.isConnected();
+ try {
+ const version = await this._mecab.getVersion();
+ if (version === null) {
+ throw new Error('Could not connect to native MeCab component');
+ }
+
+ const localVersion = this._mecab.getLocalVersion();
+ if (version !== localVersion) {
+ throw new Error(`MeCab component version not supported: ${version}`);
+ }
+ } finally {
+ // Disconnect if the connection was previously disconnected
+ if (disconnect && this._mecab.isEnabled() && this._mecab.isActive()) {
+ this._mecab.disconnect();
+ }
+ }
+
+ return true;
+ }
+
// Command handlers
async _onCommandOpenSearchPage(params) {
@@ -1904,4 +1941,15 @@ class Backend {
});
});
}
+
+ _hasPermissions(permissions) {
+ return new Promise((resolve, reject) => chrome.permissions.contains(permissions, (result) => {
+ const e = chrome.runtime.lastError;
+ if (e) {
+ reject(new Error(e.message));
+ } else {
+ resolve(result);
+ }
+ }));
+ }
}