aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/permissions-toggle-controller.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-02-08 17:52:41 -0500
committerGitHub <noreply@github.com>2021-02-08 17:52:41 -0500
commit849e4fabe1dffc2851fcb338dae8400d6c8e46ca (patch)
treee49dbd86ec20a2a11fc8938b8f6a4e1a9c57501e /ext/bg/js/settings/permissions-toggle-controller.js
parent008809e0e73665249b7b3dd7c14a1761aa06bd39 (diff)
Native messaging optional permission (#1348)
* Refactor PermissionsToggleController to not require a setting * Update nativeMessaging to be optional on Chrome * Update parsing.enableMecabParser setting to request permissions * Update permissions page to use PermissionsToggleController * Update permissions documentation * Disable toggle for permissions which are not optional
Diffstat (limited to 'ext/bg/js/settings/permissions-toggle-controller.js')
-rw-r--r--ext/bg/js/settings/permissions-toggle-controller.js42
1 files changed, 32 insertions, 10 deletions
diff --git a/ext/bg/js/settings/permissions-toggle-controller.js b/ext/bg/js/settings/permissions-toggle-controller.js
index 07db1cf8..2e58ec67 100644
--- a/ext/bg/js/settings/permissions-toggle-controller.js
+++ b/ext/bg/js/settings/permissions-toggle-controller.js
@@ -41,9 +41,16 @@ class PermissionsToggleController {
// Private
_onOptionsChanged({options}) {
- const accessor = new ObjectPropertyAccessor(options);
+ let accessor = null;
for (const toggle of this._toggles) {
- const path = ObjectPropertyAccessor.getPathArray(toggle.dataset.permissionsSetting);
+ const {permissionsSetting} = toggle.dataset;
+ if (typeof permissionsSetting !== 'string') { continue; }
+
+ if (accessor === null) {
+ accessor = new ObjectPropertyAccessor(options);
+ }
+
+ const path = ObjectPropertyAccessor.getPathArray(permissionsSetting);
let value;
try {
value = accessor.get(path, path.length);
@@ -58,23 +65,38 @@ class PermissionsToggleController {
async _onPermissionsToggleChange(e) {
const toggle = e.currentTarget;
let value = toggle.checked;
+ const valuePre = !value;
+ const {permissionsSetting} = toggle.dataset;
+ const hasPermissionsSetting = typeof permissionsSetting === 'string';
- if (value) {
- toggle.checked = false;
- value = await this._settingsController.setPermissionsGranted(this._getRequiredPermissions(toggle), true);
+ if (value || !hasPermissionsSetting) {
+ toggle.checked = valuePre;
+ try {
+ value = await this._settingsController.setPermissionsGranted(this._getRequiredPermissions(toggle), value);
+ } catch (error) {
+ value = valuePre;
+ }
toggle.checked = value;
}
- this._setToggleValid(toggle, true);
-
- await this._settingsController.setProfileSetting(toggle.dataset.permissionsSetting, value);
+ if (hasPermissionsSetting) {
+ this._setToggleValid(toggle, true);
+ await this._settingsController.setProfileSetting(permissionsSetting, value);
+ }
}
_onPermissionsChanged({permissions: {permissions}}) {
const permissionsSet = new Set(permissions);
for (const toggle of this._toggles) {
- const valid = !toggle.checked || this._hasAll(permissionsSet, this._getRequiredPermissions(toggle));
- this._setToggleValid(toggle, valid);
+ const {permissionsSetting} = toggle.dataset;
+ const hasPermissions = this._hasAll(permissionsSet, this._getRequiredPermissions(toggle));
+
+ if (typeof permissionsSetting === 'string') {
+ const valid = !toggle.checked || hasPermissions;
+ this._setToggleValid(toggle, valid);
+ } else {
+ toggle.checked = hasPermissions;
+ }
}
}