From 4ece57be34a18a795dc4d29c27f74d2f57c28af7 Mon Sep 17 00:00:00 2001 From: Jake Fink Date: Thu, 11 May 2023 13:39:37 -0400 Subject: [PATCH] [PM-1389] Fix SSO failure when vault timeout set to "Log Out" (#5286) * [PM-1389] don't include background page while reloading windows * [PM-1389] update sidebar action apis * [PM-1389] simplify return from getSidebarAction * [PM-1380] fix device type call after browser api change --- apps/browser/src/browser/browserApi.ts | 18 +++++++++++------- apps/browser/src/globals.d.ts | 14 +------------- apps/browser/src/listeners/update-badge.ts | 8 +++++--- .../services/browserPlatformUtils.service.ts | 17 +++-------------- 4 files changed, 20 insertions(+), 37 deletions(-) diff --git a/apps/browser/src/browser/browserApi.ts b/apps/browser/src/browser/browserApi.ts index 3dd87736840..22c8f68c524 100644 --- a/apps/browser/src/browser/browserApi.ts +++ b/apps/browser/src/browser/browserApi.ts @@ -1,3 +1,5 @@ +import { DeviceType } from "@bitwarden/common/enums/device-type.enum"; + import BrowserPlatformUtilsService from "../services/browserPlatformUtils.service"; import { TabMessage } from "../types/tab-messages"; @@ -217,7 +219,7 @@ export class BrowserApi { static reloadOpenWindows() { const views = chrome.extension.getViews() as Window[]; views - .filter((w) => w.location.href != null) + .filter((w) => w.location.href != null && !w.location.href.includes("background.html")) .forEach((w) => { w.location.reload(); }); @@ -253,11 +255,13 @@ export class BrowserApi { return BrowserApi.manifestVersion === 3 ? chrome.action : chrome.browserAction; } - static getSidebarAction(win: Window & typeof globalThis) { - return BrowserPlatformUtilsService.isSafari(win) - ? null - : typeof win.opr !== "undefined" && win.opr.sidebarAction - ? win.opr.sidebarAction - : win.chrome.sidebarAction; + static getSidebarAction( + win: Window & typeof globalThis + ): OperaSidebarAction | FirefoxSidebarAction | null { + const deviceType = BrowserPlatformUtilsService.getDevice(win); + if (deviceType !== DeviceType.FirefoxExtension && deviceType !== DeviceType.OperaExtension) { + return null; + } + return win.opr?.sidebarAction || browser.sidebarAction; } } diff --git a/apps/browser/src/globals.d.ts b/apps/browser/src/globals.d.ts index ace24cf3900..9c7be016b0b 100644 --- a/apps/browser/src/globals.d.ts +++ b/apps/browser/src/globals.d.ts @@ -105,25 +105,13 @@ type OperaSidebarAction = { * * @link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/sidebarAction */ -type FirefoxSidebarAction = Omit< - OperaSidebarAction, - | "setBadgeText" - | "getBadgeText" - | "setBadgeBackgroundColor" - | "getBadgeBackgroundColor" - | "onFocus" - | "onBlur" ->; +type FirefoxSidebarAction = typeof browser.sidebarAction; type Opera = { addons: OperaAddons; sidebarAction: OperaSidebarAction; }; -declare namespace chrome { - let sidebarAction: FirefoxSidebarAction | undefined; -} - interface Window { opr: Opera | undefined; opera: unknown; diff --git a/apps/browser/src/listeners/update-badge.ts b/apps/browser/src/listeners/update-badge.ts index cac57833dfe..14a93c474ff 100644 --- a/apps/browser/src/listeners/update-badge.ts +++ b/apps/browser/src/listeners/update-badge.ts @@ -220,10 +220,12 @@ export class UpdateBadge { return; } - if (this.useSyncApiCalls) { - this.sidebarAction.setIcon(options); + if (this.isOperaSidebar(this.sidebarAction)) { + await new Promise((resolve) => + (this.sidebarAction as OperaSidebarAction).setIcon(options, () => resolve()) + ); } else { - await new Promise((resolve) => this.sidebarAction.setIcon(options, () => resolve())); + await this.sidebarAction.setIcon(options); } } diff --git a/apps/browser/src/services/browserPlatformUtils.service.ts b/apps/browser/src/services/browserPlatformUtils.service.ts index 3120e740560..29086db9ec8 100644 --- a/apps/browser/src/services/browserPlatformUtils.service.ts +++ b/apps/browser/src/services/browserPlatformUtils.service.ts @@ -159,13 +159,12 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService return false; } - const sidebarView = this.sidebarViewName(); - const sidebarOpen = - sidebarView != null && chrome.extension.getViews({ type: sidebarView }).length > 0; - if (sidebarOpen) { + // Opera has "sidebar_panel" as a ViewType but doesn't currently work + if (this.isFirefox() && chrome.extension.getViews({ type: "sidebar" }).length > 0) { return true; } + // Opera sidebar has type of "tab" (will stick around for a while after closing sidebar) const tabOpen = chrome.extension.getViews({ type: "tab" }).length > 0; return tabOpen; } @@ -326,16 +325,6 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService return this.biometricCallback(); } - sidebarViewName(): string { - if (this.win.chrome.sidebarAction && this.isFirefox()) { - return "sidebar"; - } else if (this.isOpera() && typeof opr !== "undefined" && opr.sidebarAction) { - return "sidebar_panel"; - } - - return null; - } - supportsSecureStorage(): boolean { return false; }