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

[EC-598] feat: close popout directly from bg script

This commit is contained in:
Andreas Coroiu
2023-04-28 09:20:42 +02:00
parent 2d390f0492
commit cc31f1efc0
3 changed files with 71 additions and 45 deletions

View File

@@ -103,8 +103,6 @@ export class Fido2Component implements OnInit, OnDestroy {
return cipher.decrypt(); return cipher.decrypt();
}) })
); );
} else if (message.type === "CloseRequest") {
window.close();
} }
return { return {

View File

@@ -3,6 +3,16 @@ import { fromEvent, Subscription } from "rxjs";
import { BrowserApi } from "../../browser/browserApi"; import { BrowserApi } from "../../browser/browserApi";
export type Popout =
| {
type: "window";
window: chrome.windows.Window;
}
| {
type: "tab";
tab: chrome.tabs.Tab;
};
@Injectable() @Injectable()
export class PopupUtilsService { export class PopupUtilsService {
private unloadSubscription: Subscription; private unloadSubscription: Subscription;
@@ -45,7 +55,8 @@ export class PopupUtilsService {
} }
} }
popOut(win: Window, href: string = null, options: { center?: boolean } = {}): void { popOut(win: Window, href: string = null, options: { center?: boolean } = {}): Promise<Popout> {
return new Promise((resolve, reject) => {
if (href === null) { if (href === null) {
href = win.location.href; href = win.location.href;
} }
@@ -67,14 +78,17 @@ export class PopupUtilsService {
const height = Math.round(bodyRect.height || 600); const height = Math.round(bodyRect.height || 600);
const top = options.center ? Math.round((screen.height - height) / 2) : undefined; const top = options.center ? Math.round((screen.height - height) / 2) : undefined;
const left = options.center ? Math.round((screen.width - width) / 2) : undefined; const left = options.center ? Math.round((screen.width - width) / 2) : undefined;
chrome.windows.create({ chrome.windows.create(
{
url: href, url: href,
type: "popup", type: "popup",
width, width,
height, height,
top, top,
left, left,
}); },
(window) => resolve({ type: "window", window })
);
if (win && this.inPopup(win)) { if (win && this.inPopup(win)) {
BrowserApi.closePopup(win); BrowserApi.closePopup(win);
@@ -84,10 +98,26 @@ export class PopupUtilsService {
.replace("uilocation=popup", "uilocation=tab") .replace("uilocation=popup", "uilocation=tab")
.replace("uilocation=popout", "uilocation=tab") .replace("uilocation=popout", "uilocation=tab")
.replace("uilocation=sidebar", "uilocation=tab"); .replace("uilocation=sidebar", "uilocation=tab");
chrome.tabs.create({ chrome.tabs.create(
{
url: href, url: href,
},
(tab) => resolve({ type: "tab", tab })
);
} else {
reject(new Error("Cannot open tab or window"));
}
}); });
} }
closePopOut(popout: Popout): Promise<void> {
return new Promise((resolve) => {
if (popout.type === "window") {
chrome.windows.remove(popout.window.id, resolve);
} else {
chrome.tabs.remove(popout.tab.id, resolve);
}
});
} }
/** /**

View File

@@ -20,7 +20,7 @@ import {
import { Utils } from "@bitwarden/common/misc/utils"; import { Utils } from "@bitwarden/common/misc/utils";
import { BrowserApi } from "../../browser/browserApi"; import { BrowserApi } from "../../browser/browserApi";
import { PopupUtilsService } from "../../popup/services/popup-utils.service"; import { Popout, PopupUtilsService } from "../../popup/services/popup-utils.service";
const BrowserFido2MessageName = "BrowserFido2UserInterfaceServiceMessage"; const BrowserFido2MessageName = "BrowserFido2UserInterfaceServiceMessage";
@@ -89,9 +89,6 @@ export type BrowserFido2Message = { sessionId: string } & (
type: "AbortResponse"; type: "AbortResponse";
fallbackRequested: boolean; fallbackRequested: boolean;
} }
| {
type: "CloseRequest";
}
); );
export class BrowserFido2UserInterfaceService implements Fido2UserInterfaceServiceAbstraction { export class BrowserFido2UserInterfaceService implements Fido2UserInterfaceServiceAbstraction {
@@ -120,6 +117,7 @@ export class BrowserFido2UserInterfaceSession implements Fido2UserInterfaceSessi
); );
private connected$ = new BehaviorSubject(false); private connected$ = new BehaviorSubject(false);
private destroy$ = new Subject<void>(); private destroy$ = new Subject<void>();
private popout?: Popout;
private constructor( private constructor(
private readonly popupUtilsService: PopupUtilsService, private readonly popupUtilsService: PopupUtilsService,
@@ -252,7 +250,7 @@ export class BrowserFido2UserInterfaceSession implements Fido2UserInterfaceSessi
} }
async close() { async close() {
await this.send({ type: "CloseRequest", sessionId: this.sessionId }); this.popupUtilsService.closePopOut(this.popout);
this.closed = true; this.closed = true;
this.destroy$.next(); this.destroy$.next();
this.destroy$.complete(); this.destroy$.complete();
@@ -290,7 +288,7 @@ export class BrowserFido2UserInterfaceSession implements Fido2UserInterfaceSessi
} }
const queryParams = new URLSearchParams({ sessionId: this.sessionId }).toString(); const queryParams = new URLSearchParams({ sessionId: this.sessionId }).toString();
this.popupUtilsService.popOut( this.popout = await this.popupUtilsService.popOut(
null, null,
`popup/index.html?uilocation=popout#/fido2?${queryParams}`, `popup/index.html?uilocation=popout#/fido2?${queryParams}`,
{ center: true } { center: true }