Skip to content

Commit

Permalink
[PM-650] Split ClipboardService from PlatformUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Jan 15, 2025
1 parent 55e4b5e commit 3227b7f
Show file tree
Hide file tree
Showing 85 changed files with 931 additions and 738 deletions.
10 changes: 5 additions & 5 deletions apps/browser/src/autofill/background/overlay.background.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "@bitwarden/common/autofill/services/domain-settings.service";
import { InlineMenuVisibilitySetting } from "@bitwarden/common/autofill/types";
import { NeverDomains } from "@bitwarden/common/models/domain/domain-service";
import { ClipboardService } from "@bitwarden/common/platform/abstractions/clipboard.service";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import {
EnvironmentService,
Expand All @@ -40,7 +41,6 @@ import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { Fido2CredentialView } from "@bitwarden/common/vault/models/view/fido2-credential.view";

import { BrowserApi } from "../../platform/browser/browser-api";
import { BrowserPlatformUtilsService } from "../../platform/services/platform-utils/browser-platform-utils.service";
import {
AutofillOverlayElement,
AutofillOverlayPort,
Expand Down Expand Up @@ -102,7 +102,7 @@ describe("OverlayBackground", () => {
let inlineMenuVisibilityMock$: BehaviorSubject<InlineMenuVisibilitySetting>;
let autofillSettingsService: MockProxy<AutofillSettingsService>;
let i18nService: MockProxy<I18nService>;
let platformUtilsService: MockProxy<BrowserPlatformUtilsService>;
let clipboardService: MockProxy<ClipboardService>;
let enablePasskeysMock$: BehaviorSubject<boolean>;
let vaultSettingsServiceMock: MockProxy<VaultSettingsService>;
let fido2ActiveRequestManager: Fido2ActiveRequestManager;
Expand Down Expand Up @@ -181,7 +181,7 @@ describe("OverlayBackground", () => {
autofillSettingsService = mock<AutofillSettingsService>();
autofillSettingsService.inlineMenuVisibility$ = inlineMenuVisibilityMock$;
i18nService = mock<I18nService>();
platformUtilsService = mock<BrowserPlatformUtilsService>();
clipboardService = mock<ClipboardService>();
enablePasskeysMock$ = new BehaviorSubject(true);
vaultSettingsServiceMock = mock<VaultSettingsService>();
vaultSettingsServiceMock.enablePasskeys$ = enablePasskeysMock$;
Expand All @@ -200,7 +200,7 @@ describe("OverlayBackground", () => {
domainSettingsService,
autofillSettingsService,
i18nService,
platformUtilsService,
clipboardService,
vaultSettingsServiceMock,
fido2ActiveRequestManager,
inlineMenuFieldQualificationService,
Expand Down Expand Up @@ -3300,7 +3300,7 @@ describe("OverlayBackground", () => {
]);
autofillService.isPasswordRepromptRequired.mockResolvedValue(false);
const copyToClipboardSpy = jest
.spyOn(overlayBackground["platformUtilsService"], "copyToClipboard")
.spyOn(overlayBackground["clipboardService"], "copyToClipboard")
.mockImplementation();
autofillService.doAutoFill.mockResolvedValue("totp-code");

Expand Down
10 changes: 4 additions & 6 deletions apps/browser/src/autofill/background/overlay.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import { DomainSettingsService } from "@bitwarden/common/autofill/services/domai
import { InlineMenuVisibilitySetting } from "@bitwarden/common/autofill/types";
import { parseYearMonthExpiry } from "@bitwarden/common/autofill/utils";
import { NeverDomains } from "@bitwarden/common/models/domain/domain-service";
import { ClipboardService } from "@bitwarden/common/platform/abstractions/clipboard.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import {
Fido2ActiveRequestEvents,
Fido2ActiveRequestManager,
} from "@bitwarden/common/platform/abstractions/fido2/fido2-active-request-manager.abstraction";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { ThemeStateService } from "@bitwarden/common/platform/theming/theme-state.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
Expand Down Expand Up @@ -219,7 +219,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
private domainSettingsService: DomainSettingsService,
private autofillSettingsService: AutofillSettingsServiceAbstraction,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
private clipboardService: ClipboardService,
private vaultSettingsService: VaultSettingsService,
private fido2ActiveRequestManager: Fido2ActiveRequestManager,
private inlineMenuFieldQualificationService: InlineMenuFieldQualificationService,
Expand Down Expand Up @@ -1116,9 +1116,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
this.updateLastUsedInlineMenuCipher(inlineMenuCipherId, cipher);

if (cipher.login?.totp) {
this.platformUtilsService.copyToClipboard(
await this.totpService.getCode(cipher.login.totp),
);
this.clipboardService.copyToClipboard(await this.totpService.getCode(cipher.login.totp));
}
return;
}
Expand All @@ -1144,7 +1142,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
});

if (totpCode) {
this.platformUtilsService.copyToClipboard(totpCode);
this.clipboardService.copyToClipboard(totpCode);
}

this.updateLastUsedInlineMenuCipher(inlineMenuCipherId, cipher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
DefaultDomainSettingsService,
DomainSettingsService,
} from "@bitwarden/common/autofill/services/domain-settings.service";
import { ClipboardService } from "@bitwarden/common/platform/abstractions/clipboard.service";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import {
EnvironmentService,
Expand All @@ -34,7 +35,6 @@ import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { CipherService } from "@bitwarden/common/vault/services/cipher.service";

import { BrowserApi } from "../../../platform/browser/browser-api";
import { BrowserPlatformUtilsService } from "../../../platform/services/platform-utils/browser-platform-utils.service";
import {
AutofillOverlayElement,
AutofillOverlayPort,
Expand Down Expand Up @@ -76,7 +76,7 @@ describe("OverlayBackground", () => {
);
const autofillSettingsService = mock<AutofillSettingsService>();
const i18nService = mock<I18nService>();
const platformUtilsService = mock<BrowserPlatformUtilsService>();
const clipboardService = mock<ClipboardService>();
const themeStateService = mock<ThemeStateService>();
const initOverlayElementPorts = async (options = { initList: true, initButton: true }) => {
const { initList, initButton } = options;
Expand Down Expand Up @@ -108,7 +108,7 @@ describe("OverlayBackground", () => {
domainSettingsService,
autofillSettingsService,
i18nService,
platformUtilsService,
clipboardService,
themeStateService,
);

Expand Down Expand Up @@ -1372,7 +1372,7 @@ describe("OverlayBackground", () => {
]);
isPasswordRepromptRequiredSpy.mockResolvedValue(false);
const copyToClipboardSpy = jest
.spyOn(overlayBackground["platformUtilsService"], "copyToClipboard")
.spyOn(overlayBackground["clipboardService"], "copyToClipboard")
.mockImplementation();
doAutoFillSpy.mockReturnValueOnce("totp-code");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { SHOW_AUTOFILL_BUTTON } from "@bitwarden/common/autofill/constants";
import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service";
import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service";
import { InlineMenuVisibilitySetting } from "@bitwarden/common/autofill/types";
import { ClipboardService } from "@bitwarden/common/platform/abstractions/clipboard.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { ThemeStateService } from "@bitwarden/common/platform/theming/theme-state.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
Expand Down Expand Up @@ -104,7 +104,7 @@ class LegacyOverlayBackground implements OverlayBackgroundInterface {
private domainSettingsService: DomainSettingsService,
private autofillSettingsService: AutofillSettingsServiceAbstraction,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
private clipboardService: ClipboardService,
private themeStateService: ThemeStateService,
) {}

Expand Down Expand Up @@ -256,7 +256,7 @@ class LegacyOverlayBackground implements OverlayBackgroundInterface {
});

if (totpCode) {
this.platformUtilsService.copyToClipboard(totpCode);
this.clipboardService.copyToClipboard(totpCode);
}

this.overlayLoginCiphers = new Map([[overlayCipherId, cipher], ...this.overlayLoginCiphers]);
Expand Down
24 changes: 14 additions & 10 deletions apps/browser/src/background/main.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ProcessReloadServiceAbstraction } from "@bitwarden/common/key-management/abstractions/process-reload.service";
import { DefaultProcessReloadService } from "@bitwarden/common/key-management/services/default-process-reload.service";
import { AppIdService as AppIdServiceAbstraction } from "@bitwarden/common/platform/abstractions/app-id.service";
import { ClipboardService } from "@bitwarden/common/platform/abstractions/clipboard.service";
import { ConfigApiServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config-api.service.abstraction";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@bitwarden/common/platform/abstractions/crypto-function.service";
Expand Down Expand Up @@ -251,14 +252,14 @@ import { ChromeMessageSender } from "../platform/messaging/chrome-message.sender
import { OffscreenDocumentService } from "../platform/offscreen-document/abstractions/offscreen-document";
import { DefaultOffscreenDocumentService } from "../platform/offscreen-document/offscreen-document.service";
import { BrowserTaskSchedulerService } from "../platform/services/abstractions/browser-task-scheduler.service";
import { BrowserClipboardService } from "../platform/services/browser-clipboard.service";
import { BrowserEnvironmentService } from "../platform/services/browser-environment.service";
import BrowserLocalStorageService from "../platform/services/browser-local-storage.service";
import BrowserMemoryStorageService from "../platform/services/browser-memory-storage.service";
import { BrowserScriptInjectorService } from "../platform/services/browser-script-injector.service";
import I18nService from "../platform/services/i18n.service";
import { LocalBackedSessionStorageService } from "../platform/services/local-backed-session-storage.service";
import { BackgroundPlatformUtilsService } from "../platform/services/platform-utils/background-platform-utils.service";
import { BrowserPlatformUtilsService } from "../platform/services/platform-utils/browser-platform-utils.service";
import { PopupViewCacheBackgroundService } from "../platform/services/popup-view-cache-background.service";
import { BrowserSdkClientFactory } from "../platform/services/sdk/browser-sdk-client-factory";
import { BackgroundTaskSchedulerService } from "../platform/services/task-scheduler/background-task-scheduler.service";
Expand All @@ -285,6 +286,7 @@ export default class MainBackground {
largeObjectMemoryStorageForStateProviders: AbstractStorageService & ObservableStorageService;
i18nService: I18nServiceAbstraction;
platformUtilsService: PlatformUtilsServiceAbstraction;
clipboardService: ClipboardService;
logService: LogServiceAbstraction;
keyGenerationService: KeyGenerationServiceAbstraction;
keyService: KeyServiceAbstraction;
Expand Down Expand Up @@ -457,8 +459,10 @@ export default class MainBackground {

this.offscreenDocumentService = new DefaultOffscreenDocumentService(this.logService);

this.platformUtilsService = new BackgroundPlatformUtilsService(
this.messagingService,
this.platformUtilsService = new BackgroundPlatformUtilsService(this.messagingService, self);

this.clipboardService = new BrowserClipboardService(
this.platformUtilsService,
(clipboardValue, clearMs) => this.clearClipboard(clipboardValue, clearMs),
self,
this.offscreenDocumentService,
Expand Down Expand Up @@ -1070,7 +1074,7 @@ export default class MainBackground {
};

this.systemService = new SystemService(
this.platformUtilsService,
this.clipboardService,
this.autofillSettingsService,
this.taskSchedulerService,
);
Expand Down Expand Up @@ -1105,7 +1109,7 @@ export default class MainBackground {
this.runtimeBackground = new RuntimeBackground(
this,
this.autofillService,
this.platformUtilsService as BrowserPlatformUtilsService,
this.clipboardService,
this.notificationsService,
this.autofillSettingsService,
this.processReloadService,
Expand Down Expand Up @@ -1180,11 +1184,11 @@ export default class MainBackground {
);

const contextMenuClickedHandler = new ContextMenuClickedHandler(
(options) => this.platformUtilsService.copyToClipboard(options.text),
(options) => this.clipboardService.copyToClipboard(options.text),
async (_tab) => {
const options = (await this.passwordGenerationService.getOptions())?.[0] ?? {};
const password = await this.passwordGenerationService.generatePassword(options);
this.platformUtilsService.copyToClipboard(password);
this.clipboardService.copyToClipboard(password);
// 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.passwordGenerationService.addHistory(password);
Expand Down Expand Up @@ -1622,7 +1626,7 @@ export default class MainBackground {
this.domainSettingsService,
this.autofillSettingsService,
this.i18nService,
this.platformUtilsService,
this.clipboardService,
this.themeStateService,
);
} else {
Expand All @@ -1635,7 +1639,7 @@ export default class MainBackground {
this.domainSettingsService,
this.autofillSettingsService,
this.i18nService,
this.platformUtilsService,
this.clipboardService,
this.vaultSettingsService,
this.fido2ActiveRequestManager,
this.inlineMenuFieldQualificationService,
Expand Down Expand Up @@ -1663,7 +1667,7 @@ export default class MainBackground {

generatePasswordToClipboard = async () => {
const password = await this.generatePassword();
this.platformUtilsService.copyToClipboard(password);
this.clipboardService.copyToClipboard(password);
await this.addPasswordToHistory(password);
};

Expand Down
10 changes: 5 additions & 5 deletions apps/browser/src/background/runtime.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/s
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ProcessReloadServiceAbstraction } from "@bitwarden/common/key-management/abstractions/process-reload.service";
import { ClipboardService } from "@bitwarden/common/platform/abstractions/clipboard.service";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
Expand All @@ -28,7 +29,6 @@ import { LockedVaultPendingNotificationsData } from "../autofill/background/abst
import { AutofillService } from "../autofill/services/abstractions/autofill.service";
import { BrowserApi } from "../platform/browser/browser-api";
import { BrowserEnvironmentService } from "../platform/services/browser-environment.service";
import { BrowserPlatformUtilsService } from "../platform/services/platform-utils/browser-platform-utils.service";

import MainBackground from "./main.background";

Expand All @@ -41,7 +41,7 @@ export default class RuntimeBackground {
constructor(
private main: MainBackground,
private autofillService: AutofillService,
private platformUtilsService: BrowserPlatformUtilsService,
private clipboardService: ClipboardService,
private notificationsService: NotificationsService,
private autofillSettingsService: AutofillSettingsServiceAbstraction,
private processReloadSerivce: ProcessReloadServiceAbstraction,
Expand Down Expand Up @@ -143,7 +143,7 @@ export default class RuntimeBackground {
msg.sender === ExtensionCommand.AutofillCommand,
);
if (totpCode != null) {
this.platformUtilsService.copyToClipboard(totpCode);
this.clipboardService.copyToClipboard(totpCode);
}
break;
}
Expand Down Expand Up @@ -351,7 +351,7 @@ export default class RuntimeBackground {
});
break;
case "getClickedElementResponse":
this.platformUtilsService.copyToClipboard(msg.identifier);
this.clipboardService.copyToClipboard(msg.identifier);
break;
case "switchAccount": {
await this.main.switchAccount(msg.userId);
Expand All @@ -374,7 +374,7 @@ export default class RuntimeBackground {
});

if (totpCode != null) {
this.platformUtilsService.copyToClipboard(totpCode);
this.clipboardService.copyToClipboard(totpCode);
}

// reset
Expand Down
Loading

0 comments on commit 3227b7f

Please sign in to comment.