Skip to content

Commit

Permalink
PM-6558 Vault Onboarding Extension Check on Install (#8216)
Browse files Browse the repository at this point in the history
updated browser runtime background to send hasBWInstalled message on installation
  • Loading branch information
Jingo88 authored Mar 6, 2024
1 parent 0699359 commit e2a5435
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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,
});
});
});

Expand Down
4 changes: 3 additions & 1 deletion apps/browser/src/autofill/content/content-message-handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { VaultOnboardingMessages } from "@bitwarden/common/vault/enums/vault-onboarding.enum";

import {
ContentMessageWindowData,
ContentMessageWindowEventHandlers,
Expand Down Expand Up @@ -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 });
}

/**
Expand Down
24 changes: 24 additions & 0 deletions apps/browser/src/background/runtime.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
}
5 changes: 5 additions & 0 deletions apps/browser/src/vault/content/send-on-installed-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { VaultOnboardingMessages } from "@bitwarden/common/vault/enums/vault-onboarding.enum";

(function (globalContext) {
globalContext.postMessage({ command: VaultOnboardingMessages.HasBwInstalled });
})(window);
1 change: 1 addition & 0 deletions apps/browser/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -143,15 +144,19 @@ 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;
component.checkForBrowserExtension();
messageEventSubject.next(messageEvent);

void fixture.whenStable().then(() => {
expect(window.postMessage).toHaveBeenCalledWith({ command: "checkIfBWExtensionInstalled" });
expect(window.postMessage).toHaveBeenCalledWith({
command: VaultOnboardingMessages.checkBwInstalled,
});
expect(getMessagesSpy).toHaveBeenCalled();
});
});
Expand All @@ -168,7 +173,7 @@ describe("VaultOnboardingComponent", () => {
installExtension: false,
});

const eventData = { data: { command: "hasBWInstalled" } };
const eventData = { data: { command: VaultOnboardingMessages.HasBwInstalled } };

(component as any).showOnboarding = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions libs/common/src/vault/enums/vault-onboarding.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const VaultOnboardingMessages = {
HasBwInstalled: "hasBwInstalled",
checkBwInstalled: "checkIfBWExtensionInstalled",
} as const;

export { VaultOnboardingMessages };

0 comments on commit e2a5435

Please sign in to comment.