1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-07 09:31:31 +01:00

safari app messaging for storage service

This commit is contained in:
Kyle Spearrin 2019-08-13 11:47:29 -04:00
parent 384f3f1e47
commit 343ebe341b

View File

@ -3,31 +3,23 @@ import {
StorageService,
} from 'jslib/abstractions';
export default class BrowserStorageService implements StorageService {
private safariStorageApi: any;
private chromeStorageApi: any;
import { SafariApp } from '../browser/safariApp';
constructor(private platformUtilsService: PlatformUtilsService, private secure: boolean) {
if (platformUtilsService.isSafari()) {
this.safariStorageApi = secure ? safari.extension.secureSettings : safari.extension.settings;
} else {
export default class BrowserStorageService implements StorageService {
private chromeStorageApi: any;
private isSafari: boolean;
constructor(platformUtilsService: PlatformUtilsService) {
this.isSafari = platformUtilsService.isSafari();
if (!this.isSafari) {
this.chromeStorageApi = chrome.storage.local;
}
}
get<T>(key: string): Promise<T> {
if (this.safariStorageApi) {
return new Promise((resolve) => {
const json = this.safariStorageApi.getItem(key);
if (json) {
const obj = JSON.parse(json);
if (obj != null && obj[key] != null) {
resolve(obj[key] as T);
return;
}
}
resolve(null);
});
async get<T>(key: string): Promise<T> {
if (this.isSafari) {
const obj = await SafariApp.sendMessageToApp('storage_get', key);
return obj as T;
} else {
return new Promise((resolve) => {
this.chromeStorageApi.get(key, (obj: any) => {
@ -41,11 +33,13 @@ export default class BrowserStorageService implements StorageService {
}
}
save(key: string, obj: any): Promise<any> {
async save(key: string, obj: any): Promise<any> {
const keyedObj = { [key]: obj };
if (this.safariStorageApi) {
this.safariStorageApi.setItem(key, JSON.stringify(keyedObj));
return Promise.resolve();
if (this.isSafari) {
await SafariApp.sendMessageToApp('storage_save', {
key: key,
obj: obj,
});
} else {
return new Promise((resolve) => {
this.chromeStorageApi.set(keyedObj, () => {
@ -55,10 +49,9 @@ export default class BrowserStorageService implements StorageService {
}
}
remove(key: string): Promise<any> {
if (this.safariStorageApi) {
this.safariStorageApi.removeItem(key);
return Promise.resolve();
async remove(key: string): Promise<any> {
if (this.isSafari) {
await SafariApp.sendMessageToApp('storage_remove', key);
} else {
return new Promise((resolve) => {
this.chromeStorageApi.remove(key, () => {