Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/emergencyAlert/electron-browser/emergencyAlert.contribution.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IWorkbenchContribution, registerWorkbenchContribution2, WorkbenchPhase } from '../../../common/contributions.js';
7
import { IBannerService } from '../../../services/banner/browser/bannerService.js';
8
import { asJson, IRequestService } from '../../../../platform/request/common/request.js';
9
import { IProductService } from '../../../../platform/product/common/productService.js';
10
import { CancellationToken } from '../../../../base/common/cancellation.js';
11
import { ILogService } from '../../../../platform/log/common/log.js';
12
import { Codicon } from '../../../../base/common/codicons.js';
13
import { arch, platform } from '../../../../base/common/process.js';
14
15
interface IEmergencyAlert {
16
readonly commit: string;
17
readonly platform?: string;
18
readonly arch?: string;
19
readonly message: string;
20
readonly actions?: [{
21
readonly label: string;
22
readonly href: string;
23
}];
24
}
25
26
interface IEmergencyAlerts {
27
readonly alerts: IEmergencyAlert[];
28
}
29
30
export class EmergencyAlert implements IWorkbenchContribution {
31
32
static readonly ID = 'workbench.contrib.emergencyAlert';
33
34
constructor(
35
@IBannerService private readonly bannerService: IBannerService,
36
@IRequestService private readonly requestService: IRequestService,
37
@IProductService private readonly productService: IProductService,
38
@ILogService private readonly logService: ILogService
39
) {
40
if (productService.quality !== 'insider') {
41
return; // only enabled in insiders for now
42
}
43
44
const emergencyAlertUrl = productService.emergencyAlertUrl;
45
if (!emergencyAlertUrl) {
46
return; // no emergency alert configured
47
}
48
49
this.fetchAlerts(emergencyAlertUrl);
50
}
51
52
private async fetchAlerts(url: string): Promise<void> {
53
try {
54
await this.doFetchAlerts(url);
55
} catch (e) {
56
this.logService.error(e);
57
}
58
}
59
60
private async doFetchAlerts(url: string): Promise<void> {
61
const requestResult = await this.requestService.request({ type: 'GET', url, disableCache: true }, CancellationToken.None);
62
63
if (requestResult.res.statusCode !== 200) {
64
throw new Error(`Failed to fetch emergency alerts: HTTP ${requestResult.res.statusCode}`);
65
}
66
67
const emergencyAlerts = await asJson<IEmergencyAlerts>(requestResult);
68
if (!emergencyAlerts) {
69
return;
70
}
71
72
for (const emergencyAlert of emergencyAlerts.alerts) {
73
if (
74
(emergencyAlert.commit !== this.productService.commit) || // version mismatch
75
(emergencyAlert.platform && emergencyAlert.platform !== platform) || // platform mismatch
76
(emergencyAlert.arch && emergencyAlert.arch !== arch) // arch mismatch
77
) {
78
return;
79
}
80
81
this.bannerService.show({
82
id: 'emergencyAlert.banner',
83
icon: Codicon.warning,
84
message: emergencyAlert.message,
85
actions: emergencyAlert.actions
86
});
87
88
break;
89
}
90
}
91
}
92
93
registerWorkbenchContribution2('workbench.emergencyAlert', EmergencyAlert, WorkbenchPhase.Eventually);
94
95