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