This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,353 additions
and
13 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import log from 'electron-log'; | ||
import * as path from 'path'; | ||
|
||
import { isDev } from '../utils'; | ||
|
||
import { LogLevelType } from '../../enums/logLevelType'; | ||
|
||
import { LogService as LogServiceAbstraction } from '../../abstractions/log.service'; | ||
|
||
export class ElectronLogService implements LogServiceAbstraction { | ||
constructor(private filter: (level: LogLevelType) => boolean = null, logDir: string = null) { | ||
if (log.transports == null) { | ||
return; | ||
} | ||
|
||
log.transports.file.level = 'info'; | ||
if (logDir != null) { | ||
log.transports.file.file = path.join(logDir, 'app.log'); | ||
} | ||
} | ||
|
||
debug(message: string) { | ||
if (!isDev()) { | ||
return; | ||
} | ||
|
||
this.write(LogLevelType.Debug, message); | ||
} | ||
|
||
info(message: string) { | ||
this.write(LogLevelType.Info, message); | ||
} | ||
|
||
warning(message: string) { | ||
this.write(LogLevelType.Warning, message); | ||
} | ||
|
||
error(message: string) { | ||
this.write(LogLevelType.Error, message); | ||
} | ||
|
||
write(level: LogLevelType, message: string) { | ||
if (this.filter != null && this.filter(level)) { | ||
return; | ||
} | ||
|
||
switch (level) { | ||
case LogLevelType.Debug: | ||
log.debug(message); | ||
break; | ||
case LogLevelType.Info: | ||
log.info(message); | ||
break; | ||
case LogLevelType.Warning: | ||
log.warn(message); | ||
break; | ||
case LogLevelType.Error: | ||
log.error(message); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { | ||
clipboard, | ||
remote, | ||
shell, | ||
} from 'electron'; | ||
|
||
import { | ||
isDev, | ||
isMacAppStore, | ||
} from '../utils'; | ||
|
||
import { DeviceType } from '../../enums/deviceType'; | ||
|
||
import { I18nService } from '../../abstractions/i18n.service'; | ||
import { PlatformUtilsService } from '../../abstractions/platformUtils.service'; | ||
|
||
import { AnalyticsIds } from '../../misc/analytics'; | ||
import { Utils } from '../../misc/utils'; | ||
|
||
export class ElectronPlatformUtilsService implements PlatformUtilsService { | ||
identityClientId: string; | ||
|
||
private deviceCache: DeviceType = null; | ||
private analyticsIdCache: string = null; | ||
|
||
constructor(private i18nService: I18nService, private isDesktopApp: boolean) { | ||
this.identityClientId = isDesktopApp ? 'desktop' : 'connector'; | ||
} | ||
|
||
getDevice(): DeviceType { | ||
if (!this.deviceCache) { | ||
switch (process.platform) { | ||
case 'win32': | ||
this.deviceCache = DeviceType.Windows; | ||
break; | ||
case 'darwin': | ||
this.deviceCache = DeviceType.MacOs; | ||
break; | ||
case 'linux': | ||
default: | ||
this.deviceCache = DeviceType.Linux; | ||
break; | ||
} | ||
} | ||
|
||
return this.deviceCache; | ||
} | ||
|
||
getDeviceString(): string { | ||
return DeviceType[this.getDevice()].toLowerCase(); | ||
} | ||
|
||
isFirefox(): boolean { | ||
return false; | ||
} | ||
|
||
isChrome(): boolean { | ||
return true; | ||
} | ||
|
||
isEdge(): boolean { | ||
return false; | ||
} | ||
|
||
isOpera(): boolean { | ||
return false; | ||
} | ||
|
||
isVivaldi(): boolean { | ||
return false; | ||
} | ||
|
||
isSafari(): boolean { | ||
return false; | ||
} | ||
|
||
isMacAppStore(): boolean { | ||
return isMacAppStore(); | ||
} | ||
|
||
analyticsId(): string { | ||
if (!this.isDesktopApp) { | ||
return null; | ||
} | ||
|
||
if (this.analyticsIdCache) { | ||
return this.analyticsIdCache; | ||
} | ||
|
||
this.analyticsIdCache = (AnalyticsIds as any)[this.getDevice()]; | ||
return this.analyticsIdCache; | ||
} | ||
|
||
getDomain(uriString: string): string { | ||
return Utils.getHostname(uriString); | ||
} | ||
|
||
isViewOpen(): boolean { | ||
return false; | ||
} | ||
|
||
launchUri(uri: string, options?: any): void { | ||
shell.openExternal(uri); | ||
} | ||
|
||
saveFile(win: Window, blobData: any, blobOptions: any, fileName: string): void { | ||
const blob = new Blob([blobData], blobOptions); | ||
const a = win.document.createElement('a'); | ||
a.href = win.URL.createObjectURL(blob); | ||
a.download = fileName; | ||
window.document.body.appendChild(a); | ||
a.click(); | ||
window.document.body.removeChild(a); | ||
} | ||
|
||
getApplicationVersion(): string { | ||
return remote.app.getVersion(); | ||
} | ||
|
||
supportsU2f(win: Window): boolean { | ||
// Not supported in Electron at this time. | ||
// ref: https://github.com/electron/electron/issues/3226 | ||
return false; | ||
} | ||
|
||
showDialog(text: string, title?: string, confirmText?: string, cancelText?: string, type?: string): | ||
Promise<boolean> { | ||
const buttons = [confirmText == null ? this.i18nService.t('ok') : confirmText]; | ||
if (cancelText != null) { | ||
buttons.push(cancelText); | ||
} | ||
|
||
const result = remote.dialog.showMessageBox(remote.getCurrentWindow(), { | ||
type: type, | ||
title: title, | ||
message: title, | ||
detail: text, | ||
buttons: buttons, | ||
cancelId: buttons.length === 2 ? 1 : null, | ||
defaultId: 0, | ||
noLink: true, | ||
}); | ||
|
||
return Promise.resolve(result === 0); | ||
} | ||
|
||
isDev(): boolean { | ||
return isDev(); | ||
} | ||
|
||
copyToClipboard(text: string, options?: any): void { | ||
const type = options ? options.type : null; | ||
clipboard.writeText(text, type); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/electron/services/electronRendererSecureStorage.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ipcRenderer } from 'electron'; | ||
|
||
import { StorageService } from '../../abstractions/storage.service'; | ||
|
||
export class ElectronRendererSecureStorageService implements StorageService { | ||
async get<T>(key: string): Promise<T> { | ||
const val = ipcRenderer.sendSync('keytar', { | ||
action: 'getPassword', | ||
key: key, | ||
}); | ||
return Promise.resolve(val != null ? JSON.parse(val) as T : null); | ||
} | ||
|
||
async save(key: string, obj: any): Promise<any> { | ||
ipcRenderer.sendSync('keytar', { | ||
action: 'setPassword', | ||
key: key, | ||
value: JSON.stringify(obj), | ||
}); | ||
return Promise.resolve(); | ||
} | ||
|
||
async remove(key: string): Promise<any> { | ||
ipcRenderer.sendSync('keytar', { | ||
action: 'deletePassword', | ||
key: key, | ||
}); | ||
return Promise.resolve(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { StorageService } from '../../abstractions/storage.service'; | ||
|
||
import { ConstantsService } from '../../services/constants.service'; | ||
|
||
// tslint:disable-next-line | ||
const Store = require('electron-store'); | ||
|
||
export class ElectronStorageService implements StorageService { | ||
private store: any; | ||
|
||
constructor() { | ||
const storeConfig: any = { | ||
defaults: {} as any, | ||
name: 'data', | ||
}; | ||
// Default lock options to "on restart". | ||
storeConfig.defaults[ConstantsService.lockOptionKey] = -1; | ||
this.store = new Store(storeConfig); | ||
} | ||
|
||
get<T>(key: string): Promise<T> { | ||
const val = this.store.get(key) as T; | ||
return Promise.resolve(val != null ? val : null); | ||
} | ||
|
||
save(key: string, obj: any): Promise<any> { | ||
this.store.set(key, obj); | ||
return Promise.resolve(); | ||
} | ||
|
||
remove(key: string): Promise<any> { | ||
this.store.delete(key); | ||
return Promise.resolve(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export function isDev() { | ||
// ref: https://github.com/sindresorhus/electron-is-dev | ||
if ('ELECTRON_IS_DEV' in process.env) { | ||
return parseInt(process.env.ELECTRON_IS_DEV, 10) === 1; | ||
} | ||
return (process.defaultApp || /node_modules[\\/]electron[\\/]/.test(process.execPath)); | ||
} | ||
|
||
export function isAppImage() { | ||
return process.platform === 'linux' && 'APPIMAGE' in process.env; | ||
} | ||
|
||
export function isMacAppStore() { | ||
return process.platform === 'darwin' && process.mas && process.mas === true; | ||
} | ||
|
||
export function isWindowsStore() { | ||
return process.platform === 'win32' && process.windowsStore && process.windowsStore === true; | ||
} | ||
|
||
export function isSnapStore() { | ||
return process.platform === 'linux' && process.env.SNAP_USER_DATA != null; | ||
} | ||
|
||
export function isWindowsPortable() { | ||
return process.platform === 'win32' && process.env.PORTABLE_EXECUTABLE_DIR != null; | ||
} |
Oops, something went wrong.