"Fossies" - the Fresh Open Source Software Archive

Member "vscode-1.49.1/src/vs/platform/update/electron-main/abstractUpdateService.ts" (16 Sep 2020, 6766 Bytes) of package /linux/misc/vscode-1.49.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 { Event, Emitter } from 'vs/base/common/event';
    7 import { timeout } from 'vs/base/common/async';
    8 import { IConfigurationService, getMigratedSettingValue } from 'vs/platform/configuration/common/configuration';
    9 import { ILifecycleMainService } from 'vs/platform/lifecycle/electron-main/lifecycleMainService';
   10 import product from 'vs/platform/product/common/product';
   11 import { IUpdateService, State, StateType, AvailableForDownload, UpdateType } from 'vs/platform/update/common/update';
   12 import { IEnvironmentService } from 'vs/platform/environment/common/environment';
   13 import { INativeEnvironmentService } from 'vs/platform/environment/node/environmentService';
   14 import { ILogService } from 'vs/platform/log/common/log';
   15 import { IRequestService } from 'vs/platform/request/common/request';
   16 import { CancellationToken } from 'vs/base/common/cancellation';
   17 
   18 export function createUpdateURL(platform: string, quality: string): string {
   19     return `${product.updateUrl}/api/update/${platform}/${quality}/${product.commit}`;
   20 }
   21 
   22 export type UpdateNotAvailableClassification = {
   23     explicit: { classification: 'SystemMetaData', purpose: 'FeatureInsight', isMeasurement: true };
   24 };
   25 
   26 export abstract class AbstractUpdateService implements IUpdateService {
   27 
   28     declare readonly _serviceBrand: undefined;
   29 
   30     protected url: string | undefined;
   31 
   32     private _state: State = State.Uninitialized;
   33 
   34     private readonly _onStateChange = new Emitter<State>();
   35     readonly onStateChange: Event<State> = this._onStateChange.event;
   36 
   37     get state(): State {
   38         return this._state;
   39     }
   40 
   41     protected setState(state: State): void {
   42         this.logService.info('update#setState', state.type);
   43         this._state = state;
   44         this._onStateChange.fire(state);
   45     }
   46 
   47     constructor(
   48         @ILifecycleMainService private readonly lifecycleMainService: ILifecycleMainService,
   49         @IConfigurationService protected configurationService: IConfigurationService,
   50         @IEnvironmentService private readonly environmentService: INativeEnvironmentService,
   51         @IRequestService protected requestService: IRequestService,
   52         @ILogService protected logService: ILogService,
   53     ) { }
   54 
   55     /**
   56      * This must be called before any other call. This is a performance
   57      * optimization, to avoid using extra CPU cycles before first window open.
   58      * https://github.com/microsoft/vscode/issues/89784
   59      */
   60     initialize(): void {
   61         if (!this.environmentService.isBuilt) {
   62             return; // updates are never enabled when running out of sources
   63         }
   64 
   65         if (this.environmentService.disableUpdates) {
   66             this.logService.info('update#ctor - updates are disabled by the environment');
   67             return;
   68         }
   69 
   70         if (!product.updateUrl || !product.commit) {
   71             this.logService.info('update#ctor - updates are disabled as there is no update URL');
   72             return;
   73         }
   74 
   75         const updateMode = getMigratedSettingValue<string>(this.configurationService, 'update.mode', 'update.channel');
   76         const quality = this.getProductQuality(updateMode);
   77 
   78         if (!quality) {
   79             this.logService.info('update#ctor - updates are disabled by user preference');
   80             return;
   81         }
   82 
   83         this.url = this.buildUpdateFeedUrl(quality);
   84         if (!this.url) {
   85             this.logService.info('update#ctor - updates are disabled as the update URL is badly formed');
   86             return;
   87         }
   88 
   89         this.setState(State.Idle(this.getUpdateType()));
   90 
   91         if (updateMode === 'manual') {
   92             this.logService.info('update#ctor - manual checks only; automatic updates are disabled by user preference');
   93             return;
   94         }
   95 
   96         if (updateMode === 'start') {
   97             this.logService.info('update#ctor - startup checks only; automatic updates are disabled by user preference');
   98 
   99             // Check for updates only once after 30 seconds
  100             setTimeout(() => this.checkForUpdates(null), 30 * 1000);
  101         } else {
  102             // Start checking for updates after 30 seconds
  103             this.scheduleCheckForUpdates(30 * 1000).then(undefined, err => this.logService.error(err));
  104         }
  105     }
  106 
  107     private getProductQuality(updateMode: string): string | undefined {
  108         return updateMode === 'none' ? undefined : product.quality;
  109     }
  110 
  111     private scheduleCheckForUpdates(delay = 60 * 60 * 1000): Promise<void> {
  112         return timeout(delay)
  113             .then(() => this.checkForUpdates(null))
  114             .then(() => {
  115                 // Check again after 1 hour
  116                 return this.scheduleCheckForUpdates(60 * 60 * 1000);
  117             });
  118     }
  119 
  120     async checkForUpdates(context: any): Promise<void> {
  121         this.logService.trace('update#checkForUpdates, state = ', this.state.type);
  122 
  123         if (this.state.type !== StateType.Idle) {
  124             return;
  125         }
  126 
  127         this.doCheckForUpdates(context);
  128     }
  129 
  130     async downloadUpdate(): Promise<void> {
  131         this.logService.trace('update#downloadUpdate, state = ', this.state.type);
  132 
  133         if (this.state.type !== StateType.AvailableForDownload) {
  134             return;
  135         }
  136 
  137         await this.doDownloadUpdate(this.state);
  138     }
  139 
  140     protected async doDownloadUpdate(state: AvailableForDownload): Promise<void> {
  141         // noop
  142     }
  143 
  144     async applyUpdate(): Promise<void> {
  145         this.logService.trace('update#applyUpdate, state = ', this.state.type);
  146 
  147         if (this.state.type !== StateType.Downloaded) {
  148             return;
  149         }
  150 
  151         await this.doApplyUpdate();
  152     }
  153 
  154     protected async doApplyUpdate(): Promise<void> {
  155         // noop
  156     }
  157 
  158     quitAndInstall(): Promise<void> {
  159         this.logService.trace('update#quitAndInstall, state = ', this.state.type);
  160 
  161         if (this.state.type !== StateType.Ready) {
  162             return Promise.resolve(undefined);
  163         }
  164 
  165         this.logService.trace('update#quitAndInstall(): before lifecycle quit()');
  166 
  167         this.lifecycleMainService.quit(true /* from update */).then(vetod => {
  168             this.logService.trace(`update#quitAndInstall(): after lifecycle quit() with veto: ${vetod}`);
  169             if (vetod) {
  170                 return;
  171             }
  172 
  173             this.logService.trace('update#quitAndInstall(): running raw#quitAndInstall()');
  174             this.doQuitAndInstall();
  175         });
  176 
  177         return Promise.resolve(undefined);
  178     }
  179 
  180     isLatestVersion(): Promise<boolean | undefined> {
  181         if (!this.url) {
  182             return Promise.resolve(undefined);
  183         }
  184 
  185         return this.requestService.request({ url: this.url }, CancellationToken.None).then(context => {
  186             // The update server replies with 204 (No Content) when no
  187             // update is available - that's all we want to know.
  188             if (context.res.statusCode === 204) {
  189                 return true;
  190             } else {
  191                 return false;
  192             }
  193         });
  194     }
  195 
  196     protected getUpdateType(): UpdateType {
  197         return UpdateType.Archive;
  198     }
  199 
  200     protected doQuitAndInstall(): void {
  201         // noop
  202     }
  203 
  204     protected abstract buildUpdateFeedUrl(quality: string): string | undefined;
  205     protected abstract doCheckForUpdates(context: any): void;
  206 }