diff --git a/.changeset/spicy-masks-sort.md b/.changeset/spicy-masks-sort.md new file mode 100644 index 00000000000..ad63ea52d4f --- /dev/null +++ b/.changeset/spicy-masks-sort.md @@ -0,0 +1,6 @@ +--- +"@firebase/firebase": patch +"@firebase/analytics": patch +--- + +Added Browser Extension check for Firebase Analytics. analytics.isSupported() will now return Promise for extension environments. diff --git a/packages/analytics/index.ts b/packages/analytics/index.ts index dd7d18f917a..b3544c68462 100644 --- a/packages/analytics/index.ts +++ b/packages/analytics/index.ts @@ -35,7 +35,8 @@ import { ERROR_FACTORY, AnalyticsError } from './src/errors'; import { isIndexedDBAvailable, validateIndexedDBOpenable, - areCookiesEnabled + areCookiesEnabled, + isBrowserExtension } from '@firebase/util'; import { name, version } from './package.json'; @@ -109,19 +110,25 @@ declare module '@firebase/app-types' { } /** - * this is a public static method provided to users that wraps three different checks: + * this is a public static method provided to users that wraps four different checks: * + * 1. check if it's not a browser extension environment. * 1. check if cookie is enabled in current browser. - * 2. check if IndexedDB is supported by the browser environment. - * 3. check if the current browser context is valid for using IndexedDB. + * 3. check if IndexedDB is supported by the browser environment. + * 4. check if the current browser context is valid for using IndexedDB. + * */ async function isSupported(): Promise { + if (isBrowserExtension()) { + return false; + } if (!areCookiesEnabled()) { return false; } if (!isIndexedDBAvailable()) { return false; } + try { const isDBOpenable: boolean = await validateIndexedDBOpenable(); return isDBOpenable; diff --git a/packages/analytics/src/errors.ts b/packages/analytics/src/errors.ts index 65aed9c847b..eccc437c362 100644 --- a/packages/analytics/src/errors.ts +++ b/packages/analytics/src/errors.ts @@ -25,7 +25,8 @@ export const enum AnalyticsError { INTEROP_COMPONENT_REG_FAILED = 'interop-component-reg-failed', INDEXED_DB_UNSUPPORTED = 'indexedDB-unsupported', INVALID_INDEXED_DB_CONTEXT = 'invalid-indexedDB-context', - COOKIES_NOT_ENABLED = 'cookies-not-enabled' + COOKIES_NOT_ENABLED = 'cookies-not-enabled', + INVALID_ANALYTICS_CONTEXT = 'invalid-analytics-context' } const ERRORS: ErrorMap = { @@ -50,7 +51,9 @@ const ERRORS: ErrorMap = { 'Wrap initialization of analytics in analytics.isSupported() ' + 'to prevent initialization in unsupported environments', [AnalyticsError.COOKIES_NOT_ENABLED]: - 'Cookies are not enabled in this browser environment. Analytics requires cookies to be enabled.' + 'Cookies are not enabled in this browser environment. Analytics requires cookies to be enabled.', + [AnalyticsError.INVALID_ANALYTICS_CONTEXT]: + 'Firebase Analytics is not supported in browser extensions.' }; interface ErrorParams { diff --git a/packages/analytics/src/factory.ts b/packages/analytics/src/factory.ts index c7a7d04308a..3335f9b03c7 100644 --- a/packages/analytics/src/factory.ts +++ b/packages/analytics/src/factory.ts @@ -41,7 +41,8 @@ import { FirebaseInstallations } from '@firebase/installations-types'; import { isIndexedDBAvailable, validateIndexedDBOpenable, - areCookiesEnabled + areCookiesEnabled, + isBrowserExtension } from '@firebase/util'; /** @@ -122,6 +123,9 @@ export function factory( app: FirebaseApp, installations: FirebaseInstallations ): FirebaseAnalytics { + if (isBrowserExtension()) { + throw ERROR_FACTORY.create(AnalyticsError.INVALID_ANALYTICS_CONTEXT); + } if (!areCookiesEnabled()) { throw ERROR_FACTORY.create(AnalyticsError.COOKIES_NOT_ENABLED); }