1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-12 00:41:29 +01:00

system service for proc reload and clear clipboard

This commit is contained in:
Kyle Spearrin 2019-02-27 09:21:58 -05:00
parent 68f7557e44
commit 808437ab06
4 changed files with 72 additions and 34 deletions

View File

@ -6,6 +6,4 @@ export abstract class LockService {
setLockOption: (lockOption: number) => Promise<void>;
isPinLockSet: () => Promise<[boolean, boolean]>;
clear: () => Promise<any>;
startLockReload: () => void;
cancelLockReload: () => void;
}

View File

@ -0,0 +1,5 @@
export abstract class SystemService {
startProcessReload: () => void;
cancelProcessReload: () => void;
clearClipboard: (clipboardValue: string, timeoutMs?: number) => void;
}

View File

@ -14,13 +14,12 @@ export class LockService implements LockServiceAbstraction {
pinLocked = false;
private inited = false;
private reloadInterval: any = null;
constructor(private cipherService: CipherService, private folderService: FolderService,
private collectionService: CollectionService, private cryptoService: CryptoService,
private platformUtilsService: PlatformUtilsService, private storageService: StorageService,
private messagingService: MessagingService, private searchService: SearchService,
private lockedCallback: () => Promise<void> = null, private reloadCallback: () => Promise<void> = null) {
private lockedCallback: () => Promise<void> = null) {
}
init(checkOnInterval: boolean) {
@ -118,34 +117,4 @@ export class LockService implements LockServiceAbstraction {
clear(): Promise<any> {
return this.storageService.remove(ConstantsService.protectedPin);
}
startLockReload(): void {
if (this.pinLocked || this.reloadInterval != null) {
return;
}
this.reloadInterval = setInterval(async () => {
let doRefresh = false;
const lastActive = await this.storageService.get<number>(ConstantsService.lastActiveKey);
if (lastActive != null) {
const diffSeconds = (new Date()).getTime() - lastActive;
// Don't refresh if they are still active in the window
doRefresh = diffSeconds >= 5000;
}
if (doRefresh) {
clearInterval(this.reloadInterval);
this.reloadInterval = null;
this.messagingService.send('reloadProcess');
if (this.reloadCallback != null) {
await this.reloadCallback();
}
}
}, 10000);
}
cancelLockReload(): void {
if (this.reloadInterval != null) {
clearInterval(this.reloadInterval);
this.reloadInterval = null;
}
}
}

View File

@ -0,0 +1,66 @@
import { LockService } from '../abstractions/lock.service';
import { MessagingService } from '../abstractions/messaging.service';
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
import { StorageService } from '../abstractions/storage.service';
import { SystemService as SystemServiceAbstraction } from '../abstractions/system.service';
import { ConstantsService } from './constants.service';
import { Utils } from '../misc/utils';
export class SystemService implements SystemServiceAbstraction {
private reloadInterval: any = null;
private clearClipboardTimeout: any = null;
constructor(private storageService: StorageService, private lockService: LockService,
private messagingService: MessagingService, private platformUtilsService: PlatformUtilsService,
private reloadCallback: () => Promise<void> = null) {
}
startProcessReload(): void {
if (this.lockService.pinLocked || this.reloadInterval != null) {
return;
}
this.cancelProcessReload();
this.reloadInterval = setInterval(async () => {
let doRefresh = false;
const lastActive = await this.storageService.get<number>(ConstantsService.lastActiveKey);
if (lastActive != null) {
const diffSeconds = (new Date()).getTime() - lastActive;
// Don't refresh if they are still active in the window
doRefresh = diffSeconds >= 5000;
}
if (doRefresh) {
clearInterval(this.reloadInterval);
this.reloadInterval = null;
this.messagingService.send('reloadProcess');
if (this.reloadCallback != null) {
await this.reloadCallback();
}
}
}, 10000);
}
cancelProcessReload(): void {
if (this.reloadInterval != null) {
clearInterval(this.reloadInterval);
this.reloadInterval = null;
}
}
clearClipboard(clipboardValue: string, timeoutMs = 5000): void {
if (this.clearClipboardTimeout != null) {
clearTimeout(this.clearClipboardTimeout);
this.clearClipboardTimeout = null;
}
if (Utils.isNullOrWhitespace(clipboardValue)) {
return;
}
this.clearClipboardTimeout = setTimeout(async () => {
const clipboardValueNow = await this.platformUtilsService.readFromClipboard();
if (clipboardValue === clipboardValueNow) {
this.platformUtilsService.copyToClipboard('');
}
}, timeoutMs);
}
}