Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 1 | import {compareLoose} from 'semver'; |
| 2 | |
Adrià Vilanova Martínez | 0511335 | 2023-07-21 20:22:50 +0200 | [diff] [blame] | 3 | import actionApi from '../common/actionApi.js'; |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 4 | import {getExtVersion} from '../common/extUtils.js'; |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 5 | |
| 6 | import * as commonPb from './api_proto/common_pb.js'; |
| 7 | import {KillSwitchServicePromiseClient} from './api_proto/kill_switch_grpc_web_pb.js'; |
| 8 | import * as ksPb from './api_proto/kill_switch_pb.js'; |
| 9 | |
| 10 | const host = |
| 11 | (PRODUCTION ? 'https://twpt-grpc-web.avm99963.com/' : |
| 12 | 'http://localhost:8081'); |
| 13 | |
Adrià Vilanova Martínez | 0511335 | 2023-07-21 20:22:50 +0200 | [diff] [blame] | 14 | const KILLSWITCH_BADGE_OPTIONS = { |
| 15 | 'iconTitleI18nKey': 'actionbadge_killswitch_enabled', |
| 16 | 'badgeText': '!', |
| 17 | 'bgColor': '#B71C1C', |
| 18 | }; |
| 19 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 20 | export default class KillSwitchMechanism { |
| 21 | constructor() { |
| 22 | this.client = new KillSwitchServicePromiseClient(host, null, null); |
| 23 | } |
| 24 | |
Adrià Vilanova Martínez | 0511335 | 2023-07-21 20:22:50 +0200 | [diff] [blame] | 25 | setBadge(anyKillSwitchEnabled) { |
| 26 | if (anyKillSwitchEnabled) { |
| 27 | actionApi.setBadgeBackgroundColor( |
| 28 | {color: KILLSWITCH_BADGE_OPTIONS.bgColor}); |
| 29 | actionApi.setBadgeText({text: KILLSWITCH_BADGE_OPTIONS.badgeText}); |
| 30 | let title = |
| 31 | chrome.i18n.getMessage(KILLSWITCH_BADGE_OPTIONS.iconTitleI18nKey); |
| 32 | actionApi.setTitle({title}); |
| 33 | } else { |
| 34 | actionApi.setBadgeText({text: ''}); |
| 35 | actionApi.setTitle({title: ''}); |
| 36 | } |
| 37 | } |
| 38 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 39 | getCurrentBrowser() { |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 40 | // #!if browser_target == 'gecko' |
| 41 | return commonPb.Environment.Browser.BROWSER_GECKO; |
| 42 | // #!else |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 43 | return commonPb.Environment.Browser.BROWSER_CHROMIUM; |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 44 | // #!endif |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | updateKillSwitchStatus() { |
| 48 | let request = new ksPb.GetKillSwitchOverviewRequest(); |
| 49 | request.setWithNonactiveKillSwitches(false); |
| 50 | |
| 51 | this.client.getKillSwitchOverview(request) |
| 52 | .then(res => { |
| 53 | let killSwitches = res.getKillSwitchesList(); |
| 54 | let currentVersion = getExtVersion(); |
| 55 | if (currentVersion === '0') currentVersion = '0.0.0'; |
| 56 | |
| 57 | let forceDisabledFeaturesSet = new Set(); |
| 58 | for (let killSwitch of killSwitches) { |
| 59 | // If it isn't active, this kill switch is not applicable. |
| 60 | if (!killSwitch.getActive()) continue; |
| 61 | |
| 62 | // If min_version is set and is greater than the current version, |
| 63 | // this kill switch is not applicable. |
| 64 | if (killSwitch.getMinVersion() != '' && |
| 65 | compareLoose(killSwitch.getMinVersion(), currentVersion) == 1) |
| 66 | continue; |
| 67 | |
| 68 | // If max_version is set and is less than the current version, this |
| 69 | // kill switch is not applicable. |
| 70 | if (killSwitch.getMaxVersion() != '' && |
| 71 | compareLoose(killSwitch.getMaxVersion(), currentVersion) == -1) |
| 72 | continue; |
| 73 | |
| 74 | let browsers = killSwitch.getBrowsersList(); |
| 75 | let currentBrowser = this.getCurrentBrowser(); |
| 76 | |
| 77 | // If this browser isn't included as part of the kill switch, the |
| 78 | // kill switch is not applicable. |
| 79 | if (!browsers.includes(currentBrowser)) continue; |
| 80 | |
| 81 | console.warn( |
| 82 | 'Kill switch ' + killSwitch.getId() + ' will be applied!'); |
| 83 | |
| 84 | let featureCodename = killSwitch.getFeature()?.getCodename?.(); |
| 85 | if (featureCodename) forceDisabledFeaturesSet.add(featureCodename); |
| 86 | } |
| 87 | |
| 88 | let forceDisabledFeatures = Array.from(forceDisabledFeaturesSet); |
| 89 | |
| 90 | chrome.storage.sync.set( |
| 91 | {_forceDisabledFeatures: forceDisabledFeatures}, () => { |
Adrià Vilanova Martínez | 0511335 | 2023-07-21 20:22:50 +0200 | [diff] [blame] | 92 | let anyKillSwitchEnabled = forceDisabledFeatures.length > 0; |
| 93 | this.setBadge(anyKillSwitchEnabled); |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 94 | }); |
| 95 | }) |
| 96 | .catch(err => { |
| 97 | console.error( |
| 98 | '[killSwitch] Can\'t retrieve kill switch status: ', err); |
| 99 | }); |
| 100 | } |
Adrià Vilanova Martínez | 0511335 | 2023-07-21 20:22:50 +0200 | [diff] [blame] | 101 | } |