From b06f239c0dadafc9d15c348467d16cf29316de8f Mon Sep 17 00:00:00 2001 From: Radu-Cristian Popa Date: Tue, 21 May 2024 12:29:14 +0300 Subject: [PATCH 1/2] Pause monetization when losing window focus --- src/background/services/background.ts | 28 +++++++++++++++++++++++++ src/background/services/monetization.ts | 26 +++++++++++++++++++++++ src/shared/types.ts | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/background/services/background.ts b/src/background/services/background.ts index aecef1d7..cfa5bac7 100644 --- a/src/background/services/background.ts +++ b/src/background/services/background.ts @@ -29,6 +29,34 @@ export class Background { this.bindOnInstalled() this.bindMessageHandler() this.bindTabHandlers() + this.bindWindowHandlers() + } + + bindWindowHandlers() { + this.logger.debug('binding window') + this.browser.windows.onFocusChanged.addListener(async () => { + const windows = await this.browser.windows.getAll({ + windowTypes: ['normal', 'panel', 'popup'] + }) + windows.forEach(async (w) => { + const activeTab = ( + await this.browser.tabs.query({ windowId: w.id, active: true }) + )[0] + if (!activeTab?.id) return + + if (w.focused) { + this.logger.debug( + `Trying to resume monetization for window=${w.id}, activeTab=${activeTab.id} (URL: ${activeTab.url})` + ) + this.monetizationService.resumePaymentSessionsByTabId(activeTab.id) + } else { + this.logger.debug( + `Trying to pause monetization for window=${w.id}, activeTab=${activeTab.id} (URL: ${activeTab.url})` + ) + this.monetizationService.stopPaymentSessionsByTabId(activeTab.id) + } + }) + }) } bindTabHandlers() { diff --git a/src/background/services/monetization.ts b/src/background/services/monetization.ts index a77385fe..a898c3ab 100644 --- a/src/background/services/monetization.ts +++ b/src/background/services/monetization.ts @@ -66,6 +66,19 @@ export class MonetizationService { } } + stopPaymentSessionsByTabId(tabId: number) { + const sessions = this.sessions[tabId] + + if (!sessions) { + this.logger.debug(`No active sessions found for tab ${tabId}.`) + return + } + + for (const session of sessions.values()) { + session.stop() + } + } + stopPaymentSession( payload: StopMonetizationPayload, sender: Runtime.MessageSender @@ -98,6 +111,19 @@ export class MonetizationService { this.sessions[tabId].get(requestId)?.resume() } + resumePaymentSessionsByTabId(tabId: number) { + const sessions = this.sessions[tabId] + + if (!sessions) { + this.logger.debug(`No active sessions found for tab ${tabId}.`) + return + } + + for (const session of sessions.values()) { + session.resume() + } + } + async toggleWM() { const { enabled } = await this.storage.get(['enabled']) await this.storage.set({ enabled: !enabled }) diff --git a/src/shared/types.ts b/src/shared/types.ts index 0bb07659..423d4f2c 100644 --- a/src/shared/types.ts +++ b/src/shared/types.ts @@ -61,7 +61,7 @@ export type PopupStore = Omit< Storage, 'privateKey' | 'keyId' | 'exceptionList' | 'token' | 'grant' > & { - url: string | undefined + url: string | undefined } export type DeepNonNullable = { From 7fe2ed3af8a06df5f3c42c1eab62476cbf5f7823 Mon Sep 17 00:00:00 2001 From: Radu-Cristian Popa Date: Tue, 21 May 2024 12:30:46 +0300 Subject: [PATCH 2/2] Remove unnecessary log --- src/background/services/background.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/background/services/background.ts b/src/background/services/background.ts index cfa5bac7..19a4745d 100644 --- a/src/background/services/background.ts +++ b/src/background/services/background.ts @@ -33,7 +33,6 @@ export class Background { } bindWindowHandlers() { - this.logger.debug('binding window') this.browser.windows.onFocusChanged.addListener(async () => { const windows = await this.browser.windows.getAll({ windowTypes: ['normal', 'panel', 'popup']