diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 8906f8b5fc..1df9442425 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -62,6 +62,7 @@ import { AutofillService as AutofillServiceAbstraction } from '../services/abstr export default class MainBackground { messagingService: MessagingServiceAbstraction; storageService: StorageServiceAbstraction; + secureStorageService: StorageServiceAbstraction; i18nService: any; platformUtilsService: PlatformUtilsServiceAbstraction; utilsService: UtilsServiceAbstraction; @@ -107,11 +108,11 @@ export default class MainBackground { this.utilsService = new UtilsService(); this.platformUtilsService = new BrowserPlatformUtilsService(); this.messagingService = new BrowserMessagingService(this.platformUtilsService); - this.storageService = new BrowserStorageService(this.platformUtilsService); + this.storageService = new BrowserStorageService(this.platformUtilsService, false); + this.secureStorageService = new BrowserStorageService(this.platformUtilsService, true); this.i18nService = i18nService(this.platformUtilsService); this.constantsService = new ConstantsService(this.i18nService, this.platformUtilsService); - this.cryptoService = new CryptoService(this.storageService, - this.storageService); + this.cryptoService = new CryptoService(this.storageService, this.secureStorageService); this.tokenService = new TokenService(this.storageService); this.appIdService = new AppIdService(this.storageService); this.apiService = new ApiService(this.tokenService, this.platformUtilsService, diff --git a/src/services/browserStorage.service.ts b/src/services/browserStorage.service.ts index 4d8c345c5d..af460b7366 100644 --- a/src/services/browserStorage.service.ts +++ b/src/services/browserStorage.service.ts @@ -4,13 +4,21 @@ import { } from 'jslib/abstractions'; export default class BrowserStorageService implements StorageService { - constructor(private platformUtilsService: PlatformUtilsService) { + private safariStorageApi: any; + private chromeStorageApi: any; + + constructor(private platformUtilsService: PlatformUtilsService, private secure: boolean) { + if (platformUtilsService.isSafari()) { + this.safariStorageApi = secure ? safari.extension.secureSettings : safari.extension.settings; + } else { + this.chromeStorageApi = chrome.storage.local; + } } get(key: string): Promise { - if (this.platformUtilsService.isSafari()) { + if (this.safariStorageApi) { return new Promise((resolve) => { - const json = window.localStorage.getItem(key); + const json = this.safariStorageApi.getItem(key); if (json) { const obj = JSON.parse(json); if (obj && (typeof obj[key] !== 'undefined') && obj[key] !== null) { @@ -22,7 +30,7 @@ export default class BrowserStorageService implements StorageService { }); } else { return new Promise((resolve) => { - chrome.storage.local.get(key, (obj: any) => { + this.chromeStorageApi.get(key, (obj: any) => { if (obj && (typeof obj[key] !== 'undefined') && obj[key] !== null) { resolve(obj[key] as T); return; @@ -35,12 +43,12 @@ export default class BrowserStorageService implements StorageService { save(key: string, obj: any): Promise { const keyedObj = { [key]: obj }; - if (this.platformUtilsService.isSafari()) { - window.localStorage.setItem(key, JSON.stringify(keyedObj)); + if (this.safariStorageApi) { + this.safariStorageApi.setItem(key, JSON.stringify(keyedObj)); return Promise.resolve(); } else { return new Promise((resolve) => { - chrome.storage.local.set(keyedObj, () => { + this.chromeStorageApi.set(keyedObj, () => { resolve(); }); }); @@ -48,12 +56,12 @@ export default class BrowserStorageService implements StorageService { } remove(key: string): Promise { - if (this.platformUtilsService.isSafari()) { - window.localStorage.removeItem(key); + if (this.safariStorageApi) { + this.safariStorageApi.removeItem(key); return Promise.resolve(); } else { return new Promise((resolve) => { - chrome.storage.local.remove(key, () => { + this.chromeStorageApi.remove(key, () => { resolve(); }); });