1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00

[PM-6383] Migrate clearClipboard account setting to autofill settings service (#8022)

* migrate clearClipboard account setting to autofill settings state provider

* replace state service get/set clearClipboard with autofill settings service equivalents

* PR suggestions cleanup
This commit is contained in:
Jonathan Prusik
2024-02-23 13:52:13 -05:00
committed by GitHub
parent 38e40a0471
commit 34a8d9af86
17 changed files with 365 additions and 61 deletions

View File

@@ -18,6 +18,7 @@ import {
} from "../../auth/background/service-factories/auth-service.factory";
import { userVerificationServiceFactory } from "../../auth/background/service-factories/user-verification-service.factory";
import { openUnlockPopout } from "../../auth/popup/utils/auth-popout-window";
import { autofillSettingsServiceFactory } from "../../autofill/background/service_factories/autofill-settings-service.factory";
import { eventCollectionServiceFactory } from "../../background/service-factories/event-collection-service.factory";
import { Account } from "../../models/account";
import { CachedServices } from "../../platform/background/service-factories/factory-options";
@@ -104,11 +105,14 @@ export class ContextMenuClickedHandler {
stateServiceOptions: {
stateFactory: stateFactory,
},
autofillSettingsServiceOptions: {
stateFactory: autofillSettingsServiceFactory,
},
};
const generatePasswordToClipboardCommand = new GeneratePasswordToClipboardCommand(
await passwordGenerationServiceFactory(cachedServices, serviceOptions),
await stateServiceFactory(cachedServices, serviceOptions),
await autofillSettingsServiceFactory(cachedServices, serviceOptions),
);
const autofillCommand = new AutofillTabCommand(

View File

@@ -1,10 +1,10 @@
import { mock, MockProxy } from "jest-mock-extended";
import { AutofillSettingsService } from "@bitwarden/common/autofill/services/autofill-settings.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/password";
import { setAlarmTime } from "../../platform/alarms/alarm-state";
import { BrowserApi } from "../../platform/browser/browser-api";
import { BrowserStateService } from "../../platform/services/abstractions/browser-state.service";
import { clearClipboardAlarmName } from "./clear-clipboard";
import { GeneratePasswordToClipboardCommand } from "./generate-password-to-clipboard-command";
@@ -19,13 +19,12 @@ const setAlarmTimeMock = setAlarmTime as jest.Mock;
describe("GeneratePasswordToClipboardCommand", () => {
let passwordGenerationService: MockProxy<PasswordGenerationServiceAbstraction>;
let stateService: MockProxy<BrowserStateService>;
let autofillSettingsService: MockProxy<AutofillSettingsService>;
let sut: GeneratePasswordToClipboardCommand;
beforeEach(() => {
passwordGenerationService = mock<PasswordGenerationServiceAbstraction>();
stateService = mock<BrowserStateService>();
passwordGenerationService.getOptions.mockResolvedValue([{ length: 8 }, {} as any]);
@@ -33,7 +32,10 @@ describe("GeneratePasswordToClipboardCommand", () => {
jest.spyOn(BrowserApi, "sendTabsMessage").mockReturnValue();
sut = new GeneratePasswordToClipboardCommand(passwordGenerationService, stateService);
sut = new GeneratePasswordToClipboardCommand(
passwordGenerationService,
autofillSettingsService,
);
});
afterEach(() => {
@@ -42,7 +44,7 @@ describe("GeneratePasswordToClipboardCommand", () => {
describe("generatePasswordToClipboard", () => {
it("has clear clipboard value", async () => {
stateService.getClearClipboard.mockResolvedValue(5 * 60); // 5 minutes
jest.spyOn(sut as any, "getClearClipboard").mockImplementation(() => 5 * 60); // 5 minutes
await sut.generatePasswordToClipboard({ id: 1 } as any);
@@ -59,7 +61,7 @@ describe("GeneratePasswordToClipboardCommand", () => {
});
it("does not have clear clipboard value", async () => {
stateService.getClearClipboard.mockResolvedValue(null);
jest.spyOn(sut as any, "getClearClipboard").mockImplementation(() => null);
await sut.generatePasswordToClipboard({ id: 1 } as any);

View File

@@ -1,7 +1,9 @@
import { firstValueFrom } from "rxjs";
import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/password";
import { setAlarmTime } from "../../platform/alarms/alarm-state";
import { BrowserStateService } from "../../platform/services/abstractions/browser-state.service";
import { clearClipboardAlarmName } from "./clear-clipboard";
import { copyToClipboard } from "./copy-to-clipboard-command";
@@ -9,9 +11,13 @@ import { copyToClipboard } from "./copy-to-clipboard-command";
export class GeneratePasswordToClipboardCommand {
constructor(
private passwordGenerationService: PasswordGenerationServiceAbstraction,
private stateService: BrowserStateService,
private autofillSettingsService: AutofillSettingsServiceAbstraction,
) {}
async getClearClipboard() {
return await firstValueFrom(this.autofillSettingsService.clearClipboardDelay$);
}
async generatePasswordToClipboard(tab: chrome.tabs.Tab) {
const [options] = await this.passwordGenerationService.getOptions();
const password = await this.passwordGenerationService.generatePassword(options);
@@ -20,7 +26,7 @@ export class GeneratePasswordToClipboardCommand {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
copyToClipboard(tab, password);
const clearClipboard = await this.stateService.getClearClipboard();
const clearClipboard = await this.getClearClipboard();
if (clearClipboard != null) {
await setAlarmTime(clearClipboardAlarmName, clearClipboard * 1000);

View File

@@ -22,6 +22,19 @@ export const EVENTS = {
FOCUSOUT: "focusout",
} as const;
export const ClearClipboardDelay = {
Never: null as null,
TenSeconds: 10,
TwentySeconds: 20,
ThirtySeconds: 30,
OneMinute: 60,
TwoMinutes: 120,
FiveMinutes: 300,
} as const;
export type ClearClipboardDelaySetting =
(typeof ClearClipboardDelay)[keyof typeof ClearClipboardDelay];
/* Context Menu item Ids */
export const AUTOFILL_CARD_ID = "autofill-card";
export const AUTOFILL_ID = "autofill";

View File

@@ -779,6 +779,7 @@ export default class MainBackground {
this.platformUtilsService,
systemUtilsServiceReloadCallback,
this.stateService,
this.autofillSettingsService,
this.vaultTimeoutSettingsService,
);

View File

@@ -4,10 +4,10 @@ import { GlobalState } from "@bitwarden/common/platform/models/domain/global-sta
import { authServiceFactory } from "../../auth/background/service-factories/auth-service.factory";
import { autofillServiceFactory } from "../../autofill/background/service_factories/autofill-service.factory";
import { autofillSettingsServiceFactory } from "../../autofill/background/service_factories/autofill-settings-service.factory";
import { GeneratePasswordToClipboardCommand } from "../../autofill/clipboard";
import { AutofillTabCommand } from "../../autofill/commands/autofill-tab-command";
import { Account } from "../../models/account";
import { stateServiceFactory } from "../../platform/background/service-factories/state-service.factory";
import {
passwordGenerationServiceFactory,
PasswordGenerationServiceInitOptions,
@@ -94,11 +94,14 @@ const doGeneratePasswordToClipboard = async (tab: chrome.tabs.Tab): Promise<void
stateServiceOptions: {
stateFactory: stateFactory,
},
autofillSettingsServiceOptions: {
stateFactory: autofillSettingsServiceFactory,
},
};
const command = new GeneratePasswordToClipboardCommand(
await passwordGenerationServiceFactory(cache, options),
await stateServiceFactory(cache, options),
await autofillSettingsServiceFactory(cache, options),
);
// 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

View File

@@ -11,6 +11,7 @@ import { ThemeType } from "@bitwarden/common/platform/enums";
import { VaultSettingsService } from "@bitwarden/common/vault/abstractions/vault-settings/vault-settings.service";
import { UriMatchType } from "@bitwarden/common/vault/enums";
import { ClearClipboardDelaySetting } from "../../../../../apps/browser/src/autofill/constants";
import { enableAccountSwitching } from "../../platform/flags";
@Component({
@@ -35,7 +36,7 @@ export class OptionsComponent implements OnInit {
themeOptions: any[];
defaultUriMatch = UriMatchType.Domain;
uriMatchOptions: any[];
clearClipboard: number;
clearClipboard: ClearClipboardDelaySetting;
clearClipboardOptions: any[];
showGeneral = true;
showAutofill = true;
@@ -115,7 +116,7 @@ export class OptionsComponent implements OnInit {
const defaultUriMatch = await this.stateService.getDefaultUriMatch();
this.defaultUriMatch = defaultUriMatch == null ? UriMatchType.Domain : defaultUriMatch;
this.clearClipboard = await this.stateService.getClearClipboard();
this.clearClipboard = await firstValueFrom(this.autofillSettingsService.clearClipboardDelay$);
}
async updateAddLoginNotification() {
@@ -175,6 +176,6 @@ export class OptionsComponent implements OnInit {
}
async saveClearClipboard() {
await this.stateService.setClearClipboard(this.clearClipboard);
await this.autofillSettingsService.setClearClipboardDelay(this.clearClipboard);
}
}

View File

@@ -10,6 +10,7 @@ import { VaultTimeoutSettingsService } from "@bitwarden/common/abstractions/vaul
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { UserVerificationService as UserVerificationServiceAbstraction } from "@bitwarden/common/auth/abstractions/user-verification/user-verification.service.abstraction";
import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service";
import { DeviceType } from "@bitwarden/common/enums";
import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum";
import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service";
@@ -81,7 +82,7 @@ export class SettingsComponent implements OnInit {
requirePasswordOnStart: false,
approveLoginRequests: false,
// Account Preferences
clearClipboard: [null as number | null],
clearClipboard: [null],
minimizeOnCopyToClipboard: false,
enableFavicons: false,
// App Settings
@@ -111,6 +112,7 @@ export class SettingsComponent implements OnInit {
private platformUtilsService: PlatformUtilsService,
private vaultTimeoutSettingsService: VaultTimeoutSettingsService,
private stateService: StateService,
private autofillSettingsService: AutofillSettingsServiceAbstraction,
private messagingService: MessagingService,
private cryptoService: CryptoService,
private modalService: ModalService,
@@ -247,7 +249,7 @@ export class SettingsComponent implements OnInit {
this.biometricStateService.requirePasswordOnStart$,
),
approveLoginRequests: (await this.stateService.getApproveLoginRequests()) ?? false,
clearClipboard: await this.stateService.getClearClipboard(),
clearClipboard: await firstValueFrom(this.autofillSettingsService.clearClipboardDelay$),
minimizeOnCopyToClipboard: await this.stateService.getMinimizeOnCopyToClipboard(),
enableFavicons: !(await this.stateService.getDisableFavicon()),
enableTray: await this.stateService.getEnableTray(),
@@ -563,7 +565,7 @@ export class SettingsComponent implements OnInit {
}
async saveClearClipboard() {
await this.stateService.setClearClipboard(this.form.value.clearClipboard);
await this.autofillSettingsService.setClearClipboardDelay(this.form.value.clearClipboard);
}
async saveAlwaysShowDock() {