1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-25 16:59:17 +01:00

PM-6558 Vault Onboarding Extension Check on Install (#8216)

updated browser runtime background to send hasBWInstalled message on installation
This commit is contained in:
Jason Ng 2024-03-06 10:48:27 -05:00 committed by GitHub
parent 06993594cc
commit e2a543506a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 56 additions and 8 deletions

View File

@ -1,5 +1,7 @@
import { mock } from "jest-mock-extended";
import { VaultOnboardingMessages } from "@bitwarden/common/vault/enums/vault-onboarding.enum";
import { postWindowMessage, sendExtensionRuntimeMessage } from "../spec/testing-utils";
describe("ContentMessageHandler", () => {
@ -30,9 +32,11 @@ describe("ContentMessageHandler", () => {
const mockPostMessage = jest.fn();
window.postMessage = mockPostMessage;
postWindowMessage({ command: "checkIfBWExtensionInstalled" });
postWindowMessage({ command: VaultOnboardingMessages.checkBwInstalled });
expect(mockPostMessage).toHaveBeenCalled();
expect(mockPostMessage).toHaveBeenCalledWith({
command: VaultOnboardingMessages.HasBwInstalled,
});
});
});

View File

@ -1,3 +1,5 @@
import { VaultOnboardingMessages } from "@bitwarden/common/vault/enums/vault-onboarding.enum";
import {
ContentMessageWindowData,
ContentMessageWindowEventHandlers,
@ -33,7 +35,7 @@ const windowMessageHandlers: ContentMessageWindowEventHandlers = {
* Handles the post to the web vault showing the extension has been installed
*/
function handleExtensionInstallCheck() {
window.postMessage({ command: "hasBWInstalled" });
window.postMessage({ command: VaultOnboardingMessages.HasBwInstalled });
}
/**

View File

@ -345,10 +345,34 @@ export default class RuntimeBackground {
if (await this.environmentService.hasManagedEnvironment()) {
await this.environmentService.setUrlsToManagedEnvironment();
}
await this.sendBwInstalledMessageToVault();
}
this.onInstalledReason = null;
}
}, 100);
}
async sendBwInstalledMessageToVault() {
try {
const vaultUrl = this.environmentService.getWebVaultUrl();
const urlObj = new URL(vaultUrl);
const tabs = await BrowserApi.tabsQuery({ url: `${urlObj.href}*` });
if (!tabs?.length) {
return;
}
for (const tab of tabs) {
await BrowserApi.executeScriptInTab(tab.id, {
file: "content/send-on-installed-message.js",
runAt: "document_end",
});
}
} catch (e) {
this.logService.error(`Error sending on installed message to vault: ${e}`);
}
}
}

View File

@ -0,0 +1,5 @@
import { VaultOnboardingMessages } from "@bitwarden/common/vault/enums/vault-onboarding.enum";
(function (globalContext) {
globalContext.postMessage({ command: VaultOnboardingMessages.HasBwInstalled });
})(window);

View File

@ -179,6 +179,7 @@ const mainConfig = {
"overlay/list": "./src/autofill/overlay/pages/list/bootstrap-autofill-overlay-list.ts",
"encrypt-worker": "../../libs/common/src/platform/services/cryptography/encrypt.worker.ts",
"content/lp-fileless-importer": "./src/tools/content/lp-fileless-importer.ts",
"content/send-on-installed-message": "./src/vault/content/send-on-installed-message.ts",
"content/lp-suppress-import-download": "./src/tools/content/lp-suppress-import-download.ts",
},
optimization: {

View File

@ -9,6 +9,7 @@ import { ConfigServiceAbstraction } from "@bitwarden/common/platform/abstraction
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { StateProvider } from "@bitwarden/common/platform/state";
import { VaultOnboardingMessages } from "@bitwarden/common/vault/enums/vault-onboarding.enum";
import { VaultOnboardingService as VaultOnboardingServiceAbstraction } from "./services/abstraction/vault-onboarding.service";
import { VaultOnboardingComponent } from "./vault-onboarding.component";
@ -143,7 +144,9 @@ describe("VaultOnboardingComponent", () => {
describe("checkBrowserExtension", () => {
it("should call getMessages when showOnboarding is true", () => {
const messageEventSubject = new Subject<MessageEvent>();
const messageEvent = new MessageEvent("message", { data: "hasBWInstalled" });
const messageEvent = new MessageEvent("message", {
data: VaultOnboardingMessages.HasBwInstalled,
});
const getMessagesSpy = jest.spyOn(component, "getMessages");
(component as any).showOnboarding = true;
@ -151,7 +154,9 @@ describe("VaultOnboardingComponent", () => {
messageEventSubject.next(messageEvent);
void fixture.whenStable().then(() => {
expect(window.postMessage).toHaveBeenCalledWith({ command: "checkIfBWExtensionInstalled" });
expect(window.postMessage).toHaveBeenCalledWith({
command: VaultOnboardingMessages.checkBwInstalled,
});
expect(getMessagesSpy).toHaveBeenCalled();
});
});
@ -168,7 +173,7 @@ describe("VaultOnboardingComponent", () => {
installExtension: false,
});
const eventData = { data: { command: "hasBWInstalled" } };
const eventData = { data: { command: VaultOnboardingMessages.HasBwInstalled } };
(component as any).showOnboarding = true;

View File

@ -19,6 +19,7 @@ import { Organization } from "@bitwarden/common/admin-console/models/domain/orga
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config.service.abstraction";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { VaultOnboardingMessages } from "@bitwarden/common/vault/enums/vault-onboarding.enum";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { LinkModule } from "@bitwarden/components";
@ -95,12 +96,12 @@ export class VaultOnboardingComponent implements OnInit, OnChanges, OnDestroy {
void this.getMessages(event);
});
window.postMessage({ command: "checkIfBWExtensionInstalled" });
window.postMessage({ command: VaultOnboardingMessages.checkBwInstalled });
}
}
async getMessages(event: any) {
if (event.data.command === "hasBWInstalled" && this.showOnboarding) {
if (event.data.command === VaultOnboardingMessages.HasBwInstalled && this.showOnboarding) {
const currentTasks = await firstValueFrom(this.onboardingTasks$);
const updatedTasks = {
createAccount: currentTasks.createAccount,

View File

@ -0,0 +1,6 @@
const VaultOnboardingMessages = {
HasBwInstalled: "hasBwInstalled",
checkBwInstalled: "checkIfBWExtensionInstalled",
} as const;
export { VaultOnboardingMessages };