1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00

[PM-11661]Add New Reseed - Fill Buffer Behind Feature Flag (#10905)

* Add New Reseed - Fill Buffer Behind Feature Flag

* Add Tests

* Lint
This commit is contained in:
Justin Baur
2024-09-06 09:48:27 -04:00
committed by GitHub
parent 03b3345bf6
commit 92e71d9252
8 changed files with 151 additions and 14 deletions

View File

@@ -1463,7 +1463,14 @@ export default class MainBackground {
});
if (needStorageReseed) {
await this.reseedStorage();
await this.reseedStorage(
await firstValueFrom(
this.configService.userCachedFeatureFlag$(
FeatureFlag.StorageReseedRefactor,
userBeingLoggedOut,
),
),
);
}
if (BrowserApi.isManifestVersion(3)) {
@@ -1518,7 +1525,7 @@ export default class MainBackground {
await SafariApp.sendMessageToApp("showPopover", null, true);
}
async reseedStorage() {
async reseedStorage(doFillBuffer: boolean) {
if (
!this.platformUtilsService.isChrome() &&
!this.platformUtilsService.isVivaldi() &&
@@ -1527,7 +1534,11 @@ export default class MainBackground {
return;
}
await this.storageService.reseed();
if (doFillBuffer) {
await this.storageService.fillBuffer();
} else {
await this.storageService.reseed();
}
}
async clearClipboard(clipboardValue: string, clearMs: number) {

View File

@@ -1,4 +1,4 @@
import { firstValueFrom, map, mergeMap } from "rxjs";
import { firstValueFrom, map, mergeMap, of, switchMap } from "rxjs";
import { NotificationsService } from "@bitwarden/common/abstractions/notifications.service";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
@@ -272,9 +272,25 @@ export default class RuntimeBackground {
await this.main.refreshBadge();
await this.main.refreshMenu();
break;
case "bgReseedStorage":
await this.main.reseedStorage();
case "bgReseedStorage": {
const doFillBuffer = await firstValueFrom(
this.accountService.activeAccount$.pipe(
switchMap((account) => {
if (account == null) {
return of(false);
}
return this.configService.userCachedFeatureFlag$(
FeatureFlag.StorageReseedRefactor,
account.id,
);
}),
),
);
await this.main.reseedStorage(doFillBuffer);
break;
}
case "authResult": {
const env = await firstValueFrom(this.environmentService.environment$);
const vaultUrl = env.getWebVaultUrl();

View File

@@ -32,6 +32,46 @@ export default class BrowserLocalStorageService extends AbstractChromeStorageSer
}
}
async fillBuffer() {
// Write 4MB of data in chrome.storage.local, log files will hold 4MB of data (by default)
// before forcing a compaction. To force a compaction and have it remove previously saved data,
// we want to fill it's buffer so that anything newly marked for deletion is gone.
// https://github.com/google/leveldb/blob/main/doc/impl.md#log-files
// It's important that if Google uses a different buffer length that we match that, as far as I can tell
// Google uses the default value in Chromium:
// https://github.com/chromium/chromium/blob/148774efa6b3a047369af6179a4248566b39d68f/components/value_store/lazy_leveldb.cc#L65-L66
const fakeData = "0".repeat(1024 * 1024); // 1MB of data
await new Promise<void>((resolve, reject) => {
this.chromeStorageApi.set(
{
fake_data_1: fakeData,
fake_data_2: fakeData,
fake_data_3: fakeData,
fake_data_4: fakeData,
},
() => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve();
},
);
});
await new Promise<void>((resolve, reject) => {
this.chromeStorageApi.remove(
["fake_data_1", "fake_data_2", "fake_data_3", "fake_data_4"],
() => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve();
},
);
});
}
override async get<T>(key: string): Promise<T> {
await this.awaitReseed();
return super.get(key);