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 | f190585 | 2024-02-28 22:28:36 +0100 | [diff] [blame] | 4 | import {getSemVerExtVersion} 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(); |
Adrià Vilanova Martínez | f190585 | 2024-02-28 22:28:36 +0100 | [diff] [blame] | 54 | let currentVersion = getSemVerExtVersion(); |
| 55 | if (currentVersion === '0' || currentVersion === null) { |
| 56 | currentVersion = '0.0.0'; |
| 57 | } |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 58 | |
| 59 | let forceDisabledFeaturesSet = new Set(); |
| 60 | for (let killSwitch of killSwitches) { |
| 61 | // If it isn't active, this kill switch is not applicable. |
| 62 | if (!killSwitch.getActive()) continue; |
| 63 | |
| 64 | // If min_version is set and is greater than the current version, |
| 65 | // this kill switch is not applicable. |
| 66 | if (killSwitch.getMinVersion() != '' && |
| 67 | compareLoose(killSwitch.getMinVersion(), currentVersion) == 1) |
| 68 | continue; |
| 69 | |
| 70 | // If max_version is set and is less than the current version, this |
| 71 | // kill switch is not applicable. |
| 72 | if (killSwitch.getMaxVersion() != '' && |
| 73 | compareLoose(killSwitch.getMaxVersion(), currentVersion) == -1) |
| 74 | continue; |
| 75 | |
| 76 | let browsers = killSwitch.getBrowsersList(); |
| 77 | let currentBrowser = this.getCurrentBrowser(); |
| 78 | |
| 79 | // If this browser isn't included as part of the kill switch, the |
| 80 | // kill switch is not applicable. |
| 81 | if (!browsers.includes(currentBrowser)) continue; |
| 82 | |
| 83 | console.warn( |
| 84 | 'Kill switch ' + killSwitch.getId() + ' will be applied!'); |
| 85 | |
| 86 | let featureCodename = killSwitch.getFeature()?.getCodename?.(); |
| 87 | if (featureCodename) forceDisabledFeaturesSet.add(featureCodename); |
| 88 | } |
| 89 | |
| 90 | let forceDisabledFeatures = Array.from(forceDisabledFeaturesSet); |
| 91 | |
| 92 | chrome.storage.sync.set( |
| 93 | {_forceDisabledFeatures: forceDisabledFeatures}, () => { |
Adrià Vilanova Martínez | 0511335 | 2023-07-21 20:22:50 +0200 | [diff] [blame] | 94 | let anyKillSwitchEnabled = forceDisabledFeatures.length > 0; |
| 95 | this.setBadge(anyKillSwitchEnabled); |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 96 | }); |
| 97 | }) |
| 98 | .catch(err => { |
| 99 | console.error( |
| 100 | '[killSwitch] Can\'t retrieve kill switch status: ', err); |
| 101 | }); |
| 102 | } |
Adrià Vilanova Martínez | 0511335 | 2023-07-21 20:22:50 +0200 | [diff] [blame] | 103 | } |