import { translate as $l } from "@padloc/locale/src/translate" ; import { Serializable } from "./encoding" ; import { CryptoProvider } from "./crypto" ; import { Err , ErrorCode } from "./error" ; import { StubCryptoProvider } from "./stub-crypto-provider" ; import { Storage , MemoryStorage } from "./storage" ; /**
 * Object representing all information available for a given device.
 */ export class DeviceInfo extends Serializable { /** Platform/Operating System running on the device */ platform : string = "" ; /** OS version running on the device */ osVersion : string = "" ; /** Unique device identifier */ id : string = "" ; /** Padloc version installed on the device */ appVersion : string = "" ; /** The user agent of the browser running the application */ userAgent : string = "" ; /** The devices locale setting */ locale : string = "en" ; /** The device manufacturer, if available */ manufacturer : string = "" ; /** The device mode, if available */ model : string = "" ; /** The browser the application was loaded in, if applicable */ browser : string = "" ; supportsBioAuth : boolean = false ; supportsKeyStore : boolean = false ; get description ( ) { return this . browser ? $l ( "{0} on {1}" , this . browser , this . platform ) : $l ( "{0} Device" , this . platform ) ; } constructor ( props ? ) { super ( ) ; props && Object . assign ( this , props ) ; } } /**
 * Generic interface for various platform APIs
 */ export interface Platform { /** Copies the given `text` to the system clipboard */ setClipboard ( text ) : Promise < void > ; /** Retrieves the current text from the system clipboard */ getClipboard ( ) : Promise < string > ; /** Get information about the current device */ getDeviceInfo ( ) : Promise < DeviceInfo > ; crypto : CryptoProvider ; storage : Storage ; scanQR ( ) : Promise < string > ; stopScanQR ( ) : Promise < void > ; isBiometricAuthAvailable ( ) : Promise < boolean > ; biometricAuth ( message ? ) : Promise < boolean > ; isKeyStoreAvailable ( ) : Promise < boolean > ; keyStoreGet ( name ) : Promise < string > ; keyStoreSet ( name , val ) : Promise < void > ; keyStoreDelete ( name ) : Promise < void > ; composeEmail ( addr , subject , message ) : Promise < void > ; saveFile ( name , type , contents ) : Promise < void > ; } /**
 * Stub implementation of the [[Platform]] interface. Useful for testing
 */ export class StubPlatform implements Platform { crypto = new StubCryptoProvider ( ) ; storage : Storage = new MemoryStorage ( ) ; async setClipboard ( _val ) { throw new Err ( ErrorCode . NOT_SUPPORTED ) ; } async getClipboard ( ) { throw new Err ( ErrorCode . NOT_SUPPORTED ) ; return "" ; } async getDeviceInfo ( ) { return new DeviceInfo ( ) ; } async scanQR ( ) { throw new Err ( ErrorCode . NOT_SUPPORTED ) ; return "" ; } async stopScanQR ( ) { throw new Err ( ErrorCode . NOT_SUPPORTED ) ; } async isBiometricAuthAvailable ( ) { return false ; } async biometricAuth ( ) { throw new Err ( ErrorCode . NOT_SUPPORTED ) ; return false ; } async isKeyStoreAvailable ( ) { return false ; } async keyStoreGet ( _name ) { throw new Err ( ErrorCode . NOT_SUPPORTED ) ; return "" ; } async keyStoreSet ( _name , _val ) { throw new Err ( ErrorCode . NOT_SUPPORTED ) ; } async keyStoreDelete ( _name ) { throw new Err ( ErrorCode . NOT_SUPPORTED ) ; } async composeEmail ( _addr , _subject , _message ) { throw new Err ( ErrorCode . NOT_SUPPORTED ) ; } async saveFile ( _name , _type , _contents ) { } } let platform = new StubPlatform ( ) ; /**
 * Set the appropriate [[Platform]] implemenation for the current environment
 */ export function setPlatform ( p ) { platform = p ; } /**
 * Get the current [[Platform]] implemenation
 */ export function getPlatform ( ) { return platform ; } /** Copies the given `text` to the system clipboard */ export function getClipboard ( ) { return platform . getClipboard ( ) ; } /** Retrieves the current text from the system clipboard */ export function setClipboard ( val ) { return platform . setClipboard ( val ) ; } /** Get information about the current device */ export function getDeviceInfo ( ) { return platform . getDeviceInfo ( ) ; } export function getCryptoProvider ( ) { return platform . crypto ; } export function getStorage ( ) { return platform . storage ; } export function scanQR ( ) { return platform . scanQR ( ) ; } export function stopScanQR ( ) { return platform . stopScanQR ( ) ; } export function isBiometricAuthAvailable ( ) { return platform . isBiometricAuthAvailable ( ) ; } export function biometricAuth ( message ? ) { return platform . biometricAuth ( message ) ; } export function isKeyStoreAvailable ( ) { return platform . isKeyStoreAvailable ( ) ; } export function keyStoreSet ( name , value ) { return platform . keyStoreSet ( name , value ) ; } export function keyStoreGet ( name ) { return platform . keyStoreGet ( name ) ; } export function keyStoreDelete ( name ) { return platform . keyStoreDelete ( name ) ; } export function composeEmail ( addr , subject , message ) { return platform . composeEmail ( addr , subject , message ) ; } export function saveFile ( name , type , contents ) { return platform . saveFile ( name , type , contents ) ; }