提交 40d0de32 编写于 作者: D Daniel Imms

Use IWindowsService to get window count

Fixes #15642
上级 dc961cbc
......@@ -44,6 +44,7 @@ export interface IWindowsService {
openNewWindow(): TPromise<void>;
showWindow(windowId: number): TPromise<void>;
getWindows(): TPromise<{ id: number; path: string; title: string; }[]>;
getWindowCount(): TPromise<number>;
log(severity: string, ...messages: string[]): TPromise<void>;
// TODO@joao: what?
closeExtensionHostWindow(extensionDevelopmentPath: string): TPromise<void>;
......
......@@ -34,6 +34,7 @@ export interface IWindowsChannel extends IChannel {
call(command: 'openNewWindow'): TPromise<void>;
call(command: 'showWindow', arg: number): TPromise<void>;
call(command: 'getWindows'): TPromise<{ id: number; path: string; title: string; }[]>;
call(command: 'getWindowCount'): TPromise<number>;
call(command: 'log', arg: [string, string[]]): TPromise<void>;
call(command: 'closeExtensionHostWindow', arg: string): TPromise<void>;
call(command: 'showItemInFolder', arg: string): TPromise<void>;
......@@ -78,6 +79,7 @@ export class WindowsChannel implements IWindowsChannel {
case 'openNewWindow': return this.service.openNewWindow();
case 'showWindow': return this.service.showWindow(arg);
case 'getWindows': return this.service.getWindows();
case 'getWindowCount': return this.service.getWindowCount();
case 'log': return this.service.log(arg[0], arg[1]);
case 'closeExtensionHostWindow': return this.service.closeExtensionHostWindow(arg);
case 'showItemInFolder': return this.service.showItemInFolder(arg);
......@@ -187,6 +189,10 @@ export class WindowsChannelClient implements IWindowsService {
return this.channel.call('getWindows');
}
getWindowCount(): TPromise<number> {
return this.channel.call('getWindowCount');
}
log(severity: string, ...messages: string[]): TPromise<void> {
return this.channel.call('log', [severity, messages]);
}
......
......@@ -226,6 +226,10 @@ export class WindowsService implements IWindowsService, IDisposable {
return TPromise.as(result);
}
getWindowCount(): TPromise<number> {
return TPromise.as(this.windowsMainService.getWindows().length);
}
log(severity: string, ...messages: string[]): TPromise<void> {
console[severity].apply(console, ...messages);
return TPromise.as(null);
......
......@@ -35,13 +35,6 @@ export interface IBackupService {
export interface IBackupFileService {
_serviceBrand: any;
/**
* Gets the set of active workspace backup paths being tracked for restoration.
*
* @return The set of active workspace backup paths being tracked for restoration.
*/
getWorkspaceBackupPaths(): TPromise<string[]>;
/**
* Gets whether a text file has a backup to restore.
*
......
......@@ -9,7 +9,6 @@ import * as path from 'path';
import * as crypto from 'crypto';
import pfs = require('vs/base/node/pfs');
import Uri from 'vs/base/common/uri';
import { IBackupWorkspacesFormat } from 'vs/platform/backup/common/backup';
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IFileService } from 'vs/platform/files/common/files';
......@@ -31,16 +30,6 @@ export class BackupFileService implements IBackupFileService {
this.workspacesJsonPath = environmentService.backupWorkspacesPath;
}
public getWorkspaceBackupPaths(): TPromise<string[]> {
if (this.environmentService.isExtensionDevelopment) {
return TPromise.as([]);
}
return this.loadWorkspaces().then(workspacesJsonContent => {
return workspacesJsonContent.folderWorkspaces;
});
}
public hasBackup(resource: Uri): TPromise<boolean> {
const backupResource = this.getBackupResource(resource);
if (!backupResource) {
......@@ -103,29 +92,4 @@ export class BackupFileService implements IBackupFileService {
return this.fileService.del(Uri.file(this.getWorkspaceBackupDirectory()));
}
private loadWorkspaces(): TPromise<IBackupWorkspacesFormat> {
return pfs.readFile(this.workspacesJsonPath, 'utf8').then(content => {
let result: IBackupWorkspacesFormat;
try {
result = JSON.parse(content.toString());
// Ensure folderWorkspaces is a string[]
if (result.folderWorkspaces) {
const fws = result.folderWorkspaces;
if (!Array.isArray(fws) || fws.some(f => typeof f !== 'string')) {
result = Object.create(null);
}
}
} catch (ex) {
result = Object.create(null);
}
if (!result.folderWorkspaces) {
result.folderWorkspaces = [];
}
return result;
}, () => {
return { folderWorkspaces: [] };
});
}
}
\ No newline at end of file
......@@ -16,6 +16,7 @@ import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { TPromise } from 'vs/base/common/winjs.base';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IWindowsService } from 'vs/platform/windows/common/windows';
export class BackupService implements IBackupService {
......@@ -34,7 +35,8 @@ export class BackupService implements IBackupService {
@IFileService private fileService: IFileService,
@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@IEnvironmentService private environmentService: IEnvironmentService
@IEnvironmentService private environmentService: IEnvironmentService,
@IWindowsService private windowsService: IWindowsService
) {
this.toDispose = [];
......@@ -98,12 +100,12 @@ export class BackupService implements IBackupService {
return TPromise.as({ didBackup: false });
}
return this.backupFileService.getWorkspaceBackupPaths().then(workspaceBackupPaths => {
return this.windowsService.getWindowCount().then(windowCount => {
// When quit is requested skip the confirm callback and attempt to backup all workspaces.
// When quit is not requested the confirm callback should be shown when the window being
// closed is the only VS Code window open, except for on Mac where hot exit is only
// ever activated when quit is requested.
if (!quitRequested && (workspaceBackupPaths.length > 1 || platform.isMacintosh)) {
if (!quitRequested && (windowCount > 1 || platform.isMacintosh)) {
return TPromise.as({ didBackup: false });
}
......
......@@ -156,64 +156,4 @@ suite('BackupFileService', () => {
});
});
});
test('getWorkspaceBackupPaths should return [] when workspaces.json doesn\'t exist', () => {
fs.unlinkSync(workspacesJsonPath);
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPathsSync should return [] when workspaces.json is not properly formed JSON #1', () => {
fs.writeFileSync(workspacesJsonPath, '');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPathsSync should return [] when workspaces.json is not properly formed JSON #1', () => {
fs.writeFileSync(workspacesJsonPath, '{]');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPathsSync should return [] when workspaces.json is not properly formed JSON #1', () => {
fs.writeFileSync(workspacesJsonPath, 'foo');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPathsSync should return [] when folderWorkspaces in workspaces.json is absent', () => {
fs.writeFileSync(workspacesJsonPath, '{}');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is not a string array #1', () => {
fs.writeFileSync(workspacesJsonPath, '{"folderWorkspaces":{}}');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is not a string array #2', () => {
fs.writeFileSync(workspacesJsonPath, '{"folderWorkspaces":{"foo": ["bar"]}}');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is not a string array #3', () => {
fs.writeFileSync(workspacesJsonPath, '{"folderWorkspaces":{"foo": []}}');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is not a string array #4', () => {
fs.writeFileSync(workspacesJsonPath, '{"folderWorkspaces":{"foo": "bar"}}');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is not a string array #5', () => {
fs.writeFileSync(workspacesJsonPath, '{"folderWorkspaces":"foo"}');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is not a string array #6', () => {
fs.writeFileSync(workspacesJsonPath, '{"folderWorkspaces":1}');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, []));
});
test('getWorkspaceBackupPaths should return the contents of folderWorkspaces in workspaces.json', () => {
fs.writeFileSync(workspacesJsonPath, '{"folderWorkspaces":["foo", "bar"]}');
service.getWorkspaceBackupPaths().then(paths => assert.deepEqual(paths, ['foo', 'bar']));
});
});
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册