1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 17:53:39 +00:00

[PM-5571] Migrate enableDDG to state provider framework (#8384)

Migrate enableDuckDuckGo to state provider framework.
This commit is contained in:
Oscar Hinton
2024-03-22 18:32:03 +01:00
committed by GitHub
parent 0f375c3a0e
commit 51f46e797c
14 changed files with 147 additions and 41 deletions

View File

@@ -23,6 +23,7 @@ import { ThemeStateService } from "@bitwarden/common/platform/theming/theme-stat
import { DialogService } from "@bitwarden/components";
import { SetPinComponent } from "../../auth/components/set-pin.component";
import { DesktopAutofillSettingsService } from "../../autofill/services/desktop-autofill-settings.service";
import { flagEnabled } from "../../platform/flags";
import { DesktopSettingsService } from "../../platform/services/desktop-settings.service";
@@ -122,6 +123,7 @@ export class SettingsComponent implements OnInit {
private userVerificationService: UserVerificationServiceAbstraction,
private desktopSettingsService: DesktopSettingsService,
private biometricStateService: BiometricStateService,
private desktopAutofillSettingsService: DesktopAutofillSettingsService,
) {
const isMac = this.platformUtilsService.getDevice() === DeviceType.MacOsDesktop;
@@ -262,11 +264,12 @@ export class SettingsComponent implements OnInit {
enableBrowserIntegration: await this.stateService.getEnableBrowserIntegration(),
enableBrowserIntegrationFingerprint:
await this.stateService.getEnableBrowserIntegrationFingerprint(),
enableDuckDuckGoBrowserIntegration: await firstValueFrom(
this.desktopAutofillSettingsService.enableDuckDuckGoBrowserIntegration$,
),
enableHardwareAcceleration: await firstValueFrom(
this.desktopSettingsService.hardwareAcceleration$,
),
enableDuckDuckGoBrowserIntegration:
await this.stateService.getEnableDuckDuckGoBrowserIntegration(),
theme: await firstValueFrom(this.themeStateService.selectedTheme$),
locale: await firstValueFrom(this.i18nService.userSetLocale$),
};
@@ -640,7 +643,7 @@ export class SettingsComponent implements OnInit {
}
async saveDdgBrowserIntegration() {
await this.stateService.setEnableDuckDuckGoBrowserIntegration(
await this.desktopAutofillSettingsService.setEnableDuckDuckGoBrowserIntegration(
this.form.value.enableDuckDuckGoBrowserIntegration,
);

View File

@@ -53,6 +53,7 @@ import { CipherService as CipherServiceAbstraction } from "@bitwarden/common/vau
import { DialogService } from "@bitwarden/components";
import { LoginGuard } from "../../auth/guards/login.guard";
import { DesktopAutofillSettingsService } from "../../autofill/services/desktop-autofill-settings.service";
import { Account } from "../../models/account";
import { DesktopSettingsService } from "../../platform/services/desktop-settings.service";
import { ElectronCryptoService } from "../../platform/services/electron-crypto.service";
@@ -218,6 +219,11 @@ const RELOAD_CALLBACK = new InjectionToken<() => any>("RELOAD_CALLBACK");
useClass: DesktopSettingsService,
deps: [StateProvider],
},
{
provide: DesktopAutofillSettingsService,
useClass: DesktopAutofillSettingsService,
deps: [StateProvider],
},
],
})
export class ServicesModule {}

View File

@@ -0,0 +1,29 @@
import { map } from "rxjs";
import {
AUTOFILL_SETTINGS_DISK,
KeyDefinition,
StateProvider,
} from "@bitwarden/common/platform/state";
const ENABLE_DUCK_DUCK_GO_BROWSER_INTEGRATION = new KeyDefinition(
AUTOFILL_SETTINGS_DISK,
"enableDuckDuckGoBrowserIntegration",
{
deserializer: (v: boolean) => v,
},
);
export class DesktopAutofillSettingsService {
private enableDuckDuckGoBrowserIntegrationState = this.stateProvider.getGlobal(
ENABLE_DUCK_DUCK_GO_BROWSER_INTEGRATION,
);
readonly enableDuckDuckGoBrowserIntegration$ =
this.enableDuckDuckGoBrowserIntegrationState.state$.pipe(map((x) => x ?? false));
constructor(private stateProvider: StateProvider) {}
async setEnableDuckDuckGoBrowserIntegration(newValue: boolean): Promise<void> {
await this.enableDuckDuckGoBrowserIntegrationState.update(() => newValue);
}
}

