mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-14 12:11:43 +01:00
214 lines
6.9 KiB
TypeScript
214 lines
6.9 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
import { timeout } from 'vs/base/common/async';
|
|
import { CancellationToken } from 'vs/base/common/cancellation';
|
|
import { Emitter, Event } from 'vs/base/common/event';
|
|
import { getMigratedSettingValue, IConfigurationService } from 'vs/platform/configuration/common/configuration';
|
|
import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
|
|
import { ILifecycleMainService } from 'vs/platform/lifecycle/electron-main/lifecycleMainService';
|
|
import { ILogService } from 'vs/platform/log/common/log';
|
|
import { IProductService } from 'vs/platform/product/common/productService';
|
|
import { IRequestService } from 'vs/platform/request/common/request';
|
|
import { AvailableForDownload, IUpdateService, State, StateType, UpdateType } from 'vs/platform/update/common/update';
|
|
|
|
export function createUpdateURL(platform: string, quality: string, productService: IProductService): string {
|
|
return `${productService.updateUrl}/api/update/${platform}/${quality}/${productService.commit}`;
|
|
}
|
|
|
|
export type UpdateNotAvailableClassification = {
|
|
owner: 'joaomoreno';
|
|
explicit: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; isMeasurement: true };
|
|
};
|
|
|
|
export abstract class AbstractUpdateService implements IUpdateService {
|
|
|
|
declare readonly _serviceBrand: undefined;
|
|
|
|
protected url: string | undefined;
|
|
|
|
private _state: State = State.Uninitialized;
|
|
|
|
private readonly _onStateChange = new Emitter<State>();
|
|
readonly onStateChange: Event<State> = this._onStateChange.event;
|
|
|
|
get state(): State {
|
|
return this._state;
|
|
}
|
|
|
|
protected setState(state: State): void {
|
|
this.logService.info('update#setState', state.type);
|
|
this._state = state;
|
|
this._onStateChange.fire(state);
|
|
}
|
|
|
|
constructor(
|
|
@ILifecycleMainService private readonly lifecycleMainService: ILifecycleMainService,
|
|
@IConfigurationService protected configurationService: IConfigurationService,
|
|
@IEnvironmentMainService private readonly environmentMainService: IEnvironmentMainService,
|
|
@IRequestService protected requestService: IRequestService,
|
|
@ILogService protected logService: ILogService,
|
|
@IProductService protected readonly productService: IProductService
|
|
) { }
|
|
|
|
/**
|
|
* This must be called before any other call. This is a performance
|
|
* optimization, to avoid using extra CPU cycles before first window open.
|
|
* https://github.com/microsoft/vscode/issues/89784
|
|
*/
|
|
initialize(): void {
|
|
if (!this.environmentMainService.isBuilt) {
|
|
return; // updates are never enabled when running out of sources
|
|
}
|
|
|
|
if (this.environmentMainService.disableUpdates) {
|
|
this.logService.info('update#ctor - updates are disabled by the environment');
|
|
return;
|
|
}
|
|
|
|
if (!this.productService.updateUrl || !this.productService.commit) {
|
|
this.logService.info('update#ctor - updates are disabled as there is no update URL');
|
|
return;
|
|
}
|
|
|
|
const updateMode = this.getUpdateMode();
|
|
const quality = this.getProductQuality(updateMode);
|
|
|
|
if (!quality) {
|
|
this.logService.info('update#ctor - updates are disabled by user preference');
|
|
return;
|
|
}
|
|
|
|
this.url = this.buildUpdateFeedUrl(quality);
|
|
if (!this.url) {
|
|
this.logService.info('update#ctor - updates are disabled as the update URL is badly formed');
|
|
return;
|
|
}
|
|
|
|
this.setState(State.Idle(this.getUpdateType()));
|
|
|
|
if (updateMode === 'manual') {
|
|
this.logService.info('update#ctor - manual checks only; automatic updates are disabled by user preference');
|
|
return;
|
|
}
|
|
|
|
if (updateMode === 'start') {
|
|
this.logService.info('update#ctor - startup checks only; automatic updates are disabled by user preference');
|
|
|
|
// Check for updates only once after 30 seconds
|
|
setTimeout(() => this.checkForUpdates(false), 30 * 1000);
|
|
} else {
|
|
// Start checking for updates after 30 seconds
|
|
this.scheduleCheckForUpdates(30 * 1000).then(undefined, err => this.logService.error(err));
|
|
}
|
|
}
|
|
|
|
protected getUpdateMode(): 'none' | 'manual' | 'start' | 'default' {
|
|
return getMigratedSettingValue<'none' | 'manual' | 'start' | 'default'>(this.configurationService, 'update.mode', 'update.channel');
|
|
}
|
|
|
|
private getProductQuality(updateMode: string): string | undefined {
|
|
return updateMode === 'none' ? undefined : this.productService.quality;
|
|
}
|
|
|
|
private scheduleCheckForUpdates(delay = 60 * 60 * 1000): Promise<void> {
|
|
return timeout(delay)
|
|
.then(() => this.checkForUpdates(false))
|
|
.then(() => {
|
|
// Check again after 1 hour
|
|
return this.scheduleCheckForUpdates(60 * 60 * 1000);
|
|
});
|
|
}
|
|
|
|
async checkForUpdates(explicit: boolean): Promise<void> {
|
|
this.logService.trace('update#checkForUpdates, state = ', this.state.type);
|
|
|
|
if (this.state.type !== StateType.Idle) {
|
|
return;
|
|
}
|
|
|
|
this.doCheckForUpdates(explicit);
|
|
}
|
|
|
|
async downloadUpdate(): Promise<void> {
|
|
this.logService.trace('update#downloadUpdate, state = ', this.state.type);
|
|
|
|
if (this.state.type !== StateType.AvailableForDownload) {
|
|
return;
|
|
}
|
|
|
|
await this.doDownloadUpdate(this.state);
|
|
}
|
|
|
|
protected async doDownloadUpdate(state: AvailableForDownload): Promise<void> {
|
|
// noop
|
|
}
|
|
|
|
async applyUpdate(): Promise<void> {
|
|
this.logService.trace('update#applyUpdate, state = ', this.state.type);
|
|
|
|
if (this.state.type !== StateType.Downloaded) {
|
|
return;
|
|
}
|
|
|
|
await this.doApplyUpdate();
|
|
}
|
|
|
|
protected async doApplyUpdate(): Promise<void> {
|
|
// noop
|
|
}
|
|
|
|
quitAndInstall(): Promise<void> {
|
|
this.logService.trace('update#quitAndInstall, state = ', this.state.type);
|
|
|
|
if (this.state.type !== StateType.Ready) {
|
|
return Promise.resolve(undefined);
|
|
}
|
|
|
|
this.logService.trace('update#quitAndInstall(): before lifecycle quit()');
|
|
|
|
this.lifecycleMainService.quit(true /* will restart */).then(vetod => {
|
|
this.logService.trace(`update#quitAndInstall(): after lifecycle quit() with veto: ${vetod}`);
|
|
if (vetod) {
|
|
return;
|
|
}
|
|
|
|
this.logService.trace('update#quitAndInstall(): running raw#quitAndInstall()');
|
|
this.doQuitAndInstall();
|
|
});
|
|
|
|
return Promise.resolve(undefined);
|
|
}
|
|
|
|
async isLatestVersion(): Promise<boolean | undefined> {
|
|
if (!this.url) {
|
|
return undefined;
|
|
}
|
|
|
|
const mode = await this.getUpdateMode();
|
|
|
|
if (mode === 'none') {
|
|
return false;
|
|
}
|
|
|
|
const context = await this.requestService.request({ url: this.url }, CancellationToken.None);
|
|
|
|
// The update server replies with 204 (No Content) when no
|
|
// update is available - that's all we want to know.
|
|
return context.res.statusCode === 204;
|
|
}
|
|
|
|
protected getUpdateType(): UpdateType {
|
|
return UpdateType.Archive;
|
|
}
|
|
|
|
protected doQuitAndInstall(): void {
|
|
// noop
|
|
}
|
|
|
|
protected abstract buildUpdateFeedUrl(quality: string): string | undefined;
|
|
protected abstract doCheckForUpdates(context: any): void;
|
|
}
|