electronTelemetryService.ts 3.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import getmac = require('getmac');
import crypto = require('crypto');

import {MainTelemetryService, TelemetryServiceConfig} from 'vs/platform/telemetry/browser/mainTelemetryService';
S
Sofian Hnaide 已提交
10
import {ITelemetryService, ITelemetryInfo} from 'vs/platform/telemetry/common/telemetry';
E
Erich Gamma 已提交
11 12 13 14 15 16 17 18 19 20 21
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
import errors = require('vs/base/common/errors');
import uuid = require('vs/base/common/uuid');

class StorageKeys {
		public static MachineId: string = 'telemetry.machineId';
		public static InstanceId: string = 'telemetry.instanceId';
}

export class ElectronTelemetryService extends MainTelemetryService implements ITelemetryService {

S
Sofian Hnaide 已提交
22 23
	private _setupIds: Promise<ITelemetryInfo>;

E
Erich Gamma 已提交
24 25 26
	constructor(@IStorageService private storageService: IStorageService, config?:TelemetryServiceConfig) {
		super(config);

S
Sofian Hnaide 已提交
27 28 29 30 31 32 33 34
		this._setupIds = this.setupIds();
	}

	/**
	 * override the base getTelemetryInfo to make sure this information is not retrieved before it's ready
	 */
	public getTelemetryInfo(): Promise<ITelemetryInfo> {
		return this._setupIds;
E
Erich Gamma 已提交
35 36
	}

S
Sofian Hnaide 已提交
37 38 39 40 41 42 43 44 45 46 47
	private setupIds(): Promise<ITelemetryInfo> {
		return Promise.all([this.setupInstanceId(), this.setupMachineId()]).then(()=> {
				return {
					machineId: this.machineId,
					instanceId: this.instanceId,
					sessionId: this.sessionId
				}
		});
	}

	private setupInstanceId(): Promise<string> {
E
Erich Gamma 已提交
48 49 50 51 52 53
		var instanceId = this.storageService.get(StorageKeys.InstanceId);
		if(!instanceId) {
			instanceId = uuid.generateUuid();
			this.storageService.store(StorageKeys.InstanceId, instanceId);
		}
		this.instanceId = instanceId;
S
Sofian Hnaide 已提交
54
		return Promise.resolve(this.instanceId);
E
Erich Gamma 已提交
55 56
	}

S
Sofian Hnaide 已提交
57
	private setupMachineId(): Promise<string> {
E
Erich Gamma 已提交
58 59 60
		var machineId = this.storageService.get(StorageKeys.MachineId);
		if (machineId) {
			this.machineId = machineId;
S
Sofian Hnaide 已提交
61
			return Promise.resolve(this.machineId);
E
Erich Gamma 已提交
62
		} else {
S
Sofian Hnaide 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
			return new Promise((resolve, reject) => {
				try {
					// add a unique machine id as a hash of the macAddress
					getmac.getMac((error, macAddress) => {
						if (!error) {
							// crypt machine id
							machineId = crypto.createHash('sha256').update(macAddress, 'utf8').digest('hex');
						} else {
							// generate a UUID
							machineId = uuid.generateUuid();
						}

						this.machineId = machineId;
						this.storageService.store(StorageKeys.MachineId, machineId);
						resolve(this.machineId);
					});
				} catch (err) {
					errors.onUnexpectedError(err);
E
Erich Gamma 已提交
81

S
Sofian Hnaide 已提交
82 83
					// generate a UUID
					machineId = uuid.generateUuid();
E
Erich Gamma 已提交
84 85
					this.machineId = machineId;
					this.storageService.store(StorageKeys.MachineId, machineId);
S
Sofian Hnaide 已提交
86 87 88
					resolve(this.machineId);
				}
			});
E
Erich Gamma 已提交
89 90 91 92

		}
	}
}