Skip to content

Browser Extension Check for Analytics Module #3555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/spicy-masks-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@firebase/firebase": patch
"@firebase/analytics": patch
---

Added Browser Extension check for Firebase Analytics. analytics.isSupported() will now return Promise<false> for extension environments.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: backticks around analytics.isSupported() and Promise<false> will make it look nicer in markdown.

15 changes: 11 additions & 4 deletions packages/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<boolean> {
if (isBrowserExtension()) {
return false;
}
if (!areCookiesEnabled()) {
return false;
}
if (!isIndexedDBAvailable()) {
return false;
}

try {
const isDBOpenable: boolean = await validateIndexedDBOpenable();
return isDBOpenable;
Expand Down
7 changes: 5 additions & 2 deletions packages/analytics/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnalyticsError> = {
Expand All @@ -50,7 +51,9 @@ const ERRORS: ErrorMap<AnalyticsError> = {
'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 {
Expand Down
6 changes: 5 additions & 1 deletion packages/analytics/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import { FirebaseInstallations } from '@firebase/installations-types';
import {
isIndexedDBAvailable,
validateIndexedDBOpenable,
areCookiesEnabled
areCookiesEnabled,
isBrowserExtension
} from '@firebase/util';

/**
Expand Down Expand Up @@ -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);
}
Expand Down