From 524123ac01346936445935c78f72253836d4e59e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Tue, 10 Oct 2023 19:13:50 +0200 Subject: [PATCH] Add lint to try to limit reappearance of Safari memory leaks (#6382) --- .eslintrc.json | 17 +++++++++++++++++ .../browser/src/platform/browser/browser-api.ts | 2 ++ 2 files changed, 19 insertions(+) diff --git a/.eslintrc.json b/.eslintrc.json index d6c329a94d..44b4e09adf 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -179,6 +179,23 @@ "rules": { "no-restricted-imports": ["error", { "patterns": ["@bitwarden/vault/*", "src/**/*"] }] } + }, + { + "files": ["apps/browser/src/**/*.ts", "libs/**/*.ts"], + "excludedFiles": "apps/browser/src/autofill/{content,notification}/**/*.ts", + "rules": { + "no-restricted-syntax": [ + "error", + { + "message": "Using addListener in the browser popup produces a memory leak in Safari, use `BrowserApi.messageListener` instead", + "selector": "CallExpression > [object.object.object.name='chrome'][object.object.property.name='runtime'][object.property.name='onMessage'][property.name='addListener']" + }, + { + "message": "Using addListener in the browser popup produces a memory leak in Safari, use `BrowserApi.storageChangeListener` instead", + "selector": "CallExpression > [object.object.object.name='chrome'][object.object.property.name='storage'][object.property.name='onChanged'][property.name='addListener']" + } + ] + } } ] } diff --git a/apps/browser/src/platform/browser/browser-api.ts b/apps/browser/src/platform/browser/browser-api.ts index b71b8b80b6..5d0c2f1a51 100644 --- a/apps/browser/src/platform/browser/browser-api.ts +++ b/apps/browser/src/platform/browser/browser-api.ts @@ -208,6 +208,7 @@ export class BrowserApi { name: string, callback: (message: any, sender: chrome.runtime.MessageSender, response: any) => void ) { + // eslint-disable-next-line no-restricted-syntax chrome.runtime.onMessage.addListener(callback); if (BrowserApi.isSafariApi && !BrowserApi.isBackgroundPage(window)) { @@ -219,6 +220,7 @@ export class BrowserApi { static storageChangeListener( callback: Parameters[0] ) { + // eslint-disable-next-line no-restricted-syntax chrome.storage.onChanged.addListener(callback); if (BrowserApi.isSafariApi && !BrowserApi.isBackgroundPage(window)) {