Adrià Vilanova MartÃnez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 1 | import {compareLoose} from 'semver'; |
| 2 | |
| 3 | import {getExtVersion, isFirefox} from '../common/extUtils.js'; |
| 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() { |
| 19 | if (isFirefox()) return commonPb.Environment.Browser.BROWSER_GECKO; |
| 20 | return commonPb.Environment.Browser.BROWSER_CHROMIUM; |
| 21 | } |
| 22 | |
| 23 | updateKillSwitchStatus() { |
| 24 | let request = new ksPb.GetKillSwitchOverviewRequest(); |
| 25 | request.setWithNonactiveKillSwitches(false); |
| 26 | |
| 27 | this.client.getKillSwitchOverview(request) |
| 28 | .then(res => { |
| 29 | let killSwitches = res.getKillSwitchesList(); |
| 30 | let currentVersion = getExtVersion(); |
| 31 | if (currentVersion === '0') currentVersion = '0.0.0'; |
| 32 | |
| 33 | let forceDisabledFeaturesSet = new Set(); |
| 34 | for (let killSwitch of killSwitches) { |
| 35 | // If it isn't active, this kill switch is not applicable. |
| 36 | if (!killSwitch.getActive()) continue; |
| 37 | |
| 38 | // If min_version is set and is greater than the current version, |
| 39 | // this kill switch is not applicable. |
| 40 | if (killSwitch.getMinVersion() != '' && |
| 41 | compareLoose(killSwitch.getMinVersion(), currentVersion) == 1) |
| 42 | continue; |
| 43 | |
| 44 | // If max_version is set and is less than the current version, this |
| 45 | // kill switch is not applicable. |
| 46 | if (killSwitch.getMaxVersion() != '' && |
| 47 | compareLoose(killSwitch.getMaxVersion(), currentVersion) == -1) |
| 48 | continue; |
| 49 | |
| 50 | let browsers = killSwitch.getBrowsersList(); |
| 51 | let currentBrowser = this.getCurrentBrowser(); |
| 52 | |
| 53 | // If this browser isn't included as part of the kill switch, the |
| 54 | // kill switch is not applicable. |
| 55 | if (!browsers.includes(currentBrowser)) continue; |
| 56 | |
| 57 | console.warn( |
| 58 | 'Kill switch ' + killSwitch.getId() + ' will be applied!'); |
| 59 | |
| 60 | let featureCodename = killSwitch.getFeature()?.getCodename?.(); |
| 61 | if (featureCodename) forceDisabledFeaturesSet.add(featureCodename); |
| 62 | } |
| 63 | |
| 64 | let forceDisabledFeatures = Array.from(forceDisabledFeaturesSet); |
| 65 | |
| 66 | chrome.storage.sync.set( |
| 67 | {_forceDisabledFeatures: forceDisabledFeatures}, () => { |
| 68 | if (forceDisabledFeatures.length > 0) { |
| 69 | // TODO(avm99963): show a badge to warn that some features |
| 70 | // have been force disabled. |
| 71 | } |
| 72 | }); |
| 73 | }) |
| 74 | .catch(err => { |
| 75 | console.error( |
| 76 | '[killSwitch] Can\'t retrieve kill switch status: ', err); |
| 77 | }); |
| 78 | } |
| 79 | }; |