2023-11-09 23:06:42 +01:00
|
|
|
import { Observable, mergeMap } from "rxjs";
|
|
|
|
|
|
|
|
import {
|
|
|
|
AbstractStorageService,
|
|
|
|
StorageUpdate,
|
|
|
|
StorageUpdateType,
|
|
|
|
} from "@bitwarden/common/platform/abstractions/storage.service";
|
|
|
|
|
|
|
|
import { fromChromeEvent } from "../../browser/from-chrome-event";
|
2018-01-04 16:51:08 +01:00
|
|
|
|
2022-06-27 19:38:12 +02:00
|
|
|
export default abstract class AbstractChromeStorageService implements AbstractStorageService {
|
2023-11-09 23:06:42 +01:00
|
|
|
constructor(protected chromeStorageApi: chrome.storage.StorageArea) {}
|
|
|
|
|
2023-11-16 20:15:34 +01:00
|
|
|
get valuesRequireDeserialization(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-09 23:06:42 +01:00
|
|
|
get updates$(): Observable<StorageUpdate> {
|
|
|
|
return fromChromeEvent(this.chromeStorageApi.onChanged).pipe(
|
|
|
|
mergeMap(([changes]) => {
|
|
|
|
return Object.entries(changes).map(([key, change]) => {
|
|
|
|
// The `newValue` property isn't on the StorageChange object
|
|
|
|
// when the change was from a remove. Similarly a check of the `oldValue`
|
|
|
|
// could be used to tell if the operation was the first creation of this key
|
|
|
|
// but we currently do not differentiate that.
|
|
|
|
// Ref: https://developer.chrome.com/docs/extensions/reference/storage/#type-StorageChange
|
|
|
|
// Ref: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/StorageChange
|
|
|
|
const updateType: StorageUpdateType = "newValue" in change ? "save" : "remove";
|
|
|
|
|
|
|
|
return {
|
|
|
|
key: key,
|
|
|
|
// For removes this property will not exist but then it will just be
|
|
|
|
// undefined which is fine.
|
|
|
|
updateType: updateType,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-06-15 20:40:49 +02:00
|
|
|
async get<T>(key: string): Promise<T> {
|
2021-04-23 10:45:20 +02:00
|
|
|
return new Promise((resolve) => {
|
2021-02-03 20:36:05 +01:00
|
|
|
this.chromeStorageApi.get(key, (obj: any) => {
|
|
|
|
if (obj != null && obj[key] != null) {
|
2021-01-13 16:43:18 +01:00
|
|
|
resolve(obj[key] as T);
|
2021-06-04 01:38:17 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-02-03 20:36:05 +01:00
|
|
|
resolve(null);
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
2021-02-03 20:36:05 +01:00
|
|
|
});
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2019-08-13 17:47:29 +02:00
|
|
|
async has(key: string): Promise<boolean> {
|
2021-02-03 20:36:05 +01:00
|
|
|
return (await this.get(key)) != null;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:38:12 +02:00
|
|
|
async save(key: string, obj: any): Promise<void> {
|
2021-02-03 20:36:05 +01:00
|
|
|
if (obj == null) {
|
|
|
|
// Fix safari not liking null in set
|
2023-11-09 23:06:42 +01:00
|
|
|
return this.remove(key);
|
2018-01-04 16:51:08 +01:00
|
|
|
}
|
|
|
|
|
2019-08-13 17:47:29 +02:00
|
|
|
if (obj instanceof Set) {
|
2021-02-03 20:36:05 +01:00
|
|
|
obj = Array.from(obj);
|
2018-01-04 16:51:08 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-02-03 20:36:05 +01:00
|
|
|
const keyedObj = { [key]: obj };
|
2021-04-23 10:45:20 +02:00
|
|
|
return new Promise<void>((resolve) => {
|
2021-02-03 20:36:05 +01:00
|
|
|
this.chromeStorageApi.set(keyedObj, () => {
|
|
|
|
resolve();
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:38:12 +02:00
|
|
|
async remove(key: string): Promise<void> {
|
2021-04-23 10:45:20 +02:00
|
|
|
return new Promise<void>((resolve) => {
|
2021-02-03 20:36:05 +01:00
|
|
|
this.chromeStorageApi.remove(key, () => {
|
|
|
|
resolve();
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-01-04 16:51:08 +01:00
|
|
|
}
|