blob: a1083b1dd447e20a2fcdedaab86b7b79bc17fc9e [file] [log] [blame]
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +02001import {compareLoose} from 'semver';
2
Adrià Vilanova Martínez05113352023-07-21 20:22:50 +02003import actionApi from '../common/actionApi.js';
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +01004import {getExtVersion} from '../common/extUtils.js';
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +02005
6import * as commonPb from './api_proto/common_pb.js';
7import {KillSwitchServicePromiseClient} from './api_proto/kill_switch_grpc_web_pb.js';
8import * as ksPb from './api_proto/kill_switch_pb.js';
9
10const host =
11 (PRODUCTION ? 'https://twpt-grpc-web.avm99963.com/' :
12 'http://localhost:8081');
13
Adrià Vilanova Martínez05113352023-07-21 20:22:50 +020014const KILLSWITCH_BADGE_OPTIONS = {
15 'iconTitleI18nKey': 'actionbadge_killswitch_enabled',
16 'badgeText': '!',
17 'bgColor': '#B71C1C',
18};
19
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020020export default class KillSwitchMechanism {
21 constructor() {
22 this.client = new KillSwitchServicePromiseClient(host, null, null);
23 }
24
Adrià Vilanova Martínez05113352023-07-21 20:22:50 +020025 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ínez413cb442021-09-06 00:30:45 +020039 getCurrentBrowser() {
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010040 // #!if browser_target == 'gecko'
41 return commonPb.Environment.Browser.BROWSER_GECKO;
42 // #!else
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020043 return commonPb.Environment.Browser.BROWSER_CHROMIUM;
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010044 // #!endif
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020045 }
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ínez05113352023-07-21 20:22:50 +020092 let anyKillSwitchEnabled = forceDisabledFeatures.length > 0;
93 this.setBadge(anyKillSwitchEnabled);
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020094 });
95 })
96 .catch(err => {
97 console.error(
98 '[killSwitch] Can\'t retrieve kill switch status: ', err);
99 });
100 }
Adrià Vilanova Martínez05113352023-07-21 20:22:50 +0200101}