Skip to content

Commit

Permalink
use messenger where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jiexi committed Jun 18, 2024
1 parent 0c3f000 commit ab7767b
Showing 1 changed file with 37 additions and 61 deletions.
98 changes: 37 additions & 61 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import debounce from 'debounce-stream';
import log from 'loglevel';
import browser from 'webextension-polyfill';
import { storeAsStream } from '@metamask/obs-store';
import { hasProperty, isObject } from '@metamask/utils';
import { isObject } from '@metamask/utils';
///: BEGIN:ONLY_INCLUDE_IF(snaps)
import { ApprovalType } from '@metamask/controller-utils';
///: END:ONLY_INCLUDE_IF
Expand Down Expand Up @@ -462,59 +462,48 @@ export async function loadStateFromPersistence() {
* which should only be tracked only after a user opts into metrics and connected to the dapp
*
* @param {string} origin - URL of visited dapp
* @param {object} connectSitePermissions - Permission state to get connected accounts
* @param {object} preferencesController - Preference Controller to get total created accounts
*/
function emitDappViewedMetricEvent(
origin,
connectSitePermissions,
preferencesController,
) {
function emitDappViewedMetricEvent(origin) {
const { metaMetricsId } = controller.metaMetricsController.state;
if (!shouldEmitDappViewedEvent(metaMetricsId)) {
return;
}

// A dapp may have other permissions than eth_accounts.
// Since we are only interested in dapps that use Ethereum accounts, we bail out otherwise.
if (!hasProperty(connectSitePermissions.permissions, 'eth_accounts')) {
const permissions = controller.controllerMessenger.call(
'PermissionController:getPermissions',
origin,
);
const numberOfConnectedAccounts =
permissions?.eth_accounts?.caveats[0]?.value.length;
if (!numberOfConnectedAccounts) {
return;
}

const numberOfTotalAccounts = Object.keys(
preferencesController.store.getState().identities,
).length;
const connectAccountsCollection =
connectSitePermissions.permissions.eth_accounts.caveats;
if (connectAccountsCollection) {
const numberOfConnectedAccounts = connectAccountsCollection[0].value.length;
controller.metaMetricsController.trackEvent({
event: MetaMetricsEventName.DappViewed,
category: MetaMetricsEventCategory.InpageProvider,
referrer: {
url: origin,
},
properties: {
is_first_visit: false,
number_of_accounts: numberOfTotalAccounts,
number_of_accounts_connected: numberOfConnectedAccounts,
},
});
}
const preferencesState = controller.controllerMessenger.call(
'PreferencesController:getState',
);
const numberOfTotalAccounts = Object.keys(preferencesState.identities).length;

controller.metaMetricsController.trackEvent({
event: MetaMetricsEventName.DappViewed,
category: MetaMetricsEventCategory.InpageProvider,
referrer: {
url: origin,
},
properties: {
is_first_visit: false,
number_of_accounts: numberOfTotalAccounts,
number_of_accounts_connected: numberOfConnectedAccounts,
},
});
}

/**
* Track dapp connection when loaded and permissioned
*
* @param {Port} remotePort - The port provided by a new context.
* @param {object} preferencesController - Preference Controller to get total created accounts
* @param {object} permissionController - Permission Controller to check if origin is permitted
*/
function trackDappView(
remotePort,
preferencesController,
permissionController,
) {
function trackDappView(remotePort) {
if (!remotePort.sender || !remotePort.sender.tab || !remotePort.sender.url) {
return;
}
Expand All @@ -526,21 +515,20 @@ function trackDappView(
if (!Object.keys(tabOriginMapping).includes(tabId)) {
tabOriginMapping[tabId] = origin;
}
const connectSitePermissions = permissionController.state.subjects[origin];
// when the dapp is not connected, connectSitePermissions is undefined
const isConnectedToDapp = connectSitePermissions !== undefined;

const isConnectedToDapp = controller.controllerMessenger.call(
'PermissionController:hasPermissions',
origin,
);

// when open a new tab, this event will trigger twice, only 2nd time is with dapp loaded
const isTabLoaded = remotePort.sender.tab.title !== 'New Tab';

// *** Emit DappViewed metric event when ***
// - refresh the dapp
// - open dapp in a new tab
if (isConnectedToDapp && isTabLoaded) {
emitDappViewedMetricEvent(
origin,
connectSitePermissions,
preferencesController,
);
emitDappViewedMetricEvent(origin);
}
}

Expand Down Expand Up @@ -741,11 +729,7 @@ export function setupController(
const url = new URL(remotePort.sender.url);
const { origin } = url;

trackDappView(
remotePort,
controller.preferencesController,
controller.permissionController,
);
trackDappView(remotePort);

remotePort.onMessage.addListener((msg) => {
if (
Expand Down Expand Up @@ -781,11 +765,7 @@ export function setupController(
const url = new URL(remotePort.sender.url);
const { origin } = url;

trackDappView(
remotePort,
controller.preferencesController,
controller.permissionController,
);
trackDappView(remotePort);

// TODO: remove this when we separate the legacy and multichain rpc pipelines
remotePort.onMessage.addListener((msg) => {
Expand Down Expand Up @@ -1029,11 +1009,7 @@ function onNavigateToTab() {
// when the dapp is not connected, connectSitePermissions is undefined
const isConnectedToDapp = connectSitePermissions !== undefined;
if (isConnectedToDapp) {
emitDappViewedMetricEvent(
currentOrigin,
connectSitePermissions,
controller.preferencesController,
);
emitDappViewedMetricEvent(currentOrigin);
}
}
}
Expand Down

0 comments on commit ab7767b

Please sign in to comment.