abstractUpdateService.ts 5.7 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

M
Matt Bierner 已提交
8
import { Event, Emitter } from 'vs/base/common/event';
9
import { Throttler, timeout } from 'vs/base/common/async';
10 11 12 13
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ILifecycleService } from 'vs/platform/lifecycle/electron-main/lifecycleMain';
import product from 'vs/platform/node/product';
import { TPromise } from 'vs/base/common/winjs.base';
J
Joao Moreno 已提交
14
import { IUpdateService, State, StateType, AvailableForDownload, UpdateType } from 'vs/platform/update/common/update';
15 16
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { ILogService } from 'vs/platform/log/common/log';
17
import { IRequestService } from 'vs/platform/request/node/request';
18
import { CancellationToken } from 'vs/base/common/cancellation';
19 20 21 22 23 24 25 26 27

export function createUpdateURL(platform: string, quality: string): string {
	return `${product.updateUrl}/api/update/${platform}/${quality}/${product.commit}`;
}

export abstract class AbstractUpdateService implements IUpdateService {

	_serviceBrand: any;

28 29
	protected readonly url: string | undefined;

30 31 32 33 34 35 36 37 38 39 40
	private _state: State = State.Uninitialized;
	private throttler: Throttler = new Throttler();

	private _onStateChange = new Emitter<State>();
	get onStateChange(): Event<State> { return this._onStateChange.event; }

	get state(): State {
		return this._state;
	}

	protected setState(state: State): void {
41
		this.logService.info('update#setState', state.type);
42 43 44 45 46 47
		this._state = state;
		this._onStateChange.fire(state);
	}

	constructor(
		@ILifecycleService private lifecycleService: ILifecycleService,
J
Joao Moreno 已提交
48
		@IConfigurationService protected configurationService: IConfigurationService,
49
		@IEnvironmentService private environmentService: IEnvironmentService,
50 51
		@IRequestService protected requestService: IRequestService,
		@ILogService protected logService: ILogService,
52 53
	) {
		if (this.environmentService.disableUpdates) {
54
			this.logService.info('update#ctor - updates are disabled');
55 56 57 58
			return;
		}

		if (!product.updateUrl || !product.commit) {
59
			this.logService.info('update#ctor - updates are disabled');
60 61 62 63 64 65
			return;
		}

		const quality = this.getProductQuality();

		if (!quality) {
66
			this.logService.info('update#ctor - updates are disabled');
67 68 69
			return;
		}

70 71
		this.url = this.buildUpdateFeedUrl(quality);
		if (!this.url) {
72
			this.logService.info('update#ctor - updates are disabled');
73 74 75
			return;
		}

J
Joao Moreno 已提交
76
		this.setState(State.Idle(this.getUpdateType()));
77 78

		// Start checking for updates after 30 seconds
79
		this.scheduleCheckForUpdates(30 * 1000).then(null, err => this.logService.error(err));
80 81 82 83 84 85 86
	}

	private getProductQuality(): string {
		const quality = this.configurationService.getValue<string>('update.channel');
		return quality === 'none' ? null : product.quality;
	}

87 88
	private scheduleCheckForUpdates(delay = 60 * 60 * 1000): Thenable<void> {
		return timeout(delay)
J
Joao Moreno 已提交
89
			.then(() => this.checkForUpdates(null))
90
			.then(update => {
91 92
				// TODO: TS 3.1 upgrade. Why are we checking against void?
				if (update as any) {
93 94 95 96 97 98 99 100 101
					// Update found, no need to check more
					return TPromise.as(null);
				}

				// Check again after 1 hour
				return this.scheduleCheckForUpdates(60 * 60 * 1000);
			});
	}

J
Joao Moreno 已提交
102
	checkForUpdates(context: any): TPromise<void> {
103 104
		this.logService.trace('update#checkForUpdates, state = ', this.state.type);

J
Joao Moreno 已提交
105
		if (this.state.type !== StateType.Idle) {
106 107 108
			return TPromise.as(null);
		}

J
Joao Moreno 已提交
109
		return this.throttler.queue(() => TPromise.as(this.doCheckForUpdates(context)));
110 111
	}

112
	downloadUpdate(): TPromise<void> {
113 114
		this.logService.trace('update#downloadUpdate, state = ', this.state.type);

J
Joao Moreno 已提交
115
		if (this.state.type !== StateType.AvailableForDownload) {
116 117 118 119 120 121
			return TPromise.as(null);
		}

		return this.doDownloadUpdate(this.state);
	}

J
Joao Moreno 已提交
122
	protected doDownloadUpdate(state: AvailableForDownload): TPromise<void> {
123 124 125
		return TPromise.as(null);
	}

126
	applyUpdate(): TPromise<void> {
127 128
		this.logService.trace('update#applyUpdate, state = ', this.state.type);

J
Joao Moreno 已提交
129
		if (this.state.type !== StateType.Downloaded) {
130 131 132 133 134 135 136 137 138 139 140
			return TPromise.as(null);
		}

		return this.doApplyUpdate();
	}

	protected doApplyUpdate(): TPromise<void> {
		return TPromise.as(null);
	}

	quitAndInstall(): TPromise<void> {
141 142
		this.logService.trace('update#quitAndInstall, state = ', this.state.type);

143 144 145 146 147 148
		if (this.state.type !== StateType.Ready) {
			return TPromise.as(null);
		}

		this.logService.trace('update#quitAndInstall(): before lifecycle quit()');

149
		this.lifecycleService.quit(true /* from update */).then(vetod => {
150 151 152 153 154 155 156 157 158 159 160 161
			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 TPromise.as(null);
	}

162 163 164 165
	isLatestVersion(): TPromise<boolean | undefined> {
		if (!this.url) {
			return TPromise.as(undefined);
		}
166
		return this.requestService.request({ url: this.url }, CancellationToken.None).then(context => {
167 168 169 170 171 172 173 174 175 176
			// The update server replies with 204 (No Content) when no
			// update is available - that's all we want to know.
			if (context.res.statusCode === 204) {
				return true;
			} else {
				return false;
			}
		});
	}

J
Joao Moreno 已提交
177 178 179 180
	protected getUpdateType(): UpdateType {
		return UpdateType.Archive;
	}

181 182 183 184
	protected doQuitAndInstall(): void {
		// noop
	}

J
Johannes Rieken 已提交
185
	protected abstract buildUpdateFeedUrl(quality: string): string | undefined;
J
Joao Moreno 已提交
186
	protected abstract doCheckForUpdates(context: any): void;
187
}