View File

@@ -26,6 +26,7 @@ import { StateEventRegistrarService } from "@bitwarden/common/platform/state/sta
import { MemoryStorageService as MemoryStorageServiceForStateProviders } from "@bitwarden/common/platform/state/storage/memory-storage.service";
/* eslint-enable import/no-restricted-paths */
import { DesktopAutofillSettingsService } from "./autofill/services/desktop-autofill-settings.service";
import { MenuMain } from "./main/menu/menu.main";
import { MessagingMain } from "./main/messaging.main";
import { NativeMessagingMain } from "./main/native-messaging.main";
@@ -71,6 +72,7 @@ export class Main {
biometricsService: BiometricsServiceAbstraction;
nativeMessagingMain: NativeMessagingMain;
clipboardMain: ClipboardMain;
desktopAutofillSettingsService: DesktopAutofillSettingsService;
constructor() {
// Set paths for portable builds
@@ -233,6 +235,8 @@ export class Main {
app.getPath("exe"),
);
this.desktopAutofillSettingsService = new DesktopAutofillSettingsService(stateProvider);
this.clipboardMain = new ClipboardMain();
this.clipboardMain.init();
@@ -268,10 +272,10 @@ export class Main {
if (
(await this.stateService.getEnableBrowserIntegration()) ||
(await this.stateService.getEnableDuckDuckGoBrowserIntegration())
(await firstValueFrom(
this.desktopAutofillSettingsService.enableDuckDuckGoBrowserIntegration$,
))
) {
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.nativeMessagingMain.listen();
}

View File

@@ -77,14 +77,10 @@ export class MessagingMain {
break;
case "enableBrowserIntegration":
this.main.nativeMessagingMain.generateManifests();
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.main.nativeMessagingMain.listen();
break;
case "enableDuckDuckGoBrowserIntegration":
this.main.nativeMessagingMain.generateDdgManifests();
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.main.nativeMessagingMain.listen();
break;
case "disableBrowserIntegration":

View File

@@ -24,7 +24,7 @@ export class NativeMessagingMain {
private exePath: string,
) {}
async listen() {
listen() {
ipc.config.id = "bitwarden";
ipc.config.retry = 1500;
const ipcSocketRoot = getIpcSocketRoot();

View File

@@ -12,6 +12,7 @@ import { StateService } from "@bitwarden/common/platform/services/state.service"
import { DialogService } from "@bitwarden/components";
import { VerifyNativeMessagingDialogComponent } from "../app/components/verify-native-messaging-dialog.component";
import { DesktopAutofillSettingsService } from "../autofill/services/desktop-autofill-settings.service";
import { DecryptedCommandData } from "../models/native-messaging/decrypted-command-data";
import { EncryptedMessage } from "../models/native-messaging/encrypted-message";
import { EncryptedMessageResponse } from "../models/native-messaging/encrypted-message-response";
@@ -34,6 +35,7 @@ export class NativeMessageHandlerService {
private messagingService: MessagingService,
private encryptedMessageHandlerService: EncryptedMessageHandlerService,
private dialogService: DialogService,
private desktopAutofillSettingsService: DesktopAutofillSettingsService,
) {}
async handleMessage(message: Message) {
@@ -71,7 +73,9 @@ export class NativeMessageHandlerService {
try {
const remotePublicKey = Utils.fromB64ToArray(publicKey);
const ddgEnabled = await this.stateService.getEnableDuckDuckGoBrowserIntegration();
const ddgEnabled = await firstValueFrom(
this.desktopAutofillSettingsService.enableDuckDuckGoBrowserIntegration$,
);
if (!ddgEnabled) {
this.sendResponse({