bitwarden-estensione-browser/src/services/browserStorage.service.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

56 lines
1.3 KiB
TypeScript
Raw Normal View History

import { StorageService } from "jslib-common/abstractions/storage.service";
2018-01-04 16:51:08 +01:00
2018-01-09 20:26:20 +01:00
export default class BrowserStorageService implements StorageService {
private chromeStorageApi: any;
2021-12-21 15:43:35 +01:00
constructor() {
this.chromeStorageApi = chrome.storage.local;
}
2021-12-21 15:43:35 +01:00
async get<T>(key: string): Promise<T> {
return new Promise((resolve) => {
this.chromeStorageApi.get(key, (obj: any) => {
if (obj != null && obj[key] != null) {
resolve(obj[key] as T);
return;
}
resolve(null);
2021-12-21 15:43:35 +01:00
});
});
2021-12-21 15:43:35 +01:00
}
async has(key: string): Promise<boolean> {
return (await this.get(key)) != null;
2021-12-21 15:43:35 +01:00
}
async save(key: string, obj: any): Promise<any> {
if (obj == null) {
// Fix safari not liking null in set
return new Promise<void>((resolve) => {
this.chromeStorageApi.remove(key, () => {
resolve();
});
2021-12-21 15:43:35 +01:00
});
2018-01-04 16:51:08 +01:00
}
if (obj instanceof Set) {
obj = Array.from(obj);
2018-01-04 16:51:08 +01:00
}
2021-12-21 15:43:35 +01:00
const keyedObj = { [key]: obj };
return new Promise<void>((resolve) => {
this.chromeStorageApi.set(keyedObj, () => {
resolve();
2021-12-21 15:43:35 +01:00
});
});
}
async remove(key: string): Promise<any> {
return new Promise<void>((resolve) => {
this.chromeStorageApi.remove(key, () => {
resolve();
2021-12-21 15:43:35 +01:00
});
});
}
2018-01-04 16:51:08 +01:00
}