blob: 6be65898d4aefbb3631c9f77f1cc3b40830972d6 [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ínezf1905852024-02-28 22:28:36 +01004import {getSemVerExtVersion} 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();
Adrià Vilanova Martínezf1905852024-02-28 22:28:36 +010054 let currentVersion = getSemVerExtVersion();
55 if (currentVersion === '0' || currentVersion === null) {
56 currentVersion = '0.0.0';
57 }
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020058
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ínez05113352023-07-21 20:22:50 +020094 let anyKillSwitchEnabled = forceDisabledFeatures.length > 0;
95 this.setBadge(anyKillSwitchEnabled);
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020096 });
97 })
98 .catch(err => {
99 console.error(
100 '[killSwitch] Can\'t retrieve kill switch status: ', err);
101 });
102 }
Adrià Vilanova Martínez05113352023-07-21 20:22:50 +0200103